Detection of synaptic activity in widefield single-plane fluorescence imaging iGluSNFR3.
This repository currently provides two maintained MATLAB entry pipelines:
multi_cell_activity_detection_pipeline.m: single-condition pipeline (supports spontaneous and evoked datasets via config selection)multi_cell_activity_detection_pipeline_matching.m: evoked multi-condition pipeline with post hoc cluster matching
Use the first pipeline when you want to analyze one condition at a time. Use the second pipeline when one folder contains multiple evoked conditions that should be matched after detection.
The code processes Bio-Formats-compatible microscopy files, detects events at pixel/ROI level, clusters synchronous activity, and exports figures and processed_data.mat outputs.
- Single-plane, single-channel fluorescence recordings
- iGluSNFR3 probe datasets
- File formats readable by Bio-Formats (for example
.cxd,.tif,.nd2) - Typical acquisition rates around 100 Hz (set in config)
Use a recent MATLAB release with these toolboxes available:
- Image Processing Toolbox
- Signal Processing Toolbox
- Bio-Formats MATLAB utilities in
Scripts/bfmatlab/(includingbioformats_package.jar) - MLspike helper code in
Scripts/MLspike/
No separate clone is required if you use this repository as-is.
- Clone or download this repository.
- Open MATLAB.
- Set your MATLAB current folder to the repository root (the folder containing
multi_cell_activity_detection_pipeline.m). - Open one of the pipeline entry scripts and edit paths/configuration before running (details below).
Use multi_cell_activity_detection_pipeline.m.
- In the script, set:
ops.filedirto your raw data rootops.savedirto your output root
- Choose the config based on dataset type:
ops = config_spontaneous(ops);for spontaneous activity datasetsops = config_evoked(ops);for single-condition evoked datasets
- In the selected config file, set input matching fields as needed:
ops.fileformat(for example.cxdor.tif)ops.filename_regex(regex filter for accepted filenames)
- Confirm acquisition frame rate (
ops.fs) in the config file you selected. - Run the script in MATLAB.
Behavior:
- Recursively scans subfolders under
ops.filedir - Creates per-file output folders under
ops.savedir - Writes
processed_data.mat, figure files, and optional masks/maps
Use multi_cell_activity_detection_pipeline_matching.m.
- In the script, set:
ops.filedirops.savedir
- Choose/configure one multi-spec config file:
Config/config_evoked_multi_spec.mfor real experimentsConfig/config_evoked_multi_spec_test_data.mfor bundled test examples
- In the chosen config file:
- set input matching fields:
ops.fileformatops.filename_regex(default expects names likeCell1_1.tif)
- set
Nto the number of images/conditions - customize per-image stimulus parameters (
first_stim,n_stim,stim_freq,len_spike) as needed - verify
ops.fs
- set input matching fields:
- Run
multi_cell_activity_detection_pipeline_matching.m.
Behavior:
- Applies condition-specific parameters based on image number parsed from filename
- If image index exceeds available condition configs, selection loops cyclically (e.g. 5->1, 6->2 for 4 configs)
- Runs event detection for each file
- Runs cluster matching at the end of each leaf folder (currently via
matching_clusters_with_image4)
The example below runs the spontaneous pipeline on one TIFF recording.
Example folder layout:
iGluSNFR3/
├── multi_cell_activity_detection_pipeline.m
├── Config/
├── Scripts/
├── originals/
│ └── demo_spont/
│ └── Cell1_1.tif
└── outputs/
Edit multi_cell_activity_detection_pipeline.m:
ops.filedir = './originals/demo_spont/';
ops.savedir = './outputs/demo_spont/';
ops = config_spontaneous(ops);
% Set ops.fileformat and ops.filename_regex in Config/config_spontaneous.mRun in MATLAB:
run('multi_cell_activity_detection_pipeline.m')Expected output after a successful run:
outputs/
└── demo_spont/
└── Cell1_1/
├── processed_data.mat
├── ROI_pxMap/ (created; may be empty if plotting is off)
└── Fig*.png (if visualization is enabled)
Notes:
- If no figures are created, check whether
ops.visualizeis set tofalsein your config. - If your file is not detected, verify
ops.fileformatandops.filename_regexin your selected config.
Config/config_spontaneous.m: defaults for spontaneous analysisConfig/config_evoked.m: defaults for single-condition evoked analysisConfig/config_evoked_multi_spec.m: per-condition evoked settings for multi-spec experimentsConfig/config_evoked_multi_spec_test_data.m: test-data variant of multi-spec settings
Important: these configs define file matching (ops.fileformat, ops.filename_regex) and convert many time-based parameters into frames internally. Always verify ops.fs matches your acquisition settings before running.
You can create a custom config file for your lab or experiment instead of editing the default configs.
This is useful when you want to keep the shipped config files unchanged and put your own experiment settings in a separate file.
- Copy a starting template:
Config/config_spontaneous.mfor spontaneous-like dataConfig/config_evoked.mfor single-condition evoked data
- Save as a new file, for example
Config/config_my_experiment.m. - Rename the function inside the file so it matches the filename:
function ops = config_my_experiment(ops)In multi_cell_activity_detection_pipeline.m, replace the config call with your custom function:
ops = config_my_experiment(ops);For multi-condition evoked workflows, copy and customize:
Config/config_evoked_multi_spec.m
Then call your custom multi-spec config in multi_cell_activity_detection_pipeline_matching.m:
ops_multi = config_my_multi_spec(ops);
ops = ops_multi{1};In this example, the custom multi-spec config returns one settings set per condition, so ops_multi{1} selects the first one.
- Confirm
ops.fsmatches acquisition frame rate. - Confirm
ops.experiment_typeis set appropriately. - Run one short recording first and inspect
processed_data.matand figures.
- If
ops.use_binary_mask = false, the pipeline performs automatic threshold-based segmentation. - If
ops.use_binary_mask = true, mask lookup is configurable from the selected config file inConfig/.
Set these fields in your selected config file (for example Config/config_evoked.m, Config/config_spontaneous.m, or multi-spec config defaults):
ops.binary_mask_location = "auto";
ops.binary_mask_pattern_filedir = 'MAX_Cell*_binary_*.tif';
ops.binary_mask_pattern_savedir = 'Cell*_*_binary.tif';
ops.binary_mask_pattern_savedir_parent_recursive = 'Cell*_*_binary.tif';
ops.binary_mask_group_size = 4;Supported ops.binary_mask_location values:
"filedir": search only in the same folder as the source image (ops.filename) usingops.binary_mask_pattern_filedir"savedir": search only in the per-file output folder (ops.savedir) usingops.binary_mask_pattern_savedir"savedir_parent_recursive": search recursively underparent(ops.savedir)usingops.binary_mask_pattern_savedir_parent_recursive"auto": search all locations above in that priority order
Savedir-specific mask selection rule:
- For masks found via
"savedir"or"savedir_parent_recursive", the script expects filenames like:-
CellX_num_binary.tif(for exampleCell1_3_binary.tif)
-
- Let
nbe the recording number from the image filenameCellX_n.tif. - Let
g = ops.binary_mask_group_size. - The target mask number is computed as:
$floor((n-1)/g) * g + 1$
- Only masks whose
nummatches that target are considered valid candidates.
Note for multi-condition configs:
- In
config_evoked_multi_spec*.m,ops.binary_mask_group_sizeis synced to top-levelNby default.
Examples:
- Image
Cell1_1.tifwithg=4-> target mask number$floor((1-1)/4)*4+1 = 1$ -> selectsCell1_1_binary.tif - Image
Cell1_4.tifwithg=4-> target mask number$floor((4-1)/4)*4+1 = 1$ -> selectsCell1_1_binary.tif - Image
Cell1_5.tifwithg=4-> target mask number$floor((5-1)/4)*4+1 = 5$ -> selectsCell1_5_binary.tif
Behavior when multiple files match after filtering:
- The script keeps the first match found in search order and emits a warning.
For each processed recording, output folders typically contain:
processed_data.mat: detected events, ROI/spike statistics, analysis outputs- Figure files (format from
ops.fig_format) ROI_pxMap/(if enabled)- Optional ImageJ-compatible TIFF masks (depending on save flags)
- Set
ops.visualize = falseto reduce runtime and memory usage. - Keep
ops.close_fig = truefor large batch runs. - Use
ops.redo_detection = falseto skip files that already haveprocessed_data.mat.
- Bio-Formats/Java errors:
- ensure the repository root is your MATLAB current folder
- ensure
Scripts/bfmatlab/is added (the pipeline does this automatically)
- No files processed:
- check
ops.fileformat - check
ops.filename_regex - verify your input folder tree under
ops.filedir
- check
- Poor detection quality:
- verify frame rate (
ops.fs) - tune thresholds in the relevant config file (
m_thres,SNR_thres, peak parameters)
- verify frame rate (
If you use this analysis pipeline, please cite:
Project wiki: https://github.com/f-hamidlab/iGluSNFR3/wiki/