Skip to content

Commit

Permalink
Merge pull request #186 from ACCLAB/feat-contrast-bars
Browse files Browse the repository at this point in the history
Contrast bar feature and trimming plotter.py (refactoring)
  • Loading branch information
Jacobluke- authored Sep 23, 2024
2 parents d047528 + 5ad5a06 commit d5e9c58
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 138 deletions.
38 changes: 15 additions & 23 deletions dabest/misc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,41 +542,33 @@ def get_plot_groups(is_paired, idx, proportional, all_plot_groups):
def add_counts_to_ticks(plot_data, xvar, yvar, rawdata_axes, plot_kwargs):
# Add the counts to the rawdata axes xticks.
counts = plot_data.groupby(xvar).count()[yvar]
ticks_with_counts = []
ticks_loc = rawdata_axes.get_xticks()
rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))
def lookup_value(text, counts):

def lookup_value(text):
try:
return str(counts.loc[text])
except KeyError:
try:
numeric_key = pd.to_numeric(text, errors='coerce')
if pd.notnull(numeric_key):
return str(counts.loc[numeric_key])
else:
raise ValueError
except (ValueError, KeyError):
print(f"Key '{text}' not found in counts.")
return "N/A"
for xticklab in rawdata_axes.xaxis.get_ticklabels():
t = xticklab.get_text()
# Extract the text after the last newline, if present
if t.rfind("\n") != -1:
te = t[t.rfind("\n") + len("\n"):]
value = lookup_value(te, counts)
te = t
else:
te = t
value = lookup_value(te, counts)

# Append the modified tick label with the count to the list
ticks_with_counts.append(f"{te}\nN = {value}")
pass
print(f"Key '{text}' not found in counts.")
return "N/A"

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

if plot_kwargs["fontsize_rawxlabel"] is not None:
fontsize_rawxlabel = plot_kwargs["fontsize_rawxlabel"]
fontsize_rawxlabel = plot_kwargs.get("fontsize_rawxlabel")
rawdata_axes.set_xticklabels(ticks_with_counts, fontsize=fontsize_rawxlabel)

# Ensure ticks are at the correct locations
rawdata_axes.xaxis.set_major_locator(plt.FixedLocator(rawdata_axes.get_xticks()))


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

Expand Down
2 changes: 0 additions & 2 deletions dabest/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
if color_col is None:
rawdata_plot.legend().set_visible(False)


else:
# Plot the raw data as a barplot.
barplotter(
Expand Down Expand Up @@ -589,5 +588,4 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
plt.rcParams[parameter] = original_rcParams[parameter]

# Return the figure.
fig.show()
return fig
38 changes: 15 additions & 23 deletions nbs/API/misc_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -595,41 +595,33 @@
"def add_counts_to_ticks(plot_data, xvar, yvar, rawdata_axes, plot_kwargs):\n",
" # Add the counts to the rawdata axes xticks.\n",
" counts = plot_data.groupby(xvar).count()[yvar]\n",
" ticks_with_counts = []\n",
" ticks_loc = rawdata_axes.get_xticks()\n",
" rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))\n",
" def lookup_value(text, counts):\n",
" \n",
" def lookup_value(text):\n",
" try:\n",
" return str(counts.loc[text])\n",
" except KeyError:\n",
" try:\n",
" numeric_key = pd.to_numeric(text, errors='coerce')\n",
" if pd.notnull(numeric_key):\n",
" return str(counts.loc[numeric_key])\n",
" else:\n",
" raise ValueError\n",
" except (ValueError, KeyError):\n",
" print(f\"Key '{text}' not found in counts.\")\n",
" return \"N/A\"\n",
" for xticklab in rawdata_axes.xaxis.get_ticklabels():\n",
" t = xticklab.get_text()\n",
" # Extract the text after the last newline, if present\n",
" if t.rfind(\"\\n\") != -1:\n",
" te = t[t.rfind(\"\\n\") + len(\"\\n\"):]\n",
" value = lookup_value(te, counts)\n",
" te = t\n",
" else:\n",
" te = t\n",
" value = lookup_value(te, counts)\n",
"\n",
" # Append the modified tick label with the count to the list\n",
" ticks_with_counts.append(f\"{te}\\nN = {value}\")\n",
" pass\n",
" print(f\"Key '{text}' not found in counts.\")\n",
" return \"N/A\"\n",
"\n",
" ticks_with_counts = []\n",
" for xticklab in rawdata_axes.get_xticklabels():\n",
" t = xticklab.get_text()\n",
" te = t.split('\\n')[-1] # Get the last line of the label\n",
" value = lookup_value(te)\n",
" ticks_with_counts.append(f\"{t}\\nN = {value}\")\n",
"\n",
" if plot_kwargs[\"fontsize_rawxlabel\"] is not None:\n",
" fontsize_rawxlabel = plot_kwargs[\"fontsize_rawxlabel\"]\n",
" fontsize_rawxlabel = plot_kwargs.get(\"fontsize_rawxlabel\")\n",
" rawdata_axes.set_xticklabels(ticks_with_counts, fontsize=fontsize_rawxlabel)\n",
"\n",
" # Ensure ticks are at the correct locations\n",
" rawdata_axes.xaxis.set_major_locator(plt.FixedLocator(rawdata_axes.get_xticks()))\n",
"\n",
"\n",
"def extract_contrast_plotting_ticks(is_paired, show_pairs, two_col_sankey, plot_groups, idx, sankey_control_group):\n",
"\n",
Expand Down
2 changes: 0 additions & 2 deletions nbs/API/plotter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@
" if color_col is None:\n",
" rawdata_plot.legend().set_visible(False)\n",
"\n",
"\n",
" else:\n",
" # Plot the raw data as a barplot.\n",
" barplotter(\n",
Expand Down Expand Up @@ -646,7 +645,6 @@
" plt.rcParams[parameter] = original_rcParams[parameter]\n",
"\n",
" # Return the figure.\n",
" fig.show()\n",
" return fig"
]
}
Expand Down
88 changes: 0 additions & 88 deletions test.py

This file was deleted.

0 comments on commit d5e9c58

Please sign in to comment.