This documentation is for the older Mesmerize legacy desktop application. Please see the home page or GitHub repo for information on the latest packages.


Perform 3D 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.


The parameters used for 3D 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 3D CNMF in the viewer, execute get_workEnv().history_trace in the viewer console and look for the ‘cnmf_3d’ 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.


This module adds a “CNMF_3D” 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.

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 also

Script Editor

This example loads 3D sequences from disk & adds them to a batch with 3 parameter variants.

# just so we can reset the params for each new image file
def reset_params():
    # CNMF Params that we will use for each item
    cnmf_kwargs = \
        'p': 2,
        'merge_thresh': 0.8,
        'k': 50,
        'gSig': (10, 10, 1),
        'gSiz': (41, 41, 4)

    # component evaluation params
    eval_kwargs = \
        'min_SNR': 3.0,
        'rval_thr': 0.75,
        'decay_time': 1.0,

    # the dict that will be passed to the mesmerize caiman module
    params = \
        "cnmf_kwargs":  cnmf_kwargs,
        "eval_kwargs":  eval_kwargs,
        "refit":        True,  # if you want to perform a refit
        "item_name":    "will set later per file",
        "use_patches":  False,
        "use_memmap":   False,  # re-use the memmap from a previous batch item, reduces computation time
        "memmap_uuid:   None,   # UUID (as a str) of the batch item to use the memmap from
        "keep_memmmap": False   # keep the memmap of this batch item


    return params

# get the 3d cnmf module
cnmf_mod = get_module('cnmf_3d', hide=True)

# Path to the dir containing images
files = glob("/full_path_to_raw_images/*.tiff")
# Sort in alphabetical order (should also work for numbers)

# Open each file, crop, and add to batch with 3 diff mot cor params
for i, path in enumerate(files):
    print("Working on file " + str(i + 1) + " / " + str(len(files)))

    # get json file path for the meta data
    meta_path = path[:-5] + ".json"

    # Create a new work environment with this image sequence
    vi.viewer.workEnv = ViewerWorkEnv.from_tiff(path=path,           # tiff file path
                                                method='imread',     # use imread
                                                meta_path=meta_path, # json metadata file path
                                                axes_order=None)     # default axes order
                                                                     # see Mesmerize Tiff file module docs for more info on axes order

    # update the work environment

    # get the first variant of params
    params = reset_parmas()

    # Set name for this video file
    name = os.path.basename(path)[:-5]
    params["item_name"] = name

    # add batch item with one variant of params
    u = cnmf_mod.add_to_batch(params)

    # add the same image but change some params
    params["cnmf_kwargs"]["gSig"] = (12, 12, 1)
    params["eval_kwargs"]["min_SNR"] = 2.5

    # use the same memmap as the previous batch item
    # since it's the same image
    params["use_memmap"] = True
    params["memmap_uuid"] = str(u)

    # add this param variant to the batch

    # one more variant of params
    params["eval_kwargs"]["min_SNR"] = 2.0

    # add this param variant to the batch