CNMF#
Accessor: cnmf
- class mesmerize_core.CNMFExtensions(s)#
Extensions for managing CNMF output data
- Parameters:
s (Series) –
- get_cnmf_memmap(mode='r')#
Get the CNMF C-order memmap. This should NOT be used for viewing the movie frames use
caiman.get_input_movie()
for that purpose.- Parameters:
mode (str) –
passed to numpy.memmap
one of: {‘r+’, ‘r’, ‘w+’, ‘c’}
The file is opened in this mode:
’r’
Open existing file for reading only.
’r+’
Open existing file for reading and writing.
’w+’
Create or overwrite existing file for reading and writing.
’c’
Copy-on-write: assignments affect data in memory, but changes are not saved to disk. The file on disk is read-only.
- Returns:
numpy memmap array used for CNMF
- Return type:
np.ndarray
- get_output_path()#
Get the path to the cnmf hdf5 output file.
Note: You generally want to work with the other extensions instead of directly using the hdf5 file.
- Returns:
full path to the caiman-format CNMF hdf5 output file
- Return type:
Path
- get_output(return_copy=True)#
- Parameters:
return_copy (bool) –
ifTrue
returns a copy of the cached value in memory.ifFalse
returns the same object as the cached value in memory, not recommend this could result in strange unexpected behavior.In general you want a copy of the cached value.- Returns:
Returns the Caiman CNMF object
- Return type:
CNMF
Examples
Load the CNMF model with estimates from the hdf5 file.
from mesmerize_core import load_batch df = load_batch("/path/to/batch_dataframe_file.pickle") # assume the 0th index is a cnmf item cnmf_obj = df.iloc[0].cnmf.get_output() # see some estimates print(cnmf_obj.estimates.C) print(cnmf_obj.estimates.f)
- get_masks(component_indices=None, threshold=0.01, return_copy=True)#
- Get binary masks of the spatial components at the given
component_indices
.Created fromCNMF.estimates.A
- Parameters:
component_indices (str or np.ndarray, optional) –
indices of the components to includeif not provided,None
, or"all"
uses all componentsif"good"
uses good components, i.e.Estimates.idx_components
if"bad"
uses bad components, i.e.Estimates.idx_components_bad
ifnp.ndarray
, uses the indices in the provided arraythreshold (float) – threshold
return_copy (bool) –
ifTrue
returns a copy of the cached value in memory.ifFalse
returns the same object as the cached value in memory, not recommend this could result in strange unexpected behavior.In general you want a copy of the cached value.
- Returns:
shape is [dim_0, dim_1, n_components]
- Return type:
np.ndarray
- get_contours(component_indices=None, swap_dim=True, return_copy=True)#
Get the contour and center of mass for each spatial footprint
- Parameters:
component_indices (str or np.ndarray, optional) –
indices of the components to includeif not provided,None
, or"all"
uses all componentsif"good"
uses good components, i.e.Estimates.idx_components
if"bad"
uses bad components, i.e.Estimates.idx_components_bad
ifnp.ndarray
, uses the indices in the provided arrayswap_dim (bool) – swap the x and y coordinates, use if the contours don’t align with the cells in your image
return_copy (bool) –
ifTrue
returns a copy of the cached value in memory.ifFalse
returns the same object as the cached value in memory, not recommend this could result in strange unexpected behavior.In general you want a copy of the cached value.
- Returns:
- (List[coordinates array], List[centers of masses array])each array of coordinates is 2D, [xs, ys]each center of mass is [x, y]
- Return type:
Tuple[List[np.ndarray], List[np.ndarray]]
- get_temporal(component_indices=None, add_background=False, add_residuals=False, return_copy=True)#
Get the temporal components for this CNMF item, basically
CNMF.estimates.C
- Parameters:
component_indices (str or np.ndarray, optional) –
indices of the components to includeif not provided,None
, or"all"
uses all componentsif"good"
uses good components, i.e.Estimates.idx_components
if"bad"
uses bad components, i.e.Estimates.idx_components_bad
ifnp.ndarray
, uses the indices in the provided arrayadd_background (bool, default False) – if
True
, add the temporal background, addscnmf.estimates.f
add_residuals (bool, default False) – if
True
, add residuals, i.e.cnmf.estimates.YrA
return_copy (bool) –
ifTrue
returns a copy of the cached value in memory.ifFalse
returns the same object as the cached value in memory, not recommend this could result in strange unexpected behavior.In general you want a copy of the cached value.
- Returns:
shape is [n_components, n_frames]
- Return type:
np.ndarray
Examples
Plot the temporal components as a heatmap
from mesmerize_core import load_batch from fastplotlib import Plot df = load_batch("/path/to/batch_dataframe_file.pickle") # assumes 0th index is a cnmf batch item temporal = df.iloc[0].cnmf.get_temporal() plot = Plot() plot.add_line_collection(temporal) plot.show()
- get_rcm(component_indices=None, temporal_components=None, return_copy=False)#
Return the reconstructed movie with no background, i.e.
A ⊗ C
, as aLazyArray
. This is an array that performs lazy computation of the reconstructed movie only upon indexing.- Parameters:
component_indices (optional, Union[np.ndarray, str]) –
indices of the components to includeifnp.ndarray
, uses these indices in the provided arrayif"good"
uses good components, i.e. cnmf.estimates.idx_componentsif"bad"
uses bad components, i.e. cnmf.estimates.idx_components_badif not provided,None
, or"all"
uses all componentstemporal_components (optional, np.ndarray) –
temporal components to use as
C
for computing reconstructed movie.usescnmf.estimates.C
if not provideduseful if you want to create the reconstructed movie using dF/Fo, z-scored data, etc.return_copy (bool, default
False
) –ifTrue
returns a copy of the cached value in memory.ifFalse
returns the same object as the cached value in memoryFalse
is used by default when returningLazyArrays
for technical reasons
- Returns:
shape is [n_frames, x_dims, y_dims]
- Return type:
Examples
This example uses fastplotlib to display the reconstructed movie from a CNMF item that has already been run.
fastplotlib code must be run in a notebookSee the demo notebooks for more detailed examples.from mesmerize_core import * from fastplotlib.widgets import ImageWidget # load existing batch df = load_batch("/path/to/batch.pickle") # get the reconstructed movie as LazyArray # assumes the last index, `-1`, is a cnmf item # uses only the "good" components rcm = df.iloc[-1].cnmf.get_rcm(component_indices="good") # view with ImageWidget iw = ImageWidget(data=rcm) iw.show()
- get_rcb()#
Return the reconstructed background,
(b ⊗ f)
- Returns:
shape is [n_frames, x_dims, y_dims]
- Return type:
Examples
This example uses fastplotlib to display the reconstructed movie from a CNMF item that has already been run.
fastplotlib code must be run in a notebookSee the demo notebooks for more detailed examples.from mesmerize_core import * from fastplotlib.widgets import ImageWidget # load existing batch df = load_batch("/path/to/batch.pickle") # get the reconstructed background as a LazyArray # assumes the last index, `-1`, is a cnmf item rcb = df.iloc[-1].cnmf.get_rcb() # view with ImageWidget iw = ImageWidget(data=rcb) iw.show()
- get_residuals()#
Return residuals,
Y - (A ⊗ C) - (b ⊗ f)
- Returns:
shape is [n_frames, x_dims, y_dims]
- Return type:
Examples
This example uses fastplotlib to display the reconstructed movie from a CNMF item that has already been run.
fastplotlib code must be run in a notebookSee the demo notebooks for more detailed examples.from mesmerize_core import * from fastplotlib.widgets import ImageWidget # load existing batch df = load_batch("/path/to/batch.pickle") # get the reconstructed background as a LazyArray # assumes the last index, `-1`, is a cnmf item residuals = df.iloc[-1].cnmf.get_residuals() # view with ImageWidget iw = ImageWidget(data=residuals) iw.show()
- run_detrend_dfof(quantileMin=8, frames_window=500, flag_auto=True, use_fast=False, use_residuals=True, detrend_only=False)#
- Uses caiman’s detrend_df_f.call
CNMF.get_detrend_dfof()
to get the values.SetsCNMF.estimates.F_dff
Warning
Overwrites the existing cnmf hdf5 output file for this batch item
- Parameters:
quantileMin (float) – quantile used to estimate the baseline (values in [0,100]) used only if ‘flag_auto’ is False, i.e. ignored by default
frames_window (int) – number of frames for computing running quantile
flag_auto (bool) – flag for determining quantile automatically
use_fast (bool) – flag for using approximate fast percentile filtering
detrend_only (bool) – flag for only subtracting baseline and not normalizing by it. Used in 1p data processing where baseline fluorescence cannot be determined.
use_residuals (bool) –
- Return type:
None
Notes
invalidates the cache for this batch item.
- get_detrend_dfof(component_indices=None, return_copy=True)#
Get the detrended dF/F0 curves after calling
run_detrend_dfof
. BasicallyCNMF.estimates.F_dff
.- Parameters:
component_indices (str or np.ndarray, optional) –
indices of the components to includeif not provided,None
, or"all"
uses all componentsif"good"
uses good components, i.e.Estimates.idx_components
if"bad"
uses bad components, i.e.Estimates.idx_components_bad
ifnp.ndarray
, uses the indices in the provided arrayreturn_copy (bool) –
ifTrue
returns a copy of the cached value in memory.ifFalse
returns the same object as the cached value in memory, not recommend this could result in strange unexpected behavior.In general you want a copy of the cached value.
- Returns:
shape is [n_components, n_frames]
- Return type:
np.ndarray
- run_eval(params)#
Run component evaluation. This basically changes the indices for good and bad components.
Warning
Overwrites the existing cnmf hdf5 output file for this batch item
- Parameters:
params (dict) –
dict of parameters for component evaluation
parameter
details
SNR_lowest
float
, minimum accepted SNR valuecnn_lowest
float
, minimum accepted value for CNN classifiergSig_range
List[int, int]
orNone
, range for gSig scale for CNN classifiermin_SNR
float
, transient SNR thresholdmin_cnn_thr
float
, threshold for CNN classifierrval_lowest
float
, minimum accepted space correlationrval_thr
float
, space correlation thresholduse_cnn
bool
, use CNN based classifieruse_ecc
bool
, flag for eccentricity based filteringmax_ecc
float
, max eccentricity- Return type:
None
Notes
invalidates the cache for this batch item.
- get_good_components()#
get the good component indices,
Estimates.idx_components
- Returns:
array of ints, indices of good components
- Return type:
np.ndarray
- get_bad_components()#
get the bad component indices,
Estimates.idx_components_bad
- Returns:
array of ints, indices of bad components
- Return type:
np.ndarray