Skip to content

Commit 7c71cc0

Browse files
committed
updated delta-delta plot
1 parent 49fce3e commit 7c71cc0

12 files changed

Lines changed: 54 additions & 107 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ testtt.py
126126
real.py
127127
0to2_beforeduringafter.csv
128128

129+
TEST.ipynb
130+
TrhCsCh.csv

dabest/_classes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ def plot(self, color_col=None,
17891789

17901790
float_contrast=True,
17911791
show_pairs=True,
1792+
show_delta2=True,
17921793
group_summaries=None,
17931794
group_summaries_offset=0.1,
17941795

dabest/plotter.py

Lines changed: 51 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
2020
halfviolin_alpha=0.8,
2121
float_contrast=True,
2222
show_pairs=True,
23+
show_delta2=True,
2324
group_summaries=None,
2425
group_summaries_offset=0.1,
2526
fig_size=None,
@@ -70,8 +71,12 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
7071
all_plot_groups = dabest_obj._all_plot_groups
7172
idx = dabest_obj.idx
7273

73-
if effect_size != "mean_diff":
74-
delta2 = False
74+
75+
76+
if effect_size != "mean_diff" or not delta2:
77+
show_delta2 = False
78+
else:
79+
show_delta2 = plot_kwargs["show_delta2"]
7580

7681

7782

@@ -87,7 +92,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
8792
if effect_size_type in ['cliffs_delta']:
8893
float_contrast = False
8994

90-
if delta2:
95+
if show_delta2:
9196
float_contrast = False
9297

9398
if not is_paired:
@@ -231,7 +236,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
231236
if fig_size is None:
232237
all_groups_count = np.sum([len(i) for i in dabest_obj.idx])
233238
# Increase the width for delta-delta graph
234-
if delta2:
239+
if show_delta2:
235240
all_groups_count += 2
236241
if is_paired and show_pairs is True:
237242
frac = 0.75
@@ -306,14 +311,6 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
306311
gridspec_kw={"width_ratios": width_ratios_ga,
307312
"wspace": 0},
308313
**init_fig_kwargs)
309-
rawdata_axes = axx[0]
310-
contrast_axes = axx[1]
311-
elif delta2:
312-
fig = plt.subplots(gridspec_kw={"hspace": 0.3},
313-
**init_fig_kwargs)
314-
rawdata_axes = plt.subplot2grid((2,3), (0,0), colspan=3)
315-
contrast_axes = plt.subplot2grid((2,3), (1,0), colspan=2)
316-
delta_axes = plt.subplot2grid((2,3), (1,2))
317314
else:
318315
fig, axx = plt.subplots(nrows=2,
319316
gridspec_kw={"hspace": 0.3},
@@ -324,12 +321,11 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
324321
contrast_ax_ylim_low = list()
325322
contrast_ax_ylim_high = list()
326323
contrast_ax_ylim_tickintervals = list()
327-
rawdata_axes = axx[0]
328-
contrast_axes = axx[1]
324+
rawdata_axes = axx[0]
325+
contrast_axes = axx[1]
329326
rawdata_axes.set_frame_on(False)
330327
contrast_axes.set_frame_on(False)
331-
if delta2:
332-
delta_axes.set_frame_on(False)
328+
333329

334330
redraw_axes_kwargs = {'colors' : ytick_color,
335331
'facecolors' : ytick_color,
@@ -476,11 +472,13 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
476472
# Then obtain the ticks where we have to plot the effect sizes.
477473
ticks_to_plot = [t for t in range(0, len(all_plot_groups))
478474
if t not in ticks_to_skip]
475+
476+
ticks_to_plot_violin = ticks_to_plot.copy()
477+
ticks_to_skip_violin = ticks_to_skip.copy()
478+
if show_delta2:
479+
ticks_to_plot_violin.append(max(ticks_to_plot)+2)
480+
ticks_to_skip_violin.append(max(ticks_to_skip)+2)
479481

480-
# plot one more halfviolin graph if the plot is for delta-delta
481-
if delta2:
482-
ticks_to_plot_delta = [0]
483-
ticks_to_skip_delta = [1]
484482

485483
# Plot the bootstraps, then the effect sizes and CIs.
486484
es_marker_size = plot_kwargs["es_marker_size"]
@@ -490,7 +488,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
490488
results = EffectSizeDataFrame.results
491489
contrast_xtick_labels = []
492490

493-
for j, tick in enumerate(ticks_to_plot):
491+
for j, tick in enumerate(ticks_to_plot_violin):
494492
current_group = results.test[j]
495493
current_control = results.control[j]
496494
current_bootstrap = results.bootstraps[j]
@@ -528,73 +526,36 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
528526
contrast_xtick_labels.append("{}\nminus\n{}".format(current_group,
529527
current_control))
530528

531-
# plot delta-delta violin
532-
if delta2:
533-
current_group = results.test[2]
534-
current_control = results.control[2]
535-
current_bootstrap = results.bootstraps[2]
536-
current_effsize = results.difference[2]
537-
current_ci_low = results.bca_low[2]
538-
current_ci_high = results.bca_high[2]
539-
540-
# Create the violinplot.
541-
# New in v0.2.6: drop negative infinities before plotting.
542-
v = delta_axes.violinplot(current_bootstrap[~np.isinf(current_bootstrap)],
543-
positions=[0.5],
544-
**violinplot_kwargs)
545-
# Turn the violinplot into half, and color it the same as the swarmplot.
546-
# Do this only if the color column is not specified.
547-
# Ideally, the alpha (transparency) fo the violin plot should be
548-
# less than one so the effect size and CIs are visible.
549-
if bootstraps_color_by_group is True:
550-
fc = plot_palette_contrast[current_group]
551-
else:
552-
fc = "grey"
553-
554-
halfviolin(v, fill_color=fc, alpha=halfviolin_alpha)
555-
556-
# Plot the effect size.
557-
delta_axes.plot([0.5], current_effsize, marker='o',
558-
color=ytick_color,
559-
markersize=es_marker_size)
560-
# Plot the confidence interval.
561-
delta_axes.plot([0.5, 0.5],
562-
[current_ci_low, current_ci_high],
563-
linestyle="-",
564-
color=ytick_color,
565-
linewidth=group_summary_kwargs['lw'])
566-
567-
delta_xtick_labels = "{}\nminus\n{}".format(current_group,
568-
current_control)
569-
570529

571530
# Make sure the contrast_axes x-lims match the rawdata_axes xlims,
572531
# and add an extra violinplot tick for delta-delta plot.
573-
contrast_axes.set_xticks(rawdata_axes.get_xticks())
532+
if not show_delta2:
533+
contrast_axes.set_xticks(rawdata_axes.get_xticks())
534+
else:
535+
temp = rawdata_axes.get_xticks()
536+
temp = np.append(temp, [max(temp)+1, max(temp)+2])
537+
contrast_axes.set_xticks(temp)
574538

575-
if delta2:
576-
delta_axes.set_xticks([0,0.5])
577539

578540
if show_pairs is True:
579541
max_x = contrast_axes.get_xlim()[1]
580542
rawdata_axes.set_xlim(-0.375, max_x)
581543

582544
if float_contrast is True:
583545
contrast_axes.set_xlim(0.5, 1.5)
584-
elif delta2:
585-
# Increase the xlim of raw data by 2; set xlim and label xlim
586-
# of the delta-delta plot.
587-
contrast_axes.set_xlim(rawdata_axes.get_xlim())
546+
elif show_delta2:
547+
# Increase the xlim of raw data by 2
588548
temp = rawdata_axes.get_xlim()
589-
rawdata_axes.set_xlim(temp[0], temp[1]+2)
590-
delta_axes.set_xlim(-0.5, 1)
591-
delta_axes.set_xticklabels(["", delta_xtick_labels])
549+
if show_pairs:
550+
rawdata_axes.set_xlim(temp[0], temp[1]+0.25)
551+
else:
552+
rawdata_axes.set_xlim(temp[0], temp[1]+2)
553+
contrast_axes.set_xlim(rawdata_axes.get_xlim())
592554
else:
593555
contrast_axes.set_xlim(rawdata_axes.get_xlim())
594556

595-
596557
# Properly label the contrast ticks.
597-
for t in ticks_to_skip:
558+
for t in ticks_to_skip_violin:
598559
contrast_xtick_labels.insert(t, "")
599560
contrast_axes.set_xticklabels(contrast_xtick_labels)
600561

@@ -778,18 +739,6 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
778739
contrast_ylim_high, contrast_ylim_low = contrast_axes_ylim
779740
if contrast_ylim_low < 0 < contrast_ylim_high:
780741
contrast_axes.axhline(y=0, **reflines_kwargs)
781-
if delta2:
782-
delta_axes.axhline(y=0, xmin = 0.25, xmax = 1.5, **reflines_kwargs)
783-
784-
if delta2:
785-
if plot_kwargs['contrast_ylim'] is None:
786-
ylim_contrast = contrast_axes.get_ylim()
787-
ylim_delta = delta_axes.get_ylim()
788-
ylim=(min(ylim_contrast[0], ylim_delta[0]), max(ylim_contrast[1], ylim_delta[1]))
789-
contrast_axes.set_ylim(ylim)
790-
delta_axes.set_ylim(ylim)
791-
else:
792-
delta_axes.set_ylim(contrast_axes.get_ylim())
793742

794743
if is_paired == "baseline" and show_pairs == True:
795744
rightend_ticks_raw = np.array([len(i)-1 for i in temp_idx]) + np.array(ticks_to_skip)
@@ -841,17 +790,13 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
841790

842791
ax.set_ylim(ylim)
843792
del redraw_axes_kwargs['y']
844-
845-
if delta2:
846-
sns.despine(ax=delta_axes, bottom=True)
847793

848-
xlim = delta_axes.get_xlim()
849-
ylim = delta_axes.get_ylim()
794+
if show_delta2 is True:
795+
ylim = contrast_axes.get_ylim()
850796
redraw_axes_kwargs['y'] = ylim[0]
851-
852-
delta_axes.hlines(xmin=0, xmax=0.5, **redraw_axes_kwargs)
853-
854-
delta_axes.set_ylim(ylim)
797+
x_ticks = contrast_axes.get_xticks()
798+
contrast_axes.hlines(xmin=x_ticks[-2], xmax=x_ticks[-1],
799+
**redraw_axes_kwargs)
855800
del redraw_axes_kwargs['y']
856801

857802
# Set raw axes y-label.
@@ -882,15 +827,6 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
882827
if float_contrast is True:
883828
contrast_axes.yaxis.set_label_position("right")
884829

885-
if delta2:
886-
if plot_kwargs['delta_label'] is None:
887-
delta_label = "delta - delta"
888-
else:
889-
delta_label = plot_kwargs['delta_label']
890-
delta_axes.yaxis.tick_right()
891-
delta_axes.set_ylabel(delta_label)
892-
delta_axes.yaxis.set_label_position("right")
893-
894830
# Set the rawdata axes labels appropriately
895831
rawdata_axes.set_ylabel(swarm_label)
896832
rawdata_axes.set_xlabel("")
@@ -916,9 +852,18 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
916852
og_ylim_contrast[0], og_ylim_contrast[1],
917853
**redraw_axes_kwargs)
918854

919-
if delta2:
920-
og_xlim_delta = delta_axes.get_xlim()
921-
og_ylim_delta = delta_axes.get_ylim()
855+
856+
if show_delta2 is True:
857+
if plot_kwargs['delta_label'] is None:
858+
delta_label = "delta - delta"
859+
else:
860+
delta_label = plot_kwargs['delta_label']
861+
delta_axes = contrast_axes.twinx()
862+
delta_axes.set_frame_on(False)
863+
delta_axes.set_ylabel(delta_label)
864+
og_xlim_delta = contrast_axes.get_xlim()
865+
og_ylim_delta = contrast_axes.get_ylim()
866+
delta_axes.set_ylim(og_ylim_delta)
922867
delta_axes.vlines(og_xlim_delta[1],
923868
og_ylim_delta[0], og_ylim_delta[1],
924869
**redraw_axes_kwargs)
@@ -927,8 +872,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
927872
rawdata_axes.xaxis.set_ticks_position('bottom')
928873
rawdata_axes.yaxis.set_ticks_position('left')
929874
contrast_axes.xaxis.set_ticks_position('bottom')
930-
if delta2:
931-
delta_axes.xaxis.set_ticks_position('bottom')
875+
932876
if float_contrast is False:
933877
contrast_axes.yaxis.set_ticks_position('left')
934878

-20.8 KB
Loading
-1.78 KB
Loading
-1.78 KB
Loading
-1.87 KB
Loading
-1.83 KB
Loading
-1 KB
Loading
-4.52 KB
Loading

0 commit comments

Comments
 (0)