Skip to content

Commit 023aa70

Browse files
committed
Support 2x2 plots
- add var2 variable and support 2x2 plot
1 parent b00702b commit 023aa70

3 files changed

Lines changed: 98 additions & 18 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,5 @@ tex.cache/
125125
testtt.py
126126
real.py
127127
0to2_beforeduringafter.csv
128+
test.ipynb
129+
TrhCsCh.csv

dabest/_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# Email : joseshowh@gmail.com
55

66

7-
def load(data, idx, x=None, y=None, paired=None, id_col=None,
8-
ci=95, resamples=5000, random_seed=12345, proportional=False):
7+
def load(data, idx=None, x=None, y=None, paired=None, id_col=None,
8+
ci=95, resamples=5000, random_seed=12345, proportional=False, var2 = False, status = None):
99
'''
1010
Loads data in preparation for estimation statistics.
1111
@@ -65,4 +65,4 @@ def load(data, idx, x=None, y=None, paired=None, id_col=None,
6565
'''
6666
from ._classes import Dabest
6767

68-
return Dabest(data, idx, x, y, paired, id_col, ci, resamples, random_seed, proportional)
68+
return Dabest(data, idx, x, y, paired, id_col, ci, resamples, random_seed, proportional, var2, status)

dabest/_classes.py

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Dabest(object):
1010
"""
1111

1212
def __init__(self, data, idx, x, y, paired, id_col, ci, resamples,
13-
random_seed, proportional):
13+
random_seed, proportional, var2, status):
1414

1515
"""
1616
Parses and stores pandas DataFrames in preparation for estimation
@@ -23,9 +23,10 @@ def __init__(self, data, idx, x, y, paired, id_col, ci, resamples,
2323
import pandas as pd
2424
import seaborn as sns
2525

26+
self.__var2 = var2
27+
self.__status = status
2628
self.__ci = ci
2729
self.__data = data
28-
self.__idx = idx
2930
self.__id_col = id_col
3031
self.__is_paired = paired
3132
self.__resamples = resamples
@@ -39,6 +40,59 @@ def __init__(self, data, idx, x, y, paired, id_col, ci, resamples,
3940

4041

4142

43+
# check if this is a 2x2 ANOVA case and x & y are valid columns:
44+
if var2:
45+
if len(x) != 2:
46+
err0 = '`var2` is True but the number of variables indicated by `x` is {}.'.format(len(x))
47+
raise ValueError(err0)
48+
if any(i not in data_in.columns for i in x):
49+
err = 'Not all of {0} is a column in `data`. Please check.'.format(x)
50+
raise IndexError(err)
51+
if y not in data_in.columns:
52+
err = '{0} is not a column in `data`. Please check.'.format(y)
53+
raise IndexError(err)
54+
if status not in data_in.columns:
55+
err = '{0} is not a column in `data`. Please check.'.format(status)
56+
raise IndexError(err)
57+
58+
59+
60+
# check if idx is specified
61+
if not var2 and not idx:
62+
err = '`idx` is not a column in `data`. Please check.'
63+
raise IndexError(err)
64+
65+
66+
# create new x & idx and record the second variable if this is a valid 2x2 ANOVA case
67+
if var2:
68+
# add a new column which is a combination of experiment status and the first variable
69+
new_col_name = status+x[0]
70+
while new_col_name in data_in.columns:
71+
new_col_name += "_"
72+
data_in[new_col_name] = data_in[x[0]].apply(lambda x: str(x)) + " " + data_in[status].apply(lambda x: str(x))
73+
74+
#create idx
75+
experiment = data_in[status].unique()
76+
x1_level = data_in[x[0]].unique()
77+
idx = []
78+
for i in experiment:
79+
temp = []
80+
for j in x1_level:
81+
temp.append(j + " " + i)
82+
idx.append(temp)
83+
self.__idx = idx
84+
self.__first = x1_level
85+
self.__experiment = experiment
86+
# record the second variable and create idx
87+
self.__second = x[1]
88+
x = new_col_name
89+
90+
else:
91+
self.__second = None
92+
self.__idx = idx
93+
self.__first = None
94+
self.__experiment = experiment
95+
4296
# Determine the kind of estimation plot we need to produce.
4397
if all([isinstance(i, str) for i in idx]):
4498
# flatten out idx.
@@ -200,7 +254,7 @@ def __init__(self, data, idx, x, y, paired, id_col, ci, resamples,
200254
EffectSizeDataFrame_kwargs = dict(ci=ci, is_paired=paired,
201255
random_seed=random_seed,
202256
resamples=resamples,
203-
proportional=proportional)
257+
proportional=proportional, var2=var2, second=self.__second)
204258

205259
self.__mean_diff = EffectSizeDataFrame(self, "mean_diff",
206260
**EffectSizeDataFrame_kwargs)
@@ -495,28 +549,31 @@ def cliffs_delta(self):
495549
return self.__cliffs_delta
496550

497551

498-
499552
@property
500553
def data(self):
501554
"""
502555
Returns the pandas DataFrame that was passed to `dabest.load()`.
503556
"""
504557
return self.__data
505558

559+
506560
@property
507561
def idx(self):
508562
"""
509563
Returns the order of categories that was passed to `dabest.load()`.
510564
"""
511565
return self.__idx
512566

513-
# Removed due to the deprecation fo `is_paired`
514-
#@property
515-
#def is_paired(self):
516-
# """
517-
# Returns True if the dataset was declared as paired to `dabest.load()`.
518-
# """
519-
# return self.__is_paired
567+
568+
@property
569+
def second(self):
570+
return self.__second
571+
572+
573+
@property
574+
def var2(self):
575+
return self.__var2
576+
520577

521578
@property
522579
def is_paired(self):
@@ -525,27 +582,31 @@ def is_paired(self):
525582
"""
526583
return self.__is_paired
527584

585+
528586
@property
529587
def id_col(self):
530588
"""
531589
Returns the id column declared to `dabest.load()`.
532590
"""
533591
return self.__id_col
534592

593+
535594
@property
536595
def ci(self):
537596
"""
538597
The width of the desired confidence interval.
539598
"""
540599
return self.__ci
541600

601+
542602
@property
543603
def resamples(self):
544604
"""
545605
The number of resamples used to generate the bootstrap.
546606
"""
547607
return self.__resamples
548608

609+
549610
@property
550611
def random_seed(self):
551612
"""
@@ -562,27 +623,31 @@ def x(self):
562623
"""
563624
return self.__x
564625

626+
565627
@property
566628
def y(self):
567629
"""
568630
Returns the y column that was passed to `dabest.load()`, if any.
569631
"""
570632
return self.__y
571633

634+
572635
@property
573636
def _xvar(self):
574637
"""
575638
Returns the xvar in dabest.plot_data.
576639
"""
577640
return self.__xvar
578641

642+
579643
@property
580644
def _yvar(self):
581645
"""
582646
Returns the yvar in dabest.plot_data.
583647
"""
584648
return self.__yvar
585649

650+
586651
@property
587652
def _plot_data(self):
588653
"""
@@ -599,6 +664,7 @@ def proportional(self):
599664
"""
600665
return self.__proportional
601666

667+
602668
@property
603669
def _all_plot_groups(self):
604670
"""
@@ -1388,7 +1454,7 @@ def __init__(self, dabest, effect_size,
13881454
is_paired, ci=95, proportional=False,
13891455
resamples=5000,
13901456
permutation_count=5000,
1391-
random_seed=12345):
1457+
random_seed=12345, second=None, var2=False):
13921458
"""
13931459
Parses the data from a Dabest object, enabling plotting and printing
13941460
capability for the effect size of interest.
@@ -1402,6 +1468,8 @@ def __init__(self, dabest, effect_size,
14021468
self.__permutation_count = permutation_count
14031469
self.__random_seed = random_seed
14041470
self.__proportional = proportional
1471+
self.__second = second
1472+
self.__var2 = var2
14051473

14061474

14071475
def __pre_calc(self):
@@ -1440,9 +1508,7 @@ def __pre_calc(self):
14401508
r_dict["test"] = tname
14411509
r_dict["control_N"] = int(len(control))
14421510
r_dict["test_N"] = int(len(test))
1443-
14441511
out.append(r_dict)
1445-
14461512
if j == len(idx)-1 and ix == len(current_tuple)-2:
14471513
resamp_count = True
14481514
def_pval = True
@@ -1780,10 +1846,12 @@ def plot(self, color_col=None,
17801846

17811847
from .plotter import EffectSizeDataFramePlotter, ProportionalDataFramePlotter
17821848

1783-
17841849
if hasattr(self, "results") is False:
17851850
self.__pre_calc()
17861851

1852+
if self.__var2:
1853+
color_col = self.__second
1854+
17871855
if self.__proportional:
17881856
raw_marker_size = 0.01
17891857

@@ -1866,6 +1934,16 @@ def ci(self):
18661934
"""
18671935
return self.__ci
18681936

1937+
@property
1938+
def second(self):
1939+
return self.__second
1940+
1941+
1942+
@property
1943+
def var2(self):
1944+
return self.__var2
1945+
1946+
18691947
@property
18701948
def resamples(self):
18711949
"""

0 commit comments

Comments
 (0)