-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsamples_to_phrases.py
184 lines (151 loc) · 6.5 KB
/
samples_to_phrases.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
# -*- coding: utf-8 -*-
import argparse
import inspect
import math
import numpy as np
import os
from pprint import pprint
import sys
from lib.collection_utils import *
from lib.io_utils import *
from lib.math_utils import *
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="tmp/samples.csv", help="Input file")
parser.add_argument('-min', dest="MIN_DUR", default=4, type=float, help="Minumum phrase duration in seconds")
parser.add_argument('-max', dest="MAX_DUR", default=30, type=float, help="Maximum phrase duration in seconds")
parser.add_argument('-mins', dest="MIN_SAMPLES", default=8, type=int, help="At least this many samples per phrase")
parser.add_argument('-msd', dest="MAX_SAMPLE_DUR", default=5, type=float, help="Maximum sample duration in seconds")
parser.add_argument('-clarity', dest="CLARITY_THRESHOLD", default=0.4, type=float, help="Mean should be above this clarity")
parser.add_argument('-power', dest="POWER_THRESHOLD", default=0.125, type=float, help="Mean be above this power")
parser.add_argument('-buffer', dest="BUFFER_SIZE", default=4, type=int, help="Analyze this many samples")
parser.add_argument('-minc', dest="MIN_PHRASE_CLARITY", default=30.0, type=float, help="Minimum clarity of a phrase")
parser.add_argument('-maxp', dest="MAX_PHRASES", default=-1, type=int, help="Maximum phrases to retrieve; -1 for all")
parser.add_argument('-maxs', dest="MAX_SAMPLES", default=-1, type=int, help="Maximum samples to retrieve; -1 for all")
parser.add_argument('-out', dest="OUTPUT_FILE", default="output/phrases.csv", help="Output csv file")
parser.add_argument('-probe', dest="PROBE", action="store_true", help="Just show details?")
a = parser.parse_args()
aa = vars(a)
aa["MIN_DUR"] = roundInt(a.MIN_DUR * 1000)
aa["MAX_DUR"] = roundInt(a.MAX_DUR * 1000)
aa["MAX_SAMPLE_DUR"] = roundInt(a.MAX_SAMPLE_DUR * 1000)
# Read files
fieldNames, samples = readCsv(a.INPUT_FILE)
if len(samples) < 1:
print("No samples found in %s" % a.INPUT_FILE)
sys.exit()
# Make sure output dirs exist
makeDirectories(a.OUTPUT_FILE)
# samples = addIndices(samples)
samples = addNormalizedValues(samples, "clarity", "nclarity")
samples = addNormalizedValues(samples, "power", "npower")
def getPhraseDur(samples):
start = samples[0]["start"]
dur = samples[-1]["start"] + samples[-1]["dur"] - start
return (start, dur)
def getPhraseFeatures(samples, clarityKey="clarity", powerKey="power"):
# weights = [s["dur"] for s in samples]
# clarity = np.average([s[clarityKey] for s in samples], weights=weights)
# power = np.average([s[powerKey] for s in samples], weights=weights)
clarity = np.median([s[clarityKey] for s in samples])
power = np.median([s[powerKey] for s in samples])
return (clarity, power)
def addPhrase(phrases, phrase):
global a
count = len(phrase)
if count < a.MIN_SAMPLES:
return phrases
# remove samples at the end that don't reach threshold
for i in range(count):
index = count - i - 1
s = phrase[index]
if s["nclarity"] >= a.CLARITY_THRESHOLD and s["npower"] >= a.POWER_THRESHOLD:
break
else:
phrase.pop()
# not enough samples
if len(phrase) < a.MIN_SAMPLES:
return phrases
start, dur = getPhraseDur(phrase)
# check for valid duration
if a.MIN_DUR <= dur:
clarity, power = getPhraseFeatures(phrase)
mdur = np.median([s["dur"] for s in phrase])
phrases.append({
"filename": phrase[0]["filename"],
"start": start,
"dur": dur,
"count": len(phrase),
"clarity": round(clarity, 3),
"power": round(power, 3),
"medianDur": roundInt(mdur),
"samples": phrase
})
return phrases
phrases = []
currentPhrase = []
for i, s in enumerate(samples):
# sample is too long; check for phrase and continue
if s["dur"] > a.MAX_SAMPLE_DUR:
phrases = addPhrase(phrases, currentPhrase)
currentPhrase = []
continue
# the first sample in a phrase must pass our criteria
if len(currentPhrase) < 1:
if s["nclarity"] >= a.CLARITY_THRESHOLD and s["npower"] >= a.POWER_THRESHOLD:
currentPhrase.append(s)
continue
# check if current phrase is too long
start, dur = getPhraseDur(currentPhrase)
if dur >= a.MAX_DUR:
phrases = addPhrase(phrases, currentPhrase)
currentPhrase = []
# add sample to current phrase
currentPhrase.append(s)
# we need to collect enough samples to analyze
if len(currentPhrase) < a.BUFFER_SIZE:
continue
buffer = currentPhrase[:]
if len(currentPhrase) > a.BUFFER_SIZE:
buffer = currentPhrase[-a.BUFFER_SIZE:]
clarity, power = getPhraseFeatures(buffer, "nclarity", "npower")
# we've reached the end of a phrase, add it
if clarity < a.CLARITY_THRESHOLD or power < a.POWER_THRESHOLD:
phrases = addPhrase(phrases, currentPhrase)
currentPhrase = []
# Add the last phrase
phrases = addPhrase(phrases, currentPhrase)
if len(phrases) < 1:
print("No phrases found")
sys.exit()
# Filter based on clarity
phrases = [p for p in phrases if p["clarity"] >= a.MIN_PHRASE_CLARITY]
if len(phrases) < 1:
print("No phrases with clarity > %s" % a.MIN_PHRASE_CLARITY)
sys.exit()
phrases = sorted(phrases, key=lambda s: -s["clarity"])
if a.MAX_PHRASES > 0 and len(phrases) > a.MAX_PHRASES:
phrases = phrases[:a.MAX_PHRASES]
if a.MAX_SAMPLES > 0:
sampleTotal = 0
validPhrases = []
for p in phrases:
sampleTotal += p["count"]
if sampleTotal > a.MAX_SAMPLES:
break
validPhrases.append(p)
phrases = validPhrases[:]
phrases = addIndices(phrases, "rank", 1)
phrases = sorted(phrases, key=lambda s: s["start"])
print("Found %s valid phrases" % len(phrases))
if a.PROBE:
runningTotal = 0
runningDur = 0
for i, phrase in enumerate(phrases):
runningTotal += phrase["count"]
runningDur += phrase["dur"]
print("%s. %s -> %s dur[%s] count[%s] clarity[%s] power[%s] total[%s] running[%s] rank[%s]" % (i+1, formatSeconds(phrase["start"]/1000.0), formatSeconds(phrase["start"]/1000.0+phrase["dur"]/1000.0), formatSeconds(phrase["dur"]/1000.0), phrase["count"], round(phrase["clarity"], 2), round(phrase["power"], 2), runningTotal, formatSeconds(runningDur/1000.0), phrase["rank"]))
sys.exit()
headings = ["filename", "start", "dur", "count", "clarity", "power", "medianDur"]
writeCsv(a.OUTPUT_FILE, phrases, headings=headings)
print("Done.")