suite2p.detection package#
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
mask_centers #
mask_centers(masks)
Compute the centers and diameters for all masks in a label image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
masks
|
ndarray
|
2D integer label image where each unique positive value is an ROI. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
centers |
ndarray
|
Median center coordinates of shape (n_masks, 2), as [ymed, xmed]. |
diams |
ndarray
|
Estimated diameters for each mask, shape (n_masks,). |
Source code in suite2p/detection/anatomical.py
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 | |
mask_stats #
mask_stats(mask)
Compute the median center and diameter of a single binary mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
2D binary mask for a single ROI. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ymed |
int
|
Y-coordinate of the pixel closest to the median center. |
xmed |
int
|
X-coordinate of the pixel closest to the median center. |
diam |
float
|
Estimated diameter of the mask, computed from the number of pixels. |
Source code in suite2p/detection/anatomical.py
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 | |
masks_to_stats #
masks_to_stats(masks, weights)
Convert a label image and weight map into an array of ROI statistics dictionaries.
For each mask, extracts the pixel coordinates, pixel weights from the weight image, and computes the median center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
masks
|
ndarray
|
2D integer label image where each unique positive value is an ROI. |
required |
weights
|
ndarray
|
2D weight image of the same shape as |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stats |
ndarray
|
Array of dictionaries, one per ROI, each containing "ypix", "xpix", "lam", "med", and "footprint". |
Source code in suite2p/detection/anatomical.py
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 | |
roi_detect #
roi_detect(mproj, diameter=None, settings=None, pretrained_model=None, device=torch.device('cuda'), chan2=False)
Detect ROIs in an image using Cellpose.
Runs a Cellpose model on the input image to segment cells. Optionally rescales the image if the diameter aspect ratio is not 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mproj
|
ndarray
|
2D image to segment, shape (Ly, Lx). |
required |
diameter
|
float or list of float
|
Expected cell diameter in pixels. If scalar, used for both axes. If list, [dy, dx]. Defaults to [30., 30.]. |
None
|
settings
|
dict
|
Detection settings dictionary. Used to get "params", "chan2_params", "cellprob_threshold", and "flow_threshold" for Cellpose. |
None
|
pretrained_model
|
str
|
Name of the Cellpose pretrained model. Defaults to "cpsam". |
None
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device, used for GPU cache cleanup after detection. |
device('cuda')
|
chan2
|
bool, optional (default False)
|
If True, use "chan2_params" from settings instead of "params". |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
masks |
ndarray
|
Integer label image of shape (Ly, Lx), where each ROI has a unique positive integer label. |
centers |
ndarray
|
Median center coordinates of shape (n_masks, 2). |
median_diam |
float
|
Median diameter across all detected masks. |
mask_diams |
ndarray
|
Diameters for each detected mask, shape (n_masks,), dtype int32. |
Source code in suite2p/detection/anatomical.py
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 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 | |
select_rois #
select_rois(mean_img, max_proj, settings, diameter=[12.0, 12.0], device=torch.device('cuda'))
Find ROIs in static images using Cellpose anatomical detection.
Prepares an image for segmentation based on the "img" setting (max projection / mean image ratio, mean image, or max projection), optionally applies spatial high-pass filtering, then runs Cellpose to detect ROIs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean_img
|
ndarray
|
2D mean image of shape (Ly, Lx). |
required |
max_proj
|
ndarray
|
2D maximum projection image of shape (Ly, Lx). |
required |
settings
|
dict
|
Detection settings dictionary. Must contain "img" (str specifying which image to segment) and optionally "highpass_spatial" (float). |
required |
diameter
|
list of float, optional (default [12., 12.])
|
Expected cell diameter [dy, dx] in pixels. |
[12.0, 12.0]
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for Cellpose and GPU cache cleanup. |
device('cuda')
|
Returns:
| Name | Type | Description |
|---|---|---|
new_settings |
dict
|
Dictionary with detection metadata including "diameter", "Vcorr", and placeholder keys "Vmax", "ihop", "Vsplit", "Vmap", "spatscale_pix". |
stats |
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix", "xpix", "lam", "med", and "footprint". |
Source code in suite2p/detection/anatomical.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
cellpose_overlap #
cellpose_overlap(stats, mimg2, diameter, chan2_threshold=0.25, device=torch.device('cuda'), settings=None)
Classify cells as red by computing overlap with Cellpose-detected masks.
Runs Cellpose on the red channel mean image to detect anatomical masks, then computes the intersection-over-union (IOU) between each ROI and the Cellpose masks. ROIs with IOU above the threshold are classified as red.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix" and "xpix". |
required |
mimg2
|
ndarray
|
Red channel mean image of shape (Ly, Lx). |
required |
diameter
|
float or list of float
|
Expected cell diameter in pixels for Cellpose detection. |
required |
chan2_threshold
|
float, optional (default 0.25)
|
IOU threshold for red cell classification. |
0.25
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for Cellpose and GPU cache cleanup. |
device('cuda')
|
settings
|
dict
|
Detection settings dictionary passed to Cellpose. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
redstats |
ndarray
|
Array of shape (n_cells, 2) where column 0 is the binary red cell label and column 1 is the maximum IOU with Cellpose masks. |
masks |
ndarray
|
Integer label image of Cellpose-detected masks in the red channel, shape (Ly, Lx). |
Source code in suite2p/detection/chan2detect.py
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 211 | |
correct_bleedthrough #
correct_bleedthrough(Ly, Lx, nblks, mimg, mimg2)
Subtract bleedthrough of the green channel into the red channel.
Uses non-rigid regression with nblks x nblks spatial blocks to estimate and remove the green-to-red bleedthrough, producing a corrected red channel mean image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
nblks
|
int
|
Number of spatial blocks along each axis for piecewise regression. |
required |
mimg
|
ndarray
|
Green channel mean image of shape (Ly, Lx). |
required |
mimg2
|
ndarray
|
Red channel mean image of shape (Ly, Lx). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mimg2 |
ndarray
|
Corrected red channel mean image with bleedthrough subtracted, clipped to non-negative values, shape (Ly, Lx). |
Source code in suite2p/detection/chan2detect.py
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 100 | |
detect #
detect(meanImg, meanImg_chan2, stats, diameter, cellpose_chan2=False, chan2_threshold=0.65, device=torch.device('cuda'), settings=None, inner_neuropil_radius=2, min_neuropil_pixels=350)
Identify red cells using the second channel (e.g. tdTomato).
Attempts Cellpose-based overlap detection first if cellpose_chan2 is
True. Falls back to intensity-ratio detection if Cellpose fails or is
disabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meanImg
|
ndarray
|
Green channel mean image of shape (Ly, Lx). |
required |
meanImg_chan2
|
ndarray
|
Red channel mean image of shape (Ly, Lx). |
required |
stats
|
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix", "xpix", and "lam". |
required |
diameter
|
float or list of float
|
Expected cell diameter in pixels for Cellpose detection. |
required |
cellpose_chan2
|
bool, optional (default False)
|
If True, attempt Cellpose-based red cell detection first. |
False
|
chan2_threshold
|
float, optional (default 0.65)
|
Threshold for red cell classification. Meaning depends on method: IOU threshold for Cellpose, intensity ratio for fallback. |
0.65
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for Cellpose and GPU cache cleanup. |
device('cuda')
|
settings
|
dict
|
Detection settings dictionary passed to Cellpose. |
None
|
inner_neuropil_radius
|
int, optional (default 2)
|
Radius in pixels of the inner exclusion zone for neuropil masks, used in intensity-ratio fallback. |
2
|
min_neuropil_pixels
|
int, optional (default 350)
|
Minimum number of neuropil pixels, used in intensity-ratio fallback. |
350
|
Returns:
| Name | Type | Description |
|---|---|---|
masks |
ndarray or None
|
Cellpose-detected masks in the red channel if Cellpose was used, otherwise None. |
redstats |
ndarray
|
Array of shape (n_cells, 2) where column 0 is the binary red cell label and column 1 is the red probability. |
Source code in suite2p/detection/chan2detect.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
intensity_ratio #
intensity_ratio(mimg2, stats, chan2_threshold=0.65, inner_neuropil_radius=2, min_neuropil_pixels=350)
Classify cells as red using the intensity ratio of cell to surround.
Computes the ratio of channel 2 intensity inside each cell mask to the intensity in the surrounding neuropil, excluding other cells. Cells with a ratio above the threshold are classified as red.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mimg2
|
ndarray
|
Red channel mean image of shape (Ly, Lx). |
required |
stats
|
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix", "xpix", and "lam". |
required |
chan2_threshold
|
float, optional (default 0.65)
|
Threshold on the intensity ratio for red cell classification. |
0.65
|
inner_neuropil_radius
|
int, optional (default 2)
|
Radius in pixels of the inner exclusion zone around each cell for neuropil mask creation. |
2
|
min_neuropil_pixels
|
int, optional (default 350)
|
Minimum number of pixels in each neuropil mask. |
350
|
Returns:
| Name | Type | Description |
|---|---|---|
redcell |
ndarray
|
Array of shape (n_cells, 2) where column 0 is the binary red cell label and column 1 is the red probability (intensity ratio). |
Source code in suite2p/detection/chan2detect.py
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 | |
quadrant_mask #
quadrant_mask(Ly, Lx, ny, nx, sT)
Create a smoothed binary mask for a rectangular block of the image.
Sets a rectangular region to 1 and applies Gaussian smoothing to create soft edges for blending in the bleedthrough correction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
ny
|
ndarray
|
Y-indices defining the block rows. |
required |
nx
|
ndarray
|
X-indices defining the block columns. |
required |
sT
|
float
|
Standard deviation for Gaussian smoothing of the mask. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mask |
ndarray
|
Smoothed mask of shape (Ly, Lx), dtype float32. |
Source code in suite2p/detection/chan2detect.py
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 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
pca_denoise #
pca_denoise(mov, block_size, n_comps_frac)
Denoise a movie using block-wise PCA reconstruction.
Splits the movie into spatial blocks, projects each block onto its top PCA components, reconstructs, and blends the blocks with spatial tapering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nframes, Ly, Lx). Modified in-place (mean subtracted then restored). |
required |
block_size
|
list of int
|
Block size [Lyb, Lxb] for spatial tiling. |
required |
n_comps_frac
|
float
|
Fraction of the smaller block dimension used to set the number of PCA components (number of PCs n_comps = min(Lyb, Lxb) * n_comps_frac). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
reconstruction |
ndarray
|
Denoised movie of shape (nframes, Ly, Lx). |
Source code in suite2p/detection/denoise.py
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 67 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
bin_movie #
bin_movie(f_reg, bin_size, yrange=None, xrange=None, badframes=None, nbins=5000)
Temporally bin the registered movie.
Reads batches of frames, excludes bad frames, crops to the valid
region, and averages groups of bin_size frames to produce a
downsampled movie.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_reg
|
ndarray or BinaryFile
|
Registered movie of shape (n_frames, Ly, Lx). |
required |
bin_size
|
int
|
Number of frames to average per bin. |
required |
yrange
|
list of int
|
Two-element list [y_start, y_end] defining the Y crop range. |
None
|
xrange
|
list of int
|
Two-element list [x_start, x_end] defining the X crop range. |
None
|
badframes
|
ndarray
|
Boolean array of shape (n_frames,) where True marks frames to exclude. |
None
|
nbins
|
int, optional (default 5000)
|
Maximum number of output binned frames. |
5000
|
Returns:
| Name | Type | Description |
|---|---|---|
mov |
ndarray
|
Binned movie of shape (num_binned_frames, Lyc, Lxc), dtype float32. |
Source code in suite2p/detection/detect.py
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 100 101 102 | |
detection_wrapper #
detection_wrapper(f_reg, diameter=[12.0, 12.0], tau=1.0, fs=30, meanImg_chan2=None, yrange=None, xrange=None, badframes=None, mov=None, preclassify=0.0, classifier_path=None, settings=default_settings()['detection'], device=torch.device('cuda'))
Run the full ROI detection pipeline on a registered movie.
Bins the movie in time, optionally denoises and high-pass filters, detects ROIs using the selected algorithm (sparsery, sourcery, or cellpose), computes ROI statistics, and optionally preclassifies and detects red cells in a second channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_reg
|
ndarray or BinaryFile
|
Registered movie of shape (n_frames, Ly, Lx). |
required |
diameter
|
list of float, optional (default [12., 12.])
|
Expected cell diameter [dy, dx] in pixels. |
[12.0, 12.0]
|
tau
|
float, optional (default 1.)
|
Timescale of the indicator in seconds, used to set bin size. |
1.0
|
fs
|
float, optional (default 30)
|
Sampling rate in Hz, used with tau to set bin size. |
30
|
meanImg_chan2
|
ndarray
|
Mean image of the second channel, shape (Ly, Lx). If provided, red cell detection is performed. |
None
|
yrange
|
list of int
|
Two-element list [y_start, y_end] defining the Y crop range. |
None
|
xrange
|
list of int
|
Two-element list [x_start, x_end] defining the X crop range. |
None
|
badframes
|
ndarray
|
Boolean array of shape (n_frames,) marking frames to exclude. |
None
|
mov
|
ndarray
|
Pre-binned movie of shape (nbinned, Lyc, Lxc). If provided, skips the binning step. |
None
|
preclassify
|
float, optional (default 0.)
|
If positive, apply a classifier and remove ROIs with probability below this threshold before final statistics. |
0.0
|
classifier_path
|
str
|
Path to a saved classifier file. If None and preclassify > 0, uses the default user classifier. |
None
|
settings
|
dict
|
Detection settings dictionary. |
default_settings()['detection']
|
device
|
torch.device, optional (default torch.device("cuda"))
|
Torch device for cellpose-based detection. |
device('cuda')
|
Returns:
| Name | Type | Description |
|---|---|---|
new_settings |
dict
|
Dictionary with detection metadata including "meanImg_crop", "max_proj", "diameter", and algorithm-specific keys. |
stat |
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix", "xpix", "lam", "med", and other computed statistics. |
redcell |
ndarray or None
|
Array of shape (n_cells, 2) with red cell labels and probabilities, or None if no second channel was provided. |
Source code in suite2p/detection/detect.py
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 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 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
compute_gt_matches #
compute_gt_matches(img, masks, stat_func, ops=None, reg_file=None, threshold=0.5)
anatomical img and masks matched to functional ROIs in stat_func
Source code in suite2p/detection/metrics.py
13 14 15 16 17 18 19 20 21 22 23 | |
match_func_anat #
match_func_anat(stat_func, stat_anat, Ly, Lx, threshold=0.5)
match functional ROIs to anatomical ROIs by correlation
Source code in suite2p/detection/metrics.py
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 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
connected_region #
connected_region(stat, connected, Ly, Lx)
Optionally restrict each ROI to its largest connected component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat
|
list of dict
|
List of ROI statistics dictionaries, each containing "ypix", "xpix", and "lam". |
required |
connected
|
bool
|
If True, apply connected component extraction to each ROI. |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stat |
list of dict
|
Updated list of ROI dictionaries. |
Source code in suite2p/detection/sourcery.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
create_neuropil_basis #
create_neuropil_basis(diameter, Ly, Lx)
Compute Fourier-based neuropil basis functions for the image.
Creates a set of 2D Fourier basis functions tiled across the image, used to model spatially varying neuropil contamination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diameter
|
float or list of float
|
Cell diameter used to determine the tiling density. |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
S |
ndarray
|
Normalized neuropil basis functions of shape (Ly, Lx, nbasis). |
Source code in suite2p/detection/sourcery.py
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
extendROI #
extendROI(ypix, xpix, Ly, Lx, niter=1)
Expand an ROI by one pixel in each cardinal direction.
Adds the 4-connected neighbors of the current pixel set and removes
any that fall outside the image boundaries. Repeated for niter
iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray
|
Y-coordinates of the current ROI pixels. |
required |
xpix
|
ndarray
|
X-coordinates of the current ROI pixels. |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
niter
|
int, optional (default 1)
|
Number of expansion iterations. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
ypix |
ndarray
|
Expanded Y-coordinates. |
xpix |
ndarray
|
Expanded X-coordinates. |
Source code in suite2p/detection/sourcery.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
getSVDdata #
getSVDdata(mov, diameter)
Compute SVD spatial components from temporally binned movie data.
Smooths each frame with a 2D Gaussian filter, computes the temporal covariance matrix, and returns the top SVD spatial components reshaped as images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Temporally binned movie of shape (nbins, Ly, Lx). |
required |
diameter
|
float or list of float
|
Cell diameter used to set the Gaussian smoothing sigma (sigma = diameter / 10). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
U |
ndarray
|
Spatial SVD components of shape (Ly, Lx, nsvd). |
u |
ndarray
|
Temporal SVD components of shape (nbins, nsvd). |
Source code in suite2p/detection/sourcery.py
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 | |
getSVDproj #
getSVDproj(mov, u, diameter, smooth_masks=False)
Project temporally binned movie data onto existing SVD temporal components.
Optionally smooths frames before projection. Used during refinement to recompute spatial components from updated movie data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Temporally binned movie of shape (nbins, Ly, Lx). |
required |
u
|
ndarray
|
Temporal SVD components of shape (nbins, nsvd). |
required |
diameter
|
float or list of float
|
Cell diameter used to set the Gaussian smoothing sigma. |
required |
smooth_masks
|
bool, optional (default False)
|
If True, smooth each frame before projection. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
U |
ndarray
|
Spatial SVD projections of shape (Ly, Lx, nsvd). |
Source code in suite2p/detection/sourcery.py
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 | |
getStU #
getStU(diameter, U)
Compute neuropil basis functions and their covariances with the SVD spatial components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diameter
|
float or list of float
|
Cell diameter used for neuropil basis construction. |
required |
U
|
ndarray
|
Spatial SVD components of shape (Ly, Lx, nsvd). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
S |
ndarray
|
Neuropil basis functions of shape (Ly, Lx, nbasis). |
StU |
ndarray
|
Cross-covariance of neuropil basis with SVD components, shape (nbasis, nsvd). |
StS |
ndarray
|
Auto-covariance of neuropil basis, shape (nbasis, nbasis). |
Source code in suite2p/detection/sourcery.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 | |
getVmap #
getVmap(Ucell, sig)
Compute the variance ratio map from SVD spatial components.
Smooths the spatial components and computes the ratio of smoothed variance to total variance, producing a map that highlights regions with locally correlated activity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Ucell
|
ndarray
|
Residual SVD spatial components of shape (Ly, Lx, nsvd). |
required |
sig
|
numpy.ndarray or list of float
|
Gaussian smoothing sigma [sy, sx] in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
log_variances |
ndarray
|
Variance ratio map of shape (Ly, Lx), dtype float64. |
us |
ndarray
|
Smoothed spatial components of shape (Ly, Lx, nsvd). |
Source code in suite2p/detection/sourcery.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_connected #
get_connected(Ly, Lx, stat)
Extract the connected component of an ROI starting from its brightest pixel.
Grows outward from the pixel with maximum weight, keeping only pixels that are contiguous and have non-zero weight.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
stat
|
dict
|
ROI statistics dictionary containing "ypix", "xpix", and "lam". Modified in-place. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stat |
dict
|
Updated ROI dictionary with connected pixels only. |
Source code in suite2p/detection/sourcery.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
iter_extend #
iter_extend(ypix, xpix, Ucell, code, refine=-1, change_codes=False)
Iteratively extend an ROI by projecting onto the SVD code vector.
Starting from seed pixels, repeatedly expands the region and keeps pixels whose projection onto the code vector exceeds a threshold. Stops when no new pixels are added or the region exceeds 10000 pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray or int
|
Initial Y-coordinates of the ROI seed. |
required |
xpix
|
ndarray or int
|
Initial X-coordinates of the ROI seed. |
required |
Ucell
|
ndarray
|
Residual SVD spatial components of shape (Ly, Lx, nsvd). |
required |
code
|
ndarray
|
SVD code vector for this ROI, shape (nsvd,). |
required |
refine
|
(int, optional(default - 1))
|
Refinement stage indicator. Negative means initial detection. |
-1
|
change_codes
|
bool, optional (default False)
|
If True and refine < 0, update the code vector during extension. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
ypix |
ndarray
|
Y-coordinates of the extended ROI. |
xpix |
ndarray
|
X-coordinates of the extended ROI. |
lam |
ndarray
|
Normalized pixel weights (projections onto code vector). |
ix |
ndarray
|
Boolean mask of pixels kept in the final iteration. |
code |
ndarray
|
Updated code vector, shape (nsvd,). |
Source code in suite2p/detection/sourcery.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
localMax #
localMax(V, footprint, thres)
Find local maxima of a correlation map above a threshold.
Uses a maximum filter with the given footprint to identify pixels that are local maxima and exceed the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
ndarray
|
2D correlation map of shape (Ly, Lx). |
required |
footprint
|
ndarray
|
2D boolean footprint for the maximum filter (usually circular). |
required |
thres
|
float
|
Minimum value for a local maximum to be included. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
i |
ndarray
|
Y-indices of local maxima, dtype int32. |
j |
ndarray
|
X-indices of local maxima, dtype int32. |
Source code in suite2p/detection/sourcery.py
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 | |
morphOpen #
morphOpen(V, footprint)
Compute the morphological opening of a correlation map.
Applies a minimum filter followed by a maximum filter (negated minimum of negation) using the given footprint to remove small bright features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
ndarray
|
2D correlation map of shape (Ly, Lx). |
required |
footprint
|
ndarray
|
2D boolean footprint for the morphological operation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
vrem |
ndarray
|
Morphologically opened image of shape (Ly, Lx). |
Source code in suite2p/detection/sourcery.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
sourcery #
sourcery(mov, sdmov, diameter, threshold_scaling=1.0, connected=True, max_iterations=20, smooth_masks=False)
Detect ROIs using the Sourcery algorithm (SVD-based iterative detection).
Computes SVD components of the movie, builds neuropil basis functions and subtracts them, and iteratively detects ROIs by finding local maxima in the variance ratio map and extending them via similarity in SVD projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Temporally binned movie of shape (nbins, Ly, Lx). Divided by
|
required |
sdmov
|
ndarray
|
Standard deviation of the movie, shape (Ly * Lx,) or (Ly, Lx), used for normalization. |
required |
diameter
|
float or list of float
|
Expected cell diameter in pixels. |
required |
threshold_scaling
|
float, optional (default 1.0)
|
Scaling factor for the peak detection threshold. |
1.0
|
connected
|
bool, optional (default True)
|
If True, restrict each detected ROI to its largest connected component. |
True
|
max_iterations
|
int, optional (default 20)
|
Maximum number of detection and refinement iterations. |
20
|
smooth_masks
|
bool, optional (default False)
|
If True, spatially smooth frames before SVD projection during refinement. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
new_settings |
dict
|
Dictionary with detection metadata including "diameter", "Vcorr", and placeholder keys "Vmax", "ihop", "Vsplit", "Vmap", "spatscale_pix". |
stat |
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix", "xpix", and "lam". |
Source code in suite2p/detection/sourcery.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
add_square #
add_square(yi, xi, lx, Ly, Lx)
Create a square mask of pixels around a peak, normalized to unit norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yi
|
int
|
Y-coordinate of the center pixel. |
required |
xi
|
int
|
X-coordinate of the center pixel. |
required |
lx
|
int
|
Side length of the square in pixels. |
required |
Ly
|
int
|
Height of the full image. |
required |
Lx
|
int
|
Width of the full image. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
y0 |
ndarray
|
Y-coordinates of the square pixels, flattened. |
x0 |
ndarray
|
X-coordinates of the square pixels, flattened. |
mask |
ndarray
|
Pixel weights normalized to unit L2 norm, flattened. |
Source code in suite2p/detection/sparsedetect.py
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 211 212 213 214 215 216 217 | |
downsample #
downsample(mov, taper_edge=True)
Downsample a movie by 2x in both spatial dimensions.
Averages adjacent pixels along Y then X. If a dimension has odd size,
the last pixel is halved when taper_edge is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (n_frames, Ly, Lx). |
required |
taper_edge
|
bool, optional (default True)
|
If True, halve the edge pixel when the dimension is odd. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
mov2 |
ndarray
|
Downsampled movie of shape (n_frames, ceil(Ly/2), ceil(Lx/2)). |
Source code in suite2p/detection/sparsedetect.py
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 | |
estimate_spatial_scale #
estimate_spatial_scale(I)
Estimate the dominant spatial scale from multi-scale correlation maps.
Finds the scale index that appears most frequently among the top 50 brightest local maxima in the max-projected correlation image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
I
|
ndarray
|
Multi-scale correlation maps of shape (n_scales, Ly, Lx). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
im |
int
|
Index of the estimated best spatial scale. |
Source code in suite2p/detection/sparsedetect.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
extendROI #
extendROI(ypix, xpix, Ly, Lx, niter=1)
Extend ROI pixel coordinates by growing the mask in 4-connected directions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray
|
Y-coordinates of the mask pixels. |
required |
xpix
|
ndarray
|
X-coordinates of the mask pixels. |
required |
Ly
|
int
|
Height of the image. |
required |
Lx
|
int
|
Width of the image. |
required |
niter
|
int, optional (default 1)
|
Number of dilation iterations. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
ypix |
ndarray
|
Extended Y-coordinates. |
xpix |
ndarray
|
Extended X-coordinates. |
Source code in suite2p/detection/sparsedetect.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
extend_mask #
extend_mask(ypix, xpix, lam, Ly, Lx)
Extend a mask into the 8 surrounding pixels of each pixel.
Each pixel spreads its weight equally to itself and its 8 neighbors. Overlapping contributions are summed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray
|
Y-coordinates of the mask pixels. |
required |
xpix
|
ndarray
|
X-coordinates of the mask pixels. |
required |
lam
|
ndarray
|
Pixel weights. |
required |
Ly
|
int
|
Height of the image. |
required |
Lx
|
int
|
Width of the image. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ypix1 |
ndarray
|
Y-coordinates of the extended mask. |
xpix1 |
ndarray
|
X-coordinates of the extended mask. |
lam1 |
ndarray
|
Accumulated pixel weights for the extended mask. |
Source code in suite2p/detection/sparsedetect.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
find_best_scale #
find_best_scale(I, spatial_scale)
Determine the best spatial scale, either forced or estimated from data.
If spatial_scale is positive, clamps it to [1, 4] and returns it as
forced. Otherwise estimates the scale from the multi-scale correlation
maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
I
|
ndarray
|
Multi-scale correlation maps of shape (n_scales, Ly, Lx). |
required |
spatial_scale
|
int
|
User-specified spatial scale. If positive, used directly (forced). If zero or negative, the scale is estimated from the data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scale |
int
|
Best spatial scale index. |
estimate_mode |
EstimateMode
|
Whether the scale was |
Source code in suite2p/detection/sparsedetect.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
iter_extend #
iter_extend(ypix, xpix, mov, Lyc, Lxc, active_frames)
Iteratively extend a mask based on pixel activity on active frames.
Repeatedly grows the ROI by one pixel on each side, keeping only pixels whose mean activity on active frames exceeds 1/5 of the maximum pixel. Stops when the mask stops growing or reaches 10000 pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray
|
Y-coordinates of the initial mask pixels. |
required |
xpix
|
ndarray
|
X-coordinates of the initial mask pixels. |
required |
mov
|
ndarray
|
Binned residual movie of shape (nbinned, Lyc * Lxc). |
required |
Lyc
|
int
|
Height of the movie frame. |
required |
Lxc
|
int
|
Width of the movie frame. |
required |
active_frames
|
ndarray
|
Indices of frames used to compute activity. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ypix |
ndarray
|
Y-coordinates of the extended mask. |
xpix |
ndarray
|
X-coordinates of the extended mask. |
lam |
ndarray
|
Pixel weights normalized to unit L2 norm. |
Source code in suite2p/detection/sparsedetect.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
multiscale_mask #
multiscale_mask(ypix0, xpix0, lam0, Lyp, Lxp)
Downsample a mask to all spatial scales used in sparse detection.
Given pixel coordinates and weights at the original resolution, creates downsampled versions by successively halving coordinates and accumulating weights, then extends each mask into surrounding pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix0
|
ndarray
|
Y-coordinates of the mask pixels at full resolution. |
required |
xpix0
|
ndarray
|
X-coordinates of the mask pixels at full resolution. |
required |
lam0
|
ndarray
|
Pixel weights at full resolution. |
required |
Lyp
|
ndarray
|
Heights of the downsampled images at each scale, shape (n_scales,). |
required |
Lxp
|
ndarray
|
Widths of the downsampled images at each scale, shape (n_scales,). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ys |
list of numpy.ndarray
|
Y-coordinates of the mask at each spatial scale. |
xs |
list of numpy.ndarray
|
X-coordinates of the mask at each spatial scale. |
lms |
list of numpy.ndarray
|
Pixel weights at each spatial scale. |
Source code in suite2p/detection/sparsedetect.py
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 173 174 175 176 177 178 | |
neuropil_subtraction #
neuropil_subtraction(mov, filter_size)
Subtract a spatially low-pass filtered version of the movie to remove neuropil.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nbinned, Ly, Lx). |
required |
filter_size
|
int
|
Width of the uniform spatial filter in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
movt |
ndarray
|
High-pass filtered movie of shape (nbinned, Ly, Lx). |
Source code in suite2p/detection/sparsedetect.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
sparsery #
sparsery(mov, sdmov, highpass_neuropil, spatial_scale, threshold_scaling, max_ROIs, active_percentile=0)
Detect ROIs in a movie using doubly-sparse matrix decomposition.
Subtracts neuropil, builds multi-scale correlation maps, then greedily chooses top activity peak, extends mask, optionally splits the ROI, and subtracts the detected signal from the residual movie, then extracts the highest peak from the residual and continues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Binned movie of shape (nbinned, Ly, Lx). |
required |
sdmov
|
ndarray
|
Per-pixel standard deviation of shape (Ly, Lx), used for normalization. |
required |
highpass_neuropil
|
int
|
Filter size for spatial high-pass neuropil subtraction. |
required |
spatial_scale
|
int
|
Spatial scale setting. If positive, forced; if zero or negative, estimated from the data. |
required |
threshold_scaling
|
float
|
Multiplier for the activity threshold used to accept peaks. |
required |
max_ROIs
|
int
|
Maximum number of ROIs to detect. |
required |
active_percentile
|
float, optional (default 0)
|
If positive, use this percentile of the temporal projection as an alternative activity threshold. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
new_settings |
dict
|
Dictionary with detection metadata including "Vmax", "ihop", "Vsplit", "Vcorr", "Vmap", and "spatscale_pix". |
stats |
list of dict
|
List of ROI statistics dictionaries, each containing "ypix", "xpix", "lam", "med", and "footprint". |
Source code in suite2p/detection/sparsedetect.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
square_convolution_2d #
square_convolution_2d(mov, filter_size)
Convolve each frame with a square uniform kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nbinned, Ly, Lx). |
required |
filter_size
|
int
|
Width of the square uniform kernel in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
movt |
ndarray
|
Convolved movie of shape (nbinned, Ly, Lx), dtype float32. |
Source code in suite2p/detection/sparsedetect.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
threshold_reduce #
threshold_reduce(mov, intensity_threshold)
Compute thresholded standard deviation map across frames.
For each pixel, sums the squared values of frames exceeding the threshold, then takes the square root. Iterates frame-by-frame to reduce memory usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nbinned, Ly, Lx). |
required |
intensity_threshold
|
float
|
Only frames where the pixel value exceeds this are included. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Vt |
ndarray
|
Thresholded standard deviation map of shape (Ly, Lx), dtype float32. |
Source code in suite2p/detection/sparsedetect.py
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 | |
two_comps #
two_comps(mpix0, lam, Th2)
Check if splitting an ROI into two components increases variance explained.
Projects the ROI movie onto the current mask, then tests whether a two-component split captures more variance. Returns the variance ratio and the better component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mpix0
|
ndarray
|
Binned movie for pixels in the ROI, shape (nbinned, npix). |
required |
lam
|
ndarray
|
Pixel weights for the current ROI. |
required |
Th2
|
float
|
Intensity threshold for determining active frames. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
vrat |
float
|
Ratio of variance explained by two components to one component. Values above 1.25 suggest the ROI should be split. |
ipick |
tuple
|
Tuple of (mu, xproj, goodframe) for the better component, where mu is the pixel weights, xproj is the temporal projection on active frames, and goodframe is the boolean active-frame mask. |
Source code in suite2p/detection/sparsedetect.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
assign_overlaps #
assign_overlaps(stats, Ly, Lx)
Assign overlap labels to each ROI based on shared pixels.
For each ROI, sets an "overlap" boolean mask indicating which of its pixels are shared with at least one other ROI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
ndarray
|
Array of ROI statistics dictionaries, each containing "ypix" and "xpix". |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stats |
ndarray
|
Updated array with "overlap" key added to each ROI dictionary. |
Source code in suite2p/detection/stats.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
fitMVGaus #
fitMVGaus(y, x, lam0, dy, dx, thres=2.5, npts=100)
Fit a 2D Gaussian to weighted pixel coordinates and return an ellipse.
Computes the mean and covariance of the pixel distribution weighted by lam0,
then generates an ellipse at thres standard deviations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ndarray
|
Y-coordinates of the pixels. |
required |
x
|
ndarray
|
X-coordinates of the pixels. |
required |
lam0
|
ndarray
|
Weights for each pixel (e.g. fluorescence intensity). |
required |
dy
|
float
|
Normalization factor for the y-axis (e.g. cell diameter in y). |
required |
dx
|
float
|
Normalization factor for the x-axis (e.g. cell diameter in x). |
required |
thres
|
float, optional (default 2.5)
|
Number of standard deviations for the ellipse radius. |
2.5
|
npts
|
int, optional (default 100)
|
Number of points used to draw the ellipse. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
mu |
ndarray
|
Mean of the Gaussian fit, shape (2,). |
cov |
ndarray
|
Covariance matrix of the Gaussian fit, shape (2, 2). |
radii |
ndarray
|
Radii of the major and minor axes (sorted descending), shape (2,). |
ellipse |
ndarray
|
Points on the fitted ellipse, shape (npts, 2). |
Source code in suite2p/detection/stats.py
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 | |
median_pix #
median_pix(ypix, xpix)
Find the pixel closest to the median of a set of pixel coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray
|
Y-coordinates of the pixels. |
required |
xpix
|
ndarray
|
X-coordinates of the pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
med |
list of float
|
Two-element list [ymed, xmed] of the pixel closest to the median. |
Source code in suite2p/detection/stats.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
roi_stats #
roi_stats(stats, Ly, Lx, diameter=[12.0, 12.0], max_overlap=0.75, do_soma_crop=True, npix_norm_min=-1, npix_norm_max=np.inf, median=False)
Compute statistics for detected ROIs, including compactness, aspect ratio, and overlap.
For each ROI, computes the median center, soma crop, compactness, and aspect ratio from a 2D Gaussian fit. Normalizes pixel counts across ROIs, removes ROIs outside the normalized pixel range, and optionally removes ROIs with excessive overlap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
ndarray
|
Array of dictionaries, each containing "ypix", "xpix", and "lam" for one detected ROI. |
required |
Ly
|
int
|
Height of the image in pixels. |
required |
Lx
|
int
|
Width of the image in pixels. |
required |
diameter
|
list of float, optional (default [12., 12.])
|
Expected cell diameter [dy, dx] in pixels, used for normalization. |
[12.0, 12.0]
|
max_overlap
|
float, optional (default 0.75)
|
Maximum allowed fraction of overlapping pixels. ROIs exceeding this are removed. Set to None or 1.0 to disable. |
0.75
|
do_soma_crop
|
bool, optional (default True)
|
If True, crop dendritic pixels before computing compactness and aspect ratio. |
True
|
npix_norm_min
|
(float, optional(default - 1))
|
Minimum normalized pixel count. ROIs below this are removed. |
-1
|
npix_norm_max
|
float, optional (default np.inf)
|
Maximum normalized pixel count. ROIs above this are removed. |
inf
|
median
|
bool, optional (default False)
|
If True, use median of all ROIs for normalization. If False, use median of the 100 ROIs extracted first. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
stats |
ndarray
|
Updated array of ROI statistics dictionaries with added keys "med", "npix", "soma_crop", "npix_soma", "mrs", "mrs0", "compact", "radius", "aspect_ratio", "footprint", "npix_norm", "npix_norm_no_crop", and "overlap". |
Source code in suite2p/detection/stats.py
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 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 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 247 248 249 250 251 252 253 254 255 256 257 | |
soma_crop #
soma_crop(ypix, xpix, lam, med)
Crop dendritic pixels from an ROI by finding the soma boundary.
Computes cumulative weighted area as a function of distance from the median center, then finds the radius where the area growth drops below a threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ypix
|
ndarray
|
Y-coordinates of the ROI pixels. |
required |
xpix
|
ndarray
|
X-coordinates of the ROI pixels. |
required |
lam
|
ndarray
|
Weights (e.g. fluorescence) for each pixel. |
required |
med
|
list of float
|
Two-element list [ymed, xmed] of the ROI center. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
crop |
ndarray
|
Boolean array of length len(ypix), True for pixels within the soma. |
Source code in suite2p/detection/stats.py
93 94 95 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 | |
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
circleMask #
circleMask(d0)
Create a normalized distance array and return indices within a unit circle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d0
|
list of float
|
Two-element list [d0_y, d0_x] giving the radius in each dimension. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rs |
ndarray
|
Normalized distance array of shape (2d0_y+1, 2d0_x+1). |
dx |
ndarray
|
Normalized X-coordinates of pixels within the unit circle. |
dy |
ndarray
|
Normalized Y-coordinates of pixels within the unit circle. |
Source code in suite2p/detection/utils.py
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 | |
hp_gaussian_filter #
hp_gaussian_filter(mov, width)
Returns a high-pass-filtered mov by subtracting off the movie smoothed by a gaussian kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nframes, Ly, Lx). |
required |
width
|
int
|
The standard deviation of the Gaussian filter in time |
required |
Returns:
| Name | Type | Description |
|---|---|---|
filtered_mov |
nImg x Ly x Lx
|
The filtered video |
Source code in suite2p/detection/utils.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
hp_rolling_mean_filter #
hp_rolling_mean_filter(mov, width)
Returns high-pass-filtered mov by subtracting off the rolling mean in window of width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nframes, Ly, Lx). |
required |
width
|
int
|
The filter width in time. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
filtered_mov |
ndarray
|
Movie of shape (nframes, Ly, Lx), high-pass filtered in time. |
Source code in suite2p/detection/utils.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
square_mask #
square_mask(mask, ly, yi, xi)
Crop a square patch from a 2D mask centered at a given position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
2D array to crop from, shape (Lyc, Lxc). |
required |
ly
|
int
|
Half-width of the square patch (output is 2ly x 2ly). |
required |
yi
|
int
|
Y-coordinate of the center. |
required |
xi
|
int
|
X-coordinate of the center. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mask0 |
ndarray
|
Cropped square patch of shape (2ly, 2ly). |
Source code in suite2p/detection/utils.py
10 11 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 | |
standard_deviation_over_time #
standard_deviation_over_time(mov, batch_size)
Returns standard deviation of difference between pixels across time, computed in batches of batch_size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nframes, Ly, Lx). |
required |
batch_size
|
int
|
The batch size for computing the standard deviation of the difference. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sdmov |
Ly x Lx
|
The standard deviation of the difference across time for each pixel. |
Source code in suite2p/detection/utils.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
temporal_high_pass_filter #
temporal_high_pass_filter(mov, width)
Returns hp-filtered mov over time, selecting an algorithm for computational performance based on the kernel width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mov
|
ndarray
|
Movie of shape (nframes, Ly, Lx). |
required |
width
|
int
|
The filter width in time. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
filtered_mov |
ndarray
|
Movie of shape (nframes, Ly, Lx), high-pass filtered in time. |
Source code in suite2p/detection/utils.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |