Skip to content

Commit a31fc06

Browse files
Merge pull request #85 from ChristianHinge/dev/april_03
Reworked workflow version 0.0.5
2 parents d4a0598 + 5be0e63 commit a31fc06

17 files changed

+885
-404
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zerodose"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
description = "Zerodose"
55
authors = ["Christian Hinge <christian.hinge@regionh.dk>"]
66
license = "MIT"

src/zerodose/__main__.py

Lines changed: 204 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""Command-line interface."""
2-
import re
32
from typing import Sequence
43
from typing import Union
54

65
import click
76

8-
from zerodose.run_abnormality import create_abnormality_maps
9-
from zerodose.run_synthesize import synthesize_baselines
7+
from zerodose.pipeline import create_abnormality_maps
8+
from zerodose.pipeline import normalize_to_pet
9+
from zerodose.pipeline import run_full
10+
from zerodose.pipeline import synthesize_baselines
11+
from zerodose.utils import _create_output_fname
1012

1113

1214
@click.group()
@@ -79,50 +81,48 @@ def main() -> None:
7981
Useful if the input images already are in MNI space""",
8082
)
8183

84+
outputspace_option = click.option(
85+
"--space",
86+
"outputspace",
87+
type=click.Choice(
88+
[
89+
"mr",
90+
"pet",
91+
]
92+
),
93+
default="mr",
94+
help="Which space to ",
95+
)
96+
8297

8398
@main.command()
8499
@mri_option
85100
@mask_option
86101
@sbpet_output_option
87102
@verbose_option
88103
@device_option
89-
@no_registration_option
90104
def syn(
91105
mri_fnames: Sequence[str],
92106
mask_fnames: Sequence[str],
93107
out_fnames: Union[Sequence[str], None] = None,
94108
verbose: bool = True,
95109
device: str = "cuda:0",
96-
no_registration: bool = False,
97110
) -> None:
98111
"""Synthesize baseline PET images."""
99112
if out_fnames is None or len(out_fnames) == 0:
100113
out_fnames = [
101-
_create_output_fname(mri_fname, suffix="_sb") for mri_fname in mri_fnames
114+
_create_output_fname(mri_fname, suffix="_sbraw") for mri_fname in mri_fnames
102115
]
103116

104-
do_registration = not no_registration
105117
synthesize_baselines(
106118
mri_fnames,
107119
mask_fnames,
108120
out_fnames,
109121
verbose=verbose,
110122
device=device,
111-
do_registration=do_registration,
112123
)
113124

114125

115-
def _create_output_fname(mri_fname, suffix="_sb", file_type=".nii.gz"):
116-
"""Create output filename from input filename."""
117-
out_fname = mri_fname
118-
if out_fname.endswith(".nii.gz"):
119-
out_fname = re.sub(".nii.gz$", "", out_fname)
120-
if out_fname.endswith(".nii"):
121-
out_fname = re.sub(".nii$", "", out_fname)
122-
out_fname += suffix + file_type
123-
return out_fname
124-
125-
126126
pet_option = click.option(
127127
"-p",
128128
"--pet",
@@ -149,6 +149,14 @@ def _create_output_fname(mri_fname, suffix="_sb", file_type=".nii.gz"):
149149
multiple=True,
150150
)
151151

152+
no_resample_mask_option = click.option(
153+
"--no-resample-mask",
154+
"no_resample_mask",
155+
is_flag=True,
156+
default=False,
157+
help="Print verbose output.",
158+
)
159+
152160

153161
@pet_option
154162
@sbpet_option
@@ -179,3 +187,180 @@ def abn(
179187
verbose=verbose,
180188
device=device,
181189
)
190+
191+
192+
no_resample_sbpet_option = click.option(
193+
"--no-resample-sbpet",
194+
"no_resample_sbpet",
195+
is_flag=True,
196+
default=False,
197+
help="Print verbose output.",
198+
)
199+
200+
201+
@pet_option
202+
@sbpet_option
203+
@mask_option
204+
@abn_output_option
205+
@verbose_option
206+
@device_option
207+
@main.command()
208+
def norm(
209+
pet_fnames: Sequence[str],
210+
sbpet_fnames: Sequence[str],
211+
mask_fnames: Sequence[str],
212+
out_fnames: Union[Sequence[str], None] = None,
213+
verbose: bool = False,
214+
device: str = "cuda:0",
215+
):
216+
"""Normalize sbPET images to PET images."""
217+
if out_fnames is None or len(out_fnames) == 0:
218+
out_fnames = [
219+
_create_output_fname(pet_name, suffix="_sb") for pet_name in pet_fnames
220+
]
221+
222+
normalize_to_pet(
223+
pet_fnames=pet_fnames,
224+
sbpet_fnames=sbpet_fnames,
225+
mask_fnames=mask_fnames,
226+
out_fnames=out_fnames,
227+
verbose=verbose,
228+
device=device,
229+
)
230+
231+
232+
no_image_option = click.option(
233+
"--no-image",
234+
"no_image",
235+
is_flag=True,
236+
default=False,
237+
help="Print verbose output.",
238+
)
239+
240+
mri_option_single = click.option(
241+
"-i",
242+
"--mri",
243+
"mri_fname",
244+
type=click.Path(exists=True),
245+
required=True,
246+
)
247+
mask_option_single = click.option(
248+
"-m",
249+
"--mask",
250+
"mask_fname",
251+
type=click.Path(exists=True),
252+
required=True,
253+
)
254+
255+
pet_option_single = click.option(
256+
"-p",
257+
"--pet",
258+
"pet_fname",
259+
type=click.Path(exists=True),
260+
required=False,
261+
)
262+
263+
sbpet_output_option_single = click.option(
264+
"-os",
265+
"--out-sbpet",
266+
"out_sbpet",
267+
type=click.Path(),
268+
required=False,
269+
)
270+
271+
abn_output_option_single = click.option(
272+
"-oa",
273+
"--out-abn",
274+
"out_abn",
275+
type=click.Path(),
276+
required=False,
277+
)
278+
279+
img_output_option_single = click.option(
280+
"-oi",
281+
"--out-img",
282+
"out_img",
283+
type=click.Path(),
284+
required=False,
285+
)
286+
287+
no_abnormality_option = click.option(
288+
"--no-abn",
289+
"no_abnormality",
290+
is_flag=True,
291+
default=False,
292+
help="Print verbose output.",
293+
)
294+
295+
no_normalization_option = click.option(
296+
"--no-norm",
297+
"no_normalization",
298+
is_flag=True,
299+
default=False,
300+
help="Print verbose output.",
301+
)
302+
303+
no_image_option = click.option(
304+
"--no-img",
305+
"no_image",
306+
is_flag=True,
307+
default=False,
308+
help="Print verbose output.",
309+
)
310+
311+
312+
@mri_option_single
313+
@mask_option_single
314+
@pet_option_single
315+
@sbpet_output_option_single
316+
@abn_output_option_single
317+
@img_output_option_single
318+
@no_registration_option
319+
@no_abnormality_option
320+
@no_normalization_option
321+
@no_image_option
322+
@verbose_option
323+
@device_option
324+
@outputspace_option
325+
@main.command()
326+
def run(
327+
mri_fname,
328+
mask_fname,
329+
pet_fname,
330+
out_sbpet,
331+
out_abn,
332+
out_img,
333+
no_registration,
334+
no_abnormality,
335+
no_normalization,
336+
no_image,
337+
verbose,
338+
device,
339+
outputspace,
340+
):
341+
"""Run full pipeline."""
342+
do_registration = not no_registration
343+
do_abnormality = not no_abnormality
344+
do_normalization = not no_normalization
345+
do_image = not no_image
346+
347+
if out_sbpet is None:
348+
out_sbpet = _create_output_fname(pet_fname, suffix="_sb")
349+
if out_abn is None:
350+
out_abn = _create_output_fname(pet_fname, suffix="_abn")
351+
352+
run_full(
353+
mri_fname=mri_fname,
354+
mask_fname=mask_fname,
355+
out_sbpet=out_sbpet,
356+
pet_fname=pet_fname,
357+
out_abn=out_abn,
358+
out_img=out_img,
359+
do_registration=do_registration,
360+
do_abnormality=do_abnormality,
361+
do_normalization=do_normalization,
362+
do_image=do_image,
363+
verbose=verbose,
364+
device=device,
365+
outputspace=outputspace,
366+
)

src/zerodose/dataset.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
"""Dataset class for the ZeroDose project."""
22
from typing import Any
33
from typing import Dict
4-
from typing import List
54

65
import torchio as tio
76

8-
from zerodose import utils
9-
from zerodose.niftyreg_wrapper import NiftyRegistration
107
from zerodose.processing import Binarize
11-
from zerodose.processing import PadAndCropToMNI
8+
from zerodose.processing import PadAndCropMNI
129
from zerodose.processing import ToFloat32
1310

1411

1512
class SubjectDataset(tio.data.SubjectsDataset):
1613
"""Dataset class for the ZeroDose project."""
1714

18-
def __init__(self, mri_fnames, mask_fnames, out_fnames, do_registration=True):
15+
def __init__(self, mri_fnames, mask_fnames, out_fnames):
1916
"""Initialize the dataset."""
20-
transforms = self._get_augmentation_transform_val(
21-
do_registration=do_registration
22-
)
17+
transforms = self._get_augmentation_transform_val()
18+
2319
subjects = [
2420
self._make_subject_predict(mr_f, ma_f, ou_f)
2521
for mr_f, ma_f, ou_f in zip( # noqa
@@ -28,7 +24,6 @@ def __init__(self, mri_fnames, mask_fnames, out_fnames, do_registration=True):
2824
out_fnames,
2925
)
3026
]
31-
3227
super().__init__(subjects, transforms)
3328

3429
def _make_subject_dict(self, mr_path, mask_path) -> dict:
@@ -44,23 +39,14 @@ def _make_subject_dict(self, mr_path, mask_path) -> dict:
4439
def _make_subject_predict(self, mr_path, mask_path, out_fname) -> tio.Subject:
4540
subject_dict = self._make_subject_dict(mr_path, mask_path)
4641
subject_dict["out_fname"] = out_fname
47-
4842
return tio.Subject(subject_dict)
4943

50-
def _get_augmentation_transform_val(self, do_registration=True) -> tio.Compose:
51-
augmentations: List[tio.Transform] = []
52-
53-
if do_registration:
54-
ref = utils.get_mni_template()
55-
augmentations.append(NiftyRegistration(floating_image="mr", ref=ref))
56-
57-
augmentations.extend(
58-
[
59-
Binarize(include=["mask"]),
60-
tio.transforms.ZNormalization(include=["mr"], masking_method="mask"),
61-
PadAndCropToMNI(include=["mr", "mask"]),
62-
ToFloat32(include=["mr"]),
63-
]
64-
)
44+
def _get_augmentation_transform_val(self) -> tio.Compose:
45+
augmentations = [
46+
Binarize(include=["mask"]),
47+
tio.transforms.ZNormalization(include=["mr"], masking_method="mask"),
48+
PadAndCropMNI(),
49+
ToFloat32(include=["mr"]),
50+
]
6551

6652
return tio.Compose(augmentations)

0 commit comments

Comments
 (0)