-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing_runner.m
More file actions
94 lines (75 loc) · 2.93 KB
/
preprocessing_runner.m
File metadata and controls
94 lines (75 loc) · 2.93 KB
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
% Translational Neuromodeling Project, ETH Zurich
% 'Decoding moral judgements from neurotypical individuals compared to
% individuals with ASD'
%--------------------------------------------------------------------------
% authors: Stephan Boner, Alexander Hess, Nina Stumpf
% date: 2019-05-30
% version: 1.0
%--------------------------------------------------------------------------
% This file preprocesses all the data in the sub-xx folders in the given
% directory first it extracts all the .gz-files, then it continues with
% preprocessing the Nii-Files. After that it adjusts the textfiles and then
% it extracts the task specifications.
% This file calls: 'preprocessNiiData.m', 'helperReadTaskSpecs.m'
%==========================================================================
pathBase = what('TNproject');
pathBase = pathBase.path;
pathBase = fullfile(pathBase, 'data');
dFunc = dir(fullfile(pathBase,'sub*/func/*.nii.gz'));
dAnat = dir(fullfile(pathBase,'sub*/anat/*.nii.gz'));
d = [dFunc',dAnat'];
% unpack all data
for f=1:numel(d)
file = fullfile(d(f).folder,d(f).name);
gunzip(file);
delete(file);
end
sprintf('==== unpacked all data ====')
%preprocesses the Nii data
dirs = dir(fullfile(pathBase,'sub*'));
for f=1:numel(dirs)
% if the process got interrupted, we can begin with the given specs
% number
beginning = 0; %0, if not needed
if str2num(strrep(dirs(f).name, 'sub-','')) < beginning
sprintf("skipped %s", dirs(f).name)
else
preprocessNiiData(fullfile(pathBase,dirs(f).name))
end
end
sprintf('==== processed all nii-data ====')
%makes all the.txt-files right
for f=1:numel(dirs)
subject = dirs(f).name;
preprocessedFunc = fullfile(pathBase, subject, 'preprocessed', 'func');
txtDir = dir(fullfile(preprocessedFunc, 'rp*.txt'));
if size(txtDir,1) == 0
warning('skipped %s',subject)
continue
end
M = dlmread(fullfile(txtDir.folder, txtDir.name));
si = size(M,1);
batchSize = 166;
if mod(si, batchSize) ~= 0
warning('wrong number of lines in txt-file %s', subject)
continue
end
for i = 1:6
filename = [subject, '_task-dis_run-',num2str(i, '%02d'),'_bold', '.txt'];
part = M((i-1)*batchSize+1:i*batchSize,:);
dlmwrite(fullfile(preprocessedFunc, filename), part,'delimiter','\t', 'precision', '%.7e');
end
end
sprintf('==== processed all textfiles ====')
% preprocess taskSpecs, generate matlab structs from .tsv-files
dirs = dir(fullfile(pathBase,'sub*','func','*task-dis_run*.tsv'));
for f=1:numel(dirs)
filename = dirs(f).name;
folder = dirs(f).folder;
struct = helperReadTaskSpecs(fullfile(folder, filename));
preprocessedFolder = strrep(folder, '/func', '/preprocessed/func');
structName = strrep(filename,'tsv','mat');
save(fullfile(preprocessedFolder,structName),'struct');
end
sprintf('==== generated structs from tsv ====')
sprintf('======== Finished! ========')