Skip to content

Commit e5b26a1

Browse files
committed
Fix an error causing xtick label wrong
1 parent d712e8e commit e5b26a1

3 files changed

Lines changed: 126 additions & 38 deletions

File tree

dabest/plotter.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -805,32 +805,32 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
805805
ticks_with_counts = []
806806
ticks_loc = rawdata_axes.get_xticks()
807807
rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))
808-
for xticklab in rawdata_axes.xaxis.get_ticklabels():
809-
t = xticklab.get_text()
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-
808+
def lookup_value(text, counts):
813809
try:
814-
# Try to access 'counts' directly with 'te'.
815-
N = str(counts.loc[te])
810+
return str(counts.loc[text])
816811
except KeyError:
817-
# If direct access fails, attempt a numeric interpretation.
818812
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])
813+
numeric_key = pd.to_numeric(text, errors='coerce')
814+
if pd.notnull(numeric_key):
815+
return str(counts.loc[numeric_key])
825816
else:
826-
raise ValueError # Raise an error to trigger the except block
817+
raise ValueError
827818
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"
819+
print(f"Key '{text}' not found in counts.")
820+
return "N/A"
821+
for xticklab in rawdata_axes.xaxis.get_ticklabels():
822+
t = xticklab.get_text()
823+
# Extract the text after the last newline, if present
824+
if t.rfind("\n") != -1:
825+
te = t[t.rfind("\n") + len("\n"):]
826+
value = lookup_value(te, counts)
827+
te = t
828+
else:
829+
te = t
830+
value = lookup_value(te, counts)
831831

832832
# Append the modified tick label with the count to the list
833-
ticks_with_counts.append(f"{te}\nN = {N}")
833+
ticks_with_counts.append(f"{te}\nN = {value}")
834834

835835

836836
if plot_kwargs["fontsize_rawxlabel"] is not None:

nbs/API/plotter.ipynb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -864,32 +864,32 @@
864864
" ticks_with_counts = []\n",
865865
" ticks_loc = rawdata_axes.get_xticks()\n",
866866
" rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))\n",
867-
" for xticklab in rawdata_axes.xaxis.get_ticklabels():\n",
868-
" t = xticklab.get_text()\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",
867+
" def lookup_value(text, counts):\n",
872868
" try:\n",
873-
" # Try to access 'counts' directly with 'te'.\n",
874-
" N = str(counts.loc[te])\n",
869+
" return str(counts.loc[text])\n",
875870
" except KeyError:\n",
876-
" # If direct access fails, attempt a numeric interpretation.\n",
877871
" 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",
872+
" numeric_key = pd.to_numeric(text, errors='coerce')\n",
873+
" if pd.notnull(numeric_key):\n",
874+
" return str(counts.loc[numeric_key])\n",
884875
" else:\n",
885-
" raise ValueError # Raise an error to trigger the except block\n",
876+
" raise ValueError\n",
886877
" 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",
878+
" print(f\"Key '{text}' not found in counts.\")\n",
879+
" return \"N/A\"\n",
880+
" for xticklab in rawdata_axes.xaxis.get_ticklabels():\n",
881+
" t = xticklab.get_text()\n",
882+
" # Extract the text after the last newline, if present\n",
883+
" if t.rfind(\"\\n\") != -1:\n",
884+
" te = t[t.rfind(\"\\n\") + len(\"\\n\"):]\n",
885+
" value = lookup_value(te, counts)\n",
886+
" te = t\n",
887+
" else:\n",
888+
" te = t\n",
889+
" value = lookup_value(te, counts)\n",
890890
"\n",
891891
" # Append the modified tick label with the count to the list\n",
892-
" ticks_with_counts.append(f\"{te}\\nN = {N}\")\n",
892+
" ticks_with_counts.append(f\"{te}\\nN = {value}\")\n",
893893
"\n",
894894
"\n",
895895
" if plot_kwargs[\"fontsize_rawxlabel\"] is not None:\n",

test.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import numpy as np
2+
from scipy.stats import norm
3+
import pandas as pd
4+
import matplotlib as mpl
5+
import os
6+
from pathlib import Path
7+
8+
import matplotlib.ticker as Ticker
9+
import matplotlib.pyplot as plt
10+
11+
from dabest._api import load
12+
13+
import dabest
14+
15+
columns = [1, 2.0]
16+
columns_str = ["1", "2.0"]
17+
# create a test database
18+
N = 100
19+
df = pd.DataFrame(np.vstack([np.random.normal(loc=i, size=(N,)) for i in range(len(columns))]).T, columns=columns_str)
20+
females = np.repeat("Female", N / 2).tolist()
21+
males = np.repeat("Male", N / 2).tolist()
22+
df['gender'] = females + males
23+
24+
# Add an `id` column for paired data plotting.
25+
df['ID'] = pd.Series(range(1, N + 1))
26+
27+
28+
db = dabest.load(data=df, idx=columns_str, paired="baseline", id_col="ID")
29+
print(db.mean_diff)
30+
db.mean_diff.plot();
31+
32+
# def create_demo_dataset(seed=9999, N=20):
33+
# import numpy as np
34+
# import pandas as pd
35+
# from scipy.stats import norm # Used in generation of populations.
36+
37+
# np.random.seed(9999) # Fix the seed so the results are replicable.
38+
# # pop_size = 10000 # Size of each population.
39+
40+
# # Create samples
41+
# c1 = norm.rvs(loc=3, scale=0.4, size=N)
42+
# c2 = norm.rvs(loc=3.5, scale=0.75, size=N)
43+
# c3 = norm.rvs(loc=3.25, scale=0.4, size=N)
44+
45+
# t1 = norm.rvs(loc=3.5, scale=0.5, size=N)
46+
# t2 = norm.rvs(loc=2.5, scale=0.6, size=N)
47+
# t3 = norm.rvs(loc=3, scale=0.75, size=N)
48+
# t4 = norm.rvs(loc=3.5, scale=0.75, size=N)
49+
# t5 = norm.rvs(loc=3.25, scale=0.4, size=N)
50+
# t6 = norm.rvs(loc=3.25, scale=0.4, size=N)
51+
52+
# # Add a `gender` column for coloring the data.
53+
# females = np.repeat("Female", N / 2).tolist()
54+
# males = np.repeat("Male", N / 2).tolist()
55+
# gender = females + males
56+
57+
# # Add an `id` column for paired data plotting.
58+
# id_col = pd.Series(range(1, N + 1))
59+
60+
# # Combine samples and gender into a DataFrame.
61+
# df = pd.DataFrame(
62+
# {
63+
# "Control 1": c1,
64+
# "Test 1": t1,
65+
# "Control 2": c2,
66+
# "Test 2": t2,
67+
# "Control 3": c3,
68+
# "Test 3": t3,
69+
# "Test 4": t4,
70+
# "Test 5": t5,
71+
# "Test 6": t6,
72+
# "Gender": gender,
73+
# "ID": id_col,
74+
# }
75+
# )
76+
77+
# return df
78+
79+
80+
# df = create_demo_dataset()
81+
82+
# two_groups_unpaired = load(df, idx=("Control 1", "Test 1"))
83+
84+
# two_groups_paired = load(
85+
# df, idx=("Control 1", "Test 1"), paired="baseline", id_col="ID"
86+
# )
87+
88+
# two_groups_unpaired.mean_diff.plot()

0 commit comments

Comments
 (0)