-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolTsGetResp.m
220 lines (176 loc) · 7.57 KB
/
volTsGetResp.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
function [out, info] = volTsGetResp(do,info,volTs,volAnat,force)
if isempty(do)
do.loadIt = 0;
do.doIt = 1;
do.saveIt = 0;
end
if ~exist('force','var'); force = []; end
if ~exist('volAnat','var'); volAnat = []; end
if isempty(force); force = 0; end
% if ~isfield(info,'K'); info.K = [] ; end
% if ~isfield(info,'win'); info.win = zeros(0,2); end
% if ~isfield(info,'skipSvd'); info.skipSvd = 0 ; end
% if ~isfield(info,'dtrndOrder'); info.dtrndOrder = [] ; end
% if ~isfield(info,'onsets'); info.onsets = [] ; end
% if isempty(info.onsets); info.onsets = [] ; end
% if ~isfield(info,'ondurList'); info.ondurList = [] ; end
if ~isfield(volTs,'dsgn'); volTs.dsgn = [] ; end
if ~isempty(volTs.dsgn)
if isfield(volTs.dsgn,'onsets')
onsets = volTs.dsgn.onsets;
else
onsets = volTs.dsgn.onsetList;
end
end
if ~isempty(volTs.dsgn)
if isfield(volTs.dsgn,'ondurs')
ondurs = volTs.dsgn.ondurs;
else
ondurs = volTs.dsgn.ondurList;
end
end
%% User variables
outVar = 'volResp';
stepLabel = 'event-related response processing';
%%%%%%%%%%%%%%%%
%% House keeping
%%%%%%%%%%%%%%%%
if isfield(info,'outDir'); outDir = info.outDir; else, outDir = info.preprocDir; end; if ~exist(outDir,'dir'); mkdir(outDir); end; stepFile = fullfile(outDir,[strjoin({['sub-' info.sub] ['ses-' info.ses] mfilename},'_')]);
if exist('do','var') && ~isempty(do)
if isfield(do,'loadIt') && ~isempty(do.loadIt); loadIt = do.loadIt; else, loadIt=0; end
if isfield(do,'doIt') && ~isempty(do.doIt); doIt = do.doIt; else, doIt=0; end
if isfield(do,'saveIt') && ~isempty(do.saveIt); saveIt = do.saveIt; else, saveIt=0; end
else
loadIt = 0; doIt = 1; saveIt = 0;
end
if loadIt && doIt
warning(strjoin({'does not make sense to loadIt then doIt' 'Changing to' 'loadIt = 0' 'doIt = 1' 'saveIt = 1'},newline)); loadIt = 0; doIt = 1; saveIt = 1;
end
if loadIt && saveIt
warning(strjoin({'does not make sense to loadIt then saveIt' 'Changing to' 'loadIt = 1' 'doIt = 0' 'saveIt = 0'},newline)); loadIt = 1; doIt = 0; saveIt = 0;
end
tic; disp(' '); disp(' '); disp(repmat('-',1,length(stepLabel))); disp(upper(stepLabel)); disp(repmat('-',1,length(stepLabel)))
if loadIt
if exist([stepFile '.mat'],'file')
disp(strjoin({[upper(stepLabel) ': loading from'] [stepFile '.mat']},newline)); load(stepFile); disp([upper(stepLabel) ': loaded'])
else
warning(strjoin({[stepFile '.mat'] 'does not exist' 'Changing to' 'loadIt = 0' 'doIt = 1' 'saveIt = 1'},newline)); loadIt = 0; doIt = 1; saveIt = 1;
end
end
if doIt
%%%%%%%%%%%%%%%%%%%%%%%%%
%% Do the processing here
%%%%%%%%%%%%%%%%%%%%%%%%%
%% Mask
if ~isempty(volAnat)
if length(volAnat)==1 && ...
isfield(volAnat,'mask') && isfield(volAnat.mask,'crop') && isfield(volAnat.mask.crop,'mri') && isfield(volAnat.mask.crop.mri,'vol') && ~isempty(volAnat.mask.crop.mri.vol)
%%%crop
mask = volAnat.mask.crop.mri.vol;
%%%head
mask = mask & any(volAnat.mask.head.mri.vol,4);
% %%%brain
% mask = mask & any(volAnat.mask.brain.mri.vol,4);
%%%apply
volTs = applyMask(volTs,mask);
else
volTs = vol2vec(volTs);
for I = 1:length(volAnat)
if isfield(volAnat(I),'fun') && isfield(volAnat(I).fun,'mask') && isfield(volAnat(I).fun.mask,'crop') && ~isempty(volAnat(I).fun.mask.crop.vol)
% volTs(I) = applyMask(volTs(I),volAnat(I).fun.mask.crop.vol);
%%%crop
mask = volAnat(I).fun.mask.crop.vol;
if isfield(volAnat(I).fun.mask,'head') && ~isempty(volAnat(I).fun.mask.head)
%%%head
mask = mask & any(volAnat(I).fun.mask.head.mri.vol,4);
elseif isfield(volAnat(I).fun.mask,'brain') && ~isempty(volAnat(I).fun.mask.brain)
%%%brain
mask = mask & any(volAnat(I).fun.mask.brain.mri.vol,4);
end
%%%apply
volTs(I) = applyMask(volTs(I),mask);
end
end
% if isfield(volAnat,'fun') && isfield(volAnat.fun,'mask') && isfield(volAnat.fun.mask,'crop') && ~isempty(volAnat.fun.mask.crop.vol)
% volTs = applyMask(volTs,volAnat.fun.mask.crop.vol);
% end
end
end
% % %% Add time
% % volTs = addTime(volTs);
%
% %% Normalize to thermal noise
% % thermalNoiseRange = [0.5 inf];
% % modeToRemove = 1:5;
% % funTs = normPSD3(funTs,thermalNoiseRange,modeToRemove);
%
% %% Detrend run-by-run
% [volTs,~,info.dtrndOrder] = dtrnd2(volTs,[],[],info.dtrndOrder);
% % volTsTmp = vec2vol(volTs);
% % volTsTmp.vol = volTsTmp.vol - volTsTmp.imMean;
%
%
% % volTs = vol2vec(volTs);
if length(volTs)==1 && isfield(volTs,'nDummy') && ~isempty(volTs.nDummy)
param.nDummy = volTs.nDummy;
else
param.nDummy = info.dummy;
end
param.nDummyRemoved = param.nDummy;
if volTs.tr/1000 ~= volTs.dsgn.dt
volTs.tr/1000
if isfield(volTs.dsgn,'onsets')
diff(volTs.dsgn.onsets)
volTs.dsgn.onsets / (volTs.tr/1000)
volTs.dsgn.onsets / 4
warning(strjoin({''...
['volume TR = ' sprintf('%7.3f ',volTs.tr/1000) 'sec']...
['stim dt = ' sprintf('%7.3f ',volTs.dsgn.dt) 'sec']...
['stim onsets = [' sprintf('%7.3f ',volTs.dsgn.onsets) ']sec']...
[' = [' sprintf('%7.3f ',(volTs.dsgn.onsets / (volTs.tr/1000))) ']vol']...
'Defaulting to deconvolution TR = 1sec'},newline))
param.trDecon = 1;
else
diff(volTs.dsgn.onsetList)
volTs.dsgn.onsetList / (volTs.tr/1000)
volTs.dsgn.onsetList / volTs.dsgn.dt
% volTs.dsgn.onsetList / volTs.dsgn.dt - round(volTs.dsgn.onsetList / volTs.dsgn.dt)
tError = (volTs.dsgn.onsetList(end) / (volTs.tr/1000) - volTs.dsgn.onsetList(end) / (volTs.dsgn.dt)) * 1000;
warning(strjoin({''...
['volume TR = ' sprintf('%7.3f ',volTs.tr) 'ms']...
['stim dt = ' sprintf('%7.3f ',volTs.dsgn.dt*1000) 'ms']...
['stim onsets = [' sprintf('%7.0f ',volTs.dsgn.onsetList*1000) ']ms']...
[' = [' sprintf('%7.3f ',(volTs.dsgn.onsetList / (volTs.tr/1000))) ']vol']...
''...
[num2str(tError) 'ms error by the last onset']},newline))
if abs(tError)<10
param.trDecon = volTs.dsgn.dt;
else
dbstack; error('timing problem here')
end
end
else
param.trDecon = volTs.tr/1000; %volTs.tr/1000/4; %1
end
param.verbose = 1;
forceThis = force;
switch info.dataSetLabel
case 'vsmDriven'
[files,fRun,fSes,fSes_echoCat,param] = getResp(volTs,[],param,forceThis);
otherwise
dbstack; error('double-check that');
[files,fRun,fSes,fSes_echoCat,param] = getResp(volTs,volAnat,param,forceThis);
end
volResp.ts = MRIread(files.resp.f{1});
volResp.tsOnBase = MRIread(files.respOnBase.f{1});
volResp.base = MRIread(files.base.f{1});
volResp.F = MRIread(files.respF.f{1});
volResp.Fq = MRIread(files.respF_fdr.f{1});
volResp.vid = files.respOnBaseMovieHighBit.f{1};
end
%%%%%%%%%%%%%%%%
%% House keeping
%%%%%%%%%%%%%%%%
if saveIt; disp(strjoin({[upper(stepLabel) ': saving to '] [stepFile '.mat']},newline)); tmp = whos(outVar); if tmp.bytes/1e9<2; save(stepFile,outVar); else, save(stepFile,outVar,'-v7.3'); end; disp([upper(stepLabel) ': saved']); end
disp(repmat('-',1,length(stepLabel)+6)); disp([upper(stepLabel) ': DONE']); toc; disp(repmat('-',1,length(stepLabel)+6)); disp(' '); disp(' ');
eval(['out = ' outVar '; clear ' outVar]);