-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask_mesh.py
209 lines (152 loc) · 7.55 KB
/
mask_mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
import nibabel as nib
import sys
import os
def mask_mesh(input_file, output_file_base, mask_file):
'''
Function mask_mesh.py
Makes a mesh and mask for input to mesh_tet.py
input parameters:
input_file: A 4D coordinate image created in est_resels.py.
output_file_base: The base for output file names.
mask_file: File name for the binary mask file
returns:
None
Originally written for fmristat package by Keith Worsley.
This is a modified version using read/write functions from nibabel.
There are other modifications to make the program shorter and run faster.
'''
# Initialization
#-------------------------------------------------------------------------------
mask_thresh = 0
#-loading in the xyz coordinate info stored in a 4D file
#-------------------------------------------------------------------------------
coord_img = nib.load(input_file)
img_data = coord_img.get_data()
dim = coord_img.header['dim']
n = dim[4]
numslices = dim[3]
J = dim[2]
I = dim[1]
m = nib.load(mask_file)
m_img = m.get_data()
mm_img = m_img.copy()
# removing the extension from the coordinate file name
coord_file_path = os.path.abspath(input_file)
coord_file_ext = os.path.abspath(input_file)
while coord_file_ext!='':
coord_file_path, coord_file_ext = os.path.splitext(coord_file_path)
# temporary output file name base
pth, fname = os.path.split(coord_file_path)
base = os.path.join(pth,fname);
#-loading in the mask file
#-------------------------------------------------------------------------------
m = nib.load(mask_file)
m_data = m.get_data()
dd = nib.load(input_file)
dim2 = dd.header['dim']
dim2[0:4] = np.hstack([I, J, numslices, n])
dd_img = np.zeros(dim2[0:4])
# Set up:
#-------------------------------------------------------------------------------
i = np.kron(np.ones((1,J)),np.arange(I))
j = np.kron(np.arange(J),np.ones((1,I)))
IJ = I * J
ex = np.nonzero(i<I-1)[-1].reshape(1,-1)
ex1 = np.vstack((ex, ex+IJ)).T
ex = np.nonzero(i>0)[-1].reshape(1,-1)
ex2 = np.vstack((ex, ex+IJ)).T
ey = np.nonzero(j<J-1)[-1].reshape(1,-1)
ey1 = np.vstack((ey, ey+IJ)).T
ey = np.nonzero(j>0)[-1].reshape(1,-1)
ey2 = np.vstack((ey, ey+IJ)).T
ez = np.arange(IJ).reshape(1,-1) #added reshape
ez1 = ez
ez2 = ez+IJ
exye = np.nonzero(((i+j)%2==0) & (i<I-1) & (j<J-1))[-1].reshape(1,-1)
exyo = np.nonzero(((i+j)%2==1) & (i<I-1) & (j<J-1))[-1].reshape(1,-1)
exy = np.hstack([exye, exyo])
exy1 = np.vstack([np.hstack([exye, exyo+1]),
np.hstack([exye+1+IJ, exyo+IJ])]).T
exy2 = np.vstack([np.hstack([exye+1+I, exyo+I]),
np.hstack([exye+I+IJ, exyo+1+I+IJ])]).T
exze = np.nonzero(((i+j)%2==0) & (i<I-1))[-1].reshape(1,-1)
exzo = np.nonzero(((i+j)%2==1) & (i<I-1))[-1].reshape(1,-1)
exz = np.hstack([exze, exzo])
exz1 = np.hstack([exze, exzo+1])
exz2 = np.hstack([exze+1+IJ, exzo+IJ])
eyze = np.nonzero(((i+j)%2==0) & (j<J-1))[-1].reshape(1,-1)
eyzo = np.nonzero(((i+j)%2==0) & (j<J-1))[-1].reshape(1,-1)
eyz = np.hstack([eyze, eyzo])
eyz1 = np.hstack([eyze, eyzo + I])
eyz2 = np.hstack([eyze+I+IJ, eyzo+IJ])
edges_start1 = np.hstack([(ex1[:,0]).T, (ey1[:,0]).T, (exy1[:,0]).T, (ex2[:,0]).T, (ey2[:,0]).T, (exy2[:,0]).T]).reshape(1,-1)
edges_start2 = np.hstack([(ex2[:,0]).T, (ey2[:,0]).T, (exy2[:,0]).T, (ex1[:,0]).T, (ey1[:,0]).T, (exy1[:,0]).T]).reshape(1, -1)
edge1 = np.hstack([(ex1[:,0]).T.reshape(1, -1), (ey1[:,0]).T.reshape(1, -1), (exy1[:,0]).T.reshape(1, -1), ez1, exz1, eyz1, (ex1[:,1]).T.reshape(1,-1), (ey1[:,1]).T.reshape(1,-1), (exy1[:,1]).T.reshape(1, -1)]) #added reshape
edge2 = np.hstack([(ex2[:,0]).T.reshape(1,-1), (ey2[:,0]).T.reshape(1, -1), (exy2[:,0]).T.reshape(1,-1), ez2, exz2, eyz2, (ex2[:,1]).T.reshape(1, -1), (ey2[:,1]).T.reshape(1,-1), (exy2[:,1]).T.reshape(1, -1)]) #added reshape
edges1 = np.hstack([edge1, edge2])
edges2 = np.hstack([edge2, edge1])
#START:
u = np.zeros((2*IJ,n))
v = np.zeros((2*IJ,n))
mask = np.zeros((2*IJ, 1))
nask = np.zeros((2*IJ, 1))
flip = 1
print('Calculating mask and mesh')
for slice in range (int(numslices)):
print('.', end = '')
flip = 3-flip
tmpimg = np.array(img_data[:,:,slice,:n])
alteration = (flip -1)*IJ
u[np.arange(IJ)+ alteration, :] = tmpimg.reshape( IJ, n)
v[np.arange(IJ)+ alteration, :] = tmpimg.reshape( IJ, n)
mask[np.arange(IJ)+ alteration, :] = m_data[:, :, slice].reshape(IJ, 1)
nask[np.arange(IJ)+ alteration, :] = (m_data[:, :, slice]>mask_thresh & np.isfinite(m_data[:, :, slice])).reshape(IJ, 1)
if slice == 0:
temp1 = np.logical_not(nask[edges_start1])
# print(temp1)
# print(~ temp1)
# print((temp1.reshape(-1, 1)).shape)
# print(nask.shape)
# print(mask[edges_start2]>mask_thresh & np.isfinite(mask[edges_start2]))
# print((~nask[edges_start1]).shape)
#print(((mask[edges_start2]>mask_thresh).reshape(-1, 1)).shape) #right shape for this
# print(((np.isfinite(mask[edges_start2])).reshape(-1,1)).shape) #this is also right
#print((((mask[edges_start2]>mask_thresh).reshape(-1, 1)) & ((np.isfinite(mask[edges_start2])).reshape(-1,1))).shape) #also good
# print(((temp1.reshape(-1, 1)) & ((mask[edges_start2]>mask_thresh).reshape(-1, 1)) & ((np.isfinite(mask[edges_start2])).reshape(-1,1))).shape)
surf = (temp1.reshape(-1, 1)) & ((mask[edges_start2]>mask_thresh).reshape(-1, 1)) & ((np.isfinite(mask[edges_start2])).reshape(-1,1))
surf = np.array(np.where(surf))
# print(surf.size)
m0 = edges_start1[surf]
m1 = edges_start2[surf]
else:
temp1 = np.logical_not(nask[edges_start1])
surf = (temp1.reshape(-1, 1)) & ((mask[edges_start2]>mask_thresh).reshape(-1, 1)) & ((np.isfinite(mask[edges_start2])).reshape(-1,1))
surf = np.array(np.where(surf))
m0 = edges1[surf]
m1 = edges2[surf]
if surf.size != 0:
dm = mask[m1] - mask[m0]
mt = mask_thresh * np.ones(len(surf), 1)
w = (mt -mask[m0])/(dm+(dm <=0)) * (dm>0)
u0 = u[m0, :]
u1 = u[m1, :]
for i in range(n):
v[m0, i] = (u0[:, i] * (1-w) + u1[:, i] * w)
nask[m0] = 1
if slice > 0:
alteration2 = (2-flip)*IJ
temp2 = ((dd_img[:, :, slice-1, :]).reshape(I,J,1,n))
temp3 = ((v[np.arange(IJ) + alteration2, :]).reshape(I, J, 1, n))
temp3 = temp2
mm_img[:, :, slice -1] = (nask[np.arange(IJ) + alteration2]).reshape(I, J)
if slice == numslices:
alteration3 = (flip-1)*IJ
temp4 = ((dd_img[:, :, slice, :]).reshape(I,J,1,n))
temp5 = ((v[np.arange(IJ) + alteration3, :]).reshape(I, J, 1, n))
temp4 = temp5
mm_img[:, :, slice] = (nask[np.arange(IJ) + alteration3]).reshape(I, J)
print('Done\n')
nib.save(dd, 'outputmesh.nii')
nib.save(mm, 'ouputmask.nii')
mask_mesh('mask_coord.nii.gz', 'mask', 'mask.nii.gz')