def _write_sysinfo(self, filename=None): if filename is None: filename = self._sysinfo_slog sysinfo_logger = LogWriter(filename=filename) sysinfo_logger.write_record(data=self._sysinfo) sysinfo_logger.close()
class KeyRecord(KeyState): def __init__(self, parent=None, duration=None, name=None): super(KeyState, self).__init__(parent=parent, duration=duration, save_log=False, name=name) def begin_log(self): super(KeyRecord, self).begin_log() title = "keyrec_%s_%d_%s" % ( os.path.splitext(os.path.basename(self._instantiation_filename))[0], self._instantiation_lineno, self._name, ) self.__log_filename = self._exp.reserve_data_filename(title, "smlog") self.__log_writer = LogWriter(self.__log_filename, ["timestamp", "key", "state"]) def end_log(self, to_csv=False): super(KeyRecord, self).end_log(to_csv) if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = None if to_csv: csv_filename = os.path.splitext(self.__log_filename)[0] + ".csv" log2csv(self.__log_filename, csv_filename) def _on_key_down(self, keycode, text, modifiers, event_time): self.__log_writer.write_record({"timestamp": event_time, "key": keycode[1].upper(), "state": "down"}) def _on_key_up(self, keycode, event_time): self.__log_writer.write_record({"timestamp": event_time, "key": keycode[1].upper(), "state": "up"})
class Log(AutoFinalizeState): def __init__(self, parent=None, name=None, **kwargs): # init the parent class super(Log, self).__init__(parent=parent, name=name, duration=0.0, save_log=False) self._init_log_items = kwargs def begin_log(self): super(Log, self).begin_log() title = "log_%s_%d_%s" % ( os.path.splitext( os.path.basename(self._instantiation_filename))[0], self._instantiation_lineno, self._name) self.__log_filename = self._exp.reserve_data_filename(title, "smlog") self.__log_writer = LogWriter(self.__log_filename, ["time"] + self._init_log_items.keys()) def end_log(self, to_csv=False): super(Log, self).end_log(to_csv) if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = None if to_csv: csv_filename = (os.path.splitext(self.__log_filename)[0] + ".csv") log2csv(self.__log_filename, csv_filename) def _enter(self): record = self._log_items.copy() record["time"] = self._start_time self.__log_writer.write_record(record) clock.schedule(self.leave)
class KeyRecord(KeyState): def __init__(self, parent=None, duration=None, name=None, blocking=True): super(KeyState, self).__init__(parent=parent, duration=duration, save_log=False, name=name, blocking=blocking) def begin_log(self): super(KeyRecord, self).begin_log() title = "keyrec_%s_%d_%s" % (os.path.splitext( os.path.basename(self._instantiation_filename))[0], self._instantiation_lineno, self._name) self.__log_filename = self._exp.reserve_data_filename(title, "smlog") self.__log_writer = LogWriter(self.__log_filename, ["timestamp", "key", "state"]) def end_log(self, to_csv=False): super(KeyRecord, self).end_log(to_csv) if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = None if to_csv: csv_filename = (os.path.splitext(self.__log_filename)[0] + ".csv") log2csv(self.__log_filename, csv_filename) def _on_key_down(self, keycode, text, modifiers, event_time): self.__log_writer.write_record({ "timestamp": event_time, "key": keycode[1].upper(), "state": "down" }) def _on_key_up(self, keycode, event_time): self.__log_writer.write_record({ "timestamp": event_time, "key": keycode[1].upper(), "state": "up" })
class KeyRecord(KeyState): """A state that records keypresses during a duration. A *KeyRecord* state will record any keypress, the keyup's and keydown's, as well as any timing associated with them for a duration. Parameters ---------- duration : float (optional) The duration you would like your experiment to wait for a keypress. If set to None, then it will wait until a key from **keys** is pressed, then continue with the experiment. parent : ParentState (optional) The state you would like this state to be a child of. If not set, the *Experiment* will make it a child of a ParentState or the Experiment automatically. name : string (optional) The unique name of this state blocking : boolean (optional, default = True) If True, this state will prevent a *Parallel* state from ending. If False, this state will be canceled if its Parallel Parent finishes running. Only relevant if within a *Parallel* Parent. Logged Attributes ----------------- All parameters above and below are available to be accessed and manipulated within the experiment code, and will be automatically recorded in the state-specific log. Refer to State class docstring for additional logged parameters. """ def __init__(self, parent=None, duration=None, name=None, blocking=True): super(KeyState, self).__init__(parent=parent, duration=duration, save_log=False, name=name, blocking=blocking) def begin_log(self): super(KeyRecord, self).begin_log() title = "keyrec_%s_%d_%s" % (os.path.splitext( os.path.basename(self._instantiation_filename))[0], self._instantiation_lineno, self._name) self.__log_filename = self._exp.reserve_data_filename(title, "slog") self.__log_writer = LogWriter(self.__log_filename) def end_log(self, to_csv=False): super(KeyRecord, self).end_log(to_csv) if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = None if to_csv: csv_filename = (os.path.splitext(self.__log_filename)[0] + ".csv") log2csv(self.__log_filename, csv_filename) def _on_key_down(self, keycode, text, modifiers, event_time): self.__log_writer.write_record({ "timestamp": event_time, "key": keycode[1].upper(), "state": "down" }) def _on_key_up(self, keycode, event_time): self.__log_writer.write_record({ "timestamp": event_time, "key": keycode[1].upper(), "state": "up" })
class KeyRecord(KeyState): """A state that records keypresses during a duration. A *KeyRecord* state will record any keypress, the keyup's and keydown's, as well as any timing associated with them for a duration. Parameters ---------- duration : float (optional) The duration you would like your experiment to wait for a keypress. If set to None, then it will wait until a key from **keys** is pressed, then continue with the experiment. parent : ParentState (optional) The state you would like this state to be a child of. If not set, the *Experiment* will make it a child of a ParentState or the Experiment automatically. name : string (optional) The unique name of this state blocking : boolean (optional, default = True) If True, this state will prevent a *Parallel* state from ending. If False, this state will be canceled if its Parallel Parent finishes running. Only relevant if within a *Parallel* Parent. Logged Attributes ----------------- All parameters above and below are available to be accessed and manipulated within the experiment code, and will be automatically recorded in the state-specific log. Refer to State class docstring for additional logged parameters. """ def __init__(self, parent=None, duration=None, name=None, blocking=True): super(KeyState, self).__init__(parent=parent, duration=duration, save_log=False, name=name, blocking=blocking) self.__log_filename = None self.__log_writer = None def begin_log(self): super(KeyRecord, self).begin_log() title = "keyrec_%s_%d_%s" % ( os.path.splitext( os.path.basename(self._instantiation_filename))[0], self._instantiation_lineno, self._name) if self.__log_filename is not None: os.remove(self.__log_filename) self.__log_filename = self._exp.reserve_data_filename(title, "slog") if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = LogWriter(self.__log_filename) def end_log(self, to_csv=False): super(KeyRecord, self).end_log(to_csv) if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = None if to_csv: csv_filename = (os.path.splitext(self.__log_filename)[0] + ".csv") log2csv(self.__log_filename, csv_filename) def _on_key_down(self, keycode, text, modifiers, event_time): self.__log_writer.write_record({ "timestamp": event_time, "key": keycode[1].upper(), "state": "down"}) def _on_key_up(self, keycode, event_time): self.__log_writer.write_record({ "timestamp": event_time, "key": keycode[1].upper(), "state": "up"})
class Record(State): def __init__(self, duration=None, parent=None, name=None, blocking=True, **kwargs): super(Record, self).__init__(parent=parent, duration=duration, save_log=False, name=name, blocking=blocking) self.__refs = kwargs def begin_log(self): super(Record, self).begin_log() title = "record_%s_%d_%s" % ( os.path.splitext( os.path.basename(self._instantiation_filename))[0], self._instantiation_lineno, self._name) self.__log_filename = self._exp.reserve_data_filename(title, "smlog") self.__log_writer = LogWriter(self.__log_filename, self.__refs.keys() + ["timestamp"]) def end_log(self, to_csv=False): super(Record, self).end_log(to_csv) if self.__log_writer is not None: self.__log_writer.close() self.__log_writer = None if to_csv: csv_filename = (os.path.splitext(self.__log_filename)[0] + ".csv") log2csv(self.__log_filename, csv_filename) def _enter(self): clock.schedule(self.leave, event_time=self._start_time) if self._end_time is not None: clock.schedule(self.finalize, event_time=self._end_time) def _leave(self): for name, ref in self.__refs.iteritems(): self.record_change() ref.add_change_callback(self.record_change) def finalize(self): super(Record, self).finalize() for name, ref in self.__refs.iteritems(): ref.remove_change_callback(self.record_change) def record_change(self): record = val(self.__refs) record["timestamp"] = self._exp._app.event_time self.__log_writer.write_record(record) def cancel(self, cancel_time): if self._active: if cancel_time < self._start_time: clock.unschedule(self.leave) clock.schedule(self.leave) clock.unschedule(self.finalize) clock.schedule(self.finalize) self._end_time = self._start_time elif self._end_time is None or cancel_time < self._end_time: clock.unschedule(self.finalize) clock.schedule(self.finalize, event_time=cancel_time) self._end_time = cancel_time