From db2469a55e67b64ef4c9132506ef63dedb8e62b6 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 27 Nov 2023 12:57:12 +0100 Subject: [PATCH] :art: center swarmplot labels --- project/10_4_ald_compare_single_pg.ipynb | 35 +++++++++++++++--------- project/10_4_ald_compare_single_pg.py | 35 +++++++++++++++--------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/project/10_4_ald_compare_single_pg.ipynb b/project/10_4_ald_compare_single_pg.ipynb index 0b60a92c0..3de5bb57b 100644 --- a/project/10_4_ald_compare_single_pg.ipynb +++ b/project/10_4_ald_compare_single_pg.ipynb @@ -625,11 +625,20 @@ " tmp_dot.remove()\n", "\n", " feat_observed = data[idx].dropna()\n", - " label_template = '{method}\\n(N={n:,d}, q={q:.3f})'\n", - " key = label_template.format(method='observed',\n", - " n=len(feat_observed),\n", - " q=float(qvalues.loc[idx, ('None', 'qvalue')])\n", - " )\n", + "\n", + " def get_centered_label(method, n, q):\n", + " model_str = f'{method}'\n", + " stats_str = f'(N={n:,d}, q={q:.3f})'\n", + " if len(model_str) > len(stats_str):\n", + " stats_str = f\"{stats_str:<{len(model_str)}}\"\n", + " else:\n", + " model_str = f\"{model_str:<{len(stats_str)}}\"\n", + " return f'{model_str}\\n{stats_str}'\n", + "\n", + " key = get_centered_label(method='observed',\n", + " n=len(feat_observed),\n", + " q=float(qvalues.loc[idx, ('None', 'qvalue')])\n", + " )\n", " to_plot = {key: feat_observed}\n", " for method in model_keys:\n", " try:\n", @@ -637,15 +646,15 @@ " idx], method].dropna().droplevel(-1)\n", " if len(pred) == 0:\n", " # in case no values was imputed -> qvalue is as based on measured\n", - " key = label_template.format(method=method,\n", - " n=len(pred),\n", - " q=float(qvalues.loc[idx, ('None', 'qvalue')]\n", - " ))\n", + " key = get_centered_label(method=method,\n", + " n=len(pred),\n", + " q=float(qvalues.loc[idx, ('None', 'qvalue')]\n", + " ))\n", " elif qvalues.loc[idx, (method, 'qvalue')].notna().all():\n", - " key = label_template.format(method=method,\n", - " n=len(pred),\n", - " q=float(qvalues.loc[idx, (method, 'qvalue')]\n", - " ))\n", + " key = get_centered_label(method=method,\n", + " n=len(pred),\n", + " q=float(qvalues.loc[idx, (method, 'qvalue')]\n", + " ))\n", " elif qvalues.loc[idx, (method, 'qvalue')].isna().all():\n", " logger.info(f\"NA qvalues for {idx}: {method}\")\n", " continue\n", diff --git a/project/10_4_ald_compare_single_pg.py b/project/10_4_ald_compare_single_pg.py index 9e50574b9..96ba6f7c6 100644 --- a/project/10_4_ald_compare_single_pg.py +++ b/project/10_4_ald_compare_single_pg.py @@ -332,11 +332,20 @@ tmp_dot.remove() feat_observed = data[idx].dropna() - label_template = '{method}\n(N={n:,d}, q={q:.3f})' - key = label_template.format(method='observed', - n=len(feat_observed), - q=float(qvalues.loc[idx, ('None', 'qvalue')]) - ) + + def get_centered_label(method, n, q): + model_str = f'{method}' + stats_str = f'(N={n:,d}, q={q:.3f})' + if len(model_str) > len(stats_str): + stats_str = f"{stats_str:<{len(model_str)}}" + else: + model_str = f"{model_str:<{len(stats_str)}}" + return f'{model_str}\n{stats_str}' + + key = get_centered_label(method='observed', + n=len(feat_observed), + q=float(qvalues.loc[idx, ('None', 'qvalue')]) + ) to_plot = {key: feat_observed} for method in model_keys: try: @@ -344,15 +353,15 @@ idx], method].dropna().droplevel(-1) if len(pred) == 0: # in case no values was imputed -> qvalue is as based on measured - key = label_template.format(method=method, - n=len(pred), - q=float(qvalues.loc[idx, ('None', 'qvalue')] - )) + key = get_centered_label(method=method, + n=len(pred), + q=float(qvalues.loc[idx, ('None', 'qvalue')] + )) elif qvalues.loc[idx, (method, 'qvalue')].notna().all(): - key = label_template.format(method=method, - n=len(pred), - q=float(qvalues.loc[idx, (method, 'qvalue')] - )) + key = get_centered_label(method=method, + n=len(pred), + q=float(qvalues.loc[idx, (method, 'qvalue')] + )) elif qvalues.loc[idx, (method, 'qvalue')].isna().all(): logger.info(f"NA qvalues for {idx}: {method}") continue