mps package#
mps.analysis module#
- class mps.analysis.APDAnalysis(apds, capds, apd_points, is_significant, triangulation, slope_APD80, slope_cAPD80, const_APD80, const_cAPD80, apd_dt)#
Bases:
tuple
- apd_dt#
Alias for field number 9
- apd_points#
Alias for field number 2
- apds#
Alias for field number 0
- capds#
Alias for field number 1
- const_APD80#
Alias for field number 7
- const_cAPD80#
Alias for field number 8
- is_significant#
Alias for field number 3
- slope_APD80#
Alias for field number 5
- slope_cAPD80#
Alias for field number 6
- triangulation#
Alias for field number 4
- class mps.analysis.Collector(outdir=None, plot: bool = False, params=None)[source]#
Bases:
object
- property data#
- property info: Dict[str, Any]#
- property info_txt: str#
- property metadata: Dict[str, Any]#
- class mps.analysis.ExcludedData(new_data, included_indices, all_included_indices)#
Bases:
tuple
- all_included_indices#
Alias for field number 2
- included_indices#
Alias for field number 1
- new_data#
Alias for field number 0
- class mps.analysis.Features(features, features_beats, features_included_beats, features_1std_str, features_all_str, included_indices, included_indices_beats)#
Bases:
tuple
- features#
Alias for field number 0
- features_1std_str#
Alias for field number 3
- features_all_str#
Alias for field number 4
- features_beats#
Alias for field number 1
- features_included_beats#
Alias for field number 2
- included_indices#
Alias for field number 5
- included_indices_beats#
Alias for field number 6
- mps.analysis.analyze_apds(beats: List[Beat], max_allowed_apd_change: float | None = None, fname: str = '', plot=True) APDAnalysis [source]#
- mps.analysis.analyze_chopped_data(trace: Beats, collector: Collector, threshold_factor=0.3, extend_front=None, extend_end=None, min_window=200, max_window=2000, use_spline: bool = True, normalize: bool = False, max_allowed_apd_change: float | None = None, ead_sigma: int = 3, ead_prominence_threshold: float = 0.04, std_ex: float = 1.0, N: int = 200, **kwargs)[source]#
- mps.analysis.analyze_eads(beats: List[Beat], sigma: float = 1, prominence_threshold: float = 0.07, plot=True, fname: str = '') int [source]#
Loop over all beats and check for EADs.
- Parameters:
beats (List[apf.Beat]) – List of beats
sigma (float) – Standard deviation in the gaussian smoothing kernal used for EAD detection. Default: 3.0
prominence_threshold (float) – How prominent a peak should be in order to be characterized as an EAD. This value shold be between 0 and 1, with a greater value being more prominent. Defaulta: 0.04
plot (bool) – If True we plot the beats with the potential EAD marked as a red dot. Default: True
fname (str) – Path to file that you want to save the plot.
- Returns:
The number of EADs
- Return type:
int
- mps.analysis.analyze_frequencies(beats, time_unit: str = 'ms', fname: str = '', plot=True) ndarray [source]#
- mps.analysis.analyze_mps_func(mps_data, mask=None, analysis_window_start=0, analysis_window_end=-1, spike_duration=0, filter_signal=False, ignore_pacing=False, remove_points_list=(), threshold_factor=0.3, extend_front=None, extend_end=None, min_window=200, max_window=2000, use_spline=True, normalize=False, outdir=None, plot=False, background_correction=True, max_allowed_apd_change=None, ead_sigma=3, ead_prom=0.04, std_ex: float = 1.0, N=200, **kwargs)[source]#
- mps.analysis.analyze_unchopped_data(mps_data, collector, mask=None, analysis_window_start=0, analysis_window_end=-1, spike_duration=0, filter_signal=False, ignore_pacing=False, remove_points_list=(), background_correction=True, **kwargs) Beats [source]#
- mps.analysis.average_intensity(data, mask=None, alpha=1.0, averaging_type='spatial')[source]#
Compute the average_intensity of the frame stack. The available keyword arguments depends on the averaging_type chosen
- Parameters:
X (
numpy.ndarray
) – The frame stack (size \(M imes N imes T\))averaging_type (str) – How you want to average the frame stack. Possible values are [‘all’, ‘temporal’, ‘spatial’, ‘global’]
- mps.analysis.baseline_intensity(frames, times, N=10, x_start=0, y_start=0, x_end=None, y_end=None, normalize=False, loglevel=20, **kwargs)[source]#
Compute the baseline intensity in local windows
- Parameters:
frames (np.ndarray) – The image sequence on the form (nx, ny, T) where nx and ny are repspectively the number of pixels in the x and y direction and T are the number of frames.
times (np.ndarray or list) – An array of time stamps.
N (int) – Maximum number of grid points. The axis with the greatest number of pixels will be partitioned into a coarser grid of size n. The other axis will be scaled so that each grid point is approximately square.
x_start (int) – Index where to start in x-direction
x_end (int) – Index where to end in x-direction
y_start (int) – Index where to start in y-direction
y_end (int) – Index where to end in y-direction
backround_correction (bool) – If you want to apply background correction. You typically want to allways do this except in the case when you only have a single beat. Default: True.
normalize (bool) – If True, normalize all averages to have values between 0 and 1, if False keep the original values. Default: False
loglevel (int) – Verbosity. Default: INFO (=20). For more info see the logging library.
- mps.analysis.compute_all_features(beats, outdir=None, plot=True, use_spline: bool = True, normalize: bool = False, max_allowed_apd_change: float | None = None, ead_sigma: int = 3, ead_prominence_threshold: float = 0.04, std_ex: float = 1.0, **kwargs)[source]#
- mps.analysis.compute_features(beats: List[Beat], use_spline=True, normalize=False)[source]#
Analyze signals. Compute all features and include only the relevant beats
- Parameters:
beats (List[apf.Beat]) – List of beats
use_spline (bool) – Use spline interpolation (Default : True)
normalize (bool) – If true normalize signal first, so that max value is 1.0, and min value is zero before performing the computation.
- Returns:
data – A dictionary with the following structure
- Return type:
dict
Notes
In some cases, all the beats are not necessary representative for the underlying signal, for example if there is a lot of noise present. In this case it would be more robust compute the features by only including those beats that are closest to the average signals. Suppose we have \(N\) sub-signals, \(z_1, z_2, \cdots, z_N\), each representing one beat. Further let \(f\) denote a function which takes a sub-signal as input and output some of the properties, e.g \(f= \mathrm{APD}30\). Define
\[\bar{f} = \frac{1}{N}\sum_{i = 1}^N f(z_i),\]to be the average value of a given feature \(f\) of all sub-signals, and
\[\sigma (f) = \sqrt{\frac{1}{N-1}\sum_{i = 1}^N \left(f(z_i) - \bar{f} \right)^2},\]be the standard deviation. Now, let \(\mathcal{D}\) be the set of all sub-signals that are within 1 standard deviation of the mean, i.e
\[\mathcal{D} = \{ z : | f(z) - \bar{f} | < \sigma(f) \}.\]Then a more robust estimate of the average value of \(f\) (than \(\bar{f}\)) is
\[f_{\mathcal{D}} = \frac{1}{|\mathcal{D}|}\sum_{z \in \mathcal{D}}^N f(z).\]If the set \(\mathcal{D}\) is empty, there can be two reasons for this, namely the signal is very noisy, or the standard deviation is very small. We assume the latter, and will in these cases only return \(\bar{f}\).
- mps.analysis.exclude_x_std(data, x=None, use=None)[source]#
Given a list of values return a list of all values that are within x factor of the mean.
- Parameters:
data (dict) – A dictionary of lists of values, e.g a list of apd30
x (float) – The number of standard deviations to be included, ex x = 1.0. If none is provided everything will be included
use (List[str]) – List of features to use
- Returns:
new_data (dict) – A dictionary with new lists containing only those elements that are within \(x\) std of the mean
included_indice (dict) – Indices included for each (key, value) pair.
means (array) – The mean of the elements in
new_data
- mps.analysis.find_included_indices(data, x=None, use=None)[source]#
Given a list of values return a list of all values that are within x factor of the mean.
- Parameters:
data (dict) – A dictionary of lists of values, e.g a list of apd30
x (float) – The number of standard deviations to be included, ex x = 1.0. If none is provided everything will be included
use (List[str]) – List of features to use
- Returns:
List of indices or each beat and list of common indices
- Return type:
Tuple[Dict[str, List[int]], List[int]]
- mps.analysis.frame2average(frame, times=None, normalize=False, background_correction=True)[source]#
Compute average pixel intensity of the frames
- Parameters:
frames (np.ndarray) – The frames
times (np.ndarray) – The time stamps
normalize (bool) – In true normalize average so that it takes value between zero and one. Default: True
background_correction (bool) – If true, apply backround correction to the average. You don’t want to do this if you only have a single beat. Default: True.
- Returns:
trace – The average trace
- Return type:
np.ndarray
- mps.analysis.local_averages(frames, times=None, N=10, x_start=0, y_start=0, x_end=None, y_end=None, background_correction=True, normalize=False, loglevel=20, **kwargs)[source]#
Compute the local averages
- Parameters:
frames (np.ndarray) – The image sequence on the form (nx, ny, T) where nx and ny are repspectively the number of pixels in the x and y direction and T are the number of frames.
times (np.ndarray or list) – An array of time stamps.
N (int) – Maximum number of grid points. The axis with the greatest number of pixels will be partitioned into a coarser grid of size n. The other axis will be scaled so that each grid point is approximately square.
x_start (int) – Index where to start in x-direction
x_end (int) – Index where to end in x-direction
y_start (int) – Index where to start in y-direction
y_end (int) – Index where to end in y-direction
backround_correction (bool) – If you want to apply background correction. You typically want to allways do this except in the case when you only have a single beat. Default: True.
normalize (bool) – If True, normalize all averages to have values between 0 and 1, if False keep the original values. Default: False
loglevel (int) – Verbosity. Default: INFO (=20). For more info see the logging library.
- class mps.analysis.mps_prevalence(prevalence, tissue_covered_area, is_beating, is_tissue)#
Bases:
tuple
- is_beating#
Alias for field number 2
- is_tissue#
Alias for field number 3
- prevalence#
Alias for field number 0
- tissue_covered_area#
Alias for field number 1
- mps.analysis.plot_apd_analysis(beats: List[Beat], res: APDAnalysis, fname='')[source]#
- mps.analysis.prevalence(mps_data, snr_factor=1.5, N=50, frequency_threshold=0.2, baseline_threshold=0.1, **kwargs)[source]#
Compute the prevalence, i.e the percentage of living cells in the recording
- Parameters:
mps_data (mps.load.MPS) – The data
snr_factor (float) – Factor multiplied with the global signal to noise ratio (snr), to determine wether a region is noise or not. If a local region has a larger values than snr_factor * global snr it will be classied as noise. Default: 1.5
N (int) – Size of grid along the major axis (minor axis will be scaled proportionally). Default: 50
frequency_threshold (float) – Percentage (between 0 and 1) of how far from the global frequency a local signal can be before it is classified as non-beating. Default: 0.2
baseline_threshold (float) – Percentage (between 0 and 1) of how far from the min and max values of the baseline intensities of the beating regions that should define the rage for baseline intensity of tissue Default: 0.1
- Returns:
mps_prevalence (namedtuple) – A named tuple with the following fields
prevalence (float) – percentage of tissue that is beating
tissue_covered_area (float) – percentage of area in the image that is classified as tissue
is_beating (array) – boolean array who’s true values are classied as beating regions
is_tissue (array) – boolean array who’s true values are classied as regions with tissue
mps.average module#
- class mps.average.AveragingType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
str
,Enum
- global_ = 'global'#
- spatial = 'spatial'#
- temporal = 'temporal'#
- mps.average.get_average_all(X)[source]#
Return the global average of a frame stack, i.e
\[y = \begin{pmatrix} y^1, y^2, \cdots , y^T \end{pmatrix}\]with
\[y^i = \mathrm{avg}_{\mathrm{all}}(\mathbf{X}^i ) = \bar{X}^i = \frac{1}{MN} \sum_{m,n} x_{m,n}^i\]- Parameters:
X (
numpy.ndarray
) – The frame stack (size \(M \times N \times T\))- Returns:
average – The average vector of length \(T\)
- Return type:
numpy.ndarray
- mps.average.get_spatial_average(X, alpha=1.0)[source]#
Get the spatial average
- Parameters:
X (
numpy.ndarray
) – The frame stack (size \(M \times N \times T\))alpha (float)
- Returns:
average – The average vector of length \(T\)
- Return type:
numpy.ndarray
Notes
If we assume that the important pixels are those pixels with a high pixel value, then we can simply select those pixel with the highest pixels values. Formally, for each frame \(i\),
\[\mathcal{D} = \{ (m,n) : x_{m,n} > f(\mathbf{X}^i) \}\]then,
\[y^i = \mathrm{avg}_{\mathrm{spatial}, f}(\mathbf{X}^i ) = \frac{1}{|\mathcal{D}|} \sum_{(m,n) \in \mathcal{D}} x_{m,n}^i\]where \(|\mathcal{D}|\) is the number of pixels in \(\mathcal{D}\).
- mps.average.get_temporal_average(X, alpha=1.0, return_indices=False)[source]#
Get the temporal average
- Parameters:
X (
numpy.ndarray
) – The frame stack (size \(M \times N \times T\))bound (str) – Make average of pixels larger than the bound. Choices (‘median’, ‘mean’). Default . ‘median’
- Returns:
average – The average vector of length \(T\)
- Return type:
numpy.ndarray
Notes
If we assume that pixels that represents noise have pixel values, that do not vary much throughout the frame stack compared to signals that represents the actualt signals, we can try to include only those pixels that have a certain variation compared to all the other pixels. Let
\[\begin{split}\sigma(\mathcal{X}) = \begin{pmatrix} \sigma(X)_{1,1} & \sigma(X)_{1,2} & \cdots & \sigma(X)_{1,N} \\ \sigma(X)_{2,1} & \sigma(X)_{2,2} & \cdots & \sigma(X)_{2,N} \\ \vdots & \vdots & \vdots & \vdots \\ \sigma(X)_{M,1} & \sigma(X)_{M,2} & \cdots & \sigma(X)_{M,N} \end{pmatrix}\end{split}\]be the \(M \times N\) matrix with each coordinate beeing the temporal standard deviation. Now, for a given fuctional \(f\) let
\[\mathcal{D} = \{ (m,n) : \sigma(X)_{m,n} > f(\sigma(\mathcal{X})) \}\]be the set of pixel coordinates selecting pixels based on the rule \(f\). Then
\[y^i = \mathrm{avg}_{\mathrm{temporal}, f}(\mathbf{X}^i ) = \frac{1}{|\mathcal{D}|} \sum_{(m,n) \in \mathcal{D}} x_{m,n}^i\]where \(|\mathcal{D}|\) is the number of pixels in $mathcal{D}$.
mps.load module#
- class mps.load.MPS(fname='', verbose=False)[source]#
Bases:
object
Create instant of class for analysing MPS data
- Parameters:
fname (str) – Path to the file
parameters (dict) – Additional parameters, see MPS.default_parameters
- property framerate#
Return number of frames per second
- property frames#
- property info#
- property metadata#
- property name#
- property original_time_stamps#
- property pacing#
- property pacing_frequency#
Return the pacing frequency in Hertz
- Returns:
The pacing frequency
- Return type:
float
- class mps.load.MPSData(frames: numpy.ndarray, time_stamps: numpy.ndarray, info: Dict[str, Any], pacing: numpy.ndarray | None = None, metadata: Dict[str, Any] | None = None)[source]#
Bases:
object
- frames: ndarray#
- info: Dict[str, Any]#
- metadata: Dict[str, Any] | None = None#
- pacing: ndarray | None = None#
- time_stamps: ndarray#
- mps.load.get_single_frame(path: PathLike, index: int = 0) ndarray [source]#
Get a single frame from a czi or nd2 file.
- Parameters:
path (str) – Path to the file
index (int) – The index of the frame (Default: 0)
- Returns:
frame – The frame
- Return type:
numpy.ndarray
mps.plotter module#
- mps.plotter.plot_window(self, frames, num_frames, local=None, t=0, fname=None)[source]#
If you can to take average over a smaller region you can use this function to plot a frame at the given frame
- Parameters:
local (array) – Return list of indice to plot as frame [startx, endx, starty, endy]
t (int) – Image number
fname (str:) – Name of figure to save
mps.utils module#
- mps.utils.collect_data(voltage_data, calcium_data)[source]#
Collect voltage and calcium data togther so that it can be used in the inversion.
- mps.utils.dump_data(data, path, fileformat='npy')[source]#
Dump data to a file with given file format
- Parameters:
data (dict or array) – The data you want to dum
path (str) – Path to the data (without extension)
fileformat (str) – File format. Either ‘npy’, ‘yml’, ‘csv’ or ‘mat’. Deafult: ‘npy’
- mps.utils.get_data_from_dict(set_data, set_key, get_data, get_key, get_name)[source]#
Copy data from one dictionary to another
- mps.utils.get_grid_settings(N, x_start=0, y_start=0, x_end=None, y_end=None, frames=None, **kwargs)[source]#
- class mps.utils.grid_settings(nx, ny, dx, dy, x_start, y_start, x_end, y_end)#
Bases:
tuple
- dx#
Alias for field number 2
- dy#
Alias for field number 3
- nx#
Alias for field number 0
- ny#
Alias for field number 1
- x_end#
Alias for field number 6
- x_start#
Alias for field number 4
- y_end#
Alias for field number 7
- y_start#
Alias for field number 5
- mps.utils.json_serial(obj)[source]#
JSON serializer for objects not serializable by default json code
- mps.utils.loadmat(filename)[source]#
Short summary.
- Parameters:
filename (type) – Description of parameter filename.
- Returns:
Description of returned object.
- Return type:
type
- mps.utils.merge_pdfs(lst, fname, cleanup=True)[source]#
Given a list of paths to pdfs, merge these together and save the results in a new file
- lstlist
List with paths to existing pdfs
- fnamestr
Name of the output file where you want to save the merged pdf.
- cleanupbool
If True, delete the files in the list provided. Default: True
mps.scripts package#
mps.scripts.analyze module#
Analyze flourecense data
- mps.scripts.analyze.main(path: str, outdir: str | None = None, plot: bool = True, filter_signal: bool = False, ead_prom: float = 0.04, ead_sigma: float = 3.0, std_ex_factor: float = 1.0, spike_duration: float = 0.0, threshold_factor: float = 0.3, extend_front: int | None = None, extend_end: int | None = None, min_window: float = 50, max_window: float = 2000, ignore_pacing: bool = False, reuse_settings: bool = False, overwrite: bool = True, verbose: bool = False)[source]#
mps.scripts.mps2mp4 module#
Create movie of data file
mps.scripts.phase_plot module#
Make a phase plot with voltage on the x-axis and calcium on the y-axis.
mps.scripts.split_pacing module#
Run script on a folder with files and this will copy the files into folders with the same pacing frequency
mps.scripts.summary module#
Create a summary pdf of all files in the a directory.