-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathextract_leads_wfdb.py
72 lines (58 loc) · 2.98 KB
/
extract_leads_wfdb.py
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
#!/usr/bin/env python
# Load libraries.
import os, sys, argparse
from scipy.io import savemat
from helper_code import find_challenge_files, load_header, load_recording, get_leads
# Parse arguments.
def get_parser():
description = 'Extract reduced-lead ECGs from WFDB signal and header data.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-i', '--input_directory', type=str, required=True)
parser.add_argument('-k', '--key', type=str, required=False, default='val')
parser.add_argument('-l', '--reduced_leads', type=str, nargs='*', required=True)
parser.add_argument('-o', '--output_directory', type=str, required=True)
return parser
# Run script.
def run(args):
# Identify the reduced leads.
reduced_leads = args.reduced_leads
num_reduced_leads = len(reduced_leads)
# Create a directory for the reduced-lead header and recording files if it does not already exist.
if not os.path.isdir(args.output_directory):
os.mkdir(args.output_directory)
# Identify the full-lead header and recording files.
full_header_files, full_recording_files = find_challenge_files(args.input_directory)
# Extract a reduced-lead set from each pair of full-lead header and recording files.
for full_header_file, full_recording_file in zip(full_header_files, full_recording_files):
# Load the full-lead header file.
full_header = load_header(full_header_file)
full_leads = get_leads(full_header)
num_full_leads = len(full_leads)
# Update the header file.
full_lines = full_header.split('\n')
reduced_lines = list()
# For the first line, update the number of leads.
entries = full_lines[0].split()
entries[1] = str(num_reduced_leads)
reduced_lines.append(' '.join(entries))
# For the next lines, extract the lead metadata but reorder as needed.
reduced_indices = list()
for i in range(num_reduced_leads):
j = full_leads.index(reduced_leads[i])
reduced_lines.append(full_lines[j+1])
# For the remaining lines, extract the rest of the data as-is.
for j in range(num_full_leads+1, len(full_lines)):
reduced_lines.append(full_lines[j])
# Save the reduced-lead header file.
head, tail = os.path.split(full_header_file)
reduced_header_file = os.path.join(args.output_directory, tail)
with open(reduced_header_file, 'w') as f:
f.write('\n'.join(reduced_lines))
# Load the full-lead recording file, extract the lead data, and save the reduced-lead recording file.
recording = load_recording(full_recording_file, full_header, reduced_leads, args.key)
d = {args.key: recording}
head, tail = os.path.split(full_recording_file)
reduced_recording_file = os.path.join(args.output_directory, tail)
savemat(reduced_recording_file, d, format='4')
if __name__=='__main__':
run(get_parser().parse_args(sys.argv[1:]))