Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
stats_exporter.py
Go to the documentation of this file.
1 ## @package parsers.stats_exporter
2 
3 from utils.ui_utils import UIUtils
4 from parsers.trs_parser import TRSParser
5 from parsers.filter_manager import FilterManager
6 
7 import csv
8 
9 ## This class writes statistics from the stats app to a csv file.
10 class StatsExporter(object):
11  ## Constructor
12  # @param self
13  # @param config (Configuration) The output configuration to write to the csv file.
14  def __init__(self, config, trs_filename, export_filename):
15  self.config = config
16  self.trs_filename = trs_filename
17  self.export_filename = export_filename
18 
19  if not self.export_filename.lower().endswith('.csv'):
20  self.export_filename += '.csv'
21 
22  ## Performs the write to the CSV file.
23  # @param self
24  # @param progress_update_fcn (function=None) function accepting a value in [0,1] to display as a progress bar - see utils.ProgressDialog. This value is used to indicate the level of completeness <em>of the current phase</em>
25  # @param progress_next_phase_fcn(function=None) - moves the progress bar to the next phase, which causes new text to be displayed in the bar - see utils.ProgressDialog
26  def export(self, progress_update_fcn=None, progress_next_phase_fcn=None):
27  #create csv file
28  export_file = open(self.export_filename, 'wb')
29 
30  #write header info
31  csv_writer = csv.writer(export_file, quoting=csv.QUOTE_ALL) #use Python csv library
32  csv_writer.writerow(['Export Date: %s' % (UIUtils.get_cur_timestamp_str())])
33  csv_writer.writerow(['Configuration Creation Date: %s' % (self.config.created)])
34  csv_writer.writerow(['TRS Filename: %s' % (self.trs_filename)])
35  csv_writer.writerow(['Output Configuration:'])
36  csv_writer.writerow(['Name: %s' % (self.config.name)])
37  csv_writer.writerow(['Description: %s' % (self.config.desc)])
38  csv_writer.writerow([''])
39  csv_writer.writerow(['Outputs:'])
40  csv_writer.writerow([''])
41 
42  #parse the trs file
43  trs_parser = TRSParser(self.trs_filename)
44  segs = trs_parser.parse(progress_update_fcn, progress_next_phase_fcn, validate=False)
45  chains = None #this is populated on demand, then cached
46 
47  #iterate through all outputs in the configuration, adding segments/chains to each one, then writing the output to the spreadsheet file
48  i = 0
49  while i < len(self.config.outputs):
50  #update progress bar text
51  if progress_next_phase_fcn:
52  progress_next_phase_fcn()
53 
54  cur_output = self.config.outputs[i]
55  cur_output.reset() #clear any cached utterances from previous runs
56 
57  #if we need chains, parse them from the segment list
58  if cur_output.chained and not chains:
59  chains = FilterManager.get_chains(segs)
60 
61  #add chains/segments to the current output
62  items = chains if cur_output.chained else segs
63  j = 0
64  while j < len(items):
65  cur_output.add_item(items[j], filter_utters=True) #note: filter_utters only affects segs (not chains)
66  j += 1
67 
68  #note: updating progress individually within the above loop (for every iteration of j) slows down the processing considerably (by a factor of ~4) - a compromise is to just set each phase to 100% after it completes.
69  if progress_update_fcn:
70  progress_update_fcn(1)
71 
72  #grab the output's results and write them to the file
73  cur_output.write_csv_rows(csv_writer)
74  csv_writer.writerow([''])
75 
76  i += 1
77 
78  export_file.close()