示例#1
0
increasing_firing_rates_neuron_index, increasing_firing_rates = \
    fr_funcs.get_neurons_following_pattern_around_an_event(avg_firing_rate_around_event=avg_firing_rate_around_suc_trials,
                                                           time_around_pattern=time_around_beam_break,
                                                           pattern_regions_to_compare=[0, 0.6, 0.8, 1.2],
                                                           comparison_factor=3, comparison_direction='increase',
                                                           baserate=0.1)

fr_funcs.show_firing_rates_around_event(increasing_firing_rates)

# Show where the neurons are in the brain
template_info_increasing_fr_neurons = template_info.iloc[
    increasing_firing_rates_neuron_index]
spp.view_grouped_templates_positions(
    kilosort_folder,
    const.BRAIN_REGIONS,
    const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info=template_info_increasing_fr_neurons)

pd.to_pickle(template_info_increasing_fr_neurons,
             join(poke_folder, 'ti_increasing_neurons_on_non_trial_pokes.df'))


decreasing_firing_rates_neuron_index, decreasing_firing_rates = \
    fr_funcs.get_neurons_following_pattern_around_an_event(avg_firing_rate_around_event=avg_firing_rate_around_suc_trials,
                                                           time_around_pattern=time_around_beam_break,
                                                           pattern_regions_to_compare=[0, 0.6, 0.8, 1.2],
                                                           comparison_factor=3, comparison_direction='decrease',
                                                           baserate=0.5)

fr_funcs.show_firing_rates_around_event(decreasing_firing_rates)
clean.cleanup_kilosorted_data(kilosort_folder_denoised,
                              number_of_channels_in_binary_file=const.
                              NUMBER_OF_AP_CHANNELS_IN_BINARY_FILE,
                              binary_data_filename=binary_data_filename,
                              prb_file=const.prb_file,
                              type_of_binary=const.BINARY_FILE_ENCODING,
                              order_of_binary='F',
                              sampling_frequency=20000,
                              num_of_shanks_for_vis=5)
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# CREATE TEMPLATE INFO ----------------------------------------------------
spp.generate_probe_positions_of_templates(kilosort_folder_denoised)
spp.view_grouped_templates_positions(kilosort_folder_denoised,
                                     const.BRAIN_REGIONS,
                                     const.PROBE_DIMENSIONS,
                                     const.POSITION_MULT)

#template_info = preproc_kilo.generate_template_info_after_cleaning(kilosort_folder_denoised, sampling_freq)
template_info = np.load(join(kilosort_folder_denoised, 'template_info.df'))

templates_in_cortex = template_info[
    template_info['position Y'] *
    const.POSITION_MULT > const.BRAIN_REGIONS['CA1']]
templates_in_cortex.index = np.arange(len(templates_in_cortex))

templates_in_cortex['number of spikes'].sum()

spikes_in_cortex = np.array([])
for t in templates_in_cortex['spikes in template']:
    spikes_in_cortex = np.concatenate((spikes_in_cortex, t))
        slow_neurons_biggest_channels.append(avg_templates[neuron, channel, :])
        single_slow_indices.append(neuron)

slow_neurons_biggest_channels = np.array(slow_neurons_biggest_channels)

# Plot slow neurons
f_slow = plt.figure(1)
ax_slow = f_slow.add_subplot(111)
ax_slow.plot(tim, slow_neurons_biggest_channels.T)
ax_slow.plot(tim,
             np.mean(slow_neurons_biggest_channels, axis=0),
             c=(0, 0, 0),
             linewidth=5)

spp.view_grouped_templates_positions(
    spikes_folder, const.BRAIN_REGIONS, const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info.iloc[avg_template_indices_for_fast_neurons])
spp.view_grouped_templates_positions(
    spikes_folder, const.BRAIN_REGIONS, const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info.iloc[avg_template_indices_for_slow_neurons])

# CHECK IF THE DOUBLE FIRING NEURONS ARE ACTUALLY EXISTING SINGLE FIRING NEURONS THAT KILOSORT SAW AS DIFFERENT
# WHEN THEY FIRED VERY FAST
template_info_double_slow = template_info.iloc[double_slow_indices]
template_info_fast = template_info.iloc[fast_indices]
template_info_fast_and_double_slow = pd.concat(
    [template_info_fast, template_info_double_slow])

spp.view_grouped_templates_positions(spikes_folder, const.BRAIN_REGIONS,
                                     const.PROBE_DIMENSIONS,
示例#4
0
    [mean_sh, mean_sh + confi_intervals[0], mean_sh + confi_intervals[1]], 0,
    60)

speed_corr_neurons_index = np.squeeze(
    np.argwhere(mutual_infos_spikes_vs_speed > mean_sh + confi_intervals[1]))
speed_corr_neurons = template_info.loc[speed_corr_neurons_index]

brain_positions_of_corr_neurons = np.array([
    speed_corr_neurons['position X'].values * const_comm.POSITION_MULT,
    speed_corr_neurons['position Y'].values * const_comm.POSITION_MULT
])

spp.view_grouped_templates_positions(
    kilosort_folder,
    const_comm.BRAIN_REGIONS,
    const_comm.PROBE_DIMENSIONS,
    const_comm.POSITION_MULT,
    template_info=speed_corr_neurons,
    dot_sizes=mutual_infos_spikes_vs_speed[speed_corr_neurons_index] * 4000,
    font_size=5)

# </editor-fold>

#  -------------------------------------------------
# <editor-fold desc="DO A TIME SHIFT TO SEE IF THERE ARE TIME DEPENDENCIES">
largest_cor_neuron_index = 1140
# Average the speed and the firing rate but with a moving window with step one. Then randomly select 10 windows of 3000
# points each (to keep length of arrays low)
rate_base = np.squeeze(spike_rates[largest_cor_neuron_index, :])
rate_base_moving_window_averaged = binning.rolling_window_with_step(
    rate_base, np.mean, 70, 1)
speed_moving_window_averaged = binning.rolling_window_with_step(
示例#5
0
# Log
plt.hist(mi_pb_spikes_vs_speed, bins=np.logspace(np.log10(0.0001), np.log10(1), 50))
plt.hist(mi_non_pb_spikes_vs_speed, bins=np.logspace(np.log10(0.0001), np.log10(1), 50))
plt.hist(shuffled, bins=np.logspace(np.log10(0.0001), np.log10(1), 50), color=(1, 0, 0, 0.4))
plt.vlines([mean_sh, mean_sh+confi_intervals[0], mean_sh+confi_intervals[1]], 0, 60)
plt.gca().set_xscale("log")
# Linear
plt.hist(mi_pb_spikes_vs_distance_to_poke, bins= 200, color=(0, 0, 1, 0.4))
plt.hist(mi_non_pb_spikes_vs_distance_to_poke, bins= 200, color=(0, 1, 0, 0.4))
plt.hist(shuffled, bins=200, color=(1, 0, 0, 0.4))
plt.vlines([mean_sh, mean_sh+confi_intervals[0], mean_sh+confi_intervals[1]], 0, 20)

distance_to_poke_corr_neurons_pb_index = np.squeeze(np.argwhere(mi_pb_spikes_vs_distance_to_poke > mean_sh+confi_intervals[1]))
distance_to_poke_corr_neurons_pb = template_info.loc[distance_to_poke_corr_neurons_pb_index]
spp.view_grouped_templates_positions(kilosort_folder, const.BRAIN_REGIONS, const.PROBE_DIMENSIONS,
                                     const.POSITION_MULT, template_info=distance_to_poke_corr_neurons_pb,
                                     dot_sizes=mi_pb_spikes_vs_distance_to_poke[distance_to_poke_corr_neurons_pb_index] * 4000,
                                     font_size=5)

distance_to_poke_corr_neurons_non_pb_index = np.squeeze(np.argwhere(mi_non_pb_spikes_vs_distance_to_poke > mean_sh+confi_intervals[1]))
distance_to_poke_corr_neurons_non_pb = template_info.loc[distance_to_poke_corr_neurons_non_pb_index]
spp.view_grouped_templates_positions(kilosort_folder, const.BRAIN_REGIONS, const.PROBE_DIMENSIONS,
                                     const.POSITION_MULT, template_info=distance_to_poke_corr_neurons_non_pb,
                                     dot_sizes=mi_non_pb_spikes_vs_distance_to_poke[distance_to_poke_corr_neurons_non_pb_index] * 4000,
                                     font_size=5)


def draw1(index):
    plt.clf()
    norm_s = (distance_to_poke_patterned_behaviour_0p25 - np.min(distance_to_poke_patterned_behaviour_0p25)) / \
    (np.max(distance_to_poke_patterned_behaviour_0p25) - np.min(distance_to_poke_patterned_behaviour_0p25))
    plt.plot(norm_s)
示例#6
0
print(len(np.argwhere(template_marking == 6)))
print(len(np.argwhere(template_marking == 7)))
template_marking[np.argwhere(template_marking == 5)] = 0
template_marking[np.argwhere(template_marking == 6)] = 0
np.save(join(kilosort_folder, 'template_marking.npy'), template_marking)
# </editor-fold>
# ----------------------------------------------------------------------------------------------------------------------


# ----------------------------------------------------------------------------------------------------------------------
# <editor-fold desc = "STEP 3: CREATE TEMPLATE INFO OF ALL THE CLEANED TEMPLATES"

# a) Create the positions of the templates on the probe (and have a look)
_ = spp.generate_probe_positions_of_templates(kilosort_folder)
bad_channel_positions = spp.get_y_spread_regions_of_bad_channel_groups(kilosort_folder, const_rat.bad_channels)
spp.view_grouped_templates_positions(kilosort_folder, const_rat.BRAIN_REGIONS, const_com.PROBE_DIMENSIONS,
                                     const_com.POSITION_MULT)

# b) Create the template_info.df dataframe (or load it if you already have it)
# template_info = preproc_kilo.generate_template_info_after_cleaning(kilosort_folder, sampling_freq)
template_info = np.load(join(kilosort_folder, 'template_info.df'), allow_pickle=True)

# c) Make the spike info from the initial, cleaned, kilosort results
# spike_info = preproc_kilo.generate_spike_info_after_cleaning(kilosort_folder)
spike_info = np.load(join(kilosort_folder, 'spike_info_after_cleaning.df'), allow_pickle=True)
spp.view_grouped_templates_positions(kilosort_folder, const_rat.BRAIN_REGIONS, const_com.PROBE_DIMENSIONS,
                                     const_com.POSITION_MULT, template_info=template_info)
# </editor-fold>
# ----------------------------------------------------------------------------------------------------------------------


# ----------------------------------------------------------------------------------------------------------------------
#   Have a look at the MIs vs the chance hist
#       Log
_ = plt.hist(mi_spike_rates_vs_distance_traveled_from_poke, bins=np.logspace(np.log10(0.0001), np.log10(1), 50))
_ = plt.hist(shuffled, bins=np.logspace(np.log10(0.0001), np.log10(1), 50), color=(0, 0, 1, 1))
plt.vlines([mean_sh, mean_sh+confi_intervals[0], mean_sh+confi_intervals[1]], 0, 60)
plt.gca().set_xscale("log")
#       Linear
_ = plt.hist(mi_spike_rates_vs_distance_traveled_from_poke, bins= 200, color=(0, 0, 1, 1))
_ = plt.hist(shuffled, bins=200, color=(1, 0, 0, 0.4))
plt.vlines([mean_sh, confi_intervals[0], confi_intervals[1]], 0, 20)

#   Where are the neurons
high_corr_neurons_index = np.squeeze(np.argwhere(mi_spike_rates_vs_distance_traveled_from_poke > confi_intervals[1]))
high_corr_neurons = template_info.loc[high_corr_neurons_index]
spp.view_grouped_templates_positions(kilosort_folder, const.BRAIN_REGIONS, const.PROBE_DIMENSIONS,
                                     const.POSITION_MULT, template_info=high_corr_neurons,
                                     dot_sizes=mi_spike_rates_vs_distance_traveled_from_poke[high_corr_neurons_index] * 4000,
                                     font_size=5)

#   Look at individual spike rates vs the distance traveled
hipp_neuron_index = template_info[template_info['template number'] == 89].index.values[0]
neuron_index_to_show = hipp_neuron_index
neuron_index_to_show = max_neuron
plt.plot((distance_traveled_from_poke_smoothed - distance_traveled_from_poke_smoothed.min()) /
         (distance_traveled_from_poke_smoothed.max() - distance_traveled_from_poke_smoothed.min()))
plt.plot((spike_rates_away_from_poke_smoothed[neuron_index_to_show] - spike_rates_away_from_poke_smoothed[neuron_index_to_show].min()) /
         (spike_rates_away_from_poke_smoothed[neuron_index_to_show].max() - spike_rates_away_from_poke_smoothed[neuron_index_to_show].min()))

# </editor-fold>
# -------------------------------------------------

_ = plt.hist(mi_spike_rates_vs_distance_to_travel_to_poke,
             bins=200,
             color=(0, 0, 1, 1))
_ = plt.hist(shuffled, bins=200, color=(1, 0, 0, 0.4))
plt.vlines([mean_sh, confi_intervals[0], confi_intervals[1]], 0, 20)

#   Where are the neurons
high_corr_neurons_index = np.squeeze(
    np.argwhere(
        mi_spike_rates_vs_distance_to_travel_to_poke > confi_intervals[1]))
high_corr_neurons = template_info.loc[high_corr_neurons_index]
spp.view_grouped_templates_positions(
    kilosort_folder,
    const.BRAIN_REGIONS,
    const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info=high_corr_neurons,
    dot_sizes=(
        mi_spike_rates_vs_distance_to_travel_to_poke[high_corr_neurons_index] +
        0.027) * 4000,
    font_size=5)

#   Look at individual spike rates vs the distance traveled
hipp_neuron_index = template_info[template_info['template number'] ==
                                  89].index.values[0]
neuron_index_to_show = hipp_neuron_index
neuron_index_to_show = max_neuron
plt.plot((distance_to_travel_to_poke_smoothed -
          distance_to_travel_to_poke_smoothed.min()) /
         (distance_to_travel_to_poke_smoothed.max() -
          distance_to_travel_to_poke_smoothed.min()))
plt.plot((spike_rates_away_from_poke_smoothed[neuron_index_to_show] -
hipp_correlated_neurons = {}
hipp_correlated_neurons_indices = {}
for s in mis:
    neurons = correlated_neurons[s]
    h = np.array(hipp_borders) / const.POSITION_MULT
    hipp_correlated_neurons[s] = neurons[np.logical_and(
        neurons['position Y'] > h[0], neurons['position Y'] < 2000)]
    hipp_correlated_neurons_indices[s] = hipp_correlated_neurons[
        s].index.values

#   Look at all the high MI neurons
s = 'speed'
spp.view_grouped_templates_positions(
    kilosort_folder,
    const.BRAIN_REGIONS,
    const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info=correlated_neurons[s],
    dot_sizes=mis[s][correlated_neuron_indices[s]] * 4000,
    font_size=5)
#   Look at the hippocampal high MI neurons
s = 'dtp'
spp.view_grouped_templates_positions(
    kilosort_folder,
    const.BRAIN_REGIONS,
    const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info=hipp_correlated_neurons[s],
    dot_sizes=mis[s][correlated_neuron_indices[s]] * 4000,
    font_size=5)

# </editor-fold>