Skip to content

Commit 7f60205

Browse files
committed
Merge pull request nipy#277 from matthew-brett/parrec-v4-support
MRG: add support for V4, 4.1 of PARREC format Versions 4 and 4.1 seem to differ only in in lacking a few fields of the 4.2 PAR header.
2 parents b5fe3fe + fa70db8 commit 7f60205

File tree

4 files changed

+298
-20
lines changed

4 files changed

+298
-20
lines changed

nibabel/parrec.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
This is yet another MRI image format generated by Philips scanners. It is an
1212
ASCII header (PAR) plus a binary blob (REC).
1313
14-
This implementation aims to read version 4.2 of this format. Other versions
15-
could probably be supported, but the author is lacking samples of them.
14+
This implementation aims to read version 4 and 4.2 of this format. Other
15+
versions could probably be supported, but we need example images to test
16+
against. If you want us to support another version, and have an image we can
17+
add to the test suite, let us know. You would make us very happy by submitting
18+
a pull request.
1619
1720
###############
1821
PAR file format
@@ -120,8 +123,6 @@
120123
[1, 0, 0, 0], # L
121124
[0, 0, 0, 1]])
122125
)
123-
# PAR header versions we claim to understand
124-
supported_versions = ['V4.2']
125126

126127
# General information dict definitions
127128
# assign props to PAR header entries
@@ -159,14 +160,17 @@
159160
'Dynamic scan <0=no 1=yes> ?': ('dyn_scan', int),
160161
'Diffusion <0=no 1=yes> ?': ('diffusion', int),
161162
'Diffusion echo time [ms]': ('diffusion_echo_time', float),
163+
# Lines below added for par / rec versions > 4
162164
'Max. number of diffusion values': ('max_diffusion_values', int),
163165
'Max. number of gradient orients': ('max_gradient_orient', int),
166+
# Line below added for par / rec version > 4.1
164167
'Number of label types <0=no ASL>': ('nr_label_types', int),
165168
}
166169

167170
# Image information as coded into a numpy structured array
168171
# header items order per image definition line
169-
image_def_dtd = [
172+
image_def_dtds = {}
173+
image_def_dtds['V4'] = [
170174
('slice number', int),
171175
('echo number', int,),
172176
('dynamic scan number', int,),
@@ -201,17 +205,29 @@
201205
('minimum RR-interval', int,),
202206
('maximum RR-interval', int,),
203207
('TURBO factor', int,),
204-
('Inversion delay', float),
208+
('Inversion delay', float)]
209+
210+
# Extra image def fields for 4.1 compared to 4
211+
image_def_dtds['V4.1'] = image_def_dtds['V4'] + [
205212
('diffusion b value number', int,), # (imagekey!)
206213
('gradient orientation number', int,), # (imagekey!)
207214
('contrast type', 'S30'), # XXX might be too short?
208215
('diffusion anisotropy type', 'S30'), # XXX might be too short?
209216
('diffusion', float, (3,)),
210-
('label type', int,), # (imagekey!)
211217
]
212-
image_def_dtype = np.dtype(image_def_dtd)
213218

214-
# slice orientation codes
219+
# Extra image def fields for 4.2 compared to 4.1
220+
image_def_dtds['V4.2'] = image_def_dtds['V4.1'] + [
221+
('label type', int,), # (imagekey!)
222+
]
223+
224+
#: PAR header versions we claim to understand
225+
supported_versions = list(image_def_dtds.keys())
226+
227+
#: Deprecated; please don't use
228+
image_def_dtype = np.dtype(image_def_dtds['V4.2'])
229+
230+
#: slice orientation codes
215231
slice_orientation_codes = Recoder(( # code, label
216232
(1, 'transverse'),
217233
(2, 'sagittal'),
@@ -281,12 +297,13 @@ def _process_gen_dict(gen_dict):
281297
return general_info
282298

283299

284-
def _process_image_lines(image_lines):
285-
""" Process image information definition lines
300+
def _process_image_lines(image_lines, version):
301+
""" Process image information definition lines according to `version`
286302
"""
287303
# postproc image def props
304+
image_def_dtd = image_def_dtds[version]
288305
# create an array for all image defs
289-
image_defs = np.zeros(len(image_lines), dtype=image_def_dtype)
306+
image_defs = np.zeros(len(image_lines), dtype=image_def_dtd)
290307
# for every image definition
291308
for i, line in enumerate(image_lines):
292309
items = line.split()
@@ -385,6 +402,8 @@ def _err_or_warn(msg):
385402
warnings.warn(msg)
386403

387404
def _chk_trunc(idef_name, gdef_max_name):
405+
if not gdef_max_name in general_info:
406+
return
388407
id_values = image_defs[idef_name + ' number']
389408
n_have = len(set(id_values))
390409
n_expected = general_info[gdef_max_name]
@@ -435,7 +454,7 @@ def parse_PAR_header(fobj):
435454
list, if you are interested in adding support for this version.
436455
""".format(version)))
437456
general_info = _process_gen_dict(gen_dict)
438-
image_defs = _process_image_lines(image_lines)
457+
image_defs = _process_image_lines(image_lines, version)
439458
return general_info, image_defs
440459

441460

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# === DATA DESCRIPTION FILE ======================================================
2+
#
3+
# CAUTION - Investigational device.
4+
# Limited by Federal Law to investigational use.
5+
#
6+
# Dataset name: E:\\Export\phantom_EPI_asc_CLEAR_2_1
7+
#
8+
# CLINICAL TRYOUT Research image export tool V4
9+
#
10+
# === GENERAL INFORMATION ========================================================
11+
#
12+
. Patient name : phantom
13+
. Examination name : Konvertertest
14+
. Protocol name : EPI_asc CLEAR
15+
. Examination date/time : 2014.02.14 / 09:00:57
16+
. Series Type : Image MRSERIES
17+
. Acquisition nr : 2
18+
. Reconstruction nr : 1
19+
. Scan Duration [sec] : 14
20+
. Max. number of cardiac phases : 1
21+
. Max. number of echoes : 1
22+
. Max. number of slices/locations : 9
23+
. Max. number of dynamics : 3
24+
. Max. number of mixes : 1
25+
. Patient position : Head First Supine
26+
. Preparation direction : Anterior-Posterior
27+
. Technique : FEEPI
28+
. Scan resolution (x, y) : 64 39
29+
. Scan mode : MS
30+
. Repetition time [ms] : 2000.000
31+
. FOV (ap,fh,rl) [mm] : 240.000 70.000 240.000
32+
. Water Fat shift [pixels] : 11.050
33+
. Angulation midslice(ap,fh,rl)[degr]: -13.265 0.000 0.000
34+
. Off Centre midslice(ap,fh,rl) [mm] : 2.508 30.339 -16.032
35+
. Flow compensation <0=no 1=yes> ? : 0
36+
. Presaturation <0=no 1=yes> ? : 0
37+
. Phase encoding velocity [cm/sec] : 0.000000 0.000000 0.000000
38+
. MTC <0=no 1=yes> ? : 0
39+
. SPIR <0=no 1=yes> ? : 1
40+
. EPI factor <0,1=no EPI> : 39
41+
. Dynamic scan <0=no 1=yes> ? : 1
42+
. Diffusion <0=no 1=yes> ? : 0
43+
. Diffusion echo time [ms] : 0.0000
44+
#
45+
# === PIXEL VALUES =============================================================
46+
# PV = pixel value in REC file, FP = floating point value, DV = displayed value on console
47+
# RS = rescale slope, RI = rescale intercept, SS = scale slope
48+
# DV = PV * RS + RI FP = DV / (RS * SS)
49+
#
50+
# === IMAGE INFORMATION DEFINITION =============================================
51+
# The rest of this file contains ONE line per image, this line contains the following information:
52+
#
53+
# slice number (integer)
54+
# echo number (integer)
55+
# dynamic scan number (integer)
56+
# cardiac phase number (integer)
57+
# image_type_mr (integer)
58+
# scanning sequence (integer)
59+
# index in REC file (in images) (integer)
60+
# image pixel size (in bits) (integer)
61+
# scan percentage (integer)
62+
# recon resolution (x y) (2*integer)
63+
# rescale intercept (float)
64+
# rescale slope (float)
65+
# scale slope (float)
66+
# window center (integer)
67+
# window width (integer)
68+
# image angulation (ap,fh,rl in degrees ) (3*float)
69+
# image offcentre (ap,fh,rl in mm ) (3*float)
70+
# slice thickness (in mm ) (float)
71+
# slice gap (in mm ) (float)
72+
# image_display_orientation (integer)
73+
# slice orientation ( TRA/SAG/COR ) (integer)
74+
# fmri_status_indication (integer)
75+
# image_type_ed_es (end diast/end syst) (integer)
76+
# pixel spacing (x,y) (in mm) (2*float)
77+
# echo_time (float)
78+
# dyn_scan_begin_time (float)
79+
# trigger_time (float)
80+
# diffusion_b_factor (float)
81+
# number of averages (integer)
82+
# image_flip_angle (in degrees) (float)
83+
# cardiac frequency (bpm) (integer)
84+
# minimum RR-interval (in ms) (integer)
85+
# maximum RR-interval (in ms) (integer)
86+
# TURBO factor <0=no turbo> (integer)
87+
# Inversion delay (in ms) (float)
88+
#
89+
# === IMAGE INFORMATION ==========================================================
90+
# sl ec dyn ph ty idx pix scan% rec size (re)scale window angulation offcentre thick gap info spacing echo dtime ttime diff avg flip freq RR-int turbo delay
91+
92+
1 1 1 1 0 2 0 16 62 64 64 0.00000 1.29035 4.28404e-003 1070 1860 -13.26 -0.00 -0.00 2.51 -0.81 -8.69 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
93+
2 1 1 1 0 2 1 16 62 64 64 0.00000 1.29035 4.28404e-003 1122 1951 -13.26 -0.00 -0.00 2.51 6.98 -10.53 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
94+
3 1 1 1 0 2 2 16 62 64 64 0.00000 1.29035 4.28404e-003 1137 1977 -13.26 -0.00 -0.00 2.51 14.77 -12.36 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
95+
4 1 1 1 0 2 3 16 62 64 64 0.00000 1.29035 4.28404e-003 1217 2116 -13.26 -0.00 -0.00 2.51 22.55 -14.20 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
96+
5 1 1 1 0 2 4 16 62 64 64 0.00000 1.29035 4.28404e-003 1216 2113 -13.26 -0.00 -0.00 2.51 30.34 -16.03 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
97+
6 1 1 1 0 2 5 16 62 64 64 0.00000 1.29035 4.28404e-003 1141 1983 -13.26 -0.00 -0.00 2.51 38.13 -17.87 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
98+
7 1 1 1 0 2 6 16 62 64 64 0.00000 1.29035 4.28404e-003 1119 1945 -13.26 -0.00 -0.00 2.51 45.91 -19.70 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
99+
8 1 1 1 0 2 7 16 62 64 64 0.00000 1.29035 4.28404e-003 1097 1907 -13.26 -0.00 -0.00 2.51 53.70 -21.54 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
100+
9 1 1 1 0 2 8 16 62 64 64 0.00000 1.29035 4.28404e-003 1146 1991 -13.26 -0.00 -0.00 2.51 61.49 -23.37 6.000 2.000 0 1 0 2 3.750 3.750 30.00 0.00 0.00 0.00 0 90.00 0 0 0 39 0.0
101+
1 1 2 1 0 2 9 16 62 64 64 0.00000 1.29035 4.28404e-003 1071 1863 -13.26 -0.00 -0.00 2.51 -0.81 -8.69 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
102+
2 1 2 1 0 2 10 16 62 64 64 0.00000 1.29035 4.28404e-003 1123 1953 -13.26 -0.00 -0.00 2.51 6.98 -10.53 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
103+
3 1 2 1 0 2 11 16 62 64 64 0.00000 1.29035 4.28404e-003 1135 1973 -13.26 -0.00 -0.00 2.51 14.77 -12.36 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
104+
4 1 2 1 0 2 12 16 62 64 64 0.00000 1.29035 4.28404e-003 1209 2101 -13.26 -0.00 -0.00 2.51 22.55 -14.20 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
105+
5 1 2 1 0 2 13 16 62 64 64 0.00000 1.29035 4.28404e-003 1215 2113 -13.26 -0.00 -0.00 2.51 30.34 -16.03 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
106+
6 1 2 1 0 2 14 16 62 64 64 0.00000 1.29035 4.28404e-003 1145 1990 -13.26 -0.00 -0.00 2.51 38.13 -17.87 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
107+
7 1 2 1 0 2 15 16 62 64 64 0.00000 1.29035 4.28404e-003 1119 1945 -13.26 -0.00 -0.00 2.51 45.91 -19.70 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
108+
8 1 2 1 0 2 16 16 62 64 64 0.00000 1.29035 4.28404e-003 1093 1899 -13.26 -0.00 -0.00 2.51 53.70 -21.54 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
109+
9 1 2 1 0 2 17 16 62 64 64 0.00000 1.29035 4.28404e-003 1150 1999 -13.26 -0.00 -0.00 2.51 61.49 -23.37 6.000 2.000 0 1 0 2 3.750 3.750 30.00 2.00 0.00 0.00 0 90.00 0 0 0 39 0.0
110+
1 1 3 1 0 2 18 16 62 64 64 0.00000 1.29035 4.28404e-003 1070 1860 -13.26 -0.00 -0.00 2.51 -0.81 -8.69 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
111+
2 1 3 1 0 2 19 16 62 64 64 0.00000 1.29035 4.28404e-003 1125 1955 -13.26 -0.00 -0.00 2.51 6.98 -10.53 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
112+
3 1 3 1 0 2 20 16 62 64 64 0.00000 1.29035 4.28404e-003 1135 1973 -13.26 -0.00 -0.00 2.51 14.77 -12.36 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
113+
4 1 3 1 0 2 21 16 62 64 64 0.00000 1.29035 4.28404e-003 1211 2105 -13.26 -0.00 -0.00 2.51 22.55 -14.20 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
114+
5 1 3 1 0 2 22 16 62 64 64 0.00000 1.29035 4.28404e-003 1218 2118 -13.26 -0.00 -0.00 2.51 30.34 -16.03 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
115+
6 1 3 1 0 2 23 16 62 64 64 0.00000 1.29035 4.28404e-003 1143 1987 -13.26 -0.00 -0.00 2.51 38.13 -17.87 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
116+
7 1 3 1 0 2 24 16 62 64 64 0.00000 1.29035 4.28404e-003 1120 1947 -13.26 -0.00 -0.00 2.51 45.91 -19.70 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
117+
8 1 3 1 0 2 25 16 62 64 64 0.00000 1.29035 4.28404e-003 1093 1901 -13.26 -0.00 -0.00 2.51 53.70 -21.54 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
118+
9 1 3 1 0 2 26 16 62 64 64 0.00000 1.29035 4.28404e-003 1151 2001 -13.26 -0.00 -0.00 2.51 61.49 -23.37 6.000 2.000 0 1 0 2 3.750 3.750 30.00 4.00 0.00 0.00 0 90.00 0 0 0 39 0.0
119+
120+
# === END OF DATA DESCRIPTION FILE ===============================================

0 commit comments

Comments
 (0)