21 filenames.append(cur_row[1])
27 path = path.replace(
'\\',
'/')
30 db.delete(
'naptime_files')
34 map(
lambda name: [name], filenames)
38 db.update_timestamp_col(
41 where_cond=
'code_name=?',
42 params=[
'LAST_NAPTIME_UPDATE']
48 where_cond=
'code_name=?',
49 params=[path,
'LAST_NAPTIME_FOLDER']
60 items = os.listdir(path)
62 for cur_item
in items:
63 cur_item =
'%s/%s' % (path, cur_item)
64 if os.path.isdir(cur_item):
65 files.extend(Naptime.get_naptime_files(cur_item))
66 elif cur_item.lower().endswith(
'complete.csv'):
67 files.append(cur_item)
73 logger = logging.getLogger()
75 naptime_filenames = Naptime.get_naptime_files(path)
76 processed_filenames = []
82 for i
in range(len(naptime_filenames)):
84 nap_file = open(naptime_filenames[i],
'rb')
87 reader = csv.DictReader(nap_file)
90 month, day, year = rows[0][
'Clock_Time_TZAdj'].split(
' ')[0].split(
'/')
92 year =
'20%s' % (year)
93 child_id = rows[0][
'File_Name'].split(
'_')[0].upper()
94 child_cd =
'%s_%d%02d%02d' % (child_id, int(year), int(month), int(day))
97 dur_cols = [
'Segment_Duration',
'Audio_Duration',
'Block_Duration']
99 while dur_cols[k]
not in rows[0]
and k < len(dur_cols):
103 if k < len(dur_cols):
104 dur_col_title = dur_cols[k]
105 print 'Using dur_col_title="%s"' % (dur_col_title)
107 raise Exception(
'Raising a fuss because there\'s no Segment_Duration or Audio_Duration column in this file.')
109 if 'Naptime' not in rows[0]:
110 print 'Can\'t find "Naptime" column!'
114 last_is_naptime = bool(rows[0][
'Naptime'].strip().lower() ==
'naptime')
115 accum_time = float(rows[0][
'Elapsed_Time']) + float(rows[0][dur_col_title])
116 accum_time = round(accum_time, 2)
117 nap_start = accum_time
if last_is_naptime
else None
120 for j
in range(1, len(rows)):
121 cur_is_naptime = bool(rows[j][
'Naptime'].strip().lower() ==
'naptime')
122 cur_dur = float(rows[j][dur_col_title])
123 cur_dur = round(cur_dur, 2)
125 if cur_is_naptime
and not last_is_naptime:
126 nap_start = accum_time
128 elif not cur_is_naptime
and last_is_naptime:
133 'child_cd start end'.split(),
141 if cur_is_naptime
and j == len(rows) - 1:
142 nap_end = accum_time + cur_dur
146 'child_cd start end'.split(),
153 accum_time += cur_dur
154 last_is_naptime = cur_is_naptime
157 processed_filenames.append(naptime_filenames[i])
160 prog_diag.set_fraction(float(i + 1) / float(len(naptime_filenames)))
162 except Exception
as e:
164 print 'Error processing %s:' % (naptime_filenames[i])
166 print "Stack trace: %s" % (traceback.format_exc())
167 error_filenames.append(naptime_filenames[i])
169 Naptime._update_settings(db, processed_filenames, path)
171 return error_filenames
175 file_in = open(input_path,
'rb')
176 file_out = open(output_path,
'wb')
178 src_reader = csv.DictReader(file_in)
179 dest_writer = csv.DictWriter(file_out, src_reader.fieldnames)
180 dest_writer.writeheader()
182 rows = list(src_reader)
183 accum_start = float(rows[0][
'Elapsed_Time'])
184 accum_start = round(accum_start, 2)
187 month, day, year = rows[0][
'Clock_Time_TZAdj'].split(
' ')[0].split(
'/')
189 year =
'20%s' % (year)
191 child_id = rows[0][
'File_Name'].split(
'_')[0].upper()
192 child_cd =
'%s_%d%02d%02d' % (child_id, int(year), int(month), int(day))
194 dur_col =
'Segment_Duration' if 'Segment_Duration' in rows[0]
else 'Audio_Duration'
196 for i
in range(len(rows)):
197 seg_dur = float(rows[i][dur_col])
198 seg_dur = round(seg_dur, 2)
199 accum_end = accum_start + seg_dur
202 intersect_rows = db.select(
205 where_cond=
'child_cd = ? and ' +
206 '((end > ? and end <= ?) or ' +
207 '(start < ? and start >= ?) or ' +
208 '(start < ? and end > ?))',
210 accum_start, accum_end,
211 accum_end, accum_start,
212 accum_start, accum_end,
220 for db_row
in intersect_rows:
221 db_start, db_end = db_row
224 if db_end > accum_start
and db_end <= accum_end:
225 mag = db_end - max(accum_start, db_start)
228 elif db_start < accum_end
and db_start >= accum_start:
229 mag = min(accum_end, db_end) - db_start
233 mag = accum_end - accum_start
236 if mag >= largest_mag:
241 if round(largest_mag, 2) < round(seg_dur / 2.0, 2):
242 dest_writer.writerow(rows[i])
244 accum_start += seg_dur