Skip to content

Commit

Permalink
add cli and configure runner to use cli
Browse files Browse the repository at this point in the history
  • Loading branch information
silvandeleemput committed Dec 12, 2023
1 parent 1f9dd94 commit f3c73f3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
30 changes: 14 additions & 16 deletions models/gc_picai_baseline/utils/PicaiBaselineRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"""

import json
import sys
from pathlib import Path

from mhubio.core import Instance, InstanceData, IO, Module, ValueOutput, ClassOutput, Meta

# Import the PICAI Classifier algorithm class from /opt/algorithm
from process import csPCaAlgorithm as PicaiClassifier

CLI_PATH = Path(__file__).parent / "cli.py"


@ValueOutput.Name('prostate_cancer_likelihood')
Expand All @@ -36,22 +37,19 @@ class PicaiBaselineRunner(Module):
@IO.Output('cancer_detection_heatmap', 'cspca_detection_map.mha', "mha:mod=hm", bundle='model', the='output heatmap indicating prostate cancer likelihood')
@IO.OutputData('cancer_likelihood', ProstateCancerLikelihood, the='PICAI baseline prostate cancer likelihood')
def task(self, instance: Instance, in_data_t2: InstanceData, in_data_adc: InstanceData, in_data_hbv: InstanceData, cancer_likelihood_json: InstanceData, cancer_detection_heatmap: InstanceData, cancer_likelihood: ProstateCancerLikelihood) -> None:
# Initialize classifier object
classifier = PicaiClassifier()

# Specify input files (the order is important!)
classifier.scan_paths = [
Path(in_data_t2.abspath),
Path(in_data_adc.abspath),
Path(in_data_hbv.abspath),
# build command (order matters!)
cmd = [
sys.executable,
str(CLI_PATH),
in_data_t2.abspath,
in_data_adc.abspath,
in_data_hbv.abspath,
cancer_likelihood_json.abspath,
cancer_detection_heatmap.abspath,
]

# Specify output files
classifier.cspca_detection_map_path = Path(cancer_detection_heatmap.abspath)
classifier.case_confidence_path = Path(cancer_likelihood_json.abspath)

# Run the classifier on the input images
classifier.process()
# run the command as subprocess
self.subprocess(cmd, text=True)

# Extract cancer likelihood value from cancer_likelihood_file
if not Path(cancer_likelihood_json.abspath).is_file():
Expand Down
54 changes: 54 additions & 0 deletions models/gc_picai_baseline/utils/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
--------------------------------------------------
Mhub / DIAG - CLI for the PICAI baseline Algorithm
--------------------------------------------------
--------------------------------------------------
Author: Sil van de Leemput
Email: [email protected]
--------------------------------------------------
"""

import argparse
from pathlib import Path
from process import csPCaAlgorithm as PicaiClassifier


def run_classifier(t2: Path, adc: Path, hbv: Path, cancer_likelihood_json: Path, cancer_detection_heatmap: Path):
# Initialize classifier object
classifier = PicaiClassifier()

# Specify input files (the order is important!)
classifier.scan_paths = [
t2,
adc,
hbv,
]

# Specify output files
classifier.cspca_detection_map_path = cancer_detection_heatmap
classifier.case_confidence_path = cancer_likelihood_json

# Run the classifier on the input images
classifier.process()


def run_classifier_cli():
parser = argparse.ArgumentParser("CLI to run the PICAI baseline classifier")
parser.add_argument("input_t2", type=str, help="input T2 weighted prostate MR image (MHA)")
parser.add_argument("input_adc", type=str, help="input ADC prostate MR image (MHA")
parser.add_argument("input_hbv", type=str, help="input HBV prostate MR image (MHA)")
parser.add_argument("output_cancer_likelihood_json", type=str, help="output JSON file with PICAI baseline prostate cancer likelihood (JSON)")
parser.add_argument("output_cancer_detection_heatmap", type=str, help="output heatmap indicating prostate cancer likelihood (MHA)")
args = parser.parse_args()
run_classifier(
t2=Path(args.input_t2),
adc=Path(args.input_adc),
hbv=Path(args.input_hbv),
cancer_likelihood_json=Path(args.output_cancer_likelihood_json),
cancer_detection_heatmap=Path(args.output_cancer_detection_heatmap),
)


if __name__ == "__main__":
run_classifier_cli()

0 comments on commit f3c73f3

Please sign in to comment.