This guide walks through the complete miniscope calcium imaging processing pipeline โ from raw video to clean, separated neural signals ready for downstream analysis.
Walkthrough + sample code: MPS_Sample_Code (README + files)
License: Licensed under the Apache License, Version 2.0. You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0
Required inputs:
Advanced (Dask) Settings: 8 workers, 200 GB memory limit, 100% video percentage by default. Reduce video percentage for testing.
Note: Animal ID currently only accepts numeric values due to how naming conventions propagate through the pipeline. This is a known limitation and may be addressed in a future update.
.avi pattern is pre-set.Denoising methods:
Background removal methods:
Tip: After cropping, MPS saves data as a Zarr file. The image shown during cropping is roughly the mean pixel brightness across frames. If you need a static 2D image of the cropped field for presentations, you can export one from the Zarr output with a short Python snippet (load the array, save the mean frame as a JPEG).
Think of NNDSVD like a conductor listening to an orchestra warm up โ instead of isolating each instrument one by one, it instantly identifies the major sections from the combined sound. NNDSVD does the same: it decomposes the full video into a compact set of spatial components rapidly, giving the CNMF a strong starting point.
What it's actually doing: Non-Negative Double Singular Value Decomposition โ a fast matrix factorization that expresses your video as a product of three simpler matrices. Unlike PCA, SVD reaches a near-equivalent answer much faster on large datasets. The NN ("non-negative") constraint adds the biological prior that fluorescence can't be negative, encouraging spatially compact, blob-like components.
Parameters:
Important: The goal here is not perfection โ it is a fast, good-enough initialization for CNMF. NNDSVD is seeding the solution space so CNMF doesn't have to search from scratch. Spending significant time tuning these parameters provides diminishing returns. If your final CNMF results look reasonable, your initialization was good enough.
Some workflows stop here for preliminary results. The variance-explained plot shows cumulative % of data variance captured as components are added. For most miniscope datasets, 99%+ is typically accounted for within the first few components. You don't need 100% โ the final fraction of a percent usually contains noise. If you're at 95โ99%, your initialization is in good shape.
Watershed segmentation identifies candidate neurons. Since it's better to overestimate at this stage, this step quickly finds optimal parameters.
Known bug: After Step 4a suggests parameters, you must deselect and then reselect the "Apply Filter / Apply Search" option before running Step 4b. Skipping this causes the step to default to cache from a previous run (typically min_distance=20) rather than the new suggestions. This issue does not apply when loading parameters from a JSON file.
Collapses spatially overlapping components that were over-segmented in 4b.
Extracts calcium traces from each spatial component โ going from "where are the neurons" to "what are they doing."
Prepares the spatial (A) and temporal (C) matrices for CNMF. Skip component 0 (background).
Quality control before expensive processing. Remove NaN, empty, and flat components. These will break downstream steps if left in.
Final cleanup โ merges components that are probably the same neuron split across detections.
Estimates per-pixel noise across the field of view. Critical for CNMF โ it tells the algorithm which parts of the signal to trust.
Quality control checkpoint before CNMF computation.
Computes YrA โ the raw projection of the video data onto each component's spatial footprint. This gives the corresponding fluorescence signal at each timepoint for each neuron, before any denoising or spike inference. This is one of the computational bottlenecks of CNMF.
Sanity check on the YrA computation. What YrA actually means: it is the raw projection of video data onto each spatial footprint โ not a measure of variance explained. High YrA at a given time means strong fluorescence in that neuron's footprint at that moment. This projection feeds into the AR/sparsity optimization in Step 6d.
Analyzes your data to suggest AR order, sparse penalty, max iterations, and zero threshold. It examines SNR distribution, temporal dynamics, AR coefficient strengths, and spike rates.
Note on variability: Running Step 6c multiple times may produce slightly different suggested values due to rounding differences between how suggestions are displayed vs. stored internally. This is expected โ it does not indicate a problem. Loading parameters from a JSON file bypasses this entirely.
The core CNMF temporal update. Outputs: C (denoised calcium traces), S (inferred spike trains), b0/c0 (background), g (AR coefficients).
Removes components that didn't optimize well. Min spike sum, min calcium variance, and min spatial sum thresholds (all default 1e-6) catch dead or artifact components.
What Steps 7aโ7d are doing: These steps prepare and organize the computation for the spatial update in Step 7e โ they are not themselves changing what neurons look like. Dilation (7a) gives the update enough context around each footprint. Clustering (7b) and bounding boxes (7c) divide the field of view into manageable local patches so the algorithm only processes the relevant pixels around each neuron cluster, dramatically improving efficiency.
Expands spatial footprints for better coverage. CNMF footprints are often conservatively tight.
Groups nearby components into clusters for efficient local processing.
Creates bounding boxes around clusters โ rectangular regions used to limit the spatial update to relevant areas.
Analyzes temporal variability, spatial coherence, signal strength, and component shape to suggest minimum STD threshold, penalty scale, and maximum penalty for Step 7e. Treat these as an informed starting hypothesis.
Common scenarios:
Multi-penalty LASSO regression on local video regions. This is the longest step in the pipeline for large datasets.
On spatial maps: The footprint shows which pixels the signal is coming from โ not necessarily the full anatomical extent of the neuron. Neurons may be larger than their detected footprint depending on lens configuration, GCaMP expression level, and camera angle. As long as you are not making morphological claims, either a compact or expansive footprint is defensible. The temporal signals carry the scientific information.
Merges updated spatial components, handles cluster-boundary overlaps, and validates the final spatial result.
Step 7f is quick to re-run relative to 7e โ experiment with smoothing settings here without redoing the full spatial update.
Recomputes YrA using the refined spatial components from Step 7. This gives each neuron's temporal trace computed against better footprints than Step 6 had access to. Use step7f_A_merged as the spatial component source.
Re-runs the CNMF temporal update (CVXPY with AR modeling and sparsity constraints) on the improved YrA from Step 8a. See Step 6d for a full explanation of AR order and sparse penalty tradeoffs โ the same logic applies here.
Why Step 8 matters: Step 6 produces the first full CNMF solution. Step 8 re-runs the temporal update using the improved spatial footprints from Step 7 โ that is the key reason it matters. Because the spatial components are more accurate after Step 7's refinement, the temporal traces from Step 8 are cleaner. Note that because the solution is already near convergence, parameter choices here tend to have subtler effects than the same changes in Step 6. Both steps deserve careful attention; Step 8 is the last opportunity to refine your final traces before export.
Cross-session comparisons: Using identical Step 8 parameters across all animals and sessions means resulting traces are mathematically comparable on an absolute scale โ no Z-scoring required before comparing recordings. This holds assuming stable GCaMP expression and consistent optical conditions across sessions. If conditions varied substantially, Z-score before comparing regardless. Either approach is valid; consistency is the key.
Final quality filtering (min component size, min SNR, min correlation), then export in multiple formats.
The export includes a timestamp and all processing parameters for full reproducibility.
One of the most common questions when using MPS is: which parameters matter, and when? Understanding the pipeline's architecture helps.
Their job is to give CNMF a well-structured starting point. A CNMF run from random initialization would take far longer to converge and might settle into a suboptimal local minimum โ analogous to searching for a mountain peak when you've only been shown a narrow window of the landscape. Preprocessing, denoising, motion correction, NNDSVD, and watershed all serve to place the algorithm close to the right answer before the expensive optimization begins. Over-tuning parameters here provides diminishing returns. Focus your energy on Steps 6, 7, and 8.
These are where the actual temporal signals are estimated. Step 6 produces the first full CNMF solution; Step 8 refines it using the improved spatial footprints from Step 7. Step 6 parameters have more leverage โ changes there can produce substantially different results because the algorithm has more room to move. Step 8 is where you get the final, cleanest traces, but parameter sensitivity is subtler since the solution is already near convergence.
If you care about how footprints look โ for figures, presentations, or ROI definition โ tune Step 7. But recognize that for most scientific conclusions, the spatial map is a reference, not the result. Neurons may not fully illuminate even with GCaMP expression depending on lens position, focal plane, expression level, and vasculature. The shape you see is where reliable signal is coming from, not necessarily the full anatomical extent of the cell.
If you are recording from a region with few neurons (e.g., 20โ30 per session), this likely reflects your data rather than a pipeline failure. Every additional component you force in must be "paid for" by subtracting from noise and background estimates in the CNMF model. In a recording with 20 bright neurons, adding 2โ3 dim candidates risks introducing noise into the traces of all 20. High-quality signals from 20 neurons are more valuable than noisy signals from 25.
Default and suggested parameters are good starting points developed with specific data in mind. Your optimal parameters depend on brain region, calcium indicator kinetics (GCaMP6f vs. GCaMP8f differ meaningfully), lens numerical aperture, recording duration, and expected neuronal density. Treat parameter suggestions as informed hypotheses, and interpret the diagnostic plots at each stage as your evidence for whether to adjust.
Remember: the pipeline provides cleaned signals, but biological interpretation requires domain knowledge. When in doubt, consult the original videos to verify detected components correspond to real neurons.
Some miniscope systems produce line-splitting artifacts โ signal appearing in the leftmost pixels of frames. The pipeline can automatically detect and remove these frames.
Enable in Step 2a if you notice vertical lines or artifacts on the left edge of your videos.
Note: If no erroneous frames are detected, the all_removed_frames.txt log file will not be created. This is expected โ the absence of the file means no frames were removed, not that something went wrong.
Add custom preprocessing in Step 2a by modifying the post_process parameter in the video loading function.
For datasets with significant non-rigid motion, enable mesh-based correction in Step 2c by specifying a mesh size (e.g., (5,5) for a 5ร5 control point grid).
Parameter files are automatically applied to each step during autorun. Auto-save preserves parameters after each step.