Ejemplo n.º 1
0
def stop_experiment():
    """
    Stop the currently running experiment.
    """
    if session_state["is_running"] is False:
        raise ExperimentException("Session is not running.")

    try:
        cur_experiment.end_trial()
        cur_experiment.end_block()
        cur_experiment.end()
    except Exception:
        log.exception("Exception while ending session:")
    finally:
        try:
            schedule.cancel_all(pool="experiment_phases", wait=False)
        except ValueError:
            pass

        session_state["is_running"] = False

        event_logger.log("session/stop", session_state.get_self())
        set_phase(session_state.get("cur_block", 0),
                  session_state.get("cur_trial", 0))
        _update_state_file()

    log.info(f"Experiment {session_state['experiment']} has ended.")
Ejemplo n.º 2
0
    def end(self):
        if exp.get_params().get("record_exp", True):
            video_system.stop_record()
        if self.cancel_trials != None:
            self.cancel_trials()

        schedule.cancel_all()
        mqtt.client.publish(topic="monitor/color", payload="black")

        self.log.info("exp ended")
    def end(self, params):
        # on end cancel all records and schedules
        if params.get("record_exp", True) or params["record_all"]:
            video_system.stop_record()
        if self.cancel_trials != None:
            self.cancel_trials()
        if self.cancel_logic_trial != None:
            self.cancel_logic_trial()
        schedule.cancel_all()
        mqtt.client.publish(topic="monitor/color", payload="black")
        exp.image_observers["head_bbox"].stop_observing()
        self.yolo_log.stop()

        if exp.state["arena", "signal_led"]:
            arena.signal_led(False)
        mqtt.client.unsubscribe("reptilearn/pogona_head_bbox")
        mqtt.client.unsubscribe("reptilearn/learn_exp/end")
        self.log.info("exp ended")
Ejemplo n.º 4
0
def close_session():
    """
    Close the current session. Updates the session state file,
    calls the experiment class release() hook, shutdowns the event logger,
    removes session state from the global state.
    """
    global cur_experiment

    if not session_state.exists(()):
        raise ExperimentException("There is no started session.")

    if session_state["is_running"] is True:
        raise ExperimentException(
            "Can't close session while experiment is running.")

    _update_state_file()

    if cur_experiment is not None:
        try:
            cur_experiment.release()
        except Exception:
            log.exception("While releasing experiment:")

        try:
            schedule.cancel_all(pool="experiment", wait=False)
        except ValueError:
            pass
        except Exception:
            log.exception("While cancelling experiment schedules:")

    cur_experiment = None

    if event_logger is not None:
        event_logger.log("session/close", session_state.get_self())
        event_logger.stop()

    video_system.restore_after_experiment_session()
    session_state.delete(())
    log.info("Closed session.")
Ejemplo n.º 5
0
def set_phase(block, trial, force_run=False):
    """
    Set the current block and trial numbers.
    Calls the run_block() and run_trial() experiment class hooks.

    block, trial: int indices (starting with 0).
    force_run: When True, the hooks will be called even when the parameters are
    the same as the current phase.
    """
    if blocks.exists(()):
        if len(blocks.get_self()) <= block and block != 0:
            raise ExperimentException(f"Block {block} is not defined.")
    else:
        raise ExperimentException(
            "Session doesn't have any block definitions.")

    if not session_state["is_running"]:
        session_state.update((), {"cur_block": block, "cur_trial": trial})
        return
    else:
        cur_trial = session_state.get("cur_trial", None)
        cur_block = session_state.get("cur_block", None)

        if cur_trial != trial or cur_block != block:
            cur_experiment.end_trial()

        if cur_block != block:
            cur_experiment.end_block()

        num_trials = get_params().get("$num_trials", None)
        if num_trials is not None and trial >= num_trials:
            raise ExperimentException(
                f"Trial {trial} is out of range for block {block}.")

        def start_trial():
            session_state.update((), {"cur_block": block, "cur_trial": trial})

            if cur_block != block or force_run:
                cur_experiment.run_block()

            if cur_trial != trial or cur_block != block or force_run:
                cur_experiment.run_trial()

            block_duration = get_params().get("$block_duration", None)
            if block_duration is not None:
                schedule.once(next_block,
                              block_duration,
                              pool="experiment_phases")

            trial_duration = get_params().get("$trial_duration", None)
            if trial_duration is not None:
                schedule.once(next_trial,
                              trial_duration,
                              pool="experiment_phases")

        try:
            schedule.cancel_all(pool="experiment_phases", wait=False)
        except ValueError:
            pass

        iti = get_params().get("$inter_trial_interval", None)
        if iti is not None and cur_block != block or cur_trial != trial:
            schedule.once(start_trial, iti)
        else:
            start_trial()