Skip to content

SimulationResult

SimulationResult

A class that stores the result of running a simulation.

Attributes

SimulationState

The state with which the simulation started.

Source code in pylattica/core/simulation_result.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class SimulationResult:
    """A class that stores the result of running a simulation.

    Attributes
    ----------
    initial_state : SimulationState
        The state with which the simulation started.
    """

    @classmethod
    def from_file(cls, fpath):
        return loadfn(fpath)

    @classmethod
    def from_dict(cls, res_dict):
        diffs = res_dict["diffs"]
        res = cls(SimulationState.from_dict(res_dict["initial_state"]))
        for diff in diffs:
            formatted = {int(k): v for k, v in diff.items() if k != "GENERAL"}
            res.add_step(formatted)
        return res

    def __init__(self, starting_state: SimulationState):
        """Initializes a SimulationResult with the specified starting_state.

        Parameters
        ----------
        starting_state : SimulationState
            The state with which the simulation started.
        """
        self.initial_state = starting_state
        self._diffs: list[dict] = []
        self._stored_states = {}

    def add_step(self, updates: Dict[int, Dict]) -> None:
        """Takes a set of updates as a dictionary mapping site IDs
        to the new values for various state parameters. For instance, if at the
        new step, my_state_attribute at site 23 changed to 12, updates would look
        like this:

        {
            23: {
                "my_state_attribute": 12
            }
        }

        Parameters
        ----------
        updates : dict
            The changes associated with a new simulation step.
        """
        self._diffs.append(updates)

    def __len__(self) -> int:
        return len(self._diffs) + 1

    def steps(self) -> List[SimulationState]:
        """Returns a list of all the steps from this simulation.

        Returns
        -------
        List[SimulationState]
            The list of steps
        """
        live_state = self.initial_state.copy()
        for diff in self._diffs:
            yield live_state
            live_state.batch_update(diff)

    @property
    def last_step(self) -> SimulationState:
        """The last step of the simulation.

        Returns
        -------
        SimulationState
            The last step of the simulation
        """
        return self.get_step(len(self) - 1)

    @property
    def first_step(self):
        return self.get_step(0)

    def set_output(self, step: SimulationState):
        self.output = step

    def load_steps(self, interval=1):
        live_state = self.initial_state.copy()
        self._stored_states[0] = self.initial_state.copy()
        for ud_idx in tqdm.tqdm(
            range(0, len(self._diffs)), desc="Constructing result from diffs"
        ):
            step_no = ud_idx + 1
            live_state.batch_update(self._diffs[ud_idx])
            if step_no % interval == 0 and self._stored_states.get(step_no) is None:
                stored_state = live_state.copy()
                self._stored_states[step_no] = stored_state

    def get_step(self, step_no) -> SimulationState:
        """Retrieves the step at the provided number.

        Parameters
        ----------
        step_no : int
            The number of the step to return.

        Returns
        -------
        SimulationState
            The simulation state at the requested step.
        """

        stored = self._stored_states.get(step_no)
        if stored is not None:
            return stored
        else:
            state = self.initial_state.copy()
            for ud_idx in range(0, step_no):
                state.batch_update(self._diffs[ud_idx])
            return state

    def as_dict(self):
        return {
            "initial_state": self.initial_state.as_dict(),
            "diffs": self._diffs,
            "@module": self.__class__.__module__,
            "@class": self.__class__.__name__,
        }

    def to_file(self, fpath: str = None) -> None:
        """Serializes this result to the specified filepath.

        Parameters
        ----------
        fpath : str
            The filepath at which to save the serialized simulation result.
        """
        if fpath is None:
            now = datetime.datetime.now()
            date_string = now.strftime("%m-%d-%Y-%H-%M")
            fpath = f"{date_string}.json"

        dumpfn(self, fpath)
        return fpath

__init__(starting_state)

Initializes a SimulationResult with the specified starting_state.

Parameters
SimulationState

The state with which the simulation started.

Source code in pylattica/core/simulation_result.py
32
33
34
35
36
37
38
39
40
41
42
def __init__(self, starting_state: SimulationState):
    """Initializes a SimulationResult with the specified starting_state.

    Parameters
    ----------
    starting_state : SimulationState
        The state with which the simulation started.
    """
    self.initial_state = starting_state
    self._diffs: list[dict] = []
    self._stored_states = {}

add_step(updates)

Takes a set of updates as a dictionary mapping site IDs to the new values for various state parameters. For instance, if at the new step, my_state_attribute at site 23 changed to 12, updates would look like this:

{ 23: { "my_state_attribute": 12 } }

Parameters
dict

The changes associated with a new simulation step.

Source code in pylattica/core/simulation_result.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def add_step(self, updates: Dict[int, Dict]) -> None:
    """Takes a set of updates as a dictionary mapping site IDs
    to the new values for various state parameters. For instance, if at the
    new step, my_state_attribute at site 23 changed to 12, updates would look
    like this:

    {
        23: {
            "my_state_attribute": 12
        }
    }

    Parameters
    ----------
    updates : dict
        The changes associated with a new simulation step.
    """
    self._diffs.append(updates)

get_step(step_no)

Retrieves the step at the provided number.

Parameters
int

The number of the step to return.

Returns

SimulationState The simulation state at the requested step.

Source code in pylattica/core/simulation_result.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def get_step(self, step_no) -> SimulationState:
    """Retrieves the step at the provided number.

    Parameters
    ----------
    step_no : int
        The number of the step to return.

    Returns
    -------
    SimulationState
        The simulation state at the requested step.
    """

    stored = self._stored_states.get(step_no)
    if stored is not None:
        return stored
    else:
        state = self.initial_state.copy()
        for ud_idx in range(0, step_no):
            state.batch_update(self._diffs[ud_idx])
        return state

last_step() property

The last step of the simulation.

Returns

SimulationState The last step of the simulation

Source code in pylattica/core/simulation_result.py
79
80
81
82
83
84
85
86
87
88
@property
def last_step(self) -> SimulationState:
    """The last step of the simulation.

    Returns
    -------
    SimulationState
        The last step of the simulation
    """
    return self.get_step(len(self) - 1)

steps()

Returns a list of all the steps from this simulation.

Returns

List[SimulationState] The list of steps

Source code in pylattica/core/simulation_result.py
66
67
68
69
70
71
72
73
74
75
76
77
def steps(self) -> List[SimulationState]:
    """Returns a list of all the steps from this simulation.

    Returns
    -------
    List[SimulationState]
        The list of steps
    """
    live_state = self.initial_state.copy()
    for diff in self._diffs:
        yield live_state
        live_state.batch_update(diff)

to_file(fpath=None)

Serializes this result to the specified filepath.

Parameters
str

The filepath at which to save the serialized simulation result.

Source code in pylattica/core/simulation_result.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def to_file(self, fpath: str = None) -> None:
    """Serializes this result to the specified filepath.

    Parameters
    ----------
    fpath : str
        The filepath at which to save the serialized simulation result.
    """
    if fpath is None:
        now = datetime.datetime.now()
        date_string = now.strftime("%m-%d-%Y-%H-%M")
        fpath = f"{date_string}.json"

    dumpfn(self, fpath)
    return fpath