Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
test.py
Go to the documentation of this file.
1 ## @package data_structs.test
2 
3 from data_structs.base_objects import DBObject
4 from data_structs.segment import Segment
5 
6 ## This class represents an instance of a single step in a Check (Reliability Application).
7 # When a check is run, a Check object is created, and a number of Tests are performed (the exact number is specified by the user.). Each test consists of playing a sound clip (either with or without context), and recording the user's response (the user attempts to identify the type of segment being played).
8 class Test(DBObject):
9  ## Constructor
10  # @param self
11  # @param category_input (int) this is the user's response that they've selected from the dropdown. It corresponds to one of the options from DBConstants.COMBO_OPTIONS[DBConstants.COMBO_GROUPS.RELIABILITY_CATEGORIES]
12  # @param segment (Segment) the segment being played
13  # @param check (Check) pointer to the instance of the check this test is being performed for
14  # @param with_context (boolean) True if sound clip is being padded on both sides, False otherwise
15  # @param db_id (int) primary key from the tests DB table. Should be None if this test is not yet in the DB.
16  # @param adjusted_padding(int) The user can adjust this value on the fly for each test that is presented with context. If you pass None, this will be inferred from the with_context and/or check (context_padding attribute) parameters. Regardless of what you pass here, if with_context==False, then the DB will contain a zero for this attribute.
17  # @param syllables(int=0) number of syllables in sound clip (entered by user)
18  # @param adjusted_start (float) the user-specified absolute start time (in seconds)
19  # @param adjusted_end (float) the user-specified absolute end time (in seconds)
20  def __init__(self, check_id, category_input, syllables_w_context, syllables_wo_context, segment, is_uncertain, context_padding, db_id=None):
21  self.check_id = check_id
22  self.category_input = category_input
23  self.syllables_w_context = syllables_w_context
24  self.syllables_wo_context = syllables_wo_context
25  self.seg = segment
26  self.is_uncertain = is_uncertain
27  self.context_padding = context_padding
28  self.db_id = db_id
29 
30  ## See superclass description. This Test's corresponding Check object must already have been inserted into the database before this method is called. If not, an excpetion will be raised.
31  def db_insert(self, db):
32  super(Test, self).db_insert(db)
33 
34  #make sure the Check is already in the DB. If it's not, this object's check_id will be None. If we stored the Test anyway, we'd have a NULL value for the check_id column, and we would
35  #have no way of matching this Test up with its Check when it is selected from the DB.
36  if self.check_id == None:
37  raise Exception('Attempting to insert Test object whose corresponding Check object is not yet in the database. Insertion aborted.')
38 
39  #insert the Segment, if necessary
40  if self.seg.db_id == None:
41  self.seg.db_insert(db)
42 
43  #insert this test
44  self.db_id = db.insert('tests',
45  'check_id category_input syllables_w_context syllables_wo_context segment_id is_uncertain context_padding'.split(),
46  [[self.check_id, self.category_input, self.syllables_w_context, self.syllables_wo_context, self.seg.db_id, self.is_uncertain, self.context_padding]])[0]
47 
48  ## See superclass description.
49  def db_delete(self, db):
50  super(Test, self).db_delete(db)
51 
52  if self.db_id != None:
53  db.delete('tests', 'id=?', [self.db_id])
54  self.db_id = None
55 
56  self.seg.db_delete(db)
57 
58  def db_update_user_inputs(self, db):
59  if self.db_id != None and self.seg.db_id != None:
60  db.update('tests', 'category_input syllables_w_context syllables_wo_context is_uncertain context_padding'.split(), ' id=?', [self.category_input, self.syllables_w_context, self.syllables_wo_context, self.is_uncertain, self.context_padding, self.db_id])
61 
62  db.update('segments', 'user_adj_start user_adj_end'.split(), ' id=?', [self.seg.user_adj_start, self.seg.user_adj_end, self.seg.db_id])
63 
64  @staticmethod
65  def _build_from_db_rows(db, rows):
66  #select the corresponding Segments, create the Test objects
67  test_list = []
68  for cur_row in rows:
69  seg = Segment.db_select(db, [cur_row[5]])[0]
70 
71  test = Test(
72  cur_row[1],
73  cur_row[2],
74  cur_row[3],
75  cur_row[4],
76  seg,
77  cur_row[6],
78  cur_row[7],
79  cur_row[0],
80  )
81  test_list.append(test)
82 
83  return test_list
84 
85  ## See superclass description.
86  @staticmethod
87  def db_select(db, ids=[]):
88  DBObject.db_select(ids)
89 
90  #select the test data
91  rows = db.select('tests',
92  'id check_id category_input syllables_w_context syllables_wo_context segment_id is_uncertain context_padding'.split(),
93  DBObject._build_where_cond_from_ids(ids)
94  )
95 
96  return Test._build_from_db_rows(db, rows)
97 
98  @staticmethod
99  def db_select_by_check(db, check_id):
100  rows = db.select('tests', 'id check_id category_input syllables_w_context syllables_wo_context segment_id is_uncertain context_padding'.split(), ' check_id=?', [check_id], order_by='id')
101 
102  return Test._build_from_db_rows(db, rows)