-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (43 loc) · 1.27 KB
/
utils.py
File metadata and controls
50 lines (43 loc) · 1.27 KB
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
import os
import librosa as lb
from datetime import datetime
import numpy as np
def undersample(X,Y,inst_coords,inst_num, threshold):
X_inst = X[inst_coords]
Y_inst = Y[inst_coords, inst_num] >= threshold
count = np.sum(Y_inst)
i = 0
undersampled_coords = []
for i in range(len(Y_inst)) :
if Y_inst[i] == 1:
undersampled_coords.append(i)
elif count > 0:
undersampled_coords.append(i)
count -= 1
return X_inst[undersampled_coords], Y_inst[undersampled_coords]
def get_instrument_arrays(X, Y, mask, inst_num, threshold):
inst_coords = mask[:, inst_num]
X, Y = undersample(X,Y,inst_coords,inst_num, threshold)
X = get_transformed_array(X)
return X, Y
def get_instrument_arrays_ml(X, Y, mask, inst_num, threshold):
inst_coords = mask[:, inst_num]
X, Y = undersample(X,Y,inst_coords,inst_num, threshold)
X = get_normalized_array(X)
return X, Y
def get_transformed_array(X_old):
X = X_old
shape = X.shape
X = X.astype('float16')
X = X.reshape(shape[0],1, shape[1], shape[2])
X = lb.util.normalize(X)
return X
def get_normalized_array(X_old):
X = X_old
X = X.astype('float16')
X = lb.util.normalize(X)
return X
def create_dir(mode, data):
dir_name = "logs/" + datetime.now().strftime("%m%d%H%M%S") + "-" + mode + "-" + data
os.mkdir(dir_name)
return dir_name