import logging
import argparse
import sys
import traceback
from pathlib import Path
from cellmil.segmentation import CellSegmenter
from cellmil.interfaces import CellSegmenterConfig
from cellmil.interfaces.CellSegmenterConfig import ModelType
# Setup logging with enhanced format
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
[docs]def cell_segmentation(args: argparse.Namespace) -> None:
"""Perform cell segmentation using the specified model."""
try:
config = CellSegmenterConfig(
model=args.model,
gpu=args.gpu,
wsi_path=args.wsi_path,
patched_slide_path=args.patched_slide_path,
)
cell_segmenter = CellSegmenter(config)
cell_segmenter.process()
logger.info(f"Cell segmentation with {args.model} completed successfully.")
except Exception as e:
# Get traceback information
tb = traceback.format_exc()
logger.error(f"Error during cell segmentation: {e}\n{tb}")
sys.exit(1)
[docs]def setup_parser() -> argparse.ArgumentParser:
"""Set up command line argument parser"""
parser = argparse.ArgumentParser(
description="CLI tool for cell segmentation in whole slide images"
)
# Cell segmentation arguments
parser.add_argument(
"--model",
type=str,
required=True,
choices=ModelType.values(),
help="Model to use for cell segmentation",
)
parser.add_argument(
"--gpu", type=int, default=0, help="GPU device ID to use (default: 0)"
)
parser.add_argument(
"--wsi_path", type=Path, required=True, help="Path to the whole slide image"
)
parser.add_argument(
"--patched_slide_path",
type=Path,
required=True,
help="Path to the patched slide data generated by patch extraction",
)
return parser
[docs]def main():
"""Entry point for the cell segmentation CLI tool."""
parser = setup_parser()
args = parser.parse_args()
# Call cell segmentation
cell_segmentation(args)
if __name__ == "__main__":
main()