Skip to content

Commit

Permalink
Feature: CLI work in progress (#130)
Browse files Browse the repository at this point in the history
### Description

Adding a CAREamics CLI ! 

Full functionality will include training, prediction and configuration
building from the command line. Currently the prediction command is not
implemented.

- **What**: Added an entrypoint for the careamics command in the
pyproject.toml file. Added functions to wrap existing CAREamics
functionality that will be called with console commands.
- **Why**: A CLI will be useful for benchmarking, running on the HPC,
pipelines and convenience.
- **How**: 
- For convenience and readability the CLI has been built using third
party package Typer. Typer makes it easy to manage arguments, options,
commands and subcommands.
- The `train` command creates a `CAREamist` object from a given config
file and then runs the `CAREamist.train` method on given training and
optional validation files.
- The `conf` subcommands (`care`, `n2n`, `n2v`) are wrappers for the
existing configuration building convenience functions in
*src/careamics/config/configuration_factory.py*. The conf subcommands
have the additional functionality of saving the created configuration to
a *.yaml* file.

### Changes Made

**Added**: 
- *src/careamics/cli/main.py*
    - Creates the main `typer.Typer` object and a function to run it.
    - Contains `train` and `predict` commands. 
- *src/careamics/cli/conf.py*
    - Contains configuration builder `conf` sub `typer.Typer` object.
    - Contains subcommands `care`, `n2n` and `n2v`. 
- *src/careamics/cli/\_\_init\_\_.py*
- *tests/cli/test_main.py*
- *tests/cli/test_conf.py*
- *tests/cli/\_\_init\_\_.py*

**Modified**: 
- *pyproject.toml*: 
     - added careamics cli entrypoint (under [project.scripts])
     - added typer dependency.

### Additional Notes and Examples

The --help flag can be run on all commands and subcommands. This is
helpfully managed by Typer.
```console
(careamics) [~]melisande.croft@lin-imganf-m-01$ careamics --help
                                                                                
 Usage: careamics [OPTIONS] COMMAND [ARGS]...                                   
                                                                                
 Run CAREamics algorithms from the command line, including Noise2Void and its   
 many variants and cousins                                                      
                                                                                
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --install-completion          Install completion for the current shell.      │
│ --show-completion             Show completion for the current shell, to copy │
│                               it or customize the installation.              │
│ --help                        Show this message and exit.                    │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ conf      Build and save CAREamics configuration files.                      │
│ predict   Create and save predictions from CAREamics models.                 │
│ train     Train CAREamics models.                                            │
╰──────────────────────────────────────────────────────────────────────────────╯


```

```console
(careamics) [~]melisande.croft@lin-imganf-m-01$ careamics train --help
                                                                                
 Usage: careamics train [OPTIONS] SOURCE                                        
                                                                                
 Train CAREamics models.                                                        
                                                                                
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ *    source      FILE  Path to a configuration file or a trained model.      │
│                        [default: None]                                       │
│                        [required]                                            │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ *  --train-source     -ts                       PATH       Path to the       │
│                                                            training data.    │
│                                                            [default: None]   │
│                                                            [required]        │
│    --train-target     -tt                       PATH       Path to train     │
│                                                            target data.      │
│                                                            [default: None]   │
│    --val-source       -vs                       PATH       Path to           │
│                                                            validation data.  │
│                                                            [default: None]   │
│    --val-target       -vt                       PATH       Path to           │
│                                                            validation target │
│                                                            data.             │
│                                                            [default: None]   │
│    --use-in-memory    -m   --not-in-memory  -M             Use in memory     │
│                                                            dataset if        │
│                                                            possible.         │
│                                                            [default:         │
│                                                            use-in-memory]    │
│    --val-percentage                             FLOAT      Percentage of     │
│                                                            files to use for  │
│                                                            validation.       │
│                                                            [default: 0.1]    │
│    --val-minimum-sp…                            INTEGER    Minimum number of │
│                                                            files to use for  │
│                                                            validation,       │
│                                                            [default: 1]      │
│    --work-dir         -wd                       DIRECTORY  Path to working   │
│                                                            directory in      │
│                                                            which to save     │
│                                                            checkpoints and   │
│                                                            logs              │
│                                                            [default: None]   │
│    --help                                                  Show this message │
│                                                            and exit.         │
╰──────────────────────────────────────────────────────────────────────────────╯


```
As you can see from the help output above, I have decided on the design
choice, for the `train` command, the the config file is an argument and
everything else is passed as an option. So to train with a `<CONFIG
FILE>` and `<TRAIN SOURCE>` the syntax would be:
```console
careamics train <CONFIG FILE> -ts <TRAIN SOURCE>
```
---

- [x] Code builds and passes tests locally, including doctests
- [x] New tests have been added (for bug fixes/features)
- [x] Pre-commit passes
- [ ] PR to the documentation exists (for bug fixes / features)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
melisande-c and pre-commit-ci[bot] authored Sep 20, 2024
1 parent 59fa2dd commit 7435ce2
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies = [
'pydantic>=2.5,<2.9',
'pytorch_lightning>=2.2.0',
'pyyaml',
'typer==0.12.3',
'scikit-image<=0.23.2',
'zarr<3.0.0',
]
Expand All @@ -75,6 +76,10 @@ tensorboard = ["tensorboard", "protobuf==3.20.3"]
homepage = "https://careamics.github.io/"
repository = "https://github.com/CAREamics/careamics"

# cli
[project.scripts]
careamics = "careamics.cli.main:run"

# https://beta.ruff.rs/docs
[tool.ruff]
line-length = 88
Expand Down Expand Up @@ -188,3 +193,4 @@ exclude = [ # don't report on objects that match any of these regex
"test_*",
"src/careamics/lvae_training/*",
]

4 changes: 2 additions & 2 deletions src/careamics/careamist.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CAREamist:
def __init__( # numpydoc ignore=GL08
self,
source: Union[Path, str],
work_dir: Optional[str] = None,
work_dir: Optional[Union[Path, str]] = None,
experiment_name: str = "CAREamics",
callbacks: Optional[list[Callback]] = None,
) -> None: ...
Expand All @@ -84,7 +84,7 @@ def __init__( # numpydoc ignore=GL08
def __init__( # numpydoc ignore=GL08
self,
source: Configuration,
work_dir: Optional[str] = None,
work_dir: Optional[Union[Path, str]] = None,
experiment_name: str = "CAREamics",
callbacks: Optional[list[Callback]] = None,
) -> None: ...
Expand Down
5 changes: 5 additions & 0 deletions src/careamics/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
Package containing functions called by the careamics cli.
Built using third party package Typer.
"""
Loading

0 comments on commit 7435ce2

Please sign in to comment.