def plotColoredPatchOnPatchBorders(self,patch,patches_dict, desired_patch_names,plotAxis=None, patch_colors={-1:"#0000ff",1:"#ff0000"}, txt_colors={True:"#00cc00",False:"#F09000"}, default_color="#000000"): if not plotAxis: f,plotAxis = plt.subplots(1,1) plotAxis.invert_yaxis() plotAxis.set_aspect('equal') patch_array = ni.binary_erosion(patch.array,iterations=2) patch_hex = patch_colors.get(patch.sign,default_color) patch_cmap = get_cmap_from_hex(patch_hex) patch = RetinotopicMapping.Patch(ni.zoom(patch_array,1,order=0),patch.sign) plotAxis.imshow(patch.getSignedMask(),vmax=1,vmin=-1, interpolation='nearest',cmap=patch_cmap,alpha=0.2) for key,patch in patches_dict.iteritems(): patch_color = patch_colors.get(patch.sign,default_color) currArray = ni.binary_erosion(patch.array,iterations=2) pt.plot_mask_borders(currArray, plotAxis=plotAxis, color=patch_color, alpha=0.6) text_color = txt_colors.get((key in desired_patch_names), default_color) plotAxis.text(patch.getCenter()[1], patch.getCenter()[0], key, color=text_color, horizontalalignment='center', verticalalignment='center', fontsize=15, zorder=10, fontweight='bold') return plotAxis
curr_mask_dict = ia.get_masks(labeled=mask_labeled, keyPrefix='caiman_mask_{:03d}'.format(i), labelLength=5) for roi_key, roi_mask in curr_mask_dict.items(): final_roi_dict.update({roi_key: ia.WeightedROI(roi_mask * mask)}) print 'Total number of ROIs:', len(final_roi_dict) f = plt.figure(figsize=(15, 8)) ax1 = f.add_subplot(121) ax1.imshow(ia.array_nor(bg), vmin=0, vmax=0.5, cmap='gray', interpolation='nearest') colors1 = pt.random_color(masks.shape[0]) for i, mask in enumerate(masks): pt.plot_mask_borders(mask, plotAxis=ax1, color=colors1[i]) ax1.set_title('original ROIs') ax1.set_axis_off() ax2 = f.add_subplot(122) ax2.imshow(ia.array_nor(bg), vmin=0, vmax=0.5, cmap='gray', interpolation='nearest') colors2 = pt.random_color(len(final_roi_dict)) i = 0 for roi in final_roi_dict.values(): pt.plot_mask_borders(roi.get_binary_mask(), plotAxis=ax2, color=colors2[i]) i = i + 1
trace_type].value[roi_i] + add_to_trace _ = get_dff(traces=curr_sta, t_axis=t_axis, response_span=response_span, baseline_span=baseline_span) dff_trace_mean, dff_trace_std, dff_trace_sem, dff_mean = _ curr_tf = grating_n[29:33] tf_i = np.where(tf_lst == curr_tf)[0][0] curr_sf = grating_n[22:26] sf_i = np.where(sf_lst == curr_sf)[0][0] curr_dire = grating_n[38:41] dire_i = np.where(dire_lst == curr_dire)[0][0] ax = plt.Subplot(f, gs_in_dict[tf_i][sf_i * len(dire_lst) + dire_i]) f_color = pt.value_2_rgb(value=(dff_mean - dff_mean_min) / (dff_mean_max - dff_mean_min), cmap=face_cmap) # f_color = pt.value_2_rgb(value=dff_mean / dff_mean_max, cmap=face_cmap) # print f_color ax.set_axis_bgcolor(f_color) ax.set_xticks([]) ax.set_yticks([]) for sp in ax.spines.values(): sp.set_visible(False) ax.axhline(y=0, ls='--', color='#888888', lw=1) ax.axvspan(response_span[0], response_span[1], alpha=0.5, color='#888888',
def get_MPath(self): self.axes.clear() self.canvas.draw() self.button_MPath.setStyleSheet('QPushButton {color: #888888}') self.button_MPath.setEnabled(False) fnames = QFileDialog.getOpenFileNames( self, 'Choose Retinotopic Mapping Dictionary of TIFF/JCam file(s):', self.currMatchingFolder) fnames = list(fnames) fnames = [str(x) for x in fnames] try: if len(fnames) == 0: # no file is chosen print("no file is chosen! Setting matching map as None...") self.textbrowser_MPath.clear() self.MatchingVasMap = None self.MatchingVasMapRaw = None self.MatchingVasMapAfterChange = None elif len(fnames) == 1: # only one file is chosen filePath = fnames[0] if filePath[-3:] == 'pkl': # mapping dictionary pkl file self.trialDict = ft.loadFile(filePath) self.MatchingVasMap = pt.merge_normalized_images( [self.trialDict['vasculatureMap']]) self.MatchingVasMapRaw = self.trialDict['vasculatureMap'] self.textbrowser_MPath.setText(filePath) self.MatchingVasMapAfterChange = None elif filePath[-3:] == 'tif': # tiff file self.MatchingVasMap = pt.merge_normalized_images( [tf.imread(filePath)]) self.MatchingVasMapRaw = tf.imread(filePath) self.textbrowser_MPath.setText(filePath) self.MatchingVasMapAfterChange = None else: # raw binary file fileFolder, fileName = os.path.split(filePath) if 'JCamF' in fileName: currMap, _, _ = ft.importRawJCamF(filePath, column=1024, row=1024) self.MatchingVasMap = pt.merge_normalized_images( [currMap[0]]) self.MatchingVasMapRaw = currMap[0] self.textbrowser_MPath.setText(filePath) elif 'JCam' in fileName: currMap, _ = ft.importRawJCam(filePath) self.MatchingVasMap = pt.merge_normalized_images( [currMap[0]]) self.MatchingVasMapRaw = currMap[0] self.textbrowser_MPath.setText(filePath) else: print('Can not read matching map ' + filePath) self.textbrowser_MPath.clear() self.MatchingVasMap = None self.MatchingVasMapAfterChange = None else: # more than one file is chosen displayText = ';'.join(fnames) mapList = [] for i, filePath in enumerate(fnames): if filePath[-3:] == 'tif': # tiff file mapList.append(tf.imread(filePath)) else: # raw binary file fileFolder, fileName = os.path.split(filePath) if 'JCamF' in fileName: currMap, _, _ = ft.importRawJCamF(filePath, column=1024, row=1024) elif 'JCam' in fileName: currMap, _ = ft.importRawJCam(filePath) else: print('Can not read ' + filePath) mapList.append(currMap[0].astype(np.float32)) if len(mapList) == 0: print( "no file can be read! Setting matching map as None...") self.textbrowser_MPath.clear() self.MatchingVasMap = None self.MatchingVasMapRaw = None self.MatchingVasMapAfterChange = None else: self.MatchingVasMap = pt.merge_normalized_images( mapList, dtype=np.float32) self.MatchingVasMapRaw = pt.merge_normalized_images( mapList, dtype=np.float32, isFilter=False) self.textbrowser_MPath.setText(displayText) self.MatchingVasMapAfterChange = None except Exception as e: print(e, '\n\n') print('Can not load matching Map! Setting it as None...') self.textbrowser_MPath.clear() self.MatchingVasMap = None self.MatchingVasMapRaw = None self.MatchingVasMapAfterChange = None self.button_MPath.setEnabled(True) self.button_MPath.setStyleSheet('QPushButton {color: #000000}') self.setZero() self.currMatchingFolder = os.path.split(fnames[0])[0]
os.chdir(curr_folder) mov_file_name, mov_file_ext = os.path.splitext(mov_name) save_name = mov_file_name + '_detrend' + mov_file_ext mov = tf.imread(mov_name).astype(np.float32) height = mov.shape[1] width = mov.shape[2] roi = ia.generate_oval_mask((height, width), (height / 2, width / 2), int(height * 0.6), int(width * 0.6)) f = plt.figure(figsize=(10, 10)) ax = f.add_subplot(111) ax.imshow(mov[0, :, :], cmap='gray', interpolation='nearest') pt.plot_mask_borders(roi, plotAxis=ax) plt.show() mov_detrend, trend, amp, rvalue = hl.regression_detrend(mov, roi) f = plt.figure(figsize=(15, 4)) ax = f.add_subplot(111) ax.plot(trend) ax.set_title('trend') f = plt.figure(figsize=(15, 5)) ax1 = f.add_subplot(121) fig1 = ax1.imshow(amp) ax1.set_title('contribution') f.colorbar(fig1)
if pos_cor_loc[0][ind] < pos_cor_loc[1][ind]: roi_pairs.append([pos_cor_loc[0][ind], pos_cor_loc[1][ind]]) print(roi_pairs) roi_grps = merger_pairs(roi_pairs) print roi_grps cor_grps = [] for roi_grp in roi_grps: grp_traces = traces_subtracted[list(roi_grp)] grp_cors = np.corrcoef(grp_traces)[np.tril_indices(len(roi_grp), k=-1)] cor_grps.append(np.mean(grp_cors)) cor_grps = np.array(cor_grps) cor_scalars = [(c + 1) / 2 for c in cor_grps] print cor_scalars cor_colors = [pt.value_2_rgb(c, cmap='inferno') for c in cor_scalars] f_roi = plt.figure() ax_roi = f_roi.add_subplot(111) ax_roi.imshow(bg, vmin=0, vmax=0.5, cmap='gray', interpolation='nearest') for grp_ind, roi_grp in enumerate(roi_grps): for roi_ind in roi_grp: print roi_ind, cor_colors[grp_ind] pt.plot_mask_borders(masks[roi_ind], plotAxis=ax_roi, color=cor_colors[grp_ind]) plt.show() data_f.close()
# cell2_mask.plot_binary_mask_border(plotAxis=ax, color='#0000ff', borderWidth=1) # ax.set_title('red:'+cell1_name+'; blue:'+cell2_name) # plt.show() break if is_remove == 0: retain_cells.append(cell1_name) print cell1_name, ':', cell1_mask.get_binary_area(), ': retained' print '\ncells to be removed because of overlapping:' print '\n'.join(remove_cells) print '\ntotal number of reatined cells:', len(retain_cells) # plotting colors = pt.random_color(len(cells.keys())) bgImg = tf.imread(background_file_name) f = plt.figure(figsize=(10, 10)) ax = f.add_subplot(111) ax.imshow(ia.array_nor(bgImg), cmap='gray', vmin=0, vmax=0.5, interpolation='nearest') f2 = plt.figure(figsize=(10, 10)) ax2 = f2.add_subplot(111) ax2.imshow(np.zeros(bgImg.shape, dtype=np.uint8), vmin=0, vmax=1,
def run(): data_file_name = 'cells_refined.hdf5' background_file_name = "corrected_mean_projections.tif" save_folder = 'figures' overlap_threshold = 0.9 surround_limit = [1, 8] curr_folder = os.path.dirname(os.path.realpath(__file__)) os.chdir(curr_folder) if not os.path.isdir(save_folder): os.makedirs(save_folder) print 'reading cells file ...' data_f = h5py.File(data_file_name, 'r') cell_ns = data_f.keys() cell_ns.sort() binary_mask_array = [] weight_mask_array = [] for cell_n in cell_ns: curr_roi = ia.ROI.from_h5_group(data_f[cell_n]['roi']) binary_mask_array.append(curr_roi.get_binary_mask()) weight_mask_array.append(curr_roi.get_weighted_mask()) data_f.close() binary_mask_array = np.array(binary_mask_array) weight_mask_array = np.array(weight_mask_array) print 'starting mask_array shape:', weight_mask_array.shape print 'getting total mask ...' total_mask = np.zeros( (binary_mask_array.shape[1], binary_mask_array.shape[2]), dtype=np.uint8) for curr_mask in binary_mask_array: total_mask = np.logical_or(total_mask, curr_mask) total_mask = np.logical_not(total_mask) plt.imshow(total_mask, interpolation='nearest') plt.title('total_mask') # plt.show() print 'getting and surround masks ...' binary_surround_array = [] for binary_center in binary_mask_array: curr_surround = np.logical_xor( ni.binary_dilation(binary_center, iterations=surround_limit[1]), ni.binary_dilation(binary_center, iterations=surround_limit[0])) curr_surround = np.logical_and(curr_surround, total_mask).astype(np.uint8) binary_surround_array.append(curr_surround) # plt.imshow(curr_surround) # plt.show() binary_surround_array = np.array(binary_surround_array) print "saving rois ..." center_areas = [] surround_areas = [] for mask_ind in range(binary_mask_array.shape[0]): center_areas.append(np.sum(binary_mask_array[mask_ind].flat)) surround_areas.append(np.sum(binary_surround_array[mask_ind].flat)) roi_f = h5py.File('rois_and_traces.hdf5') roi_f['masks_center'] = weight_mask_array roi_f['masks_surround'] = binary_surround_array roi_f.close() print 'minimum surround area:', min(surround_areas), 'pixels.' f = plt.figure(figsize=(10, 10)) ax_center = f.add_subplot(211) ax_center.hist(center_areas, bins=30) ax_center.set_title('roi center area distribution') ax_surround = f.add_subplot(212) ax_surround.hist(surround_areas, bins=30) ax_surround.set_title('roi surround area distribution') # plt.show() print 'plotting ...' colors = pt.random_color(weight_mask_array.shape[0]) bg = ia.array_nor(np.max(tf.imread(background_file_name), axis=0)) f_c_bg = plt.figure(figsize=(10, 10)) ax_c_bg = f_c_bg.add_subplot(111) ax_c_bg.imshow(bg, cmap='gray', vmin=0, vmax=0.5, interpolation='nearest') f_c_nbg = plt.figure(figsize=(10, 10)) ax_c_nbg = f_c_nbg.add_subplot(111) ax_c_nbg.imshow(np.zeros(bg.shape, dtype=np.uint8), vmin=0, vmax=1, cmap='gray', interpolation='nearest') f_s_nbg = plt.figure(figsize=(10, 10)) ax_s_nbg = f_s_nbg.add_subplot(111) ax_s_nbg.imshow(np.zeros(bg.shape, dtype=np.uint8), vmin=0, vmax=1, cmap='gray', interpolation='nearest') i = 0 for mask_ind in range(binary_mask_array.shape[0]): pt.plot_mask_borders(binary_mask_array[mask_ind], plotAxis=ax_c_bg, color=colors[i], borderWidth=1) pt.plot_mask_borders(binary_mask_array[mask_ind], plotAxis=ax_c_nbg, color=colors[i], borderWidth=1) pt.plot_mask_borders(binary_surround_array[mask_ind], plotAxis=ax_s_nbg, color=colors[i], borderWidth=1) i += 1 # plt.show() print 'saving figures ...' pt.save_figure_without_borders(f_c_bg, os.path.join(save_folder, '2P_ROIs_with_background.png'), dpi=300) pt.save_figure_without_borders(f_c_nbg, os.path.join( save_folder, '2P_ROIs_without_background.png'), dpi=300) pt.save_figure_without_borders(f_s_nbg, os.path.join( save_folder, '2P_ROI_surrounds_background.png'), dpi=300) f.savefig(os.path.join(save_folder, 'roi_area_distribution.pdf'), dpi=300)
def run(): isSave = True filter_sigma = 2. # parameters only used if filter the rois thr_high = 0.0 thr_low = 0.1 bg_fn = "corrected_mean_projections.tif" save_folder = 'figures' curr_folder = os.path.dirname(os.path.realpath(__file__)) os.chdir(curr_folder) data_f = h5py.File('caiman_segmentation_results.hdf5') masks = data_f['masks'].value data_f.close() bg = ia.array_nor(np.max(tf.imread(bg_fn), axis=0)) final_roi_dict = {} roi_ind = 0 for i, mask in enumerate(masks): mask_dict = hl.threshold_mask_by_energy(mask, sigma=filter_sigma, thr_high=thr_high, thr_low=thr_low) for mask_roi in mask_dict.values(): final_roi_dict.update({'roi_{:04d}'.format(roi_ind): mask_roi}) roi_ind += 1 print 'Total number of ROIs:', len(final_roi_dict) f = plt.figure(figsize=(15, 8)) ax1 = f.add_subplot(121) ax1.imshow(bg, vmin=0, vmax=0.5, cmap='gray', interpolation='nearest') colors1 = pt.random_color(masks.shape[0]) for i, mask in enumerate(masks): pt.plot_mask_borders(mask, plotAxis=ax1, color=colors1[i]) ax1.set_title('original ROIs') ax1.set_axis_off() ax2 = f.add_subplot(122) ax2.imshow(ia.array_nor(bg), vmin=0, vmax=0.5, cmap='gray', interpolation='nearest') colors2 = pt.random_color(len(final_roi_dict)) i = 0 for roi in final_roi_dict.values(): pt.plot_mask_borders(roi.get_binary_mask(), plotAxis=ax2, color=colors2[i]) i = i + 1 ax2.set_title('filtered ROIs') ax2.set_axis_off() # plt.show() if isSave: if not os.path.isdir(save_folder): os.makedirs(save_folder) f.savefig(os.path.join(save_folder, 'caiman_segmentation_filtering.pdf'), dpi=300) cell_file = h5py.File('cells.hdf5', 'w') i = 0 for key, value in sorted(final_roi_dict.iteritems()): curr_grp = cell_file.create_group('cell{:04d}'.format(i)) curr_grp.attrs['name'] = key value.to_h5_group(curr_grp) i += 1 cell_file.close()
roi_f['masks_surround'] = binary_surround_array roi_f.close() print 'minimum surround area:', min(surround_areas), 'pixels.' f = plt.figure(figsize=(10, 10)) ax_center = f.add_subplot(211) ax_center.hist(center_areas, bins=30) ax_center.set_title('roi center area distribution') ax_surround = f.add_subplot(212) ax_surround.hist(surround_areas, bins=30) ax_surround.set_title('roi surround area distribution') plt.show() print 'plotting ...' colors = pt.random_color(weight_mask_array.shape[0]) bg = ia.array_nor(tf.imread('corrected_mean_projection.tif')) f_c_bg = plt.figure(figsize=(10, 10)) ax_c_bg = f_c_bg.add_subplot(111) ax_c_bg.imshow(bg, cmap='gray', vmin=0, vmax=0.5, interpolation='nearest') f_c_nbg = plt.figure(figsize=(10, 10)) ax_c_nbg = f_c_nbg.add_subplot(111) ax_c_nbg.imshow(np.zeros(bg.shape, dtype=np.uint8), vmin=0, vmax=1, cmap='gray', interpolation='nearest') f_s_nbg = plt.figure(figsize=(10, 10)) ax_s_nbg = f_s_nbg.add_subplot(111) ax_s_nbg.imshow(np.zeros(bg.shape, dtype=np.uint8),
def run(): # pixels, masks with center location within this pixel region at the image border will be discarded center_margin = [10, 20, 25, 10] # [top margin, bottom margin, left margin, right margin] # area range, range of number of pixels of a valid roi area_range = [150, 1000] # for the two masks that are overlapping, if the ratio between overlap and the area of the smaller mask is larger than # this value, the smaller mask will be discarded. overlap_thr = 0.2 save_folder = 'figures' data_file_name = 'cells.hdf5' save_file_name = 'cells_refined.hdf5' background_file_name = "corrected_mean_projections.tif" curr_folder = os.path.dirname(os.path.realpath(__file__)) os.chdir(curr_folder) if not os.path.isdir(save_folder): os.makedirs(save_folder) # read cells dfile = h5py.File(data_file_name) cells = {} for cellname in dfile.iterkeys(): cells.update({cellname:ia.WeightedROI.from_h5_group(dfile[cellname])}) print 'total number of cells:', len(cells) # get the names of cells which are on the edge edge_cells = [] for cellname, cellmask in cells.iteritems(): dimension = cellmask.dimension center = cellmask.get_center() if center[0] < center_margin[0] or \ center[0] > dimension[0] - center_margin[1] or \ center[1] < center_margin[2] or \ center[1] > dimension[1] - center_margin[3]: # cellmask.plot_binary_mask_border(color='#ff0000', borderWidth=1) # plt.title(cellname) # plt.show() edge_cells.append(cellname) print '\ncells to be removed because they are on the edges:' print '\n'.join(edge_cells) # remove edge cells for edge_cell in edge_cells: _ = cells.pop(edge_cell) # get dictionary of cell areas cell_areas = {} for cellname, cellmask in cells.iteritems(): cell_areas.update({cellname: cellmask.get_binary_area()}) # remove cellnames that have area outside of the area_range invalid_cell_ns = [] for cellname, cellarea in cell_areas.items(): if cellarea < area_range[0] or cellarea > area_range[1]: invalid_cell_ns.append(cellname) print "cells to be removed because they do not meet area criterion:" print "\n".join(invalid_cell_ns) for invalid_cell_n in invalid_cell_ns: cell_areas.pop(invalid_cell_n) # sort cells with their binary area cell_areas_sorted = sorted(cell_areas.items(), key=operator.itemgetter(1)) cell_areas_sorted.reverse() cell_names_sorted = [c[0] for c in cell_areas_sorted] # print '\n'.join([str(c) for c in cell_areas_sorted]) # get the name of cells that needs to be removed because of overlapping retain_cells = [] remove_cells = [] for cell1_name in cell_names_sorted: cell1_mask = cells[cell1_name] is_remove = 0 cell1_area = cell1_mask.get_binary_area() for cell2_name in retain_cells: cell2_mask = cells[cell2_name] cell2_area = cell2_mask.get_binary_area() curr_overlap = cell1_mask.binary_overlap(cell2_mask) if float(curr_overlap) / cell1_area > overlap_thr: remove_cells.append(cell1_name) is_remove = 1 print cell1_name, ':', cell1_mask.get_binary_area(), ': removed' # f = plt.figure(figsize=(10,10)) # ax = f.add_subplot(111) # cell1_mask.plot_binary_mask_border(plotAxis=ax, color='#ff0000', borderWidth=1) # cell2_mask.plot_binary_mask_border(plotAxis=ax, color='#0000ff', borderWidth=1) # ax.set_title('red:'+cell1_name+'; blue:'+cell2_name) # plt.show() break if is_remove == 0: retain_cells.append(cell1_name) print cell1_name, ':', cell1_mask.get_binary_area(), ': retained' print '\ncells to be removed because of overlapping:' print '\n'.join(remove_cells) print '\ntotal number of reatined cells:', len(retain_cells) # plotting colors = pt.random_color(len(cells.keys())) bgImg = ia.array_nor(np.max(tf.imread(background_file_name), axis=0)) f = plt.figure(figsize=(10, 10)) ax = f.add_subplot(111) ax.imshow(ia.array_nor(bgImg), cmap='gray', vmin=0, vmax=0.5, interpolation='nearest') f2 = plt.figure(figsize=(10, 10)) ax2 = f2.add_subplot(111) ax2.imshow(np.zeros(bgImg.shape, dtype=np.uint8), vmin=0, vmax=1, cmap='gray', interpolation='nearest') i = 0 for retain_cell in retain_cells: cells[retain_cell].plot_binary_mask_border(plotAxis=ax, color=colors[i], borderWidth=1) cells[retain_cell].plot_binary_mask_border(plotAxis=ax2, color=colors[i], borderWidth=1) i += 1 # plt.show() # save figures pt.save_figure_without_borders(f, os.path.join(save_folder, '2P_refined_ROIs_with_background.png'), dpi=300) pt.save_figure_without_borders(f2, os.path.join(save_folder, '2P_refined_ROIs_without_background.png'), dpi=300) # save h5 file save_file = h5py.File(save_file_name, 'w') i = 0 for retain_cell in retain_cells: print retain_cell, ':', cells[retain_cell].get_binary_area() currGroup = save_file.create_group('cell' + ft.int2str(i, 4)) currGroup.attrs['name'] = retain_cell roiGroup = currGroup.create_group('roi') cells[retain_cell].to_h5_group(roiGroup) i += 1 for attr, value in dfile.attrs.iteritems(): save_file.attrs[attr] = value save_file.close() dfile.close()
for patch_n, patch in patches_to_show.items(): if patch.sign == 1: color = '#ff0000' elif patch.sign == -1: color = '#0000ff' else: color = '#000000' zoom = float(vasmap.shape[0]) / patch.array.shape[0] mask = ni.binary_erosion(patch.array, iterations=1) pt.plot_mask_borders(mask, plotAxis=ax, color=color, zoom=zoom, borderWidth=2) if isPlotName: cen = patch.getCenter() ax.text(cen[1] * zoom, cen[0] * zoom, patch_n, va='center', ha='center', color=color) ax.set_axis_off() plt.show()