class ANOVA3nested(ANOVA3): def __init__(self, A, B, C): self.X = None self.A = Factor(A) self.B = FactorNested(B, self.A) self.C = FactorNested2(C, self.B) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A', 'B', 'C'] self.f_terms = [('A', 'B'), ('B', 'C'), ('C', 'Error')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() ### specify builder and add design matrix columns: builder = DesignBuilder(labels=['Intercept', 'A', 'B', 'C']) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced_nested3(self.B, self.C): raise (ValueError('Design must be balanced.'))
class ANOVA3nested(ANOVA3): def __init__(self, A, B, C): self.X = None self.A = Factor(A) self.B = FactorNested(B, self.A) self.C = FactorNested2(C, self.B) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A', 'B', 'C'] self.f_terms = [('A','B'), ('B','C'), ('C','Error')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() ### specify builder and add design matrix columns: builder = DesignBuilder(labels=['Intercept', 'A', 'B', 'C']) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced_nested3(self.B, self.C): raise( ValueError('Design must be balanced.') )
def parse_config(config, warning=False): independent_variables = config.get('independentVariables', None) assert independent_variables, "Independent variables field can not be None" parsed_config = {} # parse independent variables for variable_name in independent_variables: try: factor_type = independent_variables[variable_name]['design'] factors_list = parsed_config.get(factor_type, []) if factor_type == FactorType.between_subject.name: factors_list += [ Factor( name=variable_name, levels=independent_variables[variable_name]['levels'], design=FactorType.between_subject.name) ] elif factor_type == FactorType.within_subject.name: factors_list += [ Factor( name=variable_name, levels=independent_variables[variable_name]['levels'], design=FactorType.within_subject.name, order=independent_variables[variable_name]['order']) ] parsed_config[factor_type] = factors_list except Exception as e: print(e) return parsed_config
def __init__(self, A): self.X = None #design matrix self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A'] self.f_terms = [('A', 'Error')] self._assemble()
def test_set_amount_quantiles(self): message = "incorrect amount quantiles for {}" db_teach = pd.DataFrame({"amount": [100, 50, 80, 30, 20, 40, 50]}) db_test = pd.DataFrame({"amount": [35, 20.5, 120, 0.2]}) Factor.set_amount_quantiles(db_teach, db_test) self.assertListEqual(list(db_teach.amount_quntile.values), [6, 4, 5, 2, 1, 3, 4], message.format('teach')) self.assertListEqual(list(db_test.amount_quntile.values), [3, 1, 6, 1], message.format('test'))
def __init__(self, A, SUBJ): self.X = None #design matrix self.S = Factor(SUBJ) #subjects self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A', 'S', 'SA'] self.f_terms = [('A','SA')] self._check_balanced() self._assemble()
def __init__(self, A, SUBJ): self.X = None #design matrix self.S = Factor(SUBJ) #subjects self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A', 'S', 'SA'] self.f_terms = [('A', 'SA')] self._check_balanced() self._assemble()
def __init__(self, A, B): self.X = None self.A = Factor(A) self.B = FactorNested(B, self.A) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A', 'B'] self.f_terms = [('A', 'B'), ('B', 'Error')] self._check_balanced() self._assemble()
def __init__(self, A, B): self.X = None #design matrix self.A = Factor(A) #factor level vector self.B = Factor(B) #factor level vector self.J = self.A.J #number of observations self.contrasts = None self.balanced = True self.term_labels = ['Intercept', 'A', 'B', 'AB'] self.f_terms = [('A', 'Error'), ('B', 'Error'), ('AB', 'Error')] self._check_balanced() self._assemble()
class ANOVA3onerm(ANOVA3rm): '''Only C is an RM factor.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = FactorNestedTwoWay(SUBJ, self.A, self.B) # self.S = FactorNestedTwoWay(SUBJ, self.B, self.C) self.J = self.A.J self.contrasts = None self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC', 'S', 'SC' ] self.f_terms = [('A', 'S'), ('B', 'S'), ('C', 'SC'), ('AB', 'S'), ('AC', 'SC'), ('BC', 'SC'), ('ABC', 'SC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.C.get_design_interaction_3way(self.A, self.B) XSC = self.S.get_design_interaction(self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('S', XS) builder.add_main_columns('SC', XSC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.B): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.B.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.S.check_balanced_rm(self.C): raise (ValueError('Design must be balanced.'))
def __init__(self, A, B, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.S = FactorNested(SUBJ, self.A) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A', 'B', 'S', 'AB', 'SB'] self.f_terms = [('A', 'S'), ('B', 'SB'), ('AB', 'SB')] # self._check_balanced() self._assemble()
def __init__(self, A, B, C): self.X = None #design matrix self.A = Factor(A) #factor level vector self.B = Factor(B) #factor level vector self.C = Factor(C) #factor level vector self.J = self.A.J #number of observations self.term_labels = ['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC'] self.f_terms = [('A','Error'), ('B','Error'), ('C','Error'), ('AB','Error'), ('AC','Error'), ('BC','Error'), ('ABC','Error')] self.contrasts = None self._check_balanced() self._assemble()
def __init__(self, A, B, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.S = FactorNested(SUBJ, self.A) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A', 'B', 'S', 'AB', 'SB'] self.f_terms = [('A','S'), ('B','SB'), ('AB','SB')] # self._check_balanced() self._assemble()
def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = Factor(SUBJ) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A','B','C','S', 'AB','AC','BC', 'SA','SB','SC', 'SAB','SAC','SBC', 'ABC', 'SABC'] self.f_terms = [('A','SA'), ('B','SB'), ('C','SC'), ('AB','SAB'), ('AC','SAC'), ('BC','SBC'), ('ABC','SABC')] self._check_balanced() self._assemble()
class ANOVA3onerm(ANOVA3rm): '''Only C is an RM factor.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = FactorNestedTwoWay(SUBJ, self.A, self.B) # self.S = FactorNestedTwoWay(SUBJ, self.B, self.C) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A','B','C', 'AB','AC','BC', 'ABC', 'S', 'SC'] self.f_terms = [('A','S'), ('B','S'), ('C','SC'), ('AB','S'), ('AC','SC'), ('BC','SC'), ('ABC','SC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.C.get_design_interaction_3way(self.A, self.B) XSC = self.S.get_design_interaction(self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('S', XS) builder.add_main_columns('SC', XSC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.B): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.B.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.S.check_balanced_rm(self.C): raise( ValueError('Design must be balanced.') )
class ANOVA1rm(_Design): def __init__(self, A, SUBJ): self.X = None #design matrix self.S = Factor(SUBJ) #subjects self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A', 'S', 'SA'] self.f_terms = [('A', 'SA')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XS = self.S.get_design_main() XSA = self.A.get_design_interaction(self.S) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('S', XS) builder.add_main_columns('SA', XSA) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.S.balanced): raise (ValueError('Design must be balanced.')) if not self.S.check_balanced(self.A): raise (ValueError('Design must be balanced.')) def check_for_single_responses(self): A, S = self.A.A, self.S.A only_single = False for a in self.A.u: s = S[(A == a)] if np.unique(s).size == s.size: only_single = True warnings.warn( '\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single
class ANOVA3(_Design): def __init__(self, A, B, C): self.X = None #design matrix self.A = Factor(A) #factor level vector self.B = Factor(B) #factor level vector self.C = Factor(C) #factor level vector self.J = self.A.J #number of observations self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC' ] self.f_terms = [('A', 'Error'), ('B', 'Error'), ('C', 'Error'), ('AB', 'Error'), ('AC', 'Error'), ('BC', 'Error'), ('ABC', 'Error')] self.contrasts = None self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder( labels=['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.B): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.B.check_balanced(self.C): raise (ValueError('Design must be balanced.'))
def __init__(self, A): self.X = None #design matrix self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A'] self.f_terms = [('A','Error')] self._assemble()
class ANOVA1rm(_Design): def __init__(self, A, SUBJ): self.X = None #design matrix self.S = Factor(SUBJ) #subjects self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A', 'S', 'SA'] self.f_terms = [('A','SA')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XS = self.S.get_design_main() XSA = self.A.get_design_interaction(self.S) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('S', XS) builder.add_main_columns('SA', XSA) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.S.balanced): raise( ValueError('Design must be balanced.') ) if not self.S.check_balanced(self.A): raise( ValueError('Design must be balanced.') ) def check_for_single_responses(self): A,S = self.A.A, self.S.A only_single = False for a in self.A.u: s = S[(A==a)] if np.unique(s).size == s.size: only_single = True warnings.warn('\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single
def __init__(self, A, B): self.X = None self.A = Factor(A) self.B = FactorNested(B, self.A) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A', 'B'] self.f_terms = [('A','B'), ('B','Error')] self._check_balanced() self._assemble()
def setup_factors(self, node, parent_idx, cpds): if node is None: return self.setup_factors(node.left, node.idx, cpds) self.setup_factors(node.right, node.idx, cpds) node.factor = Factor(scope=[node.idx, parent_idx], card=[4, 4], val=cpds, name="F({0},{1})".format(node.idx, parent_idx), logfactor=self.use_logspace)
def __init__(self, root, prior, cpds, use_logspace=False): self.use_logspace = use_logspace self.root = root self.root.factor = Factor(scope=[root.idx], card=[4], val=prior, name="RootPrior", logfactor=self.use_logspace) self.setup_factors(root.left, root.idx, cpds) self.setup_factors(root.right, root.idx, cpds)
class ANOVA3(_Design): def __init__(self, A, B, C): self.X = None #design matrix self.A = Factor(A) #factor level vector self.B = Factor(B) #factor level vector self.C = Factor(C) #factor level vector self.J = self.A.J #number of observations self.term_labels = ['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC'] self.f_terms = [('A','Error'), ('B','Error'), ('C','Error'), ('AB','Error'), ('AC','Error'), ('BC','Error'), ('ABC','Error')] self.contrasts = None self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.B): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.B.check_balanced(self.C): raise( ValueError('Design must be balanced.') )
def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = Factor(SUBJ) self.J = self.A.J self.contrasts = None self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'S', 'AB', 'AC', 'BC', 'SA', 'SB', 'SC', 'SAB', 'SAC', 'SBC', 'ABC', 'SABC' ] self.f_terms = [('A', 'SA'), ('B', 'SB'), ('C', 'SC'), ('AB', 'SAB'), ('AC', 'SAC'), ('BC', 'SBC'), ('ABC', 'SABC')] self._check_balanced() self._assemble()
class ANOVA1(_Design): def __init__(self, A): self.X = None #design matrix self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A'] self.f_terms = [('A', 'Error')] self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts()
class ANOVA1(_Design): def __init__(self, A): self.X = None #design matrix self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A'] self.f_terms = [('A','Error')] self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts()
def constructFactorGraph(yTilde, H, epsilon): ''' :param - yTilde: observed codeword type: numpy.ndarray containing 0's and 1's shape: 2N :param - H parity check matrix type: numpy.ndarray shape: N x 2N :param epsilon - the probability that each bit is flipped to its complement return G factorGraph You should consider two kinds of factors: - M unary factors - N each parity check factors ''' N = H.shape[0] M = H.shape[1] G = FactorGraph(numVar=M, numFactor=N+M) G.var = range(M) ############################################################## # Q1 # TODO: your code starts here # Add unary factors for variable_Y in range(M): if(yTilde[variable_Y,0] == 0): G.factors.append(Factor(scope = [variable_Y], card = [2], val =np.array([1-epsilon, epsilon]), name="Variable_Y_"+str(variable_Y))) Factorfs = variable_Y G.factorToVar[Factorfs].append(variable_Y) G.varToFactor[variable_Y].append(Factorfs) else: G.factors.append(Factor(scope = [variable_Y], card = [2], val= np.array([epsilon, 1-epsilon]), name="Variable_Y_"+str(variable_Y))) Factorfs = variable_Y G.factorToVar[Factorfs].append(variable_Y) G.varToFactor[variable_Y].append(Factorfs) #Here, the index of factors are the same to index of variables since these are individual factors Factors =[] for i in range(N): H_p = H[i] related_variables = [] cardinalities=[] Factorfs = M + i for variable in range(M): if(H_p[variable] == 1): cardinalities.append(2) related_variables.append(variable) G.varToFactor[variable].append(Factorfs) #adding These new factors: P G.factorToVar[Factorfs] = related_variables codeword_size = len(cardinalities) values = np.zeros((cardinalities)) for codeword in itertools.product([0, 1], repeat=codeword_size): parity = int(sum(codeword) - 2* int(sum(codeword)/2)) if(parity == 1): values[codeword] = 0.0 if(parity == 0): values[codeword] = 1.0 #print("asghar") #print(values.flat) Factors.append(Factor(scope=related_variables, card=cardinalities, val=values, name="P_"+str(i))) G.factors = G.factors + Factors ############################################################## return G
class ANOVA3rm(ANOVA3): '''A, B and C are all RM factors.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = Factor(SUBJ) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A','B','C','S', 'AB','AC','BC', 'SA','SB','SC', 'SAB','SAC','SBC', 'ABC', 'SABC'] self.f_terms = [('A','SA'), ('B','SB'), ('C','SC'), ('AB','SAB'), ('AC','SAC'), ('BC','SBC'), ('ABC','SABC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XSA = self.S.get_design_interaction(self.A) XSB = self.S.get_design_interaction(self.B) XSC = self.S.get_design_interaction(self.C) XSAB = self.S.get_design_interaction_3way(self.A, self.B) XSAC = self.S.get_design_interaction_3way(self.A, self.C) XSBC = self.S.get_design_interaction_3way(self.B, self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) XSABC = self.S.get_design_interaction_4way(self.A, self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('S', XS) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('SA', XSA) builder.add_main_columns('SB', XSB) builder.add_main_columns('SC', XSC) builder.add_main_columns('SAB', XSAB) builder.add_main_columns('SAC', XSAC) builder.add_main_columns('SBC', XSBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('SABC', XSABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _swapAB(self): if self._swap: A,B = self.B, self.A self.A = A self.B = B def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.B): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.B.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.S.check_balanced_rm(self.C): raise( ValueError('Design must be balanced.') ) def check_for_single_responses(self): A,B,C,S = self.A.A, self.B.A, self.C.A, self.S.A only_single = False for a in self.A.u: for b in self.B.u: for c in self.C.u: s = S[(A==a) & (B==b) & (C==c)] if np.unique(s).size == s.size: only_single = True warnings.warn('\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single
def setUp(self): self.factor = Factor() self.db_teach_factor = DataForTests.db_teach_factor
class TestFactor(unittest.TestCase): def setUp(self): self.factor = Factor() self.db_teach_factor = DataForTests.db_teach_factor def test_get_is_zip_digit(self): result = self.factor.is_only_digit(self.db_teach_factor) result = result.tolist() self.assertListEqual(result, [1, 1, 0, 1, 0, 1, 0, 0, 1, 1], 'incorrect default data') def test_get_encode_length(self): result = self.factor.encode_length(self.db_teach_factor, encode={ 'unknown': 0, 'other': 0, '10': 1 }) result = result.tolist() self.assertListEqual(result, [0, 1, 1, 0, 0, 0, 0, 0, 1, 0], 'incorrect default data') def test_get_encode_length_2(self): result = self.factor.encode_length(self.db_teach_factor, encode={ 'unknown': 3, 'other': 0, '10': 1 }) result = result.tolist() self.assertListEqual(result, [0, 1, 1, 0, 0, 3, 0, 0, 1, 0], 'incorrect default data') def test_get_encode_length_3(self): result = self.factor.encode_length(self.db_teach_factor, encode={ 'unknown': 3, 'other': 5, '10': 0 }) result = result.tolist() self.assertListEqual(result, [5, 0, 0, 5, 5, 3, 5, 5, 0, 5], 'incorrect default data') def test_get_network(self): result = self.factor.get_network(self.db_teach_factor) result = result.tolist() expected_ip = [ '184.56.163', '204.210.126', '99.203.145', '69.181.49', '179.158.244', '166.137.175', '172.58.11', '174.202.7', '99.177.243', '73.108.11' ] self.assertListEqual(result, expected_ip, 'incorrect default data') def test_get_bin(self): result = self.factor.get_bin(self.db_teach_factor) result = result.tolist() expected_bin = [ '547087', '517148', '546540', '510805', '544731', '524038', '551791', '517805', '517805', '517805' ] self.assertListEqual(result, expected_bin, 'incorrect default data') def test_get_first_name(self): result = self.factor.get_first_name(self.db_teach_factor) result = result.tolist() expected_first_name = [ 'Gabriele', 'Malia', 'Stephen', 'Nicholas', 'Thiago', 'Jamal', 'Jorge', 'Joseph', 'thomas', 'Micah' ] self.assertListEqual(result, expected_first_name, 'incorrect default data') def test_get_last_name(self): result = self.factor.get_last_name(self.db_teach_factor) result = result.tolist() expected_last_name = [ 'Markes', 'Gusman', 'Gordy', 'Saephanh', 'Fernandes', 'Stephens', 'Hernandez', 'Rusolo', 'van', 'Pitchford' ] self.assertListEqual(result, expected_last_name, 'incorrect default data') def test_get_domain(self): result = self.factor.get_domain(self.db_teach_factor) result = result.tolist() expected_domain = [ 'markes.com', '4mail.com.net', '4roll.mail', 'gmail.com', 'pochta.net', 'yandex.com', 'gmail.com', '4null.com.ua', 'hotmail.com', 'mail.ru' ] self.assertListEqual(result, expected_domain, 'incorrect default data') def test_get_last_domain_zone(self): result = self.factor.get_last_domain_zone(self.db_teach_factor) result = result.tolist() expected_last_dz = [ 'com', 'net', 'mail', 'com', 'net', 'com', 'com', 'ua', 'com', 'ru' ] self.assertListEqual(result, expected_last_dz, 'incorrect default data') def test_get_count_words_in_column(self): result = self.factor.get_count_words_in_column(self.db_teach_factor) result = result.tolist() self.assertListEqual(result, [3, 2, 3, 2, 2, 2, 5, 2, 3, 2], 'incorrect default data') def test_get_phone_2(self): result = self.factor.get_phone_2(self.db_teach_factor) result = result.tolist() expected_phone_2 = [ '13', '10', '10', '11', '97', '', '71', '83', '10', '12' ] self.assertListEqual(result, expected_phone_2, 'incorrect default data') # @unittest.skip("demonstrating skipping") def test_get_phone_3(self): result = self.factor.get_phone_3(self.db_teach_factor) result = result.tolist() expected_phone_3 = [ '135', '108', '100', '119', '977', '', '718', '832', '106', '128' ] self.assertListEqual(result, expected_phone_3, 'incorrect default data') def test_get_quantile(self): message = "incorrect interval for {}" q_amount = [0, 0.3, 0.8, 0.9] x = get_quantile(0.2, q_amount) self.assertEqual(x, 1, message.format(x)) x = get_quantile(0.95, q_amount) self.assertEqual(x, 4, message.format(x)) def test_set_amount_quantiles(self): message = "incorrect amount quantiles for {}" db_teach = pd.DataFrame({"amount": [100, 50, 80, 30, 20, 40, 50]}) db_test = pd.DataFrame({"amount": [35, 20.5, 120, 0.2]}) Factor.set_amount_quantiles(db_teach, db_test) self.assertListEqual(list(db_teach.amount_quntile.values), [6, 4, 5, 2, 1, 3, 4], message.format('teach')) self.assertListEqual(list(db_test.amount_quntile.values), [3, 1, 6, 1], message.format('test'))
class ANOVA3rm(ANOVA3): '''A, B and C are all RM factors.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = Factor(SUBJ) self.J = self.A.J self.contrasts = None self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'S', 'AB', 'AC', 'BC', 'SA', 'SB', 'SC', 'SAB', 'SAC', 'SBC', 'ABC', 'SABC' ] self.f_terms = [('A', 'SA'), ('B', 'SB'), ('C', 'SC'), ('AB', 'SAB'), ('AC', 'SAC'), ('BC', 'SBC'), ('ABC', 'SABC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XSA = self.S.get_design_interaction(self.A) XSB = self.S.get_design_interaction(self.B) XSC = self.S.get_design_interaction(self.C) XSAB = self.S.get_design_interaction_3way(self.A, self.B) XSAC = self.S.get_design_interaction_3way(self.A, self.C) XSBC = self.S.get_design_interaction_3way(self.B, self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) XSABC = self.S.get_design_interaction_4way(self.A, self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('S', XS) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('SA', XSA) builder.add_main_columns('SB', XSB) builder.add_main_columns('SC', XSC) builder.add_main_columns('SAB', XSAB) builder.add_main_columns('SAC', XSAC) builder.add_main_columns('SBC', XSBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('SABC', XSABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _swapAB(self): if self._swap: A, B = self.B, self.A self.A = A self.B = B def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.B): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.B.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.S.check_balanced_rm(self.C): raise (ValueError('Design must be balanced.')) def check_for_single_responses(self): A, B, C, S = self.A.A, self.B.A, self.C.A, self.S.A only_single = False for a in self.A.u: for b in self.B.u: for c in self.C.u: s = S[(A == a) & (B == b) & (C == c)] if np.unique(s).size == s.size: only_single = True warnings.warn( '\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single