Skip to content

Commit

Permalink
manually switching branch to new dev, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mstraut committed Aug 10, 2023
1 parent 3e02cf4 commit d06fbd9
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 87 deletions.
18 changes: 11 additions & 7 deletions src/progpy/ProgPyDataFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ def add_timestamp_row(self, time: float = None, data=None):
# def add_row

def get_progpy_dict(self):
return self.to_dict('records')[0]
if self.empty:
return []
else:
return self.to_dict('records')[0]

def add_row(self, row):
if isinstance(row, ProgPyDataFrame):
row = row.get_progpy_dict()
if self.empty and not row.empty:
self.loc[0] = row
elif not self.empty and not row.empty:
self.loc[len(self)] = row
if not row.empty:
if isinstance(row, ProgPyDataFrame):
row = row.get_progpy_dict()
elif self.empty:
self.loc[0] = row
elif not self.empty:
self.loc[len(self)] = row

# def __repr__(self) -> str:
# """
Expand Down
7 changes: 4 additions & 3 deletions src/progpy/prognostics_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,10 +1095,11 @@ def output(x):
# This check prevents double recording when the last state was a savepoint
update_all()

if not saved_outputs:
if not saved_outputs.empty:
# saved_outputs is empty, so it wasn't calculated in simulation - used cached result
saved_outputs = LazySimResult(self.__output, saved_times, saved_states)
saved_event_states = LazySimResult(self.event_state, saved_times, saved_states)
# Miryam Bookmark
saved_outputs = LazySimResult(self.__output, saved_times, saved_states.get_progpy_dict())
saved_event_states = LazySimResult(self.event_state, saved_times, saved_states.get_progpy_dict())
else:
saved_outputs = SimResult(saved_times, saved_outputs, _copy=False)
saved_event_states = SimResult(saved_times, saved_event_states, _copy=False)
Expand Down
5 changes: 3 additions & 2 deletions src/progpy/state_estimators/particle_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from scipy.stats import norm
from warnings import warn

from progpy.utils.containers import DictLikeMatrixWrapper
from progpy.ProgPyDataFrame import ProgPyDataFrame

from . import state_estimator
from ..uncertain_data import UnweightedSamples, ScalarData, UncertainData
Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, model, x0, **kwargs):
self._measure = model.output

# Build array inplace
if isinstance(x0, DictLikeMatrixWrapper) or isinstance(x0, dict):
if isinstance(x0, ProgPyDataFrame) or isinstance(x0, dict):
x0 = ScalarData(x0)
elif not isinstance(x0, UncertainData):
raise TypeError(f"x0 must be of type UncertainData or StateContainer, was {type(x0)}.")
Expand Down Expand Up @@ -190,6 +190,7 @@ def estimate(self, t : float, u, z, dt = None):
for state in self.particles.keys()]

# Particles as a dictionary
# Miryam BookMark
self.particles = self.model.StateContainer(array(samples))

@property
Expand Down
4 changes: 2 additions & 2 deletions src/progpy/uncertain_data/uncertain_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ..utils.table import print_table_recursive
from ..visualize import plot_scatter, plot_hist
from progpy.utils.containers import DictLikeMatrixWrapper
from progpy.ProgPyDataFrame import ProgPyDataFrame


class UncertainData(ABC):
Expand Down Expand Up @@ -95,7 +95,7 @@ def relative_accuracy(self, ground_truth: dict) -> dict:
.. [0] Prognostics: The Science of Making Predictions (Goebel et al, 239)
"""
# if this check isn't here, goes to divide by zero check and raises AttributeError instead of TypeError. Keep? There are unittests checking for type
if not (isinstance(ground_truth, dict) or isinstance(ground_truth, DictLikeMatrixWrapper)):
if not (isinstance(ground_truth, dict) or isinstance(ground_truth, ProgPyDataFrame)):
raise TypeError("Ground truth must be passed as a dictionary or *.container argument.")
if not all(ground_truth.values()):
raise ZeroDivisionError("Ground truth values must be non-zero in calculating relative accuracy.")
Expand Down
4 changes: 2 additions & 2 deletions src/progpy/uncertain_data/unweighted_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from numpy import array, cov, random
from warnings import warn

from progpy.utils.containers import DictLikeMatrixWrapper
from progpy.ProgPyDataFrame import ProgPyDataFrame

from . import UncertainData

Expand All @@ -22,7 +22,7 @@ class UnweightedSamples(UncertainData, UserList):
"""
def __init__(self, samples: list = [], _type=dict):
super().__init__(_type)
if isinstance(samples, dict) or isinstance(samples, DictLikeMatrixWrapper):
if isinstance(samples, dict) or isinstance(samples, ProgPyDataFrame):
# Is in form of {key: [value, ...], ...}
# Convert to array of samples
if len(samples.keys()) == 0:
Expand Down
6 changes: 3 additions & 3 deletions src/progpy/utils/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __setitem__(self, key: str, value: float, _copy: bool = False) -> None:
if 'process_noise_dist' in self and self['process_noise_dist'].lower() not in process_noise_functions:
raise ValueError(f"Unsupported process noise distribution {self['process_noise_dist']}")

if all(value == 0 for value in self['process_noise'].values()):
if all(value == 0 for value in self['process_noise'].values[0]):
# No noise, use none function
fcn = process_noise_functions['none']
self._m.apply_process_noise = types.MethodType(fcn, self._m)
Expand All @@ -159,7 +159,7 @@ def __setitem__(self, key: str, value: float, _copy: bool = False) -> None:
else:
# Process noise is single number - convert to dict
if isinstance(self['measurement_noise'], Number):
self['measurement_noise'] = self._m.OutputContainer({key: self['measurement_noise'] for key in self._m.outputs})
self['measurement_noise'] = self._m.OutputContainer([{key: self['measurement_noise'] for key in self._m.outputs}])
elif isinstance(self['measurement_noise'], dict):
noise = self['measurement_noise']
for key in self._m.outputs:
Expand All @@ -172,7 +172,7 @@ def __setitem__(self, key: str, value: float, _copy: bool = False) -> None:
if 'measurement_noise_dist' in self and self['measurement_noise_dist'].lower() not in measurement_noise_functions:
raise ValueError(f"Unsupported measurement noise distribution {self['measurement_noise_dist']}")

if all(value == 0 for value in self['measurement_noise'].values()):
if all(value == 0 for value in self['measurement_noise'].values[0]):
# No noise, use none function
fcn = measurement_noise_functions['none']
self._m.apply_measurement_noise = types.MethodType(fcn, self._m)
Expand Down
10 changes: 5 additions & 5 deletions src/progpy/utils/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import numpy as np

from progpy.utils.containers import DictLikeMatrixWrapper
from progpy.ProgPyDataFrame import ProgPyDataFrame

__all__ = ['CustomEncoder', 'custom_decoder']

Expand All @@ -16,9 +16,9 @@ class CustomEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, np.ndarray):
return {'_original_type': 'ndarray', '_data': o.tolist()}
elif isinstance(o, DictLikeMatrixWrapper):
elif isinstance(o, ProgPyDataFrame):
dict_temp = {k: v for k, v in o.items()}
dict_temp['_original_type'] = 'DictLikeMatrixWrapper'
dict_temp['_original_type'] = 'ProgPyDataFrame'
return dict_temp
elif isinstance(o, np.bool_):
return bool(o)
Expand All @@ -39,9 +39,9 @@ def custom_decoder(o):
if isinstance(o, dict) and '_original_type' in o.keys():
if o['_original_type'] == 'ndarray':
return np.array(o['_data'])
elif o['_original_type'] == 'DictLikeMatrixWrapper':
elif o['_original_type'] == 'ProgPyDataFrame':
del o['_original_type']
return DictLikeMatrixWrapper(list(o.keys()), o)
return ProgPyDataFrame(list(o.keys()), o)
elif o['_original_type'] == 'pickled':
import pickle
from base64 import b64decode
Expand Down
5 changes: 3 additions & 2 deletions src/progpy/utils/size.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np

from progpy.sim_result import SimResult
from progpy.utils.containers import DictLikeMatrixWrapper
from progpy.ProgPyDataFrame import ProgPyDataFrame


def dict_handler(d):
Expand All @@ -20,13 +20,14 @@ def object_handler(o):

# Set of handlers that describe how to estimate the size of the payload of an
# object of a given type in format {type: handler}
# Miryam Bookmark
all_handlers = {tuple: iter,
list: iter,
np.ndarray: lambda a: iter(list(a.flat)),
dict: dict_handler,
set: iter,
frozenset: iter,
DictLikeMatrixWrapper: dict_handler,
ProgPyDataFrame: dict_handler,
SimResult: iter
}

Expand Down
122 changes: 61 additions & 61 deletions tests/benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,74 +11,74 @@
if __name__ == "__main__":
print('| Test | Time (s) |\n| --- | --- |')

print(FORMAT_STR.format('import main'), end='')
t = timeit.timeit('import progpy', timer=process_time)
print(f'{t} |')

print(FORMAT_STR.format('import thrown object'), end='')
t = timeit.timeit('from progpy.models import ThrownObject', timer=process_time)
print(f'{t} |')

print(FORMAT_STR.format('model initialization'), end='')
t = timeit.timeit('ThrownObject()', 'from progpy.models import ThrownObject', number=1000, timer=process_time)
print(f'{t} |')

m = ThrownObject()

print(FORMAT_STR.format('set noise'), end='')
t = timeit.timeit("m.parameters['process_dist'] = 'none'", 'from prog_models.models import ThrownObject; m = ThrownObject()', timer=process_time)
print(f'{t} |')
# print(FORMAT_STR.format('import main'), end='')
# t = timeit.timeit('import progpy', timer=process_time)
# print(f'{t} |')
#
# print(FORMAT_STR.format('import thrown object'), end='')
# t = timeit.timeit('from progpy.models import ThrownObject', timer=process_time)
# print(f'{t} |')
#
# print(FORMAT_STR.format('model initialization'), end='')
# t = timeit.timeit('ThrownObject()', 'from progpy.models import ThrownObject', number=1000, timer=process_time)
# print(f'{t} |')
#
# m = ThrownObject()
#
# print(FORMAT_STR.format('set noise'), end='')
# t = timeit.timeit("m.parameters['process_dist'] = 'none'", 'from progpy.models import ThrownObject; m = ThrownObject()', timer=process_time)
# print(f'{t} |')

print(FORMAT_STR.format('simulate'), end='')
t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact')", 'from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer([{}])', number=1000, timer=process_time)
print(f'{t} |')
# print(FORMAT_STR.format('simulate'), end='')
# t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact')", 'from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer([{}])', number=1000, timer=process_time)
# print(f'{t} |')

print(FORMAT_STR.format('simulate with saving'), end='')
t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5)", 'from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})', number=1000, timer=process_time)
print(f'{t} |')
# print(FORMAT_STR.format('simulate with saving'), end='')
# t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5)", 'from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})', number=1000, timer=process_time)
# print(f'{t} |')

print(FORMAT_STR.format('simulate with saving, dt'), end='')
t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1)", 'from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})', number=500, timer=process_time)
print(f'{t} |')
# print(FORMAT_STR.format('simulate with saving, dt'), end='')
# t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1)", 'from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})', number=500, timer=process_time)
# print(f'{t} |')

print(FORMAT_STR.format('simulate with printing results, dt'), end='')
temp_out = StringIO()
sys.stdout = temp_out
t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1, print = True)", 'from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})', number=500, timer=process_time)
t = timeit.timeit("m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1, print = True)", 'from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})', number=500, timer=process_time)
sys.stdout = sys.__stdout__
print(f'{t} |')

# print(FORMAT_STR.format('Plot results'), end='')
# t = timeit.timeit("result.outputs.plot()", "from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({}); result = m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1)", number=1000, timer=process_time)
#
# # print(FORMAT_STR.format('Plot results'), end='')
# # t = timeit.timeit("result.outputs.plot()", "from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({}); result = m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1)", number=1000, timer=process_time)
# # print(f'{t} |')
#
# print(FORMAT_STR.format('Metrics'), end='')
# t = timeit.timeit("result.event_states.monotonicity()", "from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({}); result = m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1)", number=1000, timer=process_time)
# print(f'{t} |')
#
# print(FORMAT_STR.format('Surrogate Model Generation'), end='')
# temp_out = StringIO()
# sys.stdout = temp_out
# sys.stderr = temp_out
# t = timeit.timeit("m.generate_surrogate([future_load], threshold_keys='impact')", "from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})", number=1000, timer=process_time)
# sys.stdout = sys.__stdout__
# sys.stderr = sys.__stderr__
# print(f'{t} |')
#
# print(FORMAT_STR.format('surrogate sim'), end='')
# temp_out = StringIO()
# sys.stdout = temp_out
# sys.stderr = temp_out
# t = timeit.timeit("m2.simulate_to_threshold(future_load, threshold_keys='impact')", "from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer([{}]); m2 = m.generate_surrogate([future_load], threshold_keys='impact')", number=1000, timer=process_time)
# sys.stdout = sys.__stdout__
# sys.stderr = sys.__stderr__
# print(f'{t} |')
#
# print(FORMAT_STR.format('surrogate sim, dt'), end='')
# temp_out = StringIO()
# sys.stdout = temp_out
# sys.stderr = temp_out
# t = timeit.timeit("m2.simulate_to_threshold(future_load, threshold_keys='impact', save_freq=0.25)", "from progpy.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({}); m2 = m.generate_surrogate([future_load], threshold_keys='impact')", number=1000, timer=process_time)
# sys.stdout = sys.__stdout__
# sys.stderr = sys.__stderr__
# print(f'{t} |')

print(FORMAT_STR.format('Metrics'), end='')
t = timeit.timeit("result.event_states.monotonicity()", "from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({}); result = m.simulate_to_threshold(future_load, threshold_keys='impact', save_freq = 0.5, dt=0.1)", number=1000, timer=process_time)
print(f'{t} |')

print(FORMAT_STR.format('Surrogate Model Generation'), end='')
temp_out = StringIO()
sys.stdout = temp_out
sys.stderr = temp_out
t = timeit.timeit("m.generate_surrogate([future_load], threshold_keys='impact')", "from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({})", number=1000, timer=process_time)
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
print(f'{t} |')

print(FORMAT_STR.format('surrogate sim'), end='')
temp_out = StringIO()
sys.stdout = temp_out
sys.stderr = temp_out
t = timeit.timeit("m2.simulate_to_threshold(future_load, threshold_keys='impact')", "from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer([{}]); m2 = m.generate_surrogate([future_load], threshold_keys='impact')", number=1000, timer=process_time)
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
print(f'{t} |')

print(FORMAT_STR.format('surrogate sim, dt'), end='')
temp_out = StringIO()
sys.stdout = temp_out
sys.stderr = temp_out
t = timeit.timeit("m2.simulate_to_threshold(future_load, threshold_keys='impact', save_freq=0.25)", "from prog_models.models import ThrownObject; m = ThrownObject(); future_load = lambda t, x=None : m.InputContainer({}); m2 = m.generate_surrogate([future_load], threshold_keys='impact')", number=1000, timer=process_time)
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
print(f'{t} |')

0 comments on commit d06fbd9

Please sign in to comment.