Skip to content

Generate plots

PlotGenerator

Class to generate plots.

Source code in src/pheval/analyse/generate_plots.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
class PlotGenerator:
    """Class to generate plots."""

    palette_hex_codes = [
        "#f4ae3d",
        "#ee5825",
        "#2b7288",
        "#9a84b2",
        "#0c604c",
        "#c94c4c",
        "#3d8e83",
        "#725ac1",
        "#e7ba52",
        "#1b9e77",
    ]

    def __init__(
        self,
    ):
        """
        Initialise the PlotGenerator class.
        Note:
            `self.stats` will be used to store statistics data.
            `self.mrr` will store Mean Reciprocal Rank (MRR) values.
            Matplotlib settings are configured to remove the right and top axes spines
            for generated plots.
        """
        self.stats, self.mrr = [], []
        matplotlib.rcParams["axes.spines.right"] = False
        matplotlib.rcParams["axes.spines.top"] = False

    @staticmethod
    def _create_run_identifier(results_dir: Path) -> str:
        """
        Create a run identifier from a path.

        Args:
            results_dir (Path): The directory path for results.

        Returns:
            str: A string representing the run identifier created from the given path.
        """
        return f"{Path(results_dir).parents[0].name}_{trim_corpus_results_directory_suffix(Path(results_dir).name)}"

    def return_benchmark_name(self, benchmark_result: BenchmarkRunResults) -> str:
        """
        Return the benchmark name for a run.

        Args:
            benchmark_result (BenchmarkRunResults): The benchmarking results for a run.

        Returns:
            str: The benchmark name obtained from the given BenchmarkRunResults instance.
        """
        return (
            benchmark_result.benchmark_name
            if benchmark_result.results_dir is None
            else self._create_run_identifier(benchmark_result.results_dir)
        )

    def _generate_stacked_bar_plot_data(self, benchmark_result: BenchmarkRunResults) -> None:
        """
        Generate data in the correct format for dataframe creation for a stacked bar plot,
        appending to the self.stats attribute of the class.

        Args:
            benchmark_result (BenchmarkRunResults): The benchmarking results for a run.
        """
        rank_stats = benchmark_result.rank_stats
        self.stats.append(
            {
                "Run": self.return_benchmark_name(benchmark_result),
                "Top": benchmark_result.rank_stats.percentage_top(),
                "2-3": rank_stats.percentage_difference(
                    rank_stats.percentage_top3(), rank_stats.percentage_top()
                ),
                "4-5": rank_stats.percentage_difference(
                    rank_stats.percentage_top5(), rank_stats.percentage_top3()
                ),
                "6-10": rank_stats.percentage_difference(
                    rank_stats.percentage_top10(), rank_stats.percentage_top5()
                ),
                ">10": rank_stats.percentage_difference(
                    rank_stats.percentage_found(), rank_stats.percentage_top10()
                ),
                "Missed": rank_stats.percentage_difference(100, rank_stats.percentage_found()),
            }
        )

    def _generate_stats_mrr_bar_plot_data(self, benchmark_result: BenchmarkRunResults) -> None:
        """
        Generate data in the correct format for dataframe creation for MRR (Mean Reciprocal Rank) bar plot,
        appending to the self.mrr attribute of the class.

        Args:
            benchmark_result (BenchmarkRunResults): The benchmarking results for a run.
        """
        self.mrr.extend(
            [
                {
                    "Rank": "MRR",
                    "Percentage": benchmark_result.rank_stats.return_mean_reciprocal_rank(),
                    "Run": self.return_benchmark_name(benchmark_result),
                }
            ]
        )

    def generate_stacked_bar_plot(
        self,
        benchmarking_results: List[BenchmarkRunResults],
        benchmark_generator: BenchmarkRunOutputGenerator,
        title: str = None,
    ) -> None:
        """
        Generate a stacked bar plot and Mean Reciprocal Rank (MRR) bar plot.

        Args:
            benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
            benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
            title (str, optional): Title for the generated plot. Defaults to None.
        """
        for benchmark_result in benchmarking_results:
            self._generate_stacked_bar_plot_data(benchmark_result)
            self._generate_stats_mrr_bar_plot_data(benchmark_result)
        stats_df = pd.DataFrame(self.stats)
        stats_df.set_index("Run").plot(
            kind="bar",
            stacked=True,
            color=self.palette_hex_codes,
            ylabel=benchmark_generator.y_label,
            edgecolor="white",
        ).legend(loc="center left", bbox_to_anchor=(1.0, 0.5))
        if title is None:
            plt.title(
                f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} Rank Stats"
            )
        else:
            plt.title(title, loc="center", fontsize=15)
        plt.ylim(0, 100)
        plt.savefig(
            f"{benchmark_generator.prioritisation_type_file_prefix}_rank_stats.svg",
            format="svg",
            bbox_inches="tight",
        )

        mrr_df = pd.DataFrame(self.mrr)
        mrr_df.set_index("Run").plot(
            kind="bar",
            color=self.palette_hex_codes,
            ylabel=f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} mean reciprocal rank",
            legend=False,
            edgecolor="white",
        )
        plt.title(
            f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} results - mean reciprocal rank"
        )
        plt.ylim(0, 1)
        plt.savefig(
            f"{benchmark_generator.prioritisation_type_file_prefix}_mrr.svg",
            format="svg",
            bbox_inches="tight",
        )

    def _generate_cumulative_bar_plot_data(self, benchmark_result: BenchmarkRunResults):
        """
        Generate data in the correct format for dataframe creation for a cumulative bar plot,
        appending to the self.stats attribute of the class.

        Args:
            benchmark_result (BenchmarkRunResults): The benchmarking results for a run.
        """
        rank_stats = benchmark_result.rank_stats
        run_identifier = self.return_benchmark_name(benchmark_result)
        self.stats.extend(
            [
                {
                    "Rank": "Top",
                    "Percentage": rank_stats.percentage_top() / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "Top3",
                    "Percentage": rank_stats.percentage_top3() / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "Top5",
                    "Percentage": rank_stats.percentage_top5() / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "Top10",
                    "Percentage": rank_stats.percentage_top10() / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "Found",
                    "Percentage": rank_stats.percentage_found() / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "Missed",
                    "Percentage": rank_stats.percentage_difference(
                        100, rank_stats.percentage_found()
                    )
                    / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "MRR",
                    "Percentage": rank_stats.return_mean_reciprocal_rank(),
                    "Run": run_identifier,
                },
            ]
        )

    def generate_cumulative_bar(
        self,
        benchmarking_results: List[BenchmarkRunResults],
        benchmark_generator: BenchmarkRunOutputGenerator,
        title: str = None,
    ) -> None:
        """
        Generate a cumulative bar plot.

        Args:
            benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
            benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
            title (str, optional): Title for the generated plot. Defaults to None.
        """
        for benchmark_result in benchmarking_results:
            self._generate_cumulative_bar_plot_data(benchmark_result)
        stats_df = pd.DataFrame(self.stats)
        sns.catplot(
            data=stats_df,
            kind="bar",
            x="Rank",
            y="Percentage",
            hue="Run",
            palette=self.palette_hex_codes,
            edgecolor="white",
            legend=False,
        ).set(xlabel="Rank", ylabel=benchmark_generator.y_label)
        plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15), ncol=3, title="Run")
        if title is None:
            plt.title(
                f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} Cumulative Rank Stats"
            )
        else:
            plt.title(title, loc="center", fontsize=15)
        plt.ylim(0, 1)
        plt.savefig(
            f"{benchmark_generator.prioritisation_type_file_prefix}_rank_stats.svg",
            format="svg",
            bbox_inches="tight",
        )

    def _generate_non_cumulative_bar_plot_data(
        self, benchmark_result: BenchmarkRunResults
    ) -> [dict]:
        """
        Generate data in the correct format for dataframe creation for a non-cumulative bar plot,
        appending to the self.stats attribute of the class.

        Args:
            benchmark_result (BenchmarkRunResults): The benchmarking results for a run.
        """
        rank_stats = benchmark_result.rank_stats
        run_identifier = self.return_benchmark_name(benchmark_result)
        self.stats.extend(
            [
                {
                    "Rank": "Top",
                    "Percentage": rank_stats.percentage_top() / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "2-3",
                    "Percentage": rank_stats.percentage_difference(
                        rank_stats.percentage_top3(), rank_stats.percentage_top()
                    )
                    / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "4-5",
                    "Percentage": rank_stats.percentage_difference(
                        rank_stats.percentage_top5(), rank_stats.percentage_top3()
                    )
                    / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "6-10",
                    "Percentage": rank_stats.percentage_difference(
                        rank_stats.percentage_top10(), rank_stats.percentage_top5()
                    )
                    / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": ">10",
                    "Percentage": rank_stats.percentage_difference(
                        rank_stats.percentage_found(), rank_stats.percentage_top10()
                    )
                    / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "Missed",
                    "Percentage": rank_stats.percentage_difference(
                        100, rank_stats.percentage_found()
                    )
                    / 100,
                    "Run": run_identifier,
                },
                {
                    "Rank": "MRR",
                    "Percentage": rank_stats.return_mean_reciprocal_rank(),
                    "Run": run_identifier,
                },
            ]
        )

    def generate_roc_curve(
        self,
        benchmarking_results: List[BenchmarkRunResults],
        benchmark_generator: BenchmarkRunOutputGenerator,
    ):
        """
        Generate and plot Receiver Operating Characteristic (ROC) curves for binary classification benchmark results.

        Args:
            benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
            benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
        """
        for i, benchmark_result in enumerate(benchmarking_results):
            fpr, tpr, thresh = roc_curve(
                benchmark_result.binary_classification_stats.labels,
                benchmark_result.binary_classification_stats.scores,
                pos_label=1,
            )
            roc_auc = auc(fpr, tpr)

            plt.plot(
                fpr,
                tpr,
                label=f"{self.return_benchmark_name(benchmark_result)} ROC Curve (AUC = {roc_auc:.2f})",
                color=self.palette_hex_codes[i],
            )

        plt.plot(linestyle="--", color="gray")
        plt.xlabel("False Positive Rate")
        plt.ylabel("True Positive Rate")
        plt.title("Receiver Operating Characteristic (ROC) Curve")
        plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
        plt.savefig(
            f"{benchmark_generator.prioritisation_type_file_prefix}_roc_curve.svg",
            format="svg",
            bbox_inches="tight",
        )

    def generate_precision_recall(
        self,
        benchmarking_results: List[BenchmarkRunResults],
        benchmark_generator: BenchmarkRunOutputGenerator,
    ):
        """
        Generate and plot Precision-Recall curves for binary classification benchmark results.

        Args:
            benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
            benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
        """
        plt.figure()
        for i, benchmark_result in enumerate(benchmarking_results):
            precision, recall, thresh = precision_recall_curve(
                benchmark_result.binary_classification_stats.labels,
                benchmark_result.binary_classification_stats.scores,
            )
            precision_recall_auc = auc(recall, precision)
            plt.plot(
                recall,
                precision,
                label=f"{self.return_benchmark_name(benchmark_result)} Precision-Recall Curve "
                f"(AUC = {precision_recall_auc:.2f})",
                color=self.palette_hex_codes[i],
            )

        plt.plot(linestyle="--", color="gray")
        plt.xlabel("Recall")
        plt.ylabel("Precision")
        plt.title("Precision-Recall Curve")
        plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
        plt.savefig(
            f"{benchmark_generator.prioritisation_type_file_prefix}_precision_recall_curve.svg",
            format="svg",
            bbox_inches="tight",
        )

    def generate_non_cumulative_bar(
        self,
        benchmarking_results: List[BenchmarkRunResults],
        benchmark_generator: BenchmarkRunOutputGenerator,
        title: str = None,
    ) -> None:
        """
        Generate a non-cumulative bar plot.

        Args:
            benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
            benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
            title (str, optional): Title for the generated plot. Defaults to None.
        """
        for benchmark_result in benchmarking_results:
            self._generate_non_cumulative_bar_plot_data(benchmark_result)

        stats_df = pd.DataFrame(self.stats)
        sns.catplot(
            data=stats_df,
            kind="bar",
            x="Rank",
            y="Percentage",
            hue="Run",
            palette=self.palette_hex_codes,
            edgecolor="white",
            legend=False,
        ).set(xlabel="Rank", ylabel=benchmark_generator.y_label)
        plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15), ncol=3, title="Run")
        if title is None:
            plt.title(
                f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} Non-Cumulative Rank Stats"
            )
        else:
            plt.title(title, loc="center", fontsize=15)
        plt.ylim(0, 1)
        plt.savefig(
            f"{benchmark_generator.prioritisation_type_file_prefix}_rank_stats.svg",
            format="svg",
            bbox_inches="tight",
        )

__init__()

Initialise the PlotGenerator class.

Note

self.stats will be used to store statistics data. self.mrr will store Mean Reciprocal Rank (MRR) values. Matplotlib settings are configured to remove the right and top axes spines for generated plots.

Source code in src/pheval/analyse/generate_plots.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
):
    """
    Initialise the PlotGenerator class.
    Note:
        `self.stats` will be used to store statistics data.
        `self.mrr` will store Mean Reciprocal Rank (MRR) values.
        Matplotlib settings are configured to remove the right and top axes spines
        for generated plots.
    """
    self.stats, self.mrr = [], []
    matplotlib.rcParams["axes.spines.right"] = False
    matplotlib.rcParams["axes.spines.top"] = False

generate_cumulative_bar(benchmarking_results, benchmark_generator, title=None)

Generate a cumulative bar plot.

Parameters:

Name Type Description Default
benchmarking_results List[BenchmarkRunResults]

List of benchmarking results for multiple runs.

required
benchmark_generator BenchmarkRunOutputGenerator

Object containing benchmarking output generation details.

required
title str

Title for the generated plot. Defaults to None.

None
Source code in src/pheval/analyse/generate_plots.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def generate_cumulative_bar(
    self,
    benchmarking_results: List[BenchmarkRunResults],
    benchmark_generator: BenchmarkRunOutputGenerator,
    title: str = None,
) -> None:
    """
    Generate a cumulative bar plot.

    Args:
        benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
        benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
        title (str, optional): Title for the generated plot. Defaults to None.
    """
    for benchmark_result in benchmarking_results:
        self._generate_cumulative_bar_plot_data(benchmark_result)
    stats_df = pd.DataFrame(self.stats)
    sns.catplot(
        data=stats_df,
        kind="bar",
        x="Rank",
        y="Percentage",
        hue="Run",
        palette=self.palette_hex_codes,
        edgecolor="white",
        legend=False,
    ).set(xlabel="Rank", ylabel=benchmark_generator.y_label)
    plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15), ncol=3, title="Run")
    if title is None:
        plt.title(
            f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} Cumulative Rank Stats"
        )
    else:
        plt.title(title, loc="center", fontsize=15)
    plt.ylim(0, 1)
    plt.savefig(
        f"{benchmark_generator.prioritisation_type_file_prefix}_rank_stats.svg",
        format="svg",
        bbox_inches="tight",
    )

generate_non_cumulative_bar(benchmarking_results, benchmark_generator, title=None)

Generate a non-cumulative bar plot.

Parameters:

Name Type Description Default
benchmarking_results List[BenchmarkRunResults]

List of benchmarking results for multiple runs.

required
benchmark_generator BenchmarkRunOutputGenerator

Object containing benchmarking output generation details.

required
title str

Title for the generated plot. Defaults to None.

None
Source code in src/pheval/analyse/generate_plots.py
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
def generate_non_cumulative_bar(
    self,
    benchmarking_results: List[BenchmarkRunResults],
    benchmark_generator: BenchmarkRunOutputGenerator,
    title: str = None,
) -> None:
    """
    Generate a non-cumulative bar plot.

    Args:
        benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
        benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
        title (str, optional): Title for the generated plot. Defaults to None.
    """
    for benchmark_result in benchmarking_results:
        self._generate_non_cumulative_bar_plot_data(benchmark_result)

    stats_df = pd.DataFrame(self.stats)
    sns.catplot(
        data=stats_df,
        kind="bar",
        x="Rank",
        y="Percentage",
        hue="Run",
        palette=self.palette_hex_codes,
        edgecolor="white",
        legend=False,
    ).set(xlabel="Rank", ylabel=benchmark_generator.y_label)
    plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15), ncol=3, title="Run")
    if title is None:
        plt.title(
            f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} Non-Cumulative Rank Stats"
        )
    else:
        plt.title(title, loc="center", fontsize=15)
    plt.ylim(0, 1)
    plt.savefig(
        f"{benchmark_generator.prioritisation_type_file_prefix}_rank_stats.svg",
        format="svg",
        bbox_inches="tight",
    )

generate_precision_recall(benchmarking_results, benchmark_generator)

Generate and plot Precision-Recall curves for binary classification benchmark results.

Parameters:

Name Type Description Default
benchmarking_results List[BenchmarkRunResults]

List of benchmarking results for multiple runs.

required
benchmark_generator BenchmarkRunOutputGenerator

Object containing benchmarking output generation details.

required
Source code in src/pheval/analyse/generate_plots.py
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
def generate_precision_recall(
    self,
    benchmarking_results: List[BenchmarkRunResults],
    benchmark_generator: BenchmarkRunOutputGenerator,
):
    """
    Generate and plot Precision-Recall curves for binary classification benchmark results.

    Args:
        benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
        benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
    """
    plt.figure()
    for i, benchmark_result in enumerate(benchmarking_results):
        precision, recall, thresh = precision_recall_curve(
            benchmark_result.binary_classification_stats.labels,
            benchmark_result.binary_classification_stats.scores,
        )
        precision_recall_auc = auc(recall, precision)
        plt.plot(
            recall,
            precision,
            label=f"{self.return_benchmark_name(benchmark_result)} Precision-Recall Curve "
            f"(AUC = {precision_recall_auc:.2f})",
            color=self.palette_hex_codes[i],
        )

    plt.plot(linestyle="--", color="gray")
    plt.xlabel("Recall")
    plt.ylabel("Precision")
    plt.title("Precision-Recall Curve")
    plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
    plt.savefig(
        f"{benchmark_generator.prioritisation_type_file_prefix}_precision_recall_curve.svg",
        format="svg",
        bbox_inches="tight",
    )

generate_roc_curve(benchmarking_results, benchmark_generator)

Generate and plot Receiver Operating Characteristic (ROC) curves for binary classification benchmark results.

Parameters:

Name Type Description Default
benchmarking_results List[BenchmarkRunResults]

List of benchmarking results for multiple runs.

required
benchmark_generator BenchmarkRunOutputGenerator

Object containing benchmarking output generation details.

required
Source code in src/pheval/analyse/generate_plots.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
def generate_roc_curve(
    self,
    benchmarking_results: List[BenchmarkRunResults],
    benchmark_generator: BenchmarkRunOutputGenerator,
):
    """
    Generate and plot Receiver Operating Characteristic (ROC) curves for binary classification benchmark results.

    Args:
        benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
        benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
    """
    for i, benchmark_result in enumerate(benchmarking_results):
        fpr, tpr, thresh = roc_curve(
            benchmark_result.binary_classification_stats.labels,
            benchmark_result.binary_classification_stats.scores,
            pos_label=1,
        )
        roc_auc = auc(fpr, tpr)

        plt.plot(
            fpr,
            tpr,
            label=f"{self.return_benchmark_name(benchmark_result)} ROC Curve (AUC = {roc_auc:.2f})",
            color=self.palette_hex_codes[i],
        )

    plt.plot(linestyle="--", color="gray")
    plt.xlabel("False Positive Rate")
    plt.ylabel("True Positive Rate")
    plt.title("Receiver Operating Characteristic (ROC) Curve")
    plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
    plt.savefig(
        f"{benchmark_generator.prioritisation_type_file_prefix}_roc_curve.svg",
        format="svg",
        bbox_inches="tight",
    )

generate_stacked_bar_plot(benchmarking_results, benchmark_generator, title=None)

Generate a stacked bar plot and Mean Reciprocal Rank (MRR) bar plot.

Parameters:

Name Type Description Default
benchmarking_results List[BenchmarkRunResults]

List of benchmarking results for multiple runs.

required
benchmark_generator BenchmarkRunOutputGenerator

Object containing benchmarking output generation details.

required
title str

Title for the generated plot. Defaults to None.

None
Source code in src/pheval/analyse/generate_plots.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def generate_stacked_bar_plot(
    self,
    benchmarking_results: List[BenchmarkRunResults],
    benchmark_generator: BenchmarkRunOutputGenerator,
    title: str = None,
) -> None:
    """
    Generate a stacked bar plot and Mean Reciprocal Rank (MRR) bar plot.

    Args:
        benchmarking_results (List[BenchmarkRunResults]): List of benchmarking results for multiple runs.
        benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
        title (str, optional): Title for the generated plot. Defaults to None.
    """
    for benchmark_result in benchmarking_results:
        self._generate_stacked_bar_plot_data(benchmark_result)
        self._generate_stats_mrr_bar_plot_data(benchmark_result)
    stats_df = pd.DataFrame(self.stats)
    stats_df.set_index("Run").plot(
        kind="bar",
        stacked=True,
        color=self.palette_hex_codes,
        ylabel=benchmark_generator.y_label,
        edgecolor="white",
    ).legend(loc="center left", bbox_to_anchor=(1.0, 0.5))
    if title is None:
        plt.title(
            f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} Rank Stats"
        )
    else:
        plt.title(title, loc="center", fontsize=15)
    plt.ylim(0, 100)
    plt.savefig(
        f"{benchmark_generator.prioritisation_type_file_prefix}_rank_stats.svg",
        format="svg",
        bbox_inches="tight",
    )

    mrr_df = pd.DataFrame(self.mrr)
    mrr_df.set_index("Run").plot(
        kind="bar",
        color=self.palette_hex_codes,
        ylabel=f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} mean reciprocal rank",
        legend=False,
        edgecolor="white",
    )
    plt.title(
        f"{benchmark_generator.prioritisation_type_file_prefix.capitalize()} results - mean reciprocal rank"
    )
    plt.ylim(0, 1)
    plt.savefig(
        f"{benchmark_generator.prioritisation_type_file_prefix}_mrr.svg",
        format="svg",
        bbox_inches="tight",
    )

return_benchmark_name(benchmark_result)

Return the benchmark name for a run.

Parameters:

Name Type Description Default
benchmark_result BenchmarkRunResults

The benchmarking results for a run.

required

Returns:

Name Type Description
str str

The benchmark name obtained from the given BenchmarkRunResults instance.

Source code in src/pheval/analyse/generate_plots.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def return_benchmark_name(self, benchmark_result: BenchmarkRunResults) -> str:
    """
    Return the benchmark name for a run.

    Args:
        benchmark_result (BenchmarkRunResults): The benchmarking results for a run.

    Returns:
        str: The benchmark name obtained from the given BenchmarkRunResults instance.
    """
    return (
        benchmark_result.benchmark_name
        if benchmark_result.results_dir is None
        else self._create_run_identifier(benchmark_result.results_dir)
    )

generate_plots(benchmarking_results, benchmark_generator, plot_type, title=None, generate_from_tsv=False)

Generate summary statistics bar plots for prioritisation.

This method generates summary statistics bar plots based on the provided benchmarking results and plot type.

Parameters:

Name Type Description Default
benchmarking_results list[BenchmarkRunResults]

List of benchmarking results for multiple runs.

required
benchmark_generator BenchmarkRunOutputGenerator

Object containing benchmarking output generation details.

required
plot_type str

Type of plot to be generated ("bar_stacked", "bar_cumulative", "bar_non_cumulative").

required
title str

Title for the generated plot. Defaults to None.

None
generate_from_tsv bool

Specify whether to generate plots from the TSV file. Defaults to False.

False
Source code in src/pheval/analyse/generate_plots.py
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
def generate_plots(
    benchmarking_results: List[BenchmarkRunResults],
    benchmark_generator: BenchmarkRunOutputGenerator,
    plot_type: str,
    title: str = None,
    generate_from_tsv: bool = False,
) -> None:
    """
    Generate summary statistics bar plots for prioritisation.

    This method generates summary statistics bar plots based on the provided benchmarking results and plot type.

    Args:
        benchmarking_results (list[BenchmarkRunResults]): List of benchmarking results for multiple runs.
        benchmark_generator (BenchmarkRunOutputGenerator): Object containing benchmarking output generation details.
        plot_type (str): Type of plot to be generated ("bar_stacked", "bar_cumulative", "bar_non_cumulative").
        title (str, optional): Title for the generated plot. Defaults to None.
        generate_from_tsv (bool): Specify whether to generate plots from the TSV file. Defaults to False.
    """
    plot_generator = PlotGenerator()
    if not generate_from_tsv:
        plot_generator.generate_roc_curve(benchmarking_results, benchmark_generator)
        plot_generator.generate_precision_recall(benchmarking_results, benchmark_generator)
    if plot_type == "bar_stacked":
        plot_generator.generate_stacked_bar_plot(benchmarking_results, benchmark_generator, title)
    elif plot_type == "bar_cumulative":
        plot_generator.generate_cumulative_bar(benchmarking_results, benchmark_generator, title)
    elif plot_type == "bar_non_cumulative":
        plot_generator.generate_non_cumulative_bar(benchmarking_results, benchmark_generator, title)

generate_plots_from_benchmark_summary_tsv(benchmark_summary_tsv, gene_analysis, variant_analysis, disease_analysis, plot_type, title)

Generate bar plot from summary benchmark results.

Reads a summary of benchmark results from a TSV file and generates a bar plot based on the analysis type and plot type.

Parameters:

Name Type Description Default
benchmark_summary_tsv Path

Path to the summary TSV file containing benchmark results.

required
gene_analysis bool

Flag indicating whether to analyse gene prioritisation.

required
variant_analysis bool

Flag indicating whether to analyse variant prioritisation.

required
disease_analysis bool

Flag indicating whether to analyse disease prioritisation.

required
plot_type str

Type of plot to be generated ("bar_stacked", "bar_cumulative", "bar_non_cumulative").

required
title str

Title for the generated plot.

required

Raises:

Type Description
ValueError

If an unsupported plot type is specified.

Source code in src/pheval/analyse/generate_plots.py
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
def generate_plots_from_benchmark_summary_tsv(
    benchmark_summary_tsv: Path,
    gene_analysis: bool,
    variant_analysis: bool,
    disease_analysis: bool,
    plot_type: str,
    title: str,
):
    """
    Generate bar plot from summary benchmark results.

    Reads a summary of benchmark results from a TSV file and generates a bar plot
    based on the analysis type and plot type.

    Args:
        benchmark_summary_tsv (Path): Path to the summary TSV file containing benchmark results.
        gene_analysis (bool): Flag indicating whether to analyse gene prioritisation.
        variant_analysis (bool): Flag indicating whether to analyse variant prioritisation.
        disease_analysis (bool): Flag indicating whether to analyse disease prioritisation.
        plot_type (str): Type of plot to be generated ("bar_stacked", "bar_cumulative", "bar_non_cumulative").
        title (str): Title for the generated plot.
    Raises:
         ValueError: If an unsupported plot type is specified.
    """
    benchmark_stats_summary = read_benchmark_tsv_result_summary(benchmark_summary_tsv)
    benchmarking_results = parse_benchmark_result_summary(benchmark_stats_summary)
    if gene_analysis:
        benchmark_generator = GeneBenchmarkRunOutputGenerator()
    elif variant_analysis:
        benchmark_generator = VariantBenchmarkRunOutputGenerator()
    elif disease_analysis:
        benchmark_generator = DiseaseBenchmarkRunOutputGenerator()
    else:
        raise ValueError(
            "Specify one analysis type (gene_analysis, variant_analysis, or disease_analysis)"
        )
    generate_plots(benchmarking_results, benchmark_generator, plot_type, title, True)

trim_corpus_results_directory_suffix(corpus_results_directory)

Trim the suffix from the corpus results directory name.

Parameters:

Name Type Description Default
corpus_results_directory Path

The directory path containing corpus results.

required

Returns:

Name Type Description
Path Path

The Path object with the suffix removed from the directory name.

Source code in src/pheval/analyse/generate_plots.py
24
25
26
27
28
29
30
31
32
33
34
def trim_corpus_results_directory_suffix(corpus_results_directory: Path) -> Path:
    """
    Trim the suffix from the corpus results directory name.

    Args:
        corpus_results_directory (Path): The directory path containing corpus results.

    Returns:
        Path: The Path object with the suffix removed from the directory name.
    """
    return Path(str(corpus_results_directory).replace(PHEVAL_RESULTS_DIRECTORY_SUFFIX, ""))