pykrait.pipeline package#

Submodules#

pykrait.pipeline.pipeline module#

class pykrait.pipeline.pipeline.AnalysisOutput(filepath: str = None, filename: str = None, tproj_path: str = None, masks_path: str = None, analysis_folder: str = None, frame_interval: float = None, pixel_interval_y: float = None, pixel_interval_x: float = None, number_of_cells: int = None, number_of_frames: int = None, normalized_peaks: float = None, cells_four_peaks: int = None, std_cutoff: float = None, std_quantile: float = None, random_below_std: int = None, experimental_below_std: int = None, cov_cutoff: float = None, cov_quantile: float = None, random_below_cov: int = None, experimental_below_cov: int = None, median_frequency_mHz: float = None, active_cells_median_frequency_mHz: float = None, median_peakwidth_s: float = None, median_rise2080_s: float = None, median_decay8020_s: float = None, total_coinciding_peaks_1st_neighbour: int = None, possible_coinciding_peaks_1st_neighbour: int = None, random_1st_neighbour_zscore: float = None, random_2nd_neighbour_zscore: float = None, random_3rd_neighbour_zscore: float = None, experiment_1st_neighbour_zscore: float = None, experiment_2nd_neighbour_zscore: float = None, experiment_3rd_neighbour_zscore: float = None, pykrait_version: str = None, timestamp: datetime64 = None)[source]#

Bases: object

This dataclass holds the output variables of the pipeline. :var filepath: Path to the calcium imaging video file. :vartype filepath: str :var filename: name of the video file :vartype filename: str :var tproj_path: Path to the zproj file. :vartype tproj_path: str :var masks_path: Path to the zproj file. :vartype masks_path: str :var frame_interval: time interval between frames in seconds :vartype frame_interval: float :var pixel_interval_y: pixel size in y direction in micrometers :vartype pixel_interval_y: float :var pixel_interval_x: pixel size in x direction in micrometers :vartype pixel_interval_x: float :var number_of_cells: number of cells detected in the video :vartype number_of_cells: int :var number_of_frames: number of frames in the video :vartype number_of_frames: int :var normalized_peaks: number of peaks per 100 cells and 10 minutes of recording :vartype normalized_peaks: float :var cells_four_peaks: number of cells with at least 4 peaks detected :vartype cells_four_peaks: int :var std_cutoff: cutoff for STD threshold for the standard deviation in seconds :vartype std_cutoff: float :var random_below_std: number of cells with a standard deviation below the threshold in the random control :vartype random_below_std: int :var experiment_below_std: number of cells with a standard deviation below the threshold in the recording :vartype experiment_below_std: int :var cov_cutoff: cutoff for CoV (coefficient of variation) in the recording :vartype cov_threshold: float :var cov_quantile: quantile of random CoV compared to randomly shuffled peaks :vartype cov_quantile: float :var random_below_cov: number of cells with a coefficient of variation below the threshold in the random control :vartype random_below_cov: int :var experiment_below_cov: number of cells with a coefficient of variation below the threshold in the recording :vartype experiment_below_cov: int :var median_frequency_mHz: median frequency of all peak-peak intervals in mHz :vartype median_frequency_mHz: float :var active_cells_median_frequency_mHz: median frequency of the median peak-peak intervals of cells with at least 4 peaks in mHz :vartype active_cells_median_frequency_mHz: float :var median_peakwidth_s: median peak width at half maximum in seconds :vartype median_peakwidth_s: float :var median_rise2080_s: median rise time from 20% to 80% of peak height in seconds :vartype median_rise2080_s: float :var median_decay8020_s: median decay time from 80% to 20% of peak height in seconds :vartype median_decay8020_s: float :var total_coinciding_peaks_1st_neighbour: total number of coinciding peaks between 1st degree neighbours :vartype total_coinciding_peaks_1st_neighbour: int :var possible_coinciding_peaks_1st_neighbour: total number of possible coinciding peaks between 1st degree neighbours :vartype possible_coinciding_peaks_1st_neighbour: int :var random_1st_neighbour_zscore: z-score of synchronicity of the first neighbours in the random control :vartype random_1st_neighbour_zscore: float :var random_2nd_neighbour_zscore: z-score of synchronicity of the second neighbours in the random control :vartype random_2nd_neighbour_zscore: float :var random_3rd_neighbour_zscore: z-score of synchronicity of the third neighbours in the random control :vartype random_3rd_neighbour_zscore: float :var experiment_1st_neighbour_zscore: z-score of synchronicity of the first neighbours in the recording :vartype experiment_1st_neighbour_zscore: float :var experiment_2nd_neighbour_zscore: z-score of synchronicity of the second neighbours in the recording :vartype experiment_2nd_neighbour_zscore: float :var experiment_3rd_neighbour_zscore: z-score of synchronicity of the third neighbours in the recording :vartype experiment_3rd_neighbour_zscore: float :var pykrait_version: version of the pykrait package used for the analysis :vartype pykrait_version: str :var timestamp: timestamp of the analysis :vartype timestamp: np.datetime64

active_cells_median_frequency_mHz: float = None#
analysis_folder: str = None#
cells_four_peaks: int = None#
cov_cutoff: float = None#
cov_quantile: float = None#
experiment_1st_neighbour_zscore: float = None#
experiment_2nd_neighbour_zscore: float = None#
experiment_3rd_neighbour_zscore: float = None#
experimental_below_cov: int = None#
experimental_below_std: int = None#
filename: str = None#
filepath: str = None#
frame_interval: float = None#
classmethod from_pandas(df: DataFrame) AnalysisOutput[source]#

Safely construct a dataclass from the first row of a pandas DataFrame.

masks_path: str = None#
median_decay8020_s: float = None#
median_frequency_mHz: float = None#
median_peakwidth_s: float = None#
median_rise2080_s: float = None#
normalized_peaks: float = None#
number_of_cells: int = None#
number_of_frames: int = None#
pixel_interval_x: float = None#
pixel_interval_y: float = None#
possible_coinciding_peaks_1st_neighbour: int = None#
pykrait_version: str = None#
random_1st_neighbour_zscore: float = None#
random_2nd_neighbour_zscore: float = None#
random_3rd_neighbour_zscore: float = None#
random_below_cov: int = None#
random_below_std: int = None#
std_cutoff: float = None#
std_quantile: float = None#
timestamp: datetime64 = None#
to_pandas()[source]#

Convert the dataclass to a pandas DataFrame.

total_coinciding_peaks_1st_neighbour: int = None#
tproj_path: str = None#
class pykrait.pipeline.pipeline.AnalysisParameters(tproj_type: Literal['std', 'sum'] = 'std', CLAHE_normalize: bool = True, cellpose_model_path: str = 'cpsam', frame_interval: float = None, sinc_filter_window: float = 300, peak_min_width: float = 1, peak_max_width: float = 40, peak_prominence: float = 1000, peak_min_height: float = 80, periodicity_method: Literal['cutoff', 'quantile'] = 'cutoff', std_threshold: float = 15, cov_threshold: float = 0.2, time_window: float = 10, neighbour_tolerance: float = 10)[source]#

Bases: object

This dataclass holds the modifiable parameters for calcium video analysis.

Variables:
  • tproj_type (Literal['std', 'sum'], optional) – Type of projection accross T-axis to be used for segmentation; ‘std’ computes standard deviation, ‘sum’ computes temporal sum, defaults to std.

  • CLAHE_normalize (bool, optional) – whether to apply CLAHE (Contrast Limited Adaptive Histogram Equalization) normalization to enhance image contrast.

  • cellpose_model_path (str, optional) – Path to the Cellpose model used for segmentation. Defaults to “cpsam” for the built-in CPSAM model.

  • frame_interval (float, optional) – time interval between consecutive video frames in seconds, defaults to None. If None provided, the frame interval obtained from the image metadata will be used or it will default to 1 second. The code will output a warning if a frame interval is provided which doesn’t match with the metadata frame interval.

  • sinc_filter_window (float, optional) – Window size for the sinc filter used to detrend the signal, specified in seconds. Converted to frame count internally.

  • peak_min_width (float, optional) – Minimum width at half-maximum of a detected peak in seconds. Converted to frames internally.

  • peak_max_width (float, optional) – Maximum width at half-maximum of a detected peak in seconds. Converted to frames internally.

  • peak_prominence (float, optional) – Minimum prominence of a detected peak, defined as the height difference between the peak and its surrounding baseline. Arbitrary units.

  • peak_min_height (float, optional) – Minimum height of a detected peak in arbitrary units.

  • periodicity_method (Literal['cutoff', 'quantile'], optional) – Method to determine periodicity of ROIs, either “cutoff” for fixed thresholds or “quantile” for quantile-based thresholds.

  • std_threshold – Threshold for the Standard Deviation, either quantile or cutoff depending on method.

  • cov_quantile (float, optional) – Threshold for the CoV, either quantile or cutoff depending on method.

  • time_window (float, optional) – Time window in seconds within which peaks are considered synchronous. Calculated to frames internally. Defaults to 10 seconds.

  • neighbour_tolerance (float, optional) – Maximum distance in micrometers used to detect neighboring cells. Converted to pixels internally.

CLAHE_normalize: bool = True#
cellpose_model_path: str = 'cpsam'#
cov_threshold: float = 0.2#
frame_interval: float = None#
neighbour_tolerance: float = 10#
peak_max_width: float = 40#
peak_min_height: float = 80#
peak_min_width: float = 1#
peak_prominence: float = 1000#
periodicity_method: Literal['cutoff', 'quantile'] = 'cutoff'#
sinc_filter_window: float = 300#
std_threshold: float = 15#
time_window: float = 10#
to_pandas() DataFrame[source]#

Convert the dataclass to a pandas DataFrame.

tproj_type: Literal['std', 'sum'] = 'std'#
class pykrait.pipeline.pipeline.BatchExperiment(folder: str, params: AnalysisParameters, extension: str = '.czi', load_params: bool = False)[source]#

Bases: object

Class to run a batch experiment on multiple calcium videos in a folder.

run(skip_cellpose: bool = False, skip_intensity_extraction: bool = False)[source]#

runs the analysis pipeline for each video in the folder and stores the results in a pandas DataFrame

class pykrait.pipeline.pipeline.CalciumVideo(video_path: str, params: AnalysisParameters)[source]#

Bases: object

Class to run the analysis pipeline on a single calcium video.

run(skip_cellpose: bool = False, skip_intensity_extraction: bool = False) AnalysisOutput[source]#

runs the analysis pipeline on the calcium video and returns the analysis output

Parameters:
  • skip_cellpose (bool, optional) – whether to skip the cellpose segmentation, defaults to False

  • skip_intensity_extraction – whether to skip the intensity extraction, defaults to False

Returns:

returns the AnalysisOutput dataclass, containing the results of the analysis

Return type:

AnalysisOutput

pykrait.pipeline.pipeline.calculate_periodic_cells(peaks, analysis_parameters: AnalysisParameters, analysis_output: AnalysisOutput) Tuple[int, int][source]#
pykrait.pipeline.pipeline.calculate_synchronicity(shortest_path_matrix: ndarray, peak_series: ndarray, analysis_parameters: AnalysisParameters, analysis_output: AnalysisOutput) Tuple[AnalysisOutput, ndarray, ndarray][source]#

calculates the synchronicity z-scores for 1st, 2nd and 3rd degree neighbours and updates the analysis output

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

  • time_window (float) – time window in seconds within which peaks are considered synchronous

  • analysis_parameters (AnalysisParameters) – AnalysisParameters object containing the parameters for the analysis

  • analysis_output (AnalysisOutput) – AnalysisOutput object to be modified with the synchronicity z-scores

Returns:

returns the analysis output, the synchronous peaks matrix for the experiment with and one example random synchronous peaks matrix

Return type:

Tuple[np.ndarray, np.ndarray]

pykrait.pipeline.pipeline.create_analysis_folder(analysis_output: AnalysisOutput) AnalysisOutput[source]#

creates an analysis subfolder in the parent directory of the video file

Parameters:

analysis_output (AnalysisOutput) – AnalysisOutput object containing the filepath of the video file

Returns:

returns the AnalysisOutput object with the analysis folder path set

Return type:

AnalysisOutput

pykrait.pipeline.pipeline.create_masks(timelapse_data: Array, analysis_output: AnalysisOutput, analysis_parameters: AnalysisParameters) Tuple[ndarray, ndarray, AnalysisOutput][source]#

creates a cellpose segmentation mask from the timelapse data and saves it to the analysis folder.

Returns the tproj, the masks and the modified AnalysisOutput object with the mask path set.

Parameters:
  • analysis_output (AnalysisOutput) – AnalysisOutput object to be modified with the mask path and other analysis results

  • analysis_parameters (AnalysisParameters) – AnalysisParameters object containing the parameters for the analysis

Returns:

returns the masks and the modified AnalysisOutput object with the mask path set

Return type:

List[np.ndarray, np.ndarray, AnalysisOutput]

pykrait.pipeline.pipeline.create_mean_intensities(timelapse_data: Array, masks: ndarray, analysis_output: AnalysisOutput) Tuple[ndarray, tuple, AnalysisOutput][source]#

wrapper to extract mean intensities from the timelapse data and update the analysis output

Parameters:
  • timelapse_data (da.Array) – lazily loaded timelapse data as a Dask array

  • analysis_output (AnalysisOutput) – object to be modified with the mean intensities and number of frames and cells

  • masks (np.ndarray) – analysis

Returns:

returns a T x nroi

Return type:

List[np.ndarray, tuple, AnalysisOutput]

pykrait.pipeline.pipeline.create_roi_dataframe(peak_series: ndarray, experimental_stds: ndarray, experimental_covs: ndarray, analysis_output: AnalysisOutput) DataFrame[source]#

creates a dataframe with ROI properties including peak count, std, cov, and frequency

Parameters:
  • peak_series (np.ndarray) – T x n_roi binary matrix with 1s at calcium peak frames

  • experimental_stds (np.ndarray) – array with standard deviations of each ROI

  • experimental_covs (np.ndarray) – array with coefficients of variation of each ROI

  • analysis_output (AnalysisOutput) – AnalysisOutput object containing the frame interval

Returns:

returns a pandas DataFrame with ROI properties

Return type:

pd.DataFrame

pykrait.pipeline.pipeline.do_peak_analysis(detrended_timeseries: ndarray, peak_min_width_frames: int, peak_max_width_frames: int, analysis_parameters: AnalysisParameters, analysis_output: AnalysisOutput) Tuple[ndarray, DataFrame, ndarray, AnalysisOutput][source]#

Wrapper function for peak analysis. It detects peaks, computes the normalized_peaks, analyses the peak properties and frequency.

Parameters:
  • detrended_timeseries (np.ndarray) – T x n_roi timeseries with already detrended signals

  • peak_min_width_frames (int) – minimum peak width in frames

  • peak_max_width_frames (int) – maximum peak width in frames

  • analysis_parameters (AnalysisParameters) – analysis parameter object

  • analysis_output (AnalysisOutput) – analysis output object

Returns:

returns the peak series (binary T x n_roi where 1 = peak), the peak properties dataframe, an array with the 70 frames around the peak for visualization, and the modified analysis output object

Return type:

Tuple[np.ndarray, pd.DataFrame, np.ndarray, AnalysisOutput]

pykrait.pipeline.pipeline.load_timelapse(analysis_output: AnalysisOutput, analysis_parameters: AnalysisParameters) Tuple[Array, AnalysisOutput][source]#
pykrait.pipeline.pipeline.save_analysis_results(tproj: ndarray, masks: ndarray, synchronous_peaks_matrix: ndarray, analysis_output: AnalysisOutput, analysis_params: AnalysisParameters, detrended_intensities: ndarray, peaks: ndarray, intensities: ndarray, stds: ndarray, covs: ndarray, roi_dataframe: DataFrame = None, overwrite: bool = False)[source]#
pykrait.pipeline.pipeline.save_params(analysis_output: AnalysisOutput, analysis_params: AnalysisParameters, overwrite: bool = False)[source]#

Module contents#