Skip to content

Commit d5e9c58

Browse files
authored
Merge pull request #186 from ACCLAB/feat-contrast-bars
Contrast bar feature and trimming plotter.py (refactoring)
2 parents d047528 + 5ad5a06 commit d5e9c58

File tree

5 files changed

+30
-138
lines changed

5 files changed

+30
-138
lines changed

dabest/misc_tools.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -542,41 +542,33 @@ def get_plot_groups(is_paired, idx, proportional, all_plot_groups):
542542
def add_counts_to_ticks(plot_data, xvar, yvar, rawdata_axes, plot_kwargs):
543543
# Add the counts to the rawdata axes xticks.
544544
counts = plot_data.groupby(xvar).count()[yvar]
545-
ticks_with_counts = []
546-
ticks_loc = rawdata_axes.get_xticks()
547-
rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))
548-
def lookup_value(text, counts):
545+
546+
def lookup_value(text):
549547
try:
550548
return str(counts.loc[text])
551549
except KeyError:
552550
try:
553551
numeric_key = pd.to_numeric(text, errors='coerce')
554552
if pd.notnull(numeric_key):
555553
return str(counts.loc[numeric_key])
556-
else:
557-
raise ValueError
558554
except (ValueError, KeyError):
559-
print(f"Key '{text}' not found in counts.")
560-
return "N/A"
561-
for xticklab in rawdata_axes.xaxis.get_ticklabels():
562-
t = xticklab.get_text()
563-
# Extract the text after the last newline, if present
564-
if t.rfind("\n") != -1:
565-
te = t[t.rfind("\n") + len("\n"):]
566-
value = lookup_value(te, counts)
567-
te = t
568-
else:
569-
te = t
570-
value = lookup_value(te, counts)
571-
572-
# Append the modified tick label with the count to the list
573-
ticks_with_counts.append(f"{te}\nN = {value}")
555+
pass
556+
print(f"Key '{text}' not found in counts.")
557+
return "N/A"
574558

559+
ticks_with_counts = []
560+
for xticklab in rawdata_axes.get_xticklabels():
561+
t = xticklab.get_text()
562+
te = t.split('\n')[-1] # Get the last line of the label
563+
value = lookup_value(te)
564+
ticks_with_counts.append(f"{t}\nN = {value}")
575565

576-
if plot_kwargs["fontsize_rawxlabel"] is not None:
577-
fontsize_rawxlabel = plot_kwargs["fontsize_rawxlabel"]
566+
fontsize_rawxlabel = plot_kwargs.get("fontsize_rawxlabel")
578567
rawdata_axes.set_xticklabels(ticks_with_counts, fontsize=fontsize_rawxlabel)
579568

569+
# Ensure ticks are at the correct locations
570+
rawdata_axes.xaxis.set_major_locator(plt.FixedLocator(rawdata_axes.get_xticks()))
571+
580572

581573
def extract_contrast_plotting_ticks(is_paired, show_pairs, two_col_sankey, plot_groups, idx, sankey_control_group):
582574

dabest/plotter.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
246246
if color_col is None:
247247
rawdata_plot.legend().set_visible(False)
248248

249-
250249
else:
251250
# Plot the raw data as a barplot.
252251
barplotter(
@@ -589,5 +588,4 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
589588
plt.rcParams[parameter] = original_rcParams[parameter]
590589

591590
# Return the figure.
592-
fig.show()
593591
return fig

nbs/API/misc_tools.ipynb

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -595,41 +595,33 @@
595595
"def add_counts_to_ticks(plot_data, xvar, yvar, rawdata_axes, plot_kwargs):\n",
596596
" # Add the counts to the rawdata axes xticks.\n",
597597
" counts = plot_data.groupby(xvar).count()[yvar]\n",
598-
" ticks_with_counts = []\n",
599-
" ticks_loc = rawdata_axes.get_xticks()\n",
600-
" rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))\n",
601-
" def lookup_value(text, counts):\n",
598+
" \n",
599+
" def lookup_value(text):\n",
602600
" try:\n",
603601
" return str(counts.loc[text])\n",
604602
" except KeyError:\n",
605603
" try:\n",
606604
" numeric_key = pd.to_numeric(text, errors='coerce')\n",
607605
" if pd.notnull(numeric_key):\n",
608606
" return str(counts.loc[numeric_key])\n",
609-
" else:\n",
610-
" raise ValueError\n",
611607
" except (ValueError, KeyError):\n",
612-
" print(f\"Key '{text}' not found in counts.\")\n",
613-
" return \"N/A\"\n",
614-
" for xticklab in rawdata_axes.xaxis.get_ticklabels():\n",
615-
" t = xticklab.get_text()\n",
616-
" # Extract the text after the last newline, if present\n",
617-
" if t.rfind(\"\\n\") != -1:\n",
618-
" te = t[t.rfind(\"\\n\") + len(\"\\n\"):]\n",
619-
" value = lookup_value(te, counts)\n",
620-
" te = t\n",
621-
" else:\n",
622-
" te = t\n",
623-
" value = lookup_value(te, counts)\n",
624-
"\n",
625-
" # Append the modified tick label with the count to the list\n",
626-
" ticks_with_counts.append(f\"{te}\\nN = {value}\")\n",
608+
" pass\n",
609+
" print(f\"Key '{text}' not found in counts.\")\n",
610+
" return \"N/A\"\n",
627611
"\n",
612+
" ticks_with_counts = []\n",
613+
" for xticklab in rawdata_axes.get_xticklabels():\n",
614+
" t = xticklab.get_text()\n",
615+
" te = t.split('\\n')[-1] # Get the last line of the label\n",
616+
" value = lookup_value(te)\n",
617+
" ticks_with_counts.append(f\"{t}\\nN = {value}\")\n",
628618
"\n",
629-
" if plot_kwargs[\"fontsize_rawxlabel\"] is not None:\n",
630-
" fontsize_rawxlabel = plot_kwargs[\"fontsize_rawxlabel\"]\n",
619+
" fontsize_rawxlabel = plot_kwargs.get(\"fontsize_rawxlabel\")\n",
631620
" rawdata_axes.set_xticklabels(ticks_with_counts, fontsize=fontsize_rawxlabel)\n",
632621
"\n",
622+
" # Ensure ticks are at the correct locations\n",
623+
" rawdata_axes.xaxis.set_major_locator(plt.FixedLocator(rawdata_axes.get_xticks()))\n",
624+
"\n",
633625
"\n",
634626
"def extract_contrast_plotting_ticks(is_paired, show_pairs, two_col_sankey, plot_groups, idx, sankey_control_group):\n",
635627
"\n",

nbs/API/plotter.ipynb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@
303303
" if color_col is None:\n",
304304
" rawdata_plot.legend().set_visible(False)\n",
305305
"\n",
306-
"\n",
307306
" else:\n",
308307
" # Plot the raw data as a barplot.\n",
309308
" barplotter(\n",
@@ -646,7 +645,6 @@
646645
" plt.rcParams[parameter] = original_rcParams[parameter]\n",
647646
"\n",
648647
" # Return the figure.\n",
649-
" fig.show()\n",
650648
" return fig"
651649
]
652650
}

test.py

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)