-
Notifications
You must be signed in to change notification settings - Fork 0
/
leuvenLabelsProcessing.m
130 lines (114 loc) · 4.85 KB
/
leuvenLabelsProcessing.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
clear
addpath(genpath('lib'));
minNEvent = 3; % Minimum number of events to consider a burst
maxNEvent = 8; % Maximum number of events in a burst. At maxNEvent+1 may start a new burst that can be labeled as hypo or apnea independently
maxDistEvent = 90; % Maximum distance between events to consider a burst
% Load all files in database directory
dirlist = dir('dataset/annotations/pneumo/');
files = cell([1 length(dirlist)-2]);
for kk = 3:length(dirlist)
files{kk-2} = dirlist(kk).name;
end
load("dataset\AHI.mat"); AHI = AHI.AHI;
% For dataset statistics
nAobsDataset = 0;
nAcenDataset = 0;
nAmixDataset = 0;
nHpopDataset = 0;
nHobsDataset = 0;
for kk = 1:length(files)
subject = split(files{kk},'.');
subject = subject(1);
load(strcat('dataset/annotations/pneumo/',string(files{kk})));
fprintf('Computing subject %s. ',string(subject));
starts = (pneumo{1,1}-1)/500;
ends = starts+pneumo{1,3};
events = [starts ends];
types = pneumo{1,2};
apneas = events(types<5,:);
hypopneas = events(types>4,:);
doubts = [];
% For dataset statistics
nAobsDataset = nAobsDataset+sum(types==2);
nAcenDataset = nAcenDataset+sum(types==3);
nAmixDataset = nAmixDataset+sum(types==4);
nHpopDataset = nHpopDataset+sum(types==5);
nHobsDataset = nHobsDataset+sum(types==6);
fprintf('AHI: %.2f. ',AHI(kk));
fprintf('nAobs: %i, nAcen: %i, nAmix: %i, nHpop: %i, nHobs: %i... ' ...
,sum(types==2),sum(types==3),sum(types==4),sum(types==5),sum(types==6));
% labels: 1=apnea, 2=hypopneas, 3=doubts
events = [apneas; hypopneas; doubts];
labels = [ones(size(apneas,1),1); 2*ones(size(hypopneas,1),1); 3*ones(size(doubts,1),1)];
[events,indexes] = sort(events);
indexes = indexes(:,1);
labels = labels(indexes);
counter = 0;
position = 1;
abnormalSegments = [];
nApnea = [];
nHypo = [];
nDoubt = [];
continues = false;
for ee=1:size(events,1)-1
if events(ee+1,1)-events(ee,2) <= maxDistEvent && ee<size(events,1)-1
counter = counter+1;
if counter == minNEvent
init = ee-minNEvent+1;
abnormalSegments(position,1) = events(init,1); %#ok<*SAGROW> % burst onset
elseif counter == maxNEvent
abnormalSegments(position,2) = events(ee,2); % burst endset
nApnea(position) = sum(labels(init:ee)==1);
nHypo(position) = sum(labels(init:ee)==2);
nDoubt(position) = sum(labels(init:ee)==3);
position = position+1;
init = ee+1;
abnormalSegments(position,1) = events(init,1); %#ok<*SAGROW> % burst onset
counter = 0;
continues = true;
end
else
if counter >= minNEvent || continues
abnormalSegments(position,2) = events(ee,2); % burst endset
nApnea(position) = sum(labels(init:ee)==1);
nHypo(position) = sum(labels(init:ee)==2);
nDoubt(position) = sum(labels(init:ee)==3);
position = position+1;
end
counter = 0;
continues = false;
end
end
clear ee counter position
if numel(abnormalSegments) == 1
abnormalSegments(1,2) = events(end); % All events in a burst (rare)
end
apneaSegments = [];
hypoSegments = [];
if ~isempty(abnormalSegments)
for aa=1:size(abnormalSegments,1)
doubtRatio = nDoubt(aa)/(nApnea(aa)+nHypo(aa)+nDoubt(aa));
if nApnea(aa)>0 || doubtRatio>0.5
apneaSegments = [apneaSegments; abnormalSegments(aa,:)]; %#ok<*AGROW>
else
hypoSegments = [hypoSegments; abnormalSegments(aa,:)];
end
end
end
save(strcat('results/labels/',string(subject),'_labels.mat'),...
'apneas','hypopneas','doubts','apneaSegments','hypoSegments','abnormalSegments');
fprintf('Done\n');
end
nEventsDataset = nAobsDataset+nAcenDataset+nAmixDataset+nHpopDataset+nHobsDataset;
fprintf('\n'); fprintf('Database statistics.\n');
fprintf('nAobs: %i (%.1f%%), nAcen: %i (%.1f%%), nAmix: %i (%.1f%%), nHpop: %i (%.1f%%), nHobs: %i (%.1f%%)\n', ...
nAobsDataset,nAobsDataset/nEventsDataset*100, ...
nAcenDataset,nAcenDataset/nEventsDataset*100, ...
nAmixDataset,nAmixDataset/nEventsDataset*100, ...
nHpopDataset,nHpopDataset/nEventsDataset*100, ...
nHobsDataset,nHobsDataset/nEventsDataset*100);
fprintf('AHI<5: %i (%.1f%%), 5<=AHI<15: %i (%.1f%%), 15<=AHI<30: %i (%.1f%%), AHI>=30: %i (%.1f%%)\n', ...
sum(AHI<5),sum(AHI<5)/numel(AHI)*100, ...
sum(AHI>=5 & AHI<15),sum(AHI>=5 & AHI<15)/numel(AHI)*100, ...
sum(AHI>=15 & AHI<30),sum(AHI>=15 & AHI<30)/numel(AHI)*100, ...
sum(AHI>30),sum(AHI>30)/numel(AHI)*100);