def filter_components(cnm_obj, components_quality_params, registration_params,
                      exp_date):
    cnm_obj.estimates.threshold_spatial_components(
        maxthr=registration_params['max_thr'])
    cnm_obj.estimates.remove_small_large_neurons(
        min_size_neuro=neuron_size_params['min'],
        max_size_neuro=neuron_size_params['max'])
    logging.info('%s: filtered %d neurons based on the size', exp_date,
                 len(cnm_obj.estimates.idx_components_bad))
    cnm_obj.estimates.accepted_list = cnm_obj.estimates.idx_components
    empty_images = np.zeros((1, ) + cnm_obj.dims)
    if components_quality_params['use_cnn'] and (
            not hasattr(cnm_obj.estimates, 'cnn_preds')
            or len(cnm_obj.estimates.cnn_preds) == 0):
        predictions, final_crops = evaluate_components_CNN(
            cnm_obj.estimates.A,
            cnm_obj.dims,
            cnm_obj.params.init['gSig'],
            model_name=os.path.join(caiman_src_datadir, 'model', 'cnn_model'))
        cnm_obj.estimates.cnn_preds = predictions[:, 1]
    cnm_obj.estimates.filter_components(empty_images,
                                        cnm_obj.params,
                                        new_dict=components_quality_params,
                                        select_mode='Accepted')

    logging.info(exp_date + ', bad components: ' +
                 str(cnm_obj.estimates.idx_components_bad))
    logging.info(exp_date + ', # Components remained: ' +
                 str(cnm_obj.estimates.nr -
                     len(cnm_obj.estimates.idx_components_bad)))

    cnm_obj.estimates.select_components(use_object=True)
    return cnm_obj
Example #2
0
def classify_masks(masks, soma_diameter=(12, 12)):
    """ Uses a convolutional network to predict the probability per mask of being a soma.

    :param np.array masks: Masks (image_height x image_width x num_components)

    :returns: Soma predictions (num_components).
    """
    # Reshape masks
    image_height, image_width, num_components = masks.shape
    masks = masks.reshape(-1, num_components, order='F')

    # Prepare input
    from scipy.sparse import coo_matrix
    masks = coo_matrix(masks)
    soma_radius = np.int32(np.round(np.array(soma_diameter)/2))

    model_path = '/data/pipeline/python/pipeline/data/cnn_model'
    probs, _ = components_evaluation.evaluate_components_CNN(masks, (image_height, image_width),
                                                             soma_radius, model_name=model_path)

    return probs[:, 1]
Example #3
0
def classify_masks(masks, soma_diameter=(14, 14)):
    """ Uses a convolutional network to predict the probability per mask of being a soma.

    :param np.array masks: Masks (image_height x image_width x num_components)

    :returns: Soma predictions (num_components).
    """
    # Reshape masks
    image_height, image_width, num_components = masks.shape
    masks = masks.reshape(-1, num_components, order='F')

    # Prepare input
    from scipy.sparse import coo_matrix
    masks = coo_matrix(masks)
    soma_radius = np.int32(np.round(np.array(soma_diameter)/2))

    model_path = '/data/pipeline/python/pipeline/data/cnn_model'
    probs, _ = components_evaluation.evaluate_components_CNN(masks, (image_height, image_width),
                                                             soma_radius, model_name=model_path)

    return probs[:, 1]
Example #4
0
    cnm.fit_next(t, frame.copy().reshape(-1, order='F'))
    t += 1

#%% extract the results
    
C, f = cnm.C_on[cnm.gnb:cnm.M], cnm.C_on[:cnm.gnb]
A, b = cnm.Ab[:, cnm.gnb:cnm.M], cnm.Ab[:,:cnm.gnb]
print(('Number of components:' + str(A.shape[-1])))

#%% pass through the CNN classifier with a low threshold (keeps clearer neuron shapes and excludes processes)
use_CNN = False
if use_CNN:
    print ("... using CNN classifier ...")
    thresh_cnn = 0.1                                                                # threshold for CNN classifier
    from caiman.components_evaluation import evaluate_components_CNN 
    predictions,final_crops = evaluate_components_CNN(A,dims,gSig,model_name = '/home/cat/Downloads/CaImAn/use_cases/CaImAnpaper/cnn_model')
    A_exclude, C_exclude = A[:,predictions[:,1]<thresh_cnn], C[predictions[:,1]<thresh_cnn]
    A, C = A[:,predictions[:,1]>=thresh_cnn], C[predictions[:,1]>=thresh_cnn]
    noisyC = cnm.noisyC[cnm.gnb:cnm.M]
    YrA = noisyC[predictions[:,1]>=thresh_cnn] - C
else:
    print ("...skipping CNN classifier...")
    YrA = cnm.noisyC[cnm.gnb:cnm.M] - C

#%% plot results
if False:
    pl.figure()
    crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)

    view_patches_bar(Yr, A, C, b, f,
                     dims[0], dims[1], YrA, img=Cn)
Example #5
0
                        templ = None
                        frame_cor = frame_

                    frame_cor = frame_cor / img_norm  # normalize data-frame
                    cnm2.fit_next(t, frame_cor.reshape(
                        -1, order='F'))  # run OnACID on this frame
                    timings['fitn'] += time() - t1
                    tottime.append(time() - t1)  # store time
                    cnm2.how_many.append(len(cnm2.ind_new))

                    t += 1
                    #    break

                    t_remv = time()
                    if t % T_rm == 0 and remove_flag and (t + 1000 < T1):
                        prd, _ = evaluate_components_CNN(
                            cnm2.Ab[:, gnb:], dims, gSig)
                        ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                        cnm2.remove_components(ind_rem)
                        print('Removing ' + str(len(ind_rem)) + ' components')
                    timings['remv'] += time() - t_remv

                    if t % 1000 == 0 and plot_contours_flag:
                        #if t>=4500:tours_flag:
                        pl.cla()
                        A = cnm2.Ab[:, cnm2.gnb:]
                        crd = cm.utils.visualization.plot_contours(
                            A, Cn, thr=0.9
                        )  # update the contour plot every 1000 frames
                        pl.pause(1)

                    if play_reconstr:  # generate movie with the results
Example #6
0
            else:
                templ = None
                frame_cor = frame_

            frame_cor = frame_cor / img_norm  # normalize data-frame
            cnm2.fit_next(t, frame_cor.reshape(
                -1, order='F'))  # run OnACID on this frame
            timings['fitn'] += time() - t1
            tottime.append(time() - t1)  # store time

            t += 1
            #    break

            t_remv = time()
            if t % T_rm == 0 and remove_flag and (t + 1000 < T1):
                prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims, gSig)
                ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                cnm2.remove_components(ind_rem)
                print('Removing ' + str(len(ind_rem)) + ' components')
            timings['remv'] += time() - t_remv

            if t % 1000 == 0 and plot_contours_flag:
                #if t>=4500:tours_flag:
                pl.cla()
                A = cnm2.Ab[:, cnm2.gnb:]
                crd = cm.utils.visualization.plot_contours(
                    A, Cn,
                    thr=0.9)  # update the contour plot every 1000 frames
                pl.pause(1)

            if play_reconstr:  # generate movie with the results
                frame_cor, shift = motion_correct_iteration_fast(
                    frame_, templ, max_shift, max_shift)
                shifts.append(shift)
            else:
                templ = None
                frame_cor = frame_

            frame_cor = frame_cor / img_norm                        # normalize data-frame
            cnm2.fit_next(t, frame_cor.reshape(-1, order='F'))      # run OnACID on this frame
            # store time
            tottime.append(time() - t1)
            
            t += 1
            
            if t % T_rm == 0 and remove_flag:
                prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims, gSig)
                ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                cnm2.remove_components(ind_rem)
                print('Removing '+str(len(ind_rem))+' components')

            if t % 1000 == 0 and plot_contours_flag:
                pl.cla()
                A = cnm2.Ab[:, cnm2.gnb:]
                # update the contour plot every 1000 frames
                crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
                pl.pause(1)

            if play_reconstr:                                               # generate movie with the results
                vid_frame = create_frame(cnm2, img_norm, captions)
                if save_movie:
                    out.write(vid_frame)
Example #8
0
        A_gt_thr = cm.source_extraction.cnmf.spatial.threshold_components(A_gt.tocsc()[:, :].toarray(), dims, medw=None, thr_method='max', maxthr=0.2, nrgthr=0.99, extract_cc=True,
                                                                          se=None, ss=None, dview=None)
    #%%
#    crd = cm.utils.visualization.plot_contours(A_gt, Cn, thr=.99)
    #%%
    Yr, dims, T = cm.load_memmap(fname_new)
    T = np.minimum(T, maxT)
    Yr = Yr[:, :T]
    d1, d2 = dims
    images = np.reshape(Yr.T, [T] + list(dims), order='F')
    # TODO: needinfo
    Y = np.reshape(Yr, dims + (T,), order='F')
    m_orig = np.array(images)
    #%%
    from caiman.components_evaluation import evaluate_components_CNN
    predictions, final_crops = evaluate_components_CNN(
        A_gt, dims, gSig, model_name='model/cnn_model', isGPU=False)
    #%%
    thresh = .95
    idx_components_cnn = np.where(predictions[:, 1] >= thresh)[0]
    #%%
    if T > 8000:
        num_neurons_per_group = 100
    else:
        num_neurons_per_group = 50

    patch_size = 50
    half_crop = np.minimum(gSig[0] * 4 + 1, patch_size) // 2
    for idx_included in grouper(num_neurons_per_group, idx_components_cnn, fillvalue=None):
        # idx_included = idx_components_cnn[np.arange(50)] # neurons that are displayed
        all_pos_crops = []
        all_neg_crops = []
                shifts.append(shift)
            else:
                templ = None
                frame_cor = frame_

            frame_cor = frame_cor / img_norm                        # normalize data-frame
            cnm2.fit_next(t, frame_cor.reshape(-1, order='F'))      # run OnACID on this frame
            timings['fitn'] += time() - t1
            tottime.append(time() - t1)                             # store time

            t += 1
            #    break

            t_remv = time()
            if t % T_rm == 0 and remove_flag and (t + 1000 < T1):
                prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims, gSig)
                ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                cnm2.remove_components(ind_rem)
                print('Removing '+str(len(ind_rem))+' components')
            timings['remv'] += time() - t_remv

            if t % 1000 == 0 and plot_contours_flag:
            #if t>=4500:tours_flag:
                pl.cla()
                A = cnm2.Ab[:, cnm2.gnb:]
                crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)  # update the contour plot every 1000 frames
                pl.pause(1)

            if play_reconstr:                                               # generate movie with the results
                A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray()
                C, f = cnm2.C_on[cnm2.gnb:cnm2.M, :], cnm2.C_on[:cnm2.gnb, :]
Example #10
0
                fitness_delta_min=-10, return_all=True)
            
            print(len(idx_filter_snr))
            print(len(idx_filter_snr_bad))
            A_gt, C_gt, b_gt, f_gt, traces_gt, YrA_gt = A_gt.tocsc()[:,idx_filter_snr], C_gt[idx_filter_snr], b_gt, f_gt, traces_gt[idx_filter_snr], YrA_gt[idx_filter_snr]
        
        A_gt_thr = cm.source_extraction.cnmf.spatial.threshold_components(A_gt.tocsc()[:,:].toarray(), dims, medw=None, thr_method='max', maxthr=0.2, nrgthr=0.99, extract_cc=True,
                         se=None, ss=None, dview=None) 

#%%
pl.figure()
crd = plot_contours(A_gt_thr, Cn, thr=.99)
        
#%%
from caiman.components_evaluation import evaluate_components_CNN
predictions,final_crops = evaluate_components_CNN(A,dims,gSig)
#%%
threshold = .95
from caiman.utils.visualization import matrixMontage
pl.figure()
matrixMontage(np.squeeze(final_crops[np.where(predictions[:,1]>=threshold)[0]]))
pl.figure()
matrixMontage(np.squeeze(final_crops[np.where(predictions[:,0]>=threshold)[0]]))

#%%
cm.movie(final_crops).play(gain=3,magnification = 6,fr=5)
#%%
cm.movie(np.squeeze(final_crops[np.where(predictions[:,1]>=0.95)[0]])).play(gain=2., magnification = 8,fr=5)
#%%
cm.movie(np.squeeze(final_crops[np.where(predictions[:,0]>=0.95)[0]])).play(gain=4., magnification = 8,fr=5)
#%%
except:
    pass
#analysis_file = '/mnt/ceph/neuro/jeremie_analysis/neurofinder.03.00.test/Yr_d1_498_d2_467_d3_1_order_C_frames_2250_._results_analysis.npz'
with np.load(os.path.join(os.path.split(fname_new)[0], os.path.split(fname_new)[1][:-4] + 'results_analysis.npz')) as ld:
    print(ld.keys())
    locals().update(ld)
    dims_off = d1, d2
    A = scipy.sparse.coo_matrix(A[()])
    dims = (d1, d2)
    gSig = params_movie['gSig']
    fname_new = fname_new[()]


#%%
from caiman.components_evaluation import evaluate_components_CNN
predictions, final_crops = evaluate_components_CNN(
    A, dims, gSig, model_name='model/cnn_model')
#%%
cm.movie(final_crops).play(gain=3, magnification=6, fr=5)
#%%
cm.movie(np.squeeze(final_crops[np.where(predictions[:, 1] >= 0.5)[0]])).play(
    gain=2., magnification=5, fr=5)
#%%
cm.movie(np.squeeze(final_crops[np.where(predictions[:, 0] >= 0.5)[0]])).play(
    gain=2., magnification=5, fr=5)
#%%
thresh = .5
idx_components_cnn = np.where(predictions[:, 1] >= thresh)[0]
idx_components_bad_cnn = np.where(predictions[:, 0] > (1 - thresh))[0]

print(' ***** ')
print((len(final_crops)))
Example #12
0
                    frame_, templ, max_shift, max_shift)
                shifts.append(shift)
            else:
                templ = None
                frame_cor = frame_

            frame_cor = frame_cor / img_norm  # normalize data-frame
            cnm2.fit_next(t, frame_cor.reshape(
                -1, order='F'))  # run OnACID on this frame
            # store time
            tottime.append(time() - t1)

            t += 1

            if t % T_rm == 0 and remove_flag:
                prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims, gSig)
                ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                cnm2.remove_components(ind_rem)
                print('Removing ' + str(len(ind_rem)) + ' components')

            if t % 1000 == 0 and plot_contours_flag:
                pl.cla()
                A = cnm2.Ab[:, cnm2.gnb:]
                # update the contour plot every 1000 frames
                crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
                pl.pause(1)

            if play_reconstr:  # generate movie with the results
                vid_frame = create_frame(cnm2, img_norm, captions)
                if save_movie:
                    out.write(vid_frame)
Example #13
0
#%% extract the results

C, f = cnm.C_on[cnm.gnb:cnm.M], cnm.C_on[:cnm.gnb]
A, b = cnm.Ab[:, cnm.gnb:cnm.M], cnm.Ab[:, :cnm.gnb]
print(('Number of components:' + str(A.shape[-1])))

#%% pass through the CNN classifier with a low threshold (keeps clearer neuron shapes and excludes processes)
use_CNN = True
if use_CNN:
    # threshold for CNN classifier
    thresh_cnn = 0.1
    from caiman.components_evaluation import evaluate_components_CNN
    predictions, final_crops = evaluate_components_CNN(
        A,
        dims,
        gSig,
        model_name=os.path.join(caiman_datadir(), 'model', 'cnn_model'))
    A_exclude, C_exclude = A[:, predictions[:, 1] < thresh_cnn], C[
        predictions[:, 1] < thresh_cnn]
    A, C = A[:,
             predictions[:, 1] >= thresh_cnn], C[predictions[:,
                                                             1] >= thresh_cnn]
    noisyC = cnm.noisyC[cnm.gnb:cnm.M]
    YrA = noisyC[predictions[:, 1] >= thresh_cnn] - C
else:
    YrA = cnm.noisyC[cnm.gnb:cnm.M] - C

#%% plot results
pl.figure()
crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
Example #14
0
def main():
    pass  # For compatibility between running under Spyder and the CLI

    #%%  download and list all files to be processed

    # folder inside ./example_movies where files will be saved
    fld_name = 'Mesoscope'
    download_demo('Tolias_mesoscope_1.hdf5', fld_name)
    download_demo('Tolias_mesoscope_2.hdf5', fld_name)
    download_demo('Tolias_mesoscope_3.hdf5', fld_name)

    # folder where files are located
    folder_name = os.path.join(caiman_datadir(), 'example_movies', fld_name)
    extension = 'hdf5'  # extension of files
    # read all files to be processed
    fls = glob.glob(folder_name + '/*' + extension)

    # your list of files should look something like this
    print(fls)

    #%%   Set up some parameters

    # frame rate (Hz)
    fr = 15
    # approximate length of transient event in seconds
    decay_time = 0.5
    # expected half size of neurons
    gSig = (3, 3)
    # order of AR indicator dynamics
    p = 1
    # minimum SNR for accepting new components
    min_SNR = 2.5
    # correlation threshold for new component inclusion
    rval_thr = 0.85
    # spatial downsampling factor (increases speed but may lose some fine structure)
    ds_factor = 1
    # number of background components
    gnb = 2
    # recompute gSig if downsampling is involved
    gSig = tuple(np.ceil(np.array(gSig) / ds_factor).astype('int'))
    # flag for online motion correction
    mot_corr = True
    # maximum allowed shift during motion correction
    max_shift = np.ceil(10. / ds_factor).astype('int')

    # set up some additional supporting parameters needed for the algorithm (these are default values but change according to dataset characteristics)

    # number of shapes to be updated each time (put this to a finite small value to increase speed)
    max_comp_update_shape = np.inf
    # number of files used for initialization
    init_files = 1
    # number of files used for online
    online_files = len(fls) - 1
    # number of frames for initialization (presumably from the first file)
    initbatch = 200
    # maximum number of expected components used for memory pre-allocation (exaggerate here)
    expected_comps = 300
    # initial number of components
    K = 2
    # number of timesteps to consider when testing new neuron candidates
    N_samples = np.ceil(fr * decay_time)
    # exceptionality threshold
    thresh_fitness_raw = scipy.special.log_ndtr(-min_SNR) * N_samples
    # number of passes over the data
    epochs = 2
    # upper bound for number of frames in each file (used right below)
    len_file = 1000
    # total length of all files (if not known use a large number, then truncate at the end)
    T1 = len(fls) * len_file * epochs

    #%%    Initialize movie

    # load only the first initbatch frames and possibly downsample them
    if ds_factor > 1:
        Y = cm.load(fls[0], subindices=slice(0, initbatch, None)).astype(
            np.float32).resize(1. / ds_factor, 1. / ds_factor)
    else:
        Y = cm.load(fls[0], subindices=slice(0, initbatch,
                                             None)).astype(np.float32)

    if mot_corr:  # perform motion correction on the first initbatch frames
        mc = Y.motion_correct(max_shift, max_shift)
        Y = mc[0].astype(np.float32)
        borders = np.max(mc[1])
    else:
        Y = Y.astype(np.float32)

    # minimum value of movie. Subtract it to make the data non-negative
    img_min = Y.min()
    Y -= img_min
    img_norm = np.std(Y, axis=0)
    # normalizing factor to equalize the FOV
    img_norm += np.median(img_norm)
    Y = Y / img_norm[None, :, :]  # normalize data

    _, d1, d2 = Y.shape
    dims = (d1, d2)  # dimensions of FOV
    Yr = Y.to_2D().T  # convert data into 2D array

    Cn_init = Y.local_correlations(swap_dim=False)  # compute correlation image
    #pl.imshow(Cn_init)
    #pl.title('Correlation Image on initial batch')
    #pl.colorbar()

    bnd_Y = np.percentile(Y, (0.001, 100 - 0.001))  # plotting boundaries for Y

    #%% initialize OnACID with bare initialization

    cnm_init = bare_initialization(Y[:initbatch].transpose(1, 2, 0),
                                   init_batch=initbatch,
                                   k=K,
                                   gnb=gnb,
                                   gSig=gSig,
                                   p=p,
                                   minibatch_shape=100,
                                   minibatch_suff_stat=5,
                                   update_num_comps=True,
                                   rval_thr=rval_thr,
                                   thresh_fitness_raw=thresh_fitness_raw,
                                   batch_update_suff_stat=True,
                                   max_comp_update_shape=max_comp_update_shape,
                                   deconv_flag=False,
                                   use_dense=False,
                                   simultaneously=False,
                                   n_refit=0)

    #%% Plot initialization results

    crd = plot_contours(cnm_init.A.tocsc(), Cn_init, thr=0.9)
    A, C, b, f, YrA, sn = cnm_init.A, cnm_init.C, cnm_init.b, cnm_init.f, cnm_init.YrA, cnm_init.sn
    view_patches_bar(Yr,
                     scipy.sparse.coo_matrix(A.tocsc()[:, :]),
                     C[:, :],
                     b,
                     f,
                     dims[0],
                     dims[1],
                     YrA=YrA[:, :],
                     img=Cn_init)

    bnd_AC = np.percentile(A.dot(C), (0.001, 100 - 0.005))
    bnd_BG = np.percentile(b.dot(f), (0.001, 100 - 0.001))

    #%% create a function for plotting results in real time if needed

    def create_frame(cnm2, img_norm, captions):
        A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray()
        C, f = cnm2.C_on[cnm2.gnb:cnm2.M, :], cnm2.C_on[:cnm2.gnb, :]
        # inferred activity due to components (no background)
        frame_plot = (frame_cor.copy() - bnd_Y[0]) / np.diff(bnd_Y)
        comps_frame = A.dot(C[:, t - 1]).reshape(cnm2.dims, order='F')
        bgkrnd_frame = b.dot(f[:, t - 1]).reshape(
            cnm2.dims, order='F')  # denoised frame (components + background)
        denoised_frame = comps_frame + bgkrnd_frame
        denoised_frame = (denoised_frame.copy() - bnd_Y[0]) / np.diff(bnd_Y)
        comps_frame = (comps_frame.copy() - bnd_AC[0]) / np.diff(bnd_AC)

        if show_residuals:
            #all_comps = np.reshape(cnm2.Yres_buf.mean(0), cnm2.dims, order='F')
            all_comps = np.reshape(cnm2.mean_buff, cnm2.dims, order='F')
            all_comps = np.minimum(np.maximum(all_comps, 0) * 2 + 0.25, 255)
        else:
            all_comps = np.array(A.sum(-1)).reshape(cnm2.dims, order='F')
            # spatial shapes
        frame_comp_1 = cv2.resize(
            np.concatenate([frame_plot, all_comps * 1.], axis=-1),
            (2 * np.int(cnm2.dims[1] * resize_fact),
             np.int(cnm2.dims[0] * resize_fact)))
        frame_comp_2 = cv2.resize(
            np.concatenate([comps_frame, denoised_frame], axis=-1),
            (2 * np.int(cnm2.dims[1] * resize_fact),
             np.int(cnm2.dims[0] * resize_fact)))
        frame_pn = np.concatenate([frame_comp_1, frame_comp_2], axis=0).T
        vid_frame = np.repeat(frame_pn[:, :, None], 3, axis=-1)
        vid_frame = np.minimum((vid_frame * 255.), 255).astype('u1')

        if show_residuals and cnm2.ind_new:
            add_v = np.int(cnm2.dims[1] * resize_fact)
            for ind_new in cnm2.ind_new:
                cv2.rectangle(vid_frame,
                              (int(ind_new[0][1] * resize_fact),
                               int(ind_new[1][1] * resize_fact) + add_v),
                              (int(ind_new[0][0] * resize_fact),
                               int(ind_new[1][0] * resize_fact) + add_v),
                              (255, 0, 255), 2)

        cv2.putText(vid_frame,
                    captions[0], (5, 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    captions[1], (np.int(cnm2.dims[0] * resize_fact) + 5, 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    captions[2], (5, np.int(cnm2.dims[1] * resize_fact) + 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    captions[3], (np.int(cnm2.dims[0] * resize_fact) + 5,
                                  np.int(cnm2.dims[1] * resize_fact) + 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    'Frame = ' + str(t),
                    (vid_frame.shape[1] // 2 - vid_frame.shape[1] // 10,
                     vid_frame.shape[0] - 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 255),
                    thickness=1)
        return vid_frame

#%% Prepare object for OnACID

    cnm2 = deepcopy(cnm_init)

    save_init = False  # flag for saving initialization object. Useful if you want to check OnACID with different parameters but same initialization
    if save_init:
        cnm_init.dview = None
        save_object(cnm_init, fls[0][:-4] + '_DS_' + str(ds_factor) + '.pkl')
        cnm_init = load_object(fls[0][:-4] + '_DS_' + str(ds_factor) + '.pkl')

    path_to_cnn_residual = os.path.join(caiman_datadir(), 'model',
                                        'cnn_model_online.h5')

    cnm2._prepare_object(np.asarray(Yr),
                         T1,
                         expected_comps,
                         idx_components=None,
                         min_num_trial=3,
                         max_num_added=3,
                         path_to_model=path_to_cnn_residual,
                         sniper_mode=False,
                         use_peak_max=False,
                         q=0.5)
    cnm2.thresh_CNN_noisy = 0.5

    #%% Run OnACID and optionally plot results in real time
    epochs = 1
    cnm2.Ab_epoch = []  # save the shapes at the end of each epoch
    t = cnm2.initbatch  # current timestep
    tottime = []
    Cn = Cn_init.copy()

    # flag for removing components with bad shapes
    remove_flag = False
    T_rm = 650  # remove bad components every T_rm frames
    rm_thr = 0.1  # CNN classifier removal threshold
    # flag for plotting contours of detected components at the end of each file
    plot_contours_flag = False
    # flag for showing results video online (turn off flags for improving speed)
    play_reconstr = True
    # flag for saving movie (file could be quite large..)
    save_movie = False
    movie_name = os.path.join(
        folder_name, 'sniper_meso_0.995_new.avi')  # name of movie to be saved
    resize_fact = 1.2  # image resizing factor

    if online_files == 0:  # check whether there are any additional files
        process_files = fls[:init_files]  # end processing at this file
        init_batc_iter = [initbatch]  # place where to start
        end_batch = T1
    else:
        process_files = fls[:init_files + online_files]  # additional files
        # where to start reading at each file
        init_batc_iter = [initbatch] + [0] * online_files

    shifts = []
    show_residuals = True
    if show_residuals:
        caption = 'Mean Residual Buffer'
    else:
        caption = 'Identified Components'
    captions = ['Raw Data', 'Inferred Activity', caption, 'Denoised Data']
    if save_movie and play_reconstr:
        fourcc = cv2.VideoWriter_fourcc('8', 'B', 'P', 'S')
        #    fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(
            movie_name, fourcc, 30.0,
            tuple([int(2 * x * resize_fact) for x in cnm2.dims]))

    for iter in range(epochs):
        if iter > 0:
            # if not on first epoch process all files from scratch
            process_files = fls[:init_files + online_files]
            init_batc_iter = [0] * (online_files + init_files)

        # np.array(fls)[np.array([1,2,3,4,5,-5,-4,-3,-2,-1])]:
        for file_count, ffll in enumerate(process_files):
            print('Now processing file ' + ffll)
            Y_ = cm.load(ffll,
                         subindices=slice(init_batc_iter[file_count], T1,
                                          None))

            # update max-correlation (and perform offline motion correction) just for illustration purposes
            if plot_contours_flag:
                if ds_factor > 1:
                    Y_1 = Y_.resize(1. / ds_factor, 1. / ds_factor, 1)
                else:
                    Y_1 = Y_.copy()
                if mot_corr:
                    templ = (cnm2.Ab.data[:cnm2.Ab.indptr[1]] *
                             cnm2.C_on[0, t - 1]).reshape(cnm2.dims,
                                                          order='F') * img_norm
                    newcn = (Y_1 - img_min).motion_correct(
                        max_shift, max_shift,
                        template=templ)[0].local_correlations(swap_dim=False)
                    Cn = np.maximum(Cn, newcn)
                else:
                    Cn = np.maximum(Cn, Y_1.local_correlations(swap_dim=False))

            old_comps = cnm2.N  # number of existing components
            for frame_count, frame in enumerate(Y_):  # now process each file
                if np.isnan(np.sum(frame)):
                    raise Exception('Frame ' + str(frame_count) +
                                    ' contains nan')
                if t % 100 == 0:
                    print(
                        'Epoch: ' + str(iter + 1) + '. ' + str(t) +
                        ' frames have beeen processed in total. ' +
                        str(cnm2.N - old_comps) +
                        ' new components were added. Total number of components is '
                        + str(cnm2.Ab.shape[-1] - gnb))
                    old_comps = cnm2.N

                t1 = time()  # count time only for the processing part
                frame_ = frame.copy().astype(np.float32)  #
                if ds_factor > 1:
                    frame_ = cv2.resize(frame_,
                                        img_norm.shape[::-1])  # downsampling

                frame_ -= img_min  # make data non-negative

                if mot_corr:  # motion correct
                    templ = cnm2.Ab.dot(cnm2.C_on[:cnm2.M, t - 1]).reshape(
                        cnm2.dims, order='F') * img_norm
                    frame_cor, shift = motion_correct_iteration_fast(
                        frame_, templ, max_shift, max_shift)
                    shifts.append(shift)
                else:
                    templ = None
                    frame_cor = frame_

                frame_cor = frame_cor / img_norm  # normalize data-frame
                cnm2.fit_next(t, frame_cor.reshape(
                    -1, order='F'))  # run OnACID on this frame
                # store time
                tottime.append(time() - t1)

                t += 1

                if t % T_rm == 0 and remove_flag:
                    prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims,
                                                     gSig)
                    ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                    cnm2.remove_components(ind_rem)
                    print('Removing ' + str(len(ind_rem)) + ' components')

                if t % 1000 == 0 and plot_contours_flag:
                    pl.cla()
                    A = cnm2.Ab[:, cnm2.gnb:]
                    # update the contour plot every 1000 frames
                    crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
                    pl.pause(1)

                if play_reconstr:  # generate movie with the results
                    vid_frame = create_frame(cnm2, img_norm, captions)
                    if save_movie:
                        out.write(vid_frame)
                        if t - initbatch < 100:
                            #for rp in np.int32(np.ceil(np.exp(-np.arange(1,100)/30)*20)):
                            for rp in range(len(cnm2.ind_new) * 2):
                                out.write(vid_frame)
                    cv2.imshow('frame', vid_frame)
                    if t - initbatch < 100:
                        for rp in range(len(cnm2.ind_new) * 2):
                            cv2.imshow('frame', vid_frame)
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break

            print('Cumulative processing speed is ' +
                  str((t - initbatch) / np.sum(tottime))[:5] +
                  ' frames per second.')
        # save the shapes at the end of each epoch
        cnm2.Ab_epoch.append(cnm2.Ab.copy())

    if save_movie:
        out.release()
    cv2.destroyAllWindows()

    #%%  save results (optional)
    save_results = False

    if save_results:
        np.savez('results_analysis_online_MOT_CORR.npz',
                 Cn=Cn,
                 Ab=cnm2.Ab,
                 Cf=cnm2.C_on,
                 b=cnm2.b,
                 f=cnm2.f,
                 dims=cnm2.dims,
                 tottime=tottime,
                 noisyC=cnm2.noisyC,
                 shifts=shifts)

    #%% extract results from the objects and do some plotting
    A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray()
    C, f = cnm2.C_on[cnm2.gnb:cnm2.M,
                     t - t // epochs:t], cnm2.C_on[:cnm2.gnb,
                                                   t - t // epochs:t]
    noisyC = cnm2.noisyC[:, t - t // epochs:t]
    b_trace = [osi.b for osi in cnm2.OASISinstances] if hasattr(
        cnm2, 'OASISinstances') else [0] * C.shape[0]

    pl.figure()
    crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
    view_patches_bar(Yr,
                     scipy.sparse.coo_matrix(A.tocsc()[:, :]),
                     C[:, :],
                     b,
                     f,
                     dims[0],
                     dims[1],
                     YrA=noisyC[cnm2.gnb:cnm2.M] - C,
                     img=Cn)
                    else:
                        templ = None
                        frame_cor = frame_

                    frame_cor = frame_cor / img_norm                        # normalize data-frame
                    cnm2.fit_next(t, frame_cor.reshape(-1, order='F'))      # run OnACID on this frame
                    timings['fitn'] += time() - t1
                    tottime.append(time() - t1)                             # store time
                    cnm2.how_many.append(len(cnm2.ind_new))

                    t += 1
                    #    break

                    t_remv = time()
                    if t % T_rm == 0 and remove_flag and (t + 1000 < T1):
                        prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims, gSig)
                        ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                        cnm2.remove_components(ind_rem)
                        print('Removing '+str(len(ind_rem))+' components')
                    timings['remv'] += time() - t_remv

                    if t % 1000 == 0 and plot_contours_flag:
                    #if t>=4500:tours_flag:
                        pl.cla()
                        A = cnm2.Ab[:, cnm2.gnb:]
                        crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)  # update the contour plot every 1000 frames
                        pl.pause(1)

                    if play_reconstr:                                               # generate movie with the results
                        A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray()
                        C, f = cnm2.C_on[cnm2.gnb:cnm2.M, :], cnm2.C_on[:cnm2.gnb, :]
Example #16
0
    final_frate=final_frate,
    Npeaks=10,
    r_values_min=.85,
    fitness_min=-30,
    fitness_delta_min=-30,
    return_all=True,
    N=5,
    remove_baseline=True,
    dview=dview,
    robust_std=False,
    Athresh=0.1,
    thresh_C=0.3,
    num_traces_per_group=20)
#%%
from caiman.components_evaluation import evaluate_components_CNN
predictions, final_crops = evaluate_components_CNN(
    A, dims, gSig, model_name='use_cases/CaImAnpaper/cnn_model')
#%%
threshold = .95
from caiman.utils.visualization import matrixMontage
pl.figure()
matrixMontage(
    np.squeeze(final_crops[np.where(predictions[:, 1] >= threshold)[0]]))
pl.figure()
matrixMontage(
    np.squeeze(final_crops[np.where(predictions[:, 0] >= threshold)[0]]))
#%%
thresh = .95
idx_components_cnn = np.where(predictions[:, 1] >= thresh)[0]

print(' ***** ')
print((len(final_crops)))
# %%
A, C, b, f, YrA, sn = cnm.A, cnm.C, cnm.b, cnm.f, cnm.YrA, cnm.sn
#%%
final_frate = 10

Npeaks = 10
traces = C + YrA
#        traces_a=traces-scipy.ndimage.percentile_filter(traces,8,size=[1,np.shape(traces)[-1]/5])
#        traces_b=np.diff(traces,axis=1)
fitness_raw, fitness_delta, erfc_raw, erfc_delta, r_values, significant_samples = \
    evaluate_components(Y, traces, A, C, b, f, final_frate, remove_baseline=True,
                        N=5, robust_std=False, Athresh=0.1, Npeaks=Npeaks,  thresh_C=0.3)
#%%
from caiman.components_evaluation import evaluate_components_CNN
predictions, final_crops = evaluate_components_CNN(
    A, dims, gSig, model_name='model/cnn_model')
#%%
threshold = .95
from caiman.utils.visualization import matrixMontage
pl.figure()
matrixMontage(
    np.squeeze(final_crops[np.where(predictions[:, 1] >= threshold)[0]]))
pl.figure()
matrixMontage(
    np.squeeze(final_crops[np.where(predictions[:, 0] >= threshold)[0]]))
#%%
thresh = .95
idx_components_cnn = np.where(predictions[:, 1] >= thresh)[0]

print(' ***** ')
print((len(final_crops)))
Example #18
0
def main():
    pass  # For compatibility between running under Spyder and the CLI

    #%% load data

    fname = os.path.join(caiman_datadir(), 'example_movies', 'demoMovie.tif')
    Y = cm.load(fname).astype(np.float32)  #
    # used as a background image
    Cn = cm.local_correlations(Y.transpose(1, 2, 0))
    #%% set up some parameters

    # frame rate (Hz)
    fr = 10
    # approximate length of transient event in seconds
    decay_time = 0.5
    # expected half size of neurons
    gSig = [6, 6]
    # order of AR indicator dynamics
    p = 1
    # minimum SNR for accepting new components
    min_SNR = 3.5
    # correlation threshold for new component inclusion
    rval_thr = 0.90
    # number of background components
    gnb = 3

    # set up some additional supporting parameters needed for the algorithm (these are default values but change according to dataset characteristics)

    # number of shapes to be updated each time (put this to a finite small value to increase speed)
    max_comp_update_shape = np.inf
    # maximum number of expected components used for memory pre-allocation (exaggerate here)
    expected_comps = 50
    # number of timesteps to consider when testing new neuron candidates
    N_samples = np.ceil(fr * decay_time)
    # exceptionality threshold
    thresh_fitness_raw = log_ndtr(-min_SNR) * N_samples
    # total length of file
    T1 = Y.shape[0]

    # set up CNMF initialization parameters

    # merging threshold, max correlation allowed
    merge_thresh = 0.8
    # number of frames for initialization (presumably from the first file)
    initbatch = 400
    # size of patch
    patch_size = 32
    # amount of overlap between patches
    stride = 3
    # max number of components in each patch
    K = 4

    #%% obtain initial batch file used for initialization
    # memory map file (not needed)
    fname_new = Y[:initbatch].save(os.path.join(caiman_datadir(),
                                                'example_movies', 'demo.mmap'),
                                   order='C')
    Yr, dims, T = cm.load_memmap(fname_new)
    images = np.reshape(Yr.T, [T] + list(dims), order='F')
    Cn_init = cm.local_correlations(np.reshape(Yr, dims + (T, ), order='F'))

    #%% RUN (offline) CNMF algorithm on the initial batch
    pl.close('all')
    cnm_init = cnmf.CNMF(2,
                         k=K,
                         gSig=gSig,
                         merge_thresh=merge_thresh,
                         fr=fr,
                         p=p,
                         rf=patch_size // 2,
                         stride=stride,
                         skip_refinement=False,
                         normalize_init=False,
                         options_local_NMF=None,
                         minibatch_shape=100,
                         minibatch_suff_stat=5,
                         update_num_comps=True,
                         rval_thr=rval_thr,
                         thresh_fitness_delta=-50,
                         gnb=gnb,
                         decay_time=decay_time,
                         thresh_fitness_raw=thresh_fitness_raw,
                         batch_update_suff_stat=False,
                         max_comp_update_shape=max_comp_update_shape,
                         expected_comps=expected_comps,
                         dview=None,
                         min_SNR=min_SNR)

    cnm_init = cnm_init.fit(images)

    print(('Number of components:' + str(cnm_init.estimates.A.shape[-1])))

    pl.figure()
    crd = plot_contours(cnm_init.estimates.A.tocsc(), Cn_init, thr=0.9)

    #%% run (online) OnACID algorithm

    cnm = deepcopy(cnm_init)
    cnm.params.data['dims'] = (60, 80)
    cnm._prepare_object(np.asarray(Yr), T1)

    t = initbatch

    Y_ = cm.load(fname)[initbatch:].astype(np.float32)
    for frame_count, frame in enumerate(Y_):
        cnm.fit_next(t, frame.copy().reshape(-1, order='F'))
        t += 1

#%% extract the results

    C, f = cnm.estimates.C_on[gnb:cnm.M], cnm.estimates.C_on[:gnb]
    A, b = cnm.estimates.Ab[:, gnb:cnm.M], cnm.estimates.Ab[:, :gnb]
    print(('Number of components:' + str(A.shape[-1])))

    #%% pass through the CNN classifier with a low threshold (keeps clearer neuron shapes and excludes processes)
    use_CNN = True
    if use_CNN:
        # threshold for CNN classifier
        thresh_cnn = 0.1
        from caiman.components_evaluation import evaluate_components_CNN
        predictions, final_crops = evaluate_components_CNN(
            A,
            dims,
            gSig,
            model_name=os.path.join(caiman_datadir(), 'model', 'cnn_model'))
        A_exclude, C_exclude = A[:, predictions[:, 1] < thresh_cnn], C[
            predictions[:, 1] < thresh_cnn]
        A, C = A[:,
                 predictions[:,
                             1] >= thresh_cnn], C[predictions[:,
                                                              1] >= thresh_cnn]
        noisyC = cnm.estimates.noisyC[gnb:cnm.M]
        YrA = noisyC[predictions[:, 1] >= thresh_cnn] - C
    else:
        YrA = cnm.estimates.noisyC[gnb:cnm.M] - C

#%% plot results
    pl.figure()
    crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)

    view_patches_bar(Yr, A, C, b, f, dims[0], dims[1], YrA, img=Cn)
                            templ = None
                            frame_cor = frame_

                        frame_cor = frame_cor / img_norm  # normalize data-frame
                        cnm2.fit_next(t, frame_cor.reshape(
                            -1, order='F'))  # run OnACID on this frame
                        timings['fitn'] += time() - t1
                        tottime.append(time() - t1)  # store time
                        cnm2.how_many.append(len(cnm2.ind_new))

                        t += 1
                        #    break

                        t_remv = time()
                        if t % T_rm == 0 and remove_flag and (t + 1000 < T1):
                            prd, _ = evaluate_components_CNN(
                                cnm2.Ab[:, gnb:], dims, gSig)
                            ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                            cnm2.remove_components(ind_rem)
                            print('Removing ' + str(len(ind_rem)) +
                                  ' components')
                        timings['remv'] += time() - t_remv

                        if t % 1000 == 0 and plot_contours_flag:
                            #if t>=4500:tours_flag:
                            pl.cla()
                            A = cnm2.Ab[:, cnm2.gnb:]
                            crd = cm.utils.visualization.plot_contours(
                                A, Cn, thr=0.9
                            )  # update the contour plot every 1000 frames
                            pl.pause(1)