Skip to content

Commit ee20cda

Browse files
committed
Small refactorings in API and better errors coverage
1 parent a56f285 commit ee20cda

18 files changed

Lines changed: 271 additions & 290 deletions

dabest/_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ def prop_dataset(
128128
elif not set(group_names) == set(group.keys()):
129129
# Check if the group_names provided is the same as the keys of the dict
130130
raise ValueError("group_names must be the same as the keys of the dict.")
131+
131132
# Check if the values in the dict are numeric
132133
if not all(
133134
[isinstance(group[name], (list, tuple, np.ndarray)) for name in group_names]
134135
):
135136
raise ValueError(
136137
"group must be a dict of lists, tuples, or numpy ndarrays of numeric types."
137138
)
139+
138140
# Check if the values in the dict only have two elements under each parent key
139141
if not all([len(group[name]) == 2 for name in group_names]):
140142
raise ValueError("Each parent key should have only two elements.")
@@ -143,6 +145,7 @@ def prop_dataset(
143145
else:
144146
if group_names is None:
145147
raise ValueError("group_names must be provided if group is not a dict.")
148+
146149
# Check if the length of group is two times of the length of group_names
147150
if not len(group) == 2 * len(group_names):
148151
raise ValueError(

dabest/_bootstrap_tools.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ def __init__(
8888
# check x2 is not None:
8989
if x2 is None:
9090
raise ValueError("Please specify x2.")
91-
else:
92-
x2 = pd.Series(x2).dropna()
93-
if len(x1) != len(x2):
94-
raise ValueError("x1 and x2 are not the same length.")
91+
92+
x2 = pd.Series(x2).dropna()
93+
if len(x1) != len(x2):
94+
raise ValueError("x1 and x2 are not the same length.")
9595

9696
if (x2 is None) or (paired is not None):
9797
if x2 is None:
@@ -102,7 +102,6 @@ def __init__(
102102
ttest_2_paired = "NIL"
103103
wilcoxonresult = "NIL"
104104

105-
# elif paired is not None:
106105
else: # only two options to enter here
107106
diff = True
108107
tx = x2 - x1
@@ -234,18 +233,18 @@ def jackknife_indexes(data):
234233
return (np.delete(base, i) for i in base)
235234

236235

237-
def bca(data, alphas, statarray, stat_function, ostat, reps):
236+
def bca(data, alphas, stat_array, stat_function, ostat, reps):
238237
"""
239238
Subroutine called to calculate the BCa statistics.
240239
Borrowed heavily from scikits.bootstrap code.
241240
"""
242241

243242
# The bias correction value.
244-
z0 = norm.ppf((1.0 * np.sum(statarray < ostat, axis=0)) / reps)
243+
z0 = norm.ppf((1.0 * np.sum(stat_array < ostat, axis=0)) / reps)
245244

246245
# Statistics of the jackknife distribution
247-
jackindexes = jackknife_indexes(data[0])
248-
jstat = [stat_function(*(x[indexes] for x in data)) for indexes in jackindexes]
246+
jack_indexes = jackknife_indexes(data[0])
247+
jstat = [stat_function(*(x[indexes] for x in data)) for indexes in jack_indexes]
249248
jmean = np.mean(jstat, axis=0)
250249

251250
# Acceleration value

dabest/_delta_objects.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ def __init__(self, effectsizedataframe, permutation_count,
409409
# weights
410410
self.__bootstraps_weighted_delta = ci2g.calculate_weighted_delta(
411411
self.__group_var,
412-
self.__bootstraps,
413-
self.__resamples)
412+
self.__bootstraps)
414413

415414
# Compute the weighted average mean difference based on the raw data
416415
self.__difference = es.weighted_delta(self.__effsizedf["difference"],

dabest/_effsize_objects.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,12 @@ def permutations(self):
663663

664664
@property
665665
def permutations_var(self):
666+
# TODO Missing docstring
666667
return self.__PermutationTest_result.permutations_var
667668

668669
@property
669670
def proportional_difference(self):
671+
# TODO Missing docstring
670672
try:
671673
return self.__proportional_difference
672674
except AttributeError:

dabest/_stats_tools/confint_1group.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def compute_1group_jackknife(x, func, *args, **kwargs):
3838

3939

4040
def compute_1group_acceleration(jack_dist):
41+
# TODO is it needed a function to just call one line?
4142
from . import confint_2group_diff as ci_2g
4243

4344
return ci_2g._calc_accel(jack_dist)

dabest/_stats_tools/confint_2group_diff.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def compute_meandiff_jackknife(x0, x1, is_paired, effect_size):
9898

9999

100100
def _calc_accel(jack_dist):
101+
# TODO Missing docstring
101102
jack_mean = npmean(jack_dist)
102103

103104
numer = npsum((jack_mean - jack_dist) ** 3)
@@ -283,17 +284,17 @@ def compute_interval_limits(bias, acceleration, n_boots, ci=95):
283284
if isnan(low) or isnan(high):
284285
return low, high
285286

286-
else:
287-
low = int(norm.cdf(low) * n_boots)
288-
high = int(norm.cdf(high) * n_boots)
289-
return low, high
287+
288+
low = int(norm.cdf(low) * n_boots)
289+
high = int(norm.cdf(high) * n_boots)
290+
return low, high
290291

291292

292293
def calculate_group_var(control_var, control_N, test_var, test_N):
293294
return control_var / control_N + test_var / test_N
294295

295296

296-
def calculate_weighted_delta(group_var, differences, resamples):
297+
def calculate_weighted_delta(group_var, differences):
297298
"""
298299
Compute the weighted deltas.
299300
"""

dabest/_stats_tools/effsize.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def two_group_difference(control:list|tuple|np.ndarray, #Accepts lists, tuples,
6464
if effect_size == "mean_diff":
6565
return func_difference(control, test, np.mean, is_paired)
6666

67-
elif effect_size == "median_diff":
67+
if effect_size == "median_diff":
6868
mes1 = "Using median as the statistic in bootstrapping may " + \
6969
"result in a biased estimate and cause problems with " + \
7070
"BCa confidence intervals. Consider using a different statistic, such as the mean.\n"
@@ -74,21 +74,21 @@ def two_group_difference(control:list|tuple|np.ndarray, #Accepts lists, tuples,
7474
warnings.warn(message=mes1+mes2, category=UserWarning)
7575
return func_difference(control, test, np.median, is_paired)
7676

77-
elif effect_size == "cohens_d":
77+
if effect_size == "cohens_d":
7878
return cohens_d(control, test, is_paired)
7979

80-
elif effect_size == "cohens_h":
80+
if effect_size == "cohens_h":
8181
return cohens_h(control, test)
8282

83-
elif effect_size == "hedges_g" or effect_size == "delta_g":
83+
if effect_size == "hedges_g" or effect_size == "delta_g":
8484
return hedges_g(control, test, is_paired)
8585

86-
elif effect_size == "cliffs_delta":
86+
if effect_size == "cliffs_delta":
8787
if is_paired:
8888
err1 = "`is_paired` is not None; therefore Cliff's delta is not defined."
8989
raise ValueError(err1)
90-
else:
91-
return cliffs_delta(control, test)
90+
91+
return cliffs_delta(control, test)
9292

9393

9494
# %% ../../nbs/API/effsize.ipynb 6
@@ -129,10 +129,10 @@ def func_difference(control:list|tuple|np.ndarray, # NaNs are automatically disc
129129

130130
return func(test - control)
131131

132-
else:
133-
control = control[~np.isnan(control)]
134-
test = test[~np.isnan(test)]
135-
return func(test) - func(control)
132+
133+
control = control[~np.isnan(control)]
134+
test = test[~np.isnan(test)]
135+
return func(test) - func(control)
136136

137137

138138
# %% ../../nbs/API/effsize.ipynb 7
@@ -209,7 +209,8 @@ def cohens_d(control:list|tuple|np.ndarray,
209209
else:
210210
M = np.mean(test) - np.mean(control)
211211
divisor = pooled_sd
212-
212+
213+
# TODO what if divisor = 0?
213214
return M / divisor
214215

215216
# %% ../../nbs/API/effsize.ipynb 8
@@ -289,7 +290,6 @@ def cliffs_delta(control:list|tuple|np.ndarray,
289290
See [here](https://en.wikipedia.org/wiki/Effect_size#Effect_size_for_ordinal_data)
290291
"""
291292

292-
293293
# Convert to numpy arrays for speed.
294294
# NaNs are automatically dropped.
295295
if ~isinstance(control, np.ndarray):
@@ -307,7 +307,6 @@ def cliffs_delta(control:list|tuple|np.ndarray,
307307
U, _ = mannwhitneyu(t, c, alternative='two-sided')
308308
cliffs_delta = ((2 * U) / (control_n * test_n)) - 1
309309

310-
311310
return cliffs_delta
312311

313312

@@ -332,7 +331,7 @@ def _compute_standardizers(control, test):
332331
# For paired standardized mean difference.
333332
average = np.sqrt((control_var + test_var) / 2)
334333

335-
return pooled, average # indent if you implement above code chunk.
334+
return pooled, average
336335

337336
# %% ../../nbs/API/effsize.ipynb 12
338337
def _compute_hedges_correction_factor(n1,

dabest/misc_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def unpack_and_add(l, c):
3333

3434

3535
def print_greeting():
36+
# TODO missing docstring
3637
from .__init__ import __version__
3738

3839
line1 = "DABEST v{}".format(__version__)

0 commit comments

Comments
 (0)