-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.py
193 lines (154 loc) · 5.25 KB
/
process.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
torch.manual_seed(1)
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from tqdm import tqdm
import logging
import argparse
import pickle
import os
import sys
from datetime import datetime
def open_pickle(file_path):
"""Unpickles MFCC data.
Args:
file_path (str): File path with MFCC data.
Returns:
pandas.DataFrame : DataFrame with MFCC data and labels.
"""
infile = open(file_path, "rb")
mfcc_list = pickle.load(infile)
infile.close()
return mfcc_list
def get_label_names(mfcc_list, features_data):
"""Get label names for the MFCC data.
Args:
mfcc_list (pandas.DataFrame): DataFrame for MFCC.
features_data (pandas.DataFrame): DataFrame for features.
Returns:
Pandas.DataFrame: Combined DataFrame.
"""
df = pd.read_csv(features_data)
df["labels"] = range(0, 1000)
mfcc_list = mfcc_list.merge(df, on="labels", how="left")
return mfcc_list
def split_train_test(mfcc_list, test_size=0.2):
"""Train-test split.
Args:
mfcc_list (pandas.DataFrame): MFCC DataFrame.
test_size (float, optional): Test split size. Defaults to 0.2.
Returns:
Multiple: Train-test splits.
"""
X_train, X_test, y_train, y_test = train_test_split(
mfcc_list.mfcc, mfcc_list.label, test_size=test_size, random_state=42
)
return X_train, X_test, y_train, y_test
def one_hot_labels(y_train, y_test=None):
"""Using LabelBinarizer to create one-hot labels.
Args:
y_train (list): Training labels.
y_test (list, optional): Testing labels. Defaults to None.
Returns:
Multiple: Training and/or testing one-hot labels.
"""
le = LabelBinarizer()
y_train_labelled = le.fit_transform(y_train.values)
if not isinstance(y_test, type(None)):
y_test_labelled = le.transform(y_test.values)
return y_train_labelled, y_test_labelled
else:
return y_train_labelled
def main():
if not os.path.isdir("Processed"):
os.mkdir("Processed")
if not os.path.isdir("Logs/Processed"):
os.mkdir("Logs/Processed")
# Parser for CLI configs
parser = argparse.ArgumentParser(description="Cleaning preprocessed data.")
parser.add_argument(
"--file_path",
default="Preprocessed/mfcc_list_time-series_639450_10_60",
type=str,
help="The path to the pickled MFCC file. Defaults to 'Preprocessed/mfcc_list_time-series_639450_10_60'",
)
parser.add_argument(
"--features_data",
default="Data/features_30_sec.csv",
type=str,
help="The path to the CSV file containing feature data. Defaults to 'Data/features_30_sec.csv'",
)
parser.add_argument(
"--train_test_split",
default=0.2,
type=float,
help="Specify the train-test split for training. Disables splitting when set as 0.",
)
opt = parser.parse_args()
FILE_PATH = opt.file_path
FEATURES_DATA = opt.features_data
SPLIT = opt.train_test_split
SPLIT_DECISION = "split"
START_DATETIME = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
if SPLIT == 0:
SPLIT_DECISION = "no_split"
# Logger configs
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(
f"Logs/Processed/{START_DATETIME}_process_{SPLIT_DECISION}_{int(SPLIT * 100)}.log"
),
logging.StreamHandler(sys.stdout),
],
)
# Start
logging.info(f"Taking MFCC data from {FILE_PATH}...")
mfcc_list = open_pickle(FILE_PATH)
logging.info(f"Taking features CSV from {FEATURES_DATA}...")
mfcc_list = get_label_names(mfcc_list, FEATURES_DATA)
if SPLIT_DECISION == "split":
logging.info(f"Splitting into {SPLIT} for test...")
X_train, X_test, y_train, y_test = split_train_test(mfcc_list, SPLIT)
y_train_labelled, y_test_labelled = one_hot_labels(y_train, y_test)
export_list = {
"X_train": X_train,
"X_test": X_test,
"y_train": y_train,
"y_test": y_test,
"y_train_labelled": y_train_labelled,
"y_test_labelled": y_test_labelled,
}
else:
logging.info("Not splitting...")
X_train = mfcc_list.mfcc
y_train = mfcc_list.label
y_train_labelled = one_hot_labels(y_train)
export_list = {
"X_train": X_train,
"y_train": y_train,
"y_train_labelled": y_train_labelled,
}
# Pickling
logging.info(f"Dumping into pickle file...")
if os.path.isfile(
f"Processed/{START_DATETIME}_processed_{SPLIT_DECISION}_{int(SPLIT * 100)}"
):
os.remove(
f"Processed/{START_DATETIME}_processed_{SPLIT_DECISION}_{int(SPLIT * 100)}"
)
outfile = open(
f"Processed/{START_DATETIME}_processed_{SPLIT_DECISION}_{int(SPLIT * 100)}",
"wb",
)
pickle.dump(export_list, outfile)
outfile.close()
logging.info("Done.")
if __name__ == "__main__":
main()