Skip to content

Bone app #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/apps/ct_bone_seg_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
import sys

_current_dir = os.path.abspath(os.path.dirname(__file__))
if sys.path and os.path.abspath(sys.path[0]) != _current_dir:
sys.path.insert(0, _current_dir)

del _current_dir
4 changes: 4 additions & 0 deletions examples/apps/ct_bone_seg_app/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from app import App

if __name__ == "__main__":
App(do_run=True)
36 changes: 36 additions & 0 deletions examples/apps/ct_bone_seg_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2022 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ct_bone_operator import CtBoneOperator
from dicom_to_mhd import DicomToMhd

from monai.deploy.core import Application, resource


@resource(cpu=1, gpu=1)
class App(Application):
"""This is a CT Bone application."""

name = "ct_bone_app"
description = "CT Bone segmentation app."
version = "0.1.0"

def compose(self):
"""This application has two operators."""

dcm_to_mhd = DicomToMhd()
bone_op = CtBoneOperator()

self.add_flow(dcm_to_mhd, bone_op)


if __name__ == "__main__":
App(do_run=True)
50 changes: 50 additions & 0 deletions examples/apps/ct_bone_seg_app/ct_bone_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2022 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import monai.deploy.core as md
from monai.deploy.core import DataPath, ExecutionContext, InputContext, IOType, Operator, OutputContext


@md.input("image", DataPath, IOType.DISK)
@md.output("image", DataPath, IOType.DISK)
@md.env(pip_packages=["patchelf", "clcat"])
class CtBoneOperator(Operator):
"""This Operator implements CT Bone segmentation"""

def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):

input_path = op_input.get().path
inputfile = ""

if input_path.is_dir():
inputfile = os.path.join(input_path, "intermediate_mhd_data.mhd")
elif input_path.is_file():
inputfile = str(input_path)
else:
raise IOError("Input path invalid from input context")

output_path = op_output.get().path
output_path.mkdir(parents=True, exist_ok=True)
outputfile = os.path.join(output_path, "bone_mask.mhd")

import clcat.ct_algos as cact

status = cact.ct_bone(inputfile, outputfile)

if input_path.is_dir():
os.remove(inputfile)
inputrawfile = os.path.join(input_path, "intermediate_mhd_data.raw")
os.remove(inputrawfile)

if status != 0:
raise Exception("Bone segmentation failed")
55 changes: 55 additions & 0 deletions examples/apps/ct_bone_seg_app/dicom_to_mhd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2022 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import monai.deploy.core as md
from monai.deploy.core import DataPath, ExecutionContext, InputContext, IOType, Operator, OutputContext


@md.input("image", DataPath, IOType.DISK)
@md.output("image", DataPath, IOType.DISK)
@md.env(pip_packages=["SimpleITK==1.2.4"])
class DicomToMhd(Operator):
"""
If input is DICOM: This operator converts a dicom image to a mhd image
If input is MHD: This operator sets the output path to input path
"""

def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):

input_path = op_input.get().path
if input_path.is_dir():
import SimpleITK as SITK

current_file_dir = os.path.abspath(os.path.dirname(__file__))
op_output.set(DataPath(current_file_dir))
output_path = op_output.get().path

output_file_path = os.path.join(output_path, "intermediate_mhd_data.mhd")

reader = SITK.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(str(input_path))
reader.SetFileNames(dicom_names)

fixed = reader.Execute()
SITK.WriteImage(fixed, output_file_path)
else:
if os.path.isfile(input_path):
extension = os.path.splitext(input_path)[1]
if extension == ".mhd":
# Input path is a MHD file
# Setting output folder as input folder
op_output.set(DataPath(input_path))
else:
raise IOError("Unsupported extension")
else:
raise IOError("Invalid input path")