Skip to content

Commit bee5147

Browse files
author
Petr Laštovička
committed
feat: Add support for small (1 subfigure) graphs
1 parent 6dce68d commit bee5147

2 files changed

Lines changed: 77 additions & 25 deletions

File tree

benchmarks/NAC_benchmark.ipynb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88
"\n",
99
"In this notebook we provide utils to run benchmarks and experiment with our code.\n",
1010
"\n",
11-
"In the first section we start with utility functions, in the second part we load/generate benchmark data. After we run individual benchmarks on selected graph classes with selected algorithms. The algorithms are described in that section.\n",
12-
"\n",
13-
"```bash\n",
14-
"tensorboard --logdir benchmarks/logs/nac\n",
11+
"In the first section we start with utility functions, in the second part we load/generate benchmark data. After we run individual benchmarks on selected graph classes with selected algorithms. The algorithms are described in that section."
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"If you are using VScode, add this option to your `.vscode/settings.json` file.\n",
19+
"```json\n",
20+
"{\n",
21+
" \"jupyter.notebookFileRoot\": \"${workspaceFolder}\"\n",
22+
"}\n",
1523
"```"
1624
]
1725
},
@@ -1790,7 +1798,7 @@
17901798
"name": "python",
17911799
"nbconvert_exporter": "python",
17921800
"pygments_lexer": "ipython3",
1793-
"version": "3.12.6"
1801+
"version": "3.12.8"
17941802
}
17951803
},
17961804
"nbformat": 4,

benchmarks/NAC_latex_export.ipynb

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@
1919
"The `nac` package itself is completely independent."
2020
]
2121
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"If you are using VScode, add this option to your `.vscode/settings.json` file.\n",
27+
"```json\n",
28+
"{\n",
29+
" \"jupyter.notebookFileRoot\": \"${workspaceFolder}\"\n",
30+
"}\n",
31+
"```"
32+
]
33+
},
2234
{
2335
"cell_type": "code",
24-
"execution_count": null,
36+
"execution_count": 119,
2537
"metadata": {},
2638
"outputs": [],
2739
"source": [
@@ -76,7 +88,7 @@
7688
},
7789
{
7890
"cell_type": "code",
79-
"execution_count": null,
91+
"execution_count": 120,
8092
"metadata": {},
8193
"outputs": [],
8294
"source": [
@@ -89,16 +101,17 @@
89101
},
90102
{
91103
"cell_type": "code",
92-
"execution_count": null,
104+
"execution_count": 121,
93105
"metadata": {},
94106
"outputs": [],
95107
"source": [
96108
"# https://jwalton.info/Embed-Publication-Matplotlib-Latex/\n",
97109
"import matplotlib.backends.backend_pgf\n",
98110
"\n",
111+
"DEFAULT_FIG_WIDTH = 398.33858\n",
99112
"\n",
100113
"def fig_size(\n",
101-
" width: float = 398.33858,\n",
114+
" width: float = DEFAULT_FIG_WIDTH,\n",
102115
" fraction: float = 2,\n",
103116
" subplots: Tuple[int,int] = (1, 1),\n",
104117
" ):\n",
@@ -117,6 +130,7 @@
117130
" fig_dim: tuple\n",
118131
" Dimensions of figure in inches\n",
119132
" \"\"\"\n",
133+
"\n",
120134
" # Width of figure (in pts)\n",
121135
" fig_width_pt = width * fraction\n",
122136
" # Convert from pt to inches\n",
@@ -131,6 +145,10 @@
131145
" # Figure height in inches\n",
132146
" fig_height_in = fig_width_in * golden_ratio * (subplots[0] / subplots[1])\n",
133147
"\n",
148+
" if subplots == (1, 1):\n",
149+
" fig_width_in *= 3/4\n",
150+
" fig_height_in *= 2/5\n",
151+
"\n",
134152
" return (fig_width_in, fig_height_in)\n",
135153
"\n",
136154
"\n",
@@ -173,7 +191,7 @@
173191
},
174192
{
175193
"cell_type": "code",
176-
"execution_count": null,
194+
"execution_count": 122,
177195
"metadata": {},
178196
"outputs": [],
179197
"source": [
@@ -249,19 +267,20 @@
249267
},
250268
{
251269
"cell_type": "code",
252-
"execution_count": null,
270+
"execution_count": 124,
253271
"metadata": {},
254272
"outputs": [],
255273
"source": [
274+
"\n",
256275
"def _group_and_plot(\n",
257276
" df: pd.DataFrame,\n",
258277
" with_log: bool,\n",
259278
" axs: List[plt.Axes],\n",
279+
" aggregations: List[Literal[\"mean\", \"median\", \"3rd quartile\"]],\n",
260280
" x_column: Literal[\"vertex_no\", \"monochromatic_classes_no\"],\n",
261281
" based_on: Literal[\"relabel\", \"split\", \"merging\"],\n",
262282
" value_columns: List[Literal[\"nac_first_mean_time\", \"nac_all_mean_time\"]],\n",
263283
"):\n",
264-
" aggregations = [\"mean\", \"median\", \"3rd quartile\"]\n",
265284
" df = df.loc[:, [x_column, based_on, *value_columns]]\n",
266285
" groupped = df.groupby([x_column, based_on])\n",
267286
"\n",
@@ -295,7 +314,18 @@
295314
" ax.set_yscale(\"log\")\n",
296315
" ax.xaxis.set_major_locator(MaxNLocator(integer=True))\n",
297316
" ax.set_xlabel(rename_based_on[x_column])\n",
298-
" ax.legend(loc='upper left')\n",
317+
"\n",
318+
"\n",
319+
"\n",
320+
" if len(aggregations) == 1:\n",
321+
" ax.legend(\n",
322+
" bbox_to_anchor=(1.0, 1.0),\n",
323+
" loc='upper left',\n",
324+
" )\n",
325+
" else:\n",
326+
" ax.legend(\n",
327+
" loc='upper left',\n",
328+
" )\n",
299329
"\n",
300330
"def plot_frame(\n",
301331
" title: str,\n",
@@ -314,7 +344,8 @@
314344
" # \"merging\",\n",
315345
" \"split_merging\",\n",
316346
" ],\n",
317-
" ops_aggregation = [\"mean\", \"median\",], # \"3rd quartile\",\n",
347+
" # ops_aggregation = [\"mean\", \"median\",], # \"3rd quartile\",\n",
348+
" ops_aggregation = [\"mean\"], # \"3rd quartile\",\n",
318349
") -> List[Figure]:\n",
319350
" print(f\"Plotting {df.shape[0]} records...\")\n",
320351
" figs = []\n",
@@ -337,7 +368,10 @@
337368
" ncols = len(ops_aggregation)\n",
338369
" fig = figure(\n",
339370
" nrows * ncols,\n",
340-
" figsize=fig_size(subplots=(nrows, ncols)),\n",
371+
" figsize=fig_size(\n",
372+
" # width=DEFAULT_FIG_WIDTH if len(ops_aggregation) > 1 else DEFAULT_FIG_WIDTH * 3/4,\n",
373+
" subplots=(nrows, ncols),\n",
374+
" ),\n",
341375
" layout='constrained',\n",
342376
" )\n",
343377
" title_detail = \" | \".join(title_rename[value_column] for value_column in value_columns)\n",
@@ -349,7 +383,7 @@
349383
" axs = [\n",
350384
" fig.add_subplot(nrows, ncols, i+ncols*row+1)\n",
351385
" for i in range(len(ops_aggregation))]\n",
352-
" _group_and_plot(local_df, with_log, axs, x_column, based_on, value_columns)\n",
386+
" _group_and_plot(local_df, with_log, axs, ops_aggregation, x_column, based_on, value_columns)\n",
353387
" row += 1\n",
354388
" return figs\n",
355389
"\n",
@@ -372,7 +406,7 @@
372406
},
373407
{
374408
"cell_type": "code",
375-
"execution_count": null,
409+
"execution_count": 126,
376410
"metadata": {},
377411
"outputs": [],
378412
"source": [
@@ -386,7 +420,7 @@
386420
},
387421
{
388422
"cell_type": "code",
389-
"execution_count": null,
423+
"execution_count": 127,
390424
"metadata": {},
391425
"outputs": [],
392426
"source": [
@@ -459,7 +493,7 @@
459493
},
460494
{
461495
"cell_type": "code",
462-
"execution_count": null,
496+
"execution_count": 130,
463497
"metadata": {},
464498
"outputs": [],
465499
"source": [
@@ -471,6 +505,7 @@
471505
" value_columns: List[Literal[\"nac_first_mean_time\", \"nac_all_mean_time\"]],\n",
472506
" aggregation: Literal[\"mean\", \"median\", \"3rd quartile\"],\n",
473507
" legend_rename_dict: Dict[str, str] = {},\n",
508+
" legend_outside: bool = False,\n",
474509
"):\n",
475510
" df = df.loc[:, [x_column, *value_columns]]\n",
476511
" groupped = df.groupby([x_column])\n",
@@ -496,11 +531,19 @@
496531
" ax.xaxis.set_major_locator(MaxNLocator(integer=True))\n",
497532
" ax.set_xlabel(rename_based_on[x_column])\n",
498533
" handles, labels = ax.get_legend_handles_labels()\n",
499-
" ax.legend(\n",
534+
" if legend_outside:\n",
535+
" ax.legend(\n",
500536
" handles,\n",
501537
" [legend_rename_dict[l] for l in labels],\n",
502-
" # loc = 'upper left',\n",
503-
" )\n",
538+
" bbox_to_anchor=(1.0, 1.0),\n",
539+
" loc='upper left',\n",
540+
" )\n",
541+
" else:\n",
542+
" ax.legend(\n",
543+
" handles,\n",
544+
" [legend_rename_dict[l] for l in labels],\n",
545+
" # loc = 'upper left',\n",
546+
" )\n",
504547
"\n",
505548
"def plot_is_NAC_coloring_calls(\n",
506549
" df: pd.DataFrame,\n",
@@ -567,7 +610,8 @@
567610
" # [\"inv_edge_no\", \"inv_triangle_component_no\", \"inv_monochromatic_class_no\", \"inv_nac_all_check_cycle_mask\", \"inv_nac_all_check_is_NAC\", ],\n",
568611
" # [\"new_edge_no\", \"new_triangle_component_no\", \"new_monochromatic_class_no\", \"new_nac_all_check_cycle_mask\" ],\n",
569612
" ]\n",
570-
" ops_aggregation = [\"mean\", \"median\", ] # \"3rd quartile\",\n",
613+
" # ops_aggregation = [\"mean\", \"median\", ] # \"3rd quartile\",\n",
614+
" ops_aggregation = [\"mean\" ] # \"3rd quartile\",\n",
571615
"\n",
572616
" nrows = len(ops_value_groups)\n",
573617
" ncols = len(ops_aggregation)\n",
@@ -595,7 +639,7 @@
595639
" fig.add_subplot(nrows, ncols, i+ncols*row+1)\n",
596640
" for i in range(len(ops_aggregation))]\n",
597641
" for ax, aggregation in zip(axs,ops_aggregation):\n",
598-
" _plot_is_NAC_coloring_calls_groups(title, df, ax, x_column, value_columns, aggregation, legend_rename_dict=rename_dict)\n",
642+
" _plot_is_NAC_coloring_calls_groups(title, df, ax, x_column, value_columns, aggregation, legend_rename_dict=rename_dict, legend_outside=len(ops_aggregation) <= 1)\n",
599643
" row += 1\n",
600644
"\n",
601645
" return figs"
@@ -633,7 +677,7 @@
633677
"name": "python",
634678
"nbconvert_exporter": "python",
635679
"pygments_lexer": "ipython3",
636-
"version": "3.12.6"
680+
"version": "3.12.8"
637681
}
638682
},
639683
"nbformat": 4,

0 commit comments

Comments
 (0)