def plot_crispness_for_parameters(selected_rows = None): ''' This function plots crispness for all the selected rows motion correction states. The idea is to compare crispness results :param selected_rows: analysis states for which crispness is required to be ploted :return: figure that is also saved ''' crispness_mean_original,crispness_corr_original, crispness_mean, crispness_corr = metrics.compare_crispness(selected_rows) total_states_number = len(selected_rows) fig, axes = plt.subplots(1,2) axes[0].set_title('Summary image = Mean') axes[0].plot(np.arange(1,total_states_number,1),crispness_mean_original) axes[0].plot(np.arange(1,total_states_number,1),crispness_mean) axes[0].legend(('Original', 'Motion_corrected')) axes[0].set_ylabel('Crispness') #axes[0].set_xlabel('#') axes[1].set_title('Summary image = Corr') axes[1].plot(np.arange(1,total_states_number,1),crispness_corr_original) axes[1].plot(np.arange(1,total_states_number,1),crispness_corr) axes[1].legend(('Original', 'Motion_corrected')) axes[1].set_ylabel('Crispness') #axes[0].set_xlabel('#') # Get output file paths index = selected_rows.iloc[0].name data_dir = 'data/interim/motion_correction/' step_index = db.get_step_index('motion_correction') file_name = db.create_file_name(step_index, index) output_meta_crispness = data_dir + f'meta/figures/crispness/{file_name}.png' fig.savefig(output_meta_crispness) return fig
def select_corr_pnr_threshold(mouse_row,parameters_source_extraction): ''' Plots the summary images correlation and pnr. Also the pointwise product between them (used in Caiman paper Zhou et al 2018) :param mouse_row: :param parameters_source_extraction: parameters that will be used for source extraction. the relevant parameter here are min_corr and min_pnr because the source extraction algorithm is initialized (initial cell templates) in all values that surpasses that threshold :return: max_combined, max_pnr, max_corr: threshold for corr*pnr, and corresponding values of corr and pnr ''' input_mmap_file_path = eval(mouse_row.loc['motion_correction_output'])['main'] # Load memory mappable input file if os.path.isfile(input_mmap_file_path): Yr, dims, T = cm.load_memmap(input_mmap_file_path) # logging.debug(f'{index} Loaded movie. dims = {dims}, T = {T}.') images = Yr.T.reshape((T,) + dims, order='F') else: logging.warning(f'{mouse_row.name} .mmap file does not exist. Cancelling') # Determine output paths step_index = db.get_step_index('motion_correction') data_dir = 'data/interim/source_extraction/trial_wise/' # Check if the summary images are already there gSig = parameters_source_extraction['gSig'][0] corr_npy_file_path, pnr_npy_file_path = fm.get_corr_pnr_path(mouse_row.name, gSig_abs=(gSig, gSig)) if corr_npy_file_path != None and os.path.isfile(corr_npy_file_path): # Already computed summary images logging.info(f'{mouse_row.name} Already computed summary images') cn_filter = np.load(corr_npy_file_path) pnr = np.load(pnr_npy_file_path) else: # Compute summary images t0 = datetime.datetime.today() logging.info(f'{mouse_row.name} Computing summary images') cn_filter, pnr = cm.summary_images.correlation_pnr(images[::1], gSig=parameters_source_extraction['gSig'][0], swap_dim=False) # Saving summary images as npy files corr_npy_file_path = data_dir + f'meta/corr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' pnr_npy_file_path = data_dir + f'meta/pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' with open(corr_npy_file_path, 'wb') as f: np.save(f, cn_filter) with open(pnr_npy_file_path, 'wb') as f: np.save(f, pnr) combination = cn_filter * pnr # this is as defined in Zhou et al 2018 (definition of R, P and L, eq 14) max_combined = np.argmax(combination) row = int(np.floor(max_combined / cn_filter.shape[1])) column = int(max_combined - row * cn_filter.shape[1]) max_corr = cn_filter[row, column] max_pnr = pnr[row, column] return max_combined, max_corr, max_pnr
def create_corr_pnr_histogram(mouse_row,parameters_source_extraction): ''' Returns histogram of summary images correlation and pnr :param mouse_row: :param parameters_source_extraction: parameters that will be used for source extraction. :return: histogram vector ''' input_mmap_file_path = eval(mouse_row.loc['motion_correction_output'])['main'] # Load memory mappable input file if os.path.isfile(input_mmap_file_path): Yr, dims, T = cm.load_memmap(input_mmap_file_path) # logging.debug(f'{index} Loaded movie. dims = {dims}, T = {T}.') images = Yr.T.reshape((T,) + dims, order='F') else: logging.warning(f'{mouse_row.name} .mmap file does not exist. Cancelling') # Determine output paths step_index = db.get_step_index('motion_correction') data_dir = 'data/interim/source_extraction/trial_wise/' # Check if the summary images are already there gSig = parameters_source_extraction['gSig'][0] corr_npy_file_path, pnr_npy_file_path = fm.get_corr_pnr_path(mouse_row.name, gSig_abs=(gSig, gSig)) if corr_npy_file_path != None and os.path.isfile(corr_npy_file_path): # Already computed summary images logging.info(f'{mouse_row.name} Already computed summary images') cn_filter = np.load(corr_npy_file_path) pnr = np.load(pnr_npy_file_path) else: # Compute summary images t0 = datetime.datetime.today() logging.info(f'{mouse_row.name} Computing summary images') cn_filter, pnr = cm.summary_images.correlation_pnr(images[::1], gSig=parameters_source_extraction['gSig'][0], swap_dim=False) # Saving summary images as npy files corr_npy_file_path = data_dir + f'meta/corr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' pnr_npy_file_path = data_dir + f'meta/pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' with open(corr_npy_file_path, 'wb') as f: np.save(f, cn_filter) with open(pnr_npy_file_path, 'wb') as f: np.save(f, pnr) corr_pos, corr_histogram = np.histogram(cn_filter,100) pnr_pos, pnr_histogram = np.histogram(pnr,100) return corr_pos, corr_histogram, pnr_pos, pnr_histogram
def get_fig_gSig_filt_vals(row, gSig_filt_vals): ''' Plot original cropped frame and several versions of spatial filtering for comparison :param row: analisis state row for which the filtering is computed :param gSig_filt_vals: array containing size of spatial filters that will be applyed :return: figure ''' output = row['cropping_output'] cropped_file = eval(output)['main'] m = cm.load(cropped_file) temp = cm.motion_correction.bin_median(m) N = len(gSig_filt_vals) fig, axes = plt.subplots(int(math.ceil((N + 1) / 2)), 2) axes[0, 0].imshow(temp, cmap='gray') axes[0, 0].set_title('unfiltered') axes[0, 0].axis('off') for i in range(0, N): gSig_filt = gSig_filt_vals[i] m_filt = [high_pass_filter_space(m_, (gSig_filt, gSig_filt)) for m_ in m] temp_filt = cm.motion_correction.bin_median(m_filt) axes.flatten()[i + 1].imshow(temp_filt, cmap='gray') axes.flatten()[i + 1].set_title(f'gSig_filt = {gSig_filt}') axes.flatten()[i + 1].axis('off') if N + 1 != axes.size: for i in range(N + 1, axes.size): axes.flatten()[i].axis('off') # Get output file paths index = row.name data_dir = 'data/interim/motion_correction/' step_index = db.get_step_index('motion_correction') file_name = db.create_file_name(step_index, index) output_meta_gSig_filt = data_dir + f'meta/figures/frame_gSig_filt/{file_name}.png' fig.savefig(output_meta_gSig_filt) return fig
def plot_corr_pnr_binary(mouse_row, corr_limits, pnr_limits, parameters_source_extraction, session_wise = False, alignment = False, equalization = False): ''' Plot 2 matrix of binary selected and not selected seeds for different corr_min and pnr_min :param mouse_row: analysis states data :param corr_limits: array of multiple values of corr_min to test :param pnr_limits: arrey of multiple values of pnr_min to test :param parameters_source_extraction: dictionary with parameters :return: figure pointer ''' input_mmap_file_path = eval(mouse_row.loc['motion_correction_output'])['main'] if alignment: input_mmap_file_path = eval(mouse_row.loc['alignment_output'])['main'] if equalization: input_mmap_file_path = eval(mouse_row['alignment_output'])['equalizing_output']['main'] # Load memory mappable input file if os.path.isfile(input_mmap_file_path): Yr, dims, T = cm.load_memmap(input_mmap_file_path) # logging.debug(f'{index} Loaded movie. dims = {dims}, T = {T}.') images = Yr.T.reshape((T,) + dims, order='F') else: logging.warning(f'{mouse_row.name} .mmap file does not exist. Cancelling') # Determine output paths step_index = db.get_step_index('motion_correction') data_dir = 'data/interim/source_extraction/trial_wise/' # Check if the summary images are already there gSig = parameters_source_extraction['gSig'][0] corr_npy_file_path, pnr_npy_file_path = fm.get_corr_pnr_path(mouse_row.name, gSig_abs=(gSig, gSig)) if corr_npy_file_path != None and os.path.isfile(corr_npy_file_path): # Already computed summary images logging.info(f'{mouse_row.name} Already computed summary images') cn_filter = np.load(corr_npy_file_path) pnr = np.load(pnr_npy_file_path) else: # Compute summary images t0 = datetime.datetime.today() logging.info(f'{mouse_row.name} Computing summary images') cn_filter, pnr = cm.summary_images.correlation_pnr(images[::1], gSig=parameters_source_extraction['gSig'][0], swap_dim=False) # Saving summary images as npy files corr_npy_file_path = data_dir + f'meta/corr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' pnr_npy_file_path = data_dir + f'meta/pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' with open(corr_npy_file_path, 'wb') as f: np.save(f, cn_filter) with open(pnr_npy_file_path, 'wb') as f: np.save(f, pnr) fig1 = plt.figure(figsize=(50, 50)) combined_image = cn_filter * pnr fig1, axes1 = plt.subplots(len(corr_limits), len(pnr_limits), sharex=True) fig2, axes2 = plt.subplots(len(corr_limits), len(pnr_limits), sharex=True) fig3, axes3 = plt.subplots(len(corr_limits), len(pnr_limits), sharex=True) ii=0 for min_corr in corr_limits: min_corr = round(min_corr,2) jj=0 for min_pnr in pnr_limits: min_pnr = round(min_pnr,2) # binary limit = min_corr * min_pnr axes1[ii, jj].imshow(combined_image> limit, cmap='binary') axes1[ii, jj].set_title(f'{min_corr}') axes1[ii, jj].set_ylabel(f'{min_pnr}') axes2[ii, jj].imshow(cn_filter > min_corr, cmap='binary') axes2[ii, jj].set_title(f'{min_corr}') axes2[ii, jj].set_ylabel(f'{min_pnr}') axes3[ii, jj].imshow(pnr> min_pnr, cmap='binary') axes3[ii, jj].set_title(f'{min_corr}') axes3[ii, jj].set_ylabel(f'{min_pnr}') jj=jj+1 ii=ii+1 fig_dir = 'data/interim/source_extraction/trial_wise/meta/' if session_wise: fig_dir = 'data/interim/source_extraction/session_wise/meta/' fig_name= fig_dir + f'figures/min_corr_pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}_comb.png' fig1.savefig(fig_name) fig_name= fig_dir + f'figures/min_corr_pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}_corr.png' fig2.savefig(fig_name) fig_name= fig_dir + f'figures/min_corr_pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}_pnr.png' fig3.savefig(fig_name) return fig1,fig2,fig3
def plot_corr_pnr(mouse_row, parameters_source_extraction): ''' Plots the summary images correlation and pnr. Also the pointwise product between them (used in Caiman paper Zhou et al 2018) :param mouse_row: :param parameters_source_extraction: parameters that will be used for source extraction. the relevant parameter here are min_corr and min_pnr because the source extraction algorithm is initialized (initial cell templates) in all values that surpasses that threshold :return: figure ''' input_mmap_file_path = eval(mouse_row.loc['motion_correction_output'])['main'] # Load memory mappable input file if os.path.isfile(input_mmap_file_path): Yr, dims, T = cm.load_memmap(input_mmap_file_path) # logging.debug(f'{index} Loaded movie. dims = {dims}, T = {T}.') images = Yr.T.reshape((T,) + dims, order='F') else: logging.warning(f'{mouse_row.name} .mmap file does not exist. Cancelling') # Determine output paths step_index = db.get_step_index('motion_correction') data_dir = 'data/interim/source_extraction/trial_wise/' # Check if the summary images are already there gSig = parameters_source_extraction['gSig'][0] corr_npy_file_path, pnr_npy_file_path = fm.get_corr_pnr_path(mouse_row.name, gSig_abs=(gSig, gSig)) if corr_npy_file_path != None and os.path.isfile(corr_npy_file_path): # Already computed summary images logging.info(f'{mouse_row.name} Already computed summary images') cn_filter = np.load(corr_npy_file_path) pnr = np.load(pnr_npy_file_path) else: # Compute summary images t0 = datetime.datetime.today() logging.info(f'{mouse_row.name} Computing summary images') cn_filter, pnr = cm.summary_images.correlation_pnr(images[::1], gSig=parameters_source_extraction['gSig'][0], swap_dim=False) # Saving summary images as npy files corr_npy_file_path = data_dir + f'meta/corr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' pnr_npy_file_path = data_dir + f'meta/pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.npy' with open(corr_npy_file_path, 'wb') as f: np.save(f, cn_filter) with open(pnr_npy_file_path, 'wb') as f: np.save(f, pnr) fig = plt.figure(figsize=(15, 15)) min_corr = round(parameters_source_extraction['min_corr'], 2) min_pnr = round(parameters_source_extraction['min_pnr'], 1) max_corr = round(cn_filter.max(), 2) max_pnr= 20 # continuous cmap = 'viridis' fig, axes = plt.subplots(1, 3, sharex=True) corr_fig = axes[0].imshow(np.clip(cn_filter,min_corr,max_corr), cmap=cmap) axes[0].set_title('Correlation') fig.colorbar(corr_fig, ax=axes[0]) pnr_fig = axes[1].imshow(np.clip(pnr,min_pnr,max_pnr), cmap=cmap) axes[1].set_title('PNR') fig.colorbar(pnr_fig, ax=axes[1]) combined = cn_filter*pnr max_combined = 10 min_combined = np.min(combined) corr_pnr_fig = axes[2].imshow(np.clip(cn_filter*pnr,min_combined,max_combined), cmap=cmap) axes[2].set_title('Corr * PNR') fig.colorbar(corr_pnr_fig, ax=axes[2]) fig_dir = 'data/interim/source_extraction/trial_wise/meta/' fig_name= fig_dir + f'figures/corr_pnr/{db.create_file_name(3, mouse_row.name)}_gSig_{gSig}.png' fig.savefig(fig_name) return fig