Skip to content

Commit

Permalink
Moved gridkey to separate function in plot_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
JAnns98 committed Sep 10, 2024
1 parent c298824 commit a9b6e52
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 307 deletions.
1 change: 1 addition & 0 deletions dabest/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
'dabest/plot_tools.py'),
'dabest.plot_tools.error_bar': ('API/plot_tools.html#error_bar', 'dabest/plot_tools.py'),
'dabest.plot_tools.get_swarm_spans': ('API/plot_tools.html#get_swarm_spans', 'dabest/plot_tools.py'),
'dabest.plot_tools.grid_key_WIP': ('API/plot_tools.html#grid_key_wip', 'dabest/plot_tools.py'),
'dabest.plot_tools.halfviolin': ('API/plot_tools.html#halfviolin', 'dabest/plot_tools.py'),
'dabest.plot_tools.normalize_dict': ('API/plot_tools.html#normalize_dict', 'dabest/plot_tools.py'),
'dabest.plot_tools.plot_minimeta_or_deltadelta_violins': ( 'API/plot_tools.html#plot_minimeta_or_deltadelta_violins',
Expand Down
4 changes: 3 additions & 1 deletion dabest/misc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def get_color_palette(plot_kwargs, plot_data, xvar, show_pairs):
bar_color, plot_palette_bar, plot_palette_contrast, plot_palette_sankey)

def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_paired, show_pairs, proportional,
float_contrast, face_color, h_space_cummings):
float_contrast, face_color):
fig_size = plot_kwargs["fig_size"]
if fig_size is None:
all_groups_count = np.sum([len(i) for i in dabest_obj.idx])
Expand All @@ -403,6 +403,8 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs["dpi"], tight_layout=True)
width_ratios_ga = [2.5, 1]

h_space_cummings = 0.3 if plot_kwargs["gridkey_rows"] == None else 0.1 ##### GRIDKEY WIP addition

if plot_kwargs["ax"] is not None:
# New in v0.2.6.
# Use inset axes to create the estimation plot inside a single axes.
Expand Down
135 changes: 133 additions & 2 deletions dabest/plot_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__all__ = ['halfviolin', 'get_swarm_spans', 'error_bar', 'check_data_matches_labels', 'normalize_dict', 'width_determine',
'single_sankey', 'sankeydiag', 'summary_bars_plotter', 'contrast_bars_plotter', 'swarm_bars_plotter',
'delta_text_plotter', 'DeltaDotsPlotter', 'slopegraph_plotter', 'plot_minimeta_or_deltadelta_violins',
'effect_size_curve_plotter', 'swarmplot', 'SwarmPlot']
'effect_size_curve_plotter', 'grid_key_WIP', 'swarmplot', 'SwarmPlot']

# %% ../nbs/API/plot_tools.ipynb 4
import math
Expand Down Expand Up @@ -1290,7 +1290,138 @@ def effect_size_curve_plotter(ticks_to_plot, results, ci_type, contrast_axes, vi
"{}\nminus\n{}".format(current_group, current_control)
)
return current_group, current_control, current_effsize
...


def grid_key_WIP(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, contrast_axes,
plot_data, xvar, yvar, results, show_delta2, show_mini_meta, float_contrast, plot_kwargs,):

gridkey_show_Ns=plot_kwargs["gridkey_show_Ns"]
gridkey_show_es=plot_kwargs["gridkey_show_es"]
gridkey_merge_pairs=plot_kwargs["gridkey_merge_pairs"]

# Raise error if there are more than 2 items in any idx and gridkey_merge_pairs is True and is_paired is not None
if gridkey_merge_pairs and is_paired is not None:
for i in idx:
if len(i) > 2:
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"
)
gridkey_merge_pairs = False
break
elif gridkey_merge_pairs and is_paired is None:
warnings.warn(
"gridkey_merge_pairs=True is only applicable for paired data."
)
gridkey_merge_pairs = False

# Checks for gridkey_merge_pairs and is_paired; if both are true, "merges" the gridkey per pair
if gridkey_merge_pairs and is_paired is not None:
groups_for_gridkey = []
for i in idx:
groups_for_gridkey.append(i[1])
else:
groups_for_gridkey = all_plot_groups

# raise errors if gridkey_rows is not a list, or if the list is empty
if isinstance(gridkey_rows, list) is False:
raise TypeError("gridkey_rows must be a list.")
elif len(gridkey_rows) == 0:
warnings.warn("gridkey_rows is an empty list.")

# raise Warning if an item in gridkey_rows is not contained in any idx
for i in gridkey_rows:
in_idx = 0
for j in groups_for_gridkey:
if i in j:
in_idx += 1
if in_idx == 0:
if is_paired is not None:
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."
)
else:
warnings.warn(i + " is not in any idx. Please check.")

# Populate table: checks if idx for each column contains rowlabel name
# IF so, marks that element as present w black dot, or space if not present
table_cellcols = []
for i in gridkey_rows:
thisrow = []
for q in groups_for_gridkey:
if str(i) in q:
thisrow.append("\u25CF")
else:
thisrow.append("")
table_cellcols.append(thisrow)

# Adds a row for Ns with the Ns values
if gridkey_show_Ns:
gridkey_rows.append("Ns")
list_of_Ns = []
for i in groups_for_gridkey:
list_of_Ns.append(str(plot_data.groupby(xvar).count()[yvar].loc[i]))
table_cellcols.append(list_of_Ns)

# Adds a row for effectsizes with effectsize values
if gridkey_show_es:
gridkey_rows.append("\u0394")
effsize_list = []
results_list = results.test.to_list()

# get the effect size, append + or -, 2 dec places
for i in enumerate(groups_for_gridkey):
if i[1] in results_list:
curr_esval = results.loc[results["test"] == i[1]][
"difference"
].iloc[0]
curr_esval_str = np.format_float_positional(
curr_esval,
precision=2,
sign=True,
trim="k",
min_digits=2,
)
effsize_list.append(curr_esval_str)
else:
effsize_list.append("-")

table_cellcols.append(effsize_list)

# If Gardner-Altman plot, plot on raw data and not contrast axes
if float_contrast:
axes_ploton = rawdata_axes
else:
axes_ploton = contrast_axes

# Account for extended x axis in case of show_delta2 or show_mini_meta
x_groups_for_width = len(groups_for_gridkey)
if show_delta2 or show_mini_meta:
x_groups_for_width += 2
gridkey_width = len(groups_for_gridkey) / x_groups_for_width

gridkey = axes_ploton.table(
cellText=table_cellcols,
rowLabels=gridkey_rows,
cellLoc="center",
bbox=[
0,
-len(gridkey_rows) * 0.1 - 0.05,
gridkey_width,
len(gridkey_rows) * 0.1,
],
**{"alpha": 0.5}
)

# modifies row label cells
for cell in gridkey._cells:
if cell[1] == -1:
gridkey._cells[cell].visible_edges = "open"
gridkey._cells[cell].set_text_props(**{"ha": "right"})

# turns off both x axes
rawdata_axes.get_xaxis().set_visible(False)
contrast_axes.get_xaxis().set_visible(False)

# %% ../nbs/API/plot_tools.ipynb 6
def swarmplot(
Expand Down
Loading

0 comments on commit a9b6e52

Please sign in to comment.