def nb_plot_contour(image, A, d1, d2, thr=0.995, face_color=None, line_color='black', alpha=0.4, line_width=2, **kwargs): '''Interactive Equivalent of plot_contours for ipython notebook Parameters ----------- A: np.ndarray or sparse matrix Matrix of Spatial components (d x K) Image: np.ndarray (2D) Background image (e.g. mean, correlation) d1,d2: floats dimensions os image thr: scalar between 0 and 1 Energy threshold for computing contours (default 0.995) display_number: Boolean Display number of ROIs if checked (default True) max_number: int Display the number for only the first max_number components (default None, display all numbers) cmap: string User specifies the colormap (default None, default colormap) ''' p = nb_imshow(image, cmap='jet') center = com(A, d1, d2) p.circle(center[:, 1], center[:, 0], size=10, color="black", fill_color=None, line_width=2, alpha=1) coors = plot_contours(coo_matrix(A), image, thr=thr) pl.close() # cc=coors[0]['coordinates']; cc1 = [np.clip(cor['coordinates'][:, 0], 0, d2) for cor in coors] cc2 = [np.clip(cor['coordinates'][:, 1], 0, d1) for cor in coors] p.patches(cc1, cc2, alpha=.4, color=face_color, line_color=line_color, line_width=2, **kwargs) return p
def plot_neuron_contour(A, N, n, n_group, Cn, save_path): number = 0 number1 = 0 for i in range(n_group): plt.figure() vmax = np.percentile(Cn, 97) vmin = np.percentile(Cn, 5) plt.imshow(Cn, interpolation='None', vmax=vmax, vmin=vmin, cmap=plt.cm.gray) plt.title('Neurons location') d1, d2 = np.shape(Cn) cm1 = com(A.copy().reshape((N, -1), order='F').transpose(), d1, d2) colors = 'yellow' for j in range(n): index = mat[i, j] print(index) img = A[index] img1 = img.copy() #img1[img1<np.percentile(img1[img1>0],15)] = 0 #img1 = connected_components(img1) img2 = np.multiply(img, img1) contours = measure.find_contours(img2, 0.5)[0] #img2=img.copy() img2[img2 == 0] = np.nan if index != -1: plt.plot(contours[:, 1], contours[:, 0], linewidth=1, color=colorsets[np.mod(number, 9)]) plt.text(cm1[index, 1] + 0, cm1[index, 0] - 0, str(number), color=colors) number = number + 1 plt.savefig( os.path.join(root_dir, 'neuron_contour{}-{}.pdf'.format(number1, number - 1))) number1 = number
def manually_refine_components(Y, xxx_todo_changeme, A, C, Cn, thr=0.9, display_numbers=True, max_number=None, cmap=None, **kwargs): """Plots contour of spatial components against a background image and allows to interactively add novel components by clicking with mouse Parameters ----------- Y: ndarray movie in 2D (dx,dy): tuple dimensions of the square used to identify neurons (should be set to the galue of gsiz) A: np.ndarray or sparse matrix Matrix of Spatial components (d x K) Cn: np.ndarray (2D) Background image (e.g. mean, correlation) thr: scalar between 0 and 1 Energy threshold for computing contours (default 0.995) display_number: Boolean Display number of ROIs if checked (default True) max_number: int Display the number for only the first max_number components (default None, display all numbers) cmap: string User specifies the colormap (default None, default colormap) Returns -------- A: np.ndarray matrix A os estimated spatial component contributions C: np.ndarray array of estimated calcium traces """ (dx, dy) = xxx_todo_changeme if issparse(A): A = np.array(A.todense()) else: A = np.array(A) d1, d2 = np.shape(Cn) d, nr = np.shape(A) if max_number is None: max_number = nr x, y = np.mgrid[0:d1:1, 0:d2:1] pl.imshow(Cn, interpolation=None, cmap=cmap) coordinates = [] cm = com(A, d1, d2) Bmat = np.zeros((np.minimum(nr, max_number), d1, d2)) for i in range(np.minimum(nr, max_number)): pars = dict(kwargs) indx = np.argsort(A[:, i], axis=None)[::-1] cumEn = np.cumsum(A[:, i].flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn Bmat[i] = np.reshape(Bvec, np.shape(Cn), order='F') T = np.shape(Y)[-1] pl.close() fig = pl.figure() # ax = fig.add_subplot(111) ax = pl.gca() ax.imshow(Cn, interpolation=None, cmap=cmap, vmin=np.percentile(Cn[~np.isnan(Cn)], 1), vmax=np.percentile(Cn[~np.isnan(Cn)], 99)) for i in range(np.minimum(nr, max_number)): pl.contour(y, x, Bmat[i], [thr]) if display_numbers: for i in range(np.minimum(nr, max_number)): ax.text(cm[i, 1], cm[i, 0], str(i + 1)) A3 = np.reshape(A, (d1, d2, nr), order='F') while True: pts = fig.ginput(1, timeout=0) if pts != []: print(pts) xx, yy = np.round(pts[0]).astype(np.int) coords_y = np.array(list(range(yy - dy, yy + dy + 1))) coords_x = np.array(list(range(xx - dx, xx + dx + 1))) coords_y = coords_y[(coords_y >= 0) & (coords_y < d1)] coords_x = coords_x[(coords_x >= 0) & (coords_x < d2)] a3_tiny = A3[coords_y[0]:coords_y[-1] + 1, coords_x[0]:coords_x[-1] + 1, :] y3_tiny = Y[coords_y[0]:coords_y[-1] + 1, coords_x[0]:coords_x[-1] + 1, :] # y3med = np.median(y3_tiny,axis=-1) # y3_tiny = y3_tiny - y3med[...,np.newaxis] #y3_tiny = y3_tiny-np.median(y3_tiny,axis=-1) dy_sz, dx_sz = np.shape(a3_tiny)[:-1] y2_tiny = np.reshape(y3_tiny, (dx_sz * dy_sz, T), order='F') a2_tiny = np.reshape(a3_tiny, (dx_sz * dy_sz, nr), order='F') y2_res = y2_tiny - a2_tiny.dot(C) # pl.plot(xx,yy,'k*') y3_res = np.reshape(y2_res, (dy_sz, dx_sz, T), order='F') a__, c__, center__, b_in__, f_in__ = greedyROI( y3_res, nr=1, gSig=[ np.floor(old_div(dx_sz, 2)), np.floor(old_div(dy_sz, 2)) ], gSiz=[dx_sz, dy_sz]) # a__ = model.fit_transform(np.maximum(y2_res,0)); # c__ = model.components_; a_f = np.zeros((d, 1)) idxs = np.meshgrid(coords_y, coords_x) a_f[np.ravel_multi_index(idxs, (d1, d2), order='F').flatten()] = a__ A = np.concatenate([A, a_f], axis=1) C = np.concatenate([C, c__], axis=0) indx = np.argsort(a_f, axis=None)[::-1] cumEn = np.cumsum(a_f.flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn bmat = np.reshape(Bvec, np.shape(Cn), order='F') pl.contour(y, x, bmat, [thr]) pl.pause(.01) elif pts == []: break nr += 1 A3 = np.reshape(A, (d1, d2, nr), order='F') return A, C
def manually_refine_components(Y, xxx_todo_changeme, A, C, Cn, thr=0.9, display_numbers=True, max_number=None, cmap=None, **kwargs): """Plots contour of spatial components against a background image and allows to interactively add novel components by clicking with mouse Parameters ----------- Y: ndarray movie in 2D (dx,dy): tuple dimensions of the square used to identify neurons (should be set to the galue of gsiz) A: np.ndarray or sparse matrix Matrix of Spatial components (d x K) Cn: np.ndarray (2D) Background image (e.g. mean, correlation) thr: scalar between 0 and 1 Energy threshold for computing contours (default 0.995) display_number: Boolean Display number of ROIs if checked (default True) max_number: int Display the number for only the first max_number components (default None, display all numbers) cmap: string User specifies the colormap (default None, default colormap) Returns -------- A: np.ndarray matrix A os estimated spatial component contributions C: np.ndarray array of estimated calcium traces """ (dx, dy) = xxx_todo_changeme if issparse(A): A = np.array(A.todense()) else: A = np.array(A) d1, d2 = np.shape(Cn) d, nr = np.shape(A) if max_number is None: max_number = nr x, y = np.mgrid[0:d1:1, 0:d2:1] pl.imshow(Cn, interpolation=None, cmap=cmap) coordinates = [] cm = com(A, d1, d2) Bmat = np.zeros((np.minimum(nr, max_number), d1, d2)) for i in range(np.minimum(nr, max_number)): pars = dict(kwargs) indx = np.argsort(A[:, i], axis=None)[::-1] cumEn = np.cumsum(A[:, i].flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn Bmat[i] = np.reshape(Bvec, np.shape(Cn), order='F') T = np.shape(Y)[-1] pl.close() fig = pl.figure() # ax = fig.add_subplot(111) ax = pl.gca() ax.imshow(Cn, interpolation=None, cmap=cmap, vmin=np.percentile(Cn[~np.isnan(Cn)], 1), vmax=np.percentile(Cn[~np.isnan(Cn)], 99)) for i in range(np.minimum(nr, max_number)): pl.contour(y, x, Bmat[i], [thr]) if display_numbers: for i in range(np.minimum(nr, max_number)): ax.text(cm[i, 1], cm[i, 0], str(i + 1)) A3 = np.reshape(A, (d1, d2, nr), order='F') while True: pts = fig.ginput(1, timeout=0) if pts != []: print(pts) xx, yy = np.round(pts[0]).astype(np.int) coords_y = np.array(list(range(yy - dy, yy + dy + 1))) coords_x = np.array(list(range(xx - dx, xx + dx + 1))) coords_y = coords_y[(coords_y >= 0) & (coords_y < d1)] coords_x = coords_x[(coords_x >= 0) & (coords_x < d2)] a3_tiny = A3[coords_y[0]:coords_y[-1] + 1, coords_x[0]:coords_x[-1] + 1, :] y3_tiny = Y[coords_y[0]:coords_y[-1] + 1, coords_x[0]:coords_x[-1] + 1, :] # y3med = np.median(y3_tiny,axis=-1) # y3_tiny = y3_tiny - y3med[...,np.newaxis] #y3_tiny = y3_tiny-np.median(y3_tiny,axis=-1) dy_sz, dx_sz = np.shape(a3_tiny)[:-1] y2_tiny = np.reshape(y3_tiny, (dx_sz * dy_sz, T), order='F') a2_tiny = np.reshape(a3_tiny, (dx_sz * dy_sz, nr), order='F') y2_res = y2_tiny - a2_tiny.dot(C) # pl.plot(xx,yy,'k*') y3_res = np.reshape(y2_res, (dy_sz, dx_sz, T), order='F') a__, c__, center__, b_in__, f_in__ = greedyROI( y3_res, nr=1, gSig=[np.floor(old_div(dx_sz, 2)), np.floor(old_div(dy_sz, 2))], gSiz=[dx_sz, dy_sz]) # a__ = model.fit_transform(np.maximum(y2_res,0)); # c__ = model.components_; a_f = np.zeros((d, 1)) idxs = np.meshgrid(coords_y, coords_x) a_f[np.ravel_multi_index(idxs, (d1, d2), order='F').flatten()] = a__ A = np.concatenate([A, a_f], axis=1) C = np.concatenate([C, c__], axis=0) indx = np.argsort(a_f, axis=None)[::-1] cumEn = np.cumsum(a_f.flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn bmat = np.reshape(Bvec, np.shape(Cn), order='F') pl.contour(y, x, bmat, [thr]) pl.pause(.01) elif pts == []: break nr += 1 A3 = np.reshape(A, (d1, d2, nr), order='F') return A, C
figure.set_size_inches([50., .5 * len(C_0)]) figure.savefig( '/mnt/Data01/data/calcium_imaging_analysis/data/interim/component_evaluation/trial_wise/meta/figures/mouse_56165_session_1_3.png' ) figure, axes = plt.subplots(1) axes.hist(typical_size, bins=25) ### this part computes center of mass and distance between center of mass of the contours A_center_of_mass_list_x = [] A_center_of_mass_list_y = [] A_template = [] trial_belonging = [] for i in range(len(A_list)): for j in range(A_list[i].shape[1]): cm_coordinates = com(A_list[i][:, j], FOV_size[i][0], FOV_size[i][1]) A_center_of_mass_list_x.append(cm_coordinates[0][0]) A_center_of_mass_list_y.append(cm_coordinates[0][1]) trial_belonging.append(i + 1) A_template.append(A_list[i][:, j]) distance_list = [] for i in range(len(A_center_of_mass_list_x)): for j in range(i + 1, len(A_center_of_mass_list_x)): x1 = A_center_of_mass_list_x[i] x2 = A_center_of_mass_list_x[j] y1 = A_center_of_mass_list_y[i] y2 = A_center_of_mass_list_y[j] distance = math.sqrt((x1 - x2)**2 + (y1 - y2)**2) distance_list.append(distance)
def plot_contours(A, Cn, thr=None, thr_method = 'max', maxthr = 0.2, nrgthr = 0.9, display_numbers=True, max_number=None, cmap=None, swap_dim=False, colors='w', vmin=None, vmax=None, **kwargs): """Plots contour of spatial components against a background image and returns their coordinates Parameters ----------- A: np.ndarray or sparse matrix Matrix of Spatial components (d x K) Cn: np.ndarray (2D) Background image (e.g. mean, correlation) thr_method: [optional] string Method of thresholding: 'max' sets to zero pixels that have value less than a fraction of the max value 'nrg' keeps the pixels that contribute up to a specified fraction of the energy maxthr: [optional] scalar Threshold of max value nrgthr: [optional] scalar Threshold of energy thr: scalar between 0 and 1 Energy threshold for computing contours (default 0.9) Kept for backwards compatibility. If not None then thr_method = 'nrg', and nrgthr = thr display_number: Boolean Display number of ROIs if checked (default True) max_number: int Display the number for only the first max_number components (default None, display all numbers) cmap: string User specifies the colormap (default None, default colormap) Returns -------- Coor: list of coordinates with center of mass, contour plot coordinates and bounding box for each component """ if issparse(A): A = np.array(A.todense()) else: A = np.array(A) if swap_dim: Cn = Cn.T print('Swapping dim') d1, d2 = np.shape(Cn) d, nr = np.shape(A) if max_number is None: max_number = nr if thr is not None: thr_method = 'nrg' nrgthr = thr warn("The way to call utilities.plot_contours has changed. Look at the definition for more details.") x, y = np.mgrid[0:d1:1, 0:d2:1] ax = pl.gca() # Cn[np.isnan(Cn)]=0 if vmax is None and vmin is None: pl.imshow(Cn, interpolation=None, cmap=cmap, vmin=np.percentile(Cn[~np.isnan(Cn)], 1), vmax=np.percentile(Cn[~np.isnan(Cn)], 99)) else: pl.imshow(Cn, interpolation=None, cmap=cmap, vmin=vmin, vmax=vmax) coordinates = [] cm = com(A, d1, d2) for i in range(np.minimum(nr, max_number)): pars = dict(kwargs) if thr_method == 'nrg': indx = np.argsort(A[:, i], axis=None)[::-1] cumEn = np.cumsum(A[:, i].flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn thr = nrgthr else: # thr_method = 'max' if ~(thr_method == 'max'): warn("Unknown threshold method. Choosing max") Bvec = A[:,i].flatten() Bvec /= np.max(Bvec) thr = maxthr if swap_dim: Bmat = np.reshape(Bvec, np.shape(Cn), order='C') else: Bmat = np.reshape(Bvec, np.shape(Cn), order='F') cs = pl.contour(y, x, Bmat, [thr], colors=colors) # this fix is necessary for having disjoint figures and borders plotted correctly p = cs.collections[0].get_paths() v = np.atleast_2d([np.nan, np.nan]) for pths in p: vtx = pths.vertices num_close_coords = np.sum(np.isclose(vtx[0, :], vtx[-1, :])) if num_close_coords < 2: if num_close_coords == 0: # case angle newpt = np.round(old_div(vtx[-1, :], [d2, d1])) * [d2, d1] #import ipdb; ipdb.set_trace() vtx = np.concatenate((vtx, newpt[np.newaxis, :]), axis=0) else: # case one is border vtx = np.concatenate((vtx, vtx[0, np.newaxis]), axis=0) #import ipdb; ipdb.set_trace() v = np.concatenate((v, vtx, np.atleast_2d([np.nan, np.nan])), axis=0) # p = cs.collections[0].get_paths()[0] # v = p.vertices pars['CoM'] = np.squeeze(cm[i, :]) pars['coordinates'] = v pars['bbox'] = [np.floor(np.min(v[:, 1])), np.ceil(np.max(v[:, 1])), np.floor(np.min(v[:, 0])), np.ceil(np.max(v[:, 0]))] pars['neuron_id'] = i + 1 coordinates.append(pars) if display_numbers: for i in range(np.minimum(nr, max_number)): if swap_dim: ax.text(cm[i, 0], cm[i, 1], str(i + 1), color=colors) else: ax.text(cm[i, 1], cm[i, 0], str(i + 1), color=colors) return coordinates
j = 0 number = 0 number1 = 0 #Cn = np.mean(np.array(m), axis=0) Cn = img A = A.astype(np.float64) for i in range(n_group): plt.figure() vmax = np.percentile(Cn, 99) vmin = np.percentile(Cn, 5) plt.imshow(Cn, interpolation='None', vmax=vmax, vmin=vmin) #cmap=plt.cm.gray, plt.title('Neurons location') d1, d2 = np.shape(Cn) #d, nr = np.shape(A) cm1 = com(A.reshape((N, -1), order='F').transpose(), d1, d2) max_number = n colors = 'yellow' for j in range(np.int(np.ceil(N / n_group))): index = mat[i, j] print(index) img = A[index] img1 = img.copy() #img1[img1<np.percentile(img1[img1>0],15)] = 0 #img1 = connected_components(img1) img2 = np.multiply(img, img1) contours = measure.find_contours(img2, 0.5)[0] #img2=img.copy() img2[img2 == 0] = np.nan if index != -1: plt.plot(contours[:, 1],
spatial[pair[1]], dims, template1=templates[pair[0]], template2=templates[pair[1]], plot_results=False, max_thr=max_thr, thresh_cost=thresh_cost, max_dist=max_dist, Cn=templates[pair[0]]) plt.suptitle(cnm_titles[pair[0]] + ' vs ' + cnm_titles[pair[1]]) my_roi_image(spatial[pair[0]].toarray(), A2, dims, match_1, match_2, non_1, non_2, max_thr) # Calculate centroid distances for the matched cells cm_1 = com(spatial[pair[0]], dims[0], dims[1])[match_1] cm_2 = com(spatial[pair[1]], dims[0], dims[1])[match_2] cm_2_registered = com(A2, dims[0], dims[1])[match_2] distances = [0] * len(cm_1) distances_registered = [0] * len(cm_1) matched_cors = footprint_cors(spatial[pair[0]][:, match_1], A2[:, match_2], cm_1) for i, centroid1, centroid2, centroid2_reg in zip(range(len(cm_1)), cm_1, cm_2, cm_2_registered): distances[i] = np.linalg.norm(centroid1 - centroid2) distances_registered[i] = np.linalg.norm(centroid1 - centroid2_reg) print('Median distance=' + str(np.median(distances))) print('Median distance registered=' + str(np.median(distances_registered))) print('Median correlation of matches=' + str(np.median(matched_cors))) trial_stats = pd.DataFrame() trial_stats['reg_dist_px'] = distances_registered