patchkori 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- patchkori/__init__.py +3 -0
- patchkori/cli.py +107 -0
- patchkori/config.py +32 -0
- patchkori/core/__init__.py +17 -0
- patchkori/core/models.py +111 -0
- patchkori/core/pyramidal_cropper/__init__.py +5 -0
- patchkori/core/pyramidal_cropper/adapter.py +476 -0
- patchkori/core/sample_generator.py +77 -0
- patchkori/ui/__init__.py +5 -0
- patchkori/ui/app.py +42 -0
- patchkori/ui/icons.py +38 -0
- patchkori/ui/registry.py +40 -0
- patchkori/ui/theme.py +287 -0
- patchkori/ui/tools/__init__.py +5 -0
- patchkori/ui/tools/pyramidal_cropper.py +666 -0
- patchkori-0.1.0.dist-info/METADATA +153 -0
- patchkori-0.1.0.dist-info/RECORD +19 -0
- patchkori-0.1.0.dist-info/WHEEL +4 -0
- patchkori-0.1.0.dist-info/entry_points.txt +2 -0
patchkori/__init__.py
ADDED
patchkori/cli.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Optional
|
|
4
|
+
import typer
|
|
5
|
+
|
|
6
|
+
from patchkori.config import AppConfig
|
|
7
|
+
from patchkori.core.models import CropRequest
|
|
8
|
+
from patchkori.core.pyramidal_cropper.adapter import PyramidalCropperAdapter
|
|
9
|
+
from patchkori.core.sample_generator import generate_sample_pyramidal_ome_tiff
|
|
10
|
+
|
|
11
|
+
app = typer.Typer(
|
|
12
|
+
name="patchkori",
|
|
13
|
+
help="PatchKori: Modular bioinformatics imaging platform & out-of-core pyramidal toolset.",
|
|
14
|
+
add_completion=False,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@app.command("launch")
|
|
19
|
+
def launch(
|
|
20
|
+
host: str = typer.Option("127.0.0.1", "--host", "-h", help="Host interface to bind."),
|
|
21
|
+
port: int = typer.Option(7860, "--port", "-p", help="Port number for the Gradio server."),
|
|
22
|
+
share: bool = typer.Option(False, "--share", help="Create a public Gradio share link."),
|
|
23
|
+
allowed_dir: Optional[list[Path]] = typer.Option(
|
|
24
|
+
None,
|
|
25
|
+
"--allowed-dir",
|
|
26
|
+
"-d",
|
|
27
|
+
help="Additional local directories allowed for direct file serving.",
|
|
28
|
+
),
|
|
29
|
+
demo_mode: bool = typer.Option(False, "--demo", help="Run in restricted Cloud Demo mode."),
|
|
30
|
+
inbrowser: bool = typer.Option(True, "--browser/--no-browser", help="Open browser automatically."),
|
|
31
|
+
) -> None:
|
|
32
|
+
"""Launch the PatchKori Gradio web interface locally."""
|
|
33
|
+
from patchkori.ui.app import create_app
|
|
34
|
+
|
|
35
|
+
cfg = AppConfig(is_demo_mode=demo_mode)
|
|
36
|
+
demo = create_app(cfg)
|
|
37
|
+
|
|
38
|
+
allowed_paths = [str(Path.cwd()), str(Path.home())]
|
|
39
|
+
if allowed_dir:
|
|
40
|
+
allowed_paths.extend(str(p.expanduser().resolve()) for p in allowed_dir)
|
|
41
|
+
|
|
42
|
+
typer.secho(
|
|
43
|
+
f"🚀 Launching {cfg.app_name} on http://{host}:{port} ...",
|
|
44
|
+
fg=typer.colors.CYAN,
|
|
45
|
+
bold=True,
|
|
46
|
+
)
|
|
47
|
+
demo.launch(
|
|
48
|
+
server_name=host,
|
|
49
|
+
server_port=port,
|
|
50
|
+
share=share,
|
|
51
|
+
inbrowser=inbrowser,
|
|
52
|
+
show_api=False,
|
|
53
|
+
allowed_paths=allowed_paths if not demo_mode else [str(cfg.sample_data_dir)],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@app.command("crop")
|
|
58
|
+
def crop_cli(
|
|
59
|
+
input_path: Path = typer.Argument(..., help="Path to pyramidal TIFF / OME-TIFF / QPTIFF."),
|
|
60
|
+
output_dir: Path = typer.Option(Path("./outputs/crops"), "--output-dir", "-o"),
|
|
61
|
+
level: int = typer.Option(0, "--level", "-l", help="Target pyramid level index (0 = base)."),
|
|
62
|
+
x: int = typer.Option(0, "--x", help="Top-left X coordinate at target level."),
|
|
63
|
+
y: int = typer.Option(0, "--y", help="Top-left Y coordinate at target level."),
|
|
64
|
+
width: int = typer.Option(512, "--width", "-W", help="Crop width in pixels at target level."),
|
|
65
|
+
height: int = typer.Option(512, "--height", "-H", help="Crop height in pixels at target level."),
|
|
66
|
+
mode: str = typer.Option("single_roi", "--mode", "-m", help="'single_roi' or 'tile_grid'."),
|
|
67
|
+
tile_size: int = typer.Option(256, "--tile-size", help="Tile size when mode='tile_grid'."),
|
|
68
|
+
tile_overlap: int = typer.Option(0, "--overlap", help="Tile overlap when mode='tile_grid'."),
|
|
69
|
+
output_format: str = typer.Option("ome.tif", "--format", "-f", help="ome.tif, tif, png, or zarr."),
|
|
70
|
+
prefix: str = typer.Option("crop", "--prefix", help="Output filename prefix."),
|
|
71
|
+
) -> None:
|
|
72
|
+
"""Headless CLI command for out-of-core pyramidal cropping without launching the web UI."""
|
|
73
|
+
adapter = PyramidalCropperAdapter()
|
|
74
|
+
req = CropRequest(
|
|
75
|
+
input_path=input_path,
|
|
76
|
+
output_dir=output_dir,
|
|
77
|
+
target_level=level,
|
|
78
|
+
x=x,
|
|
79
|
+
y=y,
|
|
80
|
+
width=width,
|
|
81
|
+
height=height,
|
|
82
|
+
mode="tile_grid" if mode == "tile_grid" else "single_roi",
|
|
83
|
+
tile_size=tile_size,
|
|
84
|
+
tile_overlap=tile_overlap,
|
|
85
|
+
output_format=output_format, # type: ignore[arg-type]
|
|
86
|
+
prefix=prefix,
|
|
87
|
+
)
|
|
88
|
+
res = adapter.execute_crop(req)
|
|
89
|
+
typer.secho(f"✅ {res.summary_message}", fg=typer.colors.GREEN, bold=True)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@app.command("generate-sample")
|
|
93
|
+
def generate_sample_cmd(
|
|
94
|
+
output_path: Path = typer.Option(
|
|
95
|
+
Path("assets/sample_data/sample_tonsil_pyramidal.ome.tif"),
|
|
96
|
+
"--output",
|
|
97
|
+
"-o",
|
|
98
|
+
help="Destination path for synthetic 4-level pyramidal OME-TIFF.",
|
|
99
|
+
),
|
|
100
|
+
) -> None:
|
|
101
|
+
"""Generate a synthetic 4-level multiplexed pyramidal OME-TIFF for demos and testing."""
|
|
102
|
+
out = generate_sample_pyramidal_ome_tiff(output_path)
|
|
103
|
+
typer.secho(f"✅ Generated sample pyramidal OME-TIFF at: {out}", fg=typer.colors.GREEN)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
app()
|
patchkori/config.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import os
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _default_sample_dir() -> Path:
|
|
8
|
+
repo_root = Path(__file__).resolve().parents[2]
|
|
9
|
+
return repo_root / "assets" / "sample_data"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _default_output_dir() -> Path:
|
|
13
|
+
return Path.cwd() / "outputs" / "crops"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class AppConfig:
|
|
18
|
+
"""Runtime configuration supporting both Local Workstation Mode and Cloud Demo Mode."""
|
|
19
|
+
|
|
20
|
+
app_name: str = "PatchKori — Bioinformatics Patch Kits Hub"
|
|
21
|
+
is_demo_mode: bool = False
|
|
22
|
+
max_workers: int = 2
|
|
23
|
+
max_thumb_edge: int = 1024
|
|
24
|
+
sample_data_dir: Path = field(default_factory=_default_sample_dir)
|
|
25
|
+
default_output_dir: Path = field(default_factory=_default_output_dir)
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def from_env(cls) -> AppConfig:
|
|
29
|
+
mode = os.environ.get("PATCHKORI_MODE", "local").strip().lower()
|
|
30
|
+
is_demo = mode in ("demo", "cloud", "hf", "spaces", "1", "true")
|
|
31
|
+
workers = int(os.environ.get("PATCHKORI_WORKERS", "2"))
|
|
32
|
+
return cls(is_demo_mode=is_demo, max_workers=workers)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Headless bioinformatics core engines and data models."""
|
|
2
|
+
|
|
3
|
+
from patchkori.core.models import (
|
|
4
|
+
CropRequest,
|
|
5
|
+
CropResult,
|
|
6
|
+
LevelInfo,
|
|
7
|
+
PyramidMetadata,
|
|
8
|
+
translate_box_between_levels,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"CropRequest",
|
|
13
|
+
"CropResult",
|
|
14
|
+
"LevelInfo",
|
|
15
|
+
"PyramidMetadata",
|
|
16
|
+
"translate_box_between_levels",
|
|
17
|
+
]
|
patchkori/core/models.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Literal
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class LevelInfo:
|
|
9
|
+
"""Metadata for a single resolution level inside a pyramidal image."""
|
|
10
|
+
|
|
11
|
+
level_index: int
|
|
12
|
+
width: int
|
|
13
|
+
height: int
|
|
14
|
+
channels: int
|
|
15
|
+
dtype: str
|
|
16
|
+
downsample_factor: float
|
|
17
|
+
is_tiled: bool
|
|
18
|
+
tile_width: int | None = None
|
|
19
|
+
tile_height: int | None = None
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def estimated_ram_mb(self) -> float:
|
|
23
|
+
bytes_per_pixel = 2 if "16" in self.dtype else (4 if "32" in self.dtype else 1)
|
|
24
|
+
return (self.width * self.height * self.channels * bytes_per_pixel) / (1024 ** 2)
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def label(self) -> str:
|
|
28
|
+
return (
|
|
29
|
+
f"Level {self.level_index}: {self.width}×{self.height} px "
|
|
30
|
+
f"({self.downsample_factor:.1f}x downsample, ~{self.estimated_ram_mb:.1f} MB)"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class PyramidMetadata:
|
|
36
|
+
"""Container for lazily-inspected pyramidal whole-slide or multiplexed image metadata."""
|
|
37
|
+
|
|
38
|
+
file_path: Path
|
|
39
|
+
format_name: str
|
|
40
|
+
axes: str
|
|
41
|
+
levels: list[LevelInfo]
|
|
42
|
+
channel_names: list[str]
|
|
43
|
+
physical_pixel_size_um: tuple[float, float] | None = None
|
|
44
|
+
thumb_level_index: int = 0
|
|
45
|
+
thumb_width: int = 1
|
|
46
|
+
thumb_height: int = 1
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def base_level(self) -> LevelInfo:
|
|
50
|
+
return self.levels[0]
|
|
51
|
+
|
|
52
|
+
def get_level(self, level_index: int) -> LevelInfo:
|
|
53
|
+
if 0 <= level_index < len(self.levels):
|
|
54
|
+
return self.levels[level_index]
|
|
55
|
+
raise IndexError(f"Pyramid level {level_index} out of range (0..{len(self.levels) - 1})")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def translate_box_between_levels(
|
|
59
|
+
x_src: float,
|
|
60
|
+
y_src: float,
|
|
61
|
+
w_src: float,
|
|
62
|
+
h_src: float,
|
|
63
|
+
src_width: int,
|
|
64
|
+
src_height: int,
|
|
65
|
+
dst_level: LevelInfo,
|
|
66
|
+
) -> tuple[int, int, int, int]:
|
|
67
|
+
"""
|
|
68
|
+
Translates a bounding box (x, y, w, h) from a source canvas (e.g. thumbnail preview)
|
|
69
|
+
into pixel coordinates clamped to `dst_level`.
|
|
70
|
+
"""
|
|
71
|
+
scale_x = dst_level.width / max(src_width, 1)
|
|
72
|
+
scale_y = dst_level.height / max(src_height, 1)
|
|
73
|
+
|
|
74
|
+
x_dst = max(0, min(int(round(x_src * scale_x)), dst_level.width - 1))
|
|
75
|
+
y_dst = max(0, min(int(round(y_src * scale_y)), dst_level.height - 1))
|
|
76
|
+
w_dst = max(1, min(int(round(w_src * scale_x)), dst_level.width - x_dst))
|
|
77
|
+
h_dst = max(1, min(int(round(h_src * scale_y)), dst_level.height - y_dst))
|
|
78
|
+
return x_dst, y_dst, w_dst, h_dst
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass
|
|
82
|
+
class CropRequest:
|
|
83
|
+
"""Specification for an out-of-core single ROI or grid-tile crop operation."""
|
|
84
|
+
|
|
85
|
+
input_path: Path
|
|
86
|
+
output_dir: Path
|
|
87
|
+
target_level: int
|
|
88
|
+
x: int
|
|
89
|
+
y: int
|
|
90
|
+
width: int
|
|
91
|
+
height: int
|
|
92
|
+
channel_indices: list[int] = field(default_factory=list)
|
|
93
|
+
mode: Literal["single_roi", "tile_grid"] = "single_roi"
|
|
94
|
+
tile_size: int = 256
|
|
95
|
+
tile_overlap: int = 0
|
|
96
|
+
output_format: Literal["ome.tif", "tif", "png", "zarr"] = "ome.tif"
|
|
97
|
+
prefix: str = "crop"
|
|
98
|
+
create_zip: bool = False
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@dataclass
|
|
102
|
+
class CropResult:
|
|
103
|
+
"""Result summary returned by PyramidalCropperAdapter.execute_crop."""
|
|
104
|
+
|
|
105
|
+
output_files: list[Path]
|
|
106
|
+
preview_images: list[Path]
|
|
107
|
+
zip_path: Path | None
|
|
108
|
+
target_level: int
|
|
109
|
+
extracted_shape: tuple[int, ...]
|
|
110
|
+
elapsed_seconds: float
|
|
111
|
+
summary_message: str
|