diff --git a/dabest/_modidx.py b/dabest/_modidx.py index 5f64ca30..c44b6352 100644 --- a/dabest/_modidx.py +++ b/dabest/_modidx.py @@ -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', diff --git a/dabest/misc_tools.py b/dabest/misc_tools.py index a45cce1b..4f73a710 100644 --- a/dabest/misc_tools.py +++ b/dabest/misc_tools.py @@ -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]) @@ -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. diff --git a/dabest/plot_tools.py b/dabest/plot_tools.py index 5567b721..3182e8aa 100644 --- a/dabest/plot_tools.py +++ b/dabest/plot_tools.py @@ -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 @@ -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( diff --git a/dabest/plotter.py b/dabest/plotter.py index b2dcf002..4067742a 100644 --- a/dabest/plotter.py +++ b/dabest/plotter.py @@ -82,6 +82,7 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs): slopegraph_plotter, plot_minimeta_or_deltadelta_violins, effect_size_curve_plotter, + grid_key_WIP, ) from ._stats_tools.effsize import ( _compute_standardizers, @@ -126,22 +127,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs): True if proportional and not one_sankey and sankey and not flow else False ) - ################################################### GRIDKEY WIP - extracting arguments - - gridkey_rows = plot_kwargs["gridkey_rows"] - gridkey_merge_pairs = plot_kwargs["gridkey_merge_pairs"] - gridkey_show_Ns = plot_kwargs["gridkey_show_Ns"] - gridkey_show_es = plot_kwargs["gridkey_show_es"] - - if gridkey_rows is None: - gridkey_show_Ns = False - gridkey_show_es = False - h_space_cummings = 0.3 - else: - h_space_cummings = 0.1 - - ################################################### END GRIDKEY WIP - extracting arguments - # Extract Color palette (color_col, bootstraps_color_by_group, n_groups, swarm_colors, plot_palette_raw, bar_color, plot_palette_bar, plot_palette_contrast, plot_palette_sankey) = get_color_palette(plot_kwargs=plot_kwargs, plot_data=plot_data, @@ -151,7 +136,7 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs): fig, rawdata_axes, contrast_axes, swarm_ylim = initialize_fig(plot_kwargs=plot_kwargs, dabest_obj=dabest_obj, show_delta2=show_delta2, show_mini_meta=show_mini_meta, is_paired=is_paired, show_pairs=show_pairs, proportional=proportional, float_contrast=float_contrast, face_color=face_color, - h_space_cummings=h_space_cummings) + ) # Plotting the rawdata. if show_pairs: @@ -344,7 +329,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs): results = effectsize_df.results contrast_xtick_labels = [] - ##### WIP (current_group, current_control, current_effsize) = effect_size_curve_plotter(ticks_to_plot=ticks_to_plot, results=results, @@ -360,16 +344,21 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs): plot_palette_contrast=plot_palette_contrast, ) - ##### WIP - # Plot mini-meta violin if show_mini_meta or show_delta2: - contrast_xtick_labels = plot_minimeta_or_deltadelta_violins(show_mini_meta=show_mini_meta, effectsize_df=effectsize_df, - ci_type=ci_type, rawdata_axes=rawdata_axes,contrast_axes=contrast_axes, - violinplot_kwargs=violinplot_kwargs, halfviolin_alpha=halfviolin_alpha, - ytick_color=ytick_color, es_marker_size=es_marker_size, group_summary_kwargs=group_summary_kwargs, - contrast_xtick_labels=contrast_xtick_labels, effect_size=effect_size - ) + contrast_xtick_labels = plot_minimeta_or_deltadelta_violins(show_mini_meta=show_mini_meta, + effectsize_df=effectsize_df, + ci_type=ci_type, + rawdata_axes=rawdata_axes, + contrast_axes=contrast_axes, + violinplot_kwargs=violinplot_kwargs, + halfviolin_alpha=halfviolin_alpha, + ytick_color=ytick_color, + es_marker_size=es_marker_size, + group_summary_kwargs=group_summary_kwargs, + contrast_xtick_labels=contrast_xtick_labels, + effect_size=effect_size + ) # Make sure the contrast_axes x-lims match the rawdata_axes xlims, @@ -837,134 +826,26 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs): og_xlim_delta[1], og_ylim_delta[0], og_ylim_delta[1], **redraw_axes_kwargs ) - ################################################### GRIDKEY MAIN CODE WIP + ################################################### GRIDKEY WIP # if gridkey_rows is None, skip everything here + gridkey_rows = plot_kwargs["gridkey_rows"] if gridkey_rows is not None: - # 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) + grid_key_WIP(is_paired=is_paired, + idx=idx, + all_plot_groups=all_plot_groups, + gridkey_rows=gridkey_rows, + rawdata_axes=rawdata_axes, + contrast_axes=contrast_axes, + plot_data=plot_data, + xvar=xvar, + yvar=yvar, + results=results, + show_delta2=show_delta2, + show_mini_meta=show_mini_meta, + float_contrast=float_contrast, + plot_kwargs=plot_kwargs, + ) - # 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) - ####################################################### END GRIDKEY MAIN CODE WIP - ################################################### Swarm & Contrast & Summary Bars & Delta text WIP # Swarm bars WIP swarm_bars = plot_kwargs["swarm_bars"] diff --git a/nbs/API/misc_tools.ipynb b/nbs/API/misc_tools.ipynb index 5b39550b..84edbbe7 100644 --- a/nbs/API/misc_tools.ipynb +++ b/nbs/API/misc_tools.ipynb @@ -436,7 +436,7 @@ " bar_color, plot_palette_bar, plot_palette_contrast, plot_palette_sankey)\n", "\n", "def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_paired, show_pairs, proportional,\n", - " float_contrast, face_color, h_space_cummings):\n", + " float_contrast, face_color):\n", " fig_size = plot_kwargs[\"fig_size\"]\n", " if fig_size is None:\n", " all_groups_count = np.sum([len(i) for i in dabest_obj.idx])\n", @@ -460,6 +460,8 @@ " init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs[\"dpi\"], tight_layout=True)\n", " width_ratios_ga = [2.5, 1]\n", "\n", + " h_space_cummings = 0.3 if plot_kwargs[\"gridkey_rows\"] == None else 0.1 ##### GRIDKEY WIP addition\n", + "\n", " if plot_kwargs[\"ax\"] is not None:\n", " # New in v0.2.6.\n", " # Use inset axes to create the estimation plot inside a single axes.\n", diff --git a/nbs/API/plot_tools.ipynb b/nbs/API/plot_tools.ipynb index 39759c9c..baea359c 100644 --- a/nbs/API/plot_tools.ipynb +++ b/nbs/API/plot_tools.ipynb @@ -1343,7 +1343,138 @@ " \"{}\\nminus\\n{}\".format(current_group, current_control)\n", " )\n", " return current_group, current_control, current_effsize\n", - " ..." + "\n", + "\n", + "def grid_key_WIP(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, contrast_axes,\n", + " plot_data, xvar, yvar, results, show_delta2, show_mini_meta, float_contrast, plot_kwargs,):\n", + " \n", + " gridkey_show_Ns=plot_kwargs[\"gridkey_show_Ns\"]\n", + " gridkey_show_es=plot_kwargs[\"gridkey_show_es\"]\n", + " gridkey_merge_pairs=plot_kwargs[\"gridkey_merge_pairs\"]\n", + " \n", + " # Raise error if there are more than 2 items in any idx and gridkey_merge_pairs is True and is_paired is not None\n", + " if gridkey_merge_pairs and is_paired is not None:\n", + " for i in idx:\n", + " if len(i) > 2:\n", + " warnings.warn(\n", + " \"gridkey_merge_pairs=True only works if all idx in tuples have only two items. gridkey_merge_pairs has automatically been set to False\"\n", + " )\n", + " gridkey_merge_pairs = False\n", + " break\n", + " elif gridkey_merge_pairs and is_paired is None:\n", + " warnings.warn(\n", + " \"gridkey_merge_pairs=True is only applicable for paired data.\"\n", + " )\n", + " gridkey_merge_pairs = False\n", + "\n", + " # Checks for gridkey_merge_pairs and is_paired; if both are true, \"merges\" the gridkey per pair\n", + " if gridkey_merge_pairs and is_paired is not None:\n", + " groups_for_gridkey = []\n", + " for i in idx:\n", + " groups_for_gridkey.append(i[1])\n", + " else:\n", + " groups_for_gridkey = all_plot_groups\n", + "\n", + " # raise errors if gridkey_rows is not a list, or if the list is empty\n", + " if isinstance(gridkey_rows, list) is False:\n", + " raise TypeError(\"gridkey_rows must be a list.\")\n", + " elif len(gridkey_rows) == 0:\n", + " warnings.warn(\"gridkey_rows is an empty list.\")\n", + "\n", + " # raise Warning if an item in gridkey_rows is not contained in any idx\n", + " for i in gridkey_rows:\n", + " in_idx = 0\n", + " for j in groups_for_gridkey:\n", + " if i in j:\n", + " in_idx += 1\n", + " if in_idx == 0:\n", + " if is_paired is not None:\n", + " warnings.warn(\n", + " i\n", + " + \" is not in any idx. Please check. Alternatively, merging gridkey pairs may not be suitable for your data; try passing gridkey_merge_pairs=False.\"\n", + " )\n", + " else:\n", + " warnings.warn(i + \" is not in any idx. Please check.\")\n", + "\n", + " # Populate table: checks if idx for each column contains rowlabel name\n", + " # IF so, marks that element as present w black dot, or space if not present\n", + " table_cellcols = []\n", + " for i in gridkey_rows:\n", + " thisrow = []\n", + " for q in groups_for_gridkey:\n", + " if str(i) in q:\n", + " thisrow.append(\"\\u25CF\")\n", + " else:\n", + " thisrow.append(\"\")\n", + " table_cellcols.append(thisrow)\n", + "\n", + " # Adds a row for Ns with the Ns values\n", + " if gridkey_show_Ns:\n", + " gridkey_rows.append(\"Ns\")\n", + " list_of_Ns = []\n", + " for i in groups_for_gridkey:\n", + " list_of_Ns.append(str(plot_data.groupby(xvar).count()[yvar].loc[i]))\n", + " table_cellcols.append(list_of_Ns)\n", + "\n", + " # Adds a row for effectsizes with effectsize values\n", + " if gridkey_show_es:\n", + " gridkey_rows.append(\"\\u0394\")\n", + " effsize_list = []\n", + " results_list = results.test.to_list()\n", + "\n", + " # get the effect size, append + or -, 2 dec places\n", + " for i in enumerate(groups_for_gridkey):\n", + " if i[1] in results_list:\n", + " curr_esval = results.loc[results[\"test\"] == i[1]][\n", + " \"difference\"\n", + " ].iloc[0]\n", + " curr_esval_str = np.format_float_positional(\n", + " curr_esval,\n", + " precision=2,\n", + " sign=True,\n", + " trim=\"k\",\n", + " min_digits=2,\n", + " )\n", + " effsize_list.append(curr_esval_str)\n", + " else:\n", + " effsize_list.append(\"-\")\n", + "\n", + " table_cellcols.append(effsize_list)\n", + "\n", + " # If Gardner-Altman plot, plot on raw data and not contrast axes\n", + " if float_contrast:\n", + " axes_ploton = rawdata_axes\n", + " else:\n", + " axes_ploton = contrast_axes\n", + "\n", + " # Account for extended x axis in case of show_delta2 or show_mini_meta\n", + " x_groups_for_width = len(groups_for_gridkey)\n", + " if show_delta2 or show_mini_meta:\n", + " x_groups_for_width += 2\n", + " gridkey_width = len(groups_for_gridkey) / x_groups_for_width\n", + "\n", + " gridkey = axes_ploton.table(\n", + " cellText=table_cellcols,\n", + " rowLabels=gridkey_rows,\n", + " cellLoc=\"center\",\n", + " bbox=[\n", + " 0,\n", + " -len(gridkey_rows) * 0.1 - 0.05,\n", + " gridkey_width,\n", + " len(gridkey_rows) * 0.1,\n", + " ],\n", + " **{\"alpha\": 0.5}\n", + " )\n", + "\n", + " # modifies row label cells\n", + " for cell in gridkey._cells:\n", + " if cell[1] == -1:\n", + " gridkey._cells[cell].visible_edges = \"open\"\n", + " gridkey._cells[cell].set_text_props(**{\"ha\": \"right\"})\n", + "\n", + " # turns off both x axes\n", + " rawdata_axes.get_xaxis().set_visible(False)\n", + " contrast_axes.get_xaxis().set_visible(False)" ] }, { diff --git a/nbs/API/plotter.ipynb b/nbs/API/plotter.ipynb index 5c48d14d..78a48a0a 100644 --- a/nbs/API/plotter.ipynb +++ b/nbs/API/plotter.ipynb @@ -141,6 +141,7 @@ " slopegraph_plotter,\n", " plot_minimeta_or_deltadelta_violins,\n", " effect_size_curve_plotter,\n", + " grid_key_WIP,\n", " )\n", " from ._stats_tools.effsize import (\n", " _compute_standardizers,\n", @@ -185,22 +186,6 @@ " True if proportional and not one_sankey and sankey and not flow else False\n", " )\n", "\n", - " ################################################### GRIDKEY WIP - extracting arguments\n", - "\n", - " gridkey_rows = plot_kwargs[\"gridkey_rows\"]\n", - " gridkey_merge_pairs = plot_kwargs[\"gridkey_merge_pairs\"]\n", - " gridkey_show_Ns = plot_kwargs[\"gridkey_show_Ns\"]\n", - " gridkey_show_es = plot_kwargs[\"gridkey_show_es\"]\n", - "\n", - " if gridkey_rows is None:\n", - " gridkey_show_Ns = False\n", - " gridkey_show_es = False\n", - " h_space_cummings = 0.3\n", - " else:\n", - " h_space_cummings = 0.1\n", - "\n", - " ################################################### END GRIDKEY WIP - extracting arguments\n", - "\n", " # Extract Color palette\n", " (color_col, bootstraps_color_by_group, n_groups, swarm_colors, plot_palette_raw, bar_color, plot_palette_bar, \n", " plot_palette_contrast, plot_palette_sankey) = get_color_palette(plot_kwargs=plot_kwargs, plot_data=plot_data, \n", @@ -210,7 +195,7 @@ " fig, rawdata_axes, contrast_axes, swarm_ylim = initialize_fig(plot_kwargs=plot_kwargs, dabest_obj=dabest_obj, show_delta2=show_delta2, \n", " show_mini_meta=show_mini_meta, is_paired=is_paired, show_pairs=show_pairs, \n", " proportional=proportional, float_contrast=float_contrast, face_color=face_color, \n", - " h_space_cummings=h_space_cummings)\n", + " )\n", " \n", " # Plotting the rawdata.\n", " if show_pairs:\n", @@ -403,7 +388,6 @@ " results = effectsize_df.results\n", " contrast_xtick_labels = []\n", "\n", - " ##### WIP\n", " (current_group, current_control, \n", " current_effsize) = effect_size_curve_plotter(ticks_to_plot=ticks_to_plot, \n", " results=results, \n", @@ -419,16 +403,21 @@ " plot_palette_contrast=plot_palette_contrast,\n", " )\n", "\n", - " ##### WIP\n", - "\n", " # Plot mini-meta violin\n", " if show_mini_meta or show_delta2:\n", - " contrast_xtick_labels = plot_minimeta_or_deltadelta_violins(show_mini_meta=show_mini_meta, effectsize_df=effectsize_df, \n", - " ci_type=ci_type, rawdata_axes=rawdata_axes,contrast_axes=contrast_axes, \n", - " violinplot_kwargs=violinplot_kwargs, halfviolin_alpha=halfviolin_alpha, \n", - " ytick_color=ytick_color, es_marker_size=es_marker_size, group_summary_kwargs=group_summary_kwargs, \n", - " contrast_xtick_labels=contrast_xtick_labels, effect_size=effect_size\n", - " )\n", + " contrast_xtick_labels = plot_minimeta_or_deltadelta_violins(show_mini_meta=show_mini_meta, \n", + " effectsize_df=effectsize_df, \n", + " ci_type=ci_type, \n", + " rawdata_axes=rawdata_axes,\n", + " contrast_axes=contrast_axes, \n", + " violinplot_kwargs=violinplot_kwargs, \n", + " halfviolin_alpha=halfviolin_alpha, \n", + " ytick_color=ytick_color, \n", + " es_marker_size=es_marker_size, \n", + " group_summary_kwargs=group_summary_kwargs, \n", + " contrast_xtick_labels=contrast_xtick_labels, \n", + " effect_size=effect_size\n", + " )\n", "\n", "\n", " # Make sure the contrast_axes x-lims match the rawdata_axes xlims,\n", @@ -896,134 +885,26 @@ " og_xlim_delta[1], og_ylim_delta[0], og_ylim_delta[1], **redraw_axes_kwargs\n", " )\n", "\n", - " ################################################### GRIDKEY MAIN CODE WIP\n", + " ################################################### GRIDKEY WIP\n", " # if gridkey_rows is None, skip everything here\n", + " gridkey_rows = plot_kwargs[\"gridkey_rows\"]\n", " if gridkey_rows is not None:\n", - " # Raise error if there are more than 2 items in any idx and gridkey_merge_pairs is True and is_paired is not None\n", - " if gridkey_merge_pairs and is_paired is not None:\n", - " for i in idx:\n", - " if len(i) > 2:\n", - " warnings.warn(\n", - " \"gridkey_merge_pairs=True only works if all idx in tuples have only two items. gridkey_merge_pairs has automatically been set to False\"\n", - " )\n", - " gridkey_merge_pairs = False\n", - " break\n", - " elif gridkey_merge_pairs and is_paired is None:\n", - " warnings.warn(\n", - " \"gridkey_merge_pairs=True is only applicable for paired data.\"\n", - " )\n", - " gridkey_merge_pairs = False\n", - "\n", - " # Checks for gridkey_merge_pairs and is_paired; if both are true, \"merges\" the gridkey per pair\n", - " if gridkey_merge_pairs and is_paired is not None:\n", - " groups_for_gridkey = []\n", - " for i in idx:\n", - " groups_for_gridkey.append(i[1])\n", - " else:\n", - " groups_for_gridkey = all_plot_groups\n", - "\n", - " # raise errors if gridkey_rows is not a list, or if the list is empty\n", - " if isinstance(gridkey_rows, list) is False:\n", - " raise TypeError(\"gridkey_rows must be a list.\")\n", - " elif len(gridkey_rows) == 0:\n", - " warnings.warn(\"gridkey_rows is an empty list.\")\n", - "\n", - " # raise Warning if an item in gridkey_rows is not contained in any idx\n", - " for i in gridkey_rows:\n", - " in_idx = 0\n", - " for j in groups_for_gridkey:\n", - " if i in j:\n", - " in_idx += 1\n", - " if in_idx == 0:\n", - " if is_paired is not None:\n", - " warnings.warn(\n", - " i\n", - " + \" is not in any idx. Please check. Alternatively, merging gridkey pairs may not be suitable for your data; try passing gridkey_merge_pairs=False.\"\n", - " )\n", - " else:\n", - " warnings.warn(i + \" is not in any idx. Please check.\")\n", - "\n", - " # Populate table: checks if idx for each column contains rowlabel name\n", - " # IF so, marks that element as present w black dot, or space if not present\n", - " table_cellcols = []\n", - " for i in gridkey_rows:\n", - " thisrow = []\n", - " for q in groups_for_gridkey:\n", - " if str(i) in q:\n", - " thisrow.append(\"\\u25CF\")\n", - " else:\n", - " thisrow.append(\"\")\n", - " table_cellcols.append(thisrow)\n", - "\n", - " # Adds a row for Ns with the Ns values\n", - " if gridkey_show_Ns:\n", - " gridkey_rows.append(\"Ns\")\n", - " list_of_Ns = []\n", - " for i in groups_for_gridkey:\n", - " list_of_Ns.append(str(plot_data.groupby(xvar).count()[yvar].loc[i]))\n", - " table_cellcols.append(list_of_Ns)\n", - "\n", - " # Adds a row for effectsizes with effectsize values\n", - " if gridkey_show_es:\n", - " gridkey_rows.append(\"\\u0394\")\n", - " effsize_list = []\n", - " results_list = results.test.to_list()\n", - "\n", - " # get the effect size, append + or -, 2 dec places\n", - " for i in enumerate(groups_for_gridkey):\n", - " if i[1] in results_list:\n", - " curr_esval = results.loc[results[\"test\"] == i[1]][\n", - " \"difference\"\n", - " ].iloc[0]\n", - " curr_esval_str = np.format_float_positional(\n", - " curr_esval,\n", - " precision=2,\n", - " sign=True,\n", - " trim=\"k\",\n", - " min_digits=2,\n", - " )\n", - " effsize_list.append(curr_esval_str)\n", - " else:\n", - " effsize_list.append(\"-\")\n", - "\n", - " table_cellcols.append(effsize_list)\n", + " grid_key_WIP(is_paired=is_paired, \n", + " idx=idx, \n", + " all_plot_groups=all_plot_groups, \n", + " gridkey_rows=gridkey_rows, \n", + " rawdata_axes=rawdata_axes, \n", + " contrast_axes=contrast_axes,\n", + " plot_data=plot_data, \n", + " xvar=xvar, \n", + " yvar=yvar, \n", + " results=results, \n", + " show_delta2=show_delta2, \n", + " show_mini_meta=show_mini_meta, \n", + " float_contrast=float_contrast,\n", + " plot_kwargs=plot_kwargs,\n", + " )\n", "\n", - " # If Gardner-Altman plot, plot on raw data and not contrast axes\n", - " if float_contrast:\n", - " axes_ploton = rawdata_axes\n", - " else:\n", - " axes_ploton = contrast_axes\n", - "\n", - " # Account for extended x axis in case of show_delta2 or show_mini_meta\n", - " x_groups_for_width = len(groups_for_gridkey)\n", - " if show_delta2 or show_mini_meta:\n", - " x_groups_for_width += 2\n", - " gridkey_width = len(groups_for_gridkey) / x_groups_for_width\n", - "\n", - " gridkey = axes_ploton.table(\n", - " cellText=table_cellcols,\n", - " rowLabels=gridkey_rows,\n", - " cellLoc=\"center\",\n", - " bbox=[\n", - " 0,\n", - " -len(gridkey_rows) * 0.1 - 0.05,\n", - " gridkey_width,\n", - " len(gridkey_rows) * 0.1,\n", - " ],\n", - " **{\"alpha\": 0.5}\n", - " )\n", - "\n", - " # modifies row label cells\n", - " for cell in gridkey._cells:\n", - " if cell[1] == -1:\n", - " gridkey._cells[cell].visible_edges = \"open\"\n", - " gridkey._cells[cell].set_text_props(**{\"ha\": \"right\"})\n", - "\n", - " # turns off both x axes\n", - " rawdata_axes.get_xaxis().set_visible(False)\n", - " contrast_axes.get_xaxis().set_visible(False)\n", - " ####################################################### END GRIDKEY MAIN CODE WIP\n", - " \n", " ################################################### Swarm & Contrast & Summary Bars & Delta text WIP\n", " # Swarm bars WIP\n", " swarm_bars = plot_kwargs[\"swarm_bars\"]\n",