-
Notifications
You must be signed in to change notification settings - Fork 28
/
remove_data.py
51 lines (43 loc) · 1.86 KB
/
remove_data.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
#!/usr/bin/env python
# Load libraries.
import os, sys, shutil, argparse
# Parse arguments.
def get_parser():
description = 'Remove data from the dataset.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-i', '--input_folder', type=str, required=True)
parser.add_argument('-p', '--patient_ids', nargs='*', type=str, required=False, default=[])
parser.add_argument('-o', '--output_folder', type=str, required=True)
return parser
# Find folders with data files.
def find_data_folders(root_folder):
data_folders = list()
for x in sorted(os.listdir(root_folder)):
data_folder = os.path.join(root_folder, x)
if os.path.isdir(data_folder):
data_file = os.path.join(data_folder, x + '.txt')
if os.path.isfile(data_file):
data_folders.append(x)
return sorted(data_folders)
# Run script.
def run(args):
# Use either the given patient IDs or all of the patient IDs.
if args.patient_ids:
patient_ids = args.patient_ids
else:
patient_ids = find_data_folders(args.input_folder)
# Iterate over the patient IDs.
for patient_id in patient_ids:
input_path = os.path.join(args.input_folder, patient_id)
output_path = os.path.join(args.output_folder, patient_id)
os.makedirs(output_path, exist_ok=True)
# Iterate over the files in each folder.
for file_name in sorted(os.listdir(input_path)):
file_root, file_ext = os.path.splitext(file_name)
input_file = os.path.join(input_path, file_name)
output_file = os.path.join(output_path, file_name)
# If the file is not the binary signal data, then copy it.
if not (file_ext == '.mat'):
shutil.copy2(input_file, output_file)
if __name__=='__main__':
run(get_parser().parse_args(sys.argv[1:]))