Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
create_window.py
Go to the documentation of this file.
1 from gi.repository import Gtk as gtk
2 import os
3 
4 from data_structs.check import Check
5 from data_structs.test import Test
6 from data_structs.seg_filters import *
7 from db.bll_database import BLLDatabase, DBConstants
8 from parsers.trs_parser import TRSParser
9 from parsers.csv_parser import CSVParser
10 from parsers.parser_tools import ParserTools
11 from utils.ui_utils import UIUtils
12 from utils.progress_dialog import ProgressDialog
13 from ui.reliability_app.test_window import TestWindow
14 from utils.filters_frame import FiltersFrame
15 from utils.form import Form
16 
17 class CreateWindow():
18  def __init__(self):
19  self.form = Form()
20  self.window = gtk.Window(gtk.WindowType.TOPLEVEL)
21  self.window.set_title('Create Check')
22  self.window.connect('destroy', lambda w: self.window.destroy())
23  self.window.set_border_width(10)
24 
25  vbox = gtk.VBox()
26  settings_frame = self._build_settings_frame()
27  vbox.pack_start(settings_frame, True, True, 0)
28 
30  vbox.pack_start(self.filters_frame, True, True, 0)
31 
32  buttons_box = self._build_buttons_box()
33  vbox.pack_end(buttons_box, True, True, 0)
34 
35  self.window.add(vbox)
36  #name_entry.grab_focus()
37  self.window.show_all()
38 
39  def _build_buttons_box(self):
40  vbox = gtk.VBox()
41 
42  button_box = gtk.HButtonBox()
43  button_box.set_layout(gtk.ButtonBoxStyle.EDGE)
44 
45  cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL, label='Cancel')
46  cancel_button.connect('clicked', lambda w: self.window.destroy())
47  button_box.add(cancel_button)
48 
49  create_button = gtk.Button(stock=gtk.STOCK_OK, label='Create')
50  create_button.connect('clicked', lambda w: self.create_check())
51  button_box.add(create_button)
52 
53  vbox.pack_start(gtk.HSeparator(), True, True, 0)
54  vbox.pack_start(button_box, True, True, 0)
55 
56  return vbox
57 
59  settings_frame = gtk.Frame(label='Settings')
60 
61  #table = gtk.Table(7, 3, False)
62  grid = gtk.Grid()
63 
64  name_label = gtk.Label('Name:')
65  name_label.set_justify(gtk.JUSTIFY_RIGHT)
66  #table.attach(name_label, 0, 1, 0, 1, gtk.EXPAND, gtk.EXPAND)
67  grid.attach(name_label, 0, 0, 1, 1)
68 
69  self.form.name_entry = gtk.Entry()
70  self.form.name_entry.set_width_chars(50)
71  #table.attach(self.form.name_entry, 1, 2, 0, 1)
72  grid.attach(self.form.name_entry, 1, 0, 1, 1)
73 
74  input_file_label = gtk.Label('TRS / CSV File:')
75  input_file_label.set_justify(gtk.JUSTIFY_RIGHT)
76  #table.attach(input_file_label, 0, 1, 1, 2, gtk.EXPAND, gtk.EXPAND)
77  grid.attach(input_file_label, 0, 1, 1, 1)
78 
79  self.form.input_file_entry = gtk.Entry()
80  self.form.input_file_entry.set_width_chars(50)
81  self.form.wav_file_entry = gtk.Entry()
82  self.form.wav_file_entry.set_width_chars(50)
83  #table.attach(self.form.input_file_entry, 1, 2, 1, 2, gtk.EXPAND, gtk.EXPAND)
84  grid.attach(self.form.input_file_entry, 1, 1, 1, 1)
85 
86  input_file_button = gtk.Button('Browse')
87  input_file_button.connect('clicked', lambda w: UIUtils.browse_file('Select trs/csv file', self.form.input_file_entry, [UIUtils.TRS_CSV_FILE_FILTER, UIUtils.ALL_FILE_FILTER], self.form.wav_file_entry))
88  #table.attach(input_file_button, 2, 3, 1, 2, gtk.EXPAND, gtk.EXPAND)
89  grid.attach(input_file_button, 2, 1, 1, 1)
90 
91  wav_file_label = gtk.Label('WAV File:')
92  wav_file_label.set_justify(gtk.JUSTIFY_RIGHT)
93  #table.attach(wav_file_label, 0, 1, 2, 3, gtk.EXPAND, gtk.EXPAND)
94  grid.attach(wav_file_label, 0, 2, 1, 1)
95 
96  #table.attach(self.form.wav_file_entry, 1, 2, 2, 3, gtk.EXPAND, gtk.EXPAND)
97  grid.attach(self.form.wav_file_entry, 1, 2, 1, 1)
98 
99  wav_file_button = gtk.Button('Browse')
100  wav_file_button.connect('clicked', lambda w: UIUtils.browse_file('Select wav file', self.form.wav_file_entry, [UIUtils.WAV_FILE_FILTER, UIUtils.ALL_FILE_FILTER]))
101  #table.attach(wav_file_button, 2, 3, 2, 3, gtk.EXPAND, gtk.EXPAND)
102  grid.attach(wav_file_button, 2, 2, 1, 1)
103 
104  num_segs_label = gtk.Label('Number of Segments:')
105  num_segs_label.set_justify(gtk.JUSTIFY_RIGHT)
106  #table.attach(num_segs_label, 0, 1, 4, 5, gtk.EXPAND, gtk.EXPAND)
107  grid.attach(num_segs_label, 0, 4, 1, 1)
108 
109  num_segs_adj = gtk.Adjustment(value=0, lower=1, upper=2**32, step_incr=1, page_incr=5)
110  self.form.num_segs_spinner = gtk.SpinButton(num_segs_adj)
111  #table.attach(self.form.num_segs_spinner, 1, 2, 4, 5, gtk.EXPAND, gtk.EXPAND)
112  grid.attach(self.form.num_segs_spinner, 1, 4, 1, 1)
113 
114  context_pad_label = gtk.Label('Default context padding (sec):')
115  context_pad_label.set_justify(gtk.JUSTIFY_RIGHT)
116  #table.attach(context_pad_label, 0, 1, 5, 6, gtk.EXPAND, gtk.EXPAND)
117  grid.attach(context_pad_label, 0, 5, 1, 1)
118 
119  context_pad_adj = gtk.Adjustment(value=0, lower=1, upper=60, step_incr=1, page_incr=5)
120  self.form.context_pad_spinner = gtk.SpinButton(context_pad_adj)
121  #table.attach(self.form.context_pad_spinner, 1, 2, 5, 6, gtk.EXPAND, gtk.EXPAND)
122  grid.attach(self.form.context_pad_spinner, 1, 5, 1, 1)
123 
124  self.form.rand_checkbox = gtk.CheckButton('Pick segs randomly')
125  #table.attach(self.form.rand_checkbox, 0, 1, 6, 7)
126  grid.attach(self.form.rand_checkbox, 0, 6, 1, 1)
127 
128  settings_frame.add(grid)
129 
130  return settings_frame
131 
132  def validate_form(self):
133  error_msgs = []
134 
135  if (self.form.name_entry.get_text() == '' or
136  self.form.input_file_entry.get_text() == '' or
137  self.form.wav_file_entry.get_text() == ''):
138  error_msgs.append(' -Please ensure that all fields are filled.')
139 
140  if self.form.input_file_entry.get_text() != '' and not os.path.exists(self.form.input_file_entry.get_text()):
141  error_msgs.append(' -Unable to locate TRS/CSV file. Please double-check the file path.')
142 
143  if self.form.wav_file_entry.get_text() != '' and not os.path.exists(self.form.wav_file_entry.get_text()):
144  error_msgs.append(' -Unable to locate WAV file. Please double-check the file path.')
145 
146  composite_msg = None
147  if error_msgs:
148  composite_msg = 'The following issues were detected:\n%s' % ('\n'.join(error_msgs))
149 
150  return composite_msg
151 
152  def create_check(self):
153  error_msg = self.validate_form()
154 
155  if error_msg:
156  UIUtils.show_message_dialog(error_msg)
157 
158  else:
159  filters = self.filters_frame.get_filters()
160 
161  check = Check(self.form.name_entry.get_text(),
162  self.form.input_file_entry.get_text(),
163  self.form.wav_file_entry.get_text(),
164  self.form.num_segs_spinner.get_value_as_int(),
165  self.form.context_pad_spinner.get_value_as_int(),
166  [],
167  0,
168  filters=filters,
169  pick_randomly=self.form.rand_checkbox.get_active(),
170  )
171 
172  parser = None
173  progress_dialog = ProgressDialog(title='Loading File',
174  phases=['Parsing file...', 'Setting up...'])
175  segs = []
176 
177  #TRS files
178  if check.input_filename.lower().endswith('.trs'):
179  parser = TRSParser(check.input_filename)
180  progress_dialog.show()
181  segs = parser.parse(progress_update_fcn=progress_dialog.set_fraction,
182  progress_next_phase_fcn=progress_dialog.next_phase,
183  validate=False,
184  seg_filters=check.filters)
185 
186  #CSV files
187  else:
188  parser = CSVParser(check.input_filename)
189  progress_dialog.show()
190  segs = parser.parse(progress_update_fcn=progress_dialog.set_fraction,
191  seg_filters=check.filters)
192 
193  progress_dialog.next_phase()
194 
195  if check.pick_randomly:
196  #segs = ParserTools.pick_rand_segs(check.num_segs, segs)
197  segs = ParserTools.hacked_pick_rand_segs(check.num_segs, segs, os.path.basename(check.input_filename))
198  else:
199  segs = ParserTools.pick_contiguous_segs(check.num_segs, segs)
200  progress_dialog.set_fraction(1.0)
201 
202  if len(segs) < check.num_segs:
203  progress_dialog.ensure_finish() #close the progress bar (even though there's still one phase left)
204  UIUtils.show_message_dialog('The input file does not contain enough segments of the specified types.', dialog_type=gtk.MessageType.ERROR)
205 
206  else:
207  db = BLLDatabase()
208  check.db_insert(db)
209 
210  for i in range(len(segs)):
211  if segs[i].db_id == None:
212  segs[i].db_insert(db)
213 
214  test = Test(
215  check.db_id,
216  None,
217  None,
218  None,
219  segs[i],
220  None,
221  check.default_context_padding,
222  )
223  test.db_insert(db)
224  check.tests.append(test)
225 
226  progress_dialog.set_fraction( float(i + 1) / float(check.num_segs) )
227 
228  db.close()
229  progress_dialog.ensure_finish()
230 
231  self.window.destroy()
232  TestWindow(check)
233