Skip to content

Commit 6c60a58

Browse files
committed
Updated Gridkey function for horizontal plot and delta-delta and mini-meta integration
1 parent adf9151 commit 6c60a58

File tree

7 files changed

+268
-124
lines changed

7 files changed

+268
-124
lines changed

dabest/_modidx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
'dabest/plot_tools.py'),
118118
'dabest.plot_tools.error_bar': ('API/plot_tools.html#error_bar', 'dabest/plot_tools.py'),
119119
'dabest.plot_tools.get_swarm_spans': ('API/plot_tools.html#get_swarm_spans', 'dabest/plot_tools.py'),
120-
'dabest.plot_tools.grid_key_WIP': ('API/plot_tools.html#grid_key_wip', 'dabest/plot_tools.py'),
120+
'dabest.plot_tools.gridkey_plotter': ('API/plot_tools.html#gridkey_plotter', 'dabest/plot_tools.py'),
121121
'dabest.plot_tools.halfviolin': ('API/plot_tools.html#halfviolin', 'dabest/plot_tools.py'),
122122
'dabest.plot_tools.normalize_dict': ('API/plot_tools.html#normalize_dict', 'dabest/plot_tools.py'),
123123
'dabest.plot_tools.plot_minimeta_or_deltadelta_violins': ( 'API/plot_tools.html#plot_minimeta_or_deltadelta_violins',

dabest/misc_tools.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def get_kwargs(plot_kwargs, ytick_color):
390390
else:
391391
table_kwargs = merge_two_dicts(default_table_kwargs, plot_kwargs["horizontal_table_kwargs"])
392392

393+
# Return the kwargs.
393394
return (swarmplot_kwargs, barplot_kwargs, sankey_kwargs, violinplot_kwargs, slopegraph_kwargs,
394395
reflines_kwargs, legend_kwargs, group_summary_kwargs, redraw_axes_kwargs, delta_dot_kwargs,
395396
delta_text_kwargs, summary_bars_kwargs, swarm_bars_kwargs, contrast_bars_kwargs, table_kwargs)
@@ -593,7 +594,8 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
593594
init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs["dpi"], tight_layout=True)
594595

595596
width_ratios_ga = [2.5, 1]
596-
h_space_cummings = 0.3 if plot_kwargs["gridkey_rows"] == None else 0.1 ##### GRIDKEY WIP addition
597+
h_space_cummings = (0.1 if plot_kwargs["gridkey_rows"] is not None
598+
else 0.3)
597599

598600
if plot_kwargs["ax"] is not None:
599601
# New in v0.2.6.
@@ -1430,7 +1432,7 @@ def Cumming_Plot_Aesthetic_Adjustments(contrast_axes, reflines_kwargs, is_paired
14301432
del redraw_axes_kwargs["y"]
14311433

14321434
# Add x-spine line for delta2/mini meta.
1433-
if (show_delta2 or show_mini_meta) and not horizontal:
1435+
if (show_delta2 or show_mini_meta) and not horizontal and not skip_redraw_lines:
14341436
ylim = contrast_axes.get_ylim()
14351437
redraw_axes_kwargs["y"] = ylim[0]
14361438
x_ticks = contrast_axes.get_xticks()

dabest/plot_tools.py

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__all__ = ['halfviolin', 'get_swarm_spans', 'error_bar', 'check_data_matches_labels', 'normalize_dict', 'width_determine',
1010
'single_sankey', 'sankeydiag', 'summary_bars_plotter', 'contrast_bars_plotter', 'swarm_bars_plotter',
1111
'delta_text_plotter', 'DeltaDotsPlotter', 'slopegraph_plotter', 'plot_minimeta_or_deltadelta_violins',
12-
'effect_size_curve_plotter', 'grid_key_WIP', 'barplotter', 'table_for_horizontal_plots', 'swarmplot',
12+
'effect_size_curve_plotter', 'gridkey_plotter', 'barplotter', 'table_for_horizontal_plots', 'swarmplot',
1313
'SwarmPlot']
1414

1515
# %% ../nbs/API/plot_tools.ipynb 4
@@ -1609,9 +1609,10 @@ def effect_size_curve_plotter(ticks_to_plot, results, ci_type, contrast_axes, vi
16091609
return current_group, current_control, current_effsize, contrast_xtick_labels
16101610

16111611

1612-
def grid_key_WIP(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, contrast_axes,
1613-
plot_data, xvar, yvar, results, show_delta2, show_mini_meta, float_contrast,
1614-
plot_kwargs, x1_level, experiment_label):
1612+
def gridkey_plotter(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, contrast_axes,
1613+
plot_data, xvar, yvar, results, show_delta2, show_mini_meta,
1614+
plot_kwargs, x1_level, experiment_label, float_contrast, horizontal,
1615+
delta_delta, mini_meta_delta, effect_size):
16151616

16161617
gridkey_delimiters=plot_kwargs["gridkey_delimiters"] # Auto parser for gridkey - implemented by SangyuXu
16171618
if gridkey_rows == "auto":
@@ -1697,7 +1698,7 @@ def grid_key_WIP(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, co
16971698
table_cellcols.append(list_of_Ns)
16981699

16991700
# Adds a row for effectsizes with effectsize values
1700-
if gridkey_show_es:
1701+
if gridkey_show_es and not horizontal:
17011702
gridkey_rows.append("\u0394")
17021703
effsize_list = []
17031704
results_list = results.test.to_list()
@@ -1721,40 +1722,102 @@ def grid_key_WIP(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, co
17211722

17221723
table_cellcols.append(effsize_list)
17231724

1724-
# If Gardner-Altman plot, plot on raw data and not contrast axes
1725-
if float_contrast:
1726-
axes_ploton = rawdata_axes
1725+
# Set the axes to plot on
1726+
if float_contrast or horizontal:
1727+
ax_to_plot = rawdata_axes
17271728
else:
1728-
axes_ploton = contrast_axes
1729-
1730-
# Account for extended x axis in case of show_delta2 or show_mini_meta
1731-
x_groups_for_width = len(groups_for_gridkey)
1732-
if show_delta2 or show_mini_meta:
1733-
x_groups_for_width += 2
1734-
gridkey_width = len(groups_for_gridkey) / x_groups_for_width
1735-
1736-
gridkey = axes_ploton.table(
1737-
cellText=table_cellcols,
1738-
rowLabels=gridkey_rows,
1739-
cellLoc="center",
1740-
bbox=[
1741-
0,
1742-
-len(gridkey_rows) * 0.1 - 0.05,
1743-
gridkey_width,
1744-
len(gridkey_rows) * 0.1,
1745-
],
1746-
**{"alpha": 0.5}
1747-
)
1729+
ax_to_plot = contrast_axes
1730+
1731+
# Add delta-delta or mini_meta details to the table
1732+
if show_mini_meta or show_delta2:
1733+
if show_delta2:
1734+
added_group_name = ["deltas' g"] if effect_size == "delta_g" else ["delta-delta"]
1735+
else:
1736+
added_group_name = ["Weighted delta"]
1737+
gridkey_rows = added_group_name + gridkey_rows
1738+
table_cellcols = [[""]*len(table_cellcols[0])] + table_cellcols
1739+
1740+
for group_idx, group_vals in enumerate(table_cellcols):
1741+
if group_idx == 0:
1742+
added_group = ['', "\u25CF"]
1743+
elif gridkey_show_es and (group_idx == len(table_cellcols)-1) and not horizontal:
1744+
added_delta_effectsize = delta_delta.difference if show_delta2 else mini_meta_delta.difference
1745+
added_delta_effectsize_str = np.format_float_positional(
1746+
added_delta_effectsize,
1747+
precision=2,
1748+
sign=True,
1749+
trim="k",
1750+
min_digits=2,
1751+
)
1752+
added_group = ['-', added_delta_effectsize_str]
1753+
else:
1754+
added_group = ['', '']
1755+
for n in added_group:
1756+
group_vals.append(n)
17481757

1749-
# modifies row label cells
1750-
for cell in gridkey._cells:
1751-
if cell[1] == -1:
1752-
gridkey._cells[cell].visible_edges = "open"
1753-
gridkey._cells[cell].set_text_props(**{"ha": "right"})
1758+
# Create the table object
1759+
if horizontal:
1760+
# Convert the cells format for horizontal table plotting
1761+
converted_list = []
1762+
for j in range(0, len(table_cellcols[0])):
1763+
temp_list = []
1764+
for i in table_cellcols:
1765+
temp_list.append(i[j])
1766+
converted_list.append(temp_list)
1767+
1768+
# Plot the table for horizontal format
1769+
gridkey = ax_to_plot.table(
1770+
cellText=converted_list,
1771+
cellLoc="center",
1772+
bbox=[
1773+
-len(gridkey_rows) * 0.2,
1774+
0,
1775+
len(gridkey_rows) * 0.2,
1776+
1
1777+
],
1778+
**{"alpha": 0.5, "zorder": 5}
1779+
)
1780+
1781+
# Add the column labels as text below the table
1782+
text_locs = np.arange((-len(gridkey_rows)*0.2) +0.1, 0, 0.2)
1783+
for loc, txt in zip(text_locs, gridkey_rows):
1784+
ax_to_plot.text(
1785+
loc+0.04,
1786+
-0.01,
1787+
txt,
1788+
transform=ax_to_plot.transAxes,
1789+
fontsize=10,
1790+
ha='right',
1791+
rotation=45,
1792+
va='top'
1793+
)
1794+
else:
1795+
# Plot the table for vertical format
1796+
gridkey = ax_to_plot.table(
1797+
cellText=table_cellcols,
1798+
rowLabels=gridkey_rows,
1799+
cellLoc="center",
1800+
bbox=[
1801+
0,
1802+
-len(gridkey_rows) * 0.1 - 0.05,
1803+
1,
1804+
len(gridkey_rows) * 0.1,
1805+
],
1806+
**{"alpha": 0.5}
1807+
)
1808+
# modifies row label cells
1809+
for cell in gridkey._cells:
1810+
if cell[1] == -1:
1811+
gridkey._cells[cell].visible_edges = "open"
1812+
gridkey._cells[cell].set_text_props(**{"ha": "right"})
17541813

17551814
# turns off both x axes
1756-
rawdata_axes.get_xaxis().set_visible(False)
1757-
contrast_axes.get_xaxis().set_visible(False)
1815+
if horizontal:
1816+
rawdata_axes.get_yaxis().set_visible(False)
1817+
contrast_axes.get_yaxis().set_visible(False)
1818+
else:
1819+
rawdata_axes.get_xaxis().set_visible(False)
1820+
contrast_axes.get_xaxis().set_visible(False)
17581821

17591822
def barplotter(xvar, yvar, all_plot_groups, rawdata_axes, plot_data, bar_color, plot_palette_bar,
17601823
plot_kwargs, barplot_kwargs, horizontal):

dabest/plotter.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
9090
slopegraph_plotter,
9191
plot_minimeta_or_deltadelta_violins,
9292
effect_size_curve_plotter,
93-
grid_key_WIP,
93+
gridkey_plotter,
9494
barplotter,
9595
table_for_horizontal_plots,
9696
)
@@ -378,30 +378,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
378378
redraw_axes_kwargs=redraw_axes_kwargs,
379379
horizontal=horizontal,
380380
)
381-
382-
# GRIDKEY WIP
383-
skip_redraw_lines = False
384-
gridkey_rows = plot_kwargs["gridkey_rows"]
385-
if gridkey_rows is not None and not horizontal:
386-
grid_key_WIP(
387-
is_paired=is_paired,
388-
idx=idx,
389-
all_plot_groups=all_plot_groups,
390-
gridkey_rows=gridkey_rows,
391-
rawdata_axes=rawdata_axes,
392-
contrast_axes=contrast_axes,
393-
plot_data=plot_data,
394-
xvar=xvar,
395-
yvar=yvar,
396-
results=results,
397-
show_delta2=show_delta2,
398-
show_mini_meta=show_mini_meta,
399-
float_contrast=float_contrast,
400-
plot_kwargs=plot_kwargs,
401-
x1_level=x1_level,
402-
experiment_label=experiment_label,
403-
)
404-
skip_redraw_lines = True
405381

406382
# Make sure the contrast_axes x-lims match the rawdata_axes xlims,
407383
# and add an extra violinplot tick for delta-delta plot.
@@ -457,6 +433,8 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
457433
)
458434
else:
459435
# For Cumming Plots only.
436+
# skip_redraw_lines = True if plot_kwargs["gridkey_rows"] is not None and not any([show_mini_meta, show_delta2]) else False
437+
skip_redraw_lines = True if plot_kwargs["gridkey_rows"] is not None else False
460438
Cumming_Plot_Aesthetic_Adjustments(
461439
contrast_axes=contrast_axes,
462440
reflines_kwargs=reflines_kwargs,
@@ -569,6 +547,35 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
569547
show_delta2=show_delta2,
570548
table_kwargs=table_kwargs,
571549
)
550+
551+
552+
# GRIDKEY WIP
553+
gridkey_rows = plot_kwargs["gridkey_rows"]
554+
# if gridkey_rows is not None and not any([show_mini_meta, show_delta2]):
555+
if gridkey_rows is not None:
556+
gridkey_plotter(
557+
is_paired=is_paired,
558+
idx=idx,
559+
all_plot_groups=all_plot_groups,
560+
gridkey_rows=gridkey_rows,
561+
rawdata_axes = rawdata_axes,
562+
contrast_axes = contrast_axes,
563+
plot_data=plot_data,
564+
xvar=xvar,
565+
yvar=yvar,
566+
results=results,
567+
show_delta2=show_delta2,
568+
show_mini_meta=show_mini_meta,
569+
plot_kwargs=plot_kwargs,
570+
x1_level=x1_level,
571+
experiment_label=experiment_label,
572+
float_contrast=float_contrast,
573+
horizontal=horizontal,
574+
delta_delta = effectsize_df.delta_delta if show_delta2 else None,
575+
mini_meta_delta = effectsize_df.mini_meta_delta if show_mini_meta else None,
576+
effect_size = effect_size,
577+
)
578+
572579

573580
# Make sure no stray ticks appear!
574581
rawdata_axes.xaxis.set_ticks_position("bottom")

nbs/API/misc_tools.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@
443443
" else:\n",
444444
" table_kwargs = merge_two_dicts(default_table_kwargs, plot_kwargs[\"horizontal_table_kwargs\"])\n",
445445
"\n",
446+
" # Return the kwargs.\n",
446447
" return (swarmplot_kwargs, barplot_kwargs, sankey_kwargs, violinplot_kwargs, slopegraph_kwargs, \n",
447448
" reflines_kwargs, legend_kwargs, group_summary_kwargs, redraw_axes_kwargs, delta_dot_kwargs,\n",
448449
" delta_text_kwargs, summary_bars_kwargs, swarm_bars_kwargs, contrast_bars_kwargs, table_kwargs)\n",
@@ -646,7 +647,8 @@
646647
" init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs[\"dpi\"], tight_layout=True)\n",
647648
"\n",
648649
" width_ratios_ga = [2.5, 1]\n",
649-
" h_space_cummings = 0.3 if plot_kwargs[\"gridkey_rows\"] == None else 0.1 ##### GRIDKEY WIP addition\n",
650+
" h_space_cummings = (0.1 if plot_kwargs[\"gridkey_rows\"] is not None\n",
651+
" else 0.3)\n",
650652
"\n",
651653
" if plot_kwargs[\"ax\"] is not None:\n",
652654
" # New in v0.2.6.\n",
@@ -1483,7 +1485,7 @@
14831485
" del redraw_axes_kwargs[\"y\"]\n",
14841486
"\n",
14851487
" # Add x-spine line for delta2/mini meta.\n",
1486-
" if (show_delta2 or show_mini_meta) and not horizontal:\n",
1488+
" if (show_delta2 or show_mini_meta) and not horizontal and not skip_redraw_lines:\n",
14871489
" ylim = contrast_axes.get_ylim()\n",
14881490
" redraw_axes_kwargs[\"y\"] = ylim[0]\n",
14891491
" x_ticks = contrast_axes.get_xticks()\n",

0 commit comments

Comments
 (0)