Perform CNMF using the implementation provided by the CaImAn library. This modules basically provides a GUI for parameter entry.

I highly recommend going through the following before using this module



Please see the CaImAn demo notebook mentioned above to understand the parameters. The Caiman docs also provide descriptions of the parameters:

You can also enter parameters for CNMF and component evaluation as keyword arguments (kwargs) in the the respective text boxes if you select “Use CNMF kwrags” or “Use evaluation params”. This is useful if you want to enter parameters that cannot be entered in the GUI for example. Use single quotes if you want to enter string kwargs, do not use double quotes.


This module adds a “CNMF” item to the batch. Set the desired parameters (see Caiman docs & demos) and then enter a name to add it as an item to the batch. After the batch item is processed, double-click the batch item to import the CNMF output into a Viewer. You can then annotate and curate ROIs, and add the data as a Sample to your project.

See also

This modules uses the Batch Manager.


It’s recommended to open a new Viewer when you want to import 3D CNMF data. Full garbage collection of 3D data in the Viewer Work environment is a WIP for when you want to clear & import 3D data into the same viewer. However when you close the Viewer entirely it is garbage collected entirely.


The parameters used for CNMF are stored in the work environment of the viewer and this log is carried over and saved in Project Samples as well. To see the parameters that were used for CNMF in the viewer, execute get_workEnv().history_trace in the viewer console and look for the ‘cnmf’ entry.


Importing several thousands of ROIs into the Viewer can take 15-30 minutes. You will be able to track the progress of the import in the Viewer Window’s status bar.


If you’re using Windows, large memmap files will linger in your batch dir or work dir, you can clean them out periodically.

Script usage

A script can be used to add CNMF batch items. This is much faster than using the GUI. This example sets the work environment from the output of a batch item. See the Caiman Motion Correction script usage examples for how to load images if you want to add CNMF items from images that are not in a batch.

See also

Script Editor

 1def reset_params():
 2    # CNMF Params that we will use for each item
 3    cnmf_kwargs = \
 4    {
 5        'p': 2,
 6        'gnb': 1,
 7        'merge_thresh': 0.25,
 8        'rf': 70,
 9        'stride': 40,
10        'k': 16,
11        'gSig': (8, 8),
12        'gSiz': (33, 33)
13    }
15    # component evaluation params
16    eval_kwargs = \
17    {
18        'min_SNR': 2.5,
19        'rval_thr': 0.8,
20        'min_cnn_thr': 0.8,
21        'cnn_lowest': 0.1,
22        'decay_time': 2.0,
23    }
25    # the dict that will be passed to the mesmerize caiman module
26    params = \
27    {
28        "cnmf_kwargs":  cnmf_kwargs,
29        "eval_kwargs":  eval_kwargs,
30        "refit":        True,  # if you want to perform a refit
31        "item_name":    "will set later per file",
32    }
34    return params
36# Get the batch manager
37bm = get_batch_manager()
38cnmf_mod = get_module('cnmf', hide=True)
40# Start index if we want to start processing the new items after they have been added
41start_ix = bm.df.index.size + 1
43# This example uses motion corrected output items from the batch manager
44# You can also open image files directly from disk, see the motion correction
45# script examples to see how to open images from disk.
46for ix, r in bm.df.iterrows():
47    # Use output of items 6 - 12
48    # for example if items 6 - 12 were motion correction items
49    if ix < 6:
50        continue
51    if ix > 12: # You need to set a break point, else the batch grows infinitely
52        break
54    # get the first variant of params
55    params = reset_parmas()
57    # Get the name of the mot cor item
58    name = r['name']
60    # Set the name for the new cnmf item
61    params['item_name'] = name
63    # Load the mot cor output
64    bm.load_item_output(module='caiman_motion_correction', viewers=viewer, UUID=r['uuid'])
66    # Set the sampling rate of the data
67    params['eval_kwargs']['fr'] = vi.viewer.workEnv.imgdata.meta['fps']
69    # Get the border_pix value from the motion correction output
70    # skip this if loading files that don't have NaNs on the image borders
71    history_trace = vi.viewer.workEnv.history_trace
72    border_pix = next(d for ix, d in enumerate(history_trace) if 'caiman_motion_correction' in d)['caiman_motion_correction']['bord_px']
74    # Set the border_pix values
75    params['border_pix'] = border_pix
76    params['cnmf_kwargs']['border_pix'] = border_pix
78    # Add to batch
79    cnmf_mod.add_to_batch(params)
81    # change some of the params and add this variant to batch
82    params['cnmf_kwargs']['gSig'] = (10, 10)
83    params['cnmf_kwargs']['gSiz'] = (41, 41)
85    # Add to batch with this params variant
86    cnmf_mod.add_to_batch(params)
88    # another parameter variant
89    params['eval_kwargs']['rval_thr'] = 0.7
90    params['eval_kwargs']['min_cnn_thr'] = 0.65
92    # Add to batch with this params variant
93    cnmf_mod.add_to_batch(params)
95# Cleanup the work environment
98# Uncomment the last two lines to start the batch as well
99#bm.process_batch(start_ix, clear_viewers=True)