Skip to content

Commit dd41a01

Browse files
authored
Merge pull request #197 from ACCLAB/feat-results-dataframe
Add results dataframe for deltadelta and minimeta and color_palette bug fix for sankey
2 parents f9addb1 + e8ffca4 commit dd41a01

12 files changed

Lines changed: 311 additions & 65 deletions

dabest/_delta_objects.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,29 @@ def to_dict(self):
209209
dictionary.
210210
"""
211211
# Only get public (user-facing) attributes.
212-
attrs = [a for a in dir(self) if not a.startswith(("_", "to_dict"))]
212+
attrs = [a for a in dir(self) if not a.startswith(("_", "to_dict", "results"))]
213213
out = {}
214214
for a in attrs:
215215
out[a] = getattr(self, a)
216216
return out
217+
218+
def __compute_results(self):
219+
# With some inspiration from @jungyangliao
220+
delta_delta_results_df = pd.Series(self.to_dict()).to_frame().T
221+
222+
column_index = ['control', 'test', 'difference', 'ci', 'bca_low', 'bca_high', 'bca_interval_idx',
223+
'pct_low', 'pct_high', 'pct_interval_idx', 'bootstraps_control', 'bootstraps_test',
224+
'bootstraps_delta_delta', 'permutations_control', 'permutations_test', 'permutations_delta_delta',
225+
'pvalue_permutation', 'permutation_count', 'bias_correction', 'jackknives'
226+
]
227+
delta_delta_results_df['bootstraps_control'] = [delta_delta_results_df['bootstraps'][0][0]]
228+
delta_delta_results_df['bootstraps_test'] = [delta_delta_results_df['bootstraps'][0][1]]
229+
delta_delta_results_df['permutations_control'] = [delta_delta_results_df['permutations'][0][0]]
230+
delta_delta_results_df['permutations_test'] = [delta_delta_results_df['permutations'][0][1]]
231+
delta_delta_results_df = delta_delta_results_df.reindex(columns=column_index)
232+
233+
self.__results = delta_delta_results_df
234+
return self.__results
217235

218236
@property
219237
def ci(self):
@@ -352,6 +370,17 @@ def permutations_delta_delta(self):
352370
except AttributeError:
353371
self.__permutation_test()
354372
return self.__permutations_delta_delta
373+
374+
@property
375+
def results(self):
376+
"""
377+
Return the results of the delta-delta analysis.
378+
"""
379+
try:
380+
return self.__results
381+
except AttributeError:
382+
self.__compute_results()
383+
return self.__results
355384

356385
# %% ../nbs/API/delta_objects.ipynb 10
357386
class MiniMetaDelta(object):
@@ -575,11 +604,30 @@ def to_dict(self):
575604
"""
576605
# Only get public (user-facing) attributes.
577606
attrs = [a for a in dir(self)
578-
if not a.startswith(("_", "to_dict"))]
607+
if not a.startswith(("_", "to_dict", "results"))]
579608
out = {}
580609
for a in attrs:
581610
out[a] = getattr(self, a)
582611
return out
612+
613+
614+
def __compute_results(self):
615+
# With some inspiration from @jungyangliao
616+
"""
617+
Returns all attributes of the `dabest.MiniMetaDelta` object as a
618+
DataFrame.
619+
"""
620+
mini_meta_delta_results_df = pd.Series(self.to_dict()).to_frame().T
621+
column_index = ['control', 'test', 'control_N', 'test_N', 'control_var', 'test_var', 'group_var',
622+
'difference', 'ci', 'bca_low', 'bca_high', 'bca_interval_idx',
623+
'pct_low', 'pct_high', 'pct_interval_idx', 'bootstraps', 'bootstraps_weighted_delta',
624+
'permutations', 'permutations_var', 'permutations_weighted_delta', 'pvalue_permutation',
625+
'permutation_count', 'bias_correction', 'jackknives']
626+
mini_meta_delta_results_df = mini_meta_delta_results_df.reindex(columns=column_index)
627+
mini_meta_delta_results_df.rename(columns={'bootstraps': 'bootstraps_deltas'}, inplace=True)
628+
629+
self.__results = mini_meta_delta_results_df
630+
return self.__results
583631

584632

585633
@property
@@ -801,4 +849,13 @@ def permutations_weighted_delta(self):
801849
self.__permutation_test()
802850
return self.__permutations_weighted_delta
803851

804-
852+
@property
853+
def results(self):
854+
"""
855+
Return the results of the mini-meta analysis.
856+
"""
857+
try:
858+
return self.__results
859+
except AttributeError:
860+
self.__compute_results()
861+
return self.__results

dabest/misc_tools.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ def get_params(
138138
# Disable Gardner-Altman plotting if any of the idxs comprise of more than
139139
# two groups or if it is a delta-delta plot.
140140
float_contrast = plot_kwargs["float_contrast"]
141-
effect_size_type = effectsize_df.effect_size
141+
# effect_size_type = effectsize_df.effect_size
142142
if len(idx) > 1 or len(idx[0]) > 2:
143143
float_contrast = False
144144

145-
if effect_size_type in ["cliffs_delta"]:
145+
if effect_size in ["cliffs_delta"]:
146146
float_contrast = False
147147

148148
if show_delta2 or show_mini_meta:
@@ -176,7 +176,7 @@ def get_params(
176176
show_baseline_ec = plot_kwargs["show_baseline_ec"]
177177

178178
return (dabest_obj, plot_data, xvar, yvar, is_paired, effect_size, proportional, all_plot_groups, idx,
179-
show_delta2, show_mini_meta, float_contrast, show_pairs, effect_size_type, group_summaries, err_color, horizontal,
179+
show_delta2, show_mini_meta, float_contrast, show_pairs, group_summaries, err_color, horizontal,
180180
results, halfviolin_alpha, ci_type, x1_level, experiment_label, show_baseline_ec)
181181

182182
def get_kwargs(
@@ -486,6 +486,7 @@ def get_color_palette(
486486
idx: list,
487487
all_plot_groups: list,
488488
delta2: bool,
489+
sankey: bool
489490
):
490491
"""
491492
Create the color palette to be used in the plotter function.
@@ -506,6 +507,8 @@ def get_color_palette(
506507
A list of all the group names.
507508
delta2 : bool
508509
A boolean flag to determine if the plot will have a delta-delta effect size.
510+
sankey : bool
511+
A boolean flag to determine if the plot is for a Sankey diagram.
509512
"""
510513
# Create color palette that will be shared across subplots.
511514
color_col = plot_kwargs["color_col"]
@@ -560,6 +563,10 @@ def get_color_palette(
560563
groups_in_palette = {
561564
k: custom_pal[k] for k in color_groups
562565
}
566+
elif sankey:
567+
groups_in_palette = {
568+
k: custom_pal[k] for k in [1, 0]
569+
}
563570
elif color_col is None:
564571
groups_in_palette = {
565572
k: custom_pal[k] for k in all_plot_groups if k in color_groups

dabest/plotter.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
118118

119119
# Extract parameters and set kwargs
120120
(dabest_obj, plot_data, xvar, yvar, is_paired, effect_size, proportional, all_plot_groups, idx,
121-
show_delta2, show_mini_meta, float_contrast, show_pairs, effect_size_type, group_summaries, err_color, horizontal,
121+
show_delta2, show_mini_meta, float_contrast, show_pairs, group_summaries, err_color, horizontal,
122122
results, halfviolin_alpha, ci_type, x1_level, experiment_label, show_baseline_ec) = get_params(
123123
effectsize_df = effectsize_df,
124124
plot_kwargs = plot_kwargs,
@@ -152,7 +152,8 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
152152
show_pairs = show_pairs,
153153
idx = idx,
154154
all_plot_groups = all_plot_groups,
155-
delta2 = effectsize_df.delta2
155+
delta2 = effectsize_df.delta2,
156+
sankey = True if proportional and show_pairs else False,
156157
)
157158

158159
# Initialise the figure.
@@ -165,7 +166,7 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
165166
show_pairs = show_pairs,
166167
proportional = proportional,
167168
float_contrast = float_contrast,
168-
effect_size_type = effect_size_type,
169+
effect_size_type = effect_size,
169170
yvar = yvar,
170171
horizontal = horizontal,
171172
show_table = table_kwargs['show']
@@ -197,7 +198,8 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
197198

198199
# Add delta dots to the contrast axes for paired plots.
199200
show_delta_dots = plot_kwargs["delta_dot"]
200-
if show_delta_dots and is_paired is not None:
201+
unavailable_effect_sizes = ["hedges_g", "delta_g", "cohens_d"]
202+
if show_delta_dots and is_paired and not any([es in effect_size for es in unavailable_effect_sizes]):
201203
DeltaDotsPlotter(
202204
plot_data = plot_data,
203205
contrast_axes = contrast_axes,
@@ -423,7 +425,7 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
423425
if float_contrast and not horizontal:
424426
# For Gardner-Altman plots only.
425427
Gardner_Altman_Plot_Aesthetic_Adjustments(
426-
effect_size_type = effect_size_type,
428+
effect_size_type = effect_size,
427429
plot_data = plot_data,
428430
xvar = xvar,
429431
yvar = yvar,

nbs/API/delta_objects.ipynb

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,29 @@
269269
" dictionary.\n",
270270
" \"\"\"\n",
271271
" # Only get public (user-facing) attributes.\n",
272-
" attrs = [a for a in dir(self) if not a.startswith((\"_\", \"to_dict\"))]\n",
272+
" attrs = [a for a in dir(self) if not a.startswith((\"_\", \"to_dict\", \"results\"))]\n",
273273
" out = {}\n",
274274
" for a in attrs:\n",
275275
" out[a] = getattr(self, a)\n",
276276
" return out\n",
277+
" \n",
278+
" def __compute_results(self):\n",
279+
" # With some inspiration from @jungyangliao\n",
280+
" delta_delta_results_df = pd.Series(self.to_dict()).to_frame().T\n",
281+
"\n",
282+
" column_index = ['control', 'test', 'difference', 'ci', 'bca_low', 'bca_high', 'bca_interval_idx', \n",
283+
" 'pct_low', 'pct_high', 'pct_interval_idx', 'bootstraps_control', 'bootstraps_test', \n",
284+
" 'bootstraps_delta_delta', 'permutations_control', 'permutations_test', 'permutations_delta_delta',\n",
285+
" 'pvalue_permutation', 'permutation_count', 'bias_correction', 'jackknives'\n",
286+
" ]\n",
287+
" delta_delta_results_df['bootstraps_control'] = [delta_delta_results_df['bootstraps'][0][0]]\n",
288+
" delta_delta_results_df['bootstraps_test'] = [delta_delta_results_df['bootstraps'][0][1]]\n",
289+
" delta_delta_results_df['permutations_control'] = [delta_delta_results_df['permutations'][0][0]]\n",
290+
" delta_delta_results_df['permutations_test'] = [delta_delta_results_df['permutations'][0][1]]\n",
291+
" delta_delta_results_df = delta_delta_results_df.reindex(columns=column_index)\n",
292+
"\n",
293+
" self.__results = delta_delta_results_df\n",
294+
" return self.__results\n",
277295
"\n",
278296
" @property\n",
279297
" def ci(self):\n",
@@ -411,7 +429,18 @@
411429
" return self.__permutations_delta_delta\n",
412430
" except AttributeError:\n",
413431
" self.__permutation_test()\n",
414-
" return self.__permutations_delta_delta"
432+
" return self.__permutations_delta_delta\n",
433+
" \n",
434+
" @property\n",
435+
" def results(self):\n",
436+
" \"\"\"\n",
437+
" Return the results of the delta-delta analysis.\n",
438+
" \"\"\"\n",
439+
" try:\n",
440+
" return self.__results\n",
441+
" except AttributeError:\n",
442+
" self.__compute_results()\n",
443+
" return self.__results"
415444
]
416445
},
417446
{
@@ -715,11 +744,30 @@
715744
" \"\"\"\n",
716745
" # Only get public (user-facing) attributes.\n",
717746
" attrs = [a for a in dir(self)\n",
718-
" if not a.startswith((\"_\", \"to_dict\"))]\n",
747+
" if not a.startswith((\"_\", \"to_dict\", \"results\"))]\n",
719748
" out = {}\n",
720749
" for a in attrs:\n",
721750
" out[a] = getattr(self, a)\n",
722751
" return out\n",
752+
" \n",
753+
"\n",
754+
" def __compute_results(self):\n",
755+
" # With some inspiration from @jungyangliao\n",
756+
" \"\"\"\n",
757+
" Returns all attributes of the `dabest.MiniMetaDelta` object as a\n",
758+
" DataFrame.\n",
759+
" \"\"\"\n",
760+
" mini_meta_delta_results_df = pd.Series(self.to_dict()).to_frame().T\n",
761+
" column_index = ['control', 'test', 'control_N', 'test_N', 'control_var', 'test_var', 'group_var',\n",
762+
" 'difference', 'ci', 'bca_low', 'bca_high', 'bca_interval_idx', \n",
763+
" 'pct_low', 'pct_high', 'pct_interval_idx', 'bootstraps', 'bootstraps_weighted_delta', \n",
764+
" 'permutations', 'permutations_var', 'permutations_weighted_delta', 'pvalue_permutation', \n",
765+
" 'permutation_count', 'bias_correction', 'jackknives']\n",
766+
" mini_meta_delta_results_df = mini_meta_delta_results_df.reindex(columns=column_index)\n",
767+
" mini_meta_delta_results_df.rename(columns={'bootstraps': 'bootstraps_deltas'}, inplace=True)\n",
768+
"\n",
769+
" self.__results = mini_meta_delta_results_df\n",
770+
" return self.__results\n",
723771
"\n",
724772
"\n",
725773
" @property\n",
@@ -940,7 +988,17 @@
940988
" except AttributeError:\n",
941989
" self.__permutation_test()\n",
942990
" return self.__permutations_weighted_delta\n",
943-
"\n"
991+
"\n",
992+
" @property\n",
993+
" def results(self):\n",
994+
" \"\"\"\n",
995+
" Return the results of the mini-meta analysis.\n",
996+
" \"\"\"\n",
997+
" try:\n",
998+
" return self.__results\n",
999+
" except AttributeError:\n",
1000+
" self.__compute_results()\n",
1001+
" return self.__results"
9441002
]
9451003
},
9461004
{

nbs/API/misc_tools.ipynb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@
191191
" # Disable Gardner-Altman plotting if any of the idxs comprise of more than\n",
192192
" # two groups or if it is a delta-delta plot.\n",
193193
" float_contrast = plot_kwargs[\"float_contrast\"]\n",
194-
" effect_size_type = effectsize_df.effect_size\n",
194+
" # effect_size_type = effectsize_df.effect_size\n",
195195
" if len(idx) > 1 or len(idx[0]) > 2:\n",
196196
" float_contrast = False\n",
197197
"\n",
198-
" if effect_size_type in [\"cliffs_delta\"]:\n",
198+
" if effect_size in [\"cliffs_delta\"]:\n",
199199
" float_contrast = False\n",
200200
"\n",
201201
" if show_delta2 or show_mini_meta:\n",
@@ -229,7 +229,7 @@
229229
" show_baseline_ec = plot_kwargs[\"show_baseline_ec\"]\n",
230230
" \n",
231231
" return (dabest_obj, plot_data, xvar, yvar, is_paired, effect_size, proportional, all_plot_groups, idx, \n",
232-
" show_delta2, show_mini_meta, float_contrast, show_pairs, effect_size_type, group_summaries, err_color, horizontal,\n",
232+
" show_delta2, show_mini_meta, float_contrast, show_pairs, group_summaries, err_color, horizontal,\n",
233233
" results, halfviolin_alpha, ci_type, x1_level, experiment_label, show_baseline_ec)\n",
234234
"\n",
235235
"def get_kwargs(\n",
@@ -539,6 +539,7 @@
539539
" idx: list, \n",
540540
" all_plot_groups: list,\n",
541541
" delta2: bool,\n",
542+
" sankey: bool\n",
542543
" ):\n",
543544
" \"\"\"\n",
544545
" Create the color palette to be used in the plotter function.\n",
@@ -559,6 +560,8 @@
559560
" A list of all the group names.\n",
560561
" delta2 : bool\n",
561562
" A boolean flag to determine if the plot will have a delta-delta effect size.\n",
563+
" sankey : bool\n",
564+
" A boolean flag to determine if the plot is for a Sankey diagram.\n",
562565
" \"\"\"\n",
563566
" # Create color palette that will be shared across subplots.\n",
564567
" color_col = plot_kwargs[\"color_col\"]\n",
@@ -613,6 +616,10 @@
613616
" groups_in_palette = {\n",
614617
" k: custom_pal[k] for k in color_groups\n",
615618
" }\n",
619+
" elif sankey:\n",
620+
" groups_in_palette = {\n",
621+
" k: custom_pal[k] for k in [1, 0]\n",
622+
" }\n",
616623
" elif color_col is None:\n",
617624
" groups_in_palette = {\n",
618625
" k: custom_pal[k] for k in all_plot_groups if k in color_groups\n",

0 commit comments

Comments
 (0)