-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpickle_to_csv.py
61 lines (51 loc) · 2.16 KB
/
pickle_to_csv.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
""" Conversion of pickle files to comma-separated-values (csv) file.
You can supply either a directory containing .pkl files via the '-d/--data_dir' flag, or
a single .pkl file using the '--file' flag.
Alexander Neergaard Zahid, 2021.
"""
import argparse
import time
from pathlib import Path
from tqdm import tqdm
import pandas as pd
from src.utils.pickle_reader import read_pickle
def convert_to_csv(filepath):
predictions, targets = read_pickle(filepath)
df = pd.concat(
[pd.DataFrame(targets, columns=["Hypnogram"]), pd.DataFrame(predictions, columns=["W", "N1", "N2", "N3", "R"])],
axis=1,
)
df.to_csv(filepath.parent / (filepath.stem + ".csv"), index=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-d", "--data_dir", type=str, help="Path to directory containing .pkl files to convert.")
parser.add_argument("-f", "--file", type=str, help="Path to specific file to convert.")
args = parser.parse_args()
assert (args.data_dir is not None and args.file is None) or (
args.data_dir is None and args.file is not None
), f"Specify either a data directory or a file, received data_dir={args.data_dir} and file={args.file}"
if args.data_dir is not None:
data_dir = Path(args.data_dir)
list_files = sorted(list(data_dir.glob("**/*.pkl")))
# N = len(list_files)
N = 10
list_files = list_files[:N]
if N == 0:
print(f"No .pkl files found in directory!")
else:
print(f"Starting conversion of {N} .pkl files...")
bar = tqdm(list_files)
start = time.time()
for filepath in bar:
bar.set_description(filepath.stem)
convert_to_csv(filepath)
end = time.time()
print(f"Finished, {N} files converted in {end-start} seconds.")
elif args.file is not None:
N = 1
filepath = Path(args.file)
print(f"Converting file {filepath.stem}...")
start = time.time()
convert_to_csv(filepath)
end = time.time()
print(f"Converted {filepath.stem} in {end-start} seconds.")