Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CLI with better type hinting #127

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ $ kom --help

Options
--version -v
--install-completion [bash|zsh|fish|powershell|pwsh] Install completion for the specified shell. [default: None]
--show-completion [bash|zsh|fish|powershell|pwsh] Show completion for the specified shell, to copy it or customize the installation. [default: None]
--help -h Show this message and exit.

Commands
Expand Down
191 changes: 134 additions & 57 deletions kelp_o_matic/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import Optional
from typing import Annotated, Optional

import torch
import typer

from kelp_o_matic import (
Expand All @@ -9,42 +10,71 @@
find_mussels as find_mussels_,
)

cli = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
cli = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}, add_completion=False)


@cli.command()
def find_kelp(
source: Path = typer.Argument(..., help="Input image with Byte data type."),
dest: Path = typer.Argument(..., help="File path location to save output to."),
species: bool = typer.Option(
False,
"--species/--presence",
help="Segment to species or presence/absence level.",
),
crop_size: int = typer.Option(
1024,
help="The data window size to run through the segmentation model.",
),
use_nir: bool = typer.Option(
False,
"--rgbi/--rgb",
help="Use RGB and NIR bands for classification. Assumes RGBI ordering.",
),
band_order: Optional[list[int]] = typer.Option(
None,
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
"To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`.",
),
use_gpu: bool = typer.Option(
True, "--gpu/--no-gpu", help="Enable or disable GPU, if available."
),
use_tta: bool = typer.Option(
False,
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
source: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
readable=True,
help="Input image with Byte data type.",
),
],
dest: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
writable=True,
help="File path location to save output to.",
),
],
species: Annotated[
bool,
typer.Option(
"--species/--presence",
help="Segment to species or presence/absence level.",
),
] = False,
crop_size: Annotated[
int,
typer.Option(
help="The data window size to run through the segmentation model.",
),
] = 1024,
use_nir: Annotated[
bool,
typer.Option(
"--rgbi/--rgb",
help="Use RGB and NIR bands for classification. Assumes RGBI ordering.",
),
] = False,
band_order: Annotated[
Optional[list[int]],
typer.Option(
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
"To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`.",
),
] = None,
use_gpu: Annotated[
bool,
typer.Option("--gpu/--no-gpu", help="Enable or disable GPU, if available."),
] = True,
use_tta: Annotated[
bool,
typer.Option(
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
] = False,
):
"""
Detect kelp in image at path SOURCE and output the resulting classification raster
Expand All @@ -55,27 +85,52 @@ def find_kelp(

@cli.command()
def find_mussels(
source: Path = typer.Argument(..., help="Input image with Byte data type."),
dest: Path = typer.Argument(..., help="File path location to save output to."),
crop_size: int = typer.Option(
1024,
help="The data window size to run through the segmentation model.",
),
band_order: Optional[list[int]] = typer.Option(
None,
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB order. "
"To e.g., reorder a BGR image at runtime, pass flags `-b 3 -b 2 -b 1`.",
),
use_gpu: bool = typer.Option(
True, "--gpu/--no-gpu", help="Enable or disable GPU, if available."
),
use_tta: bool = typer.Option(
False,
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
source: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
readable=True,
help="Input image with Byte data type.",
),
],
dest: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
writable=True,
help="File path location to save output to.",
),
],
crop_size: Annotated[
int,
typer.Option(
help="The data window size to run through the segmentation model.",
),
] = 1024,
band_order: Annotated[
Optional[list[int]],
typer.Option(
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
"To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`.",
),
] = None,
use_gpu: Annotated[
bool,
typer.Option("--gpu/--no-gpu", help="Enable or disable GPU, if available."),
] = True,
use_tta: Annotated[
bool,
typer.Option(
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
] = False,
):
"""
Detect mussels in image at path SOURCE and output the resulting classification
Expand All @@ -90,11 +145,33 @@ def version_callback(value: bool) -> None:
raise typer.Exit()


def gpu_callback(value: bool) -> None:
if value:
typer.echo(f"GPU detected: {torch.cuda.is_available()}")
raise typer.Exit()


@cli.callback()
def main(
version: bool = typer.Option(
None, "--version", "-v", callback=version_callback, is_eager=True
),
version: Annotated[
bool,
typer.Option(
"--version",
"-v",
callback=version_callback,
is_eager=True,
help="Show version and exit.",
),
] = False,
gpu_test: Annotated[
bool,
typer.Option(
"--gpu-test",
callback=gpu_callback,
is_eager=True,
help="Test if GPU is detected and exit.",
),
] = False,
):
return

Expand Down
Loading