def test_radon( # add_data, add_box, add_condition_slope, add_group_slope, # do_mean_over_trials, do_make_change): window = BayesRegression(df=df_radon, y='radon', treatment='floor', condition=['county']) # window.plot(x='county').facet(row='floor').display() window.fit( add_condition_slope= add_condition_slope, # do_mean_over_trials=do_mean_over_trials, add_group_slope=add_group_slope, do_make_change=do_make_change, n_draws=100, num_chains=1, num_warmup=100) # window.plot().display() window.plot( x=':O', # add_data=add_data, ).display() window.plot() window.chart_posterior_kde.display() # window.chart_data_boxplot.display() # Fold change won't work, bc uneven number of entries assert len(window.charts) > 3 # Should include kde
def test_plot_posteriors_no_slope(): window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse') window.fit(model=models.model_hierarchical) window.plot()
def test_plot_slopes_2levelslope(): window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron_x_mouse', group='mouse', add_data=True) window.fit(model=models.model_hierarchical, add_group_slope=True) window.plot().display()
def test_facet(): # Slopes: window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse') window.fit(model=models.model_hierarchical) window.plot(row='neuron', width=40) window.plot(x='neuron').facet(column='mouse')
def test_plot_slopes(): window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse', add_data=True) window.fit(model=models.model_hierarchical) window.plot() window.plot_BEST()
def test_single_condition_nodata(): window = BayesRegression(df=dfl, y='Log power', treatment='stim', group='mouse') window.fit(model=models.model_hierarchical, do_make_change='divide', dist_y='normal') alt.layer(*plot_posterior( df=window.data_and_posterior, y_title='Log power', )).display() window.plot(independent_axes=True).display()
def test_estimate_posteriors_slope(add_condition_slope): window = BayesRegression(df=df, y='isi', treatment='stim', condition=['neuron', 'mouse'], group='mouse', add_data=True) window.fit(models.model_hierarchical, add_condition_slope=add_condition_slope) chart = window.plot(x='neuron', column='neuron', row='mouse') chart.display() chart = window.plot(x='neuron', column='neuron', row='mouse') chart.display()
def test_estimate_posteriors_data_overlay_slope(): window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse') window.fit(model=models.model_hierarchical) chart = window.plot(x='neuron', independent_axes=False) chart.display() window.chart.facet(row='mouse').display()
def test_chirp_data2(): dfdata = pd.read_csv(Path('tests') / 'test_data' / 'chirp_power.csv') window = BayesRegression( df=dfdata, y='Log power', treatment='stim_on', # condition=['Condition code'], group='Subject', add_data=True) window.fit( model=models.model_hierarchical, fold_change_index_cols=[ # 'Condition code', 'Brain region', 'Stim phase', 'stim_on', 'Fid', 'Subject', 'Inversion' ], do_mean_over_trials=True, num_chains=1, n_draws=100, num_warmup=100) window.plot(x='Stim phase', color='Fid', add_data=True, independent_axes=True).display()
def test_chirp_data(force_correct_fold_change_index_cols): dfdata = pd.read_csv(Path('tests') / 'test_data' / 'chirp_power.csv') window = BayesRegression( df=dfdata, y='Log power', treatment='stim_on', condition=['Stim phase', 'Inversion', 'Brain region'], group='Subject') if force_correct_fold_change_index_cols: window.fit(model=models.model_hierarchical, fold_change_index_cols=[ 'Brain region', 'Stim phase', 'stim_on', 'Fid', 'Subject', 'Inversion' ], num_chains=1, n_draws=100, num_warmup=100) else: window.fit(model=models.model_hierarchical, num_chains=1, n_draws=100, num_warmup=100) window.plot(x='Stim phase', color='Fid', independent_axes=True).display()
def test_estimate_posteriors_slope_uneven_n_data_per_condition(): # Trying to reproduce Uneven number of entries in conditions! Try setting do_take_mean=True df, df_monster, index_cols, firing_rates = generate_fake_spikes( n_trials=10, n_neurons=3, n_mice=4, dur=2, ) df = df.drop(df[(df['i_trial'] == 0) & # (df['neuron_x_mouse'] == '0m0bayes') & (df['stim'] == 0)].index) window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse', add_data=True) window.fit(models.model_hierarchical, do_make_change='divide') chart = window.plot(x='neuron', column='neuron', row='mouse') chart.display() chart = window.plot(x='neuron', column='neuron', row='mouse') chart.display()
def test_estimate_posteriors_data_overlay_indep_axes_slope( add_data, add_data_plot, add_group_slope): window = BayesRegression(df=df, add_data=add_data, y='isi', treatment='stim', condition='neuron', group='mouse') window.fit(model=models.model_hierarchical, add_group_slope=add_group_slope) chart = window.plot(independent_axes=True, add_data=add_data_plot) chart.display() if add_group_slope and add_data_plot: chart = window.facet(column='neuron', row='mouse') else: chart = window.facet(column='neuron') chart.display()
def test_chirp_data1(plot_from_data_and_posterior): dfdata = pd.read_csv(Path('tests') / 'test_data' / 'chirp_power.csv') window = BayesRegression(df=dfdata, y='Log power', treatment='stim_on', condition=['Stim phase', 'Inversion'], group='Subject') window.fit(model=models.model_hierarchical, num_chains=1, n_draws=100, num_warmup=100) chart = window.plot( x='Stim phase', # color='Fid', add_data=plot_from_data_and_posterior, independent_axes=True) chart.display() chart_data = pd.DataFrame.from_records( list(json.loads(chart.to_json())['datasets'].values())[0]) assert (chart_data['Stim phase'].unique().astype(float) == dfdata['Stim phase'].unique()).all()
def test_estimate_posteriors_two_conditions(): df = generate_spikes_stim_types( mouse_response_slope=3, n_trials=2, n_neurons=3, n_mice=4, dur=2, ) regression = BayesRegression( BayesWindow(df=df, y='isi', treatment='stim', condition=['neuron', 'stim_strength'], group='mouse', add_data=True)) regression.fit(model=models.model_hierarchical, fold_change_index_cols=None, do_mean_over_trials=False) for condition_name in regression.window.condition: assert condition_name in regression.data_and_posterior.columns, f'{condition_name} not in window.condition' chart = regression.plot(x='neuron', column='neuron', row='mouse') chart.display()
def plot(self, **kwargs): from bayes_window import BayesRegression return BayesRegression.plot(self, **kwargs)
def random_tests(): # TODO make a notebook for this window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse') window.fit(model=models.model_hierarchical, num_chains=1) window.plot(x='neuron', color='mouse', independent_axes=True, finalize=True) window.plot(independent_axes=False, x='neuron:O', color='mouse') window.plot(add_box=False, independent_axes=True, x='neuron:O', color='mouse') window.plot(independent_axes=False, x='neuron:O', color='mouse') chart = window.plot(independent_axes=True, x='neuron:O', color='mouse') chart.display() chart.resolve_scale(y='independent') window.facet(column='neuron') window = BayesRegression(df=df, y='isi', treatment='stim', condition='neuron', group='mouse') window.fit(model=models.model_hierarchical, num_chains=1) window.plot(x='neuron', color='i_trial') window.plot() # x='Stim phase', color='Fid')#,independent_axes=True) window.facet(column='neuron', row='mouse')
def fit(self, do_make_change='divide', add_interaction=False, add_data=False, formula=None, add_group_intercept=True, add_group_slope=False, add_nested_group=False, **kwargs): # model = MixedLM(endog=self.window.data[self.window.y], # exog=self.window.data[self.window.condition], # groups=self.window.data[self.window.group], # # exog_re=exog.iloc[:, 0] # ) self.b_name = 'lme' # dehumanize all columns and variable names for statsmodels: [ self.window.data.rename({col: col.replace(" ", "_")}, axis=1, inplace=True) for col in self.window.data.columns ] self.window.y = self.window.y.replace(" ", "_") self.window.group = self.window.group.replace(" ", "_") self.window.treatment = self.window.treatment.replace(" ", "_") self.window.do_make_change = do_make_change include_condition = False # in all but the following cases: if self.window.condition[0]: self.window.condition[0] = self.window.condition[0].replace( " ", "_") if len(self.window.condition) > 1: self.window.condition[1] = self.window.condition[1].replace( " ", "_") if len(self.window.data[self.window.condition[0]].unique()) > 1: include_condition = True # condition = None # Preallocate # Make formula if include_condition and not formula: if len(self.window.condition) > 1: raise NotImplementedError( f'conditions {self.window.condition}. Use combined_condition' ) # This would need a combined condition dummy variable and an index of condition in patsy: # formula = f"{self.window.y} ~ 1+ {self.window.condition}(condition_index) | {self.window.treatment}" # Combined condition # self.window.data, self._key = utils.combined_condition(self.window.data.copy(), self.window.condition) # self.window.condition = ['combined_condition'] # self.window.original_data = self.window.data.copy() # Make dummy variables for each level in condition: self.window.data = pd.concat( (self.window.data, pd.get_dummies(self.window.data[self.window.condition[0]], prefix=self.window.condition[0], prefix_sep='__', drop_first=False)), axis=1) dummy_conditions = [ cond for cond in self.window.data.columns if cond[:len(self.window.condition[0]) + 2] == f'{self.window.condition[0]}__' ] if add_group_intercept and not add_group_slope and not add_nested_group: formula = f"{self.window.y} ~ (1|{self.window.group}) + {self.window.treatment}| {dummy_conditions[0]}" # eg 'firing_rate ~ stim|neuron_x_mouse__0 +stim|neuron_x_mouse__1 ... + ( 1 |mouse )' for dummy_condition in dummy_conditions[1:]: formula += f" + {self.window.treatment}|{dummy_condition}" elif add_group_intercept and add_group_slope and not add_nested_group: formula = ( f"{self.window.y} ~ ({self.window.treatment}|{self.window.group}) " f" + {self.window.treatment}| {dummy_conditions[0]}") for dummy_condition in dummy_conditions[1:]: formula += f" + {self.window.treatment}|{dummy_condition}" elif add_group_intercept and add_group_slope and add_nested_group: formula = ( f"{self.window.y} ~ ({self.window.treatment}|{self.window.group}) + " f"{self.window.treatment}| {dummy_conditions[0]}:{self.window.group}" ) for dummy_condition in dummy_conditions[1:]: formula += f" + {self.window.treatment}|{dummy_condition}:{self.window.group}" # if add_interaction: # formula += f"+ {condition} * {self.window.treatment}" elif self.window.group and not formula: # Random intercepts and slopes (and their correlation): (Variable | Group) formula = f'{self.window.y} ~ C({self.window.treatment}, Treatment) + (1 | {self.window.group})' # (1 | {self.window.group}) + # Random intercepts and slopes (without their correlation): (1 | Group) + (0 + Variable | Group) # formula += f' + (1 | {self.window.group}) + (0 + {self.window.treatment} | {self.window.group})' elif not formula: formula = f"{self.window.y} ~ C({self.window.treatment}, Treatment)" print(f'Using formula {formula}') result = sm.mixedlm(formula, self.window.data, groups=self.window.data[self.window.group]).fit() print(result.summary().tables[1]) self.data_and_posterior = utils.scrub_lme_result( result, include_condition, self.window.condition[0], self.window.data, self.window.treatment) if add_data: raise NotImplementedError(f'No adding data to LME') self.data_and_posterior = utils.add_data_to_lme( do_make_change, include_condition, self.posterior, self.window.condition[0], self.window.data, self.window.y, self.levels, self.window.treatment) # self.trace.posterior = utils.rename_posterior(self.trace.posterior, self.b_name, # posterior_index_name='combined_condition', # group_name=self.window.group, group2_name=self.window.group2) # # # HDI and MAP: # self.posterior = {var: utils.get_hdi_map(self.trace.posterior[var], # prefix=f'{var} ' # if (var != self.b_name) and (var != 'slope_per_condition') else '') # for var in self.trace.posterior.data_vars} # # # Fill posterior into data # self.data_and_posterior = utils.insert_posterior_into_data(posteriors=self.posterior, # data=self.window.data.copy(), # group=self.window.group) # # # self.posterior = utils.recode_posterior(self.posterior, self.levels, self.window.data, self.window.original_data, # self.window.condition) from bayes_window.slopes import BayesRegression self.charts = BayesRegression.plot(self) return self