flatironinstitute / CaImAn

Computational toolbox for large scale Calcium Imaging Analysis, including movie handling, motion correction, source extraction, spike deconvolution and result visualization.
https://caiman.readthedocs.io
GNU General Public License v2.0
639 stars 370 forks source link

Seeding ROIs for CNMF-E Pipeline #865

Closed Bilalgp closed 2 years ago

Bilalgp commented 3 years ago

For better support, please use the template below to submit your issue. When your issue gets resolved please remember to close it.

Windows 10. RAM 64 GB. Intel i7.

  1. Python version (3.x):

Python 3.6.12

  1. Working environment (Python IDE/Jupyter Notebook/other): Jupyter Notebook (Jupyter Lab)

  2. Which of the demo scripts you're using for your analysis (if applicable): CNMF-E Pipeline and Seeded ROIs demo pipeline

  3. CaImAn version*: 1.8.7

    Ain_test = Ain.toarray() > 0
    cnm = cnmf.CNMF(n_processes=n_processes, dview = dview, params=opts, Ain = Ain_test)

However, for this to work, I need to set:

rf = None
only_init = False

But the CNMF-E pipeline requires only_init to be true. The same number of ROIs appear in the total components of the new batch but the contours seem a little different than the original shape in the plot_contours() function. Attached are images of the original batch and the new batch.

If I only set only_init to true, I get an error. If I set rf to any other value than None then the proper ROIs do not appear. The proper seeded ROIs do not appear either if I do the following (with rf set to None and only_init = False).


    Ain_test = Ain.toarray() > 0
    cnm = cnmf.CNMF(n_processes=n_processes, dview = dview, params=opts)
    cnm.estimates.A = Ain_test

Original Batch: image

Seeded Batch: image

Thanks.

j-friedrich commented 3 years ago

With the newest commit to the dev branch you can seed the CNMF-E method explicitly with a list/array of seed pixels, see the example below. centers could be the value of an earlier result cnm.estimates.center.

import caiman as cm
from caiman.source_extraction import cnmf

# data from https://www.dropbox.com/sh/x0xx6mxqg6llj3u/AADw9LobDktqLF4Eo9YSTewga?dl=0
fname = 'bma22_epm.mat'
Y = cm.load(fname, var_name_hdf5='Y', top=5, bottom=5, left=8, right=5)
fname_new = cm.save_memmap([Y], base_name=fname[:-4], order='C')

centers = [[ 79,  56],
    [119, 176],
    [ 91,  58],
    [122,  92],
    [111,  39],
    [ 52,  12],
    [ 99,   5],
    [ 11,  22],
    [117,  19],
    [ 85,  30],
    [176,  80],
    [ 31,  10],
    [115,  55],
    [ 30, 152],
    [  7,  89],
    [ 64,  76],
    [ 29,  33],
    [ 38, 113],
    [152,  17],
    [ 14, 138],
    [ 57,  68],
    [167, 113],
    [104, 110]]
params_dict = dict(
    fnames=[fname_new], method_init='corr_pnr', gSig=(4, 4), gSiz=(15, 15),
    p=1, tsub=1, ssub=1, only_init=True, nb=0, min_corr=0, min_pnr=0,
    normalize_init=False, ring_size_factor=2, center_psf=True, ssub_B=2,
    init_iter=1, del_duplicates=True, seed_method=centers)
opts = cnmf.params.CNMFParams(params_dict=params_dict)
cnm = cnmf.CNMF(n_processes=1, params=opts)
cnm.fit_file()

You need to set rf=None because patches aren't supported. And also only_init=True (to avoid detrimental temporal/spatial updates that don't use the accurate ring-model).

Just FYI, the other already previously supported values for seed_method are auto, manual and semi. The latter should be combined with init_iter=2 to perform one round of automatic initialization and a second round to manually add further components.