-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlabelParcels.m
More file actions
303 lines (243 loc) · 10.2 KB
/
labelParcels.m
File metadata and controls
303 lines (243 loc) · 10.2 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
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
function [num_code,peak_labels,max_intersect_label] = labelParcels(P,D,parcel_nums)
HO_cort = MRIread('./template_brains/HarvardOxford-cort-maxprob-thr0-2mm.nii.gz',0);
HO_sub = MRIread('./template_brains/HarvardOxford-sub-maxprob-thr0-2mm.nii.gz',0);
Cereb = MRIread('./template_brains/Cerebellum-MNIfnirt-maxprob-thr0-2mm.nii.gz',0);
[~,HO_cort_label] = importHarvardOxfordLabels;
[~,HO_sub_label] = importHarvardOxfordLabels_subcort;
[~,Cereb_label] = importCerebellumLabels;
HO_cort_label = removeSpaces(HO_cort_label);
HO_sub_label = removeSpaces(HO_sub_label);
Cereb_label = removeSpaces(Cereb_label);
peak_labels = cell(length(parcel_nums),1);
max_intersect_label = peak_labels;
for i = 1:length(parcel_nums)
peak_vox(i) = find((P == parcel_nums(i)),1);
num_code(i) = HO_cort.vol(peak_vox(i));
if num_code(i) ~= 0
if HO_sub.vol(peak_vox(i)) <= 7
hemi = 'Left_';
else
hemi = 'Right_';
end
peak_labels(i) = strcat(hemi,HO_cort_label(num_code(i)));
else
num_code(i) = HO_sub.vol(peak_vox(i));
if num_code ~= 0
peak_labels(i) = HO_sub_label(num_code(i));
else
num_code(i) = Cereb.vol(peak_vox(i));
if num_code ~= 0
peak_labels(i) = Cereb_label(num_code(i));
else
peak_labels(i) = {'unidentified'};
end
end
end
max_code(i) = get_max_intersect(HO_cort.vol,D,parcel_nums(i));
if max_code(i) ~= 0
if HO_sub.vol(peak_vox(i)) <= 7
hemi = 'Left_';
else
hemi = 'Right_';
end
max_intersect_label(i) = strcat(hemi,HO_cort_label(max_code(i)));
else
max_code(i) = get_max_intersect(HO_sub.vol,D,parcel_nums(i));
if max_code(i) ~= 0
max_intersect_label(i) = HO_sub_label(max_code(i));
else
max_code(i) = get_max_intersect(Cereb.vol,D,parcel_nums(i));
if max_code(i) ~= 0
max_intersect_label(i) = Cereb_label(max_code(i));
else
max_intersect_label(i) = {'unidentified'};
end
end
end
end
function new_string_list = removeSpaces(old_string_list)
new_string_list = old_string_list;
for i = 1:length(new_string_list)
new_string_list{i} = strrep(new_string_list{i},' ','_');
new_string_list{i} = strrep(new_string_list{i},',','');
end
function max_code = get_max_intersect(vol,D,parcel_num)
temp_codes_cort = vol(D == parcel_num);
[c, ia, ic] = unique(temp_codes_cort);
a_counts = accumarray(ic,1);
[~,max_idx] = max(a_counts);
max_code = c(max_idx);
function [HO_cort_code,HO_cort_label] = importHarvardOxfordLabels
%% Import data from text file.
% Script for importing data from the following text file:
%
% /Users/tlscott/Documents/PWMDLD/NWR_NWD_PAPER/data/template_brains/HarvardOxford_cort_labels.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2018/06/21 14:34:01
%% Initialize variables.
filename = './template_brains/HarvardOxford_cort_labels.txt';
delimiter = '\t';
%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{1};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, 1) = numbers{1};
raw{row, 1} = numbers{1};
end
catch me
end
end
%% Split data into numeric and cell columns.
rawNumericColumns = raw(:, 1);
rawCellColumns = raw(:, 2);
%% Allocate imported array to column variable names
HO_cort_code = cell2mat(rawNumericColumns(:, 1));
HO_cort_label = rawCellColumns(:, 1);
HO_cort_code = HO_cort_code + 1;
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns;
function [HO_sub_codes,HO_sub_labels] = importHarvardOxfordLabels_subcort
%% Import data from text file.
% Script for importing data from the following text file:
%
% /Users/tlscott/Documents/PWMDLD/NWR_NWD_PAPER/data/template_brains/HarvardOxford_sub_labels.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2018/06/21 14:38:52
%% Initialize variables.
filename = './template_brains/HarvardOxford_sub_labels.txt';
delimiter = '\t';
%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{1};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, 1) = numbers{1};
raw{row, 1} = numbers{1};
end
catch me
end
end
%% Split data into numeric and cell columns.
rawNumericColumns = raw(:, 1);
rawCellColumns = raw(:, 2);
%% Allocate imported array to column variable names
HO_sub_codes = cell2mat(rawNumericColumns(:, 1));
HO_sub_labels = rawCellColumns(:, 1);
HO_sub_codes = HO_sub_codes + 1;
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns;
function [Cereb_codes,Cereb_labels] = importCerebellumLabels
%% Import data from text file.
% Script for importing data from the following text file:
%
% /Users/tlscott/Documents/PWMDLD/NWR_NWD_PAPER/data/template_brains/Cerebellum-SUIT.nii.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2018/06/21 14:41:02
%% Initialize variables.
filename = './template_brains/Cerebellum-SUIT.nii.txt';
delimiter = ' ';
%% Format string for each line of text:
% column1: double (%f)
% column2: text (%s)
% column3: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%s%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%% Allocate imported array to column variable names
Cereb_codes = dataArray{:, 1};
Cereb_labels = dataArray{:, 2};
Cereb_other = dataArray{:, 3};
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;