This repository has been archived by the owner on Oct 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpLabelsFromSheetAndFilter.py
142 lines (119 loc) · 4.83 KB
/
dumpLabelsFromSheetAndFilter.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
import pandas as pd
import numpy as np
import os
from scipy import stats
from subprocess import call
def dumpLabelsFromSheet(workbook):
allFiles = [os.path.join(path, name) for path, dirs, files in os.walk("../sounds/") for name in files if ".wav" in name]
# print(allFiles)
menFilenames = []
womenFilenames = []
menFile = 'Docs/filenames/men_filenames.csv'
womenFile = 'Docs/filenames/women_filenames.csv'
if os.path.isfile(menFile):
os.remove(menFile)
if os.path.isfile(womenFile):
os.remove(womenFile)
# Men
labelsWorkbook = pd.read_excel(workbook, header=0, sheetname=1)
columnNames = labelsWorkbook.columns.values
j = 0
for i in range(2, len(columnNames), 3):
columnData = labelsWorkbook[columnNames[i]].dropna().values
columnName = columnNames[i - 2]
# print(columnName)
labelsFileName = ""
for tempFile in allFiles:
if columnName in tempFile:
labelsFileName = tempFile.replace("/sounds/", "/csv/")
labelsFileName = labelsFileName + "_labels.csv"
menFilenames = np.append(menFilenames, tempFile)
print("Dumping", labelsFileName)
pd.DataFrame(columnData).to_csv(labelsFileName, header=False)
pd.DataFrame(menFilenames).to_csv(menFile, header=False, index=False)
# Women
labelsWorkbook = pd.read_excel(workbook, header=0, sheetname=0)
columnNames = labelsWorkbook.columns.valuesudo
j = 0
for i in range(2, len(columnNames), 3):
columnData = labelsWorkbook[columnNames[i]].dropna().values
columnName = columnNames[i - 2]
# print(columnName)
labelsFileName = ""
for tempFile in allFiles:
if columnName in tempFile:
labelsFileName = tempFile.replace("/sounds/", "/csv/")
labelsFileName = labelsFileName + "_labels.csv"
womenFilenames = np.append(womenFilenames, tempFile)
print("Dumping", labelsFileName)
pd.DataFrame(columnData).to_csv(labelsFileName, header=False)
pd.DataFrame(womenFilenames).to_csv(womenFile, header=False, index=False)
return menFile, womenFile
def print_features(localFile):
mfcc = np.nan_to_num(np.array(pd.read_csv(localFile + '_mfcc.csv', header=None), dtype='float64'))
avgOfMfcc = np.mean(mfcc, axis = 0)
for tempMfcc in mfcc:
for i in range(len(tempMfcc)):
tempMfcc[i] = (tempMfcc[i] - avgOfMfcc[i])
return np.mean(mfcc)
def find_zscr(names, means):
zscr = stats.zscore(means)
return dict(zip(names, zscr))
def filterFiles(menFile, womenFile):
printToFileNameMen = menFile
printToFileNameWomen = womenFile
if os.path.isfile(printToFileNameMen):
os.remove(printToFileNameMen)
if os.path.isfile(printToFileNameWomen):
os.remove(printToFileNameWomen)
avgs = {}
for path, directories, filenames in os.walk('../sounds/Men'):
for filename in filenames:
if ".wav" in filename and ".csv" not in filename:
if "angry" in filename or "neutral" in filename or "anger" in filename or "normal" in filename:
localFileName = os.path.join(path, filename)
localFileName = localFileName.replace("/sounds/", "/csv/")
avgs[localFileName] = print_features(localFileName)
means = np.array(list(avgs.values()))
names = np.array(list(avgs.keys()))
scores = find_zscr(names, means)
for s in scores:
printToFile = open(printToFileNameMen, "a+")
if scores[s] > 3 or scores[s] < -3:
ns = "#" + s
print (ns, file = printToFile)
else:
print (s, file = printToFile)
avgs = {}
for path, directories, filenames in os.walk('../sounds/Women'):
for filename in filenames:
if ".wav" in filename and ".csv" not in filename:
if "angry" in filename or "neutral" in filename or "anger" in filename or "normal" in filename:
localFileName = os.path.join(path, filename)
localFileName = localFileName.replace("/sounds/", "/csv/")
avgs[localFileName] = print_features(localFileName)
means = np.array(list(avgs.values()))
names = np.array(list(avgs.keys()))
scores = find_zscr(names, means)
for s in scores:
printToFile = open(printToFileNameWomen, "a+")
if scores[s] > 3 or scores[s] < -3:
ns = "#" + s
print (ns, file = printToFile)
else:
print (s, file = printToFile)
call(["sort", printToFileNameMen, "-o", printToFileNameMen])
call(["sort", printToFileNameWomen, "-o", printToFileNameWomen])
return
def main():
menFile, womenFile = dumpLabelsFromSheet("Docs/Labels_25.xlsx")
if os.path.isfile(menFile):
if os.path.isfile(womenFile):
filterFiles(menFile, womenFile)
print(menFile, "and", womenFile, "have been filtered for outliers.")
else:
print(womenFile, "does not exist.")
else:
print(menFile, "does not exist.")
if __name__ == '__main__':
main()