def close_GUI(self, parent): # Replace old stat file print('Saving old stat') np.save(os.path.join(parent.basename, 'stat_orig.npy'), parent.stat) # Save iscell print('Num cells', self.nROIs) print(np.shape(parent.probcell)) iscell_prob = np.concatenate( (parent.iscell[:, np.newaxis], parent.probcell[:, np.newaxis]), axis=1) new_iscell = np.ones((self.nROIs, 2)) new_iscell = np.concatenate((new_iscell, iscell_prob), axis=0) np.save(os.path.join(parent.basename, 'iscell.npy'), new_iscell) # Save fluorescence traces Fcell = np.concatenate((self.Fcell, parent.Fcell), axis=0) Fneu = np.concatenate((self.Fneu, parent.Fneu), axis=0) Spks = np.concatenate( (self.Spks, parent.Spks), axis=0 ) # For now convert spikes to 0 for the new ROIS and then fix it later np.save(os.path.join(parent.basename, 'F.npy'), Fcell) np.save(os.path.join(parent.basename, 'Fneu.npy'), Fneu) np.save(os.path.join(parent.basename, 'Spks.npy'), Spks) # Append new stat file with old and save print('Saving new stat') stat_all = self.new_stat.copy() for n in range(len(parent.stat)): stat_all.append(parent.stat[n]) # Calculate overlap before saving stat_all_w_overlap = sparsedetect.get_overlaps(stat_all, parent.ops) np.save(os.path.join(parent.basename, 'stat.npy'), stat_all_w_overlap) print(np.shape(Fcell), np.shape(Fneu), np.shape(Spks), np.shape(new_iscell), np.shape(stat_all)) # close GUI parent.load_proc() self.close()
def roi_detect_and_extract(ops): t0 = time.time() if ops['sparse_mode']: ops, stat = sparsedetect.sparsery(ops) else: ops, stat = sourcery.sourcery(ops) print('Found %d ROIs, %0.2f sec' % (len(stat), toc(t0))) ### apply default classifier ### if len(stat) > 0: classfile = os.path.join( os.path.abspath(os.path.dirname(__file__)), "classifiers/classifier_user.npy", ) if not os.path.isfile(classfile): classorig = os.path.join( os.path.abspath(os.path.dirname(__file__)), "classifiers/classifier.npy") shutil.copy(classorig, classfile) print('NOTE: applying classifier %s' % classfile) iscell = classifier.run(classfile, stat, keys=['npix_norm', 'compact', 'skew']) if 'preclassify' in ops and ops['preclassify'] > 0.0: ic = (iscell[:, 0] > ops['preclassify']).flatten().astype(np.bool) stat = stat[ic] iscell = iscell[ic] print('After classification with threshold %0.2f, %d ROIs remain' % (ops['preclassify'], len(stat))) else: iscell = np.zeros((0, 2)) stat = sparsedetect.get_overlaps(stat, ops) stat, ix = sparsedetect.remove_overlaps(stat, ops, ops['Ly'], ops['Lx']) iscell = iscell[ix, :] print('After removing overlaps, %d ROIs remain' % (len(stat))) np.save(os.path.join(ops['save_path'], 'iscell.npy'), iscell) # extract fluorescence and neuropil F, Fneu, F_chan2, Fneu_chan2, ops, stat = masks_and_traces(ops, stat) # subtract neuropil dF = F - ops['neucoeff'] * Fneu # compute activity statistics for classifier sk = stats.skew(dF, axis=1) sd = np.std(dF, axis=1) for k in range(F.shape[0]): stat[k]['skew'] = sk[k] stat[k]['std'] = sd[k] # if second channel, detect bright cells in second channel fpath = ops['save_path'] if 'meanImg_chan2' in ops: if 'chan2_thres' not in ops: ops['chan2_thres'] = 0.65 ops, redcell = chan2detect.detect(ops, stat) np.save(os.path.join(fpath, 'redcell.npy'), redcell) np.save(os.path.join(fpath, 'F_chan2.npy'), F_chan2) np.save(os.path.join(fpath, 'Fneu_chan2.npy'), Fneu_chan2) # add enhanced mean image ops = utils.enhanced_mean_image(ops) # save ops np.save(ops['ops_path'], ops) # save results np.save(os.path.join(fpath, 'F.npy'), F) np.save(os.path.join(fpath, 'Fneu.npy'), Fneu) np.save(os.path.join(fpath, 'stat.npy'), stat) return ops
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)