|
| 1 | +import numpy as np |
| 2 | +import pyqtgraph as pg |
| 3 | +from scipy import stats |
| 4 | +from suite2p import utils, dcnv, celldetect2, fig |
| 5 | +import math |
| 6 | +from PyQt5 import QtGui |
| 7 | +from matplotlib.colors import hsv_to_rgb |
| 8 | +import time |
| 9 | + |
| 10 | +def activity_stats(parent): |
| 11 | + i0 = int(1-parent.iscell[parent.ichosen]) |
| 12 | + ypix = np.array([]) |
| 13 | + xpix = np.array([]) |
| 14 | + lam = np.array([]) |
| 15 | + footprints = np.array([]) |
| 16 | + F = np.zeros((0,parent.Fcell.shape[1]), np.float32) |
| 17 | + Fneu = np.zeros((0,parent.Fcell.shape[1]), np.float32) |
| 18 | + for n in np.array(parent.imerge): |
| 19 | + ypix = np.append(ypix, parent.stat[n]["ypix"]) |
| 20 | + xpix = np.append(xpix, parent.stat[n]["xpix"]) |
| 21 | + lam = np.append(lam, parent.stat[n]["lam"]) |
| 22 | + footprints = np.append(footprints, parent.stat[n]["footprint"]) |
| 23 | + F = np.append(F, parent.Fcell[n,:][np.newaxis,:], axis=0) |
| 24 | + Fneu = np.append(Fneu, parent.Fneu[n,:][np.newaxis,:], axis=0) |
| 25 | + |
| 26 | + # remove overlaps |
| 27 | + ipix = np.concatenate((ypix[:,np.newaxis], xpix[:,np.newaxis]), axis=1) |
| 28 | + _, goodi = np.unique(ipix, return_index=True, axis=0) |
| 29 | + ypix = ypix[goodi] |
| 30 | + xpix = xpix[goodi] |
| 31 | + lam = lam[goodi] |
| 32 | + stat0 = {} |
| 33 | + stat0["ypix"] = ypix.astype(np.int32) |
| 34 | + stat0["xpix"] = xpix.astype(np.int32) |
| 35 | + stat0["lam"] = lam |
| 36 | + stat0['med'] = [np.median(stat0["ypix"]), np.median(stat0["xpix"])] |
| 37 | + stat0["npix"] = ypix.size |
| 38 | + d0 = parent.ops["diameter"] |
| 39 | + radius = utils.fitMVGaus(ypix / d0[0], xpix / d0[1], lam, 2)[2] |
| 40 | + stat0["radius"] = radius[0] * d0.mean() |
| 41 | + stat0["aspect_ratio"] = 2 * radius[0]/(.01 + radius[0] + radius[1]) |
| 42 | + npix = np.array([parent.stat[n]['npix'] for n in range(len(parent.stat))]).astype('float32') |
| 43 | + stat0["npix_norm"] = stat0["npix"] / npix.mean() |
| 44 | + # compactness |
| 45 | + rs,dy,dx = celldetect2.circleMask(d0) |
| 46 | + rsort = np.sort(rs.flatten()) |
| 47 | + r2 = ((ypix - stat0["med"][0])/d0[0])**2 + ((xpix - stat0["med"][1])/d0[1])**2 |
| 48 | + r2 = r2**.5 |
| 49 | + stat0["mrs"] = np.mean(r2) |
| 50 | + stat0["mrs0"] = np.mean(rsort[:r2.size]) |
| 51 | + stat0["compact"] = stat0["mrs"] / (1e-10 + stat0["mrs0"]) |
| 52 | + # footprint |
| 53 | + stat0["footprint"] = footprints.mean() |
| 54 | + F = F.mean(axis=0) |
| 55 | + Fneu = Fneu.mean(axis=0) |
| 56 | + dF = F - parent.ops["neucoeff"]*Fneu |
| 57 | + # activity stats |
| 58 | + stat0["skew"] = stats.skew(dF) |
| 59 | + stat0["std"] = dF.std() |
| 60 | + # compute outline and circle around cell |
| 61 | + iext = fig.boundary(ypix, xpix) |
| 62 | + stat0["yext"] = ypix[iext] |
| 63 | + stat0["xext"] = xpix[iext] |
| 64 | + ycirc, xcirc = fig.circle(stat0["med"], stat0["radius"]) |
| 65 | + goodi = ( |
| 66 | + (ycirc >= 0) |
| 67 | + & (xcirc >= 0) |
| 68 | + & (ycirc < parent.ops["Ly"]) |
| 69 | + & (xcirc < parent.ops["Lx"]) |
| 70 | + ) |
| 71 | + stat0["ycirc"] = ycirc[goodi] |
| 72 | + stat0["xcirc"] = xcirc[goodi] |
| 73 | + # deconvolve activity |
| 74 | + spks = dcnv.oasis(dF[np.newaxis, :], parent.ops) |
| 75 | + # add cell to structs |
| 76 | + parent.stat = np.concatenate((parent.stat, np.array([stat0])), axis=0) |
| 77 | + print(parent.stat[-1]["ypix"].shape) |
| 78 | + parent.Fcell = np.concatenate((parent.Fcell, F[np.newaxis,:]), axis=0) |
| 79 | + parent.Fneu = np.concatenate((parent.Fneu, Fneu[np.newaxis,:]), axis=0) |
| 80 | + parent.Spks = np.concatenate((parent.Spks, spks), axis=0) |
| 81 | + iscell = np.array([parent.iscell[parent.ichosen]], dtype=bool) |
| 82 | + parent.iscell = np.concatenate((parent.iscell, iscell), axis=0) |
| 83 | + |
| 84 | +def fig_masks(parent): |
| 85 | + """ merges multiple cells' colors together """ |
| 86 | + ncells = parent.stat.size |
| 87 | + nmerge = int(ncells - 1) |
| 88 | + # cells or notcells |
| 89 | + i0 = int(1-parent.iscell[-1]) |
| 90 | + #for n in np.array(parent.imerge): |
| 91 | + # for k in range(parent.iROI.shape[1]): |
| 92 | + # ipix = np.array((parent.iROI[i0,k,:,:]==n).nonzero()).astype(np.int32) |
| 93 | + # parent.iROI[i0, k, ipix[0,:], ipix[1,:]] = nmerge |
| 94 | + # ipix = np.array((parent.iExt[i0,k,:,:]==n).nonzero()).astype(np.int32) |
| 95 | + # parent.iExt[i0, k, ipix[0,:], ipix[1,:]] = ncells + nmerged |
| 96 | + #ypix = np.append(ypix, parent.stat[n]["ypix"]) |
| 97 | + #xpix = np.append(xpix, parent.stat[n]["xpix"]) |
| 98 | + ypix = parent.stat[nmerge]["ypix"].astype(np.int32) |
| 99 | + xpix = parent.stat[nmerge]["xpix"].astype(np.int32) |
| 100 | + parent.iROI[i0, 0, ypix, xpix] = nmerge |
| 101 | + parent.iExt[i0, 0, ypix, xpix] = nmerge |
| 102 | + |
| 103 | + cols = parent.ops_plot[3] |
| 104 | + cols = np.concatenate((cols, np.ones((1,cols.shape[1]))), axis=0) |
| 105 | + parent.ops_plot[3] = cols |
| 106 | + print(cols[nmerge,:]) |
| 107 | + for c in range(cols.shape[1]): |
| 108 | + for k in range(5): |
| 109 | + if k<3 or k==4: |
| 110 | + H = cols[parent.iROI[i0,0,ypix,xpix],c] |
| 111 | + S = parent.Sroi[i0,ypix,xpix] |
| 112 | + else: |
| 113 | + H = cols[parent.iExt[i0,0,ypix,xpix],c] |
| 114 | + S = parent.Sext[i0,ypix,xpix] |
| 115 | + if k==0: |
| 116 | + V = np.maximum(0, np.minimum(1, 0.75*parent.Lam[i0,0,ypix,xpix]/parent.LamMean)) |
| 117 | + elif k==1 or k==2 or k==4: |
| 118 | + V = parent.Vback[k-1,ypix,xpix] |
| 119 | + S = np.maximum(0, np.minimum(1, 1.5*0.75*parent.Lam[i0,0,ypix,xpix]/parent.LamMean)) |
| 120 | + elif k==3: |
| 121 | + V = parent.Vback[k-1,ypix,xpix] |
| 122 | + V = np.minimum(1, V + S) |
| 123 | + H = np.expand_dims(H.flatten(),axis=1) |
| 124 | + S = np.expand_dims(S.flatten(),axis=1) |
| 125 | + V = np.expand_dims(V.flatten(),axis=1) |
| 126 | + hsv = np.concatenate((H,S,V),axis=1) |
| 127 | + parent.RGBall[i0,c,k,ypix,xpix,:] = hsv_to_rgb(hsv) |
0 commit comments