File size: 3,791 Bytes
beb4f15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Application entry point for launching the Fiberpose GUI, CLI or web interface.

Installed as the ``fiberpose`` command and run by ``python -m fiberpose``.
"""

from __future__ import annotations

import os

# Must be set before numpy/onnxruntime/torch load their OpenMP runtimes.
os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "TRUE")

import argparse

from . import __version__
from .paths import USER_CONFIG_PATH, default_config_path


def _build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="fiberpose", description="Fiberpose launcher (GUI, CLI, web).")
    parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
    group = parser.add_mutually_exclusive_group()
    group.add_argument("--cli", action="store_true", help="Run command-line batch processing mode.")
    group.add_argument("--gui", action="store_true", help="Force GUI mode (default when --cli is not set).")
    group.add_argument("--web", action="store_true", help="Serve the Gradio web interface.")

    parser.add_argument("--input", dest="input_path", help="Input path for CLI mode (folder, stack, or image).")
    parser.add_argument(
        "--config",
        dest="config_path",
        default=None,
        help=f"Config JSON path (default: {USER_CONFIG_PATH}).",
    )
    parser.add_argument("--output", dest="output_dir", help="Output directory for CLI exports.")
    parser.add_argument(
        "--measurements",
        action="store_true",
        help="Compute full measurements in CLI mode (default: segmentation only).",
    )
    parser.add_argument(
        "--invert",
        action="store_true",
        default=None,
        help="Invert input intensities before inference (CLI mode).",
    )
    parser.add_argument(
        "--save-kind",
        choices=("results", "csv"),
        default="results",
        help="CLI export preset kind (default: results).",
    )
    return parser


def _launch_gui(config_path: str) -> int:
    try:
        from .gui import launch
    except ImportError:
        print('Error: the GUI requires PyQt6. In the Fiberpose folder, run:  pip install -e ".[gui]"')
        return 2
    return launch(config_path)


def _launch_web(config_path: str) -> int:
    try:
        from .web import launch
    except ImportError:
        print('Error: the web interface requires Gradio. Run:  pip install "fiberpose[web]"')
        return 2
    from .domain import AppConfig

    launch(AppConfig.load_from_json(config_path))
    return 0


def _launch_cli(args: argparse.Namespace, config_path: str) -> int:
    if not args.input_path:
        print("Error: --input is required with --cli.")
        return 2
    if not os.path.exists(os.path.abspath(args.input_path)):
        print(f"Error: input path does not exist: {os.path.abspath(args.input_path)}")
        return 2

    from .cli_runner import CliRunner

    runner = CliRunner(
        input_path=args.input_path,
        config_path=config_path,
        output_dir=args.output_dir,
        compute_measurements=bool(args.measurements),
        invert_input=args.invert,
        save_kind=args.save_kind,
    )
    return int(runner.run())


def main(argv: list[str] | None = None) -> int:
    parser = _build_parser()
    args = parser.parse_args(argv)

    if args.config_path:
        config_path = os.path.abspath(args.config_path)
        if not os.path.exists(config_path):
            print(f"Error: config path does not exist: {config_path}")
            return 2
    else:
        config_path = default_config_path()

    if args.cli:
        return _launch_cli(args, config_path)
    if args.web:
        return _launch_web(config_path)
    return _launch_gui(config_path)


if __name__ == "__main__":
    raise SystemExit(main())