2
2
3
3
import numpy as np
4
4
import os
5
+ import shutil
5
6
from tempfile import mktemp
6
7
7
8
import ants
@@ -14,6 +15,7 @@ def build_template(
14
15
blending_weight = 0.75 ,
15
16
weights = None ,
16
17
useNoRigid = True ,
18
+ output_dir = None ,
17
19
** kwargs
18
20
):
19
21
"""
@@ -44,6 +46,10 @@ def build_template(
44
46
useNoRigid : boolean
45
47
equivalent of -y in the script. Template update
46
48
step will not use the rigid component if this is True.
49
+
50
+ output_dir : path
51
+ directory name where intermediate transforms are written
52
+
47
53
kwargs : keyword args
48
54
extra arguments passed to ants registration
49
55
@@ -60,6 +66,12 @@ def build_template(
60
66
>>> timage = ants.build_template( image_list = ( image, image2, image3 ) ).resample_image( (45,45))
61
67
>>> timagew = ants.build_template( image_list = ( image, image2, image3 ), weights = (5,1,1) )
62
68
"""
69
+ work_dir = mktemp () if output_dir is None else output_dir
70
+
71
+ def make_outprefix (k : int ):
72
+ os .makedirs (os .path .join (work_dir , f"img{ k :04d} " ), exist_ok = True )
73
+ return os .path .join (work_dir , f"img{ k :04d} " , "out" )
74
+
63
75
if "type_of_transform" not in kwargs :
64
76
type_of_transform = "SyN"
65
77
else :
@@ -80,7 +92,7 @@ def build_template(
80
92
affinelist = []
81
93
for k in range (len (image_list )):
82
94
w1 = ants .registration (
83
- xavg , image_list [k ], type_of_transform = type_of_transform , ** kwargs
95
+ xavg , image_list [k ], type_of_transform = type_of_transform , outprefix = make_outprefix ( k ), ** kwargs
84
96
)
85
97
L = len (w1 ["fwdtransforms" ])
86
98
# affine is the last one
@@ -99,7 +111,7 @@ def build_template(
99
111
avgaffine = ants .average_affine_transform_no_rigid (affinelist )
100
112
else :
101
113
avgaffine = ants .average_affine_transform (affinelist )
102
- afffn = mktemp ( suffix = " .mat" )
114
+ afffn = os . path . join ( work_dir , "avgAffine .mat" )
103
115
ants .write_transform (avgaffine , afffn )
104
116
105
117
if L == 2 :
@@ -108,17 +120,18 @@ def build_template(
108
120
wavg = wavg * wscl
109
121
# apply affine to the nonlinear?
110
122
# need to save the average
111
- wavgA = ants .apply_transforms (fixed = xavgNew , moving = wavg , imagetype = 1 , transformlist = afffn , whichtoinvert = [1 ])
112
- wavgfn = mktemp ( suffix = " .nii.gz" )
123
+ wavgA = ants .apply_transforms (fixed = xavgNew , moving = wavg , imagetype = 1 , transformlist = afffn , whichtoinvert = [1 ])
124
+ wavgfn = os . path . join ( work_dir , "avgWarp .nii.gz" )
113
125
ants .image_write (wavgA , wavgfn )
114
126
xavg = ants .apply_transforms (fixed = xavgNew , moving = xavgNew , transformlist = [wavgfn , afffn ], whichtoinvert = [0 , 1 ])
115
127
else :
116
128
xavg = ants .apply_transforms (fixed = xavgNew , moving = xavgNew , transformlist = [afffn ], whichtoinvert = [1 ])
117
129
118
- os .remove (afffn )
119
130
if blending_weight is not None :
120
131
xavg = xavg * blending_weight + ants .iMath (xavg , "Sharpen" ) * (
121
132
1.0 - blending_weight
122
133
)
123
134
135
+ if output_dir is None :
136
+ shutil .rmtree (work_dir )
124
137
return xavg
0 commit comments