/
getScanNumFromIdentifiers.m
173 lines (143 loc) · 6.81 KB
/
getScanNumFromIdentifiers.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
function scanNumV = getScanNumFromIdentifiers(idS,planC,origFlag)
% Get scan no. with associated metadata matching supplied list of identifiers.
% ------------------------------------------------------------------------
% INPUTS
% idS : Structure containing identifiers (tags) and expected values
% Supported identifiers include 'imageType', 'seriesDescription',
% 'scanType', 'seriesDate', 'scanNum', and 'assocStructure'.
% planC
%----- Optional ---
% origFlag : Set to 1 to ignore 'warped' and 'filtered' scans (default:0).
%--------------------------------------------------------------------------
% AI 9/18/20
% Get no. scans
indexS = planC{end};
numScan = length(planC{indexS.scan});
%Read list of identifiers
identifierC = fieldnames(idS);
%Filter reserved fields
resFieldsC = {'warped','filtered','resampled'};
for k = 1 : length(resFieldsC)
keyFlag = strcmpi(identifierC,resFieldsC{k});
if any(keyFlag)
identifierC(keyFlag) = [];
end
end
matchIdxV = true(1,numScan);
%Loop over identifiers
for n = 1:length(identifierC)
matchValC = idS.(identifierC{n});
%Match against metadata in planC
switch(identifierC{n})
case 'imageType'
imTypeC = arrayfun(@(x)x.scanInfo(1).imageType,...
planC{indexS.scan},'un',0);
idV = strcmpi(matchValC,imTypeC);
case 'seriesDescription'
seriesDescC = arrayfun(@(x)x.scanInfo(1).seriesDescription,...
planC{indexS.scan},'un',0);
seriesDescC = cellfun(@char,seriesDescC,'un',0);
idV = ~cellfun('isempty', strfind(seriesDescC, matchValC));
case 'scanType'
scanTypeC = {planC{indexS.scan}.scanType};
idV = strcmpi(matchValC,scanTypeC);
case 'scanNum'
idV = false(size(matchIdxV));
idV(matchValC) = true;
case 'seriesDate'
seriesDatesC = arrayfun(@(x)x.scanInfo(1).seriesDate,...
planC{indexS.scan},'un',0);
emptyIdxC = cellfun(@isempty,seriesDatesC,'un',0);
seriesDatesC([emptyIdxC{:}]) = {'00000000'};
seriesTimesC = arrayfun(@(x) strtok(x.scanInfo(1).seriesTime,'.'),...
planC{indexS.scan},'un',0);
emptyIdxC = cellfun(@isempty,seriesTimesC,'un',0);
seriesTimesC([emptyIdxC{:}]) = {'000000.00'};
seriesDateTimesC = cellfun(@(x,y) [x,':',y], seriesDatesC, ...
seriesTimesC,'un',0);
seriesDateTimesC = datetime(seriesDateTimesC,'InputFormat','yyyyMMdd:HHmmss');
[sortSeriesDateTimesC,ordV] = sort(seriesDateTimesC,'ascend');
idV = false(size(matchIdxV));
if strcmp(matchValC,'first')
if length(ordV)>1 && ordV(1)==ordV(2)
error(['Error identifiying first scan. Scans ',...
'%d and %d have identical series dates/times:' ...
' %s '],ordV(1),ordV(2),sortSeriesDateTimesC{1})
end
idV(ordV(1)) = true;
elseif strcmp(matchValC,'last')
if length(ordV)>1 && ordV(end)==ordV(end-1)
error(['Error identifiying last scan. Scans ',...
'%d and %d have identical series date & time:' ...
' %s '],ordV(end-1),ordV(end),sortSeriesDateTimesC{end})
end
idV(ordV(end)) = true;
else
error(['seriesDate value ''',matchValC,''' is not supported.'])
end
case 'studyDate'
studyDatesC = arrayfun(@(x)x.scanInfo(1).studyDate,...
planC{indexS.scan},'un',0);
emptyIdxC = cellfun(@isempty,studyDatesC,'un',0);
studyDatesC([emptyIdxC{:}]) = {'00000000'};
studyTimesC = arrayfun(@(x) strtok(x.scanInfo(1).studyTime,'.'),...
planC{indexS.scan},'un',0);
emptyIdxC = cellfun(@isempty,studyTimesC,'un',0);
studyTimesC([emptyIdxC{:}]) = {'000000.00'};
studyDateTimesC = cellfun(@(x,y) [x,':',y], studyDatesC, ...
studyTimesC,'un',0);
studyDateTimesC = datetime(studyDateTimesC,'InputFormat','yyyyMMdd:HHmmss');
[sortStudyDateTimesC,ordV] = sort(studyDateTimesC,'ascend');
idV = false(size(matchIdxV));
if strcmp(matchValC,'first')
if length(ordV)>1 && isequal(sortStudyDateTimesC(ordV(1)),...
sortStudyDateTimesC(ordV(2)))
error(['Error identifiying first scan. Scans ',...
'%d and %d have identical study dates & time:' ...
' %s '],ordV(1),ordV(2),sortStudyDateTimesC(1))
end
idV(ordV(1)) = true;
elseif strcmp(matchValC,'last')
if length(ordV)>1 && isequal(sortStudyDateTimesC(ordV(end-1)),...
sortStudyDateTimesC(ordV(end)))
error(['Error identifiying first scan. Scans ',...
'%d and %d have identical series dates/times:' ...
' %s '],ordV(end-1),ordV(end),sortStudyDateTimesC(end))
end
idV(ordV(end)) = true;
else
error(['studyDate value ''',matchValC,''' is not supported.'])
end
case 'assocStructure'
if strcmp(matchValC,'none')
strAssocScanV = unique([planC{indexS.structures}.associatedScan]);
idV = ~ismember(1:numScan,strAssocScanV);
else
idV = true(1,numScan);
scanNumV = 1:numScan;
for nStr = 1:length(matchValC)
strListC = {planC{indexS.structures}.structureName};
strNum = getMatchingIndex(matchValC{nStr},strListC,'EXACT');
matchScan = getStructureAssociatedScan(strNum,planC);
idV = idV & ismember(scanNumV,matchScan);
end
end
otherwise
error('Identifier %s not supported.',identifierC{n});
end
matchIdxV = matchIdxV & idV;
end
%Return matching scan nos.
scanNumV = find(matchIdxV);
if ~exist('origFlag','var') || ~origFlag
if isfield(idS,'filtered') && ~isempty(idS) && idS.filtered
scanNumV = getAssocFilteredScanNum(scanNumV,planC);
end
if isfield(idS,'warped') && ~isempty(idS) && idS.warped
scanNumV = getAssocWarpedScanNum(scanNumV,planC);
end
if isfield(idS,'resampled') && ~isempty(idS) && idS.resampled
scanNumV = getAssocResampledScanNum(scanNumV,planC);
end
end
end