forked from LauraDugue/endo_exo_MRI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
percentTSeries.m
284 lines (256 loc) · 8.37 KB
/
percentTSeries.m
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
function ptSeries = percentTSeries(tSeries, varargin)
%
% $Id$
%
% ptSeries = percentTSeries(tSeries, [param1], [value1], [param2], [value2])
%
% 1) Optional temporal normalization.
% 2) Optional spatial normalization.
% 3) Optional temporal detrending.
% 4) Optional subtract mean (note that this may have already happened
% depending on the choice of temporal detrending).
%
% Valid params are:
% 'detrend'
% 'highpassPeriod'
% 'spatialNormalization'
% 'spatialNorm'
% 'subtractMean',
% 'temporalNormalization'
%
% Values for detrend:
% 'None' no trend removal
% 'Highpass' highpass trend removal (using 'highpassPeriod')
% 'Linear' linear trend removal
% 'Quadratic' quadratic removal
% Default: 'Highpass'
%
% 'highpassPeriod' controls the highpass filtering. For a periodic or
% block alternation experiment, highpassPeriod should be equal to the number
% of frames in the block period.
% Default: nFrames/6 (number of frames in tSeries divided by 6)
%
% Values for spatialNormalization (e.g., to to compensate coil sensitivity):
% 'None' do nothing
% 'Divide by mean' divide by the mean, independently at each voxel
% 'Arbitrary' (using 'spatialNorm')
% Default: 'Divide by mean'
%
% 'spatialNorm' must be an array that has the same number of elements as the
% number of voxels in the tSeries.
% Default: mean(tSeries)
%
% Values for subtractMean:
% 'No'
% 'Yes'
% Default: 'Yes'
%
% Values for temporalNormalization that divides each temporal frame by its
% mean:
% 'No'
% 'Yes'
% Default: 'No'
%
% djh, 1/22/98
% djh, 7/7/2004 Updated to mrLoadRet 4.0
% Get nVoxels
[nFrames, nVoxels] = size(tSeries);
% Parse varargin to get parameters and values
for index = 1:2:length(varargin)
field = varargin{index};
val = varargin{index+1};
switch field
case 'detrend'
detrend = val;
case 'spatialNormalization'
spatialNormalization = val;
case 'subtractMean'
subtractMean = val;
case 'temporalNormalization'
temporalNormalization = val;
case 'highpassPeriod'
if ~isnumeric(val) | (size(val) ~= 1)
mrErrorDlg(['Invalid highpassPeriod option: ',val]);
else
highpassPeriod = val;
end
case 'spatialNorm'
if ~isnumeric(val) | (prod(size(val)) ~= nVoxels)
mrErrorDlg(['Invalid normalizationVol option.']);
else
% reshape into a row vector
spatialNorm = val(:)';
end
otherwise
warning('percentTSeries: invalid parameter')
end
end
if ieNotDefined('detrend')
detrend = 'Highpass';
end
if ieNotDefined('subtractMean')
subtractMean = 'Yes';
end
if ieNotDefined('spatialNormalization')
spatialNormalization = 'Divide by mean';
end
if ieNotDefined('temporalNormalization')
temporalNormalization = 'No';
end
% If detrend = 'Highpass' then highpassPeriod must be set to something.
% Use nFrames/6 as the default.
if strcmp(detrend,'Highpass')
if ieNotDefined('highpassPeriod')
highpassPeriod = round(nFrames/6);
end
end
% If spatialNormalization = 'Aribrary' then spatialNorm must be set to
% something. Use mean of tSeries as the default.
if strcmp(spatialNormalization,'Arbitrary')
if ieNotDefined('spatialNorm')
spatialNorm = mean(tSeries);
end
end
% Temporal normalization divides each frame by its mean.
% Added by ARW
%
switch temporalNormalization
case 'No'
% do nothing
case 'Yes'
disp('Temporal normalization to first frame');
% Mean of each frame
meanFrames = mean(tSeries,2);
disp(sprintf('Mean tseries value of the first frame %.05f\n',meanFrames(1)));
tSeries = (tSeries ./ repmat(meanFrames,1,nVoxels)) * meanFrames(1);
otherwise
mrErrorDlg(['Invalid termporalNormalization option: ',temporalNormalization]);
end
% Divide by either the mean or whatever you passed in as spatialNormalization
%
switch spatialNormalization
case 'None'
ptSeries = tSeries;
case 'Divide by mean'
dc = mean(tSeries);
if any(dc<0)
mrWarnDlg('(percentTSeries) Dividing some time-series by a negative value during spatial normalization. Is that really what you want ?')
end
ptSeries = tSeries./(ones(nFrames,1)*dc);
case 'Arbitrary'
ptSeries = tSeries./(ones(nFrames,1)*spatialNorm);
otherwise
mrErrorDlg(['Invalid spatialNormalization option: ',spatialNormalization]);
end
% Remove trend
%
switch detrend
case 'None'
% Do nothing
case 'Highpass'
% Do high-pass baseline removal
ptSeries = removeBaseline(ptSeries, highpassPeriod);
case 'Linear'
% remove a linear function
model = [(1:nFrames);ones(1,nFrames)]';
wgts = model \ ptSeries;
fit = model*wgts;
ptSeries = ptSeries - fit;
case 'Quadratic'
% remove a quadratic function
model = [(1:nFrames).*(1:nFrames);(1:nFrames);ones(1,nFrames)]';
wgts = model \ ptSeries;
fit = model*wgts;
ptSeries = ptSeries - fit;
otherwise
mrErrorDlg(['Invalid detrend option: ',detrend]);
end
% Subtract the mean
% Used to just subtract 1 under the assumption that we had already divided by
% the mean, but now with the spatialGrad option the mean may not be exactly 1.
%
switch subtractMean
case 'No'
% Do nothing
case 'Yes'
ptSeries = ptSeries - ones(nFrames,1)*mean(ptSeries);
if ~strcmp(spatialNormalization,'None')
% Multiply by 100 to get percent
ptSeries = 100*ptSeries;
end
otherwise
mrErrorDlg(['Invalid subtractMean option: ',subtractMean]);
end
return%
%%%%%%%%%%%%%%
% Test/debug %
%%%%%%%%%%%%%%
nFrames = 100;
nVoxels = 10;
baseline = 100+linspace(-10,10,nFrames)';
tSeries = 10 * (randn(nFrames,nVoxels) + repmat(baseline,1,nVoxels));
figure(1)
plot(tSeries)
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','None','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','Linear',...
'spatialNormalization','None','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','Quadratic',...
'spatialNormalization','None','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
% Stupid combination
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','None','subtractMean','Yes',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','Divide by mean','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','Divide by mean','subtractMean','Yes',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','Linear',...
'spatialNormalization','Divide by mean','subtractMean','Yes',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','None','subtractMean','No',...
'temporalNormalization','Yes');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','Arbitrary','spatialNorm',10*[1:nVoxels],...
'subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','Linear',...
'spatialNormalization','Arbitrary','spatialNorm',mean(tSeries),...
'subtractMean','Yes',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,'detrend','None',...
'spatialNormalization','Arbitrary','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,...
'detrend','Highpass','highpassPeriod',round(nFrames/10),...
'spatialNormalization','None','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
ptSeries = percentTSeries(tSeries,...
'detrend','Highpass',...
'spatialNormalization','None','subtractMean','No',...
'temporalNormalization','No');
plot(ptSeries)
% Default
ptSeries = percentTSeries(tSeries,...
'detrend','Highpass','highpassPeriod',round(nFrames/6),...
'spatialNormalization','Divide by mean','subtractMean','Yes',...
'temporalNormalization','No');
plot(ptSeries)