-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetLabelInfo_All.m
93 lines (83 loc) · 2.72 KB
/
getLabelInfo_All.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
function getLabelInfo_All(labeled_dir,unlabeled_dir,saveFileName)
% Function to read labeled data for all m-mode scans within a given
% directory. The directory can contain multiple studies, with multiple
% series. The function requires as inputs the location of the manually
% labeled images, the location of the unlabeled images, and a location in
% which the training data should be saved. Data is saved as a csv
% containing the frame number and cropping indices for each scan, labeled
% by scan name.
%
% Mary Kate Montgomery
% March 2021
% Pfizer, Inc
% Check can locate directories
notDir = check_dir({unlabeled_dir,labeled_dir});
if ~isempty(notDir)
disp(['Error: Cannot locate ' strjoin(notDir,' or ')]);
return
end
isFmt = checkFormat(saveFileName);
if ~isFmt
disp('Error: Please enter a .csv or .xlsx save file');
return
end
% Get names of studies
studyNames = getSubfolders(unlabeled_dir);
for study_n = 1:numel(studyNames)
% Set up folder names
labStudyDir = fullfile(labeled_dir,studyNames{study_n});
unlabStudyDir = fullfile(unlabeled_dir,studyNames{study_n});
% Label study
label_study(labStudyDir,unlabStudyDir,saveFileName);
end
end
function missingDirs = check_dir(dirList)
% Function to check if any directories contained in dirList do not exist.
% If so, a list of missing directories is returned. If not, an empty cell
% is returned.
missingDirs = {}; missingCt = 0;
for i = 1:numel(dirList)
if ~isfolder(dirList{i})
missingCt = missingCt + 1;
missingDirs{missingCt} = dirList{i};
end
end
end
function label_study(labeled_dir,unlabeled_dir,saveFileName)
% Function to read labeled data for a study. Locates all subfolders
% within the study and runs the getLabelFrame function on each
% folder.
% Get names of series within study folder
seriesNames = getSubfolders(unlabeled_dir);
% Generate training data for all series within study
for series_n = 1:numel(seriesNames)
% Set up folders
labSeriesDir = fullfile(labeled_dir,seriesNames{series_n});
unlabSeriesDir = fullfile(unlabeled_dir,seriesNames{series_n});
% Label series
getLabelFrame(labSeriesDir,unlabSeriesDir,saveFileName,'all')
end
end
function subFolders = getSubfolders(dirName)
% Function to get names of subfolders within a folder
allfiles = dir(dirName);
dirflags = [allfiles.isdir];
subdirs = allfiles(dirflags);
if numel(subdirs) < 3
subFolders = {''};
return
end
subFolders = cell(numel(subdirs)-2,1);
for i = 3:numel(subdirs); subFolders{i-2} = subdirs(i).name; end
end
function isFmt = checkFormat(f)
% Function to check if file is .csv or .xlsx
tmp = strsplit(f,'.');
fmt = tmp(end);
if max(strcmp(fmt{1},{'csv','xlsx'}))
isFmt = 1;
else
isFmt = 0;
end
end