-
Notifications
You must be signed in to change notification settings - Fork 5
/
team_training_code.m
138 lines (88 loc) · 3.51 KB
/
team_training_code.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
function model = team_training_code(input_directory,output_directory) % train_PCG_classifier
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Purpose: Train PCG classifiers and obtain the models
% Inputs:
% 1. input_directory
% 2. output_directory
%
% Outputs:
% 1. model: trained model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find text files
patient_files=dir(fullfile(input_directory,'*.txt'));
patient_files={patient_files.name};
patient_files=sort(patient_files); % To help debugging
num_patient_files=length(patient_files);
fprintf('Loading data for %d patients...\n', num_patient_files)
% Extract classes from data
classes_murmur={};
classes_outcome={};
for j=1:num_patient_files
current_class_murmur=get_class_murmur(fullfile(input_directory,patient_files{j}));
classes_murmur=unique([classes_murmur current_class_murmur]);
current_class_outcome=get_class_outcome(fullfile(input_directory,patient_files{j}));
classes_outcome=unique([classes_outcome current_class_outcome]);
end
classes_murmur=sort(classes_murmur);
num_classes_murmur=length(classes_murmur);
classes_outcome=sort(classes_outcome);
num_classes_outcome=length(classes_outcome);
% Extracting features and labels
disp('Extracting features and labels...')
features=[];
labels_murmur=categorical;
labels_outcome=categorical;
for j=1:num_patient_files
fprintf('%d/%d \n',j,num_patient_files)
current_header=get_header(fullfile(input_directory,patient_files{j}));
current_recordings=load_recordings(input_directory,current_header);
current_features=get_features(current_header, current_recordings);
features(j,:) = current_features(:);
labels_murmur(j)=get_class_murmur(fullfile(input_directory,patient_files{j}));
labels_outcome(j)=get_class_outcome(fullfile(input_directory,patient_files{j}));
end
%% train RF
disp('Training the model...')
model_murmur = TreeBagger(300,features,labels_murmur);
model_outcome = TreeBagger(300,features,labels_outcome);
save_model(model_murmur,classes_murmur,model_outcome,classes_outcome,output_directory);
disp('Done.')
end
function save_model(model_murmur,classes_murmur,model_outcome,classes_outcome,output_directory) %save_PCG_model
% Save results.
filename = fullfile(output_directory,'model.mat');
save(filename,'model_murmur','classes_murmur','model_outcome','classes_outcome','-v7.3');
disp('Done.')
end
function class=get_class_murmur(input_header)
current_header=get_header(input_header);
class=current_header(startsWith(current_header,'#Murmur'));
class=strsplit(class{1},':');
class=strtrim(class{2});
end
function class=get_class_outcome(input_header)
current_header=get_header(input_header);
class=current_header(startsWith(current_header,'#Outcome'));
class=strsplit(class{1},':');
class=strtrim(class{2});
end
function current_header=get_header(input_header)
current_header=fileread(input_header);
current_header=strsplit(current_header,'\n');
end
function current_recordings=load_recordings(input_directory,current_header)
recording_files=get_recording_files(current_header);
current_recordings={};
for j=1:length(recording_files)
current_recordings{j}=audioread(fullfile(input_directory,strtrim(recording_files{j})));
end
end
function recording_files=get_recording_files(current_header)
recording_files={};
num_locations=strsplit(current_header{1},' ');
num_locations=str2double(num_locations{2});
for j=2:num_locations+1
current_line=strsplit(current_header{j},' ');
recording_files{j-1}=current_line{3};
end
end