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
622 stars 364 forks source link

Setting nb and nb_patch to 0 or negative values results in crash #1068

Open oterocoronel opened 1 year ago

oterocoronel commented 1 year ago

I have a relatively low background signal in my recording, so I would like to try cnmf with no background components. I have tried to set nb and nb_patch to 0, -1, and -2, but when trying to run:

cnm = cnm.fit(images)

I get this error:

---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/map_reduce.py", line 137, in cnmf_patches
    cnm = cnm.fit(images)
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/cnmf.py", line 500, in fit
    self.initialize(Y)
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/cnmf.py", line 970, in initialize
    initialize_components(Y, sn=estim.sn, options_total=self.params.to_dict(),
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/initialization.py", line 346, in initialize_components
    Ain, Cin, _, b_in, f_in = greedyROI(
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/initialization.py", line 970, in greedyROI
    b_in = model.fit_transform(np.maximum(res, 0)).astype(np.float32)
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/sklearn/utils/_set_output.py", line 140, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/sklearn/decomposition/_nmf.py", line 1559, in fit_transform
    self._validate_params()
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/sklearn/base.py", line 600, in _validate_params
    validate_parameter_constraints(
  File "/home/lab/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/sklearn/utils/_param_validation.py", line 97, in validate_parameter_constraints
    raise InvalidParameterError(
sklearn.utils._param_validation.InvalidParameterError: The 'n_components' parameter of NMF must be an int in the range [1, inf) or None. Got -2 instead.
"""

The above exception was the direct cause of the following exception:

InvalidParameterError                     Traceback (most recent call last)
Cell In[26], line 2
      1 tic = time.time()
----> 2 cnm = cnm.fit(images)
      3 toc = time.time()
      4 print('Seconds elapsed: ' + str(toc-tic))

File ~/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/cnmf.py:594, in CNMF.fit(self, images, indices)
    589 if not isinstance(images, np.memmap):
    590     raise Exception(
    591         'You need to provide a memory mapped file as input if you use patches!!')
    593 self.estimates.A, self.estimates.C, self.estimates.YrA, self.estimates.b, self.estimates.f, \
--> 594     self.estimates.sn, self.estimates.optional_outputs = run_CNMF_patches(
    595         images.filename, self.dims + (T,), self.params,
    596         dview=self.dview, memory_fact=self.params.get('patch', 'memory_fact'),
    597         gnb=self.params.get('init', 'nb'), border_pix=self.params.get('patch', 'border_pix'),
    598         low_rank_background=self.params.get('patch', 'low_rank_background'),
    599         del_duplicates=self.params.get('patch', 'del_duplicates'),
    600         indices=indices)
    602 self.estimates.bl, self.estimates.c1, self.estimates.g, self.estimates.neurons_sn = None, None, None, None
    603 logging.info("merging")

File ~/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/map_reduce.py:257, in run_CNMF_patches(file_name, shape, params, gnb, dview, memory_fact, border_pix, low_rank_background, del_duplicates, indices)
    255 if dview is not None:
    256     if 'multiprocessing' in str(type(dview)):
--> 257         file_res = dview.map_async(cnmf_patches, args_in).get(4294967)
    258     else:
    259         try:

File ~/.mambaforge/envs/caiman-spyder/lib/python3.10/multiprocessing/pool.py:774, in ApplyResult.get(self, timeout)
    772     return self._value
    773 else:
--> 774     raise self._value

InvalidParameterError: The 'n_components' parameter of NMF must be an int in the range [1, inf) or None. Got -2 instead.

Setting nb to None, as suggested above, results in this other crash:

File ~/.mambaforge/envs/caiman-spyder/lib/python3.10/site-packages/caiman/source_extraction/cnmf/params.py:929, in CNMFParams.check_consistency(self)
    925                 logging.warning("Changing rf from {0} to {1} ".format(self.patch['rf'], 2*self.init['gSiz'][0]) +
    926                                 "because the constraint rf > gSiz was not satisfied.")
    927 #        if self.motion['gSig_filt'] is None:
    928 #            self.motion['gSig_filt'] = self.init['gSig']
--> 929         if self.init['nb'] <= 0 and (self.patch['nb_patch'] != self.init['nb'] or
    930                                      self.patch['low_rank_background'] is not None):
    931             logging.warning("gnb={0}, hence setting keys nb_patch ".format(self.init['nb']) +
    932                             "and low_rank_background in group patch automatically.")
    933             self.set('patch', {'nb_patch': self.init['nb'], 'low_rank_background': None})

TypeError: '<=' not supported between instances of 'NoneType' and 'int'
kushalkolar commented 1 year ago

Do you have cells that are appearing in the background?

I don't think negative values makes sense for number of background components.

I wonder if playing with low_rank_background helps:

low_rank_background: bool, default: True Whether to update the background using a low rank approximation. If False all the nonzero elements of the background components are updated using hals (to be used with one background per patch)

or update_background_components ?

EricThomson commented 1 year ago

@oterocoronel could you show the full output of your parameters object? You said you are running CNMF not CNMFE?