Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
batch_sel_win.py
Go to the documentation of this file.
1 from gi.repository import Gtk as gtk
2 from utils.ui_utils import UIUtils
3 from db.database import Database
4 from db.bll_database import BLLDatabase
5 from data_structs.pitch_study_props import PitchStudyProps
6 from ui.pitch_study_app.testing_window import TestingWindow
7 
9  def __init__(self, db_path):
11  self.props = PitchStudyProps.db_select(self.bll_db)[0]
12  self.bll_db.close()
13 
14  self.window = gtk.Window(gtk.WindowType.TOPLEVEL)
15  self.window.set_title('Select Batch Number')
16  self.window.connect('destroy', lambda w: self.window.destroy())
17  self.window.set_border_width(10)
18  self.window.set_default_size(210, 100)
19 
20  db = Database(db_path)
21 
22  vbox = gtk.VBox()
23 
24  next_batch_num, next_part_num = self._get_next_nums(db)
25 
26  label = gtk.Label('Select the batch number to run: ')
27  batch_spin_adj = gtk.Adjustment(value=1, lower=1, upper=self._get_total_batches(db) + 1, step_increment=1, page_increment=1, page_size=1)
28  self.batch_spin = gtk.SpinButton()
29  self.batch_spin.set_adjustment(batch_spin_adj)
30  self.batch_spin.set_snap_to_ticks(True)
31  self.batch_spin.set_value(next_batch_num)
32  hbox = gtk.HBox()
33  hbox.pack_start(label, True, True, 0)
34  hbox.pack_start(self.batch_spin, True, True, 0)
35  vbox.pack_start(hbox, True, True, 0)
36 
37  label = gtk.Label('Select the participant number to use: ')
38  part_spin_adj = gtk.Adjustment(value=1, lower=1, upper=self.props.max_parts_per_batch + 1, step_increment=1, page_increment=1, page_size=1)
39  self.part_spin = gtk.SpinButton()
40  self.part_spin.set_adjustment(part_spin_adj)
41  self.part_spin.set_snap_to_ticks(True)
42  self.part_spin.set_value(next_part_num)
43  hbox = gtk.HBox()
44  hbox.pack_start(label, True, True, 0)
45  hbox.pack_start(self.part_spin, True, True, 0)
46  vbox.pack_start(hbox, True, True, 0)
47 
48  button_box = self._build_button_box(db)
49  vbox.pack_start(button_box, True, True, 0)
50 
51  self.window.add(vbox)
52  self.window.show_all()
53 
54  def _get_next_nums(self, db):
55  batch_num = 1
56  part_num = 1
57 
58  total_batches = self._get_total_batches(db)
59 
60  rows = db.select(
61  'clips c join ratings r on c.id = r.clip_id',
62  ['max(c.Batch_Num)'],
63  where_cond = 'r.Participant_Num is not null'
64  )
65  if rows and rows[0][0] is not None:
66  cur_batch_num = rows[0][0]
67  rows = db.select(
68  'clips c join ratings r on c.id = r.clip_id',
69  ['max(r.Participant_Num)'],
70  where_cond = 'c.Batch_Num = ?',
71  params = [cur_batch_num]
72  )
73  if rows and rows[0][0] is not None:
74  cur_part_num = rows[0][0]
75 
76  if cur_part_num < self.props.max_parts_per_batch:
77  part_num = cur_part_num + 1
78  batch_num = cur_batch_num
79  elif cur_batch_num < total_batches:
80  batch_num = cur_batch_num + 1
81  #part_num = 1
82 
83  return batch_num, part_num
84 
85  def _get_total_batches(self, db):
86  rows = db.select(
87  'clips',
88  ['max(Batch_Num)']
89  )
90  return rows[0][0]
91 
92  def _check_if_used(self, db, batch_num, participant_num):
93  rows = db.select(
94  'clips c join ratings r on c.id = r.clip_id',
95  ['count(c.id)'],
96  where_cond = 'c.Batch_Num = ? and r.Participant_Num = ?',
97  params = [batch_num, participant_num]
98  )
99  return rows[0][0] > 0
100 
101  def _build_button_box(self, db):
102  button_box = gtk.HButtonBox()
103  button_box.set_layout(gtk.ButtonBoxStyle.EDGE)
104 
105  cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL, label='Cancel')
106  cancel_button.connect('clicked', lambda w: self.window.destroy())
107  button_box.add(cancel_button)
108 
109  ok_button = gtk.Button(stock=gtk.STOCK_OK, label='Run')
110  ok_button.connect('clicked', lambda w: self.check_input(db))
111  button_box.add(ok_button)
112 
113  return button_box
114 
115  def check_input(self, db):
116  batch_num = self.batch_spin.get_value_as_int()
117  participant_num = self.part_spin.get_value_as_int()
118  if self._check_if_used(db, batch_num, participant_num):
119  response = UIUtils.show_confirm_dialog('Participant %d has recorded responses for Batch %d.\nContinuing will overwrite the existing data for this participant (in this batch).\nDo you want continue?' % (participant_num, batch_num))
120  if response:
121  db.delete(
122  'ratings',
123  where_cond = 'clip_id in (select id from clips where Batch_Num = ?) and Participant_Num = ?',
124  params = [batch_num, participant_num]
125  )
126  self._start_testing(db, batch_num, participant_num)
127 
128  else:
129  self._start_testing(db, batch_num, participant_num)
130 
131  #db.close()
132 
133  def _start_testing(self, db, batch_num, participant_num):
134  self.window.destroy()
135  TestingWindow(db, batch_num, participant_num)