This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·74 lines (64 loc) · 2.7 KB
/
main.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
#!/usr/bin/python
import sys, csv, commands, operator
from math import sqrt, pow
from os import listdir
from os.path import isfile, join, abspath
def main(infile,directory):
featureset = fileToFeatureSet(abspath(infile).replace(' ','\ '))
db = getDbByDirectory(directory)
return ranking(featureset,db)
def ranking(featureset,db):
ranking = list()
for f in db.keys():
#euclidean distance between normalized flatness and rms
#index to feature mapping: [0->duration,1->specCent,2->specFlat,3->normRMS]
if f != 'meta':
ranking.append([f,
sqrt(pow(((db[f][0]-db['meta'][0])/db['meta'][1]-(featureset[0]-db['meta'][0])/db['meta'][1]),2)+pow(((db[f][1]-db['meta'][2])/db['meta'][3]-(featureset[1]-db['meta'][2])/db['meta'][3]),2)+pow((db[f][2]-featureset[2]),2)+pow((db[f][3]-featureset[3]),2))])
ranking.sort(key=operator.itemgetter(1))
return ranking
def fileToFeatureSet(absfilepath):
rout = commands.getoutput('./r/analysis.r '+absfilepath).split(' ')
rout = filter(None,rout)
if rout[0]!='Error':
return map(float,rout)
def getDbByDirectory(directory):
if(not isfile(directory+'/.mirs.csv')):
db = analyzeDirectory(directory)
saveCreatedDatabase(directory,db)
return db
else:
db = dict()
with open(directory+'/.mirs.csv','rb') as mirsfile:
reader = csv.reader(mirsfile)
for row in reader:
if(row[0]!='filename'):
db[row[0]] = map(float,row[1:])
return db
def analyzeDirectory(directory):
files = [ f for f in listdir(directory) if isfile(join(directory,f)) ]
db = dict()
durBounds = [sys.float_info.max,0]
scBounds = [sys.float_info.max,0]
for f in files:
db[f] = fileToFeatureSet(abspath(directory).replace(' ','\ ')+'/'+f)
if db[f][0] < durBounds[0]:
durBounds[0] = db[f][0]
if db[f][0] > durBounds[1]:
durBounds[1] = db[f][0]
if db[f][1] < scBounds[0]:
scBounds[0] = db[f][1]
if db[f][1] > scBounds[1]:
scBounds[1] = db[f][1]
db['meta'] = durBounds + scBounds
return db
def saveCreatedDatabase(directory, db):
with open(directory+'/.mirs.csv', 'wb') as mirsfile:
writer = csv.writer(mirsfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
# writer.writerow(['filename','duration','spectralCentroid','spectralFlatness','normalizedRMS'])
for f in db.keys():
writer.writerow([f]+db[f])
if __name__ == '__main__':
ranking = main(sys.argv[1], sys.argv[2])
for x in range(0,len(ranking)):
print "{}: {} - Distance: {}".format(x+1,ranking[x][0],ranking[x][1])