def leave(self): """ Gets the end time of the state (logs current time) """ # ignore leave call if not active if self._following_may_run or not self._active: return self.claim_exceptions() self._leave_time = clock.now() self._following_may_run = True if self._parent: clock.schedule(partial(self._parent.child_leave_callback, self)) # call custom leave code self._leave() if self.__tracing: call_time = self._leave_time - self._exp._root_state._start_time call_duration = clock.now() - self._leave_time if self._end_time is None: self.print_trace_msg( "LEAVE time=%fs, duration=%fs, perpetual" % (call_time, call_duration)) else: end_time = self._end_time - self._exp._root_state._start_time self.print_trace_msg( "LEAVE time=%fs, duration=%fs, end_time=%fs" % (call_time, call_duration, end_time))
def _callback(self): # we've started self._started = True # send the code global have_parallel if have_parallel: # send the port code and time it try: # Create a parallel port object (locks it exclusively) self._pport = parallel.Parallel(port=self._port) start_time = clock.now() self._pport.setData(self._code_num) end_time = clock.now() except: # eventually figure out which errors to catch sys.stderr.write("\nWARNING: The parallel module could not send pulses,\n" + "\tso no sync pulsing will be generated.\n\n") have_parallel = False self._pport = None self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize) return # set the pulse time time_err = (end_time - start_time)/2. self._pulse_on = event_time(start_time+time_err, time_err) # schedule leaving (as soon as this method is done) clock.schedule(self.leave) # schedule the off time if self._width > 0.0: # we're gonna turn off ourselves clock.schedule(self._pulse_off_callback, event_time=self._pulse_on['time']+self._width) else: # we're gonna leave it self._pulse_off = None # clean up/ close the port self._pport = None # so we can finalize now, too clock.schedule(self.finalize) self._ended = True else: # we can leave and finalize now self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize)
def finalize(self): #TODO: call a _finalize method? if not self._active: return self._finalize_time = clock.now() self._active = False if self.__save_log: self.save_log() if self._parent: clock.schedule(partial(self._parent.child_finalize_callback, self)) if self.__tracing: call_time = self._finalize_time - self._exp._root_state._start_time call_duration = clock.now() - self._finalize_time self.print_trace_msg("FINALIZE time=%fs, duration=%fs" % (call_time, call_duration))
def FindNextChange(): ts = [] @autostart def src(): ts.append((yield 1)) # t0 ts.append((yield 1)) # t1 ts.append((yield 2)) # t2 ts.append((yield 2)) # t3 ts.append((yield 2)) # t4 ts.append((yield 3)) # t5 t0 = clock.now() t1 = t0 + timedelta(seconds=1) t2 = t0 + timedelta(seconds=2) t3 = t0 + timedelta(seconds=3) t4 = t0 + timedelta(seconds=4) t5 = t0 + timedelta(seconds=5) t6 = t0 + timedelta(seconds=6) g = find_next_change(src()) t, v = g.next() assert v == 1 assert t0 <= t <= t1, (t0, t, t1) assert ts == [t], ts t, v = g.next() assert v == 2, v assert t1 <= t <= t3 assert ts[-1] == t t, v = g.next() assert v == 3 assert t4 <= t <= t6 assert ts[-1] == t
def _callback(self): if self._server is not None: if type(self._push_val) != list: self._server.push_sample([self._push_val]) else: self._server.push_sample(self._push_val) self._push_time = clock.now()
def _pulse_off_callback(self): # turn off the code start_time = clock.now() self._pport.setData(0) end_time = clock.now() # clean up / close the port self._pport = None # set the pulse time time_err = (end_time - start_time)/2. self._pulse_off = event_time(start_time+time_err, time_err) # let's schedule finalizing self._ended = True clock.schedule(self.finalize)
def _start_recording(self): self.__rec = pyo.Record(pyo.Input(), filename=self._filename, chnls=2, fileformat=0, sampletype=1, buffering=16) self._rec_start = clock.now()
def _pulse_off_callback(self): self._task.write([0.0]) ev = clock.now() # set the pulse time self._pulse_off = event_time(ev, 0.0) # let's schedule finalizing self._ended = True clock.schedule(self.finalize)
def _callback(self): id_strs = [ "file: %r" % self._instantiation_filename, "line: %d" % self._instantiation_lineno ] if self._name is not None: id_strs.append("name: %s" % self._name) lag = clock.now() - self._start_time print "DEBUG (%s) - lag=%fs" % (", ".join(id_strs), lag) for name, value in self._kwargs.iteritems(): print " %s: %r" % (name, value)
def _pulse_off_callback(self): # turn off the code if self._sync_style == "parallel": start_time = clock.now() self._sport.setData(0) end_time = clock.now() else: start_time = clock.now() self._sport.setData("0") end_time = clock.now() # clean up / close the port self._sport = None # set the pulse time time_err = (end_time - start_time) / 2. self._pulse_off = event_time(start_time + time_err, time_err) # let's schedule finalizing self._ended = True clock.schedule(self.finalize)
def find_next_change(g, timeout=30): t = clock.now() v = g.send(t) while True: yield t, v prev = v dt = 0 while v == prev and dt < timeout: dt += 1 t += timedelta(seconds=1) v = g.send(t)
def print_traceback(self, child=None, t=None): # Use the current time, if none is provided. if t is None: t = clock.now() if self._parent is None: # If we are the root of the state tree, print the header. print " SMILE Traceback:" else: # Otherwise, let our parent print its traceback first. self._parent.print_traceback(self, t) # Get a string for the parenthesized state name, or an empty string if # the state has no custom name... if self._name is None: name_spec = "" else: name_spec = " (%s)" % self._name # Nested function to format time values as strings... def tstr(tm): if (isinstance(tm, dict) and len(tm) == 2 and "time" in tm and "error" in tm): if tm["error"] is None: error = "" else: error = ", error=%fs" % tm["error"] tm = tm["time"] else: error = "" if type(tm) not in (float, int): return repr(tm) offset = t - tm if offset < 0.0: return "%fs from now%s" % (-offset, error) else: return "%fs ago%s" % (offset, error) # Print traceback state header. print " %s%s - file: %s, line: %d" % ( type(self).__name__, name_spec, self._instantiation_filename, self._instantiation_lineno) # Print out log attributes... for attr_name in self._log_attrs: value = val(getattr(self, attr_name)) if attr_name.endswith("_time"): print " %s: %s" % (attr_name, tstr(value)) else: print " %s: %r" % (attr_name, value)
def enter(self, start_time): self.claim_exceptions() self._start_time = start_time self._enter_time = clock.now() self._leave_time = None self._finalize_time = None self.__original_state.__most_recently_entered_clone = self # say we're active self._active = True self._following_may_run = False if self._parent: clock.schedule(partial(self._parent.child_enter_callback, self)) # if we don't have the exp reference, get it now if self._exp is None: from experiment import Experiment self._exp = Experiment._last_instance() for name, value in self.__dict__.items(): if name[:6] == "_init_": setattr(self, name[5:], val(value)) if self._duration is None: self._end_time = None else: self._end_time = self._start_time + self._duration # custom enter code self._enter() if self.__tracing: call_time = self._enter_time - self._exp._root_state._start_time call_duration = clock.now() - self._enter_time start_time = self._start_time - self._exp._root_state._start_time self.print_trace_msg( "ENTER time=%fs, duration=%fs, start_time=%fs" % (call_time, call_duration, start_time))
def rise_and_set(lat, lon, horizon=0): sun = Sun() date = None while True: loc = Observer() loc.horizon = str(horizon) loc.lat = str(lat) loc.lon = str(lon) loc.date = clock.now() #if date: # loc.date = date #loc.date = loc.date.datetime().date() # strip time t_rise = loc.next_rising(sun) t_set = loc.next_setting(sun) #date = yield localtime(t_rise), localtime(t_set) yield localtime(t_rise), localtime(t_set)
def update(self): self.claim_exceptions() now = clock.now() if self.__initial_params is None: self.__initial_params = { name : getattr(self.__target_clone, name).eval() for name in self.__anim_params.iterkeys()} if self._end_time is not None and now >= self._end_time: clock.unschedule(self.update) clock.schedule(self.finalize) now = self._end_time t = now - self._start_time params = {name : func(t, self.__initial_params[name]) for name, func in self.__anim_params.iteritems()} self.__target_clone.live_change( **self.__target_clone.transform_params( self.__target_clone.apply_aliases(params)))
def update(self): self.claim_exceptions() now = clock.now() if self.__initial_params is None: self.__initial_params = { name: getattr(self.__target_clone, name).eval() for name in self.__anim_params.iterkeys() } if self._end_time is not None and now >= self._end_time: clock.unschedule(self.update) clock.schedule(self.finalize) now = self._end_time t = now - self._start_time params = { name: func(t, self.__initial_params[name]) for name, func in self.__anim_params.iteritems() } self.__target_clone.live_change(**self.__target_clone.transform_params( self.__target_clone.apply_aliases(params)))
def phase(d0, d1, curve, r0, r1): """Coroutine mapping domain (d0,d1) to range (r0,r1), allowing func to alter the curve.""" if isinstance(d0, (datetime, time)): d0 = datetime2hours(d0) if isinstance(d1, (datetime, time)): d1 = datetime2hours(d1) if d1 < d0: d1 += 24. t = yield while True: if t is None: t = clock.now() h = t.hour + t.minute / 60. + t.second / 3600. if h < d0: h += 24. if not d0 <= h <= d1: return f0 = (h - d0) / (d1 - d0) f1 = curve.send(f0) t = yield int(0.5 + r0 + f1 * (r1 - r0))
def _callback(self): # we've started self._started = True # push it outlet global _got_nidaqmx if _got_nidaqmx: if type(self._push_vals == list): self._task.write(self._push_vals) else: self._task.write([self.push_vals]) ev = clock.now() else: self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize) return # set the pulse time self._pulse_on = event_time(ev, 0.0) # schedule leaving (as soon as this method is done) clock.schedule(self.leave) # schedule the off time if self._width > 0.0: # we're gonna turn off ourselves clock.schedule(self._pulse_off_callback, event_time=self._pulse_on['time'] + self._width) else: # we're gonna leave it self._pulse_off = None # so we can finalize now, too clock.schedule(self.finalize) self._ended = True
def _start_recording(self): self.__rec = pyo.Record( pyo.Input(), filename=self._filename, chnls=2, fileformat=0, sampletype=1, buffering=16) self._rec_start = clock.now()
def _stop_recording(self): if self.__rec is not None: self.__rec.stop() self._rec_stop = clock.now() self.__rec = None
def _start_sound(self): self.__snd.out() self._sound_start_time = clock.now()
def _stop_sound(self): if self.__snd is not None: self.__snd.stop() self._sound_stop_time = clock.now() self.__snd = None
def _start_sound(self): self.__sine.out() self.__fader.play() self._sound_start_time = clock.now()
def _stop_sound(self): if self.__fader is not None: self.__fader.stop() self._sound_stop_time = clock.now() self.__fader = None self.__sine = None
def _callback(self): # we've started self._started = True # Pull in the global variables global PI global SI if PI or SI: # send the port code and time it try: if self._sync_style == "parallel": # Create a parallel port object # from the global variable (locks it exclusively) self._sport = PI(address=self._port) start_time = clock.now() self._sport.setData(self._code_num) end_time = clock.now() elif self._sync_style == "serial": self._sport = SI(address=self._port) start_time = clock.now() self._sport.setData(self._code_num) end_time = clock.now() except: # eventually figure out which errors to catch sys.stderr.write("\nWARNING: The sync module could not send pulses,\n" + "\tso no sync pulsing will be generated.\n\n") PI = None self._pport = None self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize) return # set the pulse time time_err = (end_time - start_time)/2. self._pulse_on = event_time(start_time+time_err, time_err) # schedule leaving (as soon as this method is done) clock.schedule(self.leave) # schedule the off time if self._width > 0.0: # we're gonna turn off ourselves clock.schedule(self._pulse_off_callback, event_time=self._pulse_on['time']+self._width) else: # we're gonna leave it self._pulse_off = None # clean up/ close the port self._sport = None # so we can finalize now, too clock.schedule(self.finalize) self._ended = True else: # we can leave and finalize now self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize)
def dispatch(scheduler, observer): try: t = observer.send(clock.now()) except StopIteration: return add_timer(t, scheduler, observer)
def _callback(self): # we've started self._started = True # Pull in the global variables global PI global SI if PI or SI: # send the port code and time it try: if self._sync_style == "parallel": # Create a parallel port object # from the global variable (locks it exclusively) self._sport = PI(address=self._port) start_time = clock.now() self._sport.setData(self._code_num) end_time = clock.now() elif self._sync_style == "serial": self._sport = SI(address=self._port) start_time = clock.now() self._sport.setData(self._code_num) end_time = clock.now() except: # eventually figure out which errors to catch sys.stderr.write( "\nWARNING: The sync module could not send pulses,\n" + "\tso no sync pulsing will be generated.\n\n") PI = None self._pport = None self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize) return # set the pulse time time_err = (end_time - start_time) / 2. self._pulse_on = event_time(start_time + time_err, time_err) # schedule leaving (as soon as this method is done) clock.schedule(self.leave) # schedule the off time if self._width > 0.0: # we're gonna turn off ourselves clock.schedule(self._pulse_off_callback, event_time=self._pulse_on['time'] + self._width) else: # we're gonna leave it self._pulse_off = None # clean up/ close the port self._sport = None # so we can finalize now, too clock.schedule(self.finalize) self._ended = True else: # we can leave and finalize now self._pulse_on = None self._pulse_off = None self._ended = True clock.schedule(self.leave) clock.schedule(self.finalize)
def fourth_age(shire_time=None): return ((shire_time or clock.now()) - FALL_OF_SAURON).total_seconds()
def _callback(self): if self._socket is not None: self._rtn = self._socket.send(self._msg) self._send_time = clock.now()
def manually_controlled_recently(self): state = self._tap_sensor.info()["state"] lastupdated = datetime.strptime(state["lastupdated"], "%Y-%m-%dT%H:%M:%S") # UTC, #FIXME return state["buttonevent"] in [BUTTON2, BUTTON4] and lastupdated > clock.now() - timedelta(hours=6)
def _callback(self): if type(self._push_val) != list: self._server.push_sample([self._push_val], clock.now()) else: self._server.push_sample(self._push_val, clock.now()) self._push_time = clock.now()