Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
test2.py
Go to the documentation of this file.
1 ## @package data_structs.test2
2 
3 from data_structs.base_objects import DBObject
4 from db.bll_database import DBConstants
5 
6 import pickle
7 
8 ## This class contains information for a single block that has been reviewed by the user (for the Reliability2 App).
9 # Each Check2 represents an entire testing run, and contains a list of Test2 objects that represent the individual blocks.
10 class Test2(DBObject):
11  ## Constructor
12  # @param self
13  # @param check2_id (int) the DB id of the parent check2 object for this test2
14  # @param wav_filename (string) path to the wav file from which a sound clip for this test2 will be played
15  # @param child_code (string) this is a string built from several columns in the input spreadsheet (see parsers.reliability2_parser's get_child_code() method. It looks like this: 'C002_20090831'
16  # @param spreadsheet_timestamp (string) built from spreadsheet column, in the format "dd mm yyyy <elapsed seconds as a float>" (this, together with child_code, uniquely identifies a row)
17  # @param child_vocs (int=0) the number of child vocalizations - this is grabbed from a user-adjustable spin button control in the UI
18  # @param transcription (string) the user-entered transcription text
19  # @param ui_save_data (dictionary=None) a dictionary of data with keys corresponding to the names of UI controls. This is pickled and stored here so that we can restore the UI state when a saved Check2 is loaded back up. See the Reliability2 UI classes for how this dictionary is populated.
20  # @param db_id (int) the primary key value of the row corresponding to this instance from the checks2 db table. This should be set to None if the instance is not in the database.
21  def __init__(
22  self,
23  check2_id,
24  wav_filename,
25  child_code,
26  spreadsheet_timestamp,
27  child_vocs=0,
28  transcription='',
29  ui_save_data=None,
30  db_id = None
31  ):
32 
33  super(Test2, self).__init__()
34 
35  self.check2_id = check2_id
36  self.wav_filename = wav_filename
37  self.child_code = child_code
38  self.spreadsheet_timestamp = spreadsheet_timestamp
39  self.child_vocs = child_vocs
40  self.transcription = transcription
41  self.ui_save_data = ui_save_data
42  self.db_id = db_id
43 
44  ## Retrieves a value (in seconds) indicating how far we are from the start of the entire wav file.
45  # @param self
46  # @returns (float) offset value in seconds
48  #timestamp format is "dd mm yyyy <elapsed seconds as a float>"
49  return int(self.spreadsheet_timestamp.split()[-1])
50 
51  ## See superclass description
52  def db_insert(self, db):
53  super(Test2, self).db_insert(db)
54 
55  #raise a fuss if the enclosing Check2 object is not in the DB (silently inserting child objects is reasonably sane, but this is a parent object, and silently inserting them makes things insane)
56  if self.check2_id == None:
57  raise Exception('Cannot insert Test2 object whose parent Check2 object is not in the database.')
58 
59  cols = 'check2_id wav_file child_vocs transcription ui_save_data child_code spreadsheet_timestamp'.split()
60  #we pickle the ui_save_data dictionary (i.e. store the actual object instance in the DB)
61  row = [self.check2_id, self.wav_filename, self.child_vocs, self.transcription, pickle.dumps(self.ui_save_data), self.child_code, self.spreadsheet_timestamp]
62 
63  #grab the last inserted id and use it to assign this object's db_id
64  last_ids = db.insert('tests2', cols, [row])
65  self.db_id = last_ids[0]
66 
67  ## See superclass description.
68  def db_delete(self, db):
69  super(Test2, self).db_delete(db)
70 
71  num_rows = db.delete('tests2', 'id=?', [self.db_id])
72  if num_rows == 1:
73  self.db_id = None
74 
75  return num_rows
76 
77  ## Static method to construct a list of Test2 objects from a list of database rows (from the test2 table).
78  # @param rows (list) a list of lists, one sublist per row.
79  # @returns (list) a list of Test2 instances
80  @staticmethod
82  test2_list = []
83  for cur_row in rows:
84  test2 = Test2(
85  cur_row[0],
86  cur_row[1],
87  cur_row[2],
88  cur_row[3],
89  cur_row[4],
90  cur_row[5],
91  #we unpickle the ui_save_data to get the dictionary object back
92  pickle.loads(cur_row[6]),
93  cur_row[7],
94  )
95 
96  test2_list.append(test2)
97 
98  return test2_list
99 
100  ## See superclass description.
101  @staticmethod
102  def db_select(db, ids=[]):
103  DBObject.db_select(db, ids)
104 
105  where_cond = DBObject._build_where_cond_from_ids(ids)
106 
107  rows = db.select('tests2', 'check2_id wav_file child_code spreadsheet_timestamp child_vocs transcription ui_save_data id'.split(), where_cond)
108 
109  return Test2._build_from_db_rows(rows)
110 
111  ## Static method to (query the DB and) construct a list of Test2 instances corresponding to a particular Check2 object.
112  # @param db (BLLDatabase) a database object to query
113  # @param check2_id (int) primary key value of the check2 whose test2's we're looking for
114  # @returns (list) list of Test2 instances, ordered by db_id
115  @staticmethod
116  def db_select_by_check2(db, check2_id):
117  DBObject.db_select(db, [check2_id])
118 
119  rows = db.select('tests2', 'check2_id wav_file child_code spreadsheet_timestamp child_vocs transcription ui_save_data id'.split(), ' check2_id=?', [check2_id], order_by='id')
120 
121  return Test2._build_from_db_rows(rows)