Browse code

searchlight ready. missing nifti-image-write support. added timing to FBS

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@167 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on30/03/2009 17:54:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,255 @@
1
+%  Decode extra NIFTI header information into hdr.extra
2
+%
3
+%  Usage: hdr = extra_nii_hdr(hdr)
4
+%
5
+%  hdr can be obtained from load_nii_hdr
6
+%
7
+%  NIFTI data format can be found on: http://nifti.nimh.nih.gov
8
+%
9
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
10
+%
11
+function hdr = extra_nii_hdr(hdr)
12
+
13
+   switch hdr.dime.datatype
14
+   case 1
15
+      extra.NIFTI_DATATYPES = 'DT_BINARY';
16
+   case 2
17
+      extra.NIFTI_DATATYPES = 'DT_UINT8';
18
+   case 4
19
+      extra.NIFTI_DATATYPES = 'DT_INT16';
20
+   case 8
21
+      extra.NIFTI_DATATYPES = 'DT_INT32';
22
+   case 16
23
+      extra.NIFTI_DATATYPES = 'DT_FLOAT32';
24
+   case 32
25
+      extra.NIFTI_DATATYPES = 'DT_COMPLEX64';
26
+   case 64
27
+      extra.NIFTI_DATATYPES = 'DT_FLOAT64';
28
+   case 128
29
+      extra.NIFTI_DATATYPES = 'DT_RGB24';
30
+   case 256
31
+      extra.NIFTI_DATATYPES = 'DT_INT8';
32
+   case 512
33
+      extra.NIFTI_DATATYPES = 'DT_UINT16';
34
+   case 768
35
+      extra.NIFTI_DATATYPES = 'DT_UINT32';
36
+   case 1024
37
+      extra.NIFTI_DATATYPES = 'DT_INT64';
38
+   case 1280
39
+      extra.NIFTI_DATATYPES = 'DT_UINT64';
40
+   case 1536
41
+      extra.NIFTI_DATATYPES = 'DT_FLOAT128';
42
+   case 1792
43
+      extra.NIFTI_DATATYPES = 'DT_COMPLEX128';
44
+   case 2048
45
+      extra.NIFTI_DATATYPES = 'DT_COMPLEX256';
46
+   otherwise
47
+      extra.NIFTI_DATATYPES = 'DT_UNKNOWN';
48
+   end
49
+
50
+   switch hdr.dime.intent_code
51
+   case 2
52
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CORREL';
53
+   case 3
54
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TTEST';
55
+   case 4
56
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_FTEST';
57
+   case 5
58
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_ZSCORE';
59
+   case 6
60
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHISQ';
61
+   case 7
62
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_BETA';
63
+   case 8
64
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_BINOM';
65
+   case 9
66
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_GAMMA';
67
+   case 10
68
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_POISSON';
69
+   case 11
70
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NORMAL';
71
+   case 12
72
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_FTEST_NONC';
73
+   case 13
74
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHISQ_NONC';
75
+   case 14
76
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOGISTIC';
77
+   case 15
78
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LAPLACE';
79
+   case 16
80
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_UNIFORM';
81
+   case 17
82
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TTEST_NONC';
83
+   case 18
84
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_WEIBULL';
85
+   case 19
86
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHI';
87
+   case 20
88
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_INVGAUSS';
89
+   case 21
90
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_EXTVAL';
91
+   case 22
92
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_PVAL';
93
+   case 23
94
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOGPVAL';
95
+   case 24
96
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOG10PVAL';
97
+   case 1001
98
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_ESTIMATE';
99
+   case 1002
100
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LABEL';
101
+   case 1003
102
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NEURONAME';
103
+   case 1004
104
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_GENMATRIX';
105
+   case 1005
106
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_SYMMATRIX';
107
+   case 1006
108
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_DISPVECT';
109
+   case 1007
110
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_VECTOR';
111
+   case 1008
112
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_POINTSET';
113
+   case 1009
114
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TRIANGLE';
115
+   case 1010
116
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_QUATERNION';
117
+   case 1011
118
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_DIMLESS';
119
+   otherwise
120
+      extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NONE';
121
+   end
122
+
123
+   extra.NIFTI_INTENT_NAMES = hdr.hist.intent_name;
124
+
125
+   if hdr.hist.sform_code > 0
126
+      switch hdr.hist.sform_code
127
+      case 1
128
+         extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_SCANNER_ANAT';
129
+      case 2
130
+         extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_ALIGNED_ANAT';
131
+      case 3
132
+         extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_TALAIRACH';
133
+      case 4
134
+         extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_MNI_152';
135
+      otherwise
136
+         extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
137
+      end
138
+
139
+      extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
140
+   elseif hdr.hist.qform_code > 0
141
+      extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
142
+
143
+      switch hdr.hist.qform_code
144
+      case 1
145
+         extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_SCANNER_ANAT';
146
+      case 2
147
+         extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_ALIGNED_ANAT';
148
+      case 3
149
+         extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_TALAIRACH';
150
+      case 4
151
+         extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_MNI_152';
152
+      otherwise
153
+         extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
154
+      end
155
+   else
156
+      extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
157
+      extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
158
+   end
159
+
160
+   switch bitand(hdr.dime.xyzt_units, 7)	% mask with 0x07
161
+   case 1
162
+      extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_METER';
163
+   case 2
164
+      extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_MM';	% millimeter
165
+   case 3
166
+      extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_MICRO';
167
+   otherwise
168
+      extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_UNKNOWN';
169
+   end
170
+
171
+   switch bitand(hdr.dime.xyzt_units, 56)	% mask with 0x38
172
+   case 8
173
+      extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_SEC';
174
+   case 16
175
+      extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_MSEC';
176
+   case 24
177
+      extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_USEC';	% microsecond
178
+   otherwise
179
+      extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_UNKNOWN';
180
+   end
181
+
182
+   switch hdr.dime.xyzt_units
183
+   case 32
184
+      extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_HZ';
185
+   case 40
186
+      extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_PPM';	% part per million
187
+   case 48
188
+      extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_RADS';	% radians per second
189
+   otherwise
190
+      extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_UNKNOWN';
191
+   end
192
+
193
+   %  MRI-specific spatial and temporal information
194
+   %
195
+   dim_info = hdr.hk.dim_info;
196
+   extra.NIFTI_FREQ_DIM = bitand(dim_info, 3);
197
+   extra.NIFTI_PHASE_DIM = bitand(bitshift(dim_info, -2), 3);
198
+   extra.NIFTI_SLICE_DIM = bitand(bitshift(dim_info, -4), 3);
199
+
200
+   %  Check slice code
201
+   %
202
+   switch hdr.dime.slice_code
203
+   case 1
204
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_SEQ_INC';	% sequential increasing
205
+   case 2
206
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_SEQ_DEC';	% sequential decreasing
207
+   case 3
208
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_INC';	% alternating increasing
209
+   case 4
210
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_DEC';	% alternating decreasing
211
+   case 5
212
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_INC2';	% ALT_INC # 2
213
+   case 6
214
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_DEC2';	% ALT_DEC # 2
215
+   otherwise
216
+      extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_UNKNOWN';
217
+   end
218
+
219
+   %  Check NIFTI version
220
+   %
221
+   if     ~isempty(hdr.hist.magic) & strcmp(hdr.hist.magic(1),'n') & ...
222
+	( strcmp(hdr.hist.magic(2),'i') | strcmp(hdr.hist.magic(2),'+') ) & ...
223
+	  str2num(hdr.hist.magic(3)) >= 1 & str2num(hdr.hist.magic(3)) <= 9
224
+
225
+      extra.NIFTI_VERSION = str2num(hdr.hist.magic(3));
226
+   else
227
+      extra.NIFTI_VERSION = 0;
228
+   end
229
+
230
+   %  Check if data stored in the same file (*.nii) or separate
231
+   %  files (*.hdr/*.img)
232
+   %
233
+   if isempty(hdr.hist.magic)
234
+      extra.NIFTI_ONEFILE = 0;
235
+   else
236
+      extra.NIFTI_ONEFILE = strcmp(hdr.hist.magic(2), '+');
237
+   end
238
+
239
+   %  Swap has been taken care of by checking whether sizeof_hdr is
240
+   %  348 (machine is 'ieee-le' or 'ieee-be' etc)
241
+   %
242
+   % extra.NIFTI_NEEDS_SWAP = (hdr.dime.dim(1) < 0 | hdr.dime.dim(1) > 7);
243
+
244
+   %  Check NIFTI header struct contains a 5th (vector) dimension
245
+   %
246
+   if hdr.dime.dim(1) > 4 & hdr.dime.dim(6) > 1
247
+      extra.NIFTI_5TH_DIM = hdr.dime.dim(6);
248
+   else
249
+      extra.NIFTI_5TH_DIM = 0;
250
+   end
251
+
252
+   hdr.extra = extra;
253
+
254
+   return;					% extra_nii_hdr
255
+