Metadata-Version: 2.5
Name: pykrait
Version: 0.4.0
Summary: Python Calcium Recording Analysis and Interpretation Toolbox
Author-email: phaleanopsis <22663502+phaleanopsis@users.noreply.github.com>
License: MIT
License-File: LICENSE
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.11
Requires-Dist: aicspylibczi>=3.3.1
Requires-Dist: bioio-czi<5,>=2.4.0
Requires-Dist: bioio-imageio<2
Requires-Dist: bioio-ome-tiff<2,>=1.1.0
Requires-Dist: bioio-tifffile<2,>=1.3.0
Requires-Dist: bioio<4,>=3.2.0
Requires-Dist: cellpose<5,>=4.0.8
Requires-Dist: dask>=2025.5.1
Requires-Dist: fsspec>=2022.8.0
Requires-Dist: imagecodecs>=2025.3.30
Requires-Dist: matplotlib>=3.10.3
Requires-Dist: numcodecs<0.16,>=0.15
Requires-Dist: numpy>2
Requires-Dist: opencv-python-headless>=4.10.0.84
Requires-Dist: pandas>=2.3.1
Requires-Dist: psutil>=6.0
Requires-Dist: pylibczirw>=6.0; sys_platform != 'darwin' or platform_machine != 'x86_64'
Requires-Dist: pyyaml>=6.0.2
Requires-Dist: scikit-image>=0.25.2
Requires-Dist: scipy>=1.15.3
Requires-Dist: tifffile>=2025.5.10
Requires-Dist: tqdm>=4.66
Requires-Dist: xarray>=2025.12
Requires-Dist: zarr>=2.15.0
Requires-Dist: zstandard>=0.23.0
Provides-Extra: dev
Requires-Dist: build>=1.2.2.post1; extra == 'dev'
Requires-Dist: hatch>=1.14.1; extra == 'dev'
Requires-Dist: ipywidgets>=8.1.7; extra == 'dev'
Requires-Dist: myst-parser>=5.0.0; extra == 'dev'
Requires-Dist: pydata-sphinx-theme>=0.16.1; extra == 'dev'
Requires-Dist: pytest-cov>=6.0; extra == 'dev'
Requires-Dist: pytest>=8.4.1; extra == 'dev'
Requires-Dist: ruff>=0.11.13; extra == 'dev'
Requires-Dist: sphinx-autoapi>=3.6.0; extra == 'dev'
Requires-Dist: sphinx-favicon>=1.0.1; extra == 'dev'
Requires-Dist: sphinx>=8.1.3; extra == 'dev'
Requires-Dist: tomli>=2.2.1; extra == 'dev'
Requires-Dist: twine>=6.1.0; extra == 'dev'
Provides-Extra: gui
Requires-Dist: pyside6>=6.9.1; extra == 'gui'
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/Winklerlab-HD/pykrait/main/docs/_static/logo.png" alt="pyKrait" width="200">
</p>

<h1 style="text-align: center;">pyKrait</h1>

<p align="center">
  <a href="https://pypi.org/project/pykrait/"><img src="https://img.shields.io/pypi/v/pykrait" alt="PyPI"></a>
  <a href="https://pypi.org/project/pykrait/"><img src="https://img.shields.io/pypi/pyversions/pykrait" alt="Python versions"></a>
  <a href="https://github.com/Winklerlab-HD/pykrait/actions"><img src="https://img.shields.io/github/actions/workflow/status/Winklerlab-HD/pykrait/CI.yaml" alt="CI"></a>
    <a href="https://pykrait.readthedocs.io"><img src="https://img.shields.io/readthedocs/pykrait" alt="Docs"></a>
  <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="License"></a>
</p>
<p align="center">
  End-to-end pipeline for segmenting cells, detecting calcium peaks,<br>
  and quantifying oscillation periodicity and spatial synchronicity<br>
  from time-lapse microscopy.
</p>


<p align="center">
  <img src="https://raw.githubusercontent.com/Winklerlab-HD/pykrait/main/docs/_static/pyKrait_steps.png" alt="Analysis Steps of the pykrait package" width="600">
</p>

## Overview
pyKrait (**py**thon **C**alcium **r**ecording **a**nalysis and **i**nterpretation **t**oolbox) automatically processes calcium-imaging videos (.czi, .tif, .tiff) and computes calcium activity, peak statistics, periodicity scores, and neighbour-aware synchronicity z-scores.

## Installation

It is recommended to install pyKrait in a virtual environment using either venv or uv. The graphical user interface is optional, install it with `pip install "pykrait[gui]"`. 

### Using `uv`

```bash
uv venv --python 3.12
source .venv/bin/activate           # Windows: .venv\Scripts\activate
uv pip install 'pykrait[gui]'
```

### Standard `venv`

```bash
python -m venv .venv
source .venv/bin/activate           # Windows: .venv\Scripts\activate
pip install --upgrade pip
pip install "pykrait[gui]"
```


## Quickstart

### Launch the GUI

From inside the activated virtual environment:
```bash
python -m pykrait
```
or
```bash
uv run python -m pykrait
```
The GUI then walks you through video selection, segmentation, peak detection, periodicity, and synchronicity, and lets you save results to disk.

### Batch process an entire folder

```python
from pykrait.pipeline.pipeline import BatchExperiment, AnalysisParameters
from pykrait.io.files import concat_analysis_files

experiment = BatchExperiment(
    folder="/path/to/videos",
    params=AnalysisParameters(),
    extension=".czi",
)
experiment.run()

concat_analysis_files("/path/to/videos", filetype="output")
```

This produces one `Analysis_<video>/` directory per video and an `analysis_output_overview.csv` at the root of the folder.

To analyze videos in subfolders, pass `recursive=True`; to analyze only some of the acquisitions, pass `filter_string` / `exclude_string` (e.g. `filter_string="*_timeseries.czi"`, where a pattern with `*`, `?` or `[]` matches the whole file name and anything else matches as a substring).

## Example Notebooks

| Scenario | Notebook |
|----------|----------|
| Run a folder with custom parameters | [notebooks/example_batch_with_custom_parameters](https://github.com/Winklerlab-HD/pykrait/blob/main/notebooks/example_batch_with_custom_parameters.ipynb) |
| Re-run a folder reusing saved parameters and cached masks | [notebooks/example_batch_reusing_parameters](https://github.com/Winklerlab-HD/pykrait/blob/main/notebooks/example_batch_reusing_parameters.ipynb) |
| Inspect or refine a single video interactively | Launch the GUI: `python -m pykrait` |

## How it works

For each video, pyKrait runs the following stages:

1. **Load** — lazy `Dask` array via `bioio` (`.czi`, `.tif`, `.tiff`).
2. **Correct motion** *(optional, off by default)* — align every frame to a template, either by a rigid translation or by a mesh-based / polynomial optical-flow warp.
3. **Project** — STD or SUM projection across time, with optional CLAHE.
4. **Segment** — Cellpose (`cpsam` by default, or a custom model path).
5. **Extract** — per-cell mean intensity per frame, computed lazily on Dask.
6. **Detrend** — Blackman-windowed sinc filter (cf. pyBOAT).
7. **Detect peaks** — `scipy.signal.find_peaks` with width / height / prominence thresholds.
8. **Periodicity** — STD and CoV of inter-peak intervals, scored against a shuffled-peaks null.
9. **Adjacency** — neighbour graph from segmented masks (kernel-based proximity).
10. **Synchronicity** — co-firing peaks within a time window and topological distance, z-scored against label-shuffled controls.

## Documentation

Full API reference and tutorials: **<https://pykrait.readthedocs.io/en/latest/>**.

## Citation

If you use pyKrait, please cite the [preprint](https://www.biorxiv.org/content/10.64898/2026.05.10.723715v1): Hebach, N. R., Olshausen, N., Schlag, J., Mayer, C. D., Henkenjohann, J., Kraft, T., Bang, S., Thommek, C., Nürnberg, C., Horsak, N. E., Hoffmann, D. C., Kourtesakis, A., Porzberg, N., Flores Valle, A., Linke, C. Z., Yang, Y., Azorín, D. D., Hausmann, D., Venkataramani, V., … Karreman, M. A. (2026). Collective cancer cell calcium activity drives brain metastasis. bioRxiv, 2026.05.10.723715. https://doi.org/10.64898/2026.05.10.723715

## Acknowledgements

pyKrait builds on and heavily uses prior open-source work:

- [Cellpose](https://github.com/MouseLand/cellpose) for cell segmentation
- [pyBOAT](https://github.com/tensionhead/pyBOAT) for the sinc-filter detrending approach
- [bioio](https://github.com/bioio-devs/bioio) for image I/O
- [Dask](https://www.dask.org/) for lazy array computation

