diff --git a/bcipy/simulator/helpers/metrics.py b/bcipy/simulator/helpers/metrics.py index 35fcb4075..8bed0d69b 100644 --- a/bcipy/simulator/helpers/metrics.py +++ b/bcipy/simulator/helpers/metrics.py @@ -5,6 +5,7 @@ import numpy as np +from bcipy.helpers.acquisition import max_inquiry_duration from bcipy.simulator.helpers.state_manager import SimState from bcipy.simulator.helpers.types import InquiryResult from bcipy.simulator.simulator_base import Simulator @@ -31,22 +32,31 @@ class SimMetrics1Handler(RefereeHandler): def handle(self, sim: Simulator) -> Dict[str, Any]: state: SimState = getattr(sim, ("state_manager")).get_state() - info: Dict = SimMetrics1().__dict__ - info['total_inquiries'] = state.total_inquiry_count() - info['total_series'] = state.series_n - + # calculating total decisions made flattened_inquiries: List[InquiryResult] = reduce(lambda l1, l2: l1 + l2, state.series_results, []) - info['total_decisions'] = len( - list(filter(lambda inq: bool(inq.decision), flattened_inquiries))) + total_decisions = len(list(filter(lambda inq: bool(inq.decision), flattened_inquiries))) + # average number of inqs before a decision inq_counts = [] for series in state.series_results: if [inq for inq in series if inq.decision]: inq_counts.append(len(series)) - info['inquiries_per_selection'] = round(np.array(inq_counts).mean(), 3) + inquiries_per_selection = round(np.array(inq_counts).mean(), 3) + + # inq and series counts + total_inquiries = state.total_inquiry_count() + total_series = state.series_n + + # total time spent estimate seconds = (max_inq_time_estimate * total_inquiries) + parameters = sim.get_parameters() + max_inq_time = max_inquiry_duration(parameters) + total_time_spent = total_inquiries * max_inq_time - return info + ret = SimMetrics1(total_series=total_series, total_inquiries=total_inquiries, + total_decisions=total_decisions, total_time_spent=total_time_spent, + inquiries_per_selection=inquiries_per_selection) + return ret.__dict__ class MetricReferee(ABC): @@ -62,19 +72,18 @@ def visualize(self, sim: Simulator): """ Generate registered visualizations for sim """ @abstractmethod - def set_metric_handler(self, name: str, handler: RefereeHandler): + def add_metric_handler(self, name: str, handler: RefereeHandler): """ Add a metric calculation that will be executed upon self.score() """ @abstractmethod - def set_viz_handler(self, name: str, handler: RefereeHandler): + def add_viz_handler(self, name: str, handler: RefereeHandler): """ Register a visualization that will be executed upon self.visualize() """ class RefereeImpl(MetricReferee): def __init__(self, metric_handlers=None, viz_handlers=None): - self.metric_handlers: Dict[ - str, RefereeHandler] = metric_handlers if metric_handlers else {} + self.metric_handlers: Dict[str, RefereeHandler] = metric_handlers if metric_handlers else {} self.viz_handlers: Dict[str, RefereeHandler] = viz_handlers if viz_handlers else {} self.inquiry_time: float = 1 # 1 inq -> 1 second @@ -96,8 +105,8 @@ def visualize(self, sim: Simulator): for handler_name, viz_handler in self.viz_handlers.items(): viz_handler.handle(sim) - def set_metric_handler(self, name: str, handler: RefereeHandler): + def add_metric_handler(self, name: str, handler: RefereeHandler): self.metric_handlers[name] = handler - def set_viz_handler(self, name: str, handler: RefereeHandler): + def add_viz_handler(self, name: str, handler: RefereeHandler): self.viz_handlers[name] = handler diff --git a/bcipy/simulator/sim.py b/bcipy/simulator/sim.py index 3ad8f1cc9..8170f2035 100644 --- a/bcipy/simulator/sim.py +++ b/bcipy/simulator/sim.py @@ -138,5 +138,5 @@ def load_parameters(self, params: Optional[Parameters]): else: return self.data_engine.get_parameters() - def get_param(self, name): - pass + def get_parameters(self): + return self.parameters.copy() diff --git a/bcipy/simulator/sim_parameters.json b/bcipy/simulator/sim_parameters.json index 7c369ccff..dd16d0066 100644 --- a/bcipy/simulator/sim_parameters.json +++ b/bcipy/simulator/sim_parameters.json @@ -34,7 +34,7 @@ "type": "float" }, "sim_lm_active": { - "value": "1", + "value": "0", "section": "sim_config", "readableName": "is lm active or off", "helpTip": "", diff --git a/bcipy/simulator/simulator_base.py b/bcipy/simulator/simulator_base.py index 79eaae114..83db6dbf1 100644 --- a/bcipy/simulator/simulator_base.py +++ b/bcipy/simulator/simulator_base.py @@ -2,6 +2,8 @@ from abc import ABC, abstractmethod +from bcipy.helpers.parameters import Parameters + class Simulator(ABC): """Simulator. @@ -17,5 +19,5 @@ def run(self): """ Run loop for simulation""" @abstractmethod - def get_param(self, name): - """ retrieving parameter """ + def get_parameters(self) -> Parameters: + """ retrieving parameters copy"""