Skip to content

Commit

Permalink
Print influxdb queries that encounter an error
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcauliffe committed Jul 25, 2019
1 parent a5ed635 commit f93181a
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions polyglotdb/corpus/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from decimal import Decimal

from influxdb import InfluxDBClient
from influxdb.exceptions import InfluxDBClientError

from ..acoustics import analyze_pitch, analyze_formant_tracks, analyze_intensity, \
analyze_script, analyze_track_script, analyze_utterance_pitch, update_utterance_pitch_track, analyze_vot
Expand Down Expand Up @@ -661,6 +662,29 @@ def has_sound_files(self):
break
return self._has_sound_files

def execute_influxdb(self, query):
"""
Execute an InfluxDB query for the corpus
Parameters
----------
query : str
Query to run
Returns
-------
:class:`influxdb.resultset.ResultSet`
Results of the query
"""
client = self.acoustic_client()
try:
result = client.query(query)
except InfluxDBClientError:
print('There was an issue with the following query:')
print(query)
raise
return result

def get_utterance_acoustics(self, acoustic_name, utterance_id, discourse, speaker):
"""
Get acoustic for a given utterance and time range
Expand All @@ -681,15 +705,14 @@ def get_utterance_acoustics(self, acoustic_name, utterance_id, discourse, speake
:class:`polyglotdb.acoustics.classes.Track`
Track object
"""
client = self.acoustic_client()
properties = [x[0] for x in self.hierarchy.acoustic_properties[acoustic_name]]
property_names = ["{}".format(x) for x in properties]
columns = '"time", {}'.format(', '.join(property_names))
query = '''select {} from "{}"
WHERE "utterance_id" = '{}'
AND "discourse" = '{}'
AND "speaker" = '{}';'''.format(columns, acoustic_name, utterance_id, discourse, speaker)
result = client.query(query)
result = self.execute_influxdb(query)
track = Track()
for r in result.get_points(acoustic_name):
s = to_seconds(r['time'])
Expand Down Expand Up @@ -729,7 +752,6 @@ def get_acoustic_measure(self, acoustic_name, discourse, begin, end, channel=0,
end = Decimal(end).quantize(Decimal('0.001'))
num_points = kwargs.pop('num_points', 0)
filter_string = generate_filter_string(discourse, begin, end, channel, num_points, kwargs)
client = self.acoustic_client()

properties = [x[0] for x in self.hierarchy.acoustic_properties[acoustic_name]]
property_names = ["{}".format(x) for x in properties]
Expand All @@ -739,7 +761,7 @@ def get_acoustic_measure(self, acoustic_name, discourse, begin, end, channel=0,
columns = '"time", {}'.format(', '.join(property_names))
query = '''select {} from "{}"
{};'''.format(columns, acoustic_name, filter_string)
result = client.query(query)
result = self.execute_influxdb(query)
track = Track()
for r in result.get_points(acoustic_name):
s = to_seconds(r['time'])
Expand Down Expand Up @@ -927,9 +949,8 @@ def discourse_has_acoustics(self, acoustic_name, discourse):
"""
if acoustic_name not in self.hierarchy.acoustics:
return False
client = self.acoustic_client()
query = '''select * from "{}" WHERE "discourse" = '{}' LIMIT 1;'''.format(acoustic_name, discourse)
result = client.query(query)
result = self.execute_influxdb(query)
if len(result) == 0:
return False
return True
Expand Down Expand Up @@ -961,7 +982,6 @@ def encode_acoustic_statistic(self, acoustic_name, statistic, by_phone=True, by_
if statistic not in available_statistics:
raise ValueError('Statistic name should be one of: {}.'.format(', '.join(available_statistics)))

client = self.acoustic_client()
acoustic_name = acoustic_name.lower()
template = statistic + '("{0}") as "{0}"'
statistic_template = 'n.{statistic}_{measure} = d.{measure}'
Expand All @@ -974,7 +994,7 @@ def encode_acoustic_statistic(self, acoustic_name, statistic, by_phone=True, by_
where "phone" = '{}' group by "speaker";'''.format(
', '.join(measures), acoustic_name, p)

influx_result = client.query(query)
influx_result = self.execute_influxdb(query)
for k, v in influx_result.items():
result = {'speaker': k[1]['speaker'], 'phone': p}
for measure in measures.keys():
Expand All @@ -1000,7 +1020,7 @@ def encode_acoustic_statistic(self, acoustic_name, statistic, by_phone=True, by_
where "phone" = '{}';'''.format(', '.join(measures.values()),
acoustic_name, p)

influx_result = client.query(query)
influx_result = self.execute_influxdb(query)
result = {'phone': p}
for k, v in influx_result.items():
for measure in measures.keys():
Expand All @@ -1019,7 +1039,7 @@ def encode_acoustic_statistic(self, acoustic_name, statistic, by_phone=True, by_
[('{}_{}'.format(statistic, x), float) for x in measures.keys()])
elif by_speaker:
query = '''select {} from "{}" group by "speaker";'''.format(', '.join(measures), acoustic_name)
influx_result = client.query(query)
influx_result = self.execute_influxdb(query)
results = []

for k, v in influx_result.items():
Expand Down

0 comments on commit f93181a

Please sign in to comment.