Skip to content

Commit d712e8e

Browse files
committed
Fix non-string input issue
Fix the bug for issue #108
1 parent 4b928b7 commit d712e8e

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

dabest/plot_tools.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,23 @@ def error_bar(
173173

174174
kwargs["zorder"] = kwargs["zorder"]
175175

176-
for xpos, central_measure in enumerate(central_measures):
176+
for xpos, val in enumerate(central_measures.index):
177+
central_measure = central_measures[val]
177178
kwargs["color"] = custom_palette[xpos]
178179

179180
if method == "sankey_error_bar":
180181
_xpos = pos[xpos] + offset[xpos]
181182
else:
182183
_xpos = xpos + offset[xpos]
183184

184-
low = lows[xpos]
185-
high = highs[xpos]
185+
# Fix for the non-string x-axis issue #108
186+
if central_measures.index.dtype.name == "category":
187+
low = lows[xpos]
188+
high = highs[xpos]
189+
else:
190+
low = lows[val]
191+
high = highs[val]
192+
186193
if low == high == central_measure:
187194
low_to_mean = mlines.Line2D(
188195
[_xpos, _xpos], [low, central_measure], **kwargs

dabest/plotter.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,15 +807,31 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
807807
rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))
808808
for xticklab in rawdata_axes.xaxis.get_ticklabels():
809809
t = xticklab.get_text()
810-
if t.rfind("\n") != -1:
811-
te = t[t.rfind("\n") + len("\n") :]
812-
N = str(counts.loc[te])
813-
te = t
814-
else:
815-
te = t
810+
# Extract the text after the last newline, if present
811+
te = t[t.rfind("\n") + len("\n"):] if t.rfind("\n") != -1 else t
812+
813+
try:
814+
# Try to access 'counts' directly with 'te'.
816815
N = str(counts.loc[te])
816+
except KeyError:
817+
# If direct access fails, attempt a numeric interpretation.
818+
try:
819+
# Attempt to convert 'te' to numeric (float or int, as appropriate)
820+
numeric_key = pd.to_numeric(te, errors='coerce')
821+
# 'pd.to_numeric()' will convert strings to float or int, as appropriate,
822+
# and will return NaN if conversion fails. It preserves integers.
823+
if pd.notnull(numeric_key): # Check if conversion was successful
824+
N = str(counts.loc[numeric_key])
825+
else:
826+
raise ValueError # Raise an error to trigger the except block
827+
except (ValueError, KeyError):
828+
# Handle cases where 'te' cannot be converted or the converted key doesn't exist
829+
print(f"Key '{te}' not found in counts.")
830+
N = "N/A"
831+
832+
# Append the modified tick label with the count to the list
833+
ticks_with_counts.append(f"{te}\nN = {N}")
817834

818-
ticks_with_counts.append("{}\nN = {}".format(te, N))
819835

820836
if plot_kwargs["fontsize_rawxlabel"] is not None:
821837
fontsize_rawxlabel = plot_kwargs["fontsize_rawxlabel"]

nbs/API/plot_tools.ipynb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,23 @@
228228
"\n",
229229
" kwargs[\"zorder\"] = kwargs[\"zorder\"]\n",
230230
"\n",
231-
" for xpos, central_measure in enumerate(central_measures):\n",
231+
" for xpos, val in enumerate(central_measures.index):\n",
232+
" central_measure = central_measures[val]\n",
232233
" kwargs[\"color\"] = custom_palette[xpos]\n",
233234
"\n",
234235
" if method == \"sankey_error_bar\":\n",
235236
" _xpos = pos[xpos] + offset[xpos]\n",
236237
" else:\n",
237238
" _xpos = xpos + offset[xpos]\n",
238239
"\n",
239-
" low = lows[xpos]\n",
240-
" high = highs[xpos]\n",
240+
" # Fix for the non-string x-axis issue #108\n",
241+
" if central_measures.index.dtype.name == \"category\":\n",
242+
" low = lows[xpos]\n",
243+
" high = highs[xpos]\n",
244+
" else: \n",
245+
" low = lows[val]\n",
246+
" high = highs[val]\n",
247+
"\n",
241248
" if low == high == central_measure:\n",
242249
" low_to_mean = mlines.Line2D(\n",
243250
" [_xpos, _xpos], [low, central_measure], **kwargs\n",

nbs/API/plotter.ipynb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -866,15 +866,31 @@
866866
" rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))\n",
867867
" for xticklab in rawdata_axes.xaxis.get_ticklabels():\n",
868868
" t = xticklab.get_text()\n",
869-
" if t.rfind(\"\\n\") != -1:\n",
870-
" te = t[t.rfind(\"\\n\") + len(\"\\n\") :]\n",
871-
" N = str(counts.loc[te])\n",
872-
" te = t\n",
873-
" else:\n",
874-
" te = t\n",
869+
" # Extract the text after the last newline, if present\n",
870+
" te = t[t.rfind(\"\\n\") + len(\"\\n\"):] if t.rfind(\"\\n\") != -1 else t\n",
871+
"\n",
872+
" try:\n",
873+
" # Try to access 'counts' directly with 'te'.\n",
875874
" N = str(counts.loc[te])\n",
875+
" except KeyError:\n",
876+
" # If direct access fails, attempt a numeric interpretation.\n",
877+
" try:\n",
878+
" # Attempt to convert 'te' to numeric (float or int, as appropriate)\n",
879+
" numeric_key = pd.to_numeric(te, errors='coerce')\n",
880+
" # 'pd.to_numeric()' will convert strings to float or int, as appropriate,\n",
881+
" # and will return NaN if conversion fails. It preserves integers.\n",
882+
" if pd.notnull(numeric_key): # Check if conversion was successful\n",
883+
" N = str(counts.loc[numeric_key])\n",
884+
" else:\n",
885+
" raise ValueError # Raise an error to trigger the except block\n",
886+
" except (ValueError, KeyError):\n",
887+
" # Handle cases where 'te' cannot be converted or the converted key doesn't exist\n",
888+
" print(f\"Key '{te}' not found in counts.\")\n",
889+
" N = \"N/A\"\n",
890+
"\n",
891+
" # Append the modified tick label with the count to the list\n",
892+
" ticks_with_counts.append(f\"{te}\\nN = {N}\")\n",
876893
"\n",
877-
" ticks_with_counts.append(\"{}\\nN = {}\".format(te, N))\n",
878894
"\n",
879895
" if plot_kwargs[\"fontsize_rawxlabel\"] is not None:\n",
880896
" fontsize_rawxlabel = plot_kwargs[\"fontsize_rawxlabel\"]\n",

0 commit comments

Comments
 (0)