Skip to content

Commit 65660da

Browse files
authored
Merge pull request #154 from ACCLAB/refactoring_phase2
Refactoring phase2
2 parents fb8c26d + ae4f939 commit 65660da

143 files changed

Lines changed: 1820 additions & 1542 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test-pytest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
run: |
1616
python -m pip install --upgrade pip
1717
pip install -e '.[dev]'
18-
pytest nbs/tests/ --mpl --mpl-baseline-path=nbs/tests/baseline_images
18+
pytest nbs/tests/ --mpl --mpl-baseline-path=nbs/tests/mpl_image_tests/baseline_images

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/_dabest_object.py

Lines changed: 124 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -54,120 +54,16 @@ def __init__(
5454
self.__proportional = proportional
5555
self.__mini_meta = mini_meta
5656

57-
# Check if it is a valid mini_meta case
58-
if self.__mini_meta:
59-
# Only mini_meta calculation but not proportional and delta-delta function
60-
if self.__proportional:
61-
err0 = "`proportional` and `mini_meta` cannot be True at the same time."
62-
raise ValueError(err0)
63-
if self.__delta2:
64-
err0 = "`delta` and `mini_meta` cannot be True at the same time."
65-
raise ValueError(err0)
66-
67-
# Check if the columns stated are valid
68-
# TODO instead of traversing twice idx you can traverse only once
69-
# and break the loop if the condition is not satisfied?
70-
# TODO What if the type is not str and not tuple,list? missing raise Error
71-
if all([isinstance(i, str) for i in idx]):
72-
if len(pd.unique([t for t in idx]).tolist()) != 2:
73-
err0 = "`mini_meta` is True, but `idx` ({})".format(idx)
74-
err1 = "does not contain exactly 2 columns."
75-
raise ValueError(err0 + err1)
76-
77-
if all([isinstance(i, (tuple, list)) for i in idx]):
78-
all_idx_lengths = [len(t) for t in idx]
79-
if (array(all_idx_lengths) != 2).any():
80-
err1 = "`mini_meta` is True, but some idx "
81-
err2 = "in {} does not consist only of two groups.".format(idx)
82-
raise ValueError(err1 + err2)
83-
84-
# TODO can you have True mini_meta and delta2 at the same time?
85-
# Check if this is a 2x2 ANOVA case and x & y are valid columns
86-
# Create experiment_label and x1_level
87-
if self.__delta2:
88-
# TODO Wrap the errors in a separate function called check_errors()
89-
if self.__proportional:
90-
err0 = "`proportional` and `delta` cannot be True at the same time."
91-
raise ValueError(err0)
92-
93-
# idx should not be specified
94-
if idx:
95-
err0 = "`idx` should not be specified when `delta2` is True.".format(
96-
len(x)
97-
)
98-
raise ValueError(err0)
99-
100-
# Check if x is valid
101-
# TODO if x is None is fine??
102-
if len(x) != 2:
103-
err0 = "`delta2` is True but the number of variables indicated by `x` is {}.".format(
104-
len(x)
105-
)
106-
raise ValueError(err0)
107-
108-
for i in x:
109-
if i not in self.__output_data.columns:
110-
err = "{0} is not a column in `data`. Please check.".format(i)
111-
raise IndexError(err)
112-
113-
# Check if y is valid
114-
if not y:
115-
err0 = "`delta2` is True but `y` is not indicated."
116-
raise ValueError(err0)
117-
118-
if y not in self.__output_data.columns:
119-
err = "{0} is not a column in `data`. Please check.".format(y)
120-
raise IndexError(err)
121-
122-
# Check if experiment is valid
123-
if experiment not in self.__output_data.columns:
124-
err = "{0} is not a column in `data`. Please check.".format(experiment)
125-
raise IndexError(err)
126-
127-
# Check if experiment_label is valid and create experiment when needed
128-
if experiment_label:
129-
if len(experiment_label) != 2:
130-
err0 = "`experiment_label` does not have a length of 2."
131-
raise ValueError(err0)
132-
133-
for i in experiment_label:
134-
if i not in self.__output_data[experiment].unique():
135-
err = "{0} is not an element in the column `{1}` of `data`. Please check.".format(
136-
i, experiment
137-
)
138-
raise IndexError(err)
139-
else:
140-
experiment_label = self.__output_data[experiment].unique()
141-
142-
# Check if x1_level is valid
143-
if x1_level:
144-
if len(x1_level) != 2:
145-
err0 = "`x1_level` does not have a length of 2."
146-
raise ValueError(err0)
147-
148-
for i in x1_level:
149-
if i not in self.__output_data[x[0]].unique():
150-
err = "{0} is not an element in the column `{1}` of `data`. Please check.".format(
151-
i, experiment
152-
)
153-
raise IndexError(err)
154-
155-
else:
156-
x1_level = self.__output_data[x[0]].unique()
157-
158-
# TODO what if experiment is None?
159-
elif experiment:
160-
experiment_label = self.__output_data[experiment].unique()
161-
x1_level = self.__output_data[x[0]].unique()
162-
self.__experiment_label = experiment_label
163-
self.__x1_level = x1_level
57+
# after this call the attributes self.__experiment_label and self.__x1_level are updated
58+
self._check_errors(x, y, idx, experiment, experiment_label, x1_level)
59+
16460

16561
# create new x & idx and record the second variable if this is a valid 2x2 ANOVA case
16662
if idx is None and x is not None and y is not None:
16763
# Add a length check for unique values in the first element in list x,
16864
# if the length is greater than 2, force delta2 to be False
16965
# Should be removed if delta2 for situations other than 2x2 is supported
170-
if len(self.__output_data[x[0]].unique()) > 2 and x1_level is None:
66+
if len(self.__output_data[x[0]].unique()) > 2 and self.__x1_level is None:
17167
self.__delta2 = False
17268
# stop the loop if delta2 is False
17369

@@ -184,9 +80,9 @@ def __init__(
18480

18581
# create idx and record the first and second x variable
18682
idx = []
187-
for i in list(map(lambda x: str(x), experiment_label)):
83+
for i in list(map(lambda x: str(x), self.__experiment_label)):
18884
temp = []
189-
for j in list(map(lambda x: str(x), x1_level)):
85+
for j in list(map(lambda x: str(x), self.__x1_level)):
19086
temp.append(j + " " + i)
19187
idx.append(temp)
19288

@@ -242,7 +138,7 @@ def __init__(
242138
err = "You have only specified `x`. Please also specify `y`."
243139
raise ValueError(err)
244140

245-
self.__plot_data = self.get_plot_data(x, y, all_plot_groups)
141+
self.__plot_data = self._get_plot_data(x, y, all_plot_groups)
246142
self.__all_plot_groups = all_plot_groups
247143

248144
# Check if `id_col` is valid
@@ -529,7 +425,123 @@ def _all_plot_groups(self):
529425
"""
530426
return self.__all_plot_groups
531427

532-
def get_plot_data(self, x, y, all_plot_groups):
428+
def _check_errors(self, x, y, idx, experiment, experiment_label, x1_level):
429+
'''
430+
Function to check some input parameters and combinations between them.
431+
At the end of this function these two class attributes are updated
432+
self.__experiment_label and self.__x1_level
433+
'''
434+
# Check if it is a valid mini_meta case
435+
if self.__mini_meta:
436+
# Only mini_meta calculation but not proportional and delta-delta function
437+
if self.__proportional:
438+
err0 = "`proportional` and `mini_meta` cannot be True at the same time."
439+
raise ValueError(err0)
440+
if self.__delta2:
441+
err0 = "`delta2` and `mini_meta` cannot be True at the same time."
442+
raise ValueError(err0)
443+
444+
# Check if the columns stated are valid
445+
# TODO instead of traversing twice idx you can traverse only once
446+
# and break the loop if the condition is not satisfied?
447+
# TODO What if the type is not str and not tuple,list? missing raise Error
448+
if all([isinstance(i, str) for i in idx]):
449+
if len(pd.unique([t for t in idx]).tolist()) != 2:
450+
err0 = "`mini_meta` is True, but `idx` ({})".format(idx)
451+
err1 = "does not contain exactly 2 columns."
452+
raise ValueError(err0 + err1)
453+
454+
if all([isinstance(i, (tuple, list)) for i in idx]):
455+
all_idx_lengths = [len(t) for t in idx]
456+
if (array(all_idx_lengths) != 2).any():
457+
err1 = "`mini_meta` is True, but some idx "
458+
err2 = "in {} does not consist only of two groups.".format(idx)
459+
raise ValueError(err1 + err2)
460+
461+
# TODO can you have True mini_meta and delta2 at the same time?
462+
# Check if this is a 2x2 ANOVA case and x & y are valid columns
463+
# Create experiment_label and x1_level
464+
if self.__delta2:
465+
if x is None:
466+
error_msg = "If `delta2` is True. `x` parameter cannot be None. String or list expected"
467+
raise ValueError(error_msg)
468+
469+
if self.__proportional:
470+
err0 = "`proportional` and `delta2` cannot be True at the same time."
471+
raise ValueError(err0)
472+
473+
# idx should not be specified
474+
if idx:
475+
err0 = "`idx` should not be specified when `delta2` is True.".format(
476+
len(x)
477+
)
478+
raise ValueError(err0)
479+
480+
# Check if x is valid
481+
if len(x) != 2:
482+
err0 = "`delta2` is True but the number of variables indicated by `x` is {}.".format(
483+
len(x)
484+
)
485+
raise ValueError(err0)
486+
487+
for i in x:
488+
if i not in self.__output_data.columns:
489+
err = "{0} is not a column in `data`. Please check.".format(i)
490+
raise IndexError(err)
491+
492+
# Check if y is valid
493+
if not y:
494+
err0 = "`delta2` is True but `y` is not indicated."
495+
raise ValueError(err0)
496+
497+
if y not in self.__output_data.columns:
498+
err = "{0} is not a column in `data`. Please check.".format(y)
499+
raise IndexError(err)
500+
501+
# Check if experiment is valid
502+
if experiment not in self.__output_data.columns:
503+
err = "{0} is not a column in `data`. Please check.".format(experiment)
504+
raise IndexError(err)
505+
506+
# Check if experiment_label is valid and create experiment when needed
507+
if experiment_label:
508+
if len(experiment_label) != 2:
509+
err0 = "`experiment_label` does not have a length of 2."
510+
raise ValueError(err0)
511+
512+
for i in experiment_label:
513+
if i not in self.__output_data[experiment].unique():
514+
err = "{0} is not an element in the column `{1}` of `data`. Please check.".format(
515+
i, experiment
516+
)
517+
raise IndexError(err)
518+
else:
519+
experiment_label = self.__output_data[experiment].unique()
520+
521+
# Check if x1_level is valid
522+
if x1_level:
523+
if len(x1_level) != 2:
524+
err0 = "`x1_level` does not have a length of 2."
525+
raise ValueError(err0)
526+
527+
for i in x1_level:
528+
if i not in self.__output_data[x[0]].unique():
529+
err = "{0} is not an element in the column `{1}` of `data`. Please check.".format(
530+
i, experiment
531+
)
532+
raise IndexError(err)
533+
534+
else:
535+
x1_level = self.__output_data[x[0]].unique()
536+
537+
# TODO what if experiment is None?
538+
elif experiment:
539+
experiment_label = self.__output_data[experiment].unique()
540+
x1_level = self.__output_data[x[0]].unique()
541+
self.__experiment_label = experiment_label
542+
self.__x1_level = x1_level
543+
544+
def _get_plot_data(self, x, y, all_plot_groups):
533545
"""
534546
Function to prepare some attributes for plotting
535547
"""

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: 4 additions & 2 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:
@@ -1139,7 +1141,7 @@ def plot(self, color_col=None,
11391141
11401142
"""
11411143

1142-
from .plotter import EffectSizeDataFramePlotter
1144+
from .plotter import effectsize_df_plotter
11431145

11441146
if hasattr(self, "results") is False:
11451147
self.__pre_calc()
@@ -1156,7 +1158,7 @@ def plot(self, color_col=None,
11561158
all_kwargs = locals()
11571159
del all_kwargs["self"]
11581160

1159-
out = EffectSizeDataFramePlotter(self, **all_kwargs)
1161+
out = effectsize_df_plotter(self, **all_kwargs)
11601162

11611163
return out
11621164

dabest/_modidx.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,4 @@
7878
'dabest.plot_tools.sankeydiag': ('API/plot_tools.html#sankeydiag', 'dabest/plot_tools.py'),
7979
'dabest.plot_tools.single_sankey': ('API/plot_tools.html#single_sankey', 'dabest/plot_tools.py'),
8080
'dabest.plot_tools.width_determine': ('API/plot_tools.html#width_determine', 'dabest/plot_tools.py')},
81-
'dabest.plotter': { 'dabest.plotter.EffectSizeDataFramePlotter': ( 'API/plotter.html#effectsizedataframeplotter',
82-
'dabest/plotter.py')}}}
81+
'dabest.plotter': {'dabest.plotter.effectsize_df_plotter': ('API/plotter.html#effectsize_df_plotter', 'dabest/plotter.py')}}}

0 commit comments

Comments
 (0)