Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
pitch_study_exporter.py
Go to the documentation of this file.
1 from db.database import Database
2 import os
3 import shutil
4 import csv
5 
6 def _get_header():
7  return [
8  'Batch Number',
9  'Participant Number',
10  'Order Number',
11  'Question Rating',
12  'Filename',
13  'Age',
14  'Condition',
15  'Speaker',
16  'Sentence Type',
17  'Question Type',
18  'Start Time',
19  'Stop Time',
20  'Duration',
21  'Lag Time',
22  'Mother Max Pitch',
23  'Mother Min Pitch',
24  'Mother Mean Pitch',
25  'Mother Pitch Delta',
26  'Mother Pitch Category',
27  'Baby Max Pitch',
28  'Baby Min Pitch',
29  'Baby Mean Pitch',
30  'Baby Pitch Delta',
31  'Baby Pitch Category',
32  ]
33 
35  return [
36  'Batch Number',
37  'Order Number',
38  'Filename',
39  'Age',
40  'Min Question Rating',
41  'Max Question Rating',
42  'Mean Question Rating',
43  'Filename',
44  'Age',
45  'Condition',
46  'Speaker',
47  'Sentence Type',
48  'Question Type',
49  'Start Time',
50  'Stop Time',
51  'Duration',
52  'Lag Time',
53  'Mother Max Pitch',
54  'Mother Min Pitch',
55  'Mother Mean Pitch',
56  'Mother Pitch Delta',
57  'Mother Pitch Category',
58  'Baby Max Pitch',
59  'Baby Min Pitch',
60  'Baby Mean Pitch',
61  'Baby Pitch Delta',
62  'Baby Pitch Category',
63  ]
64 
65 
66 def _get_part_data(db, batch_num, part_num):
67  rows = db.select(
68  'clips c join ratings r on c.id = r.clip_id',
69  [
70  'c.Batch_Num',
71  'r.Participant_Num',
72  'c.Batch_Order',
73  'r.Question_Rating',
74  'c.Filename',
75  'c.Age',
76  'c.Condition',
77  'c.Speaker',
78  'c.Sentence_Type',
79  'c.Question_Type',
80  'c.Start_Time',
81  'c.Stop_Time',
82  'c.Duration',
83  'c.Lag_Time',
84  'c.Mother_Max_Pitch',
85  'c.Mother_Min_Pitch',
86  'c.Mother_Mean_Pitch',
87  'c.Mother_Pitch_Delta',
88  'c.Mother_Pitch_Category',
89  'c.Baby_Max_Pitch',
90  'c.Baby_Min_Pitch',
91  'c.Baby_Mean_Pitch',
92  'c.Baby_Pitch_Delta',
93  'c.Baby_Pitch_Category',
94  ],
95  where_cond = 'c.Batch_Num = ? and r.Participant_Num = ?',
96  order_by = 'c.Batch_Order',
97  params = [batch_num, part_num]
98  )
99 
100  return rows
101 
102 def _get_agregate_part_data(db, batch_num):
103  rows = db.select(
104  'clips c join ratings r on c.id = r.clip_id',
105  [
106  'c.Batch_Num',
107  'c.Batch_Order',
108  'c.Filename',
109  'c.Age',
110  'min(r.Question_Rating)',
111  'max(r.Question_Rating)',
112  'avg(r.Question_Rating)',
113  'c.Filename',
114  'c.Age',
115  'c.Condition',
116  'c.Speaker',
117  'c.Sentence_Type',
118  'c.Question_Type',
119  'c.Start_Time',
120  'c.Stop_Time',
121  'c.Duration',
122  'c.Lag_Time',
123  'c.Mother_Max_Pitch',
124  'c.Mother_Min_Pitch',
125  'c.Mother_Mean_Pitch',
126  'c.Mother_Pitch_Delta',
127  'c.Mother_Pitch_Category',
128  'c.Baby_Max_Pitch',
129  'c.Baby_Min_Pitch',
130  'c.Baby_Mean_Pitch',
131  'c.Baby_Pitch_Delta',
132  'c.Baby_Pitch_Category',
133  ],
134  where_cond = 'c.Batch_Num = ?',
135  group_by = 'c.Batch_Order',
136  params = [batch_num]
137  )
138 
139  return rows
140 
141 def _export_agregate_part(db, out_dir, batch_num):
142  out_filename = '%sStats.csv' % (out_dir)
143  out_file = open(out_filename, 'wb')
144  writer = csv.writer(out_file)
145 
146  writer.writerow(_get_agregate_header())
147  rows = _get_agregate_part_data(db, batch_num)
148 
149  for cur_row in rows:
150  writer.writerow(cur_row)
151 
152  out_file.close()
153 
154 def _export_part(db, out_dir, batch_num, part_num):
155  out_filename = '%sParticipant%d.csv' % (out_dir, part_num)
156  out_file = open(out_filename, 'wb')
157  writer = csv.writer(out_file)
158 
159  writer.writerow(_get_header())
160  rows = _get_part_data(db, batch_num, part_num)
161 
162  for cur_row in rows:
163  writer.writerow(cur_row)
164 
165  out_file.close()
166 
167 def _export_batch(db, batch_num, save_dir):
168  rows = db.select(
169  'clips c join ratings r on c.id = r.clip_id',
170  ['distinct r.Participant_Num'],
171  where_cond = 'c.Batch_Num = ?',
172  params = [batch_num]
173  )
174 
175  out_dir = '%sbatch%d/' % (save_dir, batch_num)
176  #make way! ...
177  if os.path.exists(out_dir):
178  shutil.rmtree(out_dir)
179  os.mkdir(out_dir)
180 
181  part_nums = map(lambda line: line[0], rows)
182  for cur_part_num in part_nums:
183  _export_part(db, out_dir, batch_num, cur_part_num)
184 
185  _export_agregate_part(db, out_dir, batch_num)
186 
187 def export_data(db_path, save_dir, progress_fraction_fcn):
188  db = Database(db_path)
189 
190  rows = db.select(
191  'clips',
192  ['distinct Batch_Num'],
193  where_cond = 'Batch_Num is not null'
194  )
195  batch_nums = map(lambda line: line[0], rows)
196 
197  for i in range(len(batch_nums)):
198  _export_batch(db, batch_nums[i], save_dir)
199  progress_fraction_fcn((i + 1) / float(len(batch_nums)))
200 
201  db.close()