Skip to content

Commit 3830027

Browse files
Merge pull request #89 from ChristianHinge/dev/visualizations
dev/visualizations
2 parents a31fc06 + 93133ea commit 3830027

File tree

15 files changed

+1266
-224
lines changed

15 files changed

+1266
-224
lines changed

poetry.lock

Lines changed: 424 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zerodose"
3-
version = "0.0.5"
3+
version = "0.0.6"
44
description = "Zerodose"
55
authors = ["Christian Hinge <christian.hinge@regionh.dk>"]
66
license = "MIT"
@@ -21,6 +21,11 @@ click = ">=8.0.1"
2121
torch = ">=1.0.0"
2222
torchio = "^0.18.0"
2323
niftyreg = "^1.5.70rc1"
24+
matplotlib = {version = "^3.7.1", optional = true}
25+
opencv-python = {version = "^4.7.0.72", optional = true}
26+
27+
[tool.poetry.extras]
28+
plotting = ["matplotlib","opencv-python"]
2429

2530
[tool.poetry.dev-dependencies]
2631
Pygments = ">=2.10.0"

src/zerodose/__main__.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from zerodose.pipeline import create_abnormality_maps
88
from zerodose.pipeline import normalize_to_pet
9-
from zerodose.pipeline import run_full
9+
from zerodose.pipeline import run_with_registration
1010
from zerodose.pipeline import synthesize_baselines
1111
from zerodose.utils import _create_output_fname
1212

@@ -308,58 +308,61 @@ def norm(
308308
help="Print verbose output.",
309309
)
310310

311+
no_reg_pet_to_mr_option = click.option(
312+
"--no-pet-rigid",
313+
"no_reg_pet_to_mr",
314+
is_flag=True,
315+
default=False,
316+
help="Print verbose output.",
317+
)
318+
311319

312320
@mri_option_single
313321
@mask_option_single
314322
@pet_option_single
315323
@sbpet_output_option_single
316324
@abn_output_option_single
317325
@img_output_option_single
318-
@no_registration_option
319-
@no_abnormality_option
320-
@no_normalization_option
326+
@no_reg_pet_to_mr_option
321327
@no_image_option
322328
@verbose_option
323329
@device_option
324330
@outputspace_option
325331
@main.command()
326-
def run(
332+
def pipeline(
327333
mri_fname,
328334
mask_fname,
329335
pet_fname,
330336
out_sbpet,
331337
out_abn,
332338
out_img,
333-
no_registration,
334-
no_abnormality,
335-
no_normalization,
339+
no_reg_pet_to_mr,
336340
no_image,
337341
verbose,
338342
device,
339343
outputspace,
340344
):
341345
"""Run full pipeline."""
342-
do_registration = not no_registration
343-
do_abnormality = not no_abnormality
344-
do_normalization = not no_normalization
346+
reg_pet_to_mr = not no_reg_pet_to_mr
345347
do_image = not no_image
346348

347349
if out_sbpet is None:
348350
out_sbpet = _create_output_fname(pet_fname, suffix="_sb")
349351
if out_abn is None:
350352
out_abn = _create_output_fname(pet_fname, suffix="_abn")
353+
if out_img is None and do_image:
354+
out_img = _create_output_fname(
355+
pet_fname, suffix="_abn_figure", file_type=".png"
356+
)
351357

352-
run_full(
358+
run_with_registration(
353359
mri_fname=mri_fname,
354360
mask_fname=mask_fname,
355361
out_sbpet=out_sbpet,
356362
pet_fname=pet_fname,
357363
out_abn=out_abn,
358364
out_img=out_img,
359-
do_registration=do_registration,
360-
do_abnormality=do_abnormality,
361-
do_normalization=do_normalization,
362-
do_image=do_image,
365+
reg_pet_to_mri=reg_pet_to_mr,
363366
verbose=verbose,
364367
device=device,
365368
outputspace=outputspace,

src/zerodose/dataset.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,28 @@
1212
class SubjectDataset(tio.data.SubjectsDataset):
1313
"""Dataset class for the ZeroDose project."""
1414

15-
def __init__(self, mri_fnames, mask_fnames, out_fnames):
15+
def __init__(self, mri_fnames, mask_fnames, pet_fnames=None):
1616
"""Initialize the dataset."""
1717
transforms = self._get_augmentation_transform_val()
1818

1919
subjects = [
20-
self._make_subject_predict(mr_f, ma_f, ou_f)
21-
for mr_f, ma_f, ou_f in zip( # noqa
20+
self._make_subject_predict(mr_f, ma_f)
21+
for mr_f, ma_f in zip( # noqa
2222
mri_fnames,
2323
mask_fnames,
24-
out_fnames,
2524
)
2625
]
27-
super().__init__(subjects, transforms)
28-
29-
def _make_subject_dict(self, mr_path, mask_path) -> dict:
30-
subject_dict: Dict[Any, Any] = {}
31-
mri = mr_path
32-
mask = mask_path
3326

34-
subject_dict["mr"] = tio.ScalarImage(mri)
35-
subject_dict["mask"] = tio.LabelMap(mask)
27+
if pet_fnames is not None:
28+
for sub, pet_fname in zip(subjects, pet_fnames): # noqa
29+
sub.add_image(tio.ScalarImage(pet_fname), "pet")
3630

37-
return subject_dict
31+
super().__init__(subjects, transforms)
3832

39-
def _make_subject_predict(self, mr_path, mask_path, out_fname) -> tio.Subject:
40-
subject_dict = self._make_subject_dict(mr_path, mask_path)
41-
subject_dict["out_fname"] = out_fname
33+
def _make_subject_predict(self, mr_path, mask_path) -> tio.Subject:
34+
subject_dict: Dict[Any, Any] = {}
35+
subject_dict["mr"] = tio.ScalarImage(mr_path)
36+
subject_dict["mask"] = tio.LabelMap(mask_path)
4237
return tio.Subject(subject_dict)
4338

4439
def _get_augmentation_transform_val(self) -> tio.Compose:

src/zerodose/inference.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ def synthesize(mr, model, device="cuda:0"):
6161
)
6262

6363
return sbpet_tensor
64+
65+
66+
def get_inference_params():
67+
"""Returns the default parameters for inference stitching."""
68+
stride = 2
69+
params = {
70+
"patch_size": (32, 192, 192),
71+
"patch_overlap": (32 - stride, 192 - stride, 192 - stride),
72+
"batch_size": 1,
73+
"sd_weight": 5,
74+
}
75+
return params

src/zerodose/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def forward(self, mrs: torch.Tensor) -> torch.Tensor:
2727
"""Forward pass through the model."""
2828
return self.generator(mrs)
2929

30+
@staticmethod
31+
def get_default():
32+
"""Get the default model."""
33+
return utils.get_model()
34+
3035

3136
class AbnormalityMap(nn.Module):
3237
"""Abnormality map generator."""
@@ -38,6 +43,11 @@ def __init__(self, sigma_smooth=3) -> None:
3843
channels=1, kernel_size=5 * sigma_smooth, sigma=sigma_smooth, dim=3
3944
)
4045

46+
@staticmethod
47+
def get_default():
48+
"""Get the default model."""
49+
return AbnormalityMap(sigma_smooth=3)
50+
4151
def forward(
4252
self, pet: torch.Tensor, sbpet: torch.Tensor, mask: torch.Tensor
4353
) -> torch.Tensor:
@@ -88,3 +98,8 @@ def _scale_sbpet(self, pet, sbpet, mask):
8898
sbpet[~mask] = pet[~mask]
8999

90100
return sbpet
101+
102+
@staticmethod
103+
def get_default():
104+
"""Get the default model."""
105+
return QuantileNormalization(quantile=0.97, sigma_normalization=3)

src/zerodose/pipeline/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Pipeline for ZeroDose project."""
22
from .abnormality import create_abnormality_maps
3-
from .full import run_full
3+
from .full import run_with_registration
4+
from .main import run
45
from .niftyreg import from_mni
56
from .niftyreg import to_mni
67
from .normalization import normalize_to_pet
@@ -9,7 +10,8 @@
910

1011
__all__ = [
1112
"create_abnormality_maps",
12-
"run_full",
13+
"run",
14+
"run_with_registration",
1315
"from_mni",
1416
"to_mni",
1517
"normalize_to_pet",

0 commit comments

Comments
 (0)