def _updateCoords(self, A, dims): '''See if we need to recalculate the coords Also see if we need to add components ''' if self.coords is None: #initial calculation self.A = A self.coords = get_contours(A, dims) elif np.shape(A)[1] > np.shape( self.A)[1] and self.frame_number % 200 == 0: #Only recalc if we have new components # FIXME: Since this is only for viz, only do this every 100 frames # TODO: maybe only recalc coords that are new? self.A = A self.coords = get_contours(A, dims) self.counter += 1
def nb_view_patches(Yr, A, C, S, b, f, d1, d2, YrA=None, image_neurons=None, thr=0.99, denoised_color=None, cmap='jet'): """ Interactive plotting utility for ipython notebook Args: Yr: np.ndarray movie A,C,b,f: np.ndarrays outputs of matrix factorization algorithm d1,d2: floats dimensions of movie (x and y) YrA: np.ndarray ROI filtered residual as it is given from update_temporal_components If not given, then it is computed (K x T) image_neurons: np.ndarray image to be overlaid to neurons (for instance the average) thr: double threshold regulating the extent of the displayed patches denoised_color: string or None color name (e.g. 'red') or hex color code (e.g. '#F0027F') cmap: string name of colormap (e.g. 'viridis') used to plot image_neurons """ # PREPROCESSING nr, T = C.shape nA2 = np.ravel(np.power(A, 2).sum(0)) if type(A) == np.ndarray else np.ravel( A.power(2).sum(0)) b = np.squeeze(b) f = np.squeeze(f) if YrA is None: Y_r = np.array( spdiags(old_div(1, nA2), 0, nr, nr) * (A.T * np.matrix(Yr) - (A.T * np.matrix(b[:, np.newaxis])) * np.matrix(f[np.newaxis]) - A.T.dot(A) * np.matrix(C)) + C) else: Y_r = C + YrA x = np.arange(T) if image_neurons is None: image_neurons = A.mean(1).reshape((d1, d2), order='F') coors = get_contours(A, (d1, d2), thr) cc1 = [cor['coordinates'][:, 0] for cor in coors] cc2 = [cor['coordinates'][:, 1] for cor in coors] c1 = cc1[0] c2 = cc2[0] # PLOTTING fig, axes = plt.subplots(2) axes[0].imshow(image_neurons, cmap='gray') axes[0].set_title('Neural map') axes[1].plot(C[0], label='C: raw traces', c='blue') axes[1].plot(Y_r[0], label='Y_r: residuals', c='red') axes[1].plot(S[0], label='S: deconvolved activity', c='green') plt.legend() axes[1].set_xlabel('t [frames]') # WIDGETS neuron_nr_slider = IntSlider(description='Neuron Number', value=0, min=0, max=len(C) - 1) def neuron_nr_handler(*args): i = neuron_nr_slider.value axes[1].clear() axes[1].plot(C[i], label='C: raw traces', c='blue') axes[1].plot(Y_r[i], label='Y_r: residuals', c='red') axes[1].plot(S[i], label='S: deconvolved activity', c='green') plt.legend() axes[1].set_xlabel('t [frames]') neuron_nr_slider.observe(neuron_nr_handler, 'value') widgets = [neuron_nr_slider] return fig, widgets
from caiman.utils.visualization import get_contours from pathlib import Path import pickle from utils import start_server pi.install_traceback() print('ready') fld = Path(r'D:\Dropbox (UCL - SWC)\Project_vgatPAG\analysis\doric\BF136p3_dPAG_ECIC\19MAR11') cnm_name = fld/'19MAR11_BF136p3_t1_ds126_ffc_crop_cnm.hdf5' if not cnm_name.exists(): raise FileExistsError(f'Could not find cnm: {str(cnm_name)}') c, dview, n_processes = start_server() cnm = load_CNMF(cnm_name, n_processes=n_processes, dview=dview) # Spatial components: in a d1 x d2 x n_components matrix A = np.reshape(cnm.estimates.A.toarray(), list(cnm.estimates.dims)+[-1], order='F') # set of spatial footprints np.save(fld/'A.npy', A) conts = get_contours(cnm.estimates.A.toarray(), cnm.estimates.dims) with open(str(fld/'all_contour_data.pkl'), 'wb') as fp: pickle.dump(conts, fp) pi.ok('Extracted A from cnm', f'Folder \n{fld.parent.name}/{fld.name}')