Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 80 additions & 2 deletions src/prog_models/sim_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SimResult(UserList):

__slots__ = ['times', 'data'] # Optimization

def __init__(self, times, data):
def __init__(self, times = [], data = []):
"""
Args:
times (array(float)): Times for each data point where times[n] corresponds to data[n]
Expand All @@ -30,6 +30,48 @@ def __eq__(self, other):
"""
return self.times == other.times and self.data == other.data

def index(self, other, *args, **kwargs):
"""
Get the index of the first sample where other occurs

Args:
other (dict)

Returns:
int: Index of first sample where other occurs
"""
return self.data.index(other, *args, **kwargs)

def extend(self, other):
self.times.extend(other.times)
self.data.extend(other.data)

def pop(self, index = -1):
"""Remove and return an element

Args:
index (int, optional): Index of element to be removed. Defaults to -1.

Returns:
dict: Element Removed
"""
self.times.remove(index)
return self.data.pop(index)

def remove(self, index):
"""Remove an element

Args:
index (int): Index of element to be removed.
"""
self.times.remove(index)
self.data.remove(index)

def clear(self):
"""Clear the SimResult"""
self.times = []
self.data = []

def time(self, index):
"""Get time for data point at index `index`

Expand All @@ -52,13 +94,24 @@ def plot(self, **kwargs):
Figure
"""
return plot_timeseries(self.times, self.data, options=kwargs)

def __not_implemented(self):
raise NotImplementedError("Not Implemented")

# Functions of list not implemented
# Specified here to stop users from accidentally trying to use them (due to this classes similarity to list)
append = __not_implemented
count = __not_implemented
insert = __not_implemented
reverse = __not_implemented
# lgtm [py/missing-equals]


class LazySimResult(SimResult): # lgtm [py/missing-equals]
"""
Used to store the result of a simulation, which is only calculated on first request
"""
def __init__(self, fcn, times, states):
def __init__(self, fcn, times = [], states = []):
"""
Args:
fcn (callable): function (x) -> z where x is the state and z is the data
Expand All @@ -77,6 +130,31 @@ def is_cached(self):
"""
return self.__data is not None

def clear(self):
self.times = []
self.__data = None
self.states = []

def extend(self, other):
self.times.extend(other.times)
self.__data = None
self.states.extend(other.states)

def pop(self, index = -1):
"""Remove an element. If data hasn't been cached, remove the state - so it wont be calculated

Args:
index (int, optional): Index of element to be removed. Defaults to -1.

Returns:
dict: Element Removed
"""
self.times.pop(index)
x = self.states.pop(index)
if self.__data is not None:
return self.__data.pop(index)
return self.fcn(x)

@property
def data(self):
"""
Expand Down