Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to include u in the output field #142

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def estimate(self, t, u, z):
x2 = {key : float(value) + 10*(random.random()-0.5) for (key,value) in self.state.items()}

# Calculate outputs
z_est = self.m.output(t, self.state)
z_est2 = self.m.output(t, x2)
z_est = self.m.output(t)
z_est2 = self.m.output(t)

# Now score them each by how close they are to the measured z
z_est_score = sum([abs(z_est[key] - z[key]) for key in self.m.outputs])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def future_loading(t, x=None):
})

# Step 3: Sim
first_output = pump.output(pump.initialize(future_loading(0),{}))
first_output = pump.output(pump.initialize(future_loading(0), {}))
config = {
'horizon': 1e5,
'save_freq': 1e3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def estimate(self, t, u, z):
x2 = {key : float(value) + 10*(random.random()-0.5) for (key,value) in self.state.items()}

# Calculate outputs
z_est = self.m.output(t, self.state)
z_est2 = self.m.output(t, x2)
z_est = self.m.output(t)
z_est2 = self.m.output(t)

# Now score them each by how close they are to the measured z
z_est_score = sum([abs(z_est[key] - z[key]) for key in self.m.outputs])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class ProgModelTemplate(PrognosticsModel):
#
# return self.StateContainer(next_x)

def output(self, x):
def output(self, x, u=None):
"""
Calculate output, z (i.e., measurable values) given the state x

Expand All @@ -238,6 +238,7 @@ def output(self, x):
z : OutputContainer
Outputs, with keys defined by model.outputs.
e.g., z = {'t':12.4, 'v':3.3} given inputs = ['t', 'v']
:param u:
"""

# REPLACE BELOW WITH LOGIC TO CALCULATE OUTPUTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def future_loading(t, x = None):
class MyBattery(Battery):
outputs = ['tv'] # output is temperature * voltage (for some reason)

def output(self, x):
def output(self, x, u=None):
parent.parameters = self.parameters # only needed if you expect to change parameters
z = parent.output(x)
return self.OutputContainer({'tv': z['v'] * z['t']})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def future_loading(t, x = None):
class MyBattery(Battery):
outputs = ['tv'] # output is temperature * voltage (for some reason)

def output(self, x):
def output(self, x, u=None):
parent.parameters = self.parameters # only needed if you expect to change parameters
z = parent.output(x)
return self.OutputContainer({'tv': z['v'] * z['t']})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def dx(self, x, u):
return self.StateContainer({'x': x['v'],
'v': self.parameters['g']}) # Acceleration of gravity

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer({'x': x['x']})

# This is actually optional. Leaving thresholds_met empty will use the event state to define thresholds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def future_loading(t, x=None):
})

# Step 3: Sim
first_output = pump.output(pump.initialize(future_loading(0),{}))
first_output = pump.output(pump.initialize(future_loading(0), {}))
config = {
'horizon': 1e5,
'save_freq': 1e3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def future_loading(t, x=None):
})

# Step 3: Sim
first_output = pump.output(pump.initialize(future_loading(0),{}))
first_output = pump.output(pump.initialize(future_loading(0), {}))
config = {
'horizon': 1e5,
'save_freq': 1e3,
Expand Down
2 changes: 1 addition & 1 deletion examples/measurement_eqn_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def future_loading(t, x = None):
class MyBattery(Battery):
outputs = ['tv'] # output is temperature * voltage (for some reason)

def output(self, x):
def output(self, x, u=None):
parent.parameters = self.parameters # only needed if you expect to change parameters
z = parent.output(x)
return self.OutputContainer({'tv': z['v'] * z['t']})
Expand Down
2 changes: 1 addition & 1 deletion examples/new_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def dx(self, x, u):
return self.StateContainer({'x': x['v'],
'v': self.parameters['g']}) # Acceleration of gravity

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer({'x': x['x']})

# This is actually optional. Leaving thresholds_met empty will use the event state to define thresholds.
Expand Down
4 changes: 2 additions & 2 deletions examples/new_state_estimator_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def estimate(self, t, u, z):
x2 = {key : float(value) + 10*(random.random()-0.5) for (key,value) in self.state.items()}

# Calculate outputs
z_est = self.m.output(t, self.state)
z_est2 = self.m.output(t, x2)
z_est = self.m.output(t)
z_est2 = self.m.output(t)

# Now score them each by how close they are to the measured z
z_est_score = sum([abs(z_est[key] - z[key]) for key in self.m.outputs])
Expand Down
2 changes: 1 addition & 1 deletion examples/sim_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def future_loading(t, x=None):
})

# Step 3: Sim
first_output = pump.output(pump.initialize(future_loading(0),{}))
first_output = pump.output(pump.initialize(future_loading(0), {}))
config = {
'horizon': 1e5,
'save_freq': 1e3,
Expand Down
3 changes: 2 additions & 1 deletion prog_model_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class ProgModelTemplate(PrognosticsModel):
#
# return self.StateContainer(next_x)

def output(self, x):
def output(self, x, u=None):
"""
Calculate output, z (i.e., measurable values) given the state x

Expand All @@ -238,6 +238,7 @@ def output(self, x):
z : OutputContainer
Outputs, with keys defined by model.outputs.
e.g., z = {'t':12.4, 'v':3.3} given inputs = ['t', 'v']
:param u:
"""

# REPLACE BELOW WITH LOGIC TO CALCULATE OUTPUTS
Expand Down
2 changes: 1 addition & 1 deletion sphinx-config/auto_examples/sim_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def future_loading(t, x=None):
})

# Step 3: Sim
first_output = pump.output(pump.initialize(future_loading(0),{}))
first_output = pump.output(pump.initialize(future_loading(0), {}))
config = {
'horizon': 1e5,
'save_freq': 1e3,
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/composite_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def next_state(self, x, u, dt):

return x

def output(self, x):
def output(self, x, u=None):
z = {}
for (name, m) in self.parameters['models']:
# Prepare state
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/data_models/lstm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def next_state(self, x, u, _):
result = np.array(np.vstack((states, internal_states)), dtype=np.float64)
return self.StateContainer(result)

def output(self, x):
def output(self, x, u=None):
if x.matrix[0, 0] is None:
warn(f"Output estimation is not available until at least {1+self.parameters['window']} timesteps have passed.")
return self.OutputContainer(np.array([[None] for _ in self.outputs]))
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/ensemble_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def next_state(self, x, u, dt):

return self.StateContainer(xs_final)

def output(self, x):
def output(self, x, u=None):
zs = [m.output(m.StateContainer(x)) for m in self.parameters['models']]
zs_final = {}
for z in zs:
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def dx(self, x, u):
dx_array += np.matmul(self.B, u.matrix)
return self.StateContainer(dx_array)

def output(self, x):
def output(self, x, u=None):
z_array = np.matmul(self.C, x.matrix) + self.D
return self.OutputContainer(z_array)

Expand Down
2 changes: 1 addition & 1 deletion src/progpy/mixture_of_experts.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def best_model(self, x, _excepting=[]):
best_index = i
return self.parameters['models'][best_index]

def output(self, x):
def output(self, x, u=None):
excepting = []
outputs_seen = set()
z = {}
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/aircraft_model/small_rotorcraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def event_state(self, x) -> dict:
# Based on percentage of reference trajectory completed
return {'TrajectoryComplete': x['mission_complete']}

def output(self, x):
def output(self, x, u=None):
# Output is the same as the state vector, without time and mission_complete
return self.OutputContainer(x.matrix[0:-2])

Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/battery_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def event_state(self, x) -> dict:
'EOD': np.minimum(charge_EOD, voltage_EOD)
}

def output(self, x):
def output(self, x, u=None):
parameters = self.parameters
Vcs = x['qcs']/parameters['Cs']
Vcp = x['qcp']/parameters['Ccp']
Expand Down
6 changes: 3 additions & 3 deletions src/progpy/models/battery_electrochem.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def event_state(self, x) -> dict:
'EOD': np.clip(min(charge_EOD, voltage_EOD), 0, 1)
}

def output(self, x):
def output(self, x, u=None):
params = self.parameters
An = params['An']
# Negative Surface
Expand Down Expand Up @@ -628,7 +628,7 @@ def event_state(self, x) -> dict:
def threshold_met(self, x) -> dict:
return {'InsufficientCapacity': x['qMax'] < self.parameters['qMaxThreshold']}

def output(self, _):
def output(self, x, u=None):
return self.OutputContainer(np.array([]))

def merge_dicts(a: dict, b: dict) -> None:
Expand Down Expand Up @@ -709,7 +709,7 @@ def dx(self, x, u):
x_dot.matrix = np.vstack((x_dot.matrix, x_dot2.matrix))
return x_dot

def output(self, x):
def output(self, x, u=None):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.parameters['qMobile'] = x['qMax']
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/centrifugal_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def next_state(self, x, u, dt: float):

return self.StateContainer(state_array)

def output(self, x):
def output(self, x, u=None):
Qout = np.maximum(0, x['Q']-x['QLeak'])

return self.OutputContainer({
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/dcmotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,5 @@ def next_state(self, x, u, dt: float):
x.matrix[4] %= PI2 # Wrap angle
return x

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer(x.matrix[3:])
2 changes: 1 addition & 1 deletion src/progpy/models/dcmotor_singlephase.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def dx(self, x, u):
np.atleast_1d(dvrotdt) # rotor speed
]))

def output(self, x):
def output(self, x, u=None):
rotor_speed = x['v_rot']
return self.OutputContainer(np.array([
np.atleast_1d(rotor_speed),
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/esc.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ def next_state(self, x, u, dt: float):
np.atleast_1d(VP[2]),
np.atleast_1d(x['t'] + dt)]))

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer(x)
2 changes: 1 addition & 1 deletion src/progpy/models/experimental/paris_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def dx(self, x, u):
dxdt = {'c_l': r}
return self.StateContainer(dxdt)

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer(x)

def event_state(self, x) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/pneumatic_valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def next_state(self, x, u, dt: float):
np.atleast_1d(dp) # pL - pR
]))

def output(self, x):
def output(self, x, u=None):
params = self.parameters # Optimization
indicatorTopm = (x['x'] >= params['Ls']-params['indicatorTol'])
indicatorBotm = (x['x'] <= params['indicatorTol'])
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/powertrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def next_state(self, x, u, dt: float):

return self.StateContainer(x_esc)

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer(
{
'v_rot': x['v_rot'],
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/propeller_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ def next_state(self, x, u, dt: float):
return self.StateContainer({
't_l': self.parameters['C_q']*u['v_rot']**2})

def output(self, x):
def output(self, x, u=None):
return x
4 changes: 2 additions & 2 deletions src/progpy/models/test_models/other_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def dx(self, x, u):
'x0': self.parameters['a'] * u['u0']
})

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer({
'x0+b': x['x0'] + self.parameters['b'],
'x0+c': x['x0'] + self.parameters['c']
Expand Down Expand Up @@ -64,7 +64,7 @@ def dx(self, x, u):
'x0': self.parameters['a'] * u['u0']
})

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer({
'x0+d': x['x0'] + self.parameters['d'],
'x0+c': x['x0'] + self.parameters['c']
Expand Down
2 changes: 1 addition & 1 deletion src/progpy/models/thrown_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def next_state(self, x, u, dt: float):
np.atleast_1d(next_v) # Acceleration of gravity
]))

def output(self, x):
def output(self, x, u=None):
return self.OutputContainer(np.array([[x['x']]]))

def threshold_met(self, x) -> dict:
Expand Down
5 changes: 4 additions & 1 deletion src/progpy/prognostics_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def performance_metrics(self, x) -> dict:

observables = performance_metrics # For backwards compatibility

def output(self, x):
def output(self, x, u=None):
"""
Calculate :term:`output` given state

Expand All @@ -471,6 +471,9 @@ def output(self, x):
x : StateContainer
state, with keys defined by model.states \n
e.g., x = m.StateContainer({'abc': 332.1, 'def': 221.003}) given states = ['abc', 'def']
u : InputContainer
Inputs, with keys defined by model.inputs \n
e.g., u = m.InputContainer({'i':3.2}) given inputs = ['i']

Returns
-------
Expand Down
Loading