Skip to content

Commit 62b1145

Browse files
committed
Fixes to delta-delta spine in paired condition and added tutorial info for gridkey
1 parent 6c60a58 commit 62b1145

26 files changed

+349
-164
lines changed

dabest/misc_tools.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,6 @@ def extract_contrast_plotting_ticks(is_paired, show_pairs, two_col_sankey, plot_
920920
ticks_to_start_twocol_sankey.pop()
921921
ticks_to_start_twocol_sankey.insert(0, 0)
922922
else:
923-
# ticks_to_skip = np.arange(0, len(temp_all_plot_groups), 2).tolist()
924-
# ticks_to_plot = np.arange(1, len(temp_all_plot_groups), 2).tolist()
925923
ticks_to_skip = np.cumsum([len(t) for t in idx])[:-1].tolist()
926924
ticks_to_skip.insert(0, 0)
927925
# Then obtain the ticks where we have to plot the effect sizes.
@@ -1004,7 +1002,7 @@ def set_xaxis_ticks_and_lims(show_delta2, show_mini_meta, rawdata_axes, contrast
10041002
contrast_axes.set_ylim(rawdata_axes.get_ylim())
10051003
else:
10061004
contrast_axes.set_ylim(rawdata_axes.get_ylim())
1007-
1005+
# Vertical
10081006
else:
10091007
# Ticks
10101008
if show_delta2 is False and show_mini_meta is False:
@@ -1025,7 +1023,7 @@ def set_xaxis_ticks_and_lims(show_delta2, show_mini_meta, rawdata_axes, contrast
10251023
# Increase the xlim of raw data by 2
10261024
temp = rawdata_axes.get_xlim()
10271025
if show_pairs:
1028-
rawdata_axes.set_xlim(temp[0], temp[1] + 0.25)
1026+
rawdata_axes.set_xlim(temp[0], temp[1] + 0.5)
10291027
else:
10301028
rawdata_axes.set_xlim(temp[0], temp[1] + 2)
10311029
contrast_axes.set_xlim(rawdata_axes.get_xlim())
@@ -1451,7 +1449,8 @@ def Cumming_Plot_Aesthetic_Adjustments(contrast_axes, reflines_kwargs, is_paired
14511449
rawdata_axes.set_ylim(swarm_ylim[0]-0.5, swarm_ylim[1])
14521450
contrast_axes.set_ylim(contrast_ylim[0]-0.5, contrast_ylim[1])
14531451

1454-
def Redraw_Spines(rawdata_axes, contrast_axes, redraw_axes_kwargs, float_contrast, horizontal):
1452+
def Redraw_Spines(rawdata_axes, contrast_axes, redraw_axes_kwargs, float_contrast, horizontal,
1453+
show_delta2, delta2_axes):
14551454
"""
14561455
Aesthetic general adjustments across both GA and Cumming plots.
14571456
@@ -1469,6 +1468,10 @@ def Redraw_Spines(rawdata_axes, contrast_axes, redraw_axes_kwargs, float_contras
14691468
A boolean flag to determine if the plot is GA or Cum
14701469
horizontal : bool
14711470
A boolean flag to determine if the plot is for horizontal plotting.
1471+
show_delta2 : bool
1472+
A boolean flag to determine if the plot will have a delta-delta effect size.
1473+
delta2_axes : object (Axes)
1474+
The delta2 axes.
14721475
"""
14731476

14741477
# Because we turned the axes frame off, we also need to draw back the x-spine for both axes.
@@ -1505,11 +1508,22 @@ def Redraw_Spines(rawdata_axes, contrast_axes, redraw_axes_kwargs, float_contras
15051508
og_ylim_contrast[1],
15061509
**redraw_axes_kwargs
15071510
)
1511+
1512+
if show_delta2:
1513+
og_xlim_delta, og_ylim_delta = contrast_axes.get_xlim(), contrast_axes.get_ylim()
1514+
delta2_axes.set_ylim(og_ylim_delta)
1515+
1516+
delta2_axes.vlines(
1517+
og_xlim_delta[1],
1518+
og_ylim_delta[0],
1519+
og_ylim_delta[1],
1520+
**redraw_axes_kwargs
1521+
)
1522+
15081523
for ax, xlim, ylim in zip([rawdata_axes, contrast_axes], [og_xlim_raw, og_xlim_contrast], [og_ylim_raw, og_ylim_contrast]):
15091524
ax.set_xlim(xlim)
15101525
ax.set_ylim(ylim)
15111526

1512-
15131527
def extract_group_summaries(proportional, err_color, rawdata_axes, asymmetric_side, horizontal,
15141528
bootstraps_color_by_group, plot_palette_raw, all_plot_groups,
15151529
n_groups, color_col, ytick_color, plot_kwargs):

dabest/plot_tools.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ def slopegraph_plotter(dabest_obj, plot_data, xvar, yvar, color_col, plot_palett
13711371
def plot_minimeta_or_deltadelta_violins(show_mini_meta, effectsize_df, ci_type, rawdata_axes,
13721372
contrast_axes, violinplot_kwargs, halfviolin_alpha, ytick_color,
13731373
es_marker_size, group_summary_kwargs, contrast_xtick_labels, effect_size,
1374-
show_delta2, plot_kwargs, redraw_axes_kwargs, horizontal):
1374+
show_delta2, plot_kwargs, horizontal):
13751375
"""
13761376
Add mini meta-analysis or delta-delta violin plots to the contrast plot.
13771377
@@ -1405,8 +1405,6 @@ def plot_minimeta_or_deltadelta_violins(show_mini_meta, effectsize_df, ci_type,
14051405
Whether to show the delta-delta.
14061406
plot_kwargs : dict
14071407
Keyword arguments for the plot.
1408-
redraw_axes_kwargs : dict
1409-
Keyword arguments for the redraw axes.
14101408
horizontal : bool
14111409
If the plot is horizontal.
14121410
"""
@@ -1486,7 +1484,7 @@ def plot_minimeta_or_deltadelta_violins(show_mini_meta, effectsize_df, ci_type,
14861484
else:
14871485
contrast_xtick_labels.extend(["", "delta-delta"])
14881486

1489-
# Create delta2 axis and label.
1487+
# Create the delta-delta axes.
14901488
if show_delta2 and not horizontal:
14911489
if plot_kwargs["delta2_label"] is not None:
14921490
delta2_label = plot_kwargs["delta2_label"]
@@ -1500,15 +1498,10 @@ def plot_minimeta_or_deltadelta_violins(show_mini_meta, effectsize_df, ci_type,
15001498
delta2_axes.set_ylabel(delta2_label, fontsize=fontsize_delta2label)
15011499
og_xlim_delta, og_ylim_delta = contrast_axes.get_xlim(), contrast_axes.get_ylim()
15021500
delta2_axes.set_ylim(og_ylim_delta)
1503-
delta2_axes.vlines(
1504-
og_xlim_delta[1],
1505-
og_ylim_delta[0],
1506-
og_ylim_delta[1],
1507-
**redraw_axes_kwargs
1508-
)
1509-
contrast_axes.set_xlim(og_xlim_delta)
1510-
1511-
return contrast_xtick_labels
1501+
else:
1502+
delta2_axes = None
1503+
1504+
return delta2_axes, contrast_xtick_labels
15121505

15131506

15141507
def effect_size_curve_plotter(ticks_to_plot, results, ci_type, contrast_axes, violinplot_kwargs, halfviolin_alpha,
@@ -1608,11 +1601,56 @@ def effect_size_curve_plotter(ticks_to_plot, results, ci_type, contrast_axes, vi
16081601
)
16091602
return current_group, current_control, current_effsize, contrast_xtick_labels
16101603

1611-
16121604
def gridkey_plotter(is_paired, idx, all_plot_groups, gridkey_rows, rawdata_axes, contrast_axes,
16131605
plot_data, xvar, yvar, results, show_delta2, show_mini_meta,
16141606
plot_kwargs, x1_level, experiment_label, float_contrast, horizontal,
16151607
delta_delta, mini_meta_delta, effect_size):
1608+
"""
1609+
Add gridkey to the contrast plot.
1610+
1611+
Parameters
1612+
----------
1613+
is_paired : bool
1614+
Whether the data is paired.
1615+
idx : list
1616+
List of indices of the contrast objects.
1617+
all_plot_groups : list
1618+
List of all plot groups.
1619+
gridkey_rows : list
1620+
List of gridkey rows.
1621+
rawdata_axes : object
1622+
Matplotlib axis object for the raw data.
1623+
contrast_axes : object
1624+
Matplotlib axis object for the contrast data.
1625+
plot_data : object (Dataframe)
1626+
Dataframe of the plot data.
1627+
xvar : str
1628+
Column name of the x variable.
1629+
yvar : str
1630+
Column name of the y variable.
1631+
results : object (Dataframe)
1632+
Dataframe of contrast object comparisons.
1633+
show_delta2 : bool
1634+
Whether to show the delta-delta.
1635+
show_mini_meta : bool
1636+
Whether to show the mini meta-analysis.
1637+
plot_kwargs : dict
1638+
Keyword arguments for the plot.
1639+
x1_level : list
1640+
List of x1 levels.
1641+
experiment_label : list
1642+
List of experiment labels.
1643+
float_contrast : bool
1644+
Whether the DABEST plot uses Gardner-Altman or Cummings
1645+
horizontal : bool
1646+
If the plot is horizontal.
1647+
delta_delta : object
1648+
delta-delta object.
1649+
mini_meta_delta : object
1650+
Mini meta-analysis object.
1651+
effect_size : str
1652+
Type of effect size to plot
1653+
"""
16161654

16171655
gridkey_delimiters=plot_kwargs["gridkey_delimiters"] # Auto parser for gridkey - implemented by SangyuXu
16181656
if gridkey_rows == "auto":

dabest/plotter.py

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
140140
)
141141

142142
# Extract Color palette
143-
(color_col, bootstraps_color_by_group, n_groups, filled,
144-
swarm_colors, plot_palette_raw, bar_color,
143+
(color_col, bootstraps_color_by_group, n_groups,
144+
filled, swarm_colors, plot_palette_raw, bar_color,
145145
plot_palette_bar, plot_palette_contrast, plot_palette_sankey) = get_color_palette(
146146
plot_kwargs=plot_kwargs,
147147
plot_data=plot_data,
@@ -359,8 +359,9 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
359359
)
360360

361361
# Plot mini-meta or delta-delta violin
362+
delta2_axes = None
362363
if show_mini_meta or show_delta2:
363-
contrast_xtick_labels = plot_minimeta_or_deltadelta_violins(
364+
delta2_axes, contrast_xtick_labels = plot_minimeta_or_deltadelta_violins(
364365
show_mini_meta=show_mini_meta,
365366
effectsize_df=effectsize_df,
366367
ci_type=ci_type,
@@ -373,9 +374,8 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
373374
group_summary_kwargs=group_summary_kwargs,
374375
contrast_xtick_labels=contrast_xtick_labels,
375376
effect_size=effect_size,
376-
show_delta2=show_delta2,
377-
plot_kwargs=plot_kwargs,
378-
redraw_axes_kwargs=redraw_axes_kwargs,
377+
show_delta2=show_delta2,
378+
plot_kwargs=plot_kwargs,
379379
horizontal=horizontal,
380380
)
381381

@@ -433,7 +433,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
433433
)
434434
else:
435435
# 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
437436
skip_redraw_lines = True if plot_kwargs["gridkey_rows"] is not None else False
438437
Cumming_Plot_Aesthetic_Adjustments(
439438
contrast_axes=contrast_axes,
@@ -461,7 +460,9 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
461460
contrast_axes=contrast_axes,
462461
redraw_axes_kwargs=redraw_axes_kwargs,
463462
float_contrast=float_contrast,
464-
horizontal=horizontal
463+
horizontal=horizontal,
464+
show_delta2=show_delta2,
465+
delta2_axes=delta2_axes
465466
)
466467

467468
# Swarm bars WIP
@@ -503,80 +504,77 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
503504
summary_bars = plot_kwargs["summary_bars"]
504505
if summary_bars is not None and not horizontal:
505506
summary_bars_plotter(
506-
summary_bars=summary_bars,
507-
results=results,
508-
ax_to_plot=contrast_axes,
509-
float_contrast=float_contrast,
510-
summary_bars_kwargs=summary_bars_kwargs,
511-
ci_type=ci_type,
512-
ticks_to_plot=ticks_to_plot,
513-
color_col=color_col,
514-
plot_palette_raw=plot_palette_raw,
515-
proportional=proportional,
516-
is_paired=is_paired
507+
summary_bars = summary_bars,
508+
results = results,
509+
ax_to_plot = contrast_axes,
510+
float_contrast = float_contrast,
511+
summary_bars_kwargs = summary_bars_kwargs,
512+
ci_type = ci_type,
513+
ticks_to_plot = ticks_to_plot,
514+
color_col = color_col,
515+
plot_palette_raw = plot_palette_raw,
516+
proportional = proportional,
517+
is_paired = is_paired
517518
)
518519
# Delta text WIP
519520
delta_text = plot_kwargs["delta_text"]
520521
if delta_text and not horizontal:
521522
delta_text_plotter(
522-
results=results,
523-
ax_to_plot=contrast_axes,
524-
swarm_plot_ax=rawdata_axes,
525-
ticks_to_plot=ticks_to_plot,
526-
delta_text_kwargs=delta_text_kwargs,
527-
color_col=color_col,
528-
plot_palette_raw=plot_palette_raw,
529-
is_paired=is_paired,
530-
proportional=proportional,
531-
float_contrast=float_contrast,
532-
show_mini_meta=show_mini_meta,
533-
mini_meta_delta=effectsize_df.mini_meta_delta if show_mini_meta else None,
534-
show_delta2=show_delta2,
535-
delta_delta=effectsize_df.delta_delta if show_delta2 else None,
536-
idx=idx
523+
results = results,
524+
ax_to_plot = contrast_axes,
525+
swarm_plot_ax = rawdata_axes,
526+
ticks_to_plot = ticks_to_plot,
527+
delta_text_kwargs = delta_text_kwargs,
528+
color_col = color_col,
529+
plot_palette_raw = plot_palette_raw,
530+
is_paired = is_paired,
531+
proportional = proportional,
532+
float_contrast = float_contrast,
533+
show_mini_meta = show_mini_meta,
534+
mini_meta_delta = effectsize_df.mini_meta_delta if show_mini_meta else None,
535+
show_delta2 = show_delta2,
536+
delta_delta = effectsize_df.delta_delta if show_delta2 else None,
537+
idx = idx
537538
)
538539

539540
# Table Axes for horizontal plots
540541
if horizontal:
541542
table_for_horizontal_plots(
542-
effectsize_df=effectsize_df,
543+
effectsize_df = effectsize_df,
543544
ax = table_axes,
544-
contrast_axes=contrast_axes,
545-
ticks_to_plot=table_axes_ticks_to_plot,
546-
show_mini_meta=show_mini_meta,
547-
show_delta2=show_delta2,
548-
table_kwargs=table_kwargs,
545+
contrast_axes = contrast_axes,
546+
ticks_to_plot = table_axes_ticks_to_plot,
547+
show_mini_meta = show_mini_meta,
548+
show_delta2 = show_delta2,
549+
table_kwargs = table_kwargs,
549550
)
550-
551551

552-
# GRIDKEY WIP
552+
# GRIDKEY
553553
gridkey_rows = plot_kwargs["gridkey_rows"]
554-
# if gridkey_rows is not None and not any([show_mini_meta, show_delta2]):
555554
if gridkey_rows is not None:
556555
gridkey_plotter(
557-
is_paired=is_paired,
558-
idx=idx,
559-
all_plot_groups=all_plot_groups,
560-
gridkey_rows=gridkey_rows,
556+
is_paired = is_paired,
557+
idx = idx,
558+
all_plot_groups = all_plot_groups,
559+
gridkey_rows = gridkey_rows,
561560
rawdata_axes = rawdata_axes,
562561
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,
562+
plot_data = plot_data,
563+
xvar = xvar,
564+
yvar = yvar,
565+
results = results,
566+
show_delta2 = show_delta2,
567+
show_mini_meta = show_mini_meta,
568+
plot_kwargs = plot_kwargs,
569+
x1_level = x1_level,
570+
experiment_label = experiment_label,
571+
float_contrast = float_contrast,
572+
horizontal = horizontal,
574573
delta_delta = effectsize_df.delta_delta if show_delta2 else None,
575574
mini_meta_delta = effectsize_df.mini_meta_delta if show_mini_meta else None,
576575
effect_size = effect_size,
577576
)
578577

579-
580578
# Make sure no stray ticks appear!
581579
rawdata_axes.xaxis.set_ticks_position("bottom")
582580
rawdata_axes.yaxis.set_ticks_position("left")

0 commit comments

Comments
 (0)