Skip to content

Commit

Permalink
result output artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
sreekaroo committed Feb 6, 2024
1 parent 0671627 commit a4b39a1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions bcipy/simulator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from pathlib import Path

from bcipy.simulator.helpers.decision import MaxIterationsSim
from bcipy.simulator.sim_factory import SimulationFactoryV2
from bcipy.simulator.simulator_base import Simulator

Expand Down
3 changes: 3 additions & 0 deletions bcipy/simulator/helpers/decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def decide(self, series: List[InquiryResult]):
"""
raise NotImplementedError()

def __str__(self):
return type(self).__name__


class MinIterations(SimDecisionCriteria):
""" Returns true if the minimum number of iterations have not yet
Expand Down
10 changes: 10 additions & 0 deletions bcipy/simulator/helpers/state_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import dataclasses
import logging
import random
from abc import ABC
Expand Down Expand Up @@ -46,6 +47,15 @@ def get_current_likelihood(self) -> Optional[np.ndarray]:

return cur_likelihood

def to_json(self):
d = dataclasses.asdict(self)
d['series_results'] = [list(map(lambda ir: ir.to_json(), lis)) for lis in
self.series_results]

d['decision_criterion'] = [str(dec) for dec in self.decision_criterion]

return d


class StateManager(ABC):

Expand Down
17 changes: 17 additions & 0 deletions bcipy/simulator/helpers/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
from dataclasses import dataclass
from typing import Optional, List, Dict

Expand All @@ -12,6 +13,14 @@ class SimEvidence:
evidence: np.ndarray
symbol_set: List[str]

def to_json(self) -> Dict:
d = dataclasses.asdict(self)
evidence_list = self.evidence.tolist()
d['evidence'] = evidence_list
d['evidence_type'] = self.evidence_type.name

return d


@dataclass
class InquiryResult:
Expand All @@ -21,3 +30,11 @@ class InquiryResult:
evidences: Dict[str, SimEvidence]
fused_likelihood: np.ndarray
decision: Optional[str]

def to_json(self):
d = dataclasses.asdict(self)

d['fused_likelihood'] = self.fused_likelihood.tolist()
d['evidences'] = {k: v.to_json() for k, v in self.evidences.items()}

return d
19 changes: 18 additions & 1 deletion bcipy/simulator/sim.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import dataclasses
import json
import logging
import random
from time import sleep
from typing import Optional, Dict
from typing import Optional, Dict, Any

import numpy as np

Expand Down Expand Up @@ -103,6 +105,8 @@ def run(self):

log.debug(f"Final State: {final_state}")

self.output_run()

def __make_stimuli(self, state: SimState):
# TODO abstract out
subset_length = 10
Expand Down Expand Up @@ -140,5 +144,18 @@ def load_parameters(self, params: Optional[Parameters]):
else:
return self.data_engine.get_parameters()

def output_run(self):
""" Outputs the results of a run to json file """

final_state = self.state_manager.get_state()
final_state_json: Dict = final_state.to_json()

metric_dict = self.referee.score(self).__dict__
metric_dict.update(final_state_json) # adding state data to metrics

output_path = "bcipy/simulator/generated"

This comment has been minimized.

Copy link
@lawhead

lawhead Feb 6, 2024

Collaborator

The generated folder doesn't exist yet in the repository. You will need to create it and add it. You might also consider modifying .gitignore to ignore its contents.

with open(f"{output_path}/result.json", 'w') as output_file:
json.dump(metric_dict, output_file)

This comment has been minimized.

Copy link
@lawhead

lawhead Feb 6, 2024

Collaborator

I would recommend using indent=1 to pretty print this.


def get_parameters(self):
return self.parameters.copy()

0 comments on commit a4b39a1

Please sign in to comment.