pykrait.trace_analysis package#
Submodules#
pykrait.trace_analysis.filtering module#
- pykrait.trace_analysis.filtering.detrend_with_sinc_filter(signals: ndarray, cutoff_period: float, sampling_interval: float) ndarray[source]#
Applies a Blackman-windowd sinc filter to a 2D array of signals to detrend them. Frequencies lower than the cutoff frequency will be blocked, while higher frequencies will pass through unaffected.
See the pyBOAT paper (https://www.biorxiv.org/content/10.1101/2020.04.29.067744v2) and Chapter 16 of the The Scientist and Engineer’s Guide to Digital Signal Processing (https://www.dspguide.com/ch16/1.htm) for more details.
- Parameters:
signals (np.ndarray) – 2D numpy array of shape (T x n_rois)
cutoff_period (float) – cutoff period in [time units], lower frequencies will be blocked
sampling_interval (float) – sampling interval in [time units] of the signals
- Returns:
returns the detrended signals as a 2D numpy array of shape (T x n_rois)
- Return type:
np.ndarray
- pykrait.trace_analysis.filtering.smooth_gauss(signal: ndarray, sigma: float) ndarray[source]#
Applies a Gaussian smoothing filter to the 2D input signal.
- Parameters:
signal (np.ndarray) – 2D numpy array of shape (T x n_rois)
sigma (float) – Standard deviation for the Gaussian kernel.
- Returns:
The smoothed signal of shape (T x n_rois).
- Return type:
np.ndarray
pykrait.trace_analysis.oscillations module#
- pykrait.trace_analysis.oscillations.calculate_std_cov(peak_series: ndarray, frame_interval: float) Tuple[ndarray, ndarray][source]#
calculates the standard deviation and coefficient of variation of the peak intervals for each ROI. If a cell has less than 4 peaks it will return NaN for the standard deviation and coefficient of variation.
- Parameters:
peak_series (np.ndarray) – peak series of shape (T, n_roi) where T is the number of time points and n_roi is the number of ROIs
frame_interval (float) – frame interval of the recording in seconds
- Returns:
returns the standard deviation and coefficient of variation of the peak intervals for each ROI
- Return type:
Tuple[np.ndarray, np.ndarray]
- pykrait.trace_analysis.oscillations.find_median_frequency(peak_series: ndarray, frame_interval: float) Tuple[float, float, ndarray][source]#
returns the median overall frequency across all peak-peak intervals, and the median frequencies of active cells (calculated as median of the median peak-peak intervals of each cell)
- Parameters:
peak_series (np.ndarray) – peak series of shape (T, n_roi) where T is the number of time points and n_roi is the number of ROIs, 1 denotes peak
frame_interval (float) – frame interval in seconds
- Returns:
returns the overall median frequency and the median of the cell median frequencies in mHz, as well as the array of median periods for each cell
- Return type:
Tuple[float, float, np.ndarray]
- pykrait.trace_analysis.oscillations.find_oscillating_rois(periodicity_method: Literal['cutoff', 'quantile'], peak_series: ndarray, std_threshold: float, cov_threshold: float, frame_interval: float, n_iter: int = 100) Tuple[dict, ndarray, ndarray][source]#
Calculates the periodicity of the ROIs based on the peak series, using either a cutoff or quantile method.
- Parameters:
periodicity_method (str, "cutoff" or "quantile") – whether to use “cutoff” or “quantile” method for periodicity detection
peak_series (np.ndarray) – T x nroi binary peak series, where 1s indicate peaks and 0s indicate no peaks
std_threshold (float) – threshold for standard deviation (can be both a cutoff or a quantile depending on the periodicity_method)
cov_threshold (float) – threshold for standard deviation (can be both a cutoff or a quantile depending on the periodicity_method)
frame_interval (float) – frame interval of the recording in seconds
n_iter (int, optional) – number of iterations for random data, defaults to 100
- Returns:
returns a dictionary with the periodicity results, and the standard deviations and coefficients of variation for the experimental data
- Return type:
Tuple[dict, np.ndarray, np.ndarray]
- pykrait.trace_analysis.oscillations.get_random_std_covs(filtered_peak_series: ndarray, frame_interval: float, n_iter: int = 100) Tuple[ndarray, ndarray][source]#
randomly shuffles the peak series across the time axis (individually per ROI) and returns n_iter times the random standard deviations and coefficients of variation.
- Parameters:
filtered_peak_series (np.ndarray) – (T x n_roi) binary array with 1s at the peak locations and 0s elsewhere, where n_roi are ROIs with at least 4 peaks
n_iter (100) – number of iterations for random control
frame_interval (float) – frame interval of the recording in seconds
- Returns:
returns the standard deviation and coefficient of variation thresholds for the random control data
- Return type:
Tuple[np.ndarray, np.ndarray]
pykrait.trace_analysis.peak_analysis module#
- pykrait.trace_analysis.peak_analysis.calculate_normalized_peaks(peaks: ndarray, frame_interval: float) float[source]#
calculates the normalized peaks (peaks per 100 cells per 10 minutes)
- Parameters:
peaks (np.ndarray) – binary array of shape (T x n_cells) with 1s at the peak locations and 0s elsewhere
frame_interval (float) – frame interval in seconds
- Returns:
normalized peaks per 100 cells per 10 minutes
- Return type:
float
- pykrait.trace_analysis.peak_analysis.create_peak_properties(peak_series: ndarray, detrended_timeseries: ndarray, frame_interval: float) Tuple[DataFrame, ndarray][source]#
analyzes properties of the identified peaks such as FWHM, rise time, decay time, AUC and creates a numpy array with arranged peak traces for visualization
- Parameters:
peak_series (np.ndarray) – binary array of shape (T x n_cells) with 1s at the peak locations and 0s elsewhere
detrended_timeseries (np.ndarray) – the detrended timeseries
frame_interval (float) – frame interval in seconds
- Returns:
returns a dataframe with peak properties and a numpy array with arranged peak traces
- Return type:
Tuple[pd.DataFrame, np.ndarray]
- pykrait.trace_analysis.peak_analysis.find_peaks(peak_min_width: int, peak_max_width: int, peak_prominence: float, peak_min_height: float, detrended_timeseries: ndarray) ndarray[source]#
wrapper function for scipy’s find_peaks function
- Parameters:
peak_min_width (int) – minimum width at half-maximum of peak in frames
peak_max_width (int) – maximum width at half-maxumim of peak in frames
peak_prominence (float) – minimum prominence of peak
min_peak_height (float) – minimum height of peak
detrended_timeseries (ndarray) – the timeseries to find the peaks on
- Returns:
returns a (T x n_roi) np.ndarray with 1s at the peak locations and 0s elsewhere
- Return type:
np.ndarray
pykrait.trace_analysis.synchronicity module#
- pykrait.trace_analysis.synchronicity.calculate_synchronicity_zscore(shortest_path_matrix: ndarray, peak_series: ndarray, neighbour_degree: int, frame_window: int, n_iter=100) Tuple[float, float, ndarray, ndarray, ndarray][source]#
calculates the zscore of the synchronicity between connected cells given a certain neighbourhood degree and time window.
- Parameters:
shortest_path_matrix (np.ndarray) – ROI x ROI matrix with shortest path lengths between cells n and m
peak_series (np.ndarray) – T x ROI binary matrix with 1s at calcium peak frames
neighbour_degree (int) – degree of neighbourhood to consider (1 = direct neighbours)
frame_window (int) – time window within peaks are considered synchronous
n_iter (int, optional) – number of random permutations to bootstrap against, defaults to 100
- Returns:
returns the zscore of the synchronicity, the true synchronous peaks matrix, one example random synchronous peaks matrix and the possible synchronous peaks matrix
- Return type:
Tuple[float, float, np.ndarray, np.ndarray, np.ndarray]
- pykrait.trace_analysis.synchronicity.find_possible_synchronous_peaks(shortest_path_matrix: ndarray, peak_series: ndarray, neighbour_degree: int, frame_window: int) ndarray[source]#
finds the number of possible synchronous peaks between connected cells given a certain neighbourhood degree and time window. This assumes that one peak can only be synchronous with one other peak from a connected cell, so the maximum number of synchronous events is limited by the cell with the fewer peaks.
- Parameters:
shortest_path_matrix (np.ndarray) – ROI x ROI matrix with shortest path lengths between cells n and m
peak_series (np.ndarray) – T x ROI binary matrix with 1s at calcium peak frames
neighbour_degree (int) – degree of neighbourhood to consider (1 = direct neighbours)
frame_window (int) – time window within peaks are considered synchronous
- Returns:
returns a ROI x ROI matrix with the number of possible synchronous peaks between connected cells
- Return type:
np.ndarray
- pykrait.trace_analysis.synchronicity.find_synchronous_peaks(shortest_path_matrix: ndarray, peak_series: ndarray, neighbour_degree: int, frame_window: int) ndarray[source]#
finds the number of synchronous peaks between connected cells given a certain neighbourhood degree and time window.
- Parameters:
shortest_path_matrix (np.ndarray) – ROI x ROI matrix with shortest path lengths between cells n and m
peak_series (np.ndarray) – T x ROI binary matrix with 1s at calcium peak frames
neighbour_degree (int) – degree of neighbourhood to consider (1 = direct neighbours)
frame_window (int) – time window within peaks are considered synchronous
- Returns:
returns a ROI x ROI matrix with the number of synchronous peaks between connected cells
- Return type:
np.ndarray