Records state variables of interest.
| 28 | |
| 29 | |
| 30 | class Monitor(AbstractMonitor): |
| 31 | # language=rst |
| 32 | """ |
| 33 | Records state variables of interest. |
| 34 | """ |
| 35 | |
| 36 | def __init__( |
| 37 | self, |
| 38 | obj: Union[ |
| 39 | Nodes, |
| 40 | AbstractConnection, |
| 41 | AbstractMulticompartmentConnection, |
| 42 | AbstractFeature, |
| 43 | ], |
| 44 | state_vars: Iterable[str], |
| 45 | time: Optional[int] = None, |
| 46 | batch_size: int = 1, |
| 47 | device: str = "cpu", |
| 48 | sparse: Optional[bool] = False, |
| 49 | ): |
| 50 | # language=rst |
| 51 | """ |
| 52 | Constructs a ``Monitor`` object. |
| 53 | |
| 54 | :param obj: An object to record state variables from during network simulation. |
| 55 | :param state_vars: Iterable of strings indicating names of state variables to record. |
| 56 | :param time: If not ``None``, pre-allocate memory for state variable recording. |
| 57 | :param device: Allow the monitor to be on different device separate from Network device |
| 58 | """ |
| 59 | super().__init__() |
| 60 | |
| 61 | self.obj = obj |
| 62 | self.state_vars = state_vars |
| 63 | self.time = time |
| 64 | self.batch_size = batch_size |
| 65 | self.device = device |
| 66 | self.sparse = sparse |
| 67 | |
| 68 | # if time is not specified the monitor variable accumulate the logs |
| 69 | if self.time is None: |
| 70 | self.device = "cpu" |
| 71 | |
| 72 | self.recording = [] |
| 73 | self.reset_state_variables() |
| 74 | |
| 75 | def get(self, var: str) -> torch.Tensor: |
| 76 | # language=rst |
| 77 | """ |
| 78 | Return recording to user. |
| 79 | |
| 80 | :param var: State variable recording to return. |
| 81 | :return: Tensor of shape ``[time, n_1, ..., n_k]``, where ``[n_1, ..., n_k]`` is the shape of the recorded state |
| 82 | variable. |
| 83 | Note, if time == `None`, get return the logs and empty the monitor variable |
| 84 | |
| 85 | """ |
| 86 | if self.clean: |
| 87 | return_logs = torch.empty(0, device=self.device) |
no outgoing calls