Metadata-Version: 2.1
Name: napari-3dtimereg
Version: 0.0.12
Summary: Registration of 3D movies applied to all channels
Author-email: Gaëlle Letort <gaelle.letort@pasteur.fr>
License: BSD-3-Clause
Project-URL: Homepage, https://gitlab.pasteur.fr/gletort/napari-3dtimereg
Project-URL: Bug Tracker, https://gitlab.pasteur.fr/gletort/napari-3dtimereg/issues
Project-URL: Documentation, https://gitlab.pasteur.fr/gletort/napari-3dtimereg#README.md
Project-URL: Source Code, https://gitlab.pasteur.fr/gletort/napari-3dtimereg
Classifier: Framework :: napari
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Scientific/Engineering :: Image Processing
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: napari
Requires-Dist: magicgui
Requires-Dist: qtpy
Requires-Dist: tifffile
Requires-Dist: imaris-ims-file-reader
Requires-Dist: czifile
Requires-Dist: itk==5.3.0
Requires-Dist: itk-registration
Requires-Dist: itk-elastix
Requires-Dist: pydantic<=1.10.14

# napari-3dtimereg

[![License BSD-3](https://img.shields.io/pypi/l/napari-3dtimereg.svg?color=green)](https://gitlab.pasteur.fr/gletort/napari-3dtimereg/-/blob/main/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/napari-3dtimereg.svg?color=green)](https://pypi.org/project/napari-3dtimereg)
[![Python Version](https://img.shields.io/pypi/pyversions/napari-3dtimereg.svg?color=green)](https://python.org)
[![napari hub](https://img.shields.io/endpoint?url=https://api.napari-hub.org/shields/napari-3dtimereg)](https://napari-hub.org/plugins/napari-3dtimereg)

Temporal registration of 2D/3D movies on one channel based on [itk-elastix](https://pypi.org/project/itk-elastix/), and transpose alignement to the other channels.

Adaptated from [multireg](https://gitlab.pasteur.fr/gletort/multireg) for temporal movies.
For a tutorial on using `elastix` for registration, see [this tutorial](https://m-albert.github.io/elastix_tutorial/intro.html).

* [Installation](#installation)
    * [System requirement](#system-requirement)
    * [Installation steps](#installation-steps)
* [Usage](#usage)
* [Demo](#demotest)
* [License](#license)

----------------------------------
# Installation


## System requirement

`napari-3dtimereg` is a Napari plugin therefore can be installed on all operating systems. 
It requires a python environement compatible with `Napari` and `elastix` version.

### Tested system
It has been tested on python 3.9, napari XX and elastix XX.

## Installation steps

In a python environment that contains Napari:

* You can install the plugin directly in `Napari` by going to `Plugins>Install/Uninstall plugins` and search for `napari-3dtimereg`

* Or you can install `napari-3dtimereg` via [pip]:

    pip install napari-3dtimereg


If you don't have Napari installed yet, see [here](https://napari.org/stable/tutorials/fundamentals/quick_start.html#napari-quick-start) for installation instructions.


# Usage

You can launch `3dtimereg` in napari by going to `Plugins>Do 3D movie registration (napari-3dtimereg)`.

### Choose movie and reference chanel

First, choose select the movie that you want to register. The plugin will create a folder `aligned` in the folder of your selected movie where the results will be saved.

Choose the color chanel on which to calculate the registration (`reference chanel`). Color chanels are numbered from 0 to nchanels, and you can see their respective number in the layer list on the left panel of Napari. Click on `Update` when the correct chanel is selected to go to the registration calculation step.

### Calculate temporal alignement

The registration is calculated iteratively from one frame to another. Thus the first frame is not moved and all the other frames are aligned to it.
You can tune several parameters in this plugin:

![parameters screenshot](./imgs/parameters.png "Registration parameters")

The other parameters are parameters to use [itk-elastix](https://elastix.lumc.nl/) to calculate the registration.
* `show log`: to see the log of Elastix calculation
* `do rigid`: performs a rigid (affine) transformation step, that allowed to correct for translations/rotations.
* `do bspline`: performs a b-spline based transformation step, that allowed for local deformations in the image.
* `show advanced parameters`: to control the parameters used in the rigid and/or bspline transformations. These parameters control the size of the local registrations calculated, the resolutions at which the transformations are calculated, and can thus greatly impact the results.
* `final order`: is the final order of the B-Splines used for the registration. 
* `resolution`: is the number of consecutives resolutions at which the registration will be made. First the registration is made at the lowest level of resolution, correcting global deformations/motions, then at each step, the registration is done on higher resolution, allowing to correct for more local deformations.
* `final spacing`: is the physical spacing of the smallest resolution.
* `iterations`: are the maximum number of iterations allowed to minimize the distance between the two images for each resolution and type of registration.

If both rigid and bspline transformations, the program first applies the rigid transformation to allow for a global registration of the images. Then it will performs the second step of b-spline transformation that can includes local deformations.

For each frame, after calculating the registration on the reference chanel, the plugin will apply the calculated transformation to all the other color chanels of the initial movie. All results are saved as separated images in the `aligned` folder during the computation.

### Create the final aligned movie

When all frames have been processed, each color chanel and each frame have been saved in the `aligned` folder as separated images. This is usefull to calculate the registration on large movies without having to keep all the intermediates and calculated images in memory. You can directly use these separated images, or reconstruct a single composite movie of the result.

If you click on `Concatenate aligned images` on the plugin interface, the plugin will create a single composite movie from the aligned images, save it and delete the separated images in the `aligned` folder. 


# Demo/Test

To test this plugin, you can use our [demo dataset](https://zenodo.org/records/15028169) proposed in a Zenodo repository or directly on your own dataset.
If you use the data from the Zenodo repository, the file to use to test this plugin is the movie `Registration_WTn1_Crop_V1.tif` which has been cropped from our dataset to reduce the size.

## Steps
The input of the process should be a `.tiff` 3D and temporal stack with one or more channels. 
If you want to use fix points registration option, you should have a file that contains the position of each tracked point at each time.
See in our demo dataset for the format of this file.
It can be for example generated from a [TrackMate](https://imagej.net/plugins/trackmate/) file with the macro [getTimePoints.py](./src/napari_3dtimereg/getTimePoints.py) proposed in this repository. 
Run it in Fiji by going to `Plugins>Macro>Edit`, openning the [getTimePoints.py](./src/napari_3dtimereg/getTimePoints.py) macro and then click `Run`.
It will generate a file `reference_points.csv` that you should put in the same folder as your movie to register (or select this file in the plugin interface in the `refpoints file` parameter).

Activate your python virtual environement in which napari and the plugin are installed.
For example, if the name of the environment is `timereg-env`, type: `conda activate timereg-env`.

When it's activated, type `napari` to start Napari. 
Go to `Plugins>napari-3dtimereg` to start it.

Load the movie to register. The plugin will ask you to choose the channel on which to calculate the registration, so choose a channel that should be immobile during the movie (for example, the channel containing the fibers which should be stable over time).
Set the `reference channel` to the corresponding channel number (that you can see in the bottom left part of the viewer, with the layers name `Movie_C_*`). In our example dataset, choose `0` as the reference channel (the red channel that contains the fibers).

![example screenshot](./imgs/load.png)

Click `Update` to get to the step to choose the [registration parameters](#calculate-temporal-alignement).

### Parameters

In [Sarde et al., 2025](https://www.biorxiv.org/content/10.1101/2025.03.13.643016v1), we used the following parameter values to register our intravital movies:
* `use reference points: True`
* `rigid resolution: 4`
* `spline resolution: 2`
* `iterations: 1000`
* `rigid final spacing: 50`
* `spline final spacing: 200`
* `final order: 1`

When the parameters have been selected, the registration will be calculated. 
This step can take a lot of time depending on your movie size. 


## Output
At the end of the process, you will get a folder named `aligned` in which the registered movie will be saved.


# License

Distributed under the terms of the [BSD-3] license, "napari-3dtimereg" is free and open source software.

If you use it, please cite our paper [Sarde et al., 2025](https://www.biorxiv.org/content/10.1101/2025.03.13.643016v1). 


[napari]: https://github.com/napari/napari
[@napari]: https://github.com/napari
[BSD-3]: http://opensource.org/licenses/BSD-3-Clause
[tox]: https://tox.readthedocs.io/en/latest/
[pip]: https://pypi.org/project/pip/
[PyPI]: https://pypi.org/
