-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.m
221 lines (155 loc) · 5.22 KB
/
main.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
function [] = main()
disp('Loading config.json...');
% hard coded values to app config
nclust = 4;
% load my own config.json
config = loadjson('config.json');
% create output directory
mkdir('output');
% create cache dir
mkdir('cache');
%# function sptensor
%% load inputs
disp('Loading data...');
% create the labels
labs = config.parc;
mask = config.mask;
infl = config.infl;
% if these exist?
do_micro = config.compmicro;
do_tprof = config.comptprof;
do_shape = config.compshape;
nnodes = config.nnodes;
microlab = config.microdat;
if (do_micro || do_tprof)
switch config.microdat
case 'fa'
microdat = niftiRead(config.fa);
case 'md'
microdat = niftiRead(config.md);
case 'ad'
microdat = niftiRead(config.ad);
case 'rd'
microdat = niftiRead(config.rd);
otherwise
microdat = [];
microlab = 'none';
do_micro = 'False';
do_tprof = 'False';
end
end
% import streamlines
fg = dtiImportFibersMrtrix(config.fibers, .5);
% grab length
fascicle_length = fefgGet(fg, 'length');
% create the inflated parcellation
parc = feInflateLabels(labs, mask, infl, 'vert', './output/labels_dilated.nii.gz');
%% start parallel pool
disp(['Opening parallel pool with ', num2str(nclust), ' cores...']);
% create parallel cluster object
clust = parcluster;
% set number of cores from arguments
clust.NumWorkers = nclust;
% set temporary cache directory
tmpdir = tempname('cache');
% make cache dir
OK = mkdir(tmpdir);
% check and set cachedir location
if OK
% set local storage for parpool
clust.JobStorageLocation = tmpdir;
disp(['Cluster Job Storage Location set to: ' clust.JobStorageLocation]);
else
warning('Cluster Job Storage Location was unset. The default may cause failuers.');
end
% start parpool - close parpool at end of fxn
pool = parpool(clust, nclust, 'IdleTimeout', 120);
clear tmpdir OK
%% create the inputs
disp('Assigning streamlines to connections...');
% assign the initial connections - pass dummy weights
[ pconn, rois ] = feCreatePairedConnections(parc, fg.fibers, fascicle_length, ones(size(fascicle_length)));
% catch the center in an output
centers = nan(size(rois, 1), 3);
for ii = 1:size(rois)
centers(ii, 1) = rois{ii}.centroid.acpc(1);
centers(ii, 2) = rois{ii}.centroid.acpc(2);
centers(ii, 3) = rois{ii}.centroid.acpc(3);
end
clear ii
%% central tendency of microstructure
% if microstructure central tendency is requested
if do_micro
disp([ 'Computing central tendency of edge ' microlab '...' ]);
% compute mean / std of microstructure property
pconn = fnAverageEdgePropertyS(pconn, 'all', fg, microdat, microlab, 'mean');
pconn = fnAverageEdgePropertyS(pconn, 'all', fg, microdat, microlab, 'std');
end
%% tract profiles
% if tract profiles are requested
if do_tprof
disp([ 'Computing tract profiles of edge ' microlab '...' ]);
% compute tract profiles of tensor property
pconn = feTractProfilePairedConnections(fg, pconn, 'all', microdat, microlab, 100);
% create tract profile tensor to save down
[ ~, tpmat ] = fnTractProfileTensor(pconn, 'all', microlab);
end
%% tract curves
% if tract curvatures are requested
if do_shape
disp('Computing curvature and torsion of edge...');
% compute the shapes of the tracts
pconn = fnTractCurvePairedConnections(fg, pconn, 'all', nnodes);
% create tract shape tensor to save curves
[ ~, cvmat ] = fnTractProfileTensor(pconn, 'all', 'shape.curv');
[ ~, trmat ] = fnTractProfileTensor(pconn, 'all', 'shape.tors');
end
%% create matrices
disp('Creating connectivity matrices...');
% create the connectivty matrices
[ omat, olab ] = feCreateAdjacencyMatrices(pconn, 'all');
%% run network statistics
disp('Computing network statistics...');
% for every matrix computed
for ii = 1:size(omat, 3)
% compute a bunch of numbers
[ stats.(olab{ii}).glob, stats.(olab{ii}).node, stats.(olab{ii}).nets ] = fnNetworkStats(omat(:,:,ii));
end
%% save and exit
% remove parallel pool
delete(pool);
disp('Saving outputs...');
% save the matlab outputs for debugging
save('output/omat.mat', 'omat');
save('output/olab.mat', 'olab');
save('output/pconn.mat', 'pconn');
save('output/rois.mat', 'rois');
% save text outputs - convert writes to json
dlmwrite('./output/centers.csv', centers, ',');
dlmwrite('./output/count.csv', omat(:,:,1), ',');
dlmwrite('./output/density.csv', omat(:,:,2), ',');
dlmwrite('./output/length.csv', omat(:,:,3), ',');
dlmwrite('./output/denlen.csv', omat(:,:,4), ',');
% save microstructure mats if they're made
if do_micro
dlmwrite([ './output/' microlab '_mean.csv' ], omat(:,:,5), ',');
dlmwrite([ './output/' microlab '_std.csv' ], omat(:,:,6), ',');
end
% save tract profiles if they're made
if do_tprof
dlmwrite([ './output/' microlab '_tp.csv' ], tpmat, ',');
end
% save curves if they're made
if do_shape
dlmwrite('./output/curv.csv', cvmat, ',');
dlmwrite('./output/tors.csv', trmat, ',');
end
% save all the network stats
opt.filename = "./output/stats.json";
opt.ArrayIndent = 1;
opt.ArrayToStruct = 0;
opt.SingleArray = 0;
opt.SingletCell = 0;
opt.Compact = 1;
savejson('stats', stats, opt);
end