forked from DIDSR/eeDAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoad_Input_File.m
484 lines (448 loc) · 17.9 KB
/
Load_Input_File.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
% ##########################################################################
%% ######################### LOAD INPUT FILE ################################
% ##########################################################################
function Load_Input_File(handles)
try
%--------------------------------------------------------------------------
% Load_Input_File ensures that the input file defining the set of
% evaluation tasks for the current session is loaded into the myData
% structure
% Most input variables are saved in a structure called settings
% The information related to wsi files are stored in a cell array of
% structures called wsi_files
% Both "settings" and "wsi_files" are stored in myData
%--------------------------------------------------------------------------
myData = handles.myData;
addpath('bfmatlab');
%----------------------------------------------------------------------
%----------------------------------------------------------------------
settings = struct;
% The file at the path Path_to_Question_File is opened
fid = fopen(myData.workdir_inputfile);
%----------------------------------------------------------------------
% Read in the header information. Stop at 'SETTINGS'
% myData.InputFileHeader is a matrix where every cell is a
% string that contains one line of the header.
%----------------------------------------------------------------------
tline = fgets(fid);
found=strfind(tline,'SETTINGS');
i=0;
while (~feof(fid)) && isempty(found)
i=i+1;
myData.InputFileHeader{i,:}=tline;
tline = fgets(fid);
[found]=strfind(tline,'SETTINGS');
end
%----------------------------------------------------------------------
% Read in the settings information.
% The number of slots being used and the corresponding filenames will
% be saved in myData.
%----------------------------------------------------------------------
tline = fgets(fid);
% Read in the number of slots defined
field = textscan(tline,'%s %d','delimiter','=');
name = 'NUMBER_OF_WSI';
if strcmp(strtrim(char(field{1})),name)==1
n_wsi = field{2};
settings.n_wsi = n_wsi;
else
io_error(name);
end
% Read in the file names of the WSI and the RGB look up table
% corresponding to each WSI image
wsi_files = cell(n_wsi,1);
for i=1:n_wsi
% Read in the file name of the WSI
tline = fgets(fid);
field = textscan(tline,'%s %s','delimiter','=');
name = ['wsi_slot_',num2str(i,'%d')];
if strcmp(strtrim(char(field{1})),name)==1
% Check to see if filename is relative or absolute
% If not absolute, make it absolute
temp_absolute = char(field{2});
temp_relative = char([myData.workdir, field{2}]);
if ~isempty(dir( temp_absolute ))
wsi_fullname = char(cellstr(temp_absolute));
elseif ~isempty(dir( temp_relative ))
wsi_fullname = char(cellstr(temp_relative));
else
desc = sprintf('WSI does not exist. \nFilename = %s\nSlot Number = %d\n\nYou will be prompted to browse for one.', temp_absolute, i);
errordlg(desc,'Application error.','modal');
uiwait
[f_name, d_name] = ...
uigetfile('*', ['Browse to find image for ', name]);
wsi_fullname = [d_name, f_name];
desc = sprintf('The filename with full path is \n\n %s', wsi_fullname);
errordlg(desc,'Application error recovery.','modal');
uiwait
end
else
name = [name ': does not match expected label: ' strtrim(char(field{1}))];
io_error(name);
end
% Get WSI information
WSI_data = bfGetReader(wsi_fullname);
% get size of each resolution, Ignore the last two because there
% are image with label
numberOfImages = WSI_data.getSeriesCount();
for j = 0 : numberOfImages - 3
WSI_data.setSeries(j);
wsi_w(j+1)= WSI_data.getSizeX();
wsi_h(j+1)= WSI_data.getSizeY();
end
wsi_files{i}.WSI_data = WSI_data;
wsi_files{i}.fullname=wsi_fullname;
wsi_files{i}.wsi_w = wsi_w;
wsi_files{i}.wsi_h = wsi_h;
% Read in the file name of the RGB look up table
tline = fgets(fid);
field = textscan(tline,'%s %s','delimiter','=');
name = ['rgb_lut_slot_',num2str(i,'%d')];
if strcmp(strtrim(char(field{1})),name)==1
% Check to see if filename is relative or absolute
% If not absolute, make it absolute
temp_absolute = char(field{2});
temp_relative = char([myData.workdir, field{2}]);
if ~isempty(dir( temp_absolute ))
rgb_lut_filename = char(cellstr(temp_absolute));
elseif ~isempty(dir( temp_relative ))
rgb_lut_filename = char(cellstr(temp_relative));
else
name = ['WSI does not exist. Filename = ',temp_absolute];
io_error(name);
end
fid2 = fopen(rgb_lut_filename);
rgb_lut = uint8(zeros(256,3));
for channel=1:3
for level=1:256
tline2 = fgets(fid2);
rgb_lut(level,channel) = uint8(str2double(tline2));
end
end
wsi_files{i}.rgb_lutfilename = rgb_lut_filename;
wsi_files{i}.rgb_lut = rgb_lut;
fclose(fid2);
else
io_error(name);
end
end
myData.wsi_files = wsi_files;
handles.myData=myData;
guidata(handles.Administrator_Input_Screen,handles);
tline = fgets(fid);
% The Reticle ID is recorded into settings structure
[setting_name, setting_value]=strread(tline, '%s %s', 'delimiter', '='); %#ok<*REMFF1,*FPARK>
name = 'label_pos';
if strcmp(strtrim(setting_name),name)==1
settings.label_pos=char(setting_value);
else
io_error(name);
end
% label_pos on glass slide relative to the microscope operator
% in units of the clock (0,3,6,9,12)
% The typical microscope image in the eyepiece is rotated by 6 hours
% relative to the position on the stage.
% The typical label position of a WSI is at 9 o'clock
label_pos=str2num(settings.label_pos);
label_pos = mod(label_pos, 12);
% RotateWSI maps the rotation to the code expected by TIFFcomp
switch label_pos
case 0 % Label position of microscope image in eyepiece is 6:00
settings.RotateWSI = 1*90; % Rotate WSI CW 9 hours; 9:00 wsi->6:00 in eyepiece = 12:00 on stage
case 3 % Label position of microscope image in eyepiece is 9:00
settings.RotateWSI = 0*90; % No WSI rotation needed; 9:00 wsi->9:00 in eyepiece = 3:00 on stage
case 6 % Label position of microscope image in eyepiece is 12:00
settings.RotateWSI = 3*90; % Rotate WSI CW 3 hours; 9:00 wsi->12:00 in eyepiece = 6:00 on stage
case 9 % Label position of microscope image in eyepiece is 3:00
settings.RotateWSI = 2*90; % Rotate wsi CW 6 hours; 9:00 wsi->3:00 in eyepiece = 9:00 on stage
end
tline = fgets(fid);
% The Reticle ID is recorded into settings structure
[setting_name, setting_value]=strread(tline, '%s %s', 'delimiter', '='); %#ok<*REMFF1,*FPARK>
name = 'reticleID';
if strcmp(strtrim(setting_name),name)==1
settings.reticleID=char(setting_value);
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %s', 'delimiter', '=');
name = 'cam_kind';
if strcmp(strtrim(setting_name),name)==1
settings.cam_kind=char(setting_value);
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %s', 'delimiter', '=');
name = 'cam_format';
if strcmp(strtrim(setting_name),name)==1
settings.cam_format=char(setting_value);
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %f', 'delimiter', '=');
name = 'cam_pixel_size';
if strcmp(strtrim(setting_name),name)==1
settings.cam_pixel_size=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %f', 'delimiter', '=');
name = 'mag_cam';
if strcmp(strtrim(setting_name),name)==1
settings.mag_cam=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %f', 'delimiter', '=');
name = 'mag_lres';
if strcmp(strtrim(setting_name),name)==1
settings.mag_lres=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %f', 'delimiter', '=');
name = 'mag_hres';
if strcmp(strtrim(setting_name),name)==1
settings.mag_hres=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %f', 'delimiter', '=');
name = 'scan_scale';
if strcmp(strtrim(setting_name),name)==1
settings.scan_scale=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %s', 'delimiter', '=');
name = 'stage_label';
if strcmp(strtrim(setting_name),name)==1
myData.stage.label=char(setting_value);
else
io_error(name);
end
% Create reticle mask for the scanned image
pixel_size = settings.scan_scale * settings.mag_hres;
settings.scan_mask = ...
reticle_make_mask(settings.reticleID, pixel_size, [0,0]);
tline = fgets(fid);
[setting_name, tempR, tempG, tempB] =...
strread(tline, '%s %f %f %f', 'delimiter', '=');
name = 'BG_Color_RGB';
if strcmp(strtrim(setting_name),name)==1
settings.BG_color(1)=tempR;
settings.BG_color(2)=tempG;
settings.BG_color(3)=tempB;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, tempR, tempG, tempB] =...
strread(tline, '%s %f %f %f', 'delimiter', '=');
name = 'FG_Color_RGB';
if strcmp(strtrim(setting_name),name)==1
settings.FG_color(1)=tempR;
settings.FG_color(2)=tempG;
settings.FG_color(3)=tempB;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, tempR, tempG, tempB] =...
strread(tline, '%s %f %f %f', 'delimiter', '=');
name = 'AxesBG_Color_RGB';
if strcmp(strtrim(setting_name),name)==1
settings.Axes_BG(1)=tempR;
settings.Axes_BG(2)=tempG;
settings.Axes_BG(3)=tempB;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %d', 'delimiter', '=');
name = 'FontSize';
if strcmp(strtrim(setting_name),name)==1
settings.FontSize=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %d', 'delimiter', '=');
name = 'saveimages';
if strcmp(strtrim(setting_name),name)==1
settings.saveimages=setting_value;
else
io_error(name);
end
tline = fgets(fid);
[setting_name, setting_value]=strread(tline, '%s %d', 'delimiter', '=');
name = 'taskorder';
if strcmp(strtrim(setting_name),name)==1
settings.taskorder=setting_value;
else
io_error(name);
end
while (~feof(fid)) && isempty(strfind(fgets(fid),'BODY'))
end
%----------------------------------------------------------------------
% Each row in the BODY of the input file defines an ROI and a task
% The first row defines the "start" task
% The second row defines the "finish" task
% The remaining rows are the individual tasks themselves
% All tasks are stored in cell array and sorted according to taskorder
% The start task goes at the beginning
% The finish task goes at the end
%----------------------------------------------------------------------
st = dbstack;
calling_function = st(1).name;
% Read and store the task_start description
tline = fgets(fid);
temp = textscan(tline, '%s', 'delimiter', ',');
temp = temp{1};
taskinfo = struct;
taskinfo.desc = temp;
% Read the task_type and create the task_handle
% The task_handle points to the task_type.m file
taskinfo.task = char(temp{1});
taskinfo.task_handle = str2func(['@task_',taskinfo.task]);
% Call the task_type function. Here we read in the taskinfo.
taskinfo.calling_function = calling_function;
handles.myData.taskinfo = taskinfo;
handles.myData.taskinfo.duration = 0;
guidata(handles.Administrator_Input_Screen, handles);
taskinfo.task_handle(handles.Administrator_Input_Screen);
% Update the handles and task_start structure
handles = guidata(handles.Administrator_Input_Screen);
task_start = handles.myData.taskinfo;
% Read and store the task_finish description
tline = fgets(fid);
temp = textscan(tline, '%s', 'delimiter', ',');
temp = temp{1};
taskinfo = struct;
taskinfo.desc = temp;
% Read the task_type and create the task_handle
% The task_handle points to the task_type.m file
taskinfo.task = char(temp{1});
taskinfo.task_handle = str2func(['@task_',taskinfo.task]);
% Call the task_type function. Here we read in the taskinfo.
taskinfo.calling_function = calling_function;
handles.myData.taskinfo = taskinfo;
handles.myData.taskinfo.duration = 0;
guidata(handles.Administrator_Input_Screen, handles);
taskinfo.task_handle(handles.Administrator_Input_Screen);
% Update the handles and task_finish structure
handles = guidata(handles.Administrator_Input_Screen);
task_finish = handles.myData.taskinfo;
% tasks_in structure will hold all the input tasks
tasks_in = [];
ntasks = 0;
while ~feof(fid)
% Read and store the taskinfo
tline = fgets(fid);
temp = textscan(tline, '%s', 'delimiter', ',');
if isempty(temp{1})
break
else
ntasks=ntasks+1;
end
temp = temp{1};
taskinfo = struct;
taskinfo.desc = temp;
% Read the task_type and create the task_handle
taskinfo.task = char(temp{1});
taskinfo.task_handle = str2func(['@task_',taskinfo.task]);
% Call the task_type function. Here we read in the taskinfo.
taskinfo.calling_function = calling_function;
handles.myData.taskinfo = taskinfo;
guidata(handles.Administrator_Input_Screen, handles);
taskinfo.task_handle(handles.Administrator_Input_Screen);
% Update the handles and taskinfo structure
handles = guidata(handles.Administrator_Input_Screen);
handles.myData.taskinfo.duration = 0;
tasks_in{ntasks} = handles.myData.taskinfo; %#ok<AGROW>
end
% The file is closed
fclose(fid);
% Create a random order
if settings.taskorder == 0
order_vector = randperm(ntasks);
for i=1:ntasks
order = order_vector(i);
tasks_in{i}.order = order; %#ok<AGROW>
end
display('random order')
end
% Create the listed order
if settings.taskorder == 1
for i=1:ntasks
tasks_in{i}.order = i; %#ok<AGROW>
end
display('list order')
end
% Use/check the order given in the input file
if settings.taskorder == 2
order_vector = zeros(ntasks);
for i=1:ntasks
order_vector(i) = tasks_in{i}.order;
end
order_check = sort(order_vector);
for i=1:ntasks
if order_check(i) ~= i
display(num2str(order_vector))
field = 'Error in user specified order';
io_error(field);
end
end
display('user defined order')
end
% tasks_out holds task_in sorted according to taskorder
% it also has task_start as the first task and task_finish as the last
% therefore it has ntasks+2 elements
ntasks_out = ntasks+2;
tasks_out = cell(1,ntasks_out); %#ok<NASGU>
% Sort the tasks in order
tasks_out = tasks_in;
for i=1:ntasks
order = tasks_in{i}.order;
tasks_out{order} = tasks_in{i};
end
tasks_out(2:ntasks+1) = tasks_out(1:ntasks);
tasks_out{1} = task_start;
tasks_out{ntasks+2} = task_finish;
temp = get(0, 'ScreenSize');
myData.ScreenWidth = temp(3);
myData.ScreenHeight = temp(4);
% The rest of the myData structure is created
myData.ntasks = ntasks;
myData.ntasks_out = ntasks_out;
myData.iter = 1;
myData.wsi_files = wsi_files;
myData.settings = settings;
myData.tasks_in = tasks_in;
myData.tasks_out = tasks_out;
myData.task_start = task_start;
myData.task_finish = task_finish;
% Pack myData into handles
% Update handles.Administrator_Input_Screen
handles.myData=myData;
guidata(handles.Administrator_Input_Screen,handles);
catch ME
error_show(ME);
end
end
function io_error(name)
desc = char('ERROR: Inputfile not formatted correctly.',name);
disp(desc)
dbstack()
h_errordlg = errordlg(desc,'Application error','modal');
uiwait(h_errordlg)
keyboard
end