Skip to content

Commit 7617dfe

Browse files
committed
Update the unit tests and fix some bugs
1 parent 357ea26 commit 7617dfe

4 files changed

Lines changed: 53 additions & 30 deletions

File tree

dabest/_classes.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def __repr__(self):
368368
comparisons.append("{} minus {}".format(test_name, control_name))
369369

370370
if self.__delta2:
371-
comparison.append("{} minus {}".format(self.__experiment_label[1], self.__experiment_label[0]))
371+
comparisons.append("{} minus {} (only for mean difference)".format(self.__experiment_label[1], self.__experiment_label[0]))
372372

373373
for j, g in enumerate(comparisons):
374374
out.append("{}. {}".format(j+1, g))
@@ -782,6 +782,9 @@ def __init__(self, control, test, effect_size,
782782
`random_seed` is used to seed the random number generator during
783783
bootstrap resampling. This ensures that the confidence intervals
784784
reported are replicable.
785+
delta2 : boolean, default False
786+
Indicate if the control and test data are boostrap deltas that can be
787+
used to calculate delta-delta.
785788
786789
787790
Returns
@@ -1145,7 +1148,7 @@ def __repr__(self, show_resample_count=True, define_pval=True, sigfig=3):
11451148
"paired_status": PAIRED_STATUS[str(self.__is_paired)]}
11461149

11471150
if self.__delta2:
1148-
out1 = "The delta-delta {es}"
1151+
out1 = "The delta-delta "
11491152
else:
11501153
out1 = "The {paired_status} {es} {rm_status}".format(**first_line)
11511154

@@ -1594,8 +1597,12 @@ def __pre_calc(self):
15941597
r_dict["test_N"] = int(len(test))
15951598
out.append(r_dict)
15961599
if j == len(idx)-1 and ix == len(current_tuple)-2:
1597-
resamp_count = True
1598-
def_pval = True
1600+
if not self.__delta2:
1601+
resamp_count = True
1602+
def_pval = True
1603+
else:
1604+
resamp_count = False
1605+
def_pval = False
15991606
else:
16001607
resamp_count = False
16011608
def_pval = False
@@ -1612,25 +1619,30 @@ def __pre_calc(self):
16121619
delta = TwoGroupsEffectSize(out[0]["bootstraps"],
16131620
out[1]["bootstraps"],
16141621
self.__effect_size,
1615-
True,
1622+
"baseline",
16161623
self.__ci,
16171624
self.__resamples,
16181625
self.__permutation_count,
16191626
self.__random_seed,
16201627
self.__delta2
16211628
)
1629+
16221630
r_dict = delta.to_dict()
16231631
r_dict["control"] = self.__experiment_label[0]
16241632
r_dict["test"] = self.__experiment_label[1]
16251633
r_dict["control_N"] = self.__resamples
16261634
r_dict["test_N"] = self.__resamples
16271635
out.append(r_dict)
1636+
resamp_count = True
1637+
def_pval = True
1638+
text_repr = delta.__repr__(show_resample_count=resamp_count,
1639+
define_pval=def_pval)
16281640
to_replace = "between {} and {} is".format(self.__experiment_label[0], self.__experiment_label[1])
16291641
text_repr = text_repr.replace("is", to_replace, 1)
16301642
reprs.append(text_repr)
16311643
else:
1632-
err0 = 'The calculation of delta-delta is not supported for {}.'.format(self.__effect_size)
1633-
raise ValueError(err0)
1644+
self.__delta2 = False
1645+
16341646
varname = get_varname(self.__dabest_obj)
16351647
lastline = "To get the results of all valid statistical tests, " +\
16361648
"use `{}.{}.statistical_tests`".format(varname, self.__effect_size)

dabest/plotter.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,19 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
5959
ytick_color = plt.rcParams["ytick.color"]
6060
axes_facecolor = plt.rcParams['axes.facecolor']
6161

62-
dabest_obj = EffectSizeDataFrame.dabest_obj
63-
plot_data = EffectSizeDataFrame._plot_data
64-
xvar = EffectSizeDataFrame.xvar
65-
yvar = EffectSizeDataFrame.yvar
66-
is_paired = EffectSizeDataFrame.is_paired
67-
delta2 = EffectSizeDataFrame.delta2
62+
dabest_obj = EffectSizeDataFrame.dabest_obj
63+
plot_data = EffectSizeDataFrame._plot_data
64+
xvar = EffectSizeDataFrame.xvar
65+
yvar = EffectSizeDataFrame.yvar
66+
is_paired = EffectSizeDataFrame.is_paired
67+
delta2 = EffectSizeDataFrame.delta2
68+
effect_size = EffectSizeDataFrame.effect_size
6869

6970
all_plot_groups = dabest_obj._all_plot_groups
7071
idx = dabest_obj.idx
7172

73+
if effect_size != "mean_diff":
74+
delta2 = False
7275

7376

7477

@@ -93,6 +96,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
9396
show_pairs = plot_kwargs["show_pairs"]
9497

9598

99+
96100
# Set default kwargs first, then merge with user-dictated ones.
97101
default_swarmplot_kwargs = {'size': plot_kwargs["raw_marker_size"]}
98102
if plot_kwargs["swarmplot_kwargs"] is None:
@@ -102,6 +106,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
102106
plot_kwargs["swarmplot_kwargs"])
103107

104108

109+
105110
# Violinplot kwargs.
106111
default_violinplot_kwargs = {'widths':0.5, 'vert':True,
107112
'showextrema':False, 'showmedians':False}
@@ -112,6 +117,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
112117
plot_kwargs["violinplot_kwargs"])
113118

114119

120+
115121
# slopegraph kwargs.
116122
default_slopegraph_kwargs = {'lw':1, 'alpha':0.5}
117123
if plot_kwargs["slopegraph_kwargs"] is None:
@@ -121,6 +127,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
121127
plot_kwargs["slopegraph_kwargs"])
122128

123129

130+
124131
# Zero reference-line kwargs.
125132
default_reflines_kwargs = {'linestyle':'solid', 'linewidth':0.75,
126133
'zorder': 2,
@@ -218,6 +225,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
218225
plot_palette_contrast = dict(zip(names, contrast_colors))
219226

220227

228+
221229
# Infer the figsize.
222230
fig_size = plot_kwargs["fig_size"]
223231
if fig_size is None:
@@ -322,6 +330,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
322330
contrast_axes.set_frame_on(False)
323331
if delta2:
324332
delta_axes.set_frame_on(False)
333+
325334
redraw_axes_kwargs = {'colors' : ytick_color,
326335
'facecolors' : ytick_color,
327336
'lw' : 1,
@@ -460,8 +469,6 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
460469
ticks_to_plot = np.arange(1, len(temp_all_plot_groups), 2).tolist()
461470
ticks_to_skip_contrast = np.cumsum([(len(t)-1)*2 for t in idx])[:-1].tolist()
462471
ticks_to_skip_contrast.insert(0, 0)
463-
if delta2:
464-
ticks_to_skip_contrast.append(max(ticks_to_skip_contrast)+2)
465472
else:
466473
ticks_to_skip = np.cumsum([len(t) for t in idx])[:-1].tolist()
467474
ticks_to_skip.insert(0, 0)
@@ -575,6 +582,8 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
575582
if float_contrast is True:
576583
contrast_axes.set_xlim(0.5, 1.5)
577584
elif delta2:
585+
# Increase the xlim of raw data by 2; set xlim and label xlim
586+
# of the delta-delta plot.
578587
contrast_axes.set_xlim(rawdata_axes.get_xlim())
579588
temp = rawdata_axes.get_xlim()
580589
rawdata_axes.set_xlim(temp[0], temp[1]+2)
@@ -769,7 +778,8 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
769778
contrast_ylim_high, contrast_ylim_low = contrast_axes_ylim
770779
if contrast_ylim_low < 0 < contrast_ylim_high:
771780
contrast_axes.axhline(y=0, **reflines_kwargs)
772-
delta_axes.axhline(y=0, xmin = 0.25, xmax = 1.5, **reflines_kwargs)
781+
if delta2:
782+
delta_axes.axhline(y=0, xmin = 0.25, xmax = 1.5, **reflines_kwargs)
773783

774784

775785
if delta2 and plot_kwargs['delta_ylim'] is not None:
@@ -789,7 +799,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
789799
delta_axes.set_ylim(low, high)
790800
else:
791801
delta_axes.set_ylim(custom_delta_ylim)
792-
if delta2:
802+
if delta2 and plot_kwargs['delta_ylim'] is None and plot_kwargs['contrast_ylim'] is None:
793803
ylim_contrast = contrast_axes.get_ylim()
794804
ylim_delta = delta_axes.get_ylim()
795805
ylim=(min(ylim_contrast[0], ylim_delta[0]), max(ylim_contrast[1], ylim_delta[1]))
@@ -814,8 +824,6 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
814824
del redraw_axes_kwargs['y']
815825

816826
temp_length = [(len(i)-1)*2-1 for i in idx]
817-
if delta2:
818-
temp_length.append(1)
819827
rightend_ticks_contrast = np.array(temp_length) + np.array(ticks_to_skip_contrast)
820828
for ax in [contrast_axes]:
821829
sns.despine(ax=ax, bottom=True)

dabest/tests/test_01_effsizes_pvals.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@
5555

5656
# kwargs for Dabest class init.
5757
dabest_default_kwargs = dict(x=None, y=None, ci=95,
58-
resamples=5000, random_seed=12345)
58+
resamples=5000, random_seed=12345,
59+
proportional=False, delta2=False, experiment=None,
60+
experiment_label=None, x1_level=None
61+
)
5962

6063

6164

@@ -79,7 +82,7 @@ def test_mean_diff_paired():
7982
from numpy import mean as npmean
8083
mean_diff = effsize.func_difference(paired_wellbeing.pre,
8184
paired_wellbeing.post,
82-
npmean, is_paired=True)
85+
npmean, is_paired="baseline")
8386
assert mean_diff == pytest.approx(4.10)
8487

8588

@@ -88,7 +91,7 @@ def test_median_diff_paired():
8891
from numpy import median as npmedian
8992
median_diff = effsize.func_difference(paired_wellbeing.pre,
9093
paired_wellbeing.post,
91-
npmedian, is_paired=True)
94+
npmedian, is_paired="baseline")
9295
assert median_diff == pytest.approx(4.5)
9396

9497

@@ -112,15 +115,15 @@ def test_hedges_g_unpaired():
112115
def test_cohens_d_paired():
113116
import numpy as np
114117
cohens_d = effsize.cohens_d(paired_wellbeing.pre, paired_wellbeing.post,
115-
is_paired=True)
118+
is_paired="baseline")
116119
assert np.round(cohens_d, 2) == pytest.approx(0.34)
117120

118121

119122

120123
def test_hedges_g_paired():
121124
import numpy as np
122125
hedges_g = effsize.hedges_g(paired_wellbeing.pre, paired_wellbeing.post,
123-
is_paired=True)
126+
is_paired="baseline")
124127
assert np.round(hedges_g, 2) == pytest.approx(0.33)
125128

126129

@@ -155,7 +158,7 @@ def test_paired_stats():
155158
before = paired_wellbeing.pre
156159
after = paired_wellbeing.post
157160

158-
paired_es = TwoGroupsEffectSize(before, after, "mean_diff", is_paired=True)
161+
paired_es = TwoGroupsEffectSize(before, after, "mean_diff", is_paired="baseline")
159162

160163
p1 = sp.stats.ttest_rel(before, after, nan_policy='omit').pvalue
161164
assert paired_es.pvalue_paired_students_t == pytest.approx(p1)
@@ -197,7 +200,7 @@ def test_paired_permutation_test():
197200
perm_test = PermutationTest(paired_wellbeing.pre,
198201
paired_wellbeing.post,
199202
effect_size="mean_diff",
200-
is_paired=True)
203+
is_paired="baseline")
201204
assert perm_test.pvalue == pytest.approx(0.0124)
202205

203206

@@ -223,7 +226,7 @@ def test_lqrt_unpaired():
223226

224227
def test_lqrt_paired():
225228
paired_dabest = Dabest(paired_wellbeing, idx=("pre", "post"),
226-
paired=True, id_col="ID",
229+
paired="baseline", id_col="ID",
227230
**dabest_default_kwargs)
228231
lqrt_result = paired_dabest.mean_diff.lqrt
229232

dabest/tests/test_03_plotting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
two_groups_unpaired = load(df, idx=("Control 1", "Test 1"))
2626

2727
two_groups_paired = load(df, idx=("Control 1", "Test 1"),
28-
paired=True, id_col="ID")
28+
paired="baseline", id_col="ID")
2929

3030
multi_2group = load(df, idx=(("Control 1", "Test 1",),
3131
("Control 2", "Test 2"))
@@ -34,7 +34,7 @@
3434
multi_2group_paired = load(df,
3535
idx=(("Control 1", "Test 1"),
3636
("Control 2", "Test 2")),
37-
paired=True, id_col="ID")
37+
paired="baseline", id_col="ID")
3838

3939
shared_control = load(df, idx=("Control 1", "Test 1",
4040
"Test 2", "Test 3",
@@ -137,7 +137,7 @@ def test_11_inset_plots():
137137
iris_dabest3 = load(data=iris_melt[iris_melt.species=="setosa"],
138138
x="metric", y="value",
139139
idx=("sepal_length", "sepal_width"),
140-
paired=True, id_col="index")
140+
paired="baseline", id_col="index")
141141

142142

143143

0 commit comments

Comments
 (0)