Skip to content

Commit 09d38d3

Browse files
committed
Added mini-meta and delta combination functionality to forest plot
> mini-meta delta and regular deltas from those dabest objects can be plotted alongside each other now. Added a image test for this. > updated two kwargs names for forest plot > updated forest plot tutorial
1 parent 4616f79 commit 09d38d3

File tree

5 files changed

+173
-95
lines changed

5 files changed

+173
-95
lines changed

dabest/forest_plot.py

+56-38
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,41 @@ def load_plot_data(
7272
current_contrast = data[current_idx]
7373
if len(index_group)>0:
7474
for index in index_group:
75-
if index == 2:
76-
current_plot_data = getattr(getattr(current_contrast, effect_attr), contrast_attr)
77-
bootstraps.append(current_plot_data.bootstraps_delta_delta)
78-
differences.append(current_plot_data.difference)
79-
bcalows.append(current_plot_data.bca_low)
80-
bcahighs.append(current_plot_data.bca_high)
81-
elif index == 0 or index == 1:
82-
current_plot_data = getattr(current_contrast, effect_attr)
83-
bootstraps.append(current_plot_data.results.bootstraps[index])
84-
differences.append(current_plot_data.results.difference[index])
85-
bcalows.append(current_plot_data.results.bca_low[index])
86-
bcahighs.append(current_plot_data.results.bca_high[index])
75+
if contrast_type == 'delta2':
76+
if index == 2:
77+
current_plot_data = getattr(getattr(current_contrast, effect_attr), contrast_attr)
78+
bootstraps.append(current_plot_data.bootstraps_delta_delta)
79+
differences.append(current_plot_data.difference)
80+
bcalows.append(current_plot_data.bca_low)
81+
bcahighs.append(current_plot_data.bca_high)
82+
elif index == 0 or index == 1:
83+
current_plot_data = getattr(current_contrast, effect_attr)
84+
bootstraps.append(current_plot_data.results.bootstraps[index])
85+
differences.append(current_plot_data.results.difference[index])
86+
bcalows.append(current_plot_data.results.bca_low[index])
87+
bcahighs.append(current_plot_data.results.bca_high[index])
88+
else:
89+
raise ValueError("The selected indices must be 0, 1, or 2.")
8790
else:
88-
raise ValueError("The selected indices must be 0, 1, or 2.")
91+
num_of_groups = len(getattr(current_contrast, effect_attr).results)
92+
if index == num_of_groups:
93+
current_plot_data = getattr(getattr(current_contrast, effect_attr), contrast_attr)
94+
bootstraps.append(current_plot_data.bootstraps_weighted_delta)
95+
differences.append(current_plot_data.difference)
96+
bcalows.append(current_plot_data.results.bca_low)
97+
bcahighs.append(current_plot_data.results.bca_high)
98+
elif index < num_of_groups:
99+
current_plot_data = getattr(current_contrast, effect_attr)
100+
bootstraps.append(current_plot_data.results.bootstraps[index])
101+
differences.append(current_plot_data.results.difference[index])
102+
bcalows.append(current_plot_data.results.bca_low[index])
103+
bcahighs.append(current_plot_data.results.bca_high[index])
104+
else:
105+
msg1 = "There are only {} groups (starting from zero) in this dabest object. ".format(num_of_groups)
106+
msg2 = "The idx given is {}.".format(index)
107+
raise ValueError(msg1+msg2)
89108
else:
90109
contrast_plot_data = [getattr(getattr(contrast, effect_attr), contrast_attr) for contrast in data]
91-
92110
attribute_suffix = "weighted_delta" if contrast_type == "mini_meta" else "delta_delta"
93111

94112
bootstraps = [getattr(result, f"bootstraps_{attribute_suffix}") for result in contrast_plot_data]
@@ -146,8 +164,8 @@ def check_for_errors(
146164
if idx is not None:
147165
if not isinstance(idx, (tuple, list)):
148166
raise TypeError("`idx` must be a tuple or list of integers.")
149-
if contrast_type == "mini_meta":
150-
raise ValueError("The `idx` argument is not applicable to mini-meta analyses.")
167+
# if contrast_type == "mini_meta":
168+
# raise ValueError("The `idx` argument is not applicable to mini-meta analyses.")
151169

152170
# Axes
153171
if ax is not None and not isinstance(ax, plt.Axes):
@@ -245,8 +263,8 @@ def get_kwargs(
245263
violin_kwargs,
246264
zeroline_kwargs,
247265
horizontal,
248-
es_marker_kwargs,
249-
es_errorbar_kwargs,
266+
marker_kwargs,
267+
errorbar_kwargs,
250268
marker_size
251269
):
252270
from .misc_tools import merge_two_dicts
@@ -274,32 +292,32 @@ def get_kwargs(
274292
zeroline_kwargs = merge_two_dicts(default_zeroline_kwargs, zeroline_kwargs)
275293

276294
# Effect size marker kwargs
277-
default_es_marker_kwargs = {
295+
default_marker_kwargs = {
278296
'marker': 'o',
279297
'markersize': marker_size,
280298
'color': 'black',
281299
'alpha': 1,
282300
'zorder': 2,
283301
}
284-
if es_marker_kwargs is None:
285-
es_marker_kwargs = default_es_marker_kwargs
302+
if marker_kwargs is None:
303+
marker_kwargs = default_marker_kwargs
286304
else:
287-
es_marker_kwargs = merge_two_dicts(default_es_marker_kwargs, es_marker_kwargs)
305+
marker_kwargs = merge_two_dicts(default_marker_kwargs, marker_kwargs)
288306

289307
# Effect size error bar kwargs
290-
default_es_errorbar_kwargs = {
308+
default_errorbar_kwargs = {
291309
'color': 'black',
292310
'lw': 2.5,
293311
'linestyle': '-',
294312
'alpha': 1,
295313
'zorder': 1,
296314
}
297-
if es_errorbar_kwargs is None:
298-
es_errorbar_kwargs = default_es_errorbar_kwargs
315+
if errorbar_kwargs is None:
316+
errorbar_kwargs = default_errorbar_kwargs
299317
else:
300-
es_errorbar_kwargs = merge_two_dicts(default_es_errorbar_kwargs, es_errorbar_kwargs)
318+
errorbar_kwargs = merge_two_dicts(default_errorbar_kwargs, errorbar_kwargs)
301319

302-
return violin_kwargs, zeroline_kwargs, es_marker_kwargs, es_errorbar_kwargs
320+
return violin_kwargs, zeroline_kwargs, marker_kwargs, errorbar_kwargs
303321

304322

305323
def color_palette(
@@ -355,8 +373,8 @@ def forest_plot(
355373

356374
violin_kwargs: Optional[dict] = None,
357375
zeroline_kwargs: Optional[dict] = None,
358-
es_marker_kwargs: Optional[dict] = None,
359-
es_errorbar_kwargs: Optional[dict] = None,
376+
marker_kwargs: Optional[dict] = None,
377+
errorbar_kwargs: Optional[dict] = None,
360378
)-> plt.Figure:
361379
"""
362380
Custom function that generates a forest plot from given contrast objects, suitable for a range of data analysis types, including those from packages like DABEST-python.
@@ -412,9 +430,9 @@ def forest_plot(
412430
Additional arguments for violin plot customization.
413431
zeroline_kwargs : Optional[dict], default=None
414432
Additional arguments for the zero line customization.
415-
es_marker_kwargs : Optional[dict], default=None
433+
marker_kwargs : Optional[dict], default=None
416434
Additional arguments for the effect size marker customization.
417-
es_errorbar_kwargs : Optional[dict], default=None
435+
errorbar_kwargs : Optional[dict], default=None
418436
Additional arguments for the effect size error bar customization.
419437
420438
Returns
@@ -469,12 +487,12 @@ def forest_plot(
469487
fig, ax = plt.subplots(figsize=fig_size)
470488

471489
# Get Kwargs
472-
violin_kwargs, zeroline_kwargs, es_marker_kwargs, es_errorbar_kwargs = get_kwargs(
490+
violin_kwargs, zeroline_kwargs, marker_kwargs, errorbar_kwargs = get_kwargs(
473491
violin_kwargs = violin_kwargs,
474492
zeroline_kwargs = zeroline_kwargs,
475493
horizontal = horizontal,
476-
es_marker_kwargs = es_marker_kwargs,
477-
es_errorbar_kwargs = es_errorbar_kwargs,
494+
marker_kwargs = marker_kwargs,
495+
errorbar_kwargs = errorbar_kwargs,
478496
marker_size = marker_size
479497
)
480498

@@ -492,11 +510,11 @@ def forest_plot(
492510
## Plotting the effect sizes and confidence intervals
493511
for k in range(1, number_of_curves_to_plot + 1):
494512
if horizontal:
495-
ax.plot(differences[k - 1], k, **es_marker_kwargs)
496-
ax.plot([bcalows[k - 1], bcahighs[k - 1]], [k, k], **es_errorbar_kwargs)
513+
ax.plot(differences[k - 1], k, **marker_kwargs)
514+
ax.plot([bcalows[k - 1], bcahighs[k - 1]], [k, k], **errorbar_kwargs)
497515
else:
498-
ax.plot(k, differences[k - 1], **es_marker_kwargs)
499-
ax.plot([k, k], [bcalows[k - 1], bcahighs[k - 1]], **es_errorbar_kwargs)
516+
ax.plot(k, differences[k - 1], **marker_kwargs)
517+
ax.plot([k, k], [bcalows[k - 1], bcahighs[k - 1]], **errorbar_kwargs)
500518

501519
# Aesthetic Adjustments
502520
## Handle the custom color palette

0 commit comments

Comments
 (0)