Metadata-Version: 2.2
Name: pyxudeconv
Version: 0.0.2.9
Summary: 3D Deconvolution with Pyxu library
Author: Thanh-an Pham
Author-email: pham0204@gmail.com
License: 
        The MIT License (MIT)
        
        Copyright (c) 2024 Thanh-an Pham
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
        
Project-URL: Bug Tracker, https://github.com/ThanhAnPham/pyxudeconv/issues
Project-URL: Documentation, https://github.com/ThanhAnPham/pyxudeconv#README.md
Project-URL: Source Code, https://github.com/ThanhAnPham/pyxudeconv
Project-URL: User Support, https://github.com/ThanhAnPham/pyxudeconv/issues
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Image Processing
Requires-Python: <3.13,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: tifffile
Requires-Dist: pylibCZIrw
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: matplotlib
Requires-Dist: pyxu
Provides-Extra: gpu11
Requires-Dist: pyxu[complete11]; extra == "gpu11"
Provides-Extra: gpu12
Requires-Dist: pyxu[complete]; extra == "gpu12"
Provides-Extra: testing
Requires-Dist: tox; extra == "testing"
Requires-Dist: pytest; extra == "testing"
Requires-Dist: pytest-cov; extra == "testing"

# Package pyxudeconv

[![License MIT](https://img.shields.io/pypi/l/pyxudeconv.svg?color=green)](https://github.com/ThanhAnPham/pyxudeconv/raw/main/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/pyxudeconv.svg?color=green)](https://pypi.org/project/pyxudeconv)
[![Python Version](https://img.shields.io/pypi/pyversions/pyxudeconv.svg?color=green)](https://python.org)
<!--[![napari hub](https://img.shields.io/endpoint?url=https://api.napari-hub.org/shields/pyxudeconv)](https://napari-hub.org/plugins/pyxudeconv)
[![codecov](https://codecov.io/gh/ThanhAnPham/pyxudeconv/branch/main/graph/badge.svg)](https://codecov.io/gh/ThanhAnPham/pyxudeconv)
[![tests](https://github.com/ThanhAnPham/pyxudeconv/workflows/tests/badge.svg)](https://github.com/ThanhAnPham/pyxudeconv/actions)-->

3D Deconvolution with Pyxu library. In particular, it includes the Goujon accelerated Richardson-Lucy (GARL). Please read the extended documentation (GARL PDF file) to know more about the method.

----------------------------------

<!--
Don't miss the full getting started guide to set up your new package:
https://github.com/napari/napari-plugin-template#getting-started

and review the napari docs for plugin developers:
https://napari.org/stable/plugins/index.html
-->

## Installation

Before running pip install, please install via conda some packages beforehand (avoid incompatibilities):

    conda install -c conda-forge numpy scipy

You can then install `pyxudeconv` via [pip] (please look at the next section first):

    pip install pyxudeconv

or with GPU compatibility via [pip] (e.g., CUDA 12.x):

    pip install pyxudeconv[gpu12]

#### Important note for GPU version

- Please install torch with the correct CUDA version via the official pytorch website and via conda.

To install latest development version :

    pip install git+https://github.com/ThanhAnPham/pyxudeconv.git

## Deconvolution with Pyxu

### Deconvolving in a python code

After the package import, the deconvolution is performed by the function `deconvolve` which expects the parameters (namespace). To modify the parameters, there are two ways
  - Load the default parameters via `get_param` and modify each field of interest
    ````
    import pyxudeconv
    par = pyxudeconv.get_param()
    par.psfpath = '/home/tampham/3DWCR/data/simulated/psf_sample_calib_nv_32_coi_2.ome.tif'
    par.datapath = '/home/tampham/3DWCR/data/simulated/g_sample_calib_nv_32_coi_2.ome.tif'
    par.phantom = '/home/tampham/3DWCR/data/simulated/phantom_sample_calib_nv_32_coi_2.ome.tif'
    par.fres = '/home/tampham/yo'
    par.saveIter = [10]
    par.methods = ['RL','GARL','Tikhonov']
    imdeconv = pyxudeconv.deconvolve(par)
    ````
 - Change the json file and load it.
    ````
    import pyxudeconv
    par = pyxudeconv.get_param(param_file='./my_params.json')
    imdeconv = pyxudeconv.deconvolve(par)
    ````

Note that `par.psfpath`and `par.datapath` can be `numpy.ndarray` already loaded in the python code
````
par.psfpath = mypsf #numpy.ndarray
par.datapath = mydata #numpy.ndarray
````

### Deconvolving in a terminal
The main function `deconvolve` can be called as a command-line with arguments or via a bash file (see `main_example.sh` or `main_calibration.sh`) with the option -m.

Two arguments are important if applied on your own data
- `datapath`: Path to the data to deconvolve OR if ran through a python script it can be a ndarray itself
- `psfpath`: Path to the point-spread function OR if ran through a python script it can be a ndarray itself

Currently supported file formats
- `.czi`: Carl Zeiss files
- `.tif`: Expected order of the dimension (Time, Views, Channels, Z, Y, X). Note that the file is first fully loaded, then the region of interest is kept for further processing. One drawback is that the RAM memory usage may be temporarily large.

An example of calling the script with a command-line

```
python -m pyxudeconv.deconvolve --fres '../res/donuts' --gpu 0 --datapath '../data/real_donut/data.tif' --psfpath '../data/real_donut/psf.tif' --saveIter 10 10 10 10 10 --nviews 1 --methods 'RL' 'GARL' --Nepoch 50 --bufferwidth 20 10 10   --pxsz 79.4 79.4 1000 --bg 0 --psf_sz -1 -1 128 128 --roi 0 0 150 150 --config_GARL 'widefield_params'
```

## Note on dependencies

If Goujon accelerated Richardon-Lucy (GARL) and/or GPU will be used, please install `torch`[^1] according to your case. For instance, If the GPU CUDA version is 12.1, the conda environment can be created in a terminal with the commands

- `conda create -n pyxudeconv python=3.11 pytorch=2.4.1 pytorch-cuda=12.1 tifffile numpy scipy matplotlib -c pytorch -c nvidia -c conda-forge` 
- `conda activate pyxudeconv`
- `pip install pyxu[complete]`
<!--- `pip install git+https://github.com/pyxu-org/pyxu.git@feature/fast_fftconvolve pylibCZIrw`) --->

[^1]:21/10/2024, there might be an incompatiblity with the `sympy(==1.13.1)` package version required by `pytorch >= 2.5.0`. Either downgrade `sympy` to `1.13.1` (but may create incompatibilities with `pyxu`) or install `pytorch=2.4.1`.

## Goujon Accelerated Richardson-Lucy (GARL)

To use GARL, call `python -m pyxudeconv.deconvolve` with the argument `--methods 'GARL'`.
To run over different hyperparameters, you can add the argument `--config_GARL 'full_path/your_config_file.json'`.

Note: Each parameter must be a list of values, even if it is a single-valued list.
For instance, here is an example of a `.json` config file
````
{
    "WCRnet": ["pyxudeconv/trained_models/3Dtubes/"],
    "epochoi": [40180],
    "lmbd": [0.1, 0.5],
    "sigWC": [0.1, 0.5]
}
````

Alternatively, one can set a range of values for a parameter (e.g., `lmbd`) as follows
````
{
    "WCRnet": ["pyxudeconv/trained_models/3Dtubes/"],
    "epochoi": [40180],
    "lmbd_min": 0.1,
    "lmbd_max": 0.5,
    "lmbd_nsteps": 2,
    "sigWC": [0.1, 0.5]
}
````

## Simulation

The function `simulate`can simulate measurements obtained from a phantom defined by `--phantom your_phantom_file` convolved with a PSF defined by `--psfpath your_psf_file`. Future releases may change the organisation of the simulation part.


## Contributing

Contributions are very welcome. Tests can be run with [tox], please ensure
the coverage at least stays the same before you submit a pull request.

## License

Distributed under the terms of the [MIT] license,
"pyxudeconv" is free and open source software

## Issues

If you encounter any problems, please [file an issue] along with a detailed description.

[napari]: https://github.com/napari/napari
[copier]: https://copier.readthedocs.io/en/stable/
[@napari]: https://github.com/napari
[MIT]: http://opensource.org/licenses/MIT
[BSD-3]: http://opensource.org/licenses/BSD-3-Clause
[GNU GPL v3.0]: http://www.gnu.org/licenses/gpl-3.0.txt
[GNU LGPL v3.0]: http://www.gnu.org/licenses/lgpl-3.0.txt
[Apache Software License 2.0]: http://www.apache.org/licenses/LICENSE-2.0
[Mozilla Public License 2.0]: https://www.mozilla.org/media/MPL/2.0/index.txt
[napari-plugin-template]: https://github.com/napari/napari-plugin-template

[file an issue]: https://github.com/ThanhAnPham/pyxudeconv/issues

[napari]: https://github.com/napari/napari
[tox]: https://tox.readthedocs.io/en/latest/
[pip]: https://pypi.org/project/pip/
[PyPI]: https://pypi.org/
