def make_mask_figures(self): # self.ops_plot[1] = 0 # self.ops_plot[2] = 0 self.ops_plot[3] = [] self.ops_plot[4] = [] self.ops_plot[5] = [] self.ops_plot[6] = [] ncells = len(self.stat) for n in range(0, ncells): ypix = self.stat[n]["ypix"].flatten() xpix = self.stat[n]["xpix"].flatten() iext = fig.boundary(ypix, xpix) self.stat[n]["yext"] = ypix[iext] self.stat[n]["xext"] = xpix[iext] ycirc, xcirc = fig.circle(self.stat[n]["med"], self.stat[n]["radius"]) goodi = ((ycirc >= 0) & (xcirc >= 0) & (ycirc < self.ops["Ly"]) & (xcirc < self.ops["Lx"])) self.stat[n]["ycirc"] = ycirc[goodi] self.stat[n]["xcirc"] = xcirc[goodi] # # make color arrays for various views fig.make_colors(self) # # colorbar self.colormat = fig.make_colorbar() self.init_all_masks()
def make_masks_and_buttons(self): self.loadBeh.setEnabled(True) self.bloaded = False self.ROI_remove() self.isROI = False self.ops_plot[1] = 0 self.ops_plot[2] = 0 self.ops_plot[3] = [] self.ops_plot[4] = [] self.setWindowTitle(self.fname) # set bin size to be 0.5s by default self.bin = int(self.ops['tau'] * self.ops['fs'] / 2) self.binedit.setText(str(self.bin)) # add boundaries to stat for ROI overlays ncells = len(self.stat) for n in range(0, ncells): ypix = self.stat[n]['ypix'].flatten() xpix = self.stat[n]['xpix'].flatten() iext = fig.boundary(ypix, xpix) self.stat[n]['yext'] = ypix[iext] self.stat[n]['xext'] = xpix[iext] ycirc, xcirc = fig.circle(self.stat[n]['med'], self.stat[n]['radius']) goodi = (ycirc >= 0) & (xcirc >= 0) & (ycirc < self.ops['Ly']) & ( xcirc < self.ops['Lx']) self.stat[n]['ycirc'] = ycirc[goodi] self.stat[n]['xcirc'] = xcirc[goodi] # enable buttons self.enable_views_and_classifier() # make color arrays for various views fig.make_colors(self) self.ichosen = int(0) self.imerge = [int(0)] self.iflip = int(0) self.ichosen_stats() self.comboBox.setCurrentIndex(2) # colorbar self.colormat = fig.make_colorbar() fig.plot_colorbar(self, self.ops_plot[2]) tic = time.time() fig.init_masks(self) print(time.time() - tic) M = fig.draw_masks(self) fig.plot_masks(self, M) self.lcell1.setText('%d' % (ncells - self.iscell.sum())) self.lcell0.setText('%d' % (self.iscell.sum())) fig.init_range(self) fig.plot_trace(self) if (type(self.ops['diameter']) is not int) and (len(self.ops['diameter']) > 1): self.xyrat = self.ops['diameter'][0] / self.ops['diameter'][1] else: self.xyrat = 1.0 self.p1.setAspectLocked(lock=True, ratio=self.xyrat) self.p2.setAspectLocked(lock=True, ratio=self.xyrat) self.loaded = True self.mode_change(2) self.show() # no classifier loaded classifier.activate(self, False)
def activity_stats(parent): i0 = int(1 - parent.iscell[parent.ichosen]) ypix = np.array([]) xpix = np.array([]) lam = np.array([]) footprints = np.array([]) F = np.zeros((0, parent.Fcell.shape[1]), np.float32) Fneu = np.zeros((0, parent.Fcell.shape[1]), np.float32) for n in np.array(parent.imerge): ypix = np.append(ypix, parent.stat[n]["ypix"]) xpix = np.append(xpix, parent.stat[n]["xpix"]) lam = np.append(lam, parent.stat[n]["lam"]) footprints = np.append(footprints, parent.stat[n]["footprint"]) F = np.append(F, parent.Fcell[n, :][np.newaxis, :], axis=0) Fneu = np.append(Fneu, parent.Fneu[n, :][np.newaxis, :], axis=0) # remove overlaps ipix = np.concatenate((ypix[:, np.newaxis], xpix[:, np.newaxis]), axis=1) _, goodi = np.unique(ipix, return_index=True, axis=0) ypix = ypix[goodi] xpix = xpix[goodi] lam = lam[goodi] stat0 = {} stat0["ypix"] = ypix.astype(np.int32) stat0["xpix"] = xpix.astype(np.int32) stat0["lam"] = lam stat0['med'] = [np.median(stat0["ypix"]), np.median(stat0["xpix"])] stat0["npix"] = ypix.size d0 = parent.ops["diameter"] radius = utils.fitMVGaus(ypix / d0[0], xpix / d0[1], lam, 2)[2] stat0["radius"] = radius[0] * d0.mean() stat0["aspect_ratio"] = 2 * radius[0] / (.01 + radius[0] + radius[1]) npix = np.array([parent.stat[n]['npix'] for n in range(len(parent.stat))]).astype('float32') stat0["npix_norm"] = stat0["npix"] / npix.mean() # compactness rs, dy, dx = sparsedetect.circleMask(d0) rsort = np.sort(rs.flatten()) r2 = ((ypix - stat0["med"][0]) / d0[0])**2 + ( (xpix - stat0["med"][1]) / d0[1])**2 r2 = r2**.5 stat0["mrs"] = np.mean(r2) stat0["mrs0"] = np.mean(rsort[:r2.size]) stat0["compact"] = stat0["mrs"] / (1e-10 + stat0["mrs0"]) # footprint stat0["footprint"] = footprints.mean() F = F.mean(axis=0) Fneu = Fneu.mean(axis=0) dF = F - parent.ops["neucoeff"] * Fneu # activity stats stat0["skew"] = stats.skew(dF) stat0["std"] = dF.std() # compute outline and circle around cell iext = fig.boundary(ypix, xpix) stat0["yext"] = ypix[iext] stat0["xext"] = xpix[iext] ycirc, xcirc = fig.circle(stat0["med"], stat0["radius"]) goodi = ((ycirc >= 0) & (xcirc >= 0) & (ycirc < parent.ops["Ly"]) & (xcirc < parent.ops["Lx"])) stat0["ycirc"] = ycirc[goodi] stat0["xcirc"] = xcirc[goodi] # deconvolve activity spks = dcnv.oasis(dF[np.newaxis, :], parent.ops) # add cell to structs parent.stat = np.concatenate((parent.stat, np.array([stat0])), axis=0) print(parent.stat[-1]["ypix"].shape) parent.Fcell = np.concatenate((parent.Fcell, F[np.newaxis, :]), axis=0) parent.Fneu = np.concatenate((parent.Fneu, Fneu[np.newaxis, :]), axis=0) parent.Spks = np.concatenate((parent.Spks, spks), axis=0) iscell = np.array([parent.iscell[parent.ichosen]], dtype=bool) parent.iscell = np.concatenate((parent.iscell, iscell), axis=0)
def merge_activity_masks(parent): print('merging activity... this may take some time') i0 = int(1-parent.iscell[parent.ichosen]) ypix = np.array([]) xpix = np.array([]) lam = np.array([]) footprints = np.array([]) F = np.zeros((0,parent.Fcell.shape[1]), np.float32) Fneu = np.zeros((0,parent.Fcell.shape[1]), np.float32) probcell = [] probredcell = [] merged_cells = [] remove_merged = [] for n in np.array(parent.imerge): if len(parent.stat[n]['imerge']) > 0: remove_merged.append(n) for k in parent.stat[n]['imerge']: merged_cells.append(k) else: merged_cells.append(n) merged_cells = np.unique(np.array(merged_cells)) for n in merged_cells: ypix = np.append(ypix, parent.stat[n]["ypix"]) xpix = np.append(xpix, parent.stat[n]["xpix"]) lam = np.append(lam, parent.stat[n]["lam"]) footprints = np.append(footprints, parent.stat[n]["footprint"]) F = np.append(F, parent.Fcell[n,:][np.newaxis,:], axis=0) Fneu = np.append(Fneu, parent.Fneu[n,:][np.newaxis,:], axis=0) probcell.append(parent.probcell[n]) probredcell.append(parent.probredcell[n]) probcell = np.array(probcell) probredcell = np.array(probredcell) pmean = probcell.mean() prmean = probredcell.mean() # remove overlaps ipix = np.concatenate((ypix[:,np.newaxis], xpix[:,np.newaxis]), axis=1) _, goodi = np.unique(ipix, return_index=True, axis=0) ypix = ypix[goodi] xpix = xpix[goodi] lam = lam[goodi] stat0 = {} if 'aspect' in parent.ops: d0 = np.array([int(parent.ops['aspect']*10), 10]) else: d0 = parent.ops['diameter'] if isinstance(d0, int): d0 = [d0,d0] ### compute statistics of merges stat0["imerge"] = merged_cells stat0["ypix"] = ypix.astype(np.int32) stat0["xpix"] = xpix.astype(np.int32) stat0["lam"] = lam / lam.sum() * merged_cells.size stat0['med'] = [np.median(stat0["ypix"]), np.median(stat0["xpix"])] stat0["npix"] = ypix.size radius = utils.fitMVGaus(ypix/d0[0], xpix/d0[1], lam, 2)[2] stat0["radius"] = radius[0] * d0.mean() stat0["aspect_ratio"] = 2 * radius[0]/(.01 + radius[0] + radius[1]) npix = np.array([parent.stat[n]['npix'] for n in range(len(parent.stat))]).astype('float32') stat0["npix_norm"] = stat0["npix"] / npix.mean() # compactness rs,dy,dx = sparsedetect.circleMask(d0) rsort = np.sort(rs.flatten()) r2 = ((ypix - stat0["med"][0])/d0[0])**2 + ((xpix - stat0["med"][1])/d0[1])**2 r2 = r2**.5 stat0["mrs"] = np.mean(r2) stat0["mrs0"] = np.mean(rsort[:r2.size]) stat0["compact"] = stat0["mrs"] / (1e-10 + stat0["mrs0"]) # footprint stat0["footprint"] = footprints.mean() # inmerge stat0["inmerge"] = 0 ### compute activity of merged cells F = F.mean(axis=0) Fneu = Fneu.mean(axis=0) dF = F - parent.ops["neucoeff"]*Fneu # activity stats stat0["skew"] = stats.skew(dF) stat0["std"] = dF.std() ### for GUI drawing # compute outline and circle around cell iext = fig.boundary(ypix, xpix) stat0["yext"] = ypix[iext].astype(np.int32) stat0["xext"] = xpix[iext].astype(np.int32) ycirc, xcirc = fig.circle(stat0["med"], stat0["radius"]) goodi = ( (ycirc >= 0) & (xcirc >= 0) & (ycirc < parent.ops["Ly"]) & (xcirc < parent.ops["Lx"]) ) stat0["ycirc"] = ycirc[goodi] stat0["xcirc"] = xcirc[goodi] # deconvolve activity spks = dcnv.oasis(dF[np.newaxis, :], parent.ops) ### remove previously merged cell (do not replace) for k in remove_merged: remove_mask(parent, k) np.delete(parent.stat, k, 0) np.delete(parent.Fcell, k, 0) np.delete(parent.Fneu, k, 0) np.delete(parent.Spks, k, 0) np.delete(parent.iscell, k, 0) np.delete(parent.probcell, k, 0) np.delete(parent.probredcell, k, 0) np.delete(parent.redcell, k, 0) np.delete(parent.notmerged, k, 0) # add cell to structs parent.stat = np.concatenate((parent.stat, np.array([stat0])), axis=0) parent.stat = sparsedetect.get_overlaps(parent.stat, parent.ops) parent.stat = np.array(parent.stat) parent.Fcell = np.concatenate((parent.Fcell, F[np.newaxis,:]), axis=0) parent.Fneu = np.concatenate((parent.Fneu, Fneu[np.newaxis,:]), axis=0) parent.Spks = np.concatenate((parent.Spks, spks), axis=0) iscell = np.array([parent.iscell[parent.ichosen]], dtype=bool) parent.iscell = np.concatenate((parent.iscell, iscell), axis=0) parent.probcell = np.append(parent.probcell, pmean) parent.probredcell = np.append(parent.probredcell, prmean) parent.redcell = np.append(parent.redcell, prmean > parent.chan2prob) parent.notmerged = np.append(parent.notmerged, False) # recompute binned F parent.mode_change(parent.activityMode) for n in merged_cells: parent.stat[n]['inmerge'] = parent.stat.size-1 add_mask(parent, parent.iscell.size-1)