regions_pos = np.array(list(const.BRAIN_REGIONS.values()))
pos_to_elect_factor = const.NUMBER_OF_AP_CHANNELS_IN_BINARY_FILE / 8100
region_lines = binning.scale(regions_pos, np.min(regions_pos) * pos_to_elect_factor, np.max(regions_pos) * pos_to_elect_factor)

_ = plt.figure(1)
plt.imshow(np.flipud(avg_muae_around_tp), aspect='auto')
plt.vlines(x=avg_muae_around_tp.shape[1] / 2, ymin=0, ymax=avg_muae_around_tp.shape[0] - 1)
plt.hlines(y=avg_muae_around_tp.shape[0] - region_lines, xmin=0, xmax=avg_muae_around_tp.shape[1]-1, linewidth=3, color='w')

tp_n = np.empty((avg_muae_around_ntp.shape))
for i in np.arange(len(avg_muae_around_ntp)):
    tp_n[i, :] = binning.scale(avg_muae_around_ntp[i], 0, 1)

_= plt.figure(2)
plt.imshow(np.flipud(tp_n), aspect='auto')
plt.vlines(x=tp_n.shape[1] / 2, ymin=0, ymax=tp_n.shape[0] - 1)
plt.hlines(y=tp_n.shape[0] - region_lines, xmin=0, xmax=tp_n.shape[1]-1, linewidth=1, color='w')

tp_n_smooth = binning.rolling_window_with_step(tp_n, np.mean, 40, 40)

_= plt.figure(3)
plt.imshow(np.flipud(tp_n_smooth), aspect='auto')
plt.vlines(x=tp_n_smooth.shape[1] / 2, ymin=0, ymax=tp_n_smooth.shape[0] - 1)
plt.hlines(y=tp_n_smooth.shape[0] - region_lines, xmin=0, xmax=tp_n_smooth.shape[1]-1, linewidth=1, color='w')

_= plt.figure(3)
plt.imshow(np.flipud(tp_n_smooth), aspect='auto', extent=[-8, 8, len(tp_n_smooth), 0])
plt.hlines(y=tp_n_smooth.shape[0] - region_lines, xmin=-8, xmax=8, linewidth=3, color='w')
plt.vlines(x=0, ymin=0, ymax=tp_n_smooth.shape[0] - 1)
示例#2
0
#  Load the pre generated DataFrames for the event CSVs
event_dataframes = ns_funcs.load_events_dataframes(events_folder, sync_funcs.event_types)
file_to_save_to = join(kilosort_folder, 'firing_rate_with_video_frame_window.npy')
template_info = pd.read_pickle(join(kilosort_folder, 'template_info.df'))

spike_info = pd.read_pickle(join(kilosort_folder, 'spike_info_after_cleaning.df'))
spike_rates = binning.spike_count_per_frame(template_info, spike_info, event_dataframes['ev_video'],
                                            sampling_freq, file_to_save_to=file_to_save_to)

# Using the frame based spikes rates do a rolling window to average a bit more
num_of_frames_to_average = 0.25/(1/120)

spike_rates_0p25 = []
for n in np.arange(spike_rates.shape[0]):
    spike_rates_0p25.append(binning.rolling_window_with_step(spike_rates[n, :], np.mean,
                                                             num_of_frames_to_average, num_of_frames_to_average))
spike_rates_0p25 = np.array(spike_rates_0p25)
np.save(join(kilosort_folder, 'firing_rate_with_0p25s_window.npy'), spike_rates_0p25)
# </editor-fold>
# ----------------------------------------------------------------------------------------------------------------------

# -------------------------------------------------
# <editor-fold desc="GET TIMES AND FRAMES OF SUCCESSFUL TRIALS">

video_frame_spike_rates_filename = join(kilosort_folder, 'firing_rate_with_video_frame_window.npy')
spike_rates = np.load(video_frame_spike_rates_filename)
camera_pulses, beam_breaks, sounds = \
    sync_funcs.get_time_points_of_events_in_sync_file(data_folder, clean=True,
                                                      cam_ttl_pulse_period=
                                                      const_com.CAMERA_TTL_PULSES_TIMEPOINT_PERIOD)
sounds_dur = sounds[:, 1] - sounds[:, 0]
sr_trials = sr[:, trial_windows_in_frames]
sr_non_trials = sr[:, non_trial_windows_in_frames]

sr_baseline_trials = sr[:, baseline_trial_windows_in_frames]
sr_baseline_non_trials = sr[:, baseline_non_trial_windows_in_frames]

# Smooth
smoothing_window = int(0.25 * 120)  # in frames
step_window = 10  # in frames

sr_trials_smoothed = []
sr_baseline_trials_smoothed = []

for trial in np.arange(number_of_trial_pokes):
    sr_trials_smoothed.append(
        binning.rolling_window_with_step(sr_trials[:, trial, :], np.mean,
                                         smoothing_window, step_window))

    sr_baseline_trials_smoothed.append(
        binning.rolling_window_with_step(sr_baseline_trials[:,
                                                            trial, :], np.mean,
                                         smoothing_window, step_window))

sr_non_trials_smoothed = []
sr_baseline_non_trials_smoothed = []

for non_trial in np.arange(number_of_non_trial_pokes):
    sr_non_trials_smoothed.append(
        binning.rolling_window_with_step(sr_non_trials[:,
                                                       non_trial, :], np.mean,
                                         smoothing_window, step_window))
avg_voltage_around_trials = np.mean(voltage_around_trials, axis=0)

_ = plt.imshow(np.flipud(avg_voltage_around_trials), aspect='auto')
# </editor-fold>

# -------------------------------------------------
# <editor-fold desc="LOOK AT ALL THE NEURONS AROUND THE POKE EVENT">

smooth_time = 0.5
smooth_frames = smooth_time * 120

#data = avg_firing_rate_around_random
data = avg_firing_rate_around_suc_trials

t = binning.rolling_window_with_step(data, np.mean, smooth_frames,
                                     int(smooth_frames / 3))
tn = preproc.normalize(t, norm='l1', axis=0)

tn = np.asarray(t)
for i in np.arange(len(t)):
    tn[i, :] = binning.scale(t[i], 0, 1)

y_positions = template_info['position Y'].values
position_sorted_indices = np.argsort(y_positions)

regions_pos = list(const.BRAIN_REGIONS.values())
region_lines = []
for rp in regions_pos:
    region_lines.append(
        sync_funcs.find_nearest(
            y_positions[position_sorted_indices] * const.POSITION_MULT, rp)[0])
示例#5
0
        (avg_lfps_around_event[channel, :] -
         random_triggered_lfps_mean[channel, :]) /
        (random_triggered_lfps_std[channel, :] / np.sqrt(len(events))))
z_score = np.array(z_score)

time_x_axis = np.arange(-window_timepoints / const.SAMPLING_FREQUENCY,
                        window_timepoints / const.SAMPLING_FREQUENCY,
                        1 / const.SAMPLING_FREQUENCY)

_ = plt.plot(time_x_axis, z_score.T)
_ = plt.plot(time_x_axis, cdf.space_data(z_score, 3).T)

_ = plt.plot(
    time_x_axis[:-300:100],
    cdf.space_data(
        binning.rolling_window_with_step(z_score, np.mean, 300, 100), 3).T)
_ = plt.plot(time_x_axis[:-300:100],
             binning.rolling_window_with_step(z_score, np.mean, 300, 100).T)

index = 0
sv.graph_pane(globals(), 'index', 'z_factor', 'time_x_axis')

t = np.argwhere(np.abs(z_score) > 5)
channels_with_large_changes = np.unique(t[:, 0])

smooth_factor = 1000
#smoothed_z_score = binning.rolling_window_with_step(z_score, np.mean, smooth_factor, smooth_factor)
smoothed_avg_lfps_around_event = binning.rolling_window_with_step(
    avg_lfps_around_event, np.mean, smooth_factor, smooth_factor)
smoothed_time_x_axis = np.arange(-window_timepoints / const.SAMPLING_FREQUENCY,
                                 window_timepoints / const.SAMPLING_FREQUENCY,
示例#6
0
speeds = body_velocities_polar[:, 0].copy()
acc = np.diff(speeds)
for i in np.arange(len(acc)):
    if speeds[i] > 200:
        speeds[i] = np.mean(speeds[i - 10:i + 10])

# Get the 250ms averages of firing rates for all neurons and of the speed (body_velocity_polar[0] of the animal
video_frame_spike_rates_filename = join(
    kilosort_folder, 'firing_rate_with_video_frame_window.npy')
spike_rates = np.load(video_frame_spike_rates_filename)
spike_rates_0p25 = np.load(
    join(kilosort_folder, 'firing_rate_with_0p25s_window.npy'))

num_of_frames_to_average = 0.25 / (1 / 120)
speeds_0p25 = binning.rolling_window_with_step(speeds, np.mean,
                                               num_of_frames_to_average,
                                               num_of_frames_to_average)
speeds_0p25[0] = 0
# </editor-fold>

#  -------------------------------------------------
# <editor-fold desc="CREATE THE MUTUAL INFORMATION MEASURE BETWEEN SPEED AND THE FIRING RATES OF ALL NEURONS (FOR THE WHOLE RECORDING)">
# Calculate the mutual information between the speed and all firing rates (for the whole of the experiment)
# using the lnc code (a Kraskov with some local non-uniform correction for better very high correlations)

n = 0
mutual_infos_spikes_vs_speed = []
for rate in spike_rates_0p25:
    mutual_infos_spikes_vs_speed.append(
        MI.mi_LNC([rate.tolist(), speeds_0p25],
                  k=10,
body_velocities_polar = np.array([
    np.sqrt(
        np.power(body_velocities[:, 0], 2) +
        np.power(body_velocities[:, 1], 2)), 180 * (1 / np.pi) *
    np.arctan2(body_velocities[:, 1], body_velocities[:, 0])
]).transpose()

# Correct the speeds by removing the very large spikes (replace them with the average speeds around them)
speeds = body_velocities_polar[:, 0].copy()
acc = np.diff(speeds)
for i in np.arange(len(acc)):
    if speeds[i] > 200:
        speeds[i] = np.mean(speeds[i - 10:i + 10])

num_of_frames_to_average = 1 / (1 / 120)
speeds_smoothed = binning.rolling_window_with_step(speeds, np.mean,
                                                   num_of_frames_to_average, 1)
speeds_smoothed[0] = 0

speeds_smoothed = np.array(speeds_smoothed)
rest_speed_threshold_cm_per_sec = 15
rest_move_periods = np.zeros(len(speeds_smoothed))
rest_move_periods[np.where(
    speeds_smoothed > rest_speed_threshold_cm_per_sec)] = 1

# plt.plot(rest_move_periods)
# plt.plot((speeds_smoothed - np.nanmin(speeds_smoothed)) / np.nanmax(speeds_smoothed - np.nanmin(speeds_smoothed)))

# Check the calculated speeds on the video
frame = 1
sv.image_sequence(globals(), 'frame', 'full_video_file')
template_info = pd.read_pickle(join(kilosort_folder, 'template_info.df'))

time_to_bin = 0.1
frames_to_bin = time_to_bin * 120


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


# -------------------------------------------------
# <editor-fold desc="CREATE MATRIX, PCA IT AND RUN T-SNE ON THE PCs">

tsne_folder = join(tsne_folder_base, 'All_spikes_100msbin_count_top40PCs_6Kiter')

spike_rates_max_0p1 = binning.rolling_window_with_step(spike_rates_per_video_frame, np.max, frames_to_bin,
                                                       frames_to_bin)

spike_rates_binary_0p1 = np.copy(spike_rates_max_0p1)
spike_rates_binary_0p1[spike_rates_max_0p1 > 0] = 1
spike_rates_binary_0p1 = spike_rates_binary_0p1.transpose()

pca_sr_bin_0p1 = PCA()
pcs_ar_bin_0p1 = pca_sr_bin_0p1.fit_transform(spike_rates_binary_0p1)

number_of_top_pcs = 40
num_dims = 2
perplexity = 100
theta = 0.3
eta = 200
exageration = 12
iterations = 4000
# </editor-fold>
# -------------------------------------------------

# -------------------------------------------------
# <editor-fold desc="PCA THE 3D ARRAY (VIDEO) AND RUN T-SNE">

grayscale_resized_video_array = np.load(
    join(subsumpled_video_folder, 'grayscale_resized_cropped_video_array.npy'))

#   Flatten
grayscale_resized_video_array = \
    grayscale_resized_video_array.reshape((num_of_frames, video_resolution[0] * video_resolution[1]))

#   Smooth over time to 100ms per frame
grayscale_resized_video_array_frame_smoothed = np.transpose(
    binning.rolling_window_with_step(grayscale_resized_video_array.transpose(),
                                     np.mean, 12, 12))
np.save(
    join(subsumpled_video_folder,
         'grayscale_resized_cropped_video_array_frame_smoothed.npy'),
    grayscale_resized_video_array_frame_smoothed)

#   PCA
#pca_flat_video = PCA(n_components=100)
pca_flat_video = PCA()
pcs_flat_video = pca_flat_video.fit_transform(
    grayscale_resized_video_array_frame_smoothed)
pickle.dump(
    pca_flat_video,
    open(
        join(subsumpled_video_folder,
             'full_pca_of_greyscale_subsampled_video.pcl'), 'wb'))
orientation_categories = pd.cut(body_velocities_polar[:, 1], bins=20)

event_dataframes = ns_funcs.load_events_dataframes(events_folder,
                                                   sync_funcs.event_types)
file_to_save_to = join(spikes_folder,
                       'firing_rate_with_video_frame_window.npy')
template_info = pd.read_pickle(join(spikes_folder, 'template_info.df'))

spike_rates = np.load(file_to_save_to)
num_of_frames_to_average = 0.25 / (1 / 120)

spike_rates_0p25 = np.load(
    join(spikes_folder, 'firing_rate_with_0p25s_window.npy'))

speeds_0p25 = binning.rolling_window_with_step(body_velocities_polar[:, 0],
                                               np.mean,
                                               num_of_frames_to_average,
                                               num_of_frames_to_average)

mutual_infos_spikes_vs_speed = np.load(
    join(mutual_information_folder, 'mutual_infos_spikes_vs_speed.npy'))
shuffled = np.load(
    join(mutual_information_folder,
         'shuffled_mut_info_spike_rate_522_vs_speed.npy'))
mean_sh = np.mean(shuffled)
confidence_level = 0.999
confi_intervals = shuffled[int(
    (1. - confidence_level) / 2 * 1000)], shuffled[int(
        (1. + confidence_level) / 2 * 1000)]
plt.hist(mutual_infos_spikes_vs_speed,
         bins=np.logspace(np.log10(0.0001), np.log10(30), 50))
plt.hist(shuffled,
    correlated_neuron_indices[s] = np.squeeze(
        np.argwhere(mis[s] > mean_sh + confi_intervals[1]))
    if 'pb' in s:
        correlated_neuron_indices['n' + s] = np.squeeze(
            np.argwhere(mis['n' + s] > mean_sh + confi_intervals[1]))

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

# -------------------------------------------------
# <editor-fold desc="SIMPLE LINEAR REGRESSIONS">

# <editor-fold desc="SPEED REGRESSION V0">
X = spike_rates_0p25[correlated_neuron_indices['speed']].transpose()
X = spike_rates_0p25[522].transpose()
Y = binning.rolling_window_with_step(speeds, np.nanmean, 30, 30)[:-1]
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    Y,
                                                    test_size=0.2,
                                                    random_state=0)

regressor_speed = linear_model.LinearRegression(normalize=True)
regressor_speed.fit(np.transpose([X_train]), y_train)
print(regressor_speed.score(np.transpose([X_train]), y_train))

Y_pred = regressor_speed.predict(np.transpose([X_test]))
plt.plot(Y_pred)
plt.plot(y_test)
plt.scatter(np.diff(Y_pred), np.diff(y_test))
plt.plot([0, 10, 20, 30, 40], [0, 10, 20, 30, 40], c='k')
# </editor-fold>
# -------------------------------------------------
# <editor-fold desc="PREPARE THE DATA FOR THE MUTUAL INFORMATION CALC (AND THE FRAME WIDNOWS THAT THE PERIODS ARE IN)">

frame_windows_away_from_poke = np.empty(0)
frame_windows_away_from_poke_in_periods = []
duration_in_frames_of_travel_periods = []
for f in frame_regions_away_from_poke:
    period_frames = np.arange(f[0], f[1]).astype(np.int)
    frame_windows_away_from_poke_in_periods.append(period_frames)
    duration_in_frames_of_travel_periods.append(len(period_frames))
    frame_windows_away_from_poke = np.concatenate((frame_windows_away_from_poke, period_frames))
frame_windows_away_from_poke = frame_windows_away_from_poke.astype(np.int)


spike_rates_away_from_poke = spike_rates[:, frame_windows_away_from_poke]
spike_rates_away_from_poke_smoothed = binning.rolling_window_with_step(spike_rates_away_from_poke,
                                                                       np.mean, fr_smooth_frames, fr_smooth_frames)

distance_traveled_from_poke_smoothed = binning.rolling_window_with_step(distance_traveled_from_poke,
                                                                        np.mean, fr_smooth_frames, fr_smooth_frames)

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


# -------------------------------------------------
# <editor-fold desc="DO THE MIs (RUN ONCE)">
# NOTE The MIs were done with fr_smooth_time = 0.5
n = 0
mi_spike_rates_vs_distance_traveled_from_poke = []
for rate in spike_rates_away_from_poke_smoothed:
    mi_spike_rates_vs_distance_traveled_from_poke.append(MI.mi_LNC([rate.tolist(), distance_traveled_from_poke_smoothed],
示例#13
0
    correlated_neuron_indices[s] = np.squeeze(
        np.argwhere(mis[s] > mean_sh + confi_intervals[1]))
    if 'pb' in s:
        correlated_neuron_indices['n' + s] = np.squeeze(
            np.argwhere(mis['n' + s] > mean_sh + confi_intervals[1]))

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

# -------------------------------------------------
# <editor-fold desc="SIMPLE LINEAR REGRESSIONS">

# <editor-fold desc="SPEED REGRESSION">
X = spike_rates_0p25[correlated_neuron_indices['speed']].transpose()
Y = binning.rolling_window_with_step(speeds, np.nanmean, 30, 30)[:-1]
#X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

X_train = X[:10000, :]
y_train = Y[:10000]
X_test = X[10000:, :]
y_test = Y[10000:]

#   Linear
regressor_speed = linear_model.LinearRegression(normalize=True)
regressor_speed.fit(X_train, y_train)
print(regressor_speed.score(X_train, y_train))
print(regressor_speed.score(X_test, y_test))

Y_pred = regressor_speed.predict(X_test)
示例#14
0
correlated_neuron_indices_unique_npb_dtp = np.delete(correlated_neuron_indices['npb_dtp'],
                                                    np.argwhere(np.isin(correlated_neuron_indices['npb_dtp'],
                                                                        common_pb_npb_dtp_neuron_indices)))

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

# -------------------------------------------------
# <editor-fold desc="REGRESSION OF DISTANCE TO POKE WITH FIRING RATES FOR PATTERNED BEHAVIOUR NEURONS AND EPOCHS">

# First smooth the distance to poke and the firing rates with a small window and then smooth only the firing rates
# further denoting the amount of past time a classifier needs to use


spike_rates_patterned_behaviour_smoothed = \
    binning.rolling_window_with_step(spike_rates[:, windows_of_patterned_behaviour],
                                     np.mean, smoothing_frames, smoothing_frames) * 0.00833 * smoothing_frames
distance_to_poke_patterned_behaviour_smoothed = \
    binning.rolling_window_with_step(distances_rat_to_poke_all_frames[windows_of_patterned_behaviour],
                                     np.mean, smoothing_frames, smoothing_frames)


spike_rates_patterned_behaviour_smoothed = \
    binning.rolling_window_with_step(spike_rates_patterned_behaviour_smoothed, np.mean, fr_extra_smoothing_frames, 1)
distance_to_poke_patterned_behaviour_smoothed = \
    np.array(distance_to_poke_patterned_behaviour_smoothed[len(distance_to_poke_patterned_behaviour_smoothed)
             - spike_rates_patterned_behaviour_smoothed.shape[1]:])

distance_to_poke_patterned_behaviour_smoothed = preprocessing.StandardScaler().\
    fit_transform(np.reshape(distance_to_poke_patterned_behaviour_smoothed, (-1, 1))).reshape(-1)

# Then run mutliple times the regression leaving out some samples every time
示例#15
0
    analysis_folder, 'Images',
    'Tsne_of_40_top_PCs_of_0p1_count_spike_vectors.png')
tsne_pcs_count_image = plt.imread(tsne_pcs_count_image_file)
tsne_pcs_count_image_extent = [-31, 45, -45, 30]

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

# -------------------------------------------------
# <editor-fold desc="CREATE MATRIX OF COUNTS PER 100ms, PCA THIS AND RUN T-SNE">
tsne_folder = join(tsne_folder_base,
                   'All_spikes_100msbin_count_top40PCs_6Kiter')
time_to_bin = 0.1
frames_to_bin = time_to_bin * 120

spike_rates_count_0p1 = binning.rolling_window_with_step(
    spike_rates_per_video_frame, np.sum, frames_to_bin, frames_to_bin)

spike_rates_count_0p1 = spike_rates_count_0p1.transpose() * 0.00833

pca_sr_count_0p1 = PCA()
pcs_ar_count_0p1 = pca_sr_count_0p1.fit_transform(
    spike_rates_count_0p1).astype(np.int16)

number_of_top_pcs = 40
num_dims = 2
perplexity = 100
theta = 0.2
eta = 200
exageration = 12
iterations = 6000
random_seed = 1
    's': 'Successful',
    'ns': 'Not successful',
    'tb': 'Ball Touch',
    'r': 'Random'
}
avg_muae_around_event = np.load(
    join(results_folder, 'Lfp', 'Averages',
         'Muaes_around_{}.npy'.format(trial_type)))

regions_pos = np.array(list(const.BRAIN_REGIONS.values()))
pos_to_elect_factor = const_comm.NUMBER_OF_AP_CHANNELS_IN_BINARY_FILE / 8100
region_lines = binning.scale(regions_pos,
                             np.min(regions_pos) * pos_to_elect_factor,
                             np.max(regions_pos) * pos_to_elect_factor)

muae_smooth = binning.rolling_window_with_step(avg_muae_around_event, np.mean,
                                               40, 40)
muae_smooth = (muae_smooth - muae_smooth.min()) / (muae_smooth.max() -
                                                   muae_smooth.min())
_ = plt.figure(1)
plt.imshow(np.flipud(muae_smooth),
           aspect='auto',
           extent=[-8, 8, len(muae_smooth), 0])
plt.vlines(x=0, ymin=0, ymax=muae_smooth.shape[0] - 1)
plt.hlines(y=muae_smooth.shape[0] - region_lines,
           xmin=-8,
           xmax=8,
           linewidth=3,
           color='w')
plt.title('Rat = {}, Day from Imp. = {}, Event = {}, Trials = {}'\
          .format(const.rat_folder[3:], str(date_folder), events[trial_type],
                  str(len(trials[trial_type]))))
spike_info = pd.read_pickle(
    join(kilosort_folder, 'spike_info_after_cleaning.df'))
spike_rates = binning.spike_count_per_frame(template_info,
                                            spike_info,
                                            event_dataframes['ev_video'],
                                            sampling_freq,
                                            file_to_save_to=file_to_save_to)

# Using the frame based spikes rates do a rolling window to average a bit more
num_of_frames_to_average = 0.25 / (1 / 120)

spike_rates_0p25 = []
for n in np.arange(spike_rates.shape[0]):
    spike_rates_0p25.append(
        binning.rolling_window_with_step(spike_rates[n, :], np.mean,
                                         num_of_frames_to_average,
                                         num_of_frames_to_average))
spike_rates_0p25 = np.array(spike_rates_0p25)
np.save(join(kilosort_folder, 'firing_rate_with_0p25s_window.npy'),
        spike_rates_0p25)
# </editor-fold>
# ----------------------------------------------------------------------------------------------------------------------

# -------------------------------------------------
# <editor-fold desc="GET TIMES AND FRAMES OF SUCCESSFUL TRIALS">

video_frame_spike_rates_filename = join(
    kilosort_folder, 'firing_rate_with_video_frame_window.npy')
spike_rates = np.load(video_frame_spike_rates_filename)
camera_pulses, beam_breaks, sounds = \
    sync_funcs.get_time_points_of_events_in_sync_file(data_folder, clean=True,
示例#18
0
sv.image_sequence(globals(), 'frame', 'video_file')
# </editor-fold>

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

# -------------------------------------------------
# <editor-fold desc="USING THE GENERATED WINDOWS SMOOTH THE CORRESPONDING PARTS OF THE SPEED AND THE FIRING RATES">

#   Smooth speeds and spike rates for each patterned behaviour
windows_of_patterned_behaviour_list = windows_of_patterned_behaviour[0]
for k in np.arange(1, number_of_patterned_events):
    windows_of_patterned_behaviour_list = np.concatenate((windows_of_patterned_behaviour_list,
                                                          windows_of_patterned_behaviour[k]))

speeds_patterned_behaviour_0p25 = binning.rolling_window_with_step(speeds[windows_of_patterned_behaviour_list],
                                                                   np.mean, 30, 30)

distances_rat_to_poke_all_frames = np.sqrt(
    np.power(body_positions_normalised[:, 0] - poke_position[0], 2) +
    np.power(body_positions_normalised[:, 1] - poke_position[1], 2))

distance_to_poke_patterned_behaviour_0p25 = \
    binning.rolling_window_with_step(distances_rat_to_poke_all_frames[windows_of_patterned_behaviour_list],
                                     np.mean, 30, 30)

spike_rates_patterned_behaviour_all_frames = spike_rates[:, windows_of_patterned_behaviour_list]

spike_rates_patterned_behaviour_0p25 = \
    binning.rolling_window_with_step(spike_rates_patterned_behaviour_all_frames, np.mean, 30, 30)

#   Smooth speeds and spike rates for each non patterned trial
示例#19
0
# Make the spike rates using each frame as a binning window

#  Load the pre generated DataFrames for the event CSVs
event_dataframes = ns_funcs.load_events_dataframes(events_folder,
                                                   sync_funcs.event_types)
file_to_save_to = join(kilosort_folder,
                       'firing_rate_with_video_frame_window.npy')
template_info = pd.read_pickle(join(kilosort_folder, 'template_info.df'))

spike_info = pd.read_pickle(
    join(kilosort_folder, 'spike_info_after_cleaning.df'))
sampling_frequency = const.SAMPLING_FREQUENCY
spike_rates = binning.spike_count_per_frame(template_info,
                                            spike_info,
                                            event_dataframes['ev_video'],
                                            sampling_frequency,
                                            file_to_save_to=file_to_save_to)

# Using the frame based spikes rates do a rolling window to average a bit more
num_of_frames_to_average = 0.25 / (1 / 120)

spike_rates_0p25 = []
for n in np.arange(spike_rates.shape[0]):
    spike_rates_0p25.append(
        binning.rolling_window_with_step(spike_rates[n, :], np.mean,
                                         num_of_frames_to_average,
                                         num_of_frames_to_average))
spike_rates_0p25 = np.array(spike_rates_0p25)
np.save(join(kilosort_folder, 'firing_rate_with_0p25s_window.npy'),
        spike_rates_0p25)
示例#20
0
    np.argwhere(
        np.isin(correlated_neuron_indices['npb_dtp'],
                common_pb_npb_dtp_neuron_indices)))

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

# -------------------------------------------------
# <editor-fold desc="REGRESSION OF DISTANCE TO POKE WITH FIRING RATES FOR PATTERNED BEHAVIOUR NEURONS AND EPOCHS">

# First smooth the distance to poke and the firing rates with a small window and then smooth only the firing rates
# further denoting the amount of past time a classifier needs to use


spike_rates_patterned_behaviour_smoothed = \
    binning.rolling_window_with_step(spike_rates[:, windows_of_patterned_behaviour],
                                     np.mean, smoothing_frames, smoothing_frames) * 0.00833 * smoothing_frames
distance_to_poke_patterned_behaviour_smoothed = \
    binning.rolling_window_with_step(distances_rat_to_poke_all_frames[windows_of_patterned_behaviour],
                                     np.mean, smoothing_frames, smoothing_frames)


spike_rates_patterned_behaviour_smoothed = \
    binning.rolling_window_with_step(spike_rates_patterned_behaviour_smoothed, np.mean, fr_extra_smoothing_frames, 1)
distance_to_poke_patterned_behaviour_smoothed = \
    np.array(distance_to_poke_patterned_behaviour_smoothed[len(distance_to_poke_patterned_behaviour_smoothed)
             - spike_rates_patterned_behaviour_smoothed.shape[1]:])

distance_to_poke_patterned_behaviour_smoothed = preprocessing.StandardScaler().\
    fit_transform(np.reshape(distance_to_poke_patterned_behaviour_smoothed, (-1, 1))).reshape(-1)

# Then run mutliple times the regression leaving out some samples every time
示例#21
0
body_velocities_polar = np.array([
    np.sqrt(
        np.power(body_velocities[:, 0], 2) +
        np.power(body_velocities[:, 1], 2)), 180 * (1 / np.pi) *
    np.arctan2(body_velocities[:, 1], body_velocities[:, 0])
]).transpose()

# Get the 250ms averages of firing rates for all neurons and of the speed (body_velocity_polar[0] of the animal
video_frame_spike_rates_filename = join(
    spikes_folder, 'firing_rate_with_video_frame_window.npy')
spike_rates = np.load(video_frame_spike_rates_filename)
spike_rates_0p25 = np.load(
    join(spikes_folder, 'firing_rate_with_0p25s_window.npy'))

speeds_0p25 = binning.rolling_window_with_step(body_velocities_polar[:, 0],
                                               np.mean,
                                               num_of_frames_to_average,
                                               num_of_frames_to_average)

#  -------------------------------------------------
# CREATE THE MUTUAL INFORMATION MEASURE BETWEEN SPEED AND THE FIRING RATES OF ALL NEURONS (FOR THE WHOLE RECORDING)
#  -------------------------------------------------

# Calculate the mutual information between the speed and all firing rates (for the whole of the experiment)
# using the lnc code (a Kraskov with some local non-uniform correction for better very high correlations)
'''
n = 0
mutual_infos_spikes_vs_speed = []
for rate in spike_rates_0p25:
    mutual_infos_spikes_vs_speed.append(MI.mi_LNC([rate.tolist()[:const.BRAIN_DATA_UP_TO_QUARTER_SECOND], 
                                                                 speeds_0p25[:const.BRAIN_DATA_UP_TO_QUARTER_SECOND]],
                                        k=10, base=np.exp(1), alpha=0.4, intens=1e-10))
示例#22
0
    const.BRAIN_REGIONS,
    const.PROBE_DIMENSIONS,
    const.POSITION_MULT,
    template_info=template_info_decreasing_fr_neurons)

pd.to_pickle(template_info_decreasing_fr_neurons,
             join(poke_folder, 'ti_decreasing_neurons_on_non_trial_pokes.df'))

# -------------------------------------------------
# <editor-fold desc="LOOK AT ALL THE NEURONS AROUND THE POKE EVENT">

smooth_time = 0.5
smooth_frames = smooth_time * 120

t = binning.rolling_window_with_step(avg_firing_rate_around_suc_trials,
                                     np.mean, smooth_frames,
                                     int(smooth_frames / 3))
tn = preproc.normalize(t, norm='l1', axis=0)

tn = np.asarray(t)
for i in np.arange(len(t)):
    tn[i, :] = binning.scale(t[i], 0, 1)

y_positions = template_info['position Y'].values
position_sorted_indices = np.argsort(y_positions)

regions_pos = list(const.BRAIN_REGIONS.values())
region_lines = []
for rp in regions_pos:
    region_lines.append(
        sync_funcs.find_nearest(
示例#23
0
# <editor-fold desc="SEE LFPS IN RESPECT TO THE REFERENCE CHANNELS">

closest_reference_positions_bottom = np.arange(2, 72, 6) + 0.5
event_choise = 'ntp'
events = possible_events[event_choise]
num_of_events = len(events)

avg_lfps_around_event, std_lfps_around_event, time_axis = \
    tla_funcs.time_lock_raw_data(lfps, events, times_to_cut=[-window_time, window_time],
                                 sampling_freq=const.SAMPLING_FREQUENCY,
                                 baseline_time=[-window_time, -0.5 * window_time], sub_sample_freq=None,
                                 high_pass_cutoff=None, rectify=None, low_pass_cutoff=100,
                                 avg_reref=True, keep_trials=False)

smooth_factor = 1000
smoothed_avg_lfps_around_event = binning.rolling_window_with_step(
    avg_lfps_around_event, np.mean, smooth_factor, smooth_factor)
smoothed_time_x_axis = np.arange(-window_timepoints / const.SAMPLING_FREQUENCY,
                                 window_timepoints / const.SAMPLING_FREQUENCY,
                                 1 / const.SAMPLING_FREQUENCY * smooth_factor)

plt.figure(2)
plt.imshow(smoothed_avg_lfps_around_event, aspect='auto')
plt.hlines(y=closest_reference_positions_bottom,
           xmin=0,
           xmax=smoothed_avg_lfps_around_event.shape[1])
plt.hlines(y=np.arange(0, 72, 6) - 0.5,
           xmin=0,
           xmax=smoothed_avg_lfps_around_event.shape[1],
           color='r')

# </editor-fold>