kutaslab / fitgrid

Multichannel event-related time-series regression modeling for EEG, MEG, and sensor array data
https://kutaslab.github.io/fitgrid
BSD 3-Clause "New" or "Revised" License
8 stars 4 forks source link

Patsy Contrast Coding does not work #171

Open astoermann opened 4 years ago

astoermann commented 4 years ago

Code:

cm_a = ContrastMatrix([[-.5], [.5]], ["[ThreatCondition]"]) cm_b = ContrastMatrix( [ [2/3, 0], [-1/3, -.5], [-1/3, .5] ],
["[Condition:CvI]", "[Condition:URx]"])

fg_contrast_threatbycondition = fitgrid.lm( epochs_fg_stmath, LHS =['MiPa'], RHS='C(ThreatCondition, cm_a) * C(Condition, cm_b)', )

ERROR MESSAGE:


NameError Traceback (most recent call last) ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/compat.py in call_and_wrap_exc(msg, origin, f, *args, *kwargs) 35 try: ---> 36 return f(args, **kwargs) 37 except Exception as e:

~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/eval.py in eval(self, expr, source_name, inner_namespace) 165 return eval(code, {}, VarLookupDict([inner_namespace] --> 166 + self._namespaces)) 167

in NameError: name 'cm_b' is not defined The above exception was the direct cause of the following exception: PatsyError Traceback (most recent call last) in 2 epochs_fg_stmath, 3 LHS =['MiPa'], ----> 4 RHS='C(ThreatCondition, cm_a) * C(Condition, cm_b)', 5 ) ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/fitgrid/models.py in lm(epochs, LHS, RHS, parallel, n_cores, eval_env) 162 channels=LHS, 163 parallel=parallel, --> 164 n_cores=n_cores, 165 ) 166 ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/fitgrid/models.py in _run_model(epochs, function, channels, parallel, n_cores) 114 results = map(processor, groups) 115 --> 116 grid = pd.concat(results, axis=1).T 117 grid.index.name = epochs.time 118 return grid # dataframe, not FitGrid ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy) 253 verify_integrity=verify_integrity, 254 copy=copy, --> 255 sort=sort, 256 ) 257 ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/pandas/core/reshape/concat.py in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy, sort) 299 objs = [objs[k] for k in keys] 300 else: --> 301 objs = list(objs) 302 303 if len(objs) == 0: ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/fitgrid/models.py in process_key_and_group(key_and_group, function, channels) 43 def process_key_and_group(key_and_group, function, channels): 44 key, group = key_and_group ---> 45 results = {channel: function(group, channel) for channel in channels} 46 return pd.Series(results, name=key) 47 ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/fitgrid/models.py in (.0) 43 def process_key_and_group(key_and_group, function, channels): 44 key, group = key_and_group ---> 45 results = {channel: function(group, channel) for channel in channels} 46 return pd.Series(results, name=key) 47 ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/fitgrid/models.py in lm_single(data, channel, RHS, eval_env) 121 def lm_single(data, channel, RHS, eval_env): 122 formula = channel + ' ~ ' + RHS --> 123 return ols(formula, data, eval_env=eval_env).fit() 124 125 ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/statsmodels/base/model.py in from_formula(cls, formula, data, subset, drop_cols, *args, **kwargs) 167 168 tmp = handle_formula_data(data, None, formula, depth=eval_env, --> 169 missing=missing) 170 ((endog, exog), missing_idx, design_info) = tmp 171 max_endog = cls._formula_max_endog ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/statsmodels/formula/formulatools.py in handle_formula_data(Y, X, formula, depth, missing) 63 if data_util._is_using_pandas(Y, None): 64 result = dmatrices(formula, Y, depth, return_type='dataframe', ---> 65 NA_action=na_action) 66 else: 67 result = dmatrices(formula, Y, depth, return_type='dataframe', ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/highlevel.py in dmatrices(formula_like, data, eval_env, NA_action, return_type) 308 eval_env = EvalEnvironment.capture(eval_env, reference=1) 309 (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env, --> 310 NA_action, return_type) 311 if lhs.shape[1] == 0: 312 raise PatsyError("model is missing required outcome variables") ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/highlevel.py in _do_highlevel_design(formula_like, data, eval_env, NA_action, return_type) 163 return iter([data]) 164 design_infos = _try_incr_builders(formula_like, data_iter_maker, eval_env, --> 165 NA_action) 166 if design_infos is not None: 167 return build_design_matrices(design_infos, data, ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/highlevel.py in _try_incr_builders(formula_like, data_iter_maker, eval_env, NA_action) 68 data_iter_maker, 69 eval_env, ---> 70 NA_action) 71 else: 72 return None ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/build.py in design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action) 694 factor_states, 695 data_iter_maker, --> 696 NA_action) 697 # Now we need the factor infos, which encapsulate the knowledge of 698 # how to turn any given factor into a chunk of data: ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/build.py in _examine_factor_types(factors, factor_states, data_iter_maker, NA_action) 441 for data in data_iter_maker(): 442 for factor in list(examine_needed): --> 443 value = factor.eval(factor_states[factor], data) 444 if factor in cat_sniffers or guess_categorical(value): 445 if factor not in cat_sniffers: ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/eval.py in eval(self, memorize_state, data) 564 return self._eval(memorize_state["eval_code"], 565 memorize_state, --> 566 data) 567 568 __getstate__ = no_pickling ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/eval.py in _eval(self, code, memorize_state, data) 549 memorize_state["eval_env"].eval, 550 code, --> 551 inner_namespace=inner_namespace) 552 553 def memorize_chunk(self, state, which_pass, data): ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/compat.py in call_and_wrap_exc(msg, origin, f, *args, **kwargs) 41 origin) 42 # Use 'exec' to hide this syntax from the Python 2 parser: ---> 43 exec("raise new_exc from e") 44 else: 45 # In python 2, we just let the original exception escape -- better ~/.conda/envs/mkconda_0.0.10/lib/python3.6/site-packages/patsy/compat.py in PatsyError: Error evaluating factor: NameError: name 'cm_b' is not defined MiPa ~ C(ThreatCondition, cm_a) * C(Condition, cm_b) ^^^^^^^^^^^^^^^^^^
astoermann commented 4 years ago

Imported packages: from patsy import dmatrix, ContrastMatrix, balanced