Exemplo n.º 1
0
def test_fit():
    window = BayesRegression(df=df,
                             y='isi',
                             treatment='stim',
                             condition='neuron',
                             group='mouse')
    window.fit(model=models.model_hierarchical)
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def test_gpu():
    window = BayesRegression(df=df,
                             y='isi',
                             treatment='stim',
                             condition=['neuron', 'mouse'],
                             group='mouse',
                             detail='i_trial')
    window.fit(model=models.model_hierarchical,
               do_make_change='subtract',
               add_condition_slope=True,
               add_group_slope=True,
               use_gpu=True)
    assert window.data_and_posterior.dropna(
        subset=['mu_intercept_per_group center interval'
                ])['mouse'].unique().size == 4
Exemplo n.º 5
0
def test_slopes(do_make_change, detail):
    window = BayesRegression(df=df,
                             y='isi',
                             treatment='stim',
                             condition='neuron',
                             group='mouse',
                             detail=detail)
    # try:
    window.fit(
        model=models.model_hierarchical,
        do_make_change=do_make_change,
    )
    # fold_change_index_cols=('stim', 'mouse', 'neuron_x_mouse'))
    window.chart.display()
    window.window.chart_data_box_detail.display()
Exemplo n.º 6
0
def test_two_groups():
    df = generate_spikes_stim_types(
        mouse_response_slope=3,
        n_trials=2,
        n_neurons=3,
        n_mice=4,
        dur=2,
    )

    window = BayesRegression(df=df,
                             y='isi',
                             treatment='stim',
                             condition=['stim_strength', 'neuron_x_mouse'],
                             group='mouse',
                             group2='neuron')
    window.fit(model=models.model_hierarchical, add_group2_slope=True)
Exemplo n.º 7
0
def test_data_replacement1():
    window = BayesRegression(df=df,
                             y='isi',
                             treatment='stim',
                             condition='neuron_x_mouse',
                             group='mouse',
                             detail='i_trial',
                             add_data=True)
    window.fit(
        model=models.model_hierarchical,
        do_make_change='subtract',
        add_condition_slope=True,
        # add_group_slope=True
    )
    posterior_no_nan = window.data_and_posterior.dropna(
        subset=['mu_intercept_per_group center interval'])
    assert posterior_no_nan['mouse'].unique().size == 4
Exemplo n.º 8
0
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()
Exemplo n.º 9
0
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')
Exemplo n.º 10
0
def test_inheritance():
    window = BayesWindow(df=df,
                         y='isi',
                         treatment='stim',
                         condition='neuron',
                         group='mouse')
    window = BayesRegression(window)
    window.fit(model=models.model_hierarchical)
    chart = window.plot(x='neuron', independent_axes=False)
    chart.display()
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
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()
Exemplo n.º 13
0
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()
Exemplo n.º 14
0
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()
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
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()
Exemplo n.º 17
0
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()
Exemplo n.º 18
0
def test_plot_slopes_intercepts(do_make_change):
    window = BayesRegression(df=dfl,
                             y='Power',
                             treatment='stim',
                             group='mouse',
                             add_data=True)
    # Fit:
    window.fit(model=models.model_hierarchical,
               add_group_intercept=True,
               zscore_y=False,
               add_group_slope=False,
               robust_slopes=False,
               do_make_change=do_make_change,
               dist_y='gamma',
               num_chains=1,
               n_draws=100,
               num_warmup=100)

    window.plot_intercepts(x='mouse').display()
    chart_intercepts = window.chart_posterior_intercept
    chart_intercepts.display()
Exemplo n.º 19
0
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()
Exemplo n.º 20
0
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()
Exemplo n.º 21
0
 def facet(self, **kwargs):
     from bayes_window import BayesRegression
     return BayesRegression.facet(self, **kwargs)
Exemplo n.º 22
0
    assert window.data_and_posterior.dropna(
        subset=['mu_intercept_per_group center interval'
                ])['mouse'].unique().size == 4


# def test_stim_strength():
#     df = []
#     for slope in np.linspace(4, 400, 4):
#         df1 = generate_fake_lfp(mouse_response_slope=slope)[0]
#         df1['stim_strength'] = slope
#         df.append(df1)
#     df = pd.concat(df)

#     BayesRegression(df=df, 'Power', treatment='stim_strength', group='mouse', detail='i_trial').data_box_detail()
#     window = BayesRegression(df=df, 'Power', treatment='stim_strength', condition='mouse', detail='i_trial')
#     window.fit(add_condition_slope=True, center_intercept=True, dist_y='normal', num_chains=1, n_draws=100, num_warmup=100)
#     window.chart

window = BayesRegression(df=df, y='isi', treatment='stim', group='mouse')
window.fit(model=models.model_hierarchical)


def test_extra_plots():
    window.plot_intercepts().display()
    window.plot_detail_minus_intercepts().display()


# def test_explore_models():
#     window.explore_models(parallel=False)
#     window.explore_model_kinds(parallel=False)
Exemplo n.º 23
0
    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
Exemplo n.º 24
0
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')