def main():

    for expt in session.query(models.Experiment):
        print('In experiment "{}"...'.format(expt.id))

        for odor_state in ODOR_STATES:
            print('Odor state = "{}"'.format(odor_state))

            trajs = session.query(models.Trajectory).\
                filter_by(experiment=expt, odor_state=odor_state, clean=True)

            for variable in QUANTITIES:
                print('{}...'.format(variable))

                tp_data = [
                    traj.timepoint_field(session, variable) for traj in trajs
                ]
                n_data_points = np.sum([len(d) for d in tp_data])
                window_len = N_LAGS / expt.sampling_frequency

                acor, p_value, conf_lb, conf_ub = \
                    time_series.xcov_multi_with_confidence(tp_data, tp_data, 0, N_LAGS, normed=True)

                time_vector = np.arange(len(acor)) / expt.sampling_frequency

                file_name = '{}_{}_{}.pickle'.format(expt.id, odor_state,
                                                     variable)

                tp_acor = models.TimepointAutocorrelation(
                    figure_root_path_env_var=figure_data_env_var,
                    directory_path=DIRECTORY_PATH,
                    file_name=file_name,
                    variable=variable,
                    experiment_id=expt.id,
                    odor_state=odor_state,
                    n_data_points=n_data_points,
                    n_trajectories=len(tp_data),
                    window_len=window_len)
                tp_acor.data = {
                    'time_vector': time_vector,
                    'autocorrelation': acor,
                    'p_value': p_value,
                    'confidence_lower': conf_lb,
                    'confidence_upper': conf_ub
                }
                session.add(tp_acor)

                commit(session)
Пример #2
0
    def test_autocovariance_is_delta_function_for_white_noise(self):

        xs = [np.random.normal(0, 3, np.random.randint(500, 1000)) for _ in range(100)]

        acov, pv, conf_lb, conf_ub = \
            time_series.xcov_multi_with_confidence(xs, xs, lag_backward=20, lag_forward=21, normed=True)

        self.assertEqual(len(acov), 41)
        self.assertAlmostEqual(acov[:20].mean(), 0, places=2)
        self.assertAlmostEqual(acov[21:].mean(), 0, places=2)
        self.assertAlmostEqual(acov[20], 1, places=5)

        np.testing.assert_array_almost_equal(acov[:20], acov[21:][::-1])
        np.testing.assert_array_almost_equal(pv[:20], pv[21:][::-1])
        np.testing.assert_array_almost_equal(conf_lb[:20], conf_lb[21:][::-1])
        np.testing.assert_array_almost_equal(conf_ub[:20], conf_ub[21:][::-1])
def main():

    for expt in session.query(models.Experiment):
        print('In experiment "{}"...'.format(expt.id))

        for odor_state in ODOR_STATES:
            print('Odor state = "{}"'.format(odor_state))

            trajs = session.query(models.Trajectory).\
                filter_by(experiment=expt, odor_state=odor_state, clean=True)

            for variable in QUANTITIES:
                print('{}...'.format(variable))

                tp_data = [traj.timepoint_field(session, variable) for traj in trajs]
                n_data_points = np.sum([len(d) for d in tp_data])
                window_len = N_LAGS / expt.sampling_frequency

                acor, p_value, conf_lb, conf_ub = \
                    time_series.xcov_multi_with_confidence(tp_data, tp_data, 0, N_LAGS, normed=True)

                time_vector = np.arange(len(acor)) / expt.sampling_frequency

                file_name = '{}_{}_{}.pickle'.format(expt.id, odor_state, variable)

                tp_acor = models.TimepointAutocorrelation(figure_root_path_env_var=figure_data_env_var,
                                                          directory_path=DIRECTORY_PATH,
                                                          file_name=file_name,
                                                          variable=variable,
                                                          experiment_id=expt.id,
                                                          odor_state=odor_state,
                                                          n_data_points=n_data_points,
                                                          n_trajectories=len(tp_data),
                                                          window_len=window_len)
                tp_acor.data = {'time_vector': time_vector,
                                'autocorrelation': acor,
                                'p_value': p_value,
                                'confidence_lower': conf_lb,
                                'confidence_upper': conf_ub}
                session.add(tp_acor)

                commit(session)