-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathhmri_run_denoising.m
134 lines (112 loc) · 4.65 KB
/
hmri_run_denoising.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
function out = hmri_run_denoising(job)
% loop over subjects in the main function, calling the local function for
% each subject:
for in=1:numel(job.subj)
out.subj(in) = hmri_denoising_local(job.subj(in));
end
end
function out = hmri_denoising_local(jobsubj)
% concatenate all contrasts into jobsubj.mag_img and phase_img
contrasts = {'pdw','t1w','mtw'};
jobsubj.mag_img = {};
jobsubj.phase_img = {};
for c = 1:length(contrasts)
con = contrasts{c};
jobsubj.mag_img = [jobsubj.mag_img; jobsubj.(con).mag_img];
jobsubj.phase_img = [jobsubj.phase_img; jobsubj.(con).phase_img];
end
% Case indir versus outdir
try
outpath = jobsubj.output.outdir{1}; % case outdir
if ~exist(outpath,'dir'); mkdir(outpath); end
catch
Pin = char(jobsubj.mag_img);
outpath = fileparts(Pin(1,:)); % case indir
end
% save outpath as default for this job
hmri_get_defaults('outdir',outpath);
% Directory structure for results:
% <output directory>/Results
% <output directory>/Results/Supplementary
% If repeated runs, <output directory> is replaced by <output
% directory>/Run_xx to avoid overwriting previous outputs.
% define a directory for final results
% RESULTS contains the final denoised maps
% Supplementary containes additonal info and log
respath = fullfile(outpath, 'Results');
newrespath = false;
if exist(respath,'dir')
index = 1;
tmpoutpath = outpath;
while exist(tmpoutpath,'dir')
index = index + 1;
tmpoutpath = fullfile(outpath,sprintf('Run_%0.2d',index));
end
outpath = tmpoutpath;
mkdir(outpath);
respath = fullfile(outpath, 'Results');
newrespath = true;
end
if ~exist(respath,'dir'); mkdir(respath); end
supplpath = fullfile(outpath, 'Results', 'Supplementary');
if ~exist(supplpath,'dir'); mkdir(supplpath); end
% save all these paths in the jobsubj structure
jobsubj.path.dnrespath = respath;
jobsubj.path.respath = respath;
jobsubj.path.supplpath = supplpath;
% save log file location
jobsubj.log.logfile = fullfile(supplpath, 'hMRI_denoising_logfile.log');
jobsubj.log.flags = struct('LogFile',struct('Enabled',true,'FileName','hMRI_denoising_logfile.log','LogDir',supplpath), ...
'PopUp',jobsubj.popup,'ComWin',true);
flags = jobsubj.log.flags;
flags.PopUp = false;
hmri_log(sprintf('\t============ DENOISING MODULE - %s.m (%s) ============', mfilename, datetime('now')),flags);
if newrespath
hmri_log(sprintf(['WARNING: existing results from previous run(s) were found, \n' ...
'the output directory has been modified. It is now:\n%s\n'],outpath),jobsubj.log.flags);
else
hmri_log(sprintf('INFO: the output directory is:\n%s\n',outpath),flags);
end
% check basic requirements and error if basic requirements fail
check_params.mag_img = cellstr(char(spm_file(jobsubj.mag_img,'number','')));
check_params.mag_bool = any(~cellfun(@isempty, check_params.mag_img));
check_params.phase_img = cellstr(char(spm_file(jobsubj.phase_img,'number','')));
check_params.phase_bool = any(~cellfun(@isempty, check_params.phase_img));
% Issue an error and abort in cases of non-existent magnitude image data and
% non-equal number of non-empty phase and magnitude images
if ~check_params.mag_bool || ~isfield(jobsubj,'mag_img')
msg = 'No magnitude images were entered, aborting! Please check your input data and try again!';
hmri_log(msg, flags);
error(msg)
end
if check_params.phase_bool && (length(check_params.mag_img) ~= length(check_params.phase_img))
msg = 'The number of phase and magnitude images are different, please check your input data and try again!';
hmri_log(msg, flags);
error(msg)
end
% save SPM version (slight differences may appear in the results depending
% on the SPM version!)
[v,r] = spm('Ver');
jobsubj.SPMver = sprintf('%s (%s)', v, r);
% save original job to supplementary directory
spm_jsonwrite(fullfile(supplpath,'hMRI_denoising_job.json'),jobsubj,struct('indent','\t'));
% run the denoising and collect the results
hmri_log(sprintf('\t============ %s - %s.m (%s) ============',"APPLYING DENOISING", mfilename, datetime('now')),flags);
% run the denoising
[output_mag, output_phase] = hmri_denoising(jobsubj);
% assign output dependencies to denoised output images of separate contrasts
iMag = 1;
iPhase = 1;
for c = 1:length(contrasts)
con = contrasts{c};
nMag = length(jobsubj.(con).mag_img);
idxstr = ['DenoisedMagnitude' con];
out.(idxstr) = output_mag(iMag:iMag+nMag-1);
iMag = iMag + nMag;
nPhase = length(jobsubj.(con).phase_img);
idxstr = ['DenoisedPhase' con];
out.(idxstr) = output_phase(iPhase:iPhase+nPhase-1);
iPhase = iPhase + nPhase;
end
hmri_log(sprintf('\t============ DENOISING MODULE: completed (%s) ============', datetime('now')),flags);
end