def extend(children, analysis, start, end): #adds additional fields to analysisInfo() #such as time_for_bed (vector), event1_time (vector), dist_event, values for dev for ch in children.values(): cid=ch.child_id for date, d in sorted(ch.dates.items()): t_bed=d.time_for_bed t_asleep=d.event1_time if len(t_bed)>0 or len(t_asleep)>0: if len(t_bed)>0: analysis[cid].use.append(dateDiff(start, t_bed[0])) else: analysis[cid].use.append(dateDiff(start, t_asleep[0])) #print "gg", analysis[cid].use dst_event=d.dev_time_cu num_tr=len(dst_event) if num_tr>0: if len(t_bed)>0: analysis[cid].dist_event.append([dateDiff(start, t_bed[0]), num_tr]) elif len(t_asleep)>0: analysis[cid].dist_event.append([dateDiff(start, t_asleep[0]), num_tr]) else: dev_d=dtToStr((strToDT(dst_event[0])-datetime.timedelta(hours=8))) #print '???', dst_event analysis[cid].dist_event.append([dateDiff(start, dev_d), num_tr]) #print 'device info:', d.device if d.device is not None: for r in d.device.react: device_times=d.device.device_start_stop #print device_times num=len(device_times) if num>0: for times in device_times: #print times if len(t_bed)>0: analysis[cid].dev[r].append([dateDiff(start, t_bed[0]),num ]) elif len(t_asleep)>0: analysis[cid].dev[r].append([dateDiff(start, t_asleep[0]),num ]) else: t_device=dtToStr((strToDT(device_times[0][0])-datetime.timedelta(hours=8))) analysis[cid].dev[r].append([dateDiff(start, t_device),num ]) if len(d.device.react)==0: device_times=d.device.device_start_stop if len(device_times)>0: r='no react' analysis[cid].dev[r]=[] #print 'device no react', device_times num=len(device_times) if num>0: for times in device_times: #print times if len(t_bed)>0: analysis[cid].dev[r].append([dateDiff(start, t_bed[0]),num ]) elif len(t_asleep)>0: analysis[cid].dev[r].append([dateDiff(start, t_asleep[0]),num ]) else: t_device=dtToStr((strToDT(device_times[0][0])-datetime.timedelta(hours=8))) analysis[cid].dev[r].append([dateDiff(start, t_device),num ])
def addFeatureForML(children, analysis): #adds features needed for machine learning, such as std_time_for_bed for cid in analysis.keys(): time_for_beds=[] intervals=[] start_t=[] event1_times=[] for d in children[cid].dates.values(): if len(d.time_for_bed)>0: tm=strToDT(d.time_for_bed[0]).time() time_for_bed=tm.second+tm.minute*60+tm.hour*3600 #print time_for_bed time_for_beds.append(time_for_bed) if len(d.event1_time)>0: tm=strToDT(d.event1_time[0]).time() event1_time=tm.second+tm.minute*60+tm.hour*3600 event1_times.append(event1_time) if hasattr(d.device, 'shown_device_interval'): if len(d.device.shown_device_interval)>0: intervals.append(d.device.shown_device_interval[0]) if hasattr(d.device, 'device_start_stop'): if len(d.device.device_start_stop)>0: stm=strToDT(d.device.device_start_stop[0][0]).time() stms=stm.second+stm.minute*60+stm.hour*3600 start_t.append(stms) time_for_beds=numpy.array(time_for_beds) if len(time_for_beds)>0: analysis[cid].std_time_for_bed=numpy.std(time_for_beds) if len(event1_times)>0: analysis[cid].std_event1_time=numpy.std(event1_times) #print 'test event1 time', cid, analysis[cid].std_event1_time intervals=numpy.array(intervals) if len(intervals)>1: analysis[cid].std_device_interval=numpy.std(intervals) #print 'sdt intervals', analysis[cid].std_device_interval if len(start_t)>1: #print start_t analysis[cid].std_device_start_time=numpy.std(start_t) computeUseRate(analysis) computeDevRate(analysis) computeTimeToEvent1(analysis) computeOffsetDeviceStartTime(analysis) computeVibUse(analysis)
def addSleepChartOneDate(date, dateinfo, chart_rec): start = strToDT(date + ' 00:00:00') for time_str in dateinfo.time_for_bed: chart_rec.time_for_bed.append( (strToDT(time_str) - start).total_seconds() ) event1_time=[] for time_str in dateinfo.event1_time: event1_time.append( (strToDT(time_str) - start).total_seconds() ) #print "event1_time?", event1_time wakeup_times = [] if dateinfo.device is not None: #print "checking", event1_time, chart_rec.time_for_bed for event1_time_sec, device_interval in zip( event1_time, dateinfo.device.shown_device_interval): if device_interval is None: # no prediction break assert event1_time_sec is not None chart_rec.device_time.append( event1_time_sec + device_interval * 60) for ldates, lreact in zip( dateinfo.device.device_start_stop, dateinfo.device.react): lstart_str, lstop_str = ldates lstart_sec = (strToDT(lstart_str) - start).total_seconds() chart_rec.device_vib.append([ lstart_sec, (strToDT(lstop_str) - start).total_seconds(), lreact]) wakeup_times.append(lstart_sec) for time_str in dateinfo.dev_time_cu: terr_time = (strToDT(time_str) - start).total_seconds() chart_rec.night_dsev_time.append(terr_time) wakeup_times.append(terr_time) wakeup_times.sort() for time_str in dateinfo.event1_time: asleep_start = (strToDT(time_str) - start).total_seconds() wakeup_times_after = [t for t in wakeup_times if t > asleep_start] if wakeup_times_after: asleep_end = wakeup_times_after[0] else: # no wakeup time. Assume 8 hours or end of day. asleep_end = min(asleep_start + 8 * 60 * 60, 24 * 60 * 60) chart_rec.event1_time.append([asleep_start, asleep_end])