-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrsidata.py
387 lines (339 loc) · 16.1 KB
/
mrsidata.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import suspect
import matplotlib.pyplot as plt
import numpy as np
import os
import pydicom
import struct
import warnings
import nibabel as nib
import ants
import functools
from multiprocessing import Pool
def fit(f):
print(f)
os.system("/Users/voelzkey/Desktop/CodeFortran/LCModel/lcmodel < %s" %f)
def mkdir(path):
if not os.path.isdir(path):
os.mkdir(path)
def hamming(x):
a = 25/46
return a-(1-a)*np.cos(np.arange(x)*2*np.pi/x)
def hamming2D(x,y):
X = hamming(x)
Y = hamming(y)
return np.outer(X,Y)
def hamming3D(x,y,z):
X = hamming(x)
Y = hamming(y)
Z = hamming(z)
return functools.reduce(np.multiply.outer, (X, Y, Z))
def filterHamming(inp):
data = inp.copy()
x,y = data.shape[:2]
H = hamming2D(x,y)
data = np.fft.ifftshift(data, axes=(0,1))
data = np.fft.ifft2(data, axes=(0,1))
data = np.fft.fftshift(data, axes=(0,1))
dataF = data * H[...,np.newaxis]
dataF = np.fft.ifftshift(dataF, axes=(0,1))
dataF = np.fft.fft2(dataF, axes=(0,1))
dataF = np.fft.fftshift(dataF, axes=(0,1))
return dataF
def filterHamming3D(inp):
data = inp.copy()
x,y,z = data.shape[:3]
H = hamming3D(x,y,z)
print(H.shape)
data = np.fft.ifftshift(data, axes=(0,1,2))
data = np.fft.ifft2(data, axes=(0,1,2))
data = np.fft.fftshift(data, axes=(0,1,2))
dataF = data * H[...,np.newaxis]
dataF = np.fft.ifftshift(dataF, axes=(0,1,2))
dataF = np.fft.fft2(dataF, axes=(0,1,2))
dataF = np.fft.fftshift(dataF, axes=(0,1,2))
return dataF
# Define some helper functions, copyied from the suspect library
ima_types = {
"floats": ["NumberOfAverages", "RSatPositionSag", "PercentPhaseFieldOfView", "RSatOrientationSag", "MixingTime",
"PercentPhaseFieldOfView", "RSatPositionCor", "InversionTime", "RepetitionTime", "VoiThickness",
"TransmitterReferenceAmplitude", "ImageOrientationPatient", "SliceThickness", "RSatOrientationTra",
"PixelBandwidth", "SAR", "PixelSpacing", "ImagePositionPatient", "VoiPosition", "SliceLocation",
"FlipAngle", "VoiInPlaneRotation", "VoiPhaseFoV", "SliceMeasurementDuration", "HammingFilterWidth",
"RSatPositionTra", "MagneticFieldStrength", "VoiOrientation", "PercentSampling", "EchoTime",
"VoiReadoutFoV", "RSatThickness", "RSatOrientationCor", "ImagingFrequency", "TriggerTime", "dBdt",
"TransmitterCalibration", "PhaseGradientAmplitude", "ReadoutGradientAmplitude",
"SelectionGradientAmplitude", "GradientDelayTime", "dBdt_max", "t_puls_max", "dBdt_thresh",
"dBdt_limit", "SW_korr_faktor", "Stim_lim", "Stim_faktor"],
"integers": ["Rows", "Columns", "DataPointColumns", "SpectroscopyAcquisitionOut-of-planePhaseSteps",
"EchoPartitionPosition", "AcquisitionMatrix", "NumberOfFrames", "EchoNumbers", "RealDwellTime",
"EchoTrainLength", "EchoLinePosition", "EchoColumnPosition", "SpectroscopyAcquisitionDataColumns",
"SpectroscopyAcquisitionPhaseColumns", "SpectroscopyAcquisitionPhaseRows", "RfWatchdogMask",
"NumberOfPhaseEncodingSteps", "DataPointRows", "UsedPatientWeight", "NumberOfPrescans",
"Stim_mon_mode", "Operation_mode_flag", "CoilId", "MiscSequenceParam", "MrProtocolVersion",
"ProtocolSliceNumber"],
"strings": ["ReferencedImageSequence", "ScanningSequence", "SequenceName", "ImagedNucleus", "TransmittingCoil",
"PhaseEncodingDirection", "VariableFlipAngleFlag", "SequenceMask", "AcquisitionMatrixText",
"MultistepIndex", "DataRepresentation", "SignalDomainColumns", "k-spaceFiltering", "ResonantNucleus",
"ImaCoilString", "FrequencyCorrection", "WaterReferencedPhaseCorrection", "SequenceFileOwner",
"CoilForGradient", "CoilForGradient2", "PositivePCSDirections", ],
}
CSA1 = 0
CSA2 = 1
def read_csa_header(csa_header_bytes):
# two possibilities exist here, either this is a CSA2 format beginning with an SV10 string, or a CSA1 format which
# doesn't. in CSA2 after the "SV10" are four junk bytes, then the number of tags in a uint32 and a delimiter uint32
# containing the value 77. in CSA1 there is just the number of tags and the delimiter. after that the two formats
# contain the same structure for each tag, but the definition of the size of the items in each tag is different
# between the two versions
if csa_header_bytes[:4] == "SV10".encode('latin-1'):
num_tags, delimiter = struct.unpack("<II", csa_header_bytes[8:16])
header_offset = 16
header_format = CSA2
else:
num_tags, delimiter = struct.unpack("<II", csa_header_bytes[:8])
header_offset = 8
header_format = CSA1
# now we can iteratively read the tags and the items inside them
csa_header = {}
for i in range(num_tags):
name, vm, vr, syngo_dt, nitems, delimiter = struct.unpack("<64si4siii",
csa_header_bytes[header_offset:(header_offset + 84)])
header_offset += 84
# the name of the tag is 64 bytes long, but the string we want is null-terminated inside, so extract the
# real name by taking only bytes up until the first 0x00
name = name.decode('latin-1')
name = name.split("\x00", 1)[0]
# read all the items inside this tag
item_list = []
for j in range(nitems):
sizes = struct.unpack("<4L", csa_header_bytes[header_offset:(header_offset + 16)])
header_offset += 16
if header_format == CSA2:
item_length = sizes[1]
if (header_offset + item_length) > len(csa_header_bytes):
item_length = len(csa_header_bytes) - header_offset
elif header_format == CSA1:
item_length = sizes[0]
item, = struct.unpack("<%ds" % item_length,
csa_header_bytes[header_offset:(header_offset + item_length)])
item = item.decode('latin-1')
item = item.split("\x00", 1)[0]
if item_length > 0:
if name in ima_types["floats"]:
item = float(item)
elif name in ima_types["integers"]:
item = int(item)
elif name in ima_types["strings"]:
pass
else:
warnings.warn("Unhandled name {0} with vr {1} and value {2}".format(name, vr, item))
item_list.append(item)
header_offset += item_length
header_offset += (4 - (item_length % 4)) % 4 # move the offset to the next 4 byte boundary
if len(item_list) == 1:
item_list = item_list[0]
csa_header[name] = item_list
return csa_header
def header(file):
#dataset = pydicom.dicomio.read_file(file)
dataset = pydicom.dcmread(file)
xx = 0x0010
header_index = 0
while (0x0029, xx) in dataset:
if dataset[0x0029, xx].value == "SIEMENS CSA HEADER":
header_index = xx
xx += 1
# check that we have found the header
if header_index == 0:
raise KeyError("Could not find header index")
# now we know which tag contains the CSA image header info: (0029, xx10)
csa_header_bytes = dataset[0x0029, 0x0100 * header_index + 0x0010].value
return read_csa_header(csa_header_bytes)
def write_control(path,name,suffix):
with open(os.path.join(path,name+"_"+suffix+".control"), 'w') as f:
f.write(" $LCMODL\n")
f.write(" OWNER='DZNE Bonn'\n")
f.write(" key = 210387309\n")
f.write(" Title='%s'\n"%name)
f.write(" HZPPPM=2.972168e+02, DELTAT=3.600000e-04, NUNFIL=800\n")
f.write(" FILBAS='%s'\n"%os.path.join(os.path.dirname(os.path.abspath(__file__)),"basis","fid_1.300000ms.basis"))
f.write(" DOREFS(1) = T\n")
f.write(" DOREFS(2) = F\n")
f.write(" WSMET = 'DSS'\n")
f.write(" WSPPM = 0.0\n")
f.write(" N1HMET = 9\n")
f.write(" SUBBAS = T\n")
f.write(" NEACH = 99\n")
f.write(" WDLINE(6) = 0\n")
f.write(" PPMST = 4.2\n")
f.write(" PPMEND = 1.8\n")
f.write(" DEGZER = 0\n")
f.write(" SDDEGZ = 999\n")
f.write(" NDCOLS = 1\n")
f.write(" NDROWS = 1\n")
f.write(" NDSLIC = 1\n")
f.write(" DEGPPM = 0\n")
f.write(" SDDEGP = 1\n")
f.write(" NSIMUL = 0\n")
f.write(" NOMIT = 5\n")
f.write(" CHOMIT(1) = 'Cho'\n")
f.write(" CHOMIT(2) = 'Act'\n")
f.write(" CHOMIT(3) = 'mm3'\n")
f.write(" CHOMIT(4) = 'mm4'\n")
f.write(" CHOMIT(5) = 'Glc_B'\n")
f.write(" LTABLE = 7\n")
f.write(" LCSV = 0\n")
f.write(" LCOORD = 0\n")
f.write(" FILTAB='%s'\n" %os.path.join(path,name+"_"+suffix+".table"))
f.write(" FILRAW='%s'\n" %os.path.join(path,name+"_"+suffix+".RAW"))
f.write(" FILPS='%s'\n" %os.path.join(path,name+"_"+suffix+".ps"))
#f.write(" FILCOO='%s'\n" %"%s.coord"%os.path.join(path,name))
f.write(" $END\n")
def write_raw(path,name,suffix,data):
with open(os.path.join(path,name+"_"+suffix+".RAW"), 'w') as f:
f.write(" $SEQPAR\n")
f.write(" hzpppm=2.972169e+02\n")
f.write(" $END\n")
f.write(" $NMID\n")
f.write(" fmtdat='(2e14.5)'\n")
f.write(" $END\n")
for s in data[:800]:
f.write(" %.5e %.5e\n" % (s.real,s.imag))
class mrsi_data():
def __init__(self,path,site,sub,ses,name):
self.path = path
self.site = site
self.sub = sub
self.ses = ses
self.name = name
self.path_in = os.path.join(path,site,sub,ses,name)
self.path_mrsi = os.path.join(path,"derivatives",site,sub,ses,name)
self.path_maps = os.path.join(path,"derivatives",site,sub,ses,name,"maps")
self.path_lcm = os.path.join(path,"derivatives",site,sub,ses,name,"lcm")
print(self.path_lcm)
mkdir(self.path_mrsi)
mkdir(self.path_lcm)
mkdir(self.path_maps)
self.load_data()
def load_data(self):
load3D = []
zPos =[]
for f in sorted(os.listdir(self.path_in)):
if ".DS_Store" in f:
pass
elif "._" in f[:2]:
pass
else:
print(os.path.join(self.path_in,f))
self.head = header(os.path.join(self.path_in,f))
zPos.append(self.head["SliceLocation"])
data = suspect.io.load_siemens_dicom(os.path.join(self.path_in,f))
x = self.head["SpectroscopyAcquisitionPhaseColumns"]
load3D.append(np.reshape(np.array(data),(x,x,self.head["DataPointColumns"])))
load3D = np.array(load3D)
zPos = np.array(zPos)
print(load3D.shape)
load3D = load3D[np.argsort(zPos)]
self.shape = load3D.shape
self.shape = (self.shape[1],self.shape[2],self.shape[0])
self.data = filterHamming3D(load3D)
def create_dummyNII(self):
dummy = np.zeros(self.shape)
name = os.path.join(self.path_maps,"dummy.nii")
img = nib.Nifti1Image(dummy, np.identity(4))
nib.save(img, name)
img = ants.image_read(name)
(x,y,z) = self.head['VoiPosition']
self.spacing = (self.head["PixelSpacing"][0],self.head["PixelSpacing"][1],5.)
self.origin = (x+self.head["PixelSpacing"][0]*self.shape[0]/2,y+self.head["PixelSpacing"][0]*self.shape[1]/2,z-5*self.shape[2]/2)
img.set_origin(self.origin)
img.set_spacing(self.spacing)
ants.image_write(img,name)
return name
def write_lcm(self,mask):
brain = ants.image_read(mask)>.5
brain = brain[::-1,::-1]
#brain = np.ones(self.shape)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
for k in range(self.shape[2]):
suffix = "%i_%i_%i"%(i,j,k)
name = self.sub
title = "%s_%s"%(self.site,self.sub)
if brain[i,j,k]:
write_control(self.path_lcm,name,suffix)
write_raw(self.path_lcm,name,suffix,self.data[k,j,i])
print((i,j,k))
def call_lcm(self,p):
print("open call_lcm")
files = [os.path.join(self.path_lcm,f) for f in os.listdir(self.path_lcm) if ".control" in f]
print(self.path_lcm)
print(len(files))
with Pool(p) as P:
P.map(fit, files)
def save(self,name,data):
img = nib.Nifti1Image(data, np.identity(4))
nib.save(img, name)
nii = ants.image_read(name)
nii.set_origin(self.origin)
nii.set_spacing(self.spacing)
ants.image_write(nii,name)
def save_nii(self):
self.NAA = np.zeros(self.shape)
self.CR = np.zeros(self.shape)
self.GABA = np.zeros(self.shape)
self.GLX = np.zeros(self.shape)
self.CHO = np.zeros(self.shape)
self.THG = np.zeros(self.shape)
self.INS = np.zeros(self.shape)
self.ASP = np.zeros(self.shape)
self.TAU = np.zeros(self.shape)
self.SNR = np.zeros(self.shape)
self.FWHM = np.zeros(self.shape)
name = self.sub
for x in range(self.shape[0]):
for y in range(self.shape[1]):
for z in range(self.shape[2]):
try:
t = open("%s/%s_%i_%i_%i.table"%(self.path_lcm,name,x,y,z),"r")
c = t.readlines()
for line in c:
if "NAA+NAAG" in line:
self.NAA[x,y,z] = float(line.split()[0])
elif " Cr+PCr" in line:
self.CR[x,y,z] = float(line.split()[0])
elif "GABA" in line:
line =line.replace("+GABA"," GABA")
line =line.replace("-GABA"," GABA")
self.GABA[x,y,z] = float(line.split()[0])
elif "Glu+Gln" in line:
self.GLX[x,y,z] = float(line.split()[0])
elif "GPC+PCh" in line:
self.CHO[x,y,z] = float(line.split()[0])
elif "TwoHG" in line:
self.THG[x,y,z] = float(line.split()[0])
elif "Ins" in line:
self.INS[x,y,z] = float(line.split()[0])
elif "Asp" in line:
line =line.replace("+Asp"," Asp")
line =line.replace("-Asp"," Asp")
self.ASP[x,y,z] = float(line.split()[0])
elif "Tau" in line:
self.TAU[x,y,z] = float(line.split()[0])
elif "S/N =" in line:
self.SNR[x,y,z] = float(line.split()[-1])
self.FWHM[x,y,z] = float(line.split()[2])
except:
pass
prefix = "%s_%s_%s_%s_" % (self.site,self.sub,self.ses,self.name)
self.save(os.path.join(self.path_maps,prefix+"tnaa.nii"), self.NAA)
self.save(os.path.join(self.path_maps,prefix+"tcr.nii"), self.CR)
self.save(os.path.join(self.path_maps,prefix+"gaba.nii"), self.GABA)
self.save(os.path.join(self.path_maps,prefix+"glx.nii"), self.GLX)
self.save(os.path.join(self.path_maps,prefix+"tcho.nii"), self.CHO)
self.save(os.path.join(self.path_maps,prefix+"2HG.nii"), self.THG)
self.save(os.path.join(self.path_maps,prefix+"ins.nii"), self.INS)
self.save(os.path.join(self.path_maps,prefix+"asp.nii"), self.ASP)
self.save(os.path.join(self.path_maps,prefix+"tau.nii"), self.TAU)
self.save(os.path.join(self.path_maps,prefix+"snr.nii"), self.SNR)
self.save(os.path.join(self.path_maps,prefix+"fwhm.nii"), self.FWHM)