Skip to content

Commit 065ccd6

Browse files
committed
Adding gridkey implementation
1 parent 5305ec2 commit 065ccd6

4 files changed

Lines changed: 304 additions & 12 deletions

File tree

dabest/_classes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,11 @@ def plot(self, color_col=None,
24452445
fig_size=None,
24462446
dpi=100,
24472447
ax=None,
2448+
2449+
gridkey_rows=None,
2450+
gridkey_merge_pairs = False,
2451+
gridkey_show_Ns = True,
2452+
gridkey_show_es = True,
24482453

24492454
swarmplot_kwargs=None,
24502455
barplot_kwargs=None,
@@ -2536,6 +2541,10 @@ def plot(self, color_col=None,
25362541
ax : matplotlib.Axes, default None
25372542
Provide an existing Axes for the plots to be created. If no Axes is
25382543
specified, a new matplotlib Figure will be created.
2544+
gridkey_rows : list, default None
2545+
Provide a list of row labels for the gridkey. The supplied idx is
2546+
checked against the row labels to determine whether the corresponding
2547+
cell should be populated or not.
25392548
swarmplot_kwargs : dict, default None
25402549
Pass any keyword arguments accepted by the seaborn `swarmplot`
25412550
command here, as a dict. If None, the following keywords are

dabest/plotter.py

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
3030
fig_size=None,
3131
dpi=100,
3232
ax=None,
33+
gridkey_rows=None,
3334
swarmplot_kwargs=None,
3435
violinplot_kwargs=None,
3536
slopegraph_kwargs=None,
@@ -65,6 +66,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
6566

6667
ytick_color = plt.rcParams["ytick.color"]
6768
face_color = plot_kwargs["face_color"]
69+
6870
if plot_kwargs["face_color"] is None:
6971
face_color = "white"
7072

@@ -175,6 +177,16 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
175177
else:
176178
legend_kwargs = merge_two_dicts(default_legend_kwargs,
177179
plot_kwargs["legend_kwargs"])
180+
181+
182+
################################################### GRIDKEY WIP - extracting kwargs
183+
184+
gridkey_rows = plot_kwargs["gridkey_rows"]
185+
gridkey_merge_pairs = plot_kwargs["gridkey_merge_pairs"]
186+
gridkey_show_Ns = plot_kwargs["gridkey_show_Ns"]
187+
gridkey_show_es = plot_kwargs["gridkey_show_es"]
188+
189+
################################################### END GRIDKEY WIP - extracting kwargs
178190

179191
# Group summaries kwargs.
180192
gs_default = {'mean_sd', 'median_quartiles', None}
@@ -298,7 +310,17 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
298310
,tight_layout=True)
299311

300312
width_ratios_ga = [2.5, 1]
301-
h_space_cummings = 0.3
313+
314+
###################### GRIDKEY HSPACE ALTERATION
315+
316+
# Sets hspace for cummings plots if gridkey is shown.
317+
if gridkey_rows is not None:
318+
h_space_cummings = 0.1
319+
else:
320+
h_space_cummings = 0.3
321+
322+
###################### END GRIDKEY HSPACE ALTERATION
323+
302324
if plot_kwargs["ax"] is not None:
303325
# New in v0.2.6.
304326
# Use inset axes to create the estimation plot inside a single axes.
@@ -355,7 +377,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
355377

356378
else:
357379
fig, axx = plt.subplots(nrows=2,
358-
gridspec_kw={"hspace": 0.3},
380+
gridspec_kw={"hspace": h_space_cummings},
359381
**init_fig_kwargs)
360382
fig.patch.set_facecolor(face_color)
361383
# If the contrast axes are NOT floating, create lists to store
@@ -643,6 +665,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
643665
contrast_axes.plot([tick], current_effsize, marker='o',
644666
color=ytick_color,
645667
markersize=es_marker_size)
668+
646669
# Plot the confidence interval.
647670
contrast_axes.plot([tick, tick],
648671
[current_ci_low, current_ci_high],
@@ -1113,6 +1136,124 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
11131136
og_ylim_delta[0], og_ylim_delta[1],
11141137
**redraw_axes_kwargs)
11151138

1139+
1140+
################################################### GRIDKEY MAIN CODE WIP
1141+
1142+
#if gridkey_rows is None, skip everything here
1143+
if gridkey_rows is not None:
1144+
1145+
# Raise error if there are more than 2 items in any idx and gridkey_merge_pairs is True and is_paired is not None
1146+
if gridkey_merge_pairs is True and is_paired is not None:
1147+
for i in idx:
1148+
if len(i) > 2:
1149+
warnings.warn("gridkey_merge_pairs=True only works if all idx in tuples have only two items. gridkey_merge_pairs has automatically been set to False")
1150+
gridkey_merge_pairs = False
1151+
break
1152+
elif gridkey_merge_pairs is True and is_paired is None:
1153+
warnings.warn("gridkey_merge_pairs=True is only applicable for paired data.")
1154+
gridkey_merge_pairs = False
1155+
1156+
# Checks for gridkey_merge_pairs and is_paired; if both are true, "merges" the gridkey per pair
1157+
if gridkey_merge_pairs is True and is_paired is not None:
1158+
groups_for_gridkey = []
1159+
for i in idx:
1160+
groups_for_gridkey.append(i[1])
1161+
else:
1162+
groups_for_gridkey = all_plot_groups
1163+
1164+
1165+
# raise errors if gridkey_rows is not a list, or if the list is empty
1166+
if isinstance(gridkey_rows, list) is False:
1167+
raise TypeError("gridkey_rows must be a list.")
1168+
elif len(gridkey_rows) == 0:
1169+
raise ValueError("gridkey_rows cannot be an empty list.")
1170+
1171+
1172+
# raise Warning if an item in gridkey_rows is not contained in any idx
1173+
for i in gridkey_rows:
1174+
in_idx = 0
1175+
for j in groups_for_gridkey:
1176+
if i in j:
1177+
in_idx += 1
1178+
if in_idx == 0:
1179+
if is_paired is not None:
1180+
warnings.warn(i + " is not in any idx. Please check. Alternatively, merging gridkey pairs may not be suitable for your data; try passing gridkey_merge_pairs=False.")
1181+
else:
1182+
warnings.warn(i + " is not in any idx. Please check.")
1183+
1184+
1185+
# Populate table: checks if idx for each column contains rowlabel name
1186+
# IF so, marks that element as present w black dot, or space if not present
1187+
table_cellcols = []
1188+
for i in gridkey_rows:
1189+
thisrow = []
1190+
for q in groups_for_gridkey:
1191+
if str(i) in q:
1192+
thisrow.append(u"\u25CF")
1193+
else:
1194+
thisrow.append("")
1195+
table_cellcols.append(thisrow)
1196+
1197+
1198+
# Adds a row for Ns with the Ns values
1199+
if gridkey_show_Ns == True:
1200+
gridkey_rows.append("Ns")
1201+
list_of_Ns = []
1202+
for i in groups_for_gridkey:
1203+
list_of_Ns.append(str(counts.loc[i]))
1204+
table_cellcols.append(list_of_Ns)
1205+
1206+
1207+
# Adds a row for effectsizes with effectsize values
1208+
if gridkey_show_es == True:
1209+
gridkey_rows.append(u"\u0394")
1210+
effsize_list = []
1211+
results_list = results.test.to_list()
1212+
1213+
# get the effect size, append + or -, 2 dec places
1214+
for i in enumerate(groups_for_gridkey):
1215+
if i[1] in results_list:
1216+
curr_esval = results.loc[results["test"] == i[1]]["difference"].iloc[0]
1217+
if curr_esval >= 0:
1218+
curr_esval_str = "+" + str("%.2f" % curr_esval)
1219+
else:
1220+
curr_esval_str = str("%.2f" % curr_esval)
1221+
effsize_list.append(curr_esval_str)
1222+
else:
1223+
effsize_list.append("-")
1224+
1225+
table_cellcols.append(effsize_list)
1226+
1227+
# If Gardner-Altman plot, plot on raw data and not contrast axes
1228+
if float_contrast == True:
1229+
axes_ploton = rawdata_axes
1230+
else:
1231+
axes_ploton = contrast_axes
1232+
1233+
# Account for extended x axis in case of show_delta2 or show_mini_meta
1234+
x_groups_for_width = len(groups_for_gridkey)
1235+
if show_delta2 is True or show_mini_meta is True:
1236+
x_groups_for_width += 2
1237+
gridkey_width = len(groups_for_gridkey) / x_groups_for_width
1238+
1239+
gridkey = axes_ploton.table(cellText = table_cellcols,
1240+
rowLabels = gridkey_rows,
1241+
cellLoc = "center",
1242+
bbox = [0, -len(gridkey_rows)*0.1-0.05, gridkey_width, len(gridkey_rows)*0.1],
1243+
**{"alpha" : 0.5})
1244+
1245+
# modifies row label cells
1246+
for cell in gridkey._cells:
1247+
if cell[1] == -1:
1248+
gridkey._cells[cell].visible_edges = "open"
1249+
gridkey._cells[cell].set_text_props(**{ "ha" : "right" })
1250+
1251+
# turns off both x axes
1252+
rawdata_axes.get_xaxis().set_visible(False)
1253+
contrast_axes.get_xaxis().set_visible(False)
1254+
1255+
####################################################### END GRIDKEY MAIN CODE WIP
1256+
11161257
# Make sure no stray ticks appear!
11171258
rawdata_axes.xaxis.set_ticks_position('bottom')
11181259
rawdata_axes.yaxis.set_ticks_position('left')

nbs/API/class.ipynb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,6 +3257,11 @@
32573257
" fig_size=None,\n",
32583258
" dpi=100,\n",
32593259
" ax=None,\n",
3260+
" \n",
3261+
" gridkey_rows=None,\n",
3262+
" gridkey_merge_pairs = False,\n",
3263+
" gridkey_show_Ns = True,\n",
3264+
" gridkey_show_es = True,\n",
32603265
"\n",
32613266
" swarmplot_kwargs=None,\n",
32623267
" barplot_kwargs=None,\n",
@@ -3348,6 +3353,10 @@
33483353
" ax : matplotlib.Axes, default None\n",
33493354
" Provide an existing Axes for the plots to be created. If no Axes is\n",
33503355
" specified, a new matplotlib Figure will be created.\n",
3356+
" gridkey_rows : list, default None\n",
3357+
" Provide a list of row labels for the gridkey. The supplied idx is\n",
3358+
" checked against the row labels to determine whether the corresponding\n",
3359+
" cell should be populated or not.\n",
33513360
" swarmplot_kwargs : dict, default None\n",
33523361
" Pass any keyword arguments accepted by the seaborn `swarmplot`\n",
33533362
" command here, as a dict. If None, the following keywords are\n",

0 commit comments

Comments
 (0)