-
Notifications
You must be signed in to change notification settings - Fork 1
/
createParams.m
384 lines (310 loc) · 12.5 KB
/
createParams.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
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
%% Create Parameters
% Editor: Daniel Elbich
% Created: 3/14/19
%
% Creates parameters .mat file for use with MVPA scripts
%
%
% Updated: 5/22/20
%
% Major Updates:
% -Removed ROI processing. Only directories are handled now
% -fMRIPrep output data is not integrated into the pipeline
% -Reorganized script to handle variable preprocessing.
%
% Updated: 9/19/19
%
% Major updates:
% -Added flag for regressing reaction time (RT) from betas prior to
% classification
% -Added flag and variables for bootstrap MVPA classification
% -Added ERS functionality
%
%
% Minor updates:
% -Cleaned up code to get subject list/directories
% -Cleaned up code saving mat files with ROI names to preven overwriting
% between ROI & Searchlight analyses
%
%
% Updated: 4/22/19
%
% Major updates:
% -Added section to create separate params file for use with Specify
% Model script. This will be deleted during creation of final param mat
% file.
% -Rescling now completed through AFNI. Must loaded/added to system PATH
%
% Minor updates:
% -Searches common folder for ROI masks instead of project folder
% -List dialog to select 1 or more regions for reslicing
% -User input to select searchlight/no searchlight anlaysis
% -User input to select conditions of interest
% -Saves subjects, rois, and conds variables to var directory in project
% folder
%% Set Pipeline Parameters
% Set Path Variables
% Parent/Project Path - directory containing project data and analyses
directory.Project = inputdlg(['Enter path to parent directory: '],...
'File Path',[1 35],{pwd});
directory.Project = directory.Project{:};
% Directories for raw functional data and behavioral data
rawData.funcDir = inputdlg(['Enter Path to raw functional data starting from: ' directory.Project],...
'File Path',[1 35],{'e.g. CPM/subj/func'});
rawData.funcDir = [directory.Project filesep rawData.funcDir{:}];
% Directory of the behavioral data file
rawData.behavDir = inputdlg(['Enter Path to behavioral file starting from: ' directory.Project],...
'File Path',[1 35],{'e.g. CPM/subj/behav'});
rawData.behavDir = rawData.behavDir{:};
% Extension of the behavioral file to help search
rawData.behavFile = inputdlg('Enter Trial Tag File Extension:',...
'File extension',[1 35],{'e.g. xlsx, csv'});
rawData.behavFile = rawData.behavFile{:};
% Functional Data Preprocessing Program
preprocPipeline = questdlg('Select Preprocessing Pipeline',...
'Confirm Pipeline',...
'spm12','fmriprep','Cancel','fmriprep');
%% Task and Data Information
try
% Task Name
taskInfo.Name = inputdlg('Enter Task Name: [as written in functional filename]',...
'Task Name',[1 35],{'e.g. encoding, nback, rest'});
taskInfo.Name = taskInfo.Name{:};
% Conditions of Interest
taskInfo.Conditions = inputdlg('Enter Conditions of Interest: [separated by commas]',...
'Conditions',[1 35],{'e.g. face, object, place'});
% Conditions of Interest
taskInfo.accuracyFlag = questdlg('Is accuracy possible? (i.e. interactive vs. static task)',...
'Confirm Accuracy Potential',...
'Yes','No','Cancel','No');
% Length of task (in volumes)
taskInfo.Datapoints = inputdlg('How many volumes [Length of task]?',...
'Volume Number',[1 35],{'150'});
taskInfo.Datapoints = str2double(taskInfo.Datapoints{:});
taskInfo.Runs = inputdlg('How many runs [# of functional runs]?',...
'Number of Runs',[1 35],{'1'});
taskInfo.Runs = str2double(taskInfo.Runs{:});
%taskInfo.Trials = inputdlg('How many trials per run [sum trials for all conditions]?',...
% 'Volume Number',[1 35],{'40'});
%taskInfo.Trials = str2double(taskInfo.Trials{:});
%% Set Project Specific Variables
% Please specify:
% -The units used (i.e., 'scans' or 'secs')
% -The TR or repition time
Model.units = 'secs';
%Model.TR = 2.5;
Model.TR = inputdlg('Enter TR: [in seconds]',...
'Repetition Time',[1 35],{'2'});
Model.TR = str2double(Model.TR{:});
% Please specify if a mask is used during model estimation
Mask.on = 0; %Default - no mask used
Mask.dir = '/path/to/mask/directory';
Mask.name = 'name_of_mask.nii';
switch preprocPipeline
case 'spm12'
Func.dir = [directory.Project filesep rawData.funcDir];
Func.wildcard = '^ar.*\.nii'; % File
taskInfo.Slices = inputdlg('How many slices [# of slices in volume]?',...
'Number of Slices',[1 35],{'50'});
taskInfo.Slices = str2double(taskInfo.Slices{:});
case 'fmriprep'
Func.dir = [directory.Project filesep rawData.funcDir];
Func.wildcard = ['^*' taskInfo.Name '_run-']; % File
end
catch
warning('Unable to set project information!.')
end
%% Project Paths
try
% General path setup
directory.Analysis = [directory.Project filesep 'multivariate'];
directory.Model = [directory.Analysis filesep 'models' filesep ...
'SingleTrialModel' taskInfo.Name];
setenv('analysisPath',directory.Model);
!mkdir -p $analysisPath
analysisList = {'MVPA','RSA','ERS'};
[index,tf] = listdlg('PromptString','Select Multivariate Analysis:',...
'SelectionMode','Single','ListString',analysisList);
classType = analysisList{index};
% Select analysis type. No searchlight is default
analysisType = questdlg('Select Analysis Level for Multivariate Test:',...
'Confirm Searchlight',...
'Searchlight','ROI','Cancel','ROI');
% Account for RT/regress out. No is default
regressRT.flag = questdlg('Regress out Reaction Time (RT)?',...
'Confirm Regression',...
'Yes','No','Cancel','No');
catch
error('Unable to set project path information!');
end
%% Classification Flags
try
% Bootstrapping Setup
bootstrap.flag = questdlg('Perform Bootstrap?',...
'Confirm Bootstrap',...
'Yes','No','Cancel','No');
if strcmpi(bootstrap.flag,'Yes')==1
bootstrap.numRuns = taskInfo.Runs;
bootstrap.numTrials = taskInfo.Trials;
bootstrap.perm = inputdlg('How many permutations?',...
'Permutation Number',[1 35],{'1000'});
bootstrap.perm = str2double(bootstrap.perm{:});
bootstrap.trialsPerRun = randperm(length...
(1:bootstrap.numTrials));
for i=1:bootstrap.numRuns
bootstrap.structNames{i} = ['perm' num2str(i)];
end
end
switch classType
% MVPA Flags
case 'MVPA'
% Compute MVPA with entire run to train/test or individual
% trials to train/test
try
trialAnalysis = 'Run';
leaveRuns = NaN;
if strcmpi(analysisType, 'Searchlight')==1
searchlight.Metric = questdlg('Searchlight Metric',...
'SearchlightSize','count','radius','Cancel','count');
searchlight.Size = inputdlg('Size of the searchlight',...
'Searchlight Size',[1 35],{'50'});
searchlight.Size = str2double(searchlight.Size{:});
end
catch
warning(['Error in setting MVPA analysis flags. '...
'Set to debug mode.']);
end
% RSA Flags
case 'RSA'
% Compute RSA with mean activation pattern (average all trials)
% or individual trial pattern (all trials are separate)
try
trialAnalysis = 'Individual';
leaveRuns = NaN;
if strcmpi(analysisType, 'Searchlight')==1
searchlight.Metric = questdlg('Searchlight Metric',...
'SearchlightSize','count','radius','Cancel','count');
searchlight.Size = inputdlg('Size of the searchlight',...
'Searchlight Size',[1 35],{'50'});
searchlight.Size = str2double(searchlight.Size{:});
end
catch
warning(['Error in setting RSA analysis flags. '...
'Set to debug mode.']);
end
case 'ERS'
try
[index,tf] = listdlg('Name','Possible Tasks',...
'PromptString','Ctrl+Click to select conditions:',...
'ListString',tasks,...
'ListSize',[280,300]);
tasks=tasks(index);
save([Project 'vars/tasks.mat'],'tasks');
catch
warning(['Error in setting ERS analysis flags. '...
'Set to debug mode.']);
end
end
catch
warning('Error in setting classification flags. Set to debug mode.');
end
%% Create Subject List
try
% List subject directory
subjDir=dir(rawData.funcDir);
% Remove any files in directory
for i=1:length(subjDir)
if subjDir(i).isdir==0
fileFlag(i) = 0;
else
fileFlag(i) = 1;
end
end
subjDir = subjDir(logical(fileFlag));
% Remove non-subject directories
for i=1:length(subjDir)
subFlag(i) = ~isempty(strfind(subjDir(i).name,'sub-'));
end
subjDir = subjDir(subFlag);
% Search directory to get list of subjects
for i=1:length(subjDir)
subjects{i,1}=subjDir(i).name;
end
clear subjCount i subjDir;
catch
warning('Unable to create subject list. Set to debug mode.');
end
%% Assign Conditions
try
taskInfo.Conditions = strsplit(taskInfo.Conditions{:},',');
subconditionFlag = questdlg('Are there subconditions? (If unsure select No)',...
'Confirm Subconditions','Yes','No','Cancel','No');
switch subconditionFlag
case 'Yes'
[index,tf] = listdlg('Name','Possible Conditions',...
'PromptString','Ctrl+Click to select conditions:',...
'ListString',conds,...
'ListSize',[280,300]);
subconds=subconds(index);
subconditionFlag = 'TRUE';
case 'No'
subconditionFlag = 'FALSE';
end
catch
warning('Unable to assign conditions. Set to debug mode.');
end
%% Set filename for output
try
% Set filename for parameter .mat file
for i=1:length(taskInfo.Conditions)
if i==1
conds=taskInfo.Conditions{i};
else
conds=[conds '-' taskInfo.Conditions{i}];
end
end
switch analysisType
case 'Searchlight'
filename=strcat(directory.Analysis,'/params_',preprocPipeline,...
'_',taskInfo.Name,'_',classType,'_',analysisType,'_Conds_',...
conds,'_subConds_',subconditionFlag,'_Bootstrap_',...
bootstrap.flag,'_Searchlight',searchlight.Metric,'_',...
num2str(searchlight.Size),'.mat');
otherwise
filename=strcat(directory.Analysis,'/params_',preprocPipeline,...
'_',taskInfo.Name,'_',classType,'_',analysisType,'_Conds_',...
conds,'_subConds_',subconditionFlag,'_Bootstrap_',...
bootstrap.flag,'_Searchlight_No.mat');
end
catch
warning('Unable to set params filename. Set to debug mode.');
end
%% Save Params File
switch analysisType
case 'Searchlight'
save(filename,'directory','rawData','preprocPipeline',...
'taskInfo','Model','Mask', 'Func','classType',...
'subjects','analysisType','regressRT','searchlight',...
'bootstrap','trialAnalysis','leaveRuns');
otherwise
save(filename,'directory','rawData','preprocPipeline',...
'taskInfo','Model','Mask','Func','classType',...
'subjects','analysisType','regressRT',...
'bootstrap','trialAnalysis','leaveRuns');
end
%% Create PBS Script for Command Line Processing
% Identify shell script
cmd=which('multi-FRAME/modeling/createPBSScript.sh');
setenv('cmd',cmd);
% Break filename into parts
[filepath,name]=fileparts(filename);
% Set Variables
setenv('fileName',name);
setenv('filePath',filepath);
setenv('project',directory.Project);
% Create PBS script
!sh $cmd --paramsFile $fileName --paramsDir $filePath --projectDir $project
%% Cleanup
clear;
clc;
disp('All finished!!');