jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _plot_mps_batch_size_vs_metric_by_num_tasks(self, df, metric, title=None, xlabel=None, ylabel=None, filter_df=None, suffix=None, global_ymax=False):
        """
        Throughput graph:
            Y-axis = throughput
            X-axis (major) = batch-size (larger impact on throughput)
            X-axis (minor) = num_tasks (lesser impact on throughput)

        Latency graph:
            Y-axis = latency samples (mean/std across all processes)
            X-axis (major) = batch-size (larger impact on latency)
            X-axis (minor) = num_tasks (lesser impact on latency)
        """
        if df is None:
            return
        df = copy.copy(df)
        assert metric in df

        # df = self._add_config(df, df_type='trtexec')

        global_df = df
        if filter_df is not None:
            df = filter_df(df)

        sns.set(style="whitegrid")
        g = sns.factorplot(x="batch_size",
                        y=metric,
                        # data=df,
                        hue="config",
                        data=df,
                        # hue="num_threads", data=df,
                        # hue=col_titles["num_threads"], data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        if ylabel is not None:
            g.set_ylabels(ylabel)
        if xlabel is not None:
            g.set_xlabels(xlabel)
        if title is not None:
            g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        if global_ymax:
            new_ymax = global_df[metric].max()
            ymin, ymax = g.ax.get_ylim()
            g.ax.set_ylim((ymin, max(ymax, new_ymax)))


        if suffix is None:
            suffix = ""

        save_plot(df, _j(self.args['mps_dir'], f'mps_batch_size_vs_{metric}_by_num_tasks{suffix}.svg'))
//  [python]
def _plot_mps_batch_size_vs_metric_by_num_tasks(self, df, metric, title=None, xlabel=None, ylabel=None, filter_df=None, suffix=None, global_ymax=False):
        """
        Throughput graph:
            Y-axis = throughput
            X-axis (major) = batch-size (larger impact on throughput)
            X-axis (minor) = num_tasks (lesser impact on throughput)

        Latency graph:
            Y-axis = latency samples (mean/std across all processes)
            X-axis (major) = batch-size (larger impact on latency)
            X-axis (minor) = num_tasks (lesser impact on latency)
        """
        if df is None:
            return
        df = copy.copy(df)
        assert metric in df

        # df = self._add_config(df, df_type='trtexec')

        global_df = df
        if filter_df is not None:
            df = filter_df(df)

        sns.set(style="whitegrid")
        g = sns.catplot(x="batch_size",
                        y=metric,
                        # data=df,
                        hue="config",
                        data=df,
                        # hue="num_threads", data=df,
                        # hue=col_titles["num_threads"], data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        if ylabel is not None:
            g.set_ylabels(ylabel)
        if xlabel is not None:
            g.set_xlabels(xlabel)
        if title is not None:
            g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        if global_ymax:
            new_ymax = global_df[metric].max()
            ymin, ymax = g.ax.get_ylim()
            g.ax.set_ylim((ymin, max(ymax, new_ymax)))


        if suffix is None:
            suffix = ""

        save_plot(df, _j(self.args['mps_dir'], f'mps_batch_size_vs_{metric}_by_num_tasks{suffix}.svg'))
//  [python]
def _plot_simulator_vs_steptime(self):
        """
        x = simulator
        y = mean step time (seconds)

        :return:
        """
        if self.simulator_raw_df is None:
            return

        plot_df = pd.DataFrame(columns=['simulator', 'step_time_ms'])

        if self.simulator_raw_df is not None:
            df = copy.copy(self.simulator_raw_df)
            df['step_time_ms'] = df['step_time_sec'] * 1000
            plot_df = plot_df.append(df[plot_df.columns])

        plot_df.sort_values(by=['simulator', 'step_time_ms'], inplace=True)

        sns.set(style="whitegrid")
        # Boxplot?
        # CDF?
        g = sns.barplot(x="simulator", y="step_time_ms",
                        data=plot_df,
                        palette="muted")
        g.despine(left=True)
        g.set_ylabels(STEP_LATENCY_Y_LABEL)
        g.set_xlabels(SIMULATOR_X_LABEL)
        title = "Simulation latency"
        g.figure.suptitle(title)
        g.figure.subplots_adjust(top=0.90)
        g.set_xticklabels(
            g.get_xticklabels(),
            rotation=self.arg('rotation', 15),
        )

        save_plot(plot_df, _j(self.args['simulator_dir'], f'simulator_vs_latency.svg'))
//  [python]
def _plot_simulator_vs_steptime(self):
        """
        x = simulator
        y = mean step time (seconds)

        :return:
        """
        if self.simulator_raw_df is None:
            return

        plot_df = pd.DataFrame(columns=['simulator', 'step_time_ms'])

        if self.simulator_raw_df is not None:
            df = copy.copy(self.simulator_raw_df)
            df['step_time_ms'] = df['step_time_sec'] * 1000
            plot_df = plot_df.append(df[plot_df.columns])

        plot_df.sort_values(by=['simulator', 'step_time_ms'], inplace=True)

        sns.set(style="whitegrid")
        # Boxplot?
        # CDF?
        g = sns.catplot(x="simulator", y="step_time_ms",
                        data=plot_df,
                        kind="bar",
                        palette="muted")
        g.despine(left=True)
        g.set_ylabels(STEP_LATENCY_Y_LABEL)
        g.set_xlabels(SIMULATOR_X_LABEL)
        title = "Simulation latency"
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 15),
        )

        save_plot(plot_df, _j(self.args['simulator_dir'], f'simulator_vs_latency.svg'))
//  [python]
def _plot_streams_vs_trt_metric(self, trt_metric, batch_size, title=None, ylabel=None, alias=None, cuda_graph=None, suffix=None):
        if self.trtexec_df is None:
            return
        if alias is None:
            alias = trt_metric
        df = copy.copy(self.trtexec_df)
        """
        WANT:
        x_field: batch_size
        y_field: metric_value
        group_field: num_threads
        """

        df = df[df['batch_size'] == batch_size]

        # df = keep_cupti_metric(df, cupti_metric)

        # titled_df = copy.copy(df)
        # col_titles = {
        #     'num_threads': 'Number of threads',
        # }
        # titled_df.rename(columns=col_titles, inplace=True)

        df = self._add_config(df, df_type='trtexec')

        sns.set(style="whitegrid")
        plot_kwargs = dict(
            x="streams",
            y=trt_metric,
            kind="bar",
            palette="muted",
        )
        if cuda_graph is None:
            plot_kwargs.update(dict(
                hue="config",
            ))
        elif cuda_graph:
            df = df[df['cuda_graph']]
        else:
            df = df[~ df['cuda_graph']]
        plot_kwargs.update(dict(
            data=df,
        ))
        g = sns.factorplot(**plot_kwargs)
        g.despine(left=True)
        if ylabel is None:
            ylabel = TRT_METRIC_YLABELS[trt_metric]
        g.set_ylabels(ylabel)
        # if xlabel is not None:
        g.set_xlabels(STREAMS_X_LABEL)
        if title is not None:
            g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        ss = StringIO()

        if cuda_graph is None:
            pass
        elif cuda_graph:
            ss.write(f".cuda_graph_yes")
        else:
            ss.write(f".cuda_graph_no")

        if suffix is not None:
            ss.write(f".{suffix}")

        ss = ss.getvalue()

        save_plot(df, _j(self.args['trtexec_dir'], f'streams_vs_{alias}.batch_size_{batch_size}{ss}.svg'))
//  [python]
def _plot_streams_vs_trt_metric(self, trt_metric, batch_size, title=None, ylabel=None, alias=None, cuda_graph=None, suffix=None):
        if self.trtexec_df is None:
            return
        if alias is None:
            alias = trt_metric
        df = copy.copy(self.trtexec_df)
        """
        WANT:
        x_field: batch_size
        y_field: metric_value
        group_field: num_threads
        """

        df = df[df['batch_size'] == batch_size]

        # df = keep_cupti_metric(df, cupti_metric)

        # titled_df = copy.copy(df)
        # col_titles = {
        #     'num_threads': 'Number of threads',
        # }
        # titled_df.rename(columns=col_titles, inplace=True)

        df = self._add_config(df, df_type='trtexec')

        sns.set(style="whitegrid")
        plot_kwargs = dict(
            x="streams",
            y=trt_metric,
            kind="bar",
            palette="muted",
        )
        if cuda_graph is None:
            plot_kwargs.update(dict(
                hue="config",
            ))
        elif cuda_graph:
            df = df[df['cuda_graph']]
        else:
            df = df[~ df['cuda_graph']]
        plot_kwargs.update(dict(
            data=df,
        ))
        g = sns.catplot(**plot_kwargs)
        g.despine(left=True)
        if ylabel is None:
            ylabel = TRT_METRIC_YLABELS[trt_metric]
        g.set_ylabels(ylabel)
        # if xlabel is not None:
        g.set_xlabels(STREAMS_X_LABEL)
        if title is not None:
            g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        ss = StringIO()

        if cuda_graph is None:
            pass
        elif cuda_graph:
            ss.write(f".cuda_graph_yes")
        else:
            ss.write(f".cuda_graph_no")

        if suffix is not None:
            ss.write(f".{suffix}")

        ss = ss.getvalue()

        save_plot(df, _j(self.args['trtexec_dir'], f'streams_vs_{alias}.batch_size_{batch_size}{ss}.svg'))
//  [python]
def _plot_batch_size_vs_throughput(self, title, streams, filter_df=None, suffix=None):
        if self.trtexec_df is None:
            return
        """
        WANT:
        x_field: batch_size
        y_field: metric_value
        group_field: num_threads
        """


        plot_df = pd.DataFrame(columns=['batch_size', 'throughput_qps', 'config'])

        if self.trtexec_df is not None:
            df = copy.copy(self.trtexec_df)
            df = df[df['streams'] == streams]
            df.rename(columns={
                'host_latency_throughput_qps': 'throughput_qps',
            }, inplace=True)
            df = self._add_config(df, df_type='trtexec')
            plot_df = plot_df.append(df[plot_df.columns])

        if self.tf_inference_result_df is not None:
            df = copy.copy(self.tf_inference_result_df)
            df = self._add_config(df, df_type='tf_inference')
            plot_df = plot_df.append(df[plot_df.columns])

        plot_df.sort_values(by=['config', 'batch_size'], inplace=True)

        if filter_df is not None:
            plot_df = filter_df(plot_df)

        # df = keep_cupti_metric(df, cupti_metric)

        # titled_df = copy.copy(df)
        # col_titles = {
        #     'num_threads': 'Number of threads',
        # }
        # titled_df.rename(columns=col_titles, inplace=True)

        sns.set(style="whitegrid")
        # df = df[["thread_blocks", "metric_value", "num_threads"]]
        g = sns.factorplot(x="batch_size", y="throughput_qps",
                        # data=df,
                        hue="config", data=plot_df,
                        # hue=col_titles["num_threads"], data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(SAMPLE_THROUGHPUT_Y_LABEL)
        g.set_xlabels(BATCH_SIZE_X_LABEL)
        # title = "SM efficiency: percent of SMs\nthat are in use across the entire GPU"
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        if suffix is None:
            suffix = ""

        save_plot(plot_df, _j(self.args['trtexec_dir'], f'batch_size_vs_throughput.streams_{streams}{suffix}.svg'))
//  [python]
def _plot_batch_size_vs_throughput(self, title, streams, filter_df=None, suffix=None):
        if self.trtexec_df is None:
            return
        """
        WANT:
        x_field: batch_size
        y_field: metric_value
        group_field: num_threads
        """


        plot_df = pd.DataFrame(columns=['batch_size', 'throughput_qps', 'config'])

        if self.trtexec_df is not None:
            df = copy.copy(self.trtexec_df)
            df = df[df['streams'] == streams]
            df.rename(columns={
                'host_latency_throughput_qps': 'throughput_qps',
            }, inplace=True)
            df = self._add_config(df, df_type='trtexec')
            plot_df = plot_df.append(df[plot_df.columns])

        if self.tf_inference_result_df is not None:
            df = copy.copy(self.tf_inference_result_df)
            df = self._add_config(df, df_type='tf_inference')
            plot_df = plot_df.append(df[plot_df.columns])

        plot_df.sort_values(by=['config', 'batch_size'], inplace=True)

        if filter_df is not None:
            plot_df = filter_df(plot_df)

        # df = keep_cupti_metric(df, cupti_metric)

        # titled_df = copy.copy(df)
        # col_titles = {
        #     'num_threads': 'Number of threads',
        # }
        # titled_df.rename(columns=col_titles, inplace=True)

        sns.set(style="whitegrid")
        # df = df[["thread_blocks", "metric_value", "num_threads"]]
        g = sns.catplot(x="batch_size", y="throughput_qps",
                        # data=df,
                        hue="config", data=plot_df,
                        # hue=col_titles["num_threads"], data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(SAMPLE_THROUGHPUT_Y_LABEL)
        g.set_xlabels(BATCH_SIZE_X_LABEL)
        # title = "SM efficiency: percent of SMs\nthat are in use across the entire GPU"
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        if suffix is None:
            suffix = ""

        save_plot(plot_df, _j(self.args['trtexec_dir'], f'batch_size_vs_throughput.streams_{streams}{suffix}.svg'))
//  [python]
def _plot_simulator_vs_throughput(self):
        """
        x = simulator
        y = steps per second (samples/second)

        :return:
        """
        if self.simulator_df is None:
            return

        plot_df = pd.DataFrame(columns=['simulator', 'throughput_step_per_sec'])

        if self.simulator_df is not None:
            df = copy.copy(self.simulator_df)
            plot_df = plot_df.append(df[plot_df.columns])

        plot_df.sort_values(by=['simulator', 'throughput_step_per_sec'], inplace=True)

        sns.set(style="whitegrid")
        g = sns.barplot(x="simulator", y="throughput_step_per_sec",
                        data=plot_df,
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(STEP_THROUGHPUT_Y_LABEL)
        g.set_xlabels(SIMULATOR_X_LABEL)
        title = "Simulation throughput"
        g.figure.suptitle(title)
        g.figure.subplots_adjust(top=0.90)
        g.set_xticklabels(
            g.get_xticklabels(),
            rotation=15,
        )

        save_plot(plot_df, _j(self.args['simulator_dir'], f'simulator_vs_throughput.svg'))
//  [python]
def _plot_simulator_vs_throughput(self):
        """
        x = simulator
        y = steps per second (samples/second)

        :return:
        """
        if self.simulator_df is None:
            return

        plot_df = pd.DataFrame(columns=['simulator', 'throughput_step_per_sec'])

        if self.simulator_df is not None:
            df = copy.copy(self.simulator_df)
            plot_df = plot_df.append(df[plot_df.columns])

        plot_df.sort_values(by=['simulator', 'throughput_step_per_sec'], inplace=True)

        sns.set(style="whitegrid")
        g = sns.catplot(x="simulator", y="throughput_step_per_sec",
                        data=plot_df,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(STEP_THROUGHPUT_Y_LABEL)
        g.set_xlabels(SIMULATOR_X_LABEL)
        title = "Simulation throughput"
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=15,
        )

        save_plot(plot_df, _j(self.args['simulator_dir'], f'simulator_vs_throughput.svg'))
//  [python]
def _plot_sm_efficiency(self):
        if self.sm_df is None:
            return
        df = copy.copy(self.sm_df)
        """
        WANT:
        x_field: thread_blocks
        y_field: metric_value
        group_field: num_threads
        """

        df = keep_cupti_metric(df, 'sm_efficiency')

        titled_df = copy.copy(df)
        col_titles = {
            'num_threads': 'Number of threads',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        sns.set(style="whitegrid")
        # df = df[["thread_blocks", "metric_value", "num_threads"]]
        g = sns.factorplot(x="thread_blocks", y="metric_value", hue=col_titles["num_threads"], data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(SM_EFFICIENCY_Y_LABEL)
        g.set_xlabels("Number of thread blocks in kernel launch")
        title = "SM efficiency: percent of SMs\nthat are in use across the entire GPU"
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        save_plot(df, _j(self.arg('sm_efficiency_dir'), 'sm_efficiency.svg'))
//  [python]
def _plot_sm_efficiency(self):
        if self.sm_df is None:
            return
        df = copy.copy(self.sm_df)
        """
        WANT:
        x_field: thread_blocks
        y_field: metric_value
        group_field: num_threads
        """

        df = keep_cupti_metric(df, 'sm_efficiency')

        titled_df = copy.copy(df)
        col_titles = {
            'num_threads': 'Number of threads',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        sns.set(style="whitegrid")
        # df = df[["thread_blocks", "metric_value", "num_threads"]]
        g = sns.catplot(x="thread_blocks", y="metric_value", hue=col_titles["num_threads"], data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(SM_EFFICIENCY_Y_LABEL)
        g.set_xlabels("Number of thread blocks in kernel launch")
        title = "SM efficiency: percent of SMs\nthat are in use across the entire GPU"
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)

        save_plot(df, _j(self.arg('sm_efficiency_dir'), 'sm_efficiency.svg'))
//  [python]
def _plot_achieved_occupancy(self):
        if self.occupancy_df is None:
            return
        df = copy.copy(self.occupancy_df)

        df = keep_cupti_metric(df, 'achieved_occupancy')

        titled_df = copy.copy(df)
        col_titles = {
            'num_threads': 'Number of threads',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        sns.set(style="whitegrid")
        # df = df[["thread_blocks", "metric_value", "num_threads"]]
        g = sns.factorplot(x="thread_block_size", y="metric_value",
                        # hue=col_titles["num_threads"],
                        data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(SM_OCCUPANCY_Y_LABEL)
        g.set_xlabels("Number of threads per block in kernel launch")
        title = SM_OCCUPANCY_TITLE
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 45),
        )

        save_plot(df, _j(self.arg('achieved_occupancy_dir'), 'achieved_occupancy.svg'))
//  [python]
def _plot_achieved_occupancy(self):
        if self.occupancy_df is None:
            return
        df = copy.copy(self.occupancy_df)

        df = keep_cupti_metric(df, 'achieved_occupancy')

        titled_df = copy.copy(df)
        col_titles = {
            'num_threads': 'Number of threads',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        sns.set(style="whitegrid")
        # df = df[["thread_blocks", "metric_value", "num_threads"]]
        g = sns.catplot(x="thread_block_size", y="metric_value",
                        # hue=col_titles["num_threads"],
                        data=titled_df,
                        # height=6,
                        kind="bar",
                        palette="muted"
                        )
        g.despine(left=True)
        g.set_ylabels(SM_OCCUPANCY_Y_LABEL)
        g.set_xlabels("Number of threads per block in kernel launch")
        title = SM_OCCUPANCY_TITLE
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 45),
        )

        save_plot(df, _j(self.arg('achieved_occupancy_dir'), 'achieved_occupancy.svg'))
//  [python]
def _plot_rlscope_sm_efficiency(self):
        if self.rlscope_df is None:
            return
        df = copy.copy(self.rlscope_df)
        df = keep_cupti_metric(df, 'sm_efficiency')
        add_gpu_hw_fields(df)

        df = self._add_x_field(df)

        titled_df = copy.copy(df)
        col_titles = {
            'range_name': 'Operation',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        plot_kwargs = dict()
        self._add_plot_kwargs(plot_kwargs)

        sns.set(style="whitegrid")
        def _do_plot(**kwargs):
            g = sns.factorplot(x="x_field", y="metric_value", hue=col_titles["range_name"], data=titled_df,
                            kind="bar",
                            palette="muted",
                            **kwargs,
                            **plot_kwargs,
                            )
            return g
        g = _do_plot()
        capsize = self._get_capsize(g)
        g = _do_plot(capsize=capsize)
        g.despine(left=True)
        g.set_ylabels(SM_EFFICIENCY_Y_LABEL)
        x_title = self.arg('x_title', RLSCOPE_X_LABEL)
        g.set_xlabels(x_title)
        title = self.arg('title', SM_EFFICIENCY_TITLE)
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 15),
        )

        save_plot(df, _j(self.rlscope_output_dir(), 'rlscope_sm_efficiency.svg'))
//  [python]
def _plot_rlscope_sm_efficiency(self):
        if self.rlscope_df is None:
            return
        df = copy.copy(self.rlscope_df)
        df = keep_cupti_metric(df, 'sm_efficiency')
        add_gpu_hw_fields(df)

        df = self._add_x_field(df)

        titled_df = copy.copy(df)
        col_titles = {
            'range_name': 'Operation',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        plot_kwargs = dict()
        self._add_plot_kwargs(plot_kwargs)

        sns.set(style="whitegrid")
        def _do_plot(**kwargs):
            g = sns.catplot(x="x_field", y="metric_value", hue=col_titles["range_name"], data=titled_df,
                            kind="bar",
                            palette="muted",
                            **kwargs,
                            **plot_kwargs,
                            )
            return g
        g = _do_plot()
        capsize = self._get_capsize(g)
        g = _do_plot(capsize=capsize)
        g.despine(left=True)
        g.set_ylabels(SM_EFFICIENCY_Y_LABEL)
        x_title = self.arg('x_title', RLSCOPE_X_LABEL)
        g.set_xlabels(x_title)
        title = self.arg('title', SM_EFFICIENCY_TITLE)
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 15),
        )

        save_plot(df, _j(self.rlscope_output_dir(), 'rlscope_sm_efficiency.svg'))
//  [python]
def _plot_rlscope_achieved_occupancy(self):
        if self.rlscope_df is None:
            return
        df = copy.copy(self.rlscope_df)
        df = keep_cupti_metric(df, 'achieved_occupancy')

        df = self._add_x_field(df)

        titled_df = copy.copy(df)
        col_titles = {
            'range_name': 'Operation',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        plot_kwargs = dict()
        self._add_plot_kwargs(plot_kwargs)

        sns.set(style="whitegrid")
        def _do_plot(**kwargs):
            g = sns.factorplot(x="x_field", y="metric_value", hue=col_titles["range_name"], data=titled_df,
                            kind="bar",
                            palette="muted",
                            # capsize=capsize,
                            **kwargs,
                            **plot_kwargs,
                            )
            return g
        g = _do_plot()
        capsize = self._get_capsize(g)
        g = _do_plot(capsize=capsize)

        g.despine(left=True)
        g.set_ylabels(SM_OCCUPANCY_Y_LABEL)
        x_title = self.arg('x_title', RLSCOPE_X_LABEL)
        g.set_xlabels(x_title)
        title = self.arg('title', SM_OCCUPANCY_TITLE)
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 15),
        )

        save_plot(df, _j(self.rlscope_output_dir(), 'rlscope_achieved_occupancy.svg'))
//  [python]
def _plot_rlscope_achieved_occupancy(self):
        if self.rlscope_df is None:
            return
        df = copy.copy(self.rlscope_df)
        df = keep_cupti_metric(df, 'achieved_occupancy')

        df = self._add_x_field(df)

        titled_df = copy.copy(df)
        col_titles = {
            'range_name': 'Operation',
        }
        titled_df.rename(columns=col_titles, inplace=True)

        plot_kwargs = dict()
        self._add_plot_kwargs(plot_kwargs)

        sns.set(style="whitegrid")
        def _do_plot(**kwargs):
            g = sns.catplot(x="x_field", y="metric_value", hue=col_titles["range_name"], data=titled_df,
                            kind="bar",
                            palette="muted",
                            # capsize=capsize,
                            **kwargs,
                            **plot_kwargs,
                            )
            return g
        g = _do_plot()
        capsize = self._get_capsize(g)
        g = _do_plot(capsize=capsize)

        g.despine(left=True)
        g.set_ylabels(SM_OCCUPANCY_Y_LABEL)
        x_title = self.arg('x_title', RLSCOPE_X_LABEL)
        g.set_xlabels(x_title)
        title = self.arg('title', SM_OCCUPANCY_TITLE)
        g.fig.suptitle(title)
        g.fig.subplots_adjust(top=0.90)
        g.fig.axes[0].set_xticklabels(
            g.fig.axes[0].get_xticklabels(),
            rotation=self.arg('rotation', 15),
        )

        save_plot(df, _j(self.rlscope_output_dir(), 'rlscope_achieved_occupancy.svg'))
//  [python]
def plot():
    try:
        import matplotlib.pyplot as plt
        import seaborn as sns
        import pandas as pd
        import numpy as np
    except ModuleNotFoundError:
        return

    exercise = sns.load_dataset("exercise")
    g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise)

    plt.show()
//  [python]
def plot():
    try:
        import matplotlib.pyplot as plt
        import seaborn as sns
        import pandas as pd
        import numpy as np
    except ModuleNotFoundError:
        return

    exercise = sns.load_dataset("exercise")
    g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)

    plt.show()
//  [python]
def _plot_multitask_kernel_time(self):
        if self.multitask_raw_df is None:
            return

        df = self.multitask_raw_df.copy()

        sns.set(style="whitegrid")
        g = sns.barplot(x="expr_x", y="total_kernel_run_time_sec", data=df)
        # x_min, x_max = ax.get_xlim()
        # -1 so we can see markers at x=0 without them being cut off.
        # ax.set_xlim(-1, NUM_SMS)
        sns.despine(left=True)
        g.set_xlabel('Configuration')
        # g.set_ylabels(f"SM efficiency (%)\n(normalized by max # SMs = {max_num_sm})")
        g.set_ylabel(f"Total kernel execution time (sec)")
        g.set_title("Total kernel execution time\nunder different multi-task configurations")
        #g.fig.suptitle("Total kernel execution time\nunder different multi-task configurations")
        #g.fig.subplots_adjust(top=0.90)

        save_plot(df, _j(self._multitask_dir(), 'multitask_kernel_time.svg'))
//  [python]
def _plot_multitask_kernel_time(self):
        if self.multitask_raw_df is None:
            return

        df = self.multitask_raw_df.copy()

        sns.set(style="whitegrid")
        g = sns.catplot(x="expr_x", y="total_kernel_run_time_sec",
                        kind='bar',
                        data=df)
        # x_min, x_max = ax.get_xlim()
        # -1 so we can see markers at x=0 without them being cut off.
        # ax.set_xlim(-1, NUM_SMS)
        g.despine(left=True)
        g.set_xlabels('Configuration')
        # g.set_ylabels(f"SM efficiency (%)\n(normalized by max # SMs = {max_num_sm})")
        g.set_ylabels(f"Total kernel execution time (sec)")
        g.fig.suptitle("Total kernel execution time\nunder different multi-task configurations")
        g.fig.subplots_adjust(top=0.90)

        save_plot(df, _j(self._multitask_dir(), 'multitask_kernel_time.svg'))
//  [python]
plt.close('all')

file = 'C:/Users/ahino/Documents/GitHub/PyNoddyInversion/code/Combo_Scratch/ParametersWithMismatch.csv'

Data = pd.read_csv(file)

figOptim = sns.boxplot(x="OptimMethod", y="MinMismatch", data=Data)
//  [python]
plt.close('all')

file = 'C:/Users/ahino/Documents/GitHub/PyNoddyInversion/code/Combo_Scratch/ParametersWithMismatch.csv'

Data = pd.read_csv(file)

figOptim = sns.catplot(x="OptimMethod", y="MinMismatch", kind="box", data=Data)
//  [python]
def _plot_multitask_sm_efficiency(self):
        if self.multitask_df is None:
            return

        sched_df = self.multitask_df.copy()
        """
        x = sm_id
        hue = expr
        y:
          for sm_id in all available sm_ids:
            y = len(df[df['sm_id'] == sm_id]['thread_id'].unique())
        """
        plot_df = sched_df[['sm_id', 'expr_x', 'num_tasks']].drop_duplicates().reset_index()
        del plot_df['index']

        # Max num of SM ids this configuration could possibly use.
        # == num_threads
        # NOTE:
        # - for MPS, max should be how many SMs are usable based on CUDA_MPS_ACTIVE_THREAD_PERCENTAGE and
        #   the number of SMs on the device:
        #   max_num_sm = math.ceil( (1/CUDA_MPS_ACTIVE_THREAD_PERCENTAGE) * NUM_SMS )
        # - Perhaps normalize by the number of "CPU threads" the configuration uses?

        def _sm_efficiency(row):
            sm_ids = sched_df[(sched_df['expr_x'] == row['expr_x'])] \
                ['sm_id'].unique()
            ret = 100 * len(sm_ids) / row['num_tasks']
            return ret
        plot_df['sm_efficiency'] = plot_df.apply(_sm_efficiency, axis=1)

        sns.set(style="whitegrid")
        g = sns.factorplot(x="expr_x", y="sm_efficiency",
                        kind='bar',
                        data=plot_df)
        # x_min, x_max = ax.get_xlim()
        # -1 so we can see markers at x=0 without them being cut off.
        # ax.set_xlim(-1, NUM_SMS)
        g.despine(left=True)
        g.set_xlabels('Configuration')
        g.set_ylabels(f"SM efficiency (%)\n(normalized by max # SMs = $N_{{tasks}}$)")
        g.fig.suptitle(SM_EFFICIENCY_TITLE)
        g.fig.subplots_adjust(top=0.90)

        save_plot(plot_df, _j(self._multitask_dir(), 'multitask_sm_efficiency.svg'))
//  [python]
def _plot_multitask_sm_efficiency(self):
        if self.multitask_df is None:
            return

        sched_df = self.multitask_df.copy()
        """
        x = sm_id
        hue = expr
        y:
          for sm_id in all available sm_ids:
            y = len(df[df['sm_id'] == sm_id]['thread_id'].unique())
        """
        plot_df = sched_df[['sm_id', 'expr_x', 'num_tasks']].drop_duplicates().reset_index()
        del plot_df['index']

        # Max num of SM ids this configuration could possibly use.
        # == num_threads
        # NOTE:
        # - for MPS, max should be how many SMs are usable based on CUDA_MPS_ACTIVE_THREAD_PERCENTAGE and
        #   the number of SMs on the device:
        #   max_num_sm = math.ceil( (1/CUDA_MPS_ACTIVE_THREAD_PERCENTAGE) * NUM_SMS )
        # - Perhaps normalize by the number of "CPU threads" the configuration uses?

        def _sm_efficiency(row):
            sm_ids = sched_df[(sched_df['expr_x'] == row['expr_x'])] \
                ['sm_id'].unique()
            ret = 100 * len(sm_ids) / row['num_tasks']
            return ret
        plot_df['sm_efficiency'] = plot_df.apply(_sm_efficiency, axis=1)

        sns.set(style="whitegrid")
        g = sns.catplot(x="expr_x", y="sm_efficiency",
                        kind='bar',
                        data=plot_df)
        # x_min, x_max = ax.get_xlim()
        # -1 so we can see markers at x=0 without them being cut off.
        # ax.set_xlim(-1, NUM_SMS)
        g.despine(left=True)
        g.set_xlabels('Configuration')
        g.set_ylabels(f"SM efficiency (%)\n(normalized by max # SMs = $N_{{tasks}}$)")
        g.fig.suptitle(SM_EFFICIENCY_TITLE)
        g.fig.subplots_adjust(top=0.90)

        save_plot(plot_df, _j(self._multitask_dir(), 'multitask_sm_efficiency.svg'))
//  [python]
def plot_speedup(data: List[EvalQuery], appname: str, interval: int, output_dir: str):
    f, ax = plt.subplots()
    group_boundaries = [0, 1-interval/100.0, 1+interval/100.0, 1.1, 2]
    data = group_by(queries, group_boundaries)
    p = sns.countplot(x="group",
                    hue_order=["install constraints", "rewrite",
                               "install constraints + rewrite"],
                    order=group_boundaries,
                    hue="type", data=data,
                    legend=False)

    plt.xticks([0, 1, 2, 3, 4], ["slowdown", '0$\pm$' +
               str(interval) + '%', "<10%", "10~100%", ">100%"], size=tick_size)
    plt.yticks(fontsize=tick_size)
    plt.legend(prop={'size': 12})
    # plt.legend(loc='upper right', prop={'size': 11})
    if appname in ["redmine", "mastodon"]:
        plt.ylabel("Number of queries", size=label_size)
    else:
        plt.ylabel("")
    if appname == "openproject":
        plt.yticks([0, 150, 300, 450, 600])
    plt.xlabel("",  size=label_size)
    plt.title(APP_NAME[appname], pad=0, size=18)
    plt.tight_layout()
    plt.savefig(f"{output_dir}/figures/{appname}_perf.{suffix}", bbox_inches='tight')
//  [python]
def plot_speedup(data: List[EvalQuery], appname: str, interval: int, output_dir: str):
    f, ax = plt.subplots()
    group_boundaries = [0, 1-interval/100.0, 1+interval/100.0, 1.1, 2]
    data = group_by(queries, group_boundaries)
    p = sns.catplot(x="group", y=None,
                    hue_order=["install constraints", "rewrite",
                               "install constraints + rewrite"],
                    order=group_boundaries,
                    hue="type", data=data,
                    kind="count",
                    legend=False,
                    height=3, aspect=6/3)

    plt.xticks([0, 1, 2, 3, 4], ["slowdown", '0$\pm$' +
               str(interval) + '%', "<10%", "10~100%", ">100%"], size=tick_size)
    plt.yticks(fontsize=tick_size)
    plt.legend(prop={'size': 12})
    # plt.legend(loc='upper right', prop={'size': 11})
    if appname in ["redmine", "mastodon"]:
        plt.ylabel("Number of queries", size=label_size)
    else:
        plt.ylabel("")
    if appname == "openproject":
        plt.yticks([0, 150, 300, 450, 600])
    plt.xlabel("",  size=label_size)
    plt.title(APP_NAME[appname], pad=0, size=18)
    plt.tight_layout()
    plt.savefig(f"{output_dir}/figures/{appname}_perf.{suffix}", bbox_inches='tight')
//  [python]
def plot_simulation_hyperparameter_estimates(s_df, metric,
                                             showfliers=False,
                                             model_order=None,
                                             row_order=None,
                                             col_order=None,
                                             col_wrap=3,
                                             palette='Set2',
                                             log_scale=False):

    assert metric in ('Estimated Heritability', 'Estimated Prop. Causal')

    g = sns.factorplot(x="Heritability",
                    y=metric,
                    hue="Model",
                    col="Simulation model",
                    data=s_df,
                    kind="box",
                    showfliers=showfliers,
                    hue_order=model_order,
                    col_order=col_order,
                    row_order=row_order,
                    col_wrap=col_wrap,
                    palette=palette)

    if metric_name is not None:
        g.set_axis_labels("Heritability", metric_name(metric))

    # Update the subplot titles:
    for fig_ax in g.fig.axes:
        fig_ax.set_title(fig_ax.get_title().replace("Simulation model = ", ""))

    if log_scale:
        for i, ax in enumerate(g.fig.axes):
            ax.set_yscale('log', base=10)

    return g
//  [python]
def plot_simulation_hyperparameter_estimates(s_df, metric,
                                             showfliers=False,
                                             model_order=None,
                                             row_order=None,
                                             col_order=None,
                                             col_wrap=3,
                                             palette='Set2',
                                             log_scale=False):

    assert metric in ('Estimated Heritability', 'Estimated Prop. Causal')

    g = sns.catplot(x="Heritability",
                    y=metric,
                    hue="Model",
                    col="Simulation model",
                    data=s_df,
                    kind="box",
                    showfliers=showfliers,
                    hue_order=model_order,
                    col_order=col_order,
                    row_order=row_order,
                    col_wrap=col_wrap,
                    palette=palette)

    if metric_name is not None:
        g.set_axis_labels("Heritability", metric_name(metric))

    # Update the subplot titles:
    for fig_ax in g.fig.axes:
        fig_ax.set_title(fig_ax.get_title().replace("Simulation model = ", ""))

    if log_scale:
        for i, ax in enumerate(g.fig.axes):
            ax.set_yscale('log', base=10)

    return g
//  [python]
def plot_rewrite_type(data: List[EvalQuery], appname: str, output_dir: str):
    f, ax = plt.subplots()
    group_boundaries = [0, 1, 1.1, 2]
    data = group_by(queries, group_boundaries)
    data = data[data["sql"] != "none"]
    data = data[data["type"] != "install constraints"]
    p = sns.barplot(x="rewrite_type", y="sp", hue="type", data=data,
                    order=["AL", "ES", "PI/E", "JI/E", "RD", "MIX"],
                    palette={"rewrite": "#ff7f0e", "install constraints + rewrite": "#2ca02c"},
                    ci=75, capsize=.1)
    plt.ylabel("")
    plt.xlabel("", size=label_size)
    plt.yticks(fontsize=tick_size)
    plt.xticks(fontsize=tick_size)
    plt.legend(prop={'size': 11})
    if appname == "openproject":
        plt.yscale("log", base=10)
    #    plt.ylim(0,1300)
    if appname == "redmine":
        plt.ylabel("Average Speedup", size=label_size)
        # plt.yticks([0, 1, 2, 3, 4, 5, 6])
    elif appname == "mastodon":
        plt.ylabel("Average Speedup", size=label_size)
    # elif appname == "forem":
    #     plt.yticks([0, 3, 6, 9, 12, 15])
    else:
        plt.ylabel("")
    plt.grid(axis='y', color='grey')
    plt.title(APP_NAME[appname], pad=0, size=18)
    plt.tight_layout()
    plt.savefig(f"{output_dir}/figures/{appname}_type_perf.{suffix}", bbox_inches='tight')
//  [python]
def plot_rewrite_type(data: List[EvalQuery], appname: str, output_dir: str):
    f, ax = plt.subplots()
    group_boundaries = [0, 1, 1.1, 2]
    data = group_by(queries, group_boundaries)
    data = data[data["sql"] != "none"]
    data = data[data["type"] != "install constraints"]
    p = sns.catplot(x="rewrite_type", y="sp", errorbar=('ci', 75), capsize=.1,
                    order=["AL", "ES", "PI/E", "JI/E", "RD", "MIX"],
                    hue="type", kind="bar", data=data,
                    legend=False,
                    height=3, aspect=6/3, # estimator='median',
                    palette={"rewrite": "#ff7f0e", "install constraints + rewrite": "#2ca02c"})
    plt.ylabel("")
    plt.xlabel("", size=label_size)
    plt.yticks(fontsize=tick_size)
    plt.xticks(fontsize=tick_size)
    plt.legend(prop={'size': 11})
    if appname == "openproject":
        plt.yscale("log", base=10)
    #    plt.ylim(0,1300)
    if appname == "redmine":
        plt.ylabel("Average Speedup", size=label_size)
        # plt.yticks([0, 1, 2, 3, 4, 5, 6])
    elif appname == "mastodon":
        plt.ylabel("Average Speedup", size=label_size)
    # elif appname == "forem":
    #     plt.yticks([0, 3, 6, 9, 12, 15])
    else:
        plt.ylabel("")
    plt.grid(axis='y', color='grey')
    plt.title(APP_NAME[appname], pad=0, size=18)
    plt.tight_layout()
    plt.savefig(f"{output_dir}/figures/{appname}_type_perf.{suffix}", bbox_inches='tight')
//  [python]
def plot_real_hyperparameter_estimates(r_df, metric,
                                       add_bar_labels=True,
                                       x_label_rotation=90,
                                       hide_x_labels=False,
                                       model_order=None, row_order=None, col_order=None,
                                       col_wrap=3, palette='Set2',
                                       log_scale=False):

    assert metric in ('Estimated Heritability', 'Estimated Prop. Causal')

    g = sns.factorplot(x="Model",
                    y=metric,
                    col="Trait",
                    data=r_df,
                    kind="bar",
                    col_wrap=col_wrap,
                    order=model_order,
                    row_order=row_order,
                    col_order=col_order,
                    palette=palette)

    if log_scale:
        for i, ax in enumerate(g.fig.axes):
            ax.set_ylim(ymin=r_df[metric].min())
            ax.set_yscale('log', base=10)

    if add_bar_labels:
        add_labels_to_bars(g)

    if x_label_rotation != 0 or hide_x_labels:
        for fig_ax in g.fig.axes:
            if hide_x_labels:
                fig_ax.set_xticklabels([])
            else:
                fig_ax.set_xticklabels(fig_ax.get_xticklabels(), rotation=x_label_rotation)

    # Update the subplot titles:
    for fig_ax in g.fig.axes:
        fig_ax.set_title(fig_ax.get_title().replace("Trait = ", ""))

    if metric_name is not None:
        g.set_axis_labels("Model", metric_name(metric))

    return g
//  [python]
def plot_real_hyperparameter_estimates(r_df, metric,
                                       add_bar_labels=True,
                                       x_label_rotation=90,
                                       hide_x_labels=False,
                                       model_order=None, row_order=None, col_order=None,
                                       col_wrap=3, palette='Set2',
                                       log_scale=False):

    assert metric in ('Estimated Heritability', 'Estimated Prop. Causal')

    g = sns.catplot(x="Model",
                    y=metric,
                    col="Trait",
                    data=r_df,
                    kind="bar",
                    col_wrap=col_wrap,
                    order=model_order,
                    row_order=row_order,
                    col_order=col_order,
                    palette=palette)

    if log_scale:
        for i, ax in enumerate(g.fig.axes):
            ax.set_ylim(ymin=r_df[metric].min())
            ax.set_yscale('log', base=10)

    if add_bar_labels:
        add_labels_to_bars(g)

    if x_label_rotation != 0 or hide_x_labels:
        for fig_ax in g.fig.axes:
            if hide_x_labels:
                fig_ax.set_xticklabels([])
            else:
                fig_ax.set_xticklabels(fig_ax.get_xticklabels(), rotation=x_label_rotation)

    # Update the subplot titles:
    for fig_ax in g.fig.axes:
        fig_ax.set_title(fig_ax.get_title().replace("Trait = ", ""))

    if metric_name is not None:
        g.set_axis_labels("Model", metric_name(metric))

    return g
//  [python]
def plot_barcode_match_fraction(matches, suffix=""):
    """
    Matches should be a dict with values as boolean vectors
    """
    print(ct() + "Plotting fraction of correct barcodes.")

    d = pd.DataFrame(
        [[sum(x) for x in matches.values()], [x.shape[0] for x in matches.values()],],
        index=["correct", "total"],
        columns=matches.keys(),
    )

    d.loc["correct", "both"] = np.array([x for x in matches.values()]).all(axis=0).sum()
    d.loc["total", "both"] = list(matches.values())[0].shape[0]
    d.loc["fraction of exact match", :] = d.loc["correct"] / d.loc["total"]

    g = sns.FacetGrid(d.reset_index().melt(id_vars="index"), col="index", sharey=False, height=3, aspect=1)
    g.map(sns.barplot, "variable", "value")
    g.fig.savefig(
        args.output_prefix + f"barcode_matching.barplot.{suffix}.svg".replace("..", "."),
        dpi=300,
        bbox_inches="tight",
    )
//  [python]
def plot_barcode_match_fraction(matches, suffix=""):
    """
    Matches should be a dict with values as boolean vectors
    """
    print(ct() + "Plotting fraction of correct barcodes.")

    d = pd.DataFrame(
        [[sum(x) for x in matches.values()], [x.shape[0] for x in matches.values()],],
        index=["correct", "total"],
        columns=matches.keys(),
    )

    d.loc["correct", "both"] = np.array([x for x in matches.values()]).all(axis=0).sum()
    d.loc["total", "both"] = list(matches.values())[0].shape[0]
    d.loc["fraction of exact match", :] = d.loc["correct"] / d.loc["total"]

    grid = sns.catplot(
        data=d.reset_index().melt(id_vars="index"),
        x="variable",
        y="value",
        col="index",
        sharey=False,
        kind="bar",
        height=3,
        aspect=1,
    )
    grid.fig.savefig(
        args.output_prefix + f"barcode_matching.barplot.{suffix}.svg".replace("..", "."),
        dpi=300,
        bbox_inches="tight",
    )