suite2p.extraction package#
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
baseline_maximin #
baseline_maximin(F, win_baseline, sig_baseline, fs, batch_size=100, device=torch.device('cuda'))
Compute maximin baseline using GPU-accelerated max/min pooling.
Smooths traces with a Gaussian filter, then applies a rolling minimum followed by a rolling maximum to estimate the baseline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
ndarray
|
Fluorescence traces, shape (n_neurons, n_frames). |
required |
win_baseline
|
float
|
Window in seconds for the max/min filters. |
required |
sig_baseline
|
float
|
Standard deviation of the Gaussian filter in frames. |
required |
fs
|
float
|
Sampling rate per plane in Hz. |
required |
batch_size
|
int, optional (default 100)
|
Number of neurons processed per batch. |
100
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for performing operations. |
device('cuda')
|
Returns:
| Name | Type | Description |
|---|---|---|
F |
ndarray
|
Baseline-corrected fluorescence, shape (n_neurons, n_frames). |
Source code in suite2p/extraction/dcnv.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
oasis #
oasis(F, batch_size, tau, fs)
Compute non-negative deconvolution with no sparsity constraints.
Uses OASIS algorithm (Friedrich, Zhou & Paninski, 2017).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
ndarray
|
Fluorescence traces, shape (n_neurons, n_frames). In the pipeline, uses neuropil-subtracted and baselined fluorescence. |
required |
batch_size
|
int
|
Number of neurons processed per batch. |
required |
tau
|
float
|
Timescale of the indicator decay in seconds. |
required |
fs
|
float
|
Sampling rate per plane in Hz. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
S |
ndarray
|
Deconvolved fluorescence, shape (n_neurons, n_frames). |
Source code in suite2p/extraction/dcnv.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
oasis_matrix #
oasis_matrix(F, v, w, t, l, s, tau, fs)
Spike deconvolution on many neurons parallelized with prange.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
ndarray
|
Fluorescence traces, shape (n_neurons, n_frames). |
required |
v
|
ndarray
|
Pool values buffer, shape (n_neurons, n_frames). |
required |
w
|
ndarray
|
Pool weights buffer, shape (n_neurons, n_frames). |
required |
t
|
ndarray
|
Pool start times buffer, shape (n_neurons, n_frames). |
required |
l
|
ndarray
|
Pool lengths buffer, shape (n_neurons, n_frames). |
required |
s
|
ndarray
|
Output spike traces, shape (n_neurons, n_frames). Modified in place. |
required |
tau
|
float
|
Timescale of the indicator decay in seconds. |
required |
fs
|
float
|
Sampling rate per plane in Hz. |
required |
Source code in suite2p/extraction/dcnv.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
oasis_trace #
oasis_trace(F, v, w, t, l, s, tau, fs)
Spike deconvolution on a single neuron using the OASIS algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
ndarray
|
Fluorescence trace, shape (n_frames,). |
required |
v
|
ndarray
|
Pool values buffer, shape (n_frames,). |
required |
w
|
ndarray
|
Pool weights buffer, shape (n_frames,). |
required |
t
|
ndarray
|
Pool start times buffer, shape (n_frames,). |
required |
l
|
ndarray
|
Pool lengths buffer, shape (n_frames,). |
required |
s
|
ndarray
|
Output spike trace, shape (n_frames,). Modified in place. |
required |
tau
|
float
|
Timescale of the indicator decay in seconds. |
required |
fs
|
float
|
Sampling rate per plane in Hz. |
required |
Source code in suite2p/extraction/dcnv.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
preprocess #
preprocess(F, baseline, win_baseline, sig_baseline, fs, prctile_baseline=8, batch_size=100, device=torch.device('cuda'))
Preprocess fluorescence traces for spike deconvolution.
Subtracts a baseline estimated with a rolling maximin filter, constant minimum, or percentile, depending on the baseline setting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
ndarray
|
Fluorescence traces, shape (n_neurons, n_frames). In the pipeline, uses neuropil-subtracted fluorescence. |
required |
baseline
|
str
|
Method for computing the baseline of each trace. One of "maximin", "constant", or "prctile". |
required |
win_baseline
|
float
|
Window in seconds for the max/min filters. |
required |
sig_baseline
|
float
|
Standard deviation of the Gaussian filter in frames. |
required |
fs
|
float
|
Sampling rate per plane in Hz. |
required |
prctile_baseline
|
float, optional (default 8)
|
Percentile of trace to use as baseline when baseline is "prctile". |
8
|
Returns:
| Name | Type | Description |
|---|---|---|
F |
ndarray
|
Baseline-corrected fluorescence, shape (n_neurons, n_frames). |
Source code in suite2p/extraction/dcnv.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
extract_traces #
extract_traces(f_in, cell_masks, neuropil_masks, batch_size=500, device=torch.device('cuda'))
Extract fluorescence traces using cell and neuropil masks.
Performs sparse matrix multiplication on the GPU to extract ROI and neuropil traces from registered frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_in
|
ndarray or BinaryFile
|
Registered frames, shape (n_frames, Ly, Lx). |
required |
cell_masks
|
list of tuple
|
Each element is a tuple (pixel_indices, weights) for one ROI, where pixel_indices are flattened and weights are normalized to sum to 1. |
required |
neuropil_masks
|
list of numpy.ndarray
|
Each element is an array of flattened pixel indices for the neuropil mask of one ROI. |
required |
batch_size
|
int, optional (default 500)
|
Number of frames processed per batch. |
500
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for performing operations. |
device('cuda')
|
Returns:
| Name | Type | Description |
|---|---|---|
F |
ndarray
|
ROI fluorescence traces, shape (n_rois, n_frames). |
Fneu |
ndarray
|
Neuropil fluorescence traces, shape (n_rois, n_frames). |
Source code in suite2p/extraction/extract.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
extraction_wrapper #
extraction_wrapper(stat, f_reg, f_reg_chan2=None, cell_masks=None, neuropil_masks=None, settings=default_settings()['extraction'], device=torch.device('cuda'))
Main fluorescence extraction function.
Creates cell and neuropil masks (if not provided) and extracts fluorescence traces for both functional and anatomical channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat
|
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix", "xpix", "lam", "radius", and "overlap". |
required |
f_reg
|
ndarray or BinaryFile
|
Registered functional frames, shape (n_frames, Ly, Lx). |
required |
f_reg_chan2
|
numpy.ndarray or BinaryFile, optional (default None)
|
Registered anatomical channel frames, shape (n_frames, Ly, Lx). |
None
|
cell_masks
|
list of tuple, optional (default None)
|
Pre-computed cell masks. If None, masks are created from stat. |
None
|
neuropil_masks
|
list of numpy.ndarray, optional (default None)
|
Pre-computed neuropil masks. If None, masks are created from stat. |
None
|
settings
|
dict
|
Extraction settings dictionary. |
default_settings()['extraction']
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for performing operations. |
device('cuda')
|
Returns:
| Name | Type | Description |
|---|---|---|
F |
ndarray
|
ROI fluorescence traces for functional channel, shape (n_rois, n_frames). |
Fneu |
ndarray
|
Neuropil fluorescence traces for functional channel, shape (n_rois, n_frames). |
F_chan2 |
ndarray or None
|
ROI fluorescence traces for anatomical channel, or None if f_reg_chan2 is not provided. |
Fneu_chan2 |
ndarray or None
|
Neuropil fluorescence traces for anatomical channel, or None if f_reg_chan2 is not provided. |
Source code in suite2p/extraction/extract.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
create_cell_mask #
create_cell_mask(stat, Ly, Lx, allow_overlap=False)
Create the cell mask for a single ROI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat
|
dict
|
ROI statistics dictionary containing "ypix", "xpix", "lam", and "overlap". |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
allow_overlap
|
bool, optional (default False)
|
If True, include overlapping pixels in the cell mask. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
cell_mask |
ndarray
|
Flattened pixel indices for the ROI. |
lam_normed |
ndarray
|
Normalized pixel weights summing to 1. |
Source code in suite2p/extraction/masks.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
create_cell_pix #
create_cell_pix(stats, Ly, Lx, lam_percentile=50.0)
Create a binary image indicating which pixels contain a cell.
Pixels with low cell weights can be excluded using lam_percentile; set lam_percentile to 0 to disable filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
list of dict
|
List of ROI statistics dictionaries, each containing "ypix", "xpix", "lam", and "radius". |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
lam_percentile
|
float, optional (default 50.0)
|
Percentile filter threshold for excluding low-weight pixels. |
50.0
|
Returns:
| Name | Type | Description |
|---|---|---|
cell_pix |
ndarray
|
Boolean array of shape (Ly, Lx), True where a cell pixel exists. |
Source code in suite2p/extraction/masks.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
create_masks #
create_masks(stats, Ly, Lx, lam_percentile=50.0, allow_overlap=False, neuropil_extract=True, inner_neuropil_radius=2, min_neuropil_pixels=350, circular_neuropil=False)
Create cell and neuropil masks for all ROIs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
list of dict
|
List of ROI statistics dictionaries, each containing "ypix", "xpix", "lam", "radius", and "overlap". |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
lam_percentile
|
float, optional (default 50.)
|
Percentile filter threshold for excluding low-weight cell pixels from neuropil masks. Set to 0 to disable. |
50.0
|
allow_overlap
|
bool, optional (default False)
|
If True, include overlapping pixels in cell masks. |
False
|
neuropil_extract
|
bool, optional (default True)
|
If True, compute neuropil masks. |
True
|
inner_neuropil_radius
|
int, optional (default 2)
|
Number of pixels to extend around each ROI as an exclusion zone for the neuropil mask. |
2
|
min_neuropil_pixels
|
int, optional (default 350)
|
Minimum number of pixels in the neuropil mask. |
350
|
circular_neuropil
|
bool, optional (default False)
|
If True, extend neuropil masks circularly rather than as rectangles (SLOW). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
cell_masks |
list of tuple
|
Each element is a tuple (pixel_indices, weights) for one ROI. |
neuropil_masks |
list of numpy.ndarray or None
|
Each element is an array of flattened pixel indices for the neuropil mask. None if neuropil_extract is False. |
Source code in suite2p/extraction/masks.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
create_neuropil_masks #
create_neuropil_masks(ypixs, xpixs, cell_pix, inner_neuropil_radius=2, min_neuropil_pixels=350, circular=False)
Create surround neuropil masks for ROIs by extending each ROI outward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypixs
|
list of numpy.ndarray
|
Y-coordinates of the pixels for each ROI. |
required |
xpixs
|
list of numpy.ndarray
|
X-coordinates of the pixels for each ROI. |
required |
cell_pix
|
ndarray
|
Binary array of shape (Ly, Lx), True where a cell pixel exists. These pixels are excluded from neuropil masks. |
required |
inner_neuropil_radius
|
int, optional (default 2)
|
Number of pixels to extend around each ROI as an exclusion zone. |
2
|
min_neuropil_pixels
|
int, optional (default 350)
|
Minimum number of pixels in each neuropil mask. |
350
|
circular
|
bool, optional (default False)
|
If True, extend neuropil masks circularly (slower). If False, extend as rectangles. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
neuropil_ipix |
list of numpy.ndarray
|
Each element is an array of flattened pixel indices for the neuropil mask of one ROI. |
Source code in suite2p/extraction/masks.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |