Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
testing_window.py
Go to the documentation of this file.
1 from gi.repository import Gtk as gtk, Pango
2 from utils.ui_utils import UIUtils
3 from db.bll_database import BLLDatabase
4 from data_structs.pitch_study_props import PitchStudyProps
5 from parsers.wav_parser import WavParser
6 import time
7 import os
8 
9 class TestingWindow():
10  def _update_rating(self, db, rating):
11  rows = db.select(
12  'clips',
13  ['id'],
14  where_cond = 'Batch_Num = ? and Batch_Order = ?',
15  params = [self.batch_num, self.order_num]
16  )
17  clip_id = rows[0][0]
18 
19  db.insert(
20  'ratings',
21  ['clip_id', 'Participant_Num', 'Question_Rating'],
22  [[clip_id, self.participant_num, rating]]
23  )
24 
25  def _get_num_clips(self, db):
26  row = db.select(
27  'clips',
28  ['count(id)'],
29  where_cond = 'Batch_Num = ?',
30  params = [self.batch_num]
31  )
32  return row[0][0]
33 
34  def _next(self, db, rating):
35  num_clips = self._get_num_clips(db)
36 
37  if self.order_num == num_clips:
38  self._finish()
39 
40  else:
41  self._update_rating(db, rating)
42  self.order_num += 1
43  self._update_step(db)
44 
45  if (self.order_num - 1) == num_clips or num_clips == 1:
46  self._finish()
47 
48  elif (self.order_num - 1) % self.props.break_interval == 0:
49  self.test_grid.hide()
50  self.pause_grid.show_all()
51 
52  else:
53  self._play_clip(db)
54 
55  def _finish(self):
56  self.window.destroy()
57  UIUtils.show_message_dialog('Testing complete! Thanks for your help.')
58 
59  def _update_step(self, db):
60  row = db.select(
61  'clips',
62  ['Filename', 'Age'],
63  where_cond = 'Batch_Num = ? and Batch_Order = ?',
64  params = [self.batch_num, self.order_num]
65  )
66  filename, age = row[0]
67  self.filename = '%sbatch%d/%s_%d-%d.wav' % (self.props.clips_dir_path, self.batch_num, filename[:-5], age, self.order_num)
68 
69  num_clips = self._get_num_clips(db)
70  #self.label.set_text('Clip %d of %d' % (self.order_num, self._get_num_clips(db)))
71  self.progress.set_fraction(self.order_num / float(self._get_num_clips(db)))
72 
73  def _play_clip(self, db):
74  map(lambda button: button.set_sensitive(False), self.scale_buttons)
75  while gtk.events_pending():
76  gtk.main_iteration()
77 
78  time.sleep(self.props.inter_clip_sound_del)
79 
80  self._toggle_playing_icon(True)
81 
82  wav_parser = WavParser(self.filename)
83  wav_parser.play_clip(0, wav_parser.get_sound_len())
84  wav_parser.close()
85 
86  map(lambda button: button.set_sensitive(True), self.scale_buttons)
87  self._toggle_playing_icon(False)
88 
89  def _continue(self, db):
90  self.pause_grid.hide()
91  self.test_grid.show_all()
92 
93  self._play_clip(db)
94 
95  def _get_pause_grid(self, db):
96  vbox = gtk.VBox()
97 
98  label = gtk.Label('Click the button below when you\'re ready to continue.')
99  vbox.pack_start(label, False, False, 0)
100 
101  button = gtk.Button('Continue')
102  button.connect('clicked', lambda w: self._continue(db))
103  hbox = gtk.HBox()
104  hbox.pack_start(button, True, False, 0)
105  vbox.pack_start(hbox, True, True, 10)
106 
107  return vbox
108 
109  def _toggle_playing_icon(self, is_on):
110  icon = UIUtils.BUTTON_ICONS.VOLUME_OFF
111  if is_on:
112  icon = UIUtils.BUTTON_ICONS.VOLUME_ON
113 
114  icon_path = UIUtils.get_icon_path(
115  icon,
116  UIUtils.BUTTON_ICON_SIZES.PX64
117  )
118 
119  self.playing_icon.set_from_file(icon_path)
120 
121  while gtk.events_pending():
122  gtk.main_iteration()
123 
124  def _get_test_grid(self, db):
125  grid = gtk.Grid()
126 
127  self.progress = gtk.ProgressBar()
128  self.progress.set_orientation(gtk.Orientation.HORIZONTAL)
129  self.progress.set_fraction(self.order_num + 1 / float(self._get_num_clips(db)))
130  #self.progress.set_vexpand(False)
131  #self.progress.set_vexpand_set(True)
132  grid.attach(self.progress, 0, 0, 5, 1)
133 
134  #self.label = gtk.Label('Clip %d of %d' % (self.order_num + 1, self._get_num_clips(db)))
135  #UIUtils.set_font_size(self.label, 25, bold=True)
136  #self.label.set_hexpand(True)
137  #self.label.set_hexpand_set(True)
138  #grid.attach(self.label, 0, 0, 5, 1)
139 
140  question_label = gtk.Label('How much does this sentence sound like a question?')
141  UIUtils.set_font_size(question_label, 30, bold=True)
142  grid.attach(question_label, 0, 1, 5, 1)
143 
144  self.playing_icon = gtk.Image()
145  self.playing_icon.set_vexpand(True)
146  self.playing_icon.set_vexpand_set(True)
147  self._toggle_playing_icon(False)
148  grid.attach(self.playing_icon, 0, 2, 5, 1)
149 
150  self.scale_buttons = []
151  button_grid = gtk.Grid()
152  button_grid.set_column_homogeneous(True)
153 
154  label_low = gtk.Label('Not Very Much like a Question')
155  label_low.set_alignment(0, 0)
156  UIUtils.set_font_size(label_low, 20, bold=True)
157  button_grid.attach(label_low, 0, 0, 2, 1)
158 
159  label_high = gtk.Label('Very Much like a Question')
160  label_high.set_alignment(1, 0)
161  UIUtils.set_font_size(label_high, 20, bold=True)
162  button_grid.attach(label_high, self.props.num_options - 2, 0, 2, 1)
163 
164  for i in range(self.props.num_options):
165  button = gtk.Button('\n' + str(i + 1) + '\n')
166  UIUtils.set_font_size(button, 15, bold=True)
167  button.connect('button-release-event', lambda w, e: self._next(db, int(w.get_label())))
168  button.set_vexpand(False)
169  button.set_vexpand_set(False)
170 
171  self.scale_buttons.append(button)
172  button.set_hexpand(True)
173  button.set_hexpand_set(True)
174  button_grid.attach(button, i, 1, 1, 1)
175 
176  grid.attach(button_grid, 0, 3, 5, 1)
177 
178  return grid
179 
180  def __init__(self, db, batch_num, participant_num):
182  self.props = PitchStudyProps.db_select(self.bll_db)[0]
183  self.bll_db.close()
184 
185  self.batch_num = batch_num
186  self.participant_num = participant_num
187  self.order_num = 1
188  self.filename = None
189 
190  self.window = gtk.Window(gtk.WindowType.TOPLEVEL)
191  self.window.set_title('Batch %d' % (batch_num))
192  #self.window.set_resizable(False)
193  #self.window.connect('delete-event', lambda x, y: True)
194  self.window.connect('destroy', lambda w: self.window.destroy())
195  self.window.set_border_width(10)
196  self.window.set_size_request(800, 600)
197 
198  vbox = gtk.VBox()
199  self.test_grid = self._get_test_grid(db)
200  self._update_step(db)
201  vbox.pack_start(self.test_grid, True, True, 0)
202  self.test_grid.hide()
203 
204  self.pause_grid = self._get_pause_grid(db)
205  vbox.pack_start(self.pause_grid, True, False, 0)
206  self.pause_grid.show_all()
207 
208  self.window.add(vbox)
209  vbox.show()
210  self.window.show()
211