Exemplo n.º 1
0
 def test_regress(self):
     if self.test_data is None:
         return
     spike_times = self.test_data['spike_times']
     spike_clusters = self.test_data['spike_clusters']
     event_times = self.test_data['event_times']
     event_groups = self.test_data['event_groups']
     times = np.column_stack(((event_times - 0.5), (event_times + 0.5)))
     counts, cluster_ids = get_spike_counts_in_bins(spike_times,
                                                    spike_clusters, times)
     counts = counts.T
     pred = regress(counts, event_groups)
     self.assertEqual(pred.shape, event_groups.shape)
Exemplo n.º 2
0
    def test_regress(self):
        if self.test_data is None:
            return
        spike_times = self.test_data['spike_times']
        spike_clusters = self.test_data['spike_clusters']
        event_times = self.test_data['event_times']
        event_groups = self.test_data['event_groups']
        cv = KFold(n_splits=2)
        times = np.column_stack(((event_times - 0.5), (event_times + 0.5)))
        counts, cluster_ids = get_spike_counts_in_bins(spike_times,
                                                       spike_clusters, times)
        counts = counts.T

        # Test all regularization methods WITHOUT cross-validation
        pred = regress(counts,
                       event_groups,
                       cross_validation=None,
                       return_training=False,
                       regularization=None)
        self.assertEqual(pred.shape, event_groups.shape)
        pred = regress(counts,
                       event_groups,
                       cross_validation=None,
                       return_training=False,
                       regularization='L1')
        self.assertEqual(pred.shape, event_groups.shape)
        pred = regress(counts,
                       event_groups,
                       cross_validation=None,
                       return_training=False,
                       regularization='L2')
        self.assertEqual(pred.shape, event_groups.shape)

        # Test all regularization methods WITH cross-validation
        pred, pred_training = regress(counts,
                                      event_groups,
                                      cross_validation=cv,
                                      return_training=True,
                                      regularization=None)
        self.assertEqual(pred.shape, event_groups.shape)
        self.assertEqual(pred_training.shape, event_groups.shape)
        pred, pred_training = regress(counts,
                                      event_groups,
                                      cross_validation=cv,
                                      return_training=True,
                                      regularization='L1')
        self.assertEqual(pred.shape, event_groups.shape)
        self.assertEqual(pred_training.shape, event_groups.shape)
        pred, pred_training = regress(counts,
                                      event_groups,
                                      cross_validation=cv,
                                      return_training=True,
                                      regularization='L2')
        self.assertEqual(pred.shape, event_groups.shape)
        self.assertEqual(pred_training.shape, event_groups.shape)
                # Initialize cross-validation
                if VALIDATION == 'kfold-interleaved':
                    cv = KFold(n_splits=NUM_SPLITS, shuffle=True)
                elif VALIDATION == 'kfold':
                    cv = KFold(n_splits=NUM_SPLITS, shuffle=False)

                # Get target to use for this session
                if len(target.shape) == 1:
                    this_target = target
                elif len(target.shape) == 2:
                    this_target = target[j, :trials.shape[0]]

                # Decode selected trials
                pred_target, pred_target_train = regress(
                    population_activity,
                    this_target,
                    cross_validation=cv,
                    return_training=True,
                    regularization=REGULARIZATION)
                r_target = pearsonr(this_target, pred_target)[0]
                mse_target = mean_squared_error(this_target, pred_target)
                r_target_train = pearsonr(this_target, pred_target_train)[0]
                mse_target_train = mean_squared_error(this_target,
                                                      pred_target_train)

                # Estimate chance level
                r_null = np.empty(ITERATIONS)
                r_train_null = np.empty(ITERATIONS)
                mse_null = np.empty(ITERATIONS)
                mse_train_null = np.empty(ITERATIONS)
                for k in range(ITERATIONS):
    spks_region = spikes[probe].times[np.isin(spikes[probe].clusters,
                                              clusters_in_region)]
    clus_region = spikes[probe].clusters[np.isin(spikes[probe].clusters,
                                                 clusters_in_region)]

    # Decode prior from model fit
    times = np.column_stack(
        ((trials.goCue_times - PRE_TIME), (trials.goCue_times + POST_TIME)))
    population_activity, cluster_ids = get_spike_counts_in_bins(
        spks_region, clus_region, times)
    population_activity = population_activity.T
    if VALIDATION == 'kfold-interleaved':
        cv = KFold(n_splits=NUM_SPLITS, shuffle=True)
    elif VALIDATION == 'kfold':
        cv = KFold(n_splits=NUM_SPLITS, shuffle=False)
    pred_prior = regress(population_activity, priors, cross_validation=cv)
    r_prior = pearsonr(priors, pred_prior)[0]

    # Plot trial-to-trial probability
    figure_style(font_scale=1.5)
    trial_blocks = (trials.probabilityLeft == 0.2).astype(int)
    f, ax1 = plt.subplots(1, 1, figsize=(12, 6), dpi=150)

    block_trans = np.append(
        [0],
        np.array(np.where(np.diff(trials.probabilityLeft) != 0)) + 1)
    block_trans = np.append(block_trans, [trial_blocks.shape[0]])
    for j, trans in enumerate(block_trans[:-1]):
        if j == 0:
            p = Rectangle((trans, -0.05),
                          block_trans[j + 1] - trans,
# Decode prior from model fit
times = np.column_stack(
    ((trials.goCue_times - PRE_TIME), (trials.goCue_times + POST_TIME)))
pop_act_all, cluster_ids = get_spike_counts_in_bins(spikes[PROBE].times,
                                                    spikes[PROBE].clusters,
                                                    times)
pop_act_all = pop_act_all.T
pop_act_pass, cluster_ids = get_spike_counts_in_bins(spike_times,
                                                     spike_clusters, times)
pop_act_pass = pop_act_pass.T
if VALIDATION == 'kfold-interleaved':
    cv = KFold(n_splits=NUM_SPLITS, shuffle=True)
elif VALIDATION == 'kfold':
    cv = KFold(n_splits=NUM_SPLITS, shuffle=False)
pred_all = regress(pop_act_all,
                   priors,
                   cross_validation=cv,
                   regularization='L1')
r_all = pearsonr(priors, pred_all)[0]
pred_pass = regress(pop_act_pass,
                    priors,
                    cross_validation=cv,
                    regularization='L1')
r_pass = pearsonr(priors, pred_pass)[0]

# Estimate chance level
r_null_all, r_null_pass = np.empty(ITERATIONS), np.empty(ITERATIONS)
for k in range(ITERATIONS):
    # Exclude the current mice from all trials
    all_trials_excl = all_trials[all_trials['subject'] != SUBJECT]

    # Get a random chunck of trials the same length as the current session