-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheyeflowcli.m
More file actions
294 lines (227 loc) · 8.33 KB
/
eyeflowcli.m
File metadata and controls
294 lines (227 loc) · 8.33 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
function eyeflowcli(varargin)
% Initialize Application
appRoot = fileparts(mfilename('fullpath'));
versionFile = fullfile(appRoot, "version.txt");
if isfile(versionFile)
version_tag = readlines(versionFile);
fprintf("EyeFlow version : %s\n", version_tag);
else
fprintf("EyeFlow version : Unknown (version.txt not found at %s)\n", versionFile);
end
beginComputerTime = datetime('now', 'Format', 'yyyy/MM/dd HH:mm:ss');
fprintf("Begin Computer Time: %s\n", beginComputerTime);
if ~isdeployed
% In Development: Add all subfolders to path
fprintf("Running in Development Mode\n");
addpath(fullfile(appRoot, "BloodFlowVelocity"));
addpath(fullfile(appRoot, "BloodFlowVelocity", "Elastography"));
addpath(fullfile(appRoot, "CrossSection"));
addpath(fullfile(appRoot, "Loading"));
addpath(fullfile(appRoot, "Parameters"));
addpath(fullfile(appRoot, "Preprocessing"));
addpath(fullfile(appRoot, "Scripts"));
addpath(fullfile(appRoot, "Segmentation"));
addpath(fullfile(appRoot, "SHAnalysis"));
addpath(fullfile(appRoot, "Tools"));
addpath(fullfile(appRoot, "Outputs"));
else
fprintf("Running in Deployed Mode.\n");
end
fprintf("Application root is %s\n", appRoot);
% --- ARGUMENT PARSING ---
mode = 'single';
inputPath = "";
customConfigPath = "";
useSafeMode = false;
idx = 1;
while idx <= nargin
arg = string(varargin{idx});
switch arg
case "-batch"
mode = 'batch';
case "-safe"
useSafeMode = true;
case "-config"
% Check if next argument exists and isn't another flag
if idx < nargin && ~startsWith(string(varargin{idx + 1}), "-")
customConfigPath = string(varargin{idx + 1});
idx = idx + 1; % Skip next arg as it was the path
else
% Open file explorer for config
[cfg_name, cfg_path] = uigetfile('*.json', 'Select Custom EyeFlow Parameters JSON');
if isequal(cfg_name, 0)
fprintf("No config selected, using defaults.\n");
else
customConfigPath = fullfile(cfg_path, cfg_name);
end
end
otherwise
% Assume this is the input path (data folder or batch file)
inputPath = arg;
end
idx = idx + 1;
end
% --- CONFIG SELECTION ---
% Priority: 1. Custom (-config) | 2. Safe (-safe) | 3. Default
if customConfigPath ~= "" && isfile(customConfigPath)
selectedJson = customConfigPath;
fprintf("Using Custom Config: %s\n", selectedJson);
elseif useSafeMode
selectedJson = fullfile(appRoot, "Parameters", "DefaultEyeFlowParamsBatchSafeMode.json");
fprintf("Using Safe Mode Config.\n");
else
selectedJson = fullfile(appRoot, "Parameters", "DefaultEyeFlowParamsBatch.json");
fprintf("Using Standard Config.\n");
end
if ~isfile(selectedJson)
error("CRITICAL: Configuration file not found: %s", selectedJson);
end
% --- PATH SELECTION ---
try
if strcmp(mode, 'batch')
if inputPath == ""
[txt_name, txt_path] = uigetfile('*.txt', 'Select the list of HoloDoppler processed folders');
if isequal(txt_name, 0), return; end
fullInputPath = fullfile(txt_path, txt_name);
else
fullInputPath = inputPath;
if ~isfile(fullInputPath), error("Batch file not found: %s", fullInputPath); end
end
fprintf("Running in Batch Mode: %s\n", fullInputPath);
rawInputs = strtrim(readlines(fullInputPath));
rawInputs = rawInputs(rawInputs ~= "");
else
if inputPath == ""
[holo_name, holo_path] = uigetfile('*.holo', 'Select the .holo file');
if isequal(holo_name, 0), return; end
fullInputPath = fullfile(holo_path, holo_name);
else
fullInputPath = inputPath;
end
fprintf("Running in Single Mode: %s\n", fullInputPath);
rawInputs = string(fullInputPath);
end
catch ME
fprintf("Error during path selection: %s\n", ME.message);
return;
end
% Resolve .holo files to HD folders
paths = string.empty;
for k = 1:length(rawInputs)
currentInput = rawInputs(k);
if currentInput == "" || ismissing(currentInput); continue; end
if endsWith(currentInput, ".holo", 'IgnoreCase', true)
latestHD = findLatestHDFolder(currentInput);
if ~ismissing(latestHD)
paths(end + 1, 1) = latestHD;
end
elseif isfolder(currentInput)
paths(end + 1, 1) = currentInput;
end
end
if isempty(paths)
fprintf("No valid paths to process. Exiting.\n");
return;
end
% --- PRE-PROCESSING (JSON & MASKS) ---
for ind = 1:length(paths)
targetPath = paths(ind);
efPath = fullfile(targetPath, 'eyeflow');
jsonDir = fullfile(efPath, 'json');
maskDir = fullfile(efPath, 'mask');
if ~isfolder(jsonDir), mkdir(jsonDir); end
% Setup Config JSON
delete(fullfile(jsonDir, '*.json'));
copyfile(selectedJson, fullfile(jsonDir, 'input_EF_params.json'));
% Handle Masks
if isfile(fullfile(maskDir, 'forceMaskArtery.png'))
movefile(fullfile(maskDir, 'forceMaskArtery.png'), fullfile(maskDir, 'oldForceMaskArtery.png'));
end
if isfile(fullfile(maskDir, 'forceMaskVein.png'))
movefile(fullfile(maskDir, 'forceMaskVein.png'), fullfile(maskDir, 'oldForceMaskVein.png'));
end
end
% --- SETUP PARPOOL ---
% maxWorkers = parcluster("local").NumWorkers;
% params_names = checkEyeFlowParamsFromJson(rawInputs(1)); % checks compatibility between found EF params and Default EF params of this version of EF.
% params = Parameters_json(rawInputs(1), params_names{1});
% if params.json.NumberOfWorkers > 0 && params.json.NumberOfWorkers < maxWorkers
% fprintf("Using nb of workers stored inside the parameters.json (%i)", params.json.NumberOfWorkers);
% setupParpool(params.json.NumberOfWorkers);
% end
% --- AI LOADING & EXECUTION ---
fprintf("Loading AI Models...\n");
AIModels = AINetworksClass();
for ind = 1:length(paths)
p = paths(ind);
if p == "" || ismissing(p); continue; end
if isfolder(p) && ~endsWith(p, filesep), p = strcat(p, filesep); end
fprintf("Processing: %s\n", p);
runAnalysisBlock(p, AIModels);
end
endComputerTime = datetime('now', 'Format', 'yyyy/MM/dd HH:mm:ss');
fprintf("All done! Total time elapsed: %s\n", string(endComputerTime - beginComputerTime));
end
function runAnalysisBlock(path, AIModels)
totalTime = tic;
ME = [];
try
ExecClass = ExecutionClass(path);
ExecClass.AINetworks = AIModels;
% Ensure ExecutionClass uses the directory passed to it
ExecClass.ToolBoxMaster = ToolBoxClass(ExecClass.directory, ExecClass.param_name);
ExecClass.preprocessData();
ExecClass.flag_segmentation = 1;
ExecClass.flag_spectral_analysis = 1;
ExecClass.flag_bloodFlowVelocity_analysis = 1;
ExecClass.flag_pulseWaveVelocity = 1;
ExecClass.flag_crossSection_analysis = 1;
ExecClass.flag_crossSection_export = 1;
ExecClass.analyzeData([]);
catch e
ME = e;
% Check if MEdisp exists, otherwise use disp
if exist('MEdisp', 'file')
MEdisp(e, path);
else
disp(e.message);
end
end
try
ExecClass.Reporter.getA4Report(ME);
ExecClass.Reporter.saveOutputs();
ExecClass.Reporter.displayFinalSummary(totalTime);
catch e
if exist('MEdisp', 'file')
MEdisp(e, path);
else
disp(e.message);
end
end
end
function latestHDPath = findLatestHDFolder(holoPath)
% Finds the HD folder with the highest render number in the same directory as the holo file
% Naming convention: [holo_filename]_HD_[RENDER_NUMBER]
latestHDPath = string(missing);
[parentDir, fname, ~] = fileparts(holoPath);
% Get list of folders in parent directory
contents = dir(parentDir);
dirFlags = [contents.isdir];
subFolders = contents(dirFlags);
maxRenderNum = -1;
% Escaped pattern for regex (escape special regex characters in filename)
escapedName = regexptranslate('escape', fname);
pattern = "^" + escapedName + "_HD_(\d+)$";
for k = 1:length(subFolders)
thisName = string(subFolders(k).name);
tokens = regexp(thisName, pattern, 'tokens');
if ~isempty(tokens)
% Extract the number
renderNum = str2double(tokens{1}{1});
if renderNum > maxRenderNum
maxRenderNum = renderNum;
latestHDPath = fullfile(parentDir, thisName);
end
end
end
end