Exemple #1
0
def init_masks(parent):
    '''

    creates RGB masks using stat and puts them in M0 or M1 depending on
    whether or not iscell is True for a given ROI
    args:
        ops: mean_image, Vcorr
        stat: xpix,ypix,xext,yext
        iscell: vector with True if ROI is cell
        ops_plot: plotROI, view, color, randcols
    outputs:
        M0: ROIs that are True in iscell
        M1: ROIs that are False in iscell

    '''
    ops = parent.ops
    stat = parent.stat
    iscell = parent.iscell
    cols = parent.ops_plot[3]
    ncells = len(stat)
    Ly = ops['Ly']
    Lx = ops['Lx']
    Sroi = np.zeros((2, Ly, Lx), np.float32)
    Sext = np.zeros((2, Ly, Lx), np.float32)
    LamAll = np.zeros((Ly, Lx), np.float32)
    Lam = np.zeros((2, 3, Ly, Lx), np.float32)
    iExt = -1 * np.ones((2, 3, Ly, Lx), np.int32)
    iROI = -1 * np.ones((2, 3, Ly, Lx), np.int32)

    for n in range(ncells - 1, -1, -1):
        ypix = stat[n]['ypix']
        if ypix is not None:
            xpix = stat[n]['xpix']
            yext = stat[n]['yext']
            xext = stat[n]['xext']
            lam = stat[n]['lam']
            lam = lam / lam.sum()
            i = int(1 - iscell[n])
            # add cell on top
            iROI[i, 2, ypix, xpix] = iROI[i, 1, ypix, xpix]
            iROI[i, 1, ypix, xpix] = iROI[i, 0, ypix, xpix]
            iROI[i, 0, ypix, xpix] = n
            # add outline to all layers
            iExt[i, 2, yext, xext] = iExt[i, 1, yext, xext]
            iExt[i, 1, yext, xext] = iExt[i, 0, yext, xext]
            iunder = iExt[i, 1, yext, xext]
            iExt[i, 0, yext, xext] = n
            # add weighting to all layers
            Lam[i, 2, ypix, xpix] = Lam[i, 1, ypix, xpix]
            Lam[i, 1, ypix, xpix] = Lam[i, 0, ypix, xpix]
            Lam[i, 0, ypix, xpix] = lam
            Sroi[i, ypix, xpix] = 1
            Sext[i, yext, xext] = 1
            LamAll[ypix, xpix] = lam

    LamMean = LamAll[LamAll > 1e-10].mean()
    RGBall = np.zeros((2, cols.shape[1] + 1, 7, Ly, Lx, 3), np.float32)
    Vback = np.zeros((6, Ly, Lx), np.float32)
    RGBback = np.zeros((6, Ly, Lx, 3), np.float32)

    for k in range(7):
        if k > 0:
            if k == 2:
                if 'meanImgE' not in ops:
                    ops = utils.enhanced_mean_image(ops)
                mimg = ops['meanImgE']
            elif k == 1:
                mimg = ops['meanImg']
                S = np.maximum(0, np.minimum(1, Vorig * 1.5))
                mimg1 = np.percentile(mimg, 1)
                mimg99 = np.percentile(mimg, 99)
                mimg = (mimg - mimg1) / (mimg99 - mimg1)
                mimg = np.maximum(0, np.minimum(1, mimg))
            elif k == 3:
                vcorr = ops['Vcorr']
                mimg1 = np.percentile(vcorr, 1)
                mimg99 = np.percentile(vcorr, 99)
                vcorr = (vcorr - mimg1) / (mimg99 - mimg1)
                mimg = mimg1 * np.ones((ops['Ly'], ops['Lx']), np.float32)
                mimg[ops['yrange'][0]:ops['yrange'][1],
                     ops['xrange'][0]:ops['xrange'][1]] = vcorr
                mimg = np.maximum(0, np.minimum(1, mimg))
            elif k == 4:
                if 'max_proj' in ops:
                    mproj = ops['max_proj']
                    mimg1 = np.percentile(mproj, 1)
                    mimg99 = np.percentile(mproj, 99)
                    mproj = (mproj - mimg1) / (mimg99 - mimg1)
                    mimg = np.zeros((ops['Ly'], ops['Lx']), np.float32)
                    try:
                        mimg[ops['yrange'][0]:ops['yrange'][1],
                             ops['xrange'][0]:ops['xrange'][1]] = mproj
                    except:
                        print('maxproj not in combined view')
                    mimg = np.maximum(0, np.minimum(1, mimg))
                else:
                    mimg = 0.5 * np.ones((ops['Ly'], ops['Lx']), np.float32)
            elif k == 5:
                if 'meanImg_chan2_corrected' in ops:
                    mimg = ops['meanImg_chan2_corrected']
                    mimg1 = np.percentile(mimg, 1)
                    mimg99 = np.percentile(mimg, 99)
                    mimg = (mimg - mimg1) / (mimg99 - mimg1)
                    mimg = np.maximum(0, np.minimum(1, mimg))
            elif k == 6:
                if 'meanImg_chan2' in ops:
                    mimg = ops['meanImg_chan2']
                    mimg1 = np.percentile(mimg, 1)
                    mimg99 = np.percentile(mimg, 99)
                    mimg = (mimg - mimg1) / (mimg99 - mimg1)
                    mimg = np.maximum(0, np.minimum(1, mimg))
            else:
                mimg = np.zeros((ops['Ly'], ops['Lx']), np.float32)

            Vback[k - 1, :, :] = mimg
            V = mimg
            V = np.expand_dims(V, axis=2)
        for i in range(2):
            Vorig = np.maximum(0,
                               np.minimum(1, 0.75 * Lam[i, 0, :, :] / LamMean))
            Vorig = np.expand_dims(Vorig, axis=2)
            if k == 3:
                S = np.expand_dims(Sext[i, :, :], axis=2)
                Va = np.maximum(0, np.minimum(1, V + S))
            else:
                S = np.expand_dims(Sroi[i, :, :], axis=2)
                if k > 0:
                    S = np.maximum(0, np.minimum(1, Vorig * 1.5))
                    Va = V
                else:
                    Va = Vorig
            for c in range(0, cols.shape[1]):
                if k == 3:
                    H = cols[iExt[i, 0, :, :], c]
                    H = np.expand_dims(H, axis=2)
                    hsv = np.concatenate((H, S, Va), axis=2)
                    RGBall[i, c, k, :, :, :] = hsv_to_rgb(hsv)
                else:
                    H = cols[iROI[i, 0, :, :], c]
                    H = np.expand_dims(H, axis=2)
                    hsv = np.concatenate((H, S, Va), axis=2)
                    RGBall[i, c, k, :, :, :] = hsv_to_rgb(hsv)

    for k in range(6):
        H = np.zeros((Ly, Lx, 1), np.float32)
        S = np.zeros((Ly, Lx, 1), np.float32)
        V = np.expand_dims(Vback[k, :, :], axis=2)
        hsv = np.concatenate((H, S, V), axis=2)
        RGBback[k, :, :, :] = hsv_to_rgb(hsv)

    parent.RGBall = RGBall
    parent.RGBback = RGBback
    parent.Vback = Vback
    parent.iROI = iROI
    parent.iExt = iExt
    parent.Sroi = Sroi
    parent.Sext = Sext
    parent.Lam = Lam
    parent.LamMean = LamMean
Exemple #2
0
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