petpal 0.5.6__py3-none-any.whl → 0.5.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- petpal/cli/cli_pib_processing.py +2 -3
- petpal/cli/cli_preproc.py +64 -33
- petpal/cli/cli_vat_processing.py +3 -2
- petpal/pipelines/pipelines.py +0 -3
- petpal/pipelines/preproc_steps.py +8 -7
- petpal/preproc/__init__.py +2 -0
- petpal/preproc/decay_correction.py +11 -10
- petpal/preproc/image_operations_4d.py +13 -177
- petpal/preproc/motion_corr.py +31 -112
- petpal/preproc/motion_target.py +90 -0
- petpal/preproc/regional_tac_extraction.py +3 -3
- petpal/preproc/register.py +4 -11
- petpal/preproc/segmentation_tools.py +5 -5
- petpal/preproc/standard_uptake_value.py +159 -0
- petpal/utils/scan_timing.py +66 -19
- petpal/utils/stats.py +21 -0
- petpal/utils/useful_functions.py +106 -5
- {petpal-0.5.6.dist-info → petpal-0.5.8.dist-info}/METADATA +1 -1
- {petpal-0.5.6.dist-info → petpal-0.5.8.dist-info}/RECORD +22 -20
- {petpal-0.5.6.dist-info → petpal-0.5.8.dist-info}/WHEEL +0 -0
- {petpal-0.5.6.dist-info → petpal-0.5.8.dist-info}/entry_points.txt +0 -0
- {petpal-0.5.6.dist-info → petpal-0.5.8.dist-info}/licenses/LICENSE +0 -0
petpal/preproc/motion_corr.py
CHANGED
|
@@ -7,20 +7,21 @@ registration.
|
|
|
7
7
|
import ants
|
|
8
8
|
import numpy as np
|
|
9
9
|
|
|
10
|
+
from petpal.utils.useful_functions import gen_nd_image_based_on_image_list
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
|
|
13
|
+
from .motion_target import determine_motion_target
|
|
12
14
|
from ..utils import image_io
|
|
13
15
|
from ..utils.scan_timing import ScanTimingInfo, get_window_index_pairs_for_image
|
|
14
16
|
from ..utils.useful_functions import weighted_series_sum_over_window_indecies
|
|
15
17
|
from ..utils.image_io import get_half_life_from_nifti
|
|
16
18
|
|
|
17
19
|
|
|
18
|
-
def motion_corr(
|
|
20
|
+
def motion_corr(input_image_path: str,
|
|
19
21
|
motion_target_option: str | tuple,
|
|
20
22
|
out_image_path: str,
|
|
21
23
|
verbose: bool,
|
|
22
24
|
type_of_transform: str = 'DenseRigid',
|
|
23
|
-
half_life: float = None,
|
|
24
25
|
**kwargs) -> tuple[np.ndarray, list[str], list[float]]:
|
|
25
26
|
"""
|
|
26
27
|
Correct PET image series for inter-frame motion. Runs rigid motion
|
|
@@ -28,7 +29,7 @@ def motion_corr(input_image_4d_path: str,
|
|
|
28
29
|
inputs.
|
|
29
30
|
|
|
30
31
|
Args:
|
|
31
|
-
|
|
32
|
+
input_image_path (str): Path to a .nii or .nii.gz file containing a 4D
|
|
32
33
|
PET image to be motion corrected.
|
|
33
34
|
motion_target_option (str | tuple): Target image for computing
|
|
34
35
|
transformation. See :meth:`determine_motion_target`.
|
|
@@ -40,8 +41,6 @@ def motion_corr(input_image_4d_path: str,
|
|
|
40
41
|
'Translation'. Any transformation type that uses >6 degrees of
|
|
41
42
|
freedom is not recommended, use with caution. See
|
|
42
43
|
:py:func:`ants.registration`.
|
|
43
|
-
half_life (float): Half life of the PET radioisotope in seconds. Used
|
|
44
|
-
for certain settings of ``motion_target_option``.
|
|
45
44
|
kwargs (keyword arguments): Additional arguments passed to
|
|
46
45
|
:py:func:`ants.motion_correction`.
|
|
47
46
|
|
|
@@ -53,10 +52,9 @@ def motion_corr(input_image_4d_path: str,
|
|
|
53
52
|
pet_moco_fd (list[float]): List of framewise displacement measure
|
|
54
53
|
corresponding to each frame transform.
|
|
55
54
|
"""
|
|
56
|
-
pet_ants = ants.image_read(
|
|
55
|
+
pet_ants = ants.image_read(input_image_path)
|
|
57
56
|
motion_target_image_path = determine_motion_target(motion_target_option=motion_target_option,
|
|
58
|
-
|
|
59
|
-
half_life=half_life)
|
|
57
|
+
input_image_path=input_image_path)
|
|
60
58
|
|
|
61
59
|
motion_target_image = ants.image_read(motion_target_image_path)
|
|
62
60
|
pet_moco_ants_dict = ants.motion_correction(image=pet_ants,
|
|
@@ -71,21 +69,20 @@ def motion_corr(input_image_4d_path: str,
|
|
|
71
69
|
pet_moco_fd = pet_moco_ants_dict['FD']
|
|
72
70
|
pet_moco_np = pet_moco_ants.numpy()
|
|
73
71
|
ants.image_write(image=pet_moco_ants,filename=out_image_path)
|
|
74
|
-
image_io.safe_copy_meta(input_image_path=
|
|
72
|
+
image_io.safe_copy_meta(input_image_path=input_image_path, out_image_path=out_image_path)
|
|
75
73
|
|
|
76
74
|
if verbose:
|
|
77
75
|
print(f"(ImageOps4d): motion corrected image saved to {out_image_path}")
|
|
78
76
|
return pet_moco_np, pet_moco_params, pet_moco_fd
|
|
79
77
|
|
|
80
78
|
|
|
81
|
-
def motion_corr_frame_list(
|
|
79
|
+
def motion_corr_frame_list(input_image_path: str,
|
|
82
80
|
motion_target_option: str | tuple,
|
|
83
81
|
out_image_path: str,
|
|
84
82
|
verbose: bool,
|
|
85
83
|
frames_list: list = None,
|
|
86
84
|
type_of_transform: str = 'Affine',
|
|
87
85
|
transform_metric: str = 'mattes',
|
|
88
|
-
half_life: float = None,
|
|
89
86
|
**kwargs):
|
|
90
87
|
r"""
|
|
91
88
|
Perform per-frame motion correction on a 4D PET image.
|
|
@@ -94,7 +91,7 @@ def motion_corr_frame_list(input_image_4d_path: str,
|
|
|
94
91
|
motion target. Only the frames in ``frames_list`` are motion corrected, all else are kept as is.
|
|
95
92
|
|
|
96
93
|
Args:
|
|
97
|
-
|
|
94
|
+
input_image_path (str): Path to the input 4D PET image file.
|
|
98
95
|
motion_target_option (str | tuple): Option to determine the motion target. This can
|
|
99
96
|
be a path to a specific image file, a tuple of frame indices to generate a target, or
|
|
100
97
|
specific options recognized by :func:`determine_motion_target`.
|
|
@@ -106,8 +103,6 @@ def motion_corr_frame_list(input_image_4d_path: str,
|
|
|
106
103
|
is 'Affine'.
|
|
107
104
|
transform_metric (str, optional): Metric to use for the transformation. Default is
|
|
108
105
|
'mattes'.
|
|
109
|
-
half_life (float, optional): Half-life value used by `determine_motion_target` if
|
|
110
|
-
applicable. Default is None.
|
|
111
106
|
**kwargs: Additional arguments passed to the `ants.registration` method.
|
|
112
107
|
|
|
113
108
|
Returns:
|
|
@@ -119,7 +114,7 @@ def motion_corr_frame_list(input_image_4d_path: str,
|
|
|
119
114
|
|
|
120
115
|
from petpal.preproc.motion_corr import motion_corr_frame_list
|
|
121
116
|
|
|
122
|
-
motion_corr_frame_list(
|
|
117
|
+
motion_corr_frame_list(input_image_path='/path/to/image.nii.gz',
|
|
123
118
|
motion_target_option='/path/to/target_image.nii.gz',
|
|
124
119
|
out_image_path='/path/to/output_motion_corrected.nii.gz',
|
|
125
120
|
verbose=True)
|
|
@@ -134,11 +129,10 @@ def motion_corr_frame_list(input_image_4d_path: str,
|
|
|
134
129
|
path.
|
|
135
130
|
|
|
136
131
|
"""
|
|
137
|
-
input_image = ants.image_read(
|
|
132
|
+
input_image = ants.image_read(input_image_path)
|
|
138
133
|
|
|
139
134
|
motion_target_path = determine_motion_target(motion_target_option=motion_target_option,
|
|
140
|
-
|
|
141
|
-
half_life=half_life)
|
|
135
|
+
input_image_path=input_image_path)
|
|
142
136
|
motion_target = ants.image_read(motion_target_path)
|
|
143
137
|
|
|
144
138
|
frames_to_correct = np.zeros(input_image.shape[-1], dtype=bool)
|
|
@@ -182,15 +176,14 @@ def motion_corr_frame_list(input_image_4d_path: str,
|
|
|
182
176
|
print(f"(ImageOps4d): motion corrected image saved to {out_image_path}")
|
|
183
177
|
|
|
184
178
|
|
|
185
|
-
def motion_corr_frame_list_to_t1(
|
|
179
|
+
def motion_corr_frame_list_to_t1(input_image_path: str,
|
|
186
180
|
t1_image_path: str,
|
|
187
181
|
motion_target_option: str | tuple,
|
|
188
182
|
out_image_path: str,
|
|
189
183
|
verbose: bool,
|
|
190
184
|
frames_list: list = None,
|
|
191
185
|
type_of_transform: str = 'AffineFast',
|
|
192
|
-
transform_metric: str = "mattes"
|
|
193
|
-
half_life: float = None):
|
|
186
|
+
transform_metric: str = "mattes"):
|
|
194
187
|
r"""
|
|
195
188
|
Perform motion correction of a 4D PET image to a T1 anatomical image.
|
|
196
189
|
|
|
@@ -203,7 +196,7 @@ def motion_corr_frame_list_to_t1(input_image_4d_path: str,
|
|
|
203
196
|
transformed to the motion-target in T1-space.
|
|
204
197
|
|
|
205
198
|
Args:
|
|
206
|
-
|
|
199
|
+
input_image_path (str): Path to the 4D PET image to be corrected.
|
|
207
200
|
t1_image_path (str): Path to the 3D T1 anatomical image.
|
|
208
201
|
motion_target_option (str | tuple): Option for selecting the motion target image.
|
|
209
202
|
Can be a path to a file or a tuple range. If None, the average of the PET timeseries
|
|
@@ -215,8 +208,6 @@ def motion_corr_frame_list_to_t1(input_image_4d_path: str,
|
|
|
215
208
|
type_of_transform (str): Type of transformation used in registration. Default is
|
|
216
209
|
'AffineFast'.
|
|
217
210
|
transform_metric (str): Metric for transformation optimization. Default is 'mattes'.
|
|
218
|
-
half_life (float, optional): Half-life of the PET radioisotope. Used if a calculation
|
|
219
|
-
is required for the motion target.
|
|
220
211
|
|
|
221
212
|
Returns:
|
|
222
213
|
None
|
|
@@ -230,7 +221,7 @@ def motion_corr_frame_list_to_t1(input_image_4d_path: str,
|
|
|
230
221
|
.. code-block:: python
|
|
231
222
|
|
|
232
223
|
|
|
233
|
-
motion_corr_frame_list_to_t1(
|
|
224
|
+
motion_corr_frame_list_to_t1(input_image_path='pet_timeseries.nii.gz',
|
|
234
225
|
t1_image_path='t1_image.nii.gz',
|
|
235
226
|
motion_target_option='mean_image',
|
|
236
227
|
out_image_path='pet_corrected.nii.gz',
|
|
@@ -238,12 +229,11 @@ def motion_corr_frame_list_to_t1(input_image_4d_path: str,
|
|
|
238
229
|
|
|
239
230
|
"""
|
|
240
231
|
|
|
241
|
-
input_image = ants.image_read(
|
|
232
|
+
input_image = ants.image_read(input_image_path)
|
|
242
233
|
t1_image = ants.image_read(t1_image_path)
|
|
243
234
|
|
|
244
235
|
motion_target_path = determine_motion_target(motion_target_option=motion_target_option,
|
|
245
|
-
|
|
246
|
-
half_life=half_life)
|
|
236
|
+
input_image_path=input_image_path)
|
|
247
237
|
motion_target = ants.image_read(motion_target_path)
|
|
248
238
|
|
|
249
239
|
motion_target_to_mpr_reg = ants.registration(fixed=t1_image,
|
|
@@ -297,13 +287,12 @@ def motion_corr_frame_list_to_t1(input_image_4d_path: str,
|
|
|
297
287
|
print(f"(ImageOps4d): motion corrected image saved to {out_image_path}")
|
|
298
288
|
|
|
299
289
|
|
|
300
|
-
def motion_corr_frames_above_mean_value(
|
|
290
|
+
def motion_corr_frames_above_mean_value(input_image_path: str,
|
|
301
291
|
out_image_path: str,
|
|
302
292
|
motion_target_option: str | tuple,
|
|
303
293
|
verbose: bool,
|
|
304
294
|
type_of_transform: str = 'Affine',
|
|
305
295
|
transform_metric: str = 'mattes',
|
|
306
|
-
half_life: float = None,
|
|
307
296
|
scale_factor=1.0,
|
|
308
297
|
**kwargs):
|
|
309
298
|
r"""
|
|
@@ -314,7 +303,7 @@ def motion_corr_frames_above_mean_value(input_image_4d_path: str,
|
|
|
314
303
|
utilizes the :func:`motion_corr_frame_list` function to perform the motion correction.
|
|
315
304
|
|
|
316
305
|
Args:
|
|
317
|
-
|
|
306
|
+
input_image_path (str): Path to the input 4D PET image file.
|
|
318
307
|
motion_target_option (str | tuple): Option to determine the motion target. This can
|
|
319
308
|
be a path to a specific image file, a tuple of frame indices to generate a target, or
|
|
320
309
|
specific options recognized by :func:`determine_motion_target`.
|
|
@@ -324,8 +313,6 @@ def motion_corr_frames_above_mean_value(input_image_4d_path: str,
|
|
|
324
313
|
Default is 'Affine'.
|
|
325
314
|
transform_metric (str, optional): Metric to use for the transformation. Default is
|
|
326
315
|
'mattes'.
|
|
327
|
-
half_life (float, optional): Half-life value used by `determine_motion_target`, if
|
|
328
|
-
applicable. Default is None.
|
|
329
316
|
scale_factor (float, optional): Scale factor to apply to frame mean values before
|
|
330
317
|
comparison. Default is 1.0.
|
|
331
318
|
**kwargs: Additional arguments passed to the `ants.registration` method.
|
|
@@ -339,7 +326,7 @@ def motion_corr_frames_above_mean_value(input_image_4d_path: str,
|
|
|
339
326
|
|
|
340
327
|
from petpal.preproc.motion_corr import motion_corr_frames_above_mean_value
|
|
341
328
|
|
|
342
|
-
motion_corr_frames_above_mean_value(
|
|
329
|
+
motion_corr_frames_above_mean_value(input_image_path='/path/to/image.nii.gz',
|
|
343
330
|
motion_target_option='/path/to/target_image.nii.gz',
|
|
344
331
|
out_image_path='/path/to/output_motion_corrected.nii.gz',
|
|
345
332
|
verbose=True,
|
|
@@ -356,28 +343,26 @@ def motion_corr_frames_above_mean_value(input_image_4d_path: str,
|
|
|
356
343
|
|
|
357
344
|
"""
|
|
358
345
|
|
|
359
|
-
frames_list = _get_list_of_frames_above_total_mean(image_4d_path=
|
|
346
|
+
frames_list = _get_list_of_frames_above_total_mean(image_4d_path=input_image_path,
|
|
360
347
|
scale_factor=scale_factor)
|
|
361
348
|
|
|
362
|
-
motion_corr_frame_list(
|
|
349
|
+
motion_corr_frame_list(input_image_path=input_image_path,
|
|
363
350
|
motion_target_option=motion_target_option,
|
|
364
351
|
out_image_path=out_image_path,
|
|
365
352
|
verbose=verbose,
|
|
366
353
|
frames_list=frames_list,
|
|
367
354
|
type_of_transform=type_of_transform,
|
|
368
355
|
transform_metric=transform_metric,
|
|
369
|
-
half_life=half_life,
|
|
370
356
|
**kwargs)
|
|
371
357
|
|
|
372
358
|
|
|
373
|
-
def motion_corr_frames_above_mean_value_to_t1(
|
|
359
|
+
def motion_corr_frames_above_mean_value_to_t1(input_image_path: str,
|
|
374
360
|
t1_image_path: str,
|
|
375
361
|
motion_target_option: str | tuple,
|
|
376
362
|
out_image_path: str,
|
|
377
363
|
verbose: bool,
|
|
378
364
|
type_of_transform: str = 'AffineFast',
|
|
379
365
|
transform_metric: str = "mattes",
|
|
380
|
-
half_life: float = None,
|
|
381
366
|
scale_factor: float = 1.0):
|
|
382
367
|
"""
|
|
383
368
|
Perform motion correction on frames with mean values above the mean of a 4D PET image to a T1
|
|
@@ -389,7 +374,7 @@ def motion_corr_frames_above_mean_value_to_t1(input_image_4d_path: str,
|
|
|
389
374
|
function.
|
|
390
375
|
|
|
391
376
|
Args:
|
|
392
|
-
|
|
377
|
+
input_image_path (str): Path to the input 4D PET image file.
|
|
393
378
|
t1_image_path (str): Path to the 3D T1 anatomical image.
|
|
394
379
|
motion_target_option (str | tuple): Option to determine the motion target. This can
|
|
395
380
|
be a path to a specific image file, a tuple of frame indices to generate a target, or
|
|
@@ -399,8 +384,6 @@ def motion_corr_frames_above_mean_value_to_t1(input_image_4d_path: str,
|
|
|
399
384
|
type_of_transform (str, optional): Type of transformation to use for registration. Default
|
|
400
385
|
is 'AffineFast'.
|
|
401
386
|
transform_metric (str, optional): Metric to use for the transformation. Default is 'mattes'.
|
|
402
|
-
half_life (float, optional): Half-life value used by `determine_motion_target`, if
|
|
403
|
-
applicable. Default is None.
|
|
404
387
|
scale_factor (float, optional): Scale factor applied to the mean voxel value of the entire
|
|
405
388
|
image for comparison. Must be greater than 0. Default is 1.0.
|
|
406
389
|
|
|
@@ -413,7 +396,7 @@ def motion_corr_frames_above_mean_value_to_t1(input_image_4d_path: str,
|
|
|
413
396
|
|
|
414
397
|
from petpal.preproc.motion_corr import motion_corr_frames_above_mean_value_to_t1
|
|
415
398
|
|
|
416
|
-
motion_corr_frames_above_mean_value_to_t1(
|
|
399
|
+
motion_corr_frames_above_mean_value_to_t1(input_image_path='/path/to/image.nii.gz',
|
|
417
400
|
t1_image_path='/path/to/t1_image.nii.gz',
|
|
418
401
|
motion_target_option='/path/to/target_image.nii.gz',
|
|
419
402
|
out_image_path='/path/to/output_motion_corrected.nii.gz',
|
|
@@ -430,18 +413,17 @@ def motion_corr_frames_above_mean_value_to_t1(input_image_4d_path: str,
|
|
|
430
413
|
- The :func:`_get_list_of_frames_above_total_mean` function identifies
|
|
431
414
|
the frames to be motion corrected based on their mean voxel values.
|
|
432
415
|
"""
|
|
433
|
-
frames_list = _get_list_of_frames_above_total_mean(image_4d_path=
|
|
416
|
+
frames_list = _get_list_of_frames_above_total_mean(image_4d_path=input_image_path,
|
|
434
417
|
scale_factor=scale_factor)
|
|
435
418
|
|
|
436
|
-
motion_corr_frame_list_to_t1(
|
|
419
|
+
motion_corr_frame_list_to_t1(input_image_path=input_image_path,
|
|
437
420
|
t1_image_path=t1_image_path,
|
|
438
421
|
motion_target_option=motion_target_option,
|
|
439
422
|
out_image_path=out_image_path,
|
|
440
423
|
verbose=verbose,
|
|
441
424
|
frames_list=frames_list,
|
|
442
425
|
type_of_transform=type_of_transform,
|
|
443
|
-
transform_metric=transform_metric
|
|
444
|
-
half_life=half_life)
|
|
426
|
+
transform_metric=transform_metric)
|
|
445
427
|
|
|
446
428
|
|
|
447
429
|
def windowed_motion_corr_to_target(input_image_path: str,
|
|
@@ -500,8 +482,7 @@ def windowed_motion_corr_to_target(input_image_path: str,
|
|
|
500
482
|
frame_timing_info = ScanTimingInfo.from_nifti(image_path=input_image_path)
|
|
501
483
|
|
|
502
484
|
target_image = determine_motion_target(motion_target_option=motion_target_option,
|
|
503
|
-
|
|
504
|
-
half_life=half_life)
|
|
485
|
+
input_image_path=input_image_path)
|
|
505
486
|
target_image = ants.image_read(target_image)
|
|
506
487
|
|
|
507
488
|
reg_kwargs_default = {'aff_metric' : 'mattes',
|
|
@@ -551,68 +532,6 @@ def gen_timeseries_from_image_list(image_list: list[ants.core.ANTsImage]) -> ant
|
|
|
551
532
|
return ants.list_to_ndimage(tmp_image, image_list)
|
|
552
533
|
|
|
553
534
|
|
|
554
|
-
def gen_nd_image_based_on_image_list(image_list: list[ants.core.ants_image.ANTsImage]):
|
|
555
|
-
r"""
|
|
556
|
-
Generate a 4D ANTsImage based on a list of 3D ANTsImages.
|
|
557
|
-
|
|
558
|
-
This function takes a list of 3D ANTsImages and constructs a new 4D ANTsImage,
|
|
559
|
-
where the additional dimension represents the number of frames (3D images) in the list.
|
|
560
|
-
The 4D image retains the spacing, origin, direction, and shape properties of the 3D images,
|
|
561
|
-
with appropriate modifications for the additional dimension.
|
|
562
|
-
|
|
563
|
-
Args:
|
|
564
|
-
image_list (list[ants.core.ants_image.ANTsImage]):
|
|
565
|
-
List of 3D ANTsImage objects to be combined into a 4D image.
|
|
566
|
-
The list must contain at least one image, and all images must have the same
|
|
567
|
-
dimensions and properties.
|
|
568
|
-
|
|
569
|
-
Returns:
|
|
570
|
-
ants.core.ants_image.ANTsImage:
|
|
571
|
-
A 4D ANTsImage constructed from the input list of 3D images. The additional
|
|
572
|
-
dimension corresponds to the number of frames (length of the image list).
|
|
573
|
-
|
|
574
|
-
Raises:
|
|
575
|
-
AssertionError: If the `image_list` is empty or if the images in the list are not 3D.
|
|
576
|
-
|
|
577
|
-
See Also
|
|
578
|
-
* :func:`petpal.preproc.motion_corr.motion_corr_frame_list_to_t1`
|
|
579
|
-
|
|
580
|
-
Example:
|
|
581
|
-
|
|
582
|
-
.. code-block:: python
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
import ants
|
|
586
|
-
image1 = ants.image_read('frame1.nii.gz')
|
|
587
|
-
image2 = ants.image_read('frame2.nii.gz')
|
|
588
|
-
image_list = [image1, image2]
|
|
589
|
-
result = _gen_nd_image_based_on_image_list(image_list)
|
|
590
|
-
print(result.dimension) # 4
|
|
591
|
-
image4d = ants.list_to_ndimage(result, image_list)
|
|
592
|
-
|
|
593
|
-
"""
|
|
594
|
-
assert len(image_list) > 0
|
|
595
|
-
assert image_list[0].dimension == 3
|
|
596
|
-
|
|
597
|
-
num_frames = len(image_list)
|
|
598
|
-
spacing_3d = image_list[0].spacing
|
|
599
|
-
origin_3d = image_list[0].origin
|
|
600
|
-
shape_3d = image_list[0].shape
|
|
601
|
-
direction_3d = image_list[0].direction
|
|
602
|
-
|
|
603
|
-
direction_4d = np.eye(4)
|
|
604
|
-
direction_4d[:3, :3] = direction_3d
|
|
605
|
-
spacing_4d = (*spacing_3d, 1.0)
|
|
606
|
-
origin_4d = (*origin_3d, 0.0)
|
|
607
|
-
shape_4d = (*shape_3d, num_frames)
|
|
608
|
-
|
|
609
|
-
tmp_image = ants.make_image(imagesize=shape_4d,
|
|
610
|
-
spacing=spacing_4d,
|
|
611
|
-
origin=origin_4d,
|
|
612
|
-
direction=direction_4d)
|
|
613
|
-
return tmp_image
|
|
614
|
-
|
|
615
|
-
|
|
616
535
|
def _get_list_of_frames_above_total_mean(image_4d_path: str,
|
|
617
536
|
scale_factor: float = 1.0):
|
|
618
537
|
"""
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Module with function to get a motion target for motion correction and registration"""
|
|
2
|
+
import os
|
|
3
|
+
import tempfile
|
|
4
|
+
import ants
|
|
5
|
+
|
|
6
|
+
from ..utils.useful_functions import get_average_of_timeseries
|
|
7
|
+
from .standard_uptake_value import weighted_sum_for_suv
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def determine_motion_target(motion_target_option: str | tuple | list,
|
|
11
|
+
input_image_path: str = None) -> str:
|
|
12
|
+
"""
|
|
13
|
+
Produce a motion target given the ``motion_target_option`` from a method
|
|
14
|
+
running registrations on PET, i.e. :meth:`motion_correction` or
|
|
15
|
+
:meth:`register_pet`.
|
|
16
|
+
|
|
17
|
+
The motion target option can be a string or a tuple. If it is a string,
|
|
18
|
+
then if this string is a file, use the file as the motion target.
|
|
19
|
+
|
|
20
|
+
If it is the option ``weighted_series_sum``, then run
|
|
21
|
+
:meth:`weighted_series_sum` and return the output path.
|
|
22
|
+
|
|
23
|
+
If it is the option ``mean_image``, then compute the time-average of the
|
|
24
|
+
4D-PET image.
|
|
25
|
+
|
|
26
|
+
If it is a tuple, run a weighted sum on the PET series on a range of
|
|
27
|
+
frames. The elements of the tuple are treated as times in seconds, counted
|
|
28
|
+
from the time of the first frame, i.e. (0,300) would average all frames
|
|
29
|
+
from the first to the frame 300 seconds later. If the two elements are the
|
|
30
|
+
same, returns the one frame closest to the entered time.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
motion_target_option (str | tuple | list): Determines how the method behaves,
|
|
34
|
+
according to the above description. Can be a file, a method
|
|
35
|
+
('weighted_series_sum' or 'mean_image'), or a tuple range e.g. (0,600).
|
|
36
|
+
input_image_path (str): Path to the PET image. This is intended to
|
|
37
|
+
be supplied by the parent method employing this function. Default
|
|
38
|
+
value None.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
out_image_file (str): File to use as a target to compute
|
|
42
|
+
transformations on.
|
|
43
|
+
|
|
44
|
+
Raises:
|
|
45
|
+
ValueError: If ``motion_target_option`` is not a string, list, or tuple. If it is a string,
|
|
46
|
+
but does not match one of the preset options or path to a file, the error will also be
|
|
47
|
+
raised.
|
|
48
|
+
TypeError: If start and end time are incompatible with ``float`` type.
|
|
49
|
+
"""
|
|
50
|
+
if isinstance(motion_target_option, str):
|
|
51
|
+
if os.path.exists(motion_target_option):
|
|
52
|
+
return motion_target_option
|
|
53
|
+
|
|
54
|
+
if motion_target_option == 'weighted_series_sum':
|
|
55
|
+
out_image_file = tempfile.mkstemp(suffix='_wss.nii.gz')[1]
|
|
56
|
+
weighted_sum_for_suv(input_image_path=input_image_path,
|
|
57
|
+
output_image_path=out_image_file)
|
|
58
|
+
return out_image_file
|
|
59
|
+
|
|
60
|
+
if motion_target_option == 'mean_image':
|
|
61
|
+
out_image_file = tempfile.mkstemp(suffix='_mean.nii.gz')[1]
|
|
62
|
+
input_img = ants.image_read(input_image_path)
|
|
63
|
+
mean_img = get_average_of_timeseries(input_image=input_img)
|
|
64
|
+
ants.image_write(image=mean_img,filename=out_image_file)
|
|
65
|
+
return out_image_file
|
|
66
|
+
|
|
67
|
+
raise ValueError("motion_target_option did not match a file or 'weighted_series_sum'")
|
|
68
|
+
|
|
69
|
+
if isinstance(motion_target_option, (list, tuple)):
|
|
70
|
+
|
|
71
|
+
start_time = motion_target_option[0]
|
|
72
|
+
end_time = motion_target_option[1]
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
float(start_time)
|
|
76
|
+
float(end_time)
|
|
77
|
+
except Exception as exc:
|
|
78
|
+
raise TypeError('Start time and end time of calculation must be '
|
|
79
|
+
'able to be cast into float! Provided values are '
|
|
80
|
+
f"{start_time} and {end_time}.") from exc
|
|
81
|
+
|
|
82
|
+
out_image_file = tempfile.mkstemp(suffix='_wss.nii.gz')[1]
|
|
83
|
+
weighted_sum_for_suv(input_image_path=input_image_path,
|
|
84
|
+
output_image_path=out_image_file,
|
|
85
|
+
start_time=float(start_time),
|
|
86
|
+
end_time=float(end_time))
|
|
87
|
+
|
|
88
|
+
return out_image_file
|
|
89
|
+
|
|
90
|
+
raise ValueError('motion_target_option did not match str or tuple type.')
|
|
@@ -192,7 +192,7 @@ def write_tacs(input_image_path: str,
|
|
|
192
192
|
print('Finished writing TACs.')
|
|
193
193
|
|
|
194
194
|
|
|
195
|
-
def roi_tac(
|
|
195
|
+
def roi_tac(input_image_path: str,
|
|
196
196
|
roi_image_path: str,
|
|
197
197
|
region: list[int] | int,
|
|
198
198
|
out_tac_path: str | None = None,
|
|
@@ -223,8 +223,8 @@ def roi_tac(input_image_4d_path: str,
|
|
|
223
223
|
raise ValueError("'time_frame_keyword' must be one of "
|
|
224
224
|
"'FrameReferenceTime' or 'FrameTimesStart'")
|
|
225
225
|
|
|
226
|
-
pet_meta = image_io.load_metadata_for_nifti_with_same_filename(
|
|
227
|
-
pet_numpy = ants.image_read(
|
|
226
|
+
pet_meta = image_io.load_metadata_for_nifti_with_same_filename(input_image_path)
|
|
227
|
+
pet_numpy = ants.image_read(input_image_path).numpy()
|
|
228
228
|
seg_numpy = ants.image_read(roi_image_path).numpy()
|
|
229
229
|
|
|
230
230
|
region_mask = combine_regions_as_mask(segmentation_img=seg_numpy,
|
petpal/preproc/register.py
CHANGED
|
@@ -10,7 +10,7 @@ import nibabel
|
|
|
10
10
|
import numpy as np
|
|
11
11
|
from nibabel.processing import resample_from_to
|
|
12
12
|
|
|
13
|
-
from .
|
|
13
|
+
from .motion_target import determine_motion_target
|
|
14
14
|
from ..utils import image_io
|
|
15
15
|
from ..utils.useful_functions import check_physical_space_for_ants_image_pair
|
|
16
16
|
|
|
@@ -29,15 +29,10 @@ def register_pet_to_pet(input_image_path: str,
|
|
|
29
29
|
Returns:
|
|
30
30
|
ants.ANTsImage: ANTsImage containing input image registered to reference image.
|
|
31
31
|
"""
|
|
32
|
-
|
|
33
|
-
half_life = image_io.get_half_life_from_nifti(image_path=input_image_path)
|
|
34
|
-
|
|
35
32
|
wss_input = determine_motion_target(motion_target_option='weighted_series_sum',
|
|
36
|
-
|
|
37
|
-
half_life=half_life)
|
|
33
|
+
input_image_path=input_image_path)
|
|
38
34
|
wss_reference = determine_motion_target(motion_target_option='weighted_series_sum',
|
|
39
|
-
|
|
40
|
-
half_life=half_life)
|
|
35
|
+
input_image_path=reference_pet_image_path)
|
|
41
36
|
|
|
42
37
|
wss_input_ants = ants.image_read(wss_input)
|
|
43
38
|
wss_reference_ants = ants.image_read(wss_reference)
|
|
@@ -67,7 +62,6 @@ def register_pet(input_reg_image_path: str,
|
|
|
67
62
|
motion_target_option: Union[str, tuple],
|
|
68
63
|
verbose: bool,
|
|
69
64
|
type_of_transform: str = 'DenseRigid',
|
|
70
|
-
half_life: float = None,
|
|
71
65
|
**kwargs):
|
|
72
66
|
"""
|
|
73
67
|
Computes and runs rigid registration of 4D PET image series to 3D anatomical image, typically
|
|
@@ -91,8 +85,7 @@ def register_pet(input_reg_image_path: str,
|
|
|
91
85
|
kwargs (keyword arguments): Additional arguments passed to :py:func:`ants.registration`.
|
|
92
86
|
"""
|
|
93
87
|
motion_target = determine_motion_target(motion_target_option=motion_target_option,
|
|
94
|
-
|
|
95
|
-
half_life=half_life)
|
|
88
|
+
input_image_path=input_reg_image_path)
|
|
96
89
|
motion_target_image = ants.image_read(motion_target)
|
|
97
90
|
mri_image = ants.image_read(reference_image_path)
|
|
98
91
|
pet_image_ants = ants.image_read(input_reg_image_path)
|
|
@@ -14,7 +14,7 @@ import nibabel
|
|
|
14
14
|
from nibabel import processing
|
|
15
15
|
import pandas as pd
|
|
16
16
|
|
|
17
|
-
from . import
|
|
17
|
+
from ..utils.useful_functions import gen_nd_image_based_on_image_list
|
|
18
18
|
from ..utils import math_lib
|
|
19
19
|
|
|
20
20
|
|
|
@@ -203,7 +203,7 @@ def replace_probabilistic_region(segmentation_numpy: np.ndarray,
|
|
|
203
203
|
return segmentation_numpy
|
|
204
204
|
|
|
205
205
|
|
|
206
|
-
def resample_segmentation(
|
|
206
|
+
def resample_segmentation(input_image_path: str,
|
|
207
207
|
segmentation_image_path: str,
|
|
208
208
|
out_seg_path: str,
|
|
209
209
|
verbose: bool):
|
|
@@ -215,7 +215,7 @@ def resample_segmentation(input_image_4d_path: str,
|
|
|
215
215
|
PET and ROI data are registered to the same space, but have different resolutions.
|
|
216
216
|
|
|
217
217
|
Args:
|
|
218
|
-
|
|
218
|
+
input_image_path (str): Path to a .nii or .nii.gz file containing a 4D
|
|
219
219
|
PET image, registered to anatomical space, to which the segmentation file is resampled.
|
|
220
220
|
segmentation_image_path (str): Path to a .nii or .nii.gz file containing a 3D segmentation
|
|
221
221
|
image, where integer indices label specific regions.
|
|
@@ -223,7 +223,7 @@ def resample_segmentation(input_image_4d_path: str,
|
|
|
223
223
|
image is written.
|
|
224
224
|
verbose (bool): Set to ``True`` to output processing information.
|
|
225
225
|
"""
|
|
226
|
-
pet_image = nibabel.load(
|
|
226
|
+
pet_image = nibabel.load(input_image_path)
|
|
227
227
|
seg_image = nibabel.load(segmentation_image_path)
|
|
228
228
|
pet_series = pet_image.get_fdata()
|
|
229
229
|
image_first_frame = pet_series[:, :, :, 0]
|
|
@@ -363,7 +363,7 @@ def gw_segmentation(freesurfer_path: str,
|
|
|
363
363
|
origin=freesurfer.origin,
|
|
364
364
|
spacing=freesurfer.spacing,
|
|
365
365
|
direction=freesurfer.direction)
|
|
366
|
-
gw_map_template =
|
|
366
|
+
gw_map_template = gen_nd_image_based_on_image_list([gm_img, wm_img])
|
|
367
367
|
gw_map_4d = ants.list_to_ndimage(image=gw_map_template,image_list=[gm_img,wm_img])
|
|
368
368
|
ants.image_write(gw_map_4d,output_path)
|
|
369
369
|
|