Skip to content

Commit 809389f

Browse files
committed
Wire save_spatialdata flag into ISTSegmentationWriter and CLI
Adds --save-spatialdata and --boundary-method CLI flags (under the existing I/O group) and threads them through ISTSegmentationWriter to emit a SpatialData zarr store next to the AnnData output. Adds the 'spatialdata' optional dependency. Integration shape taken from PR #37 commit f99b3fb by @enric-bazz, narrowed to the output path only (no loader, no 3D, no QV filter, no logging refactor). Original-author: enric-bazz <enrico.bazzacco02@outlook.it>
1 parent b16b111 commit 809389f

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ dependencies = [
2929
"tifffile"
3030
]
3131

32+
[project.optional-dependencies]
33+
spatialdata = [
34+
"spatialdata>=0.7.2",
35+
]
36+
3237
[build-system]
3338
requires = ["hatchling"]
3439
build-backend = "hatchling.build"

src/segger/cli/segment.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,17 @@ def segment(
306306
"save_anndata",
307307
group=group_io,
308308
)] = registry.get_default("save_anndata"),
309-
309+
310+
save_spatialdata: Annotated[bool, registry.get_parameter(
311+
"save_spatialdata",
312+
group=group_io,
313+
)] = registry.get_default("save_spatialdata"),
314+
315+
boundary_method: Annotated[
316+
Literal["convex_hull", "skip"],
317+
registry.get_parameter("boundary_method", group=group_io),
318+
] = registry.get_default("boundary_method"),
319+
310320
debug: Annotated[bool, Parameter(
311321
help="Whether to save additional debug information (trainer, predictions).",
312322
)] = "none",
@@ -395,6 +405,8 @@ def segment(
395405
writer = ISTSegmentationWriter(
396406
output_directory,
397407
save_anndata=save_anndata,
408+
save_spatialdata=save_spatialdata,
409+
boundary_method=boundary_method,
398410
debug=debug,
399411
)
400412
trainer = Trainer(

src/segger/data/writer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ def __init__(
2828
self,
2929
output_directory: Path,
3030
save_anndata: bool = True,
31+
save_spatialdata: bool = False,
32+
boundary_method: str = "convex_hull",
3133
debug: bool = False
3234
):
3335
# "write" callback at the end of prediction epoch
3436
super().__init__(write_interval="epoch")
3537
self.output_directory = Path(output_directory)
3638
self.save_anndata = save_anndata
39+
self.save_spatialdata = save_spatialdata
40+
self.boundary_method = boundary_method
3741

3842
# setup debugging
3943
self.debug = debug
@@ -83,6 +87,19 @@ def write_on_epoch_end(
8387
if self.save_anndata:
8488
self.write_anndata(trainer, segmentation)
8589

90+
# write spatialdata zarr
91+
if self.save_spatialdata:
92+
logger.debug("Writing SpatialData output...")
93+
from ..export.spatialdata_writer import SpatialDataWriter
94+
SpatialDataWriter(
95+
boundary_method=self.boundary_method,
96+
).write(
97+
predictions=segmentation,
98+
output_dir=self.output_directory,
99+
transcripts=trainer.datamodule.tx,
100+
output_name="segger_segmentation.zarr",
101+
)
102+
86103
def write_anndata(
87104
self,
88105
trainer: Trainer,

0 commit comments

Comments
 (0)