## normalize activity within trial and for each neuron activity_normalized = np.zeros((activity.shape)) for j in range(activity.shape[0]): for i in range(0, len(timeline_1) - 1): activity_segment = activity[j, int(timeline_1[i]):int(timeline_1[i + 1])] activity_segment = activity_segment - min(activity_segment) if max(activity_segment): activity_segment_normalized = activity_segment / max( activity_segment) activity_normalized[j, int(timeline_1[i]):int(timeline_1[ i + 1])] = activity_segment_normalized neural_activity1 = activity[1:, :] corr_matrix1 = stats.corr_matrix(neural_activity=neural_activity1) neural_activity1 = activity_normalized[1:, :] sparceness_mean = [] sparceness_std = [] for re_sf in bin_vector: reshape_neural_activity = np.reshape( neural_activity1[:, :int( int(neural_activity1.shape[1] / re_sf) * re_sf)], (neural_activity1.shape[0], int( neural_activity1.shape[1] / re_sf), re_sf)) resample_neural_activity = np.mean(reshape_neural_activity, axis=2) denominator = np.mean(np.multiply(resample_neural_activity, resample_neural_activity), axis=1)
reshape_behaviour = np.reshape( behaviour[:int(int(behaviour.shape[0] / re_sf) * re_sf)], (int(behaviour.shape[0] / re_sf), re_sf)) resample_beh1 = np.reshape( scipy.stats.mode(reshape_behaviour, axis=1)[0], reshape_behaviour.shape[0]) resample_timeline = timeline_1 / re_sf #%% vector = resample_neural_activity_mean[:, :int(timeline_1[40])] vector_beh = resample_beh1[:int(resample_timeline[40])] neural_activity_new = [] corr_matrix = [] for i in range(6): neural_activity_new.append(vector[:, np.where(vector_beh == i)]) corr_matrix.append( stats.corr_matrix(neural_activity=neural_activity_new[i][:, 0, :])) #corr_matrix.append(stats.cov_matrix(neural_activity = neural_activity_new[i][:,0,:])) #%% correlation_behaviour_path = os.environ['PROJECT_DIR'] + 'neural_analysis/data/process/figures/' \ 'correlation_matrix_task_mouse_'+f'{mouse}'+'_session_'+f'{session}'+\ '_binsize_'+f'{re_sf}'+'.png' figs.plot_correlation_matrix_behaviour(corr_matrix_list=corr_matrix, path_save=correlation_behaviour_path, title=task) #%% Statistics of correlation values correlation_stats_path = os.environ['PROJECT_DIR'] + 'neural_analysis/data/process/figures/' \ 'correlation_statistics_mouse_'+f'{mouse}'+'_session_'\ +f'{session}'+'_binsize_'+f'{re_sf}'+'.png'
neural_activity1_days = [] time_length = np.diff(timeline_1) for i in range(0,42,2): trial_matrix = neural_activity1[:,int(timeline_1[i]):int(timeline_1[i]) + int(time_length[i])] neural_activity1_days.append(trial_matrix) neural_activity1_resting_days = [] for i in range(1,42,2): trial_matrix = neural_activity1[:,int(timeline_1[i]):int(timeline_1[i]) + int(time_length[i])] neural_activity1_resting_days.append(trial_matrix) #%% corr_matrix_days = [] for i in range(21): corr_matrix_days.append(stats.corr_matrix(neural_activity = neural_activity1_days[i])) #corr_matrix_days.append(stats.cov_matrix(neural_activity = neural_activity1_days[i])) corr_matrix_resting_days = [] for i in range(21): corr_matrix_resting_days.append(stats.corr_matrix(neural_activity = neural_activity1_resting_days[i])) #corr_matrix_resting_days.append(stats.cov_matrix(neural_activity = neural_activity1_resting_days[i])) #%% objects_fig_path = os.environ['PROJECT_DIR'] + 'neural_analysis/data/process/figures/' \ 'correlation_with_object_position_'+f'{mouse}'+\ '_session_'+f'{session}'+'_binsize_'+f'{re_sf}'+'.png' figs.plot_correlation_statistics_objects(corr_matrix1=corr_matrix_days, corr_matrix2=corr_matrix_resting_days, overlapping_matrix=overlapping_matrix, path_save=objects_fig_path,
if max(activity_segment): activity_segment_normalized = activity_segment / max( activity_segment) activity_normalized[j, int(timeline_3[i]):int(timeline_3[ i + 1])] = activity_segment_normalized neural_activity3 = activity_normalized[1:, :] #neural_activity2 = activity[1:,:] delete_list = [] sum_activity = np.sum(neural_activity3, axis=1) for i in range(neural_activity3.shape[0]): if sum_activity[i] == 0: delete_list.append(i) for i in delete_list: neural_activity3 = np.delete(neural_activity3, i, 0) corr_matrix3 = stats.corr_matrix(neural_activity=neural_activity3) eigenvalues3, eigenvectors3 = stats.compute_PCA(corr_matrix=corr_matrix3) proj3 = stats.PCA_projection(neural_activity=neural_activity3, eigenvalues=eigenvalues3, eigenvectors=eigenvectors3, n_components=6) behaviour = np.load(behaviour_dir + beh_file_name_2) c = np.linspace(0, 20, len(behaviour)) elements3 = [] testing_3 = [] color3 = [] for i in range(6): vector = proj3[:, :int(timeline_3[40])] vector_beh = behaviour[:int(timeline_3[40])] elements3.append(vector[:, np.where(vector_beh == i)])
):int(resample_timeline[i]) + int(time_length[i])] neural_activity1_days.append(trial_matrix) neural_activity1_resting_days = [] for i in range(1, 42, 2): trial_matrix = neural_activity1[:, int(resample_timeline[i] ):int(resample_timeline[i]) + int(time_length[i])] neural_activity1_resting_days.append(trial_matrix) corr_matrix_days = [] for i in range(21): corr_matrix_days.append( stats.corr_matrix(neural_activity=neural_activity1_days[i])) corr_matrix_resting_days = [] for i in range(21): corr_matrix_resting_days.append( stats.corr_matrix(neural_activity=neural_activity1_resting_days[i])) #%% plotting corr_evol_path = os.environ['PROJECT_DIR'] + 'neural_analysis/data/process/figures/' \ 'correlation_with_resting_evoluction_'+f'{mouse}'+\ '_session_'+f'{session}'+'_binsize_'+f'{re_sf}'+'.png' figs.plot_correlation_with_resting_evolution( corr_matrix1=corr_matrix_days, corr_matrix2=corr_matrix_resting_days,
time_file_session_1 = 'mouse_'+ f'{mouse}'+'_session_'+ f'{session}' +'_trial_1_v'+ f'{decoding_v}'+'.1.'+f'{1}'+\ '.'+f'{0}'+ '.pkl' beh_file_name_1 = 'mouse_' + f'{mouse}' + '_session_' + f'{session}' + '_event_' + f'{re_sf}' + '.npy' ##load activity and timeline activity = np.load(file_directory + file_name_session_1) timeline_file = open(timeline_file_dir + time_file_session_1, 'rb') timeline_info = pickle.load(timeline_file) ##normalize neural activity neural_activity1, timeline_1 = stats.normalize_neural_activity( activity=activity, timeline=timeline_info) ##downsample neural activity resample_neural_activity_mean, resample_neural_activity_std = stats.resample_matrix( neural_activity=neural_activity1, re_sf=re_sf) correlation_matrix.append( stats.corr_matrix(neural_activity=resample_neural_activity_mean)) #cov_matrix.append(stats.cov_matrix(neural_activity = resample_neural_activity_mean)) task_list.append(task) #%% plotting corr matrix for all conditions correlation_fig_path = os.environ['PROJECT_DIR'] + 'neural_analysis/data/process/figures/' \ 'correlation_matrix_mouse_'+f'{mouse}'+'_session_'+f'{session}'+\ '_binsize_'+f'{re_sf}'+'.png' figs.plot_correlation_matrix_conditions(matrix_list=correlation_matrix, save_path=correlation_fig_path, title='CorrelationMatrix', conditions=task_list) #cov_fig_path = os.environ['PROJECT_DIR'] + 'neural_analysis/data/process/figures/' \ # 'cov_matrix_mouse_'+f'{mouse}'+'_session_'+f'{session}'+\ # '_binsize_'+f'{re_sf}'+'.png'