-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_12ECG_classifier.m
145 lines (106 loc) · 3.08 KB
/
train_12ECG_classifier.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
function model = train_12ECG_classifier(input_directory,output_directory)
disp('Loading data...')
% Find files.
input_files = {};
for f = dir(input_directory)'
if exist(fullfile(input_directory, f.name), 'file') == 2 && f.name(1) ~= '.' && all(f.name(end - 2 : end) == 'mat')
input_files{end + 1} = f.name;
end
end
% read number of unique classes
classes = get_classes(input_directory,input_files);
num_classes = length(classes);
num_files = length(input_files);
Total_data=cell(1,num_files);
Total_header=cell(1,num_files);
% Iterate over files.
for i = 1:num_files
disp([' ', num2str(i), '/', num2str(num_files), '...'])
% Load data.
file_tmp=strsplit(input_files{i},'.');
tmp_input_file = fullfile(input_directory, file_tmp{1});
[data,hea_data] = load_challenge_data(tmp_input_file);
Total_data{i}=data;
Total_header{i}=hea_data;
end
disp('Training model..')
label=zeros(num_files,num_classes);
for i = 1:num_files
disp([' ', num2str(i), '/', num2str(num_files), '...']);
data = Total_data{i};
header_data = Total_header{i};
tmp_features = get_12ECG_features(data,header_data);
features(i,:)=tmp_features;
for j = 1 : length(header_data)
if startsWith(header_data{j},'#Dx')
tmp = strsplit(header_data{j},': ');
tmp_c = strsplit(tmp{2},',');
for k=1:length(tmp_c)
idx=find(strcmp(classes,tmp_c{k}));
label(i,idx)=1;
end
break
end
end
end
model = mnrfit(features,label,'model','hierarchical');
save_12_ECG_model(model,output_directory,classes);
end
function save_12_ECG_model(model,output_directory,classes)
% Save results.
tmp_file = 'finalized_model.mat';
filename=fullfile(output_directory,tmp_file);
save(filename,'model','classes','-v7.3');
disp('Done.')
end
% find unique number of classes
function classes = get_classes(input_directory,files)
classes={};
num_files = length(files);
k=1;
for i = 1:num_files
g = strrep(files{i},'.mat','.hea');
input_file = fullfile(input_directory, g);
fid=fopen(input_file);
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
if startsWith(tline,'#Dx')
tmp = strsplit(tline,': ');
tmp_c = strsplit(tmp{2},',');
for j=1:length(tmp_c)
idx2 = find(strcmp(classes,tmp_c{j}));
if isempty(idx2)
classes{k}=tmp_c{j};
k=k+1;
end
end
break
end
end
fclose(fid);
end
classes=sort(classes);
end
function [data,tlines] = load_challenge_data(filename)
% Opening header file
fid=fopen([filename '.hea']);
if (fid<=0)
disp(['error in opening file ' filename]);
end
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
f=load([filename '.mat']);
try
data = f.val;
catch ex
rethrow(ex);
end
end