Skip to content

Commit

Permalink
Added command line to generate cranioplasty implant
Browse files Browse the repository at this point in the history
  • Loading branch information
paulojamorim committed Sep 23, 2024
1 parent e4dda58 commit 0e2c34e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ def parse_command_line():
help="Debug navigated TMS E-field computation",
)

parser.add_argument("--cranioplasty", help="Creates an AI-based cranioplasty implant.")

args = parser.parse_args()
return args

Expand All @@ -359,6 +361,7 @@ def use_cmd_optargs(args):
Publisher.sendMessage("Save project", filepath=os.path.abspath(args.save))
exit(0)

check_for_segmentation(args)
check_for_export(args)

return True
Expand Down Expand Up @@ -408,6 +411,11 @@ def use_cmd_optargs(args):
return False


def check_for_segmentation(args):
if args.cranioplasty:
Publisher.sendMessage("Create implant for cranioplasty")


def sanitize(text):
text = str(text).strip().replace(" ", "_")
return re.sub(r"(?u)[^-\w.]", "", text)
Expand Down
4 changes: 4 additions & 0 deletions invesalius/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from invesalius import inv_paths, plugins
from invesalius.i18n import tr as _
from invesalius.pubsub import pub as Publisher
from invesalius.segmentation.deep_learning import segment

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -124,6 +125,9 @@ def __bind_events(self) -> None:

Publisher.subscribe(self.LoadProject, "Load project data")

# for call cranioplasty implant by command line
Publisher.subscribe(segment.run_cranioplasty_implant, "Create implant for cranioplasty")

def SetBitmapSpacing(self, spacing: Tuple[float, float, float]) -> None:
proj = prj.Project()
proj.spacing = spacing
Expand Down
43 changes: 43 additions & 0 deletions invesalius/segmentation/deep_learning/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,55 @@
from invesalius.data import imagedata_utils
from invesalius.data.converters import to_vtk
from invesalius.net.utils import download_url_to_file
from invesalius.pubsub import pub as Publisher
from invesalius.utils import new_name_by_pattern

from . import utils

SIZE = 48


def run_cranioplasty_implant():
"""
This function was created to allow the creation of implants for
cranioplasty to be called by command line.
"""
image = slc.Slice().matrix
backend = "pytorch"
device_id = list(utils.get_torch_devices().values())[0]
apply_wwwl = False
create_new_mask = True
use_gpu = True
resize_by_spacing = True
window_width = slc.Slice().window_width
window_level = slc.Slice().window_level
overlap = 50
patch_size = 480
method = 0 # binary

seg = ImplantCTSegmentProcess

ps = seg(
image,
create_new_mask,
backend,
device_id,
use_gpu,
overlap,
apply_wwwl,
window_width,
window_level,
method=method,
patch_size=patch_size,
resize_by_spacing=True,
image_spacing=slc.Slice().spacing,
)
ps._run_segmentation()
ps.apply_segment_threshold(0.75)
slc.Slice().discard_all_buffers()
Publisher.sendMessage("Reload actual slice")


patch_type = Tuple[Tuple[int, int], Tuple[int, int], Tuple[int, int]]


Expand Down
22 changes: 22 additions & 0 deletions invesalius/segmentation/deep_learning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
import sys


def get_torch_devices():
TORCH_DEVICES = {}

try:
import torch

HAS_TORCH = True
except ImportError:
HAS_TORCH = False

if HAS_TORCH:
TORCH_DEVICES = {}
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
name = torch.cuda.get_device_name()
device_id = f"cuda:{i}"
TORCH_DEVICES[name] = device_id
TORCH_DEVICES["CPU"] = "cpu"

return TORCH_DEVICES


def prepare_plaidml():
# Linux if installed plaidml with pip3 install --user
if sys.platform.startswith("linux"):
Expand Down

0 comments on commit 0e2c34e

Please sign in to comment.