-
Notifications
You must be signed in to change notification settings - Fork 13
/
ExportToCSV.m
200 lines (170 loc) · 6.93 KB
/
ExportToCSV.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
function MRS_struct = ExportToCSV(MRS_struct, vox, module)
round2 = @(x) round(x*1e3)/1e3;
if strcmp(MRS_struct.p.vendor, 'Siemens_rda')
n_rep = [size(MRS_struct.metabfile,2)/2 1];
else
n_rep = [size(MRS_struct.metabfile,2) 1];
end
out.MATLAB_ver = cellstr(repmat(version('-release'), n_rep));
out.Gannet_ver = cellstr(repmat(MRS_struct.version.Gannet, n_rep));
out.date_of_analysis = cellstr(repmat(char(datetime('now','Format','y-MM-dd')), n_rep));
%%% 1. Extract data from GannetFit %%%
if strcmp(MRS_struct.p.vendor, 'Siemens_rda')
filename = MRS_struct.metabfile(1,1:2:end)';
else
filename = MRS_struct.metabfile(1,:)';
end
for ii = 1:length(filename)
[~,b,c] = fileparts(filename{ii});
out.filename(ii,1) = cellstr([b c]);
end
out.avg_delta_F0 = MRS_struct.out.AvgDeltaF0(:);
metabs = {'GABA','Glx','GSH','EtOH','Lac','water','Cr','Cho','NAA'};
for ii = 1:length(metabs)
if ~isfield(MRS_struct.out.(vox), metabs{ii})
continue
end
out.(metabs{ii}).area = MRS_struct.out.(vox).(metabs{ii}).Area(:);
out.(metabs{ii}).FWHM = MRS_struct.out.(vox).(metabs{ii}).FWHM(:);
out.(metabs{ii}).SNR = MRS_struct.out.(vox).(metabs{ii}).SNR(:);
out.(metabs{ii}).fit_error = MRS_struct.out.(vox).(metabs{ii}).FitError(:);
if ~strcmp(metabs{ii}, 'water')
if ~strcmp(metabs{ii}, 'Cr')
out.(metabs{ii}).fit_error_Cr = MRS_struct.out.(vox).(metabs{ii}).FitError_Cr(:);
end
if strcmp(MRS_struct.p.reference, 'H2O')
out.(metabs{ii}).fit_error_w = MRS_struct.out.(vox).(metabs{ii}).FitError_W(:);
end
if ~strcmp(metabs{ii}, 'Cr')
out.(metabs{ii}).conc_Cr = MRS_struct.out.(vox).(metabs{ii}).ConcCr(:);
end
if strcmp(MRS_struct.p.reference, 'H2O')
out.(metabs{ii}).conc_iu = MRS_struct.out.(vox).(metabs{ii}).ConcIU(:);
end
end
end
T = table(out.MATLAB_ver, out.Gannet_ver, out.date_of_analysis, out.filename, round2(out.avg_delta_F0), ...
'VariableNames', {'MATLAB_version', 'Gannet_version', 'date_of_analysis', 'filename', 'avg_delta_F0'});
field_names = fieldnames(out);
for ii = 1:length(field_names)
if any(strcmp(field_names{ii}, metabs))
sub_field_names = fieldnames(out.(field_names{ii}));
for jj = 1:length(sub_field_names)
if strcmp(sub_field_names{jj}, 'area')
U = table(out.(field_names{ii}).(sub_field_names{jj}), ...
'VariableNames', {[field_names{ii} '_' sub_field_names{jj}]});
else
U = table(round2(out.(field_names{ii}).(sub_field_names{jj})), ...
'VariableNames', {[field_names{ii} '_' sub_field_names{jj}]});
end
T = [T U]; %#ok<*AGROW>
end
end
end
% Create CSV filename
if isfield(MRS_struct.out.(vox), 'csv_name')
csv_name = MRS_struct.out.(vox).csv_name;
else
csv_name = fullfile(pwd, 'Gannet_output.csv');
if exist(csv_name, 'file')
run_count = 1;
csv_name = fullfile(pwd, ['Gannet_output' num2str(run_count) '.csv']);
while 1
if exist(csv_name, 'file')
run_count = run_count + 1;
csv_name = fullfile(pwd, ['Gannet_output' num2str(run_count) '.csv']);
else
break
end
end
end
MRS_struct.out.(vox).csv_name = csv_name;
end
% End if function invoked in GannetFit
if strcmp(module, 'fit')
% Convert empty cells into NaNs
for ii = 1:size(T,2)
if ~iscell(T(:,ii).(T.Properties.VariableNames{ii}))
T(~T(:,ii).(T.Properties.VariableNames{ii}),ii) = {NaN};
end
end
fprintf('\nExporting results to %s\n', [csv_name '...']);
writetable(T, csv_name);
return
end
%%% 2. Extract data from GannetSegment %%%
out.tissue.fGM = MRS_struct.out.(vox).tissue.fGM(:);
out.tissue.fWM = MRS_struct.out.(vox).tissue.fWM(:);
out.tissue.fCSF = MRS_struct.out.(vox).tissue.fCSF(:);
metabs = {'GABA','Glx','GSH','EtOH','Lac','Cr','Cho','NAA'};
if strcmp(MRS_struct.p.reference, 'H2O')
for ii = 1:length(metabs)
if ~isfield(MRS_struct.out.(vox), metabs{ii})
continue
end
out.(metabs{ii}).ConcIU_CSFcorr = MRS_struct.out.(vox).(metabs{ii}).ConcIU_CSFcorr(:);
end
end
field_names = fieldnames(out);
X = table;
V = table;
for ii = 1:length(field_names)
if any(strcmp(field_names{ii}, metabs)) && strcmp(MRS_struct.p.reference, 'H2O')
sub_field_names = fieldnames(out.(field_names{ii}));
Y = table(round2(out.(field_names{ii}).(sub_field_names{end})), ...
'VariableNames', {[field_names{ii} '_' sub_field_names{end}]});
X = [X Y];
elseif strcmp(field_names{ii}, 'tissue')
sub_field_names = fieldnames(out.(field_names{ii}));
for jj = 1:3
U = table(round2(out.(field_names{ii}).(sub_field_names{jj})), ...
'VariableNames', sub_field_names(jj));
V = [V U];
end
end
end
T = [T V X]; % doing it this way so that tissue fractions come before the CSF-corrected values in the .csv file
% End if function invoked in GannetSegment
if strcmp(module, 'segment')
% Convert empty cells into NaNs
for ii = 1:size(T,2)
if ~iscell(T(:,ii).(T.Properties.VariableNames{ii}))
T(~T(:,ii).(T.Properties.VariableNames{ii}),ii) = {NaN};
end
end
fprintf('\nUpdating results in %s\n', [csv_name '...']);
writetable(T, csv_name);
return
end
%%% 3. Extract data from GannetQuantify %%%
if strcmp(MRS_struct.p.reference, 'H2O')
for ii = 1:length(metabs)
if ~isfield(MRS_struct.out.(vox), metabs{ii})
continue
end
out.(metabs{ii}).ConcIU_TissCorr = MRS_struct.out.(vox).(metabs{ii}).ConcIU_TissCorr(:);
out.(metabs{ii}).ConcIU_AlphaTissCorr = MRS_struct.out.(vox).(metabs{ii}).ConcIU_AlphaTissCorr(:);
out.(metabs{ii}).ConcIU_AlphaTissCorr_GrpNorm = MRS_struct.out.(vox).(metabs{ii}).ConcIU_AlphaTissCorr_GrpNorm(:);
out.(metabs{ii}).alpha = repelem(MRS_struct.out.(vox).(metabs{ii}).alpha, ...
length(MRS_struct.out.(vox).(metabs{ii}).ConcIU_AlphaTissCorr_GrpNorm(:)))';
end
end
field_names = fieldnames(out);
for ii = 1:length(field_names)
if any(strcmp(field_names{ii}, metabs))
sub_field_names = fieldnames(out.(field_names{ii}));
for jj = length(sub_field_names)-3:length(sub_field_names)
U = table(round2(out.(field_names{ii}).(sub_field_names{jj})), ...
'VariableNames', {[field_names{ii} '_' sub_field_names{jj}]});
T = [T U];
end
end
end
% Convert empty cells into NaNs
for ii = 1:size(T,2)
if ~iscell(T(:,ii).(T.Properties.VariableNames{ii}))
T(~T(:,ii).(T.Properties.VariableNames{ii}),ii) = {NaN};
end
end
fprintf('\nUpdating results in %s\n', [csv_name '...']);
writetable(T, csv_name);