Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into examples/serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kjjarvis committed Oct 11, 2023
2 parents b7aec4f + 8c5e4b8 commit d6263db
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 84 deletions.
224 changes: 224 additions & 0 deletions examples/04_New Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,230 @@
"## Custom Events"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the examples above, we have focused on the simple event of a thrown object hitting the ground or reaching `impact`. In this section, we highlight additional uses of ProgPy's generalizable concept of `events`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The term [events](https://nasa.github.io/progpy/prog_models_guide.html#events) is used to describe something to be predicted. Generally in the PHM community, these are referred to as End of Life (EOL). However, they can be much more. \n",
"\n",
"In ProgPy, events can be anything that needs to be predicted. Systems will often have multiple failure modes, and each of these modes can be represented by a separate event. Additionally, events can also be used to predict other events of interest other than failure, such as special system states or warning thresholds. Thus, `events` in ProgPy can represent End of Life (EOL), End of Mission (EOM), warning thresholds, or any Event of Interest (EOI). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a few components of the model that must be specified in order to define events:\n",
"\n",
"1. The `events` property defines the expected events \n",
"\n",
"2. The `threshold_met` method defines the conditions under which an event occurs \n",
"\n",
"3. The `event_state` method returns an estimate of progress towards the threshold \n",
"\n",
"Note that because of the interconnected relationship between `threshold_met` and `event_state`, it is only required to define one of these. However, it is generally beneficial to specify both. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To illustrate this concept, we will use the `BatteryElectroChemEOD` model (see section 03. Included Models). In the standard implementation of this model, the defined event is `EOD` or End of Discharge. This occurs when the voltage drops below a pre-defined threshold value. The State-of-Charge (SOC) of the battery is the event state for the EOD event. Recall that event states (and therefore SOC) vary between 0 and 1, where 1 is healthy and 0 signifies the event has occurred. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose we have the requirement that our battery must not fall below 5% State-of-Charge. This would correspond to an `EOD` event state of 0.05. Additionally, let's add events for two warning thresholds, a $\\text{\\textcolor{yellow}{yellow}}$ threshold at 15% SOC and a $\\text{\\textcolor{red}{red}}$ threshold at 10% SOC. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To define the model, we'll start with the necessary imports."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from progpy.loading import Piecewise\n",
"from progpy.models import BatteryElectroChemEOD"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, let's define our threshold values. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"YELLOW_THRESH = 0.15 # 15% SOC\n",
"RED_THRESH = 0.1 # 10% SOC\n",
"THRESHOLD = 0.05 # 5% SOC"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we'll create our model by subclassing from the `BatteryElectroChemEOD` model. First, we'll re-define `events` to include three new events for our two warnings and new threshold value, as well as the event `EOD` from the parent class."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class BattNewEvent(BatteryElectroChemEOD):\n",
" events = BatteryElectroChemEOD.events + ['EOD_warn_yellow', 'EOD_warn_red', 'EOD_requirement_threshold']\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we'll override the `event_state` method to additionally include calculations for progress towards each of our new events. We'll add yellow, red, and failure states by scaling the EOD state. We scale so that the threshold SOC is 0 at their associated events, while SOC of 1 is still 1. For example, for yellow, we want `EOD_warn_yellow` to be 1 when SOC is 1, and 0 when SOC is 0.15 or lower. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class BattNewEvent(BattNewEvent):\n",
" \n",
" def event_state(self, state):\n",
" # Get event state from parent\n",
" event_state = super().event_state(state)\n",
"\n",
" # Add yellow, red, and failure states by scaling EOD state\n",
" event_state['EOD_warn_yellow'] = (event_state['EOD']-YELLOW_THRESH)/(1-YELLOW_THRESH) \n",
" event_state['EOD_warn_red'] = (event_state['EOD']-RED_THRESH)/(1-RED_THRESH)\n",
" event_state['EOD_requirement_threshold'] = (event_state['EOD']-THRESHOLD)/(1-THRESHOLD)\n",
"\n",
" # Return\n",
" return event_state"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we'll override the `threshold_met` method to define when each event occurs. Based on the scaling in `event_state` each event is reached when the corresponding `event_state` value is less than or equal to 0. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class BattNewEvent(BattNewEvent):\n",
" def threshold_met(self, x):\n",
" # Get threshold met from parent\n",
" t_met = super().threshold_met(x)\n",
"\n",
" # Add yell and red states from event_state\n",
" event_state = self.event_state(x)\n",
" t_met['EOD_warn_yellow'] = event_state['EOD_warn_yellow'] <= 0\n",
" t_met['EOD_warn_red'] = event_state['EOD_warn_red'] <= 0\n",
" t_met['EOD_requirement_threshold'] = event_state['EOD_requirement_threshold'] <= 0\n",
"\n",
" return t_met"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this, we have defined the three key model components for defining new events. \n",
"\n",
"Let's test out the model. First, create an instance of it. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = BattNewEvent()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall that the battery model takes input of current. We will use a piecewise loading scheme (see 01. Simulation)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Variable (piecewise) future loading scheme\n",
"future_loading = Piecewise(\n",
" m.InputContainer,\n",
" [600, 900, 1800, 3000],\n",
" {'i': [2, 1, 4, 2, 3]})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can simulate to threshold and plot the results. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"simulated_results = m.simulate_to_threshold(future_loading, threshold_keys=['EOD'], print = True)\n",
"\n",
"simulated_results.event_states.plot()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we can see the SOC plotted for the different events throughout time. The yellow warning (15% SOC) reaches threshold first, followed by the red warning (10% SOC), new EOD threshold (5% SOC), and finally the original EOD value. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section, we have illustrated how to define custom [events](https://nasa.github.io/progpy/prog_models_guide.html#events) for prognostics models. Events can be used to define anything that a user is interested in predicting, including common values like Remaining Useful Life (RUL) and End of Discharge (EOD), as well as other values like special intermediate states or warning thresholds. "
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
84 changes: 0 additions & 84 deletions examples/events.py

This file was deleted.

0 comments on commit d6263db

Please sign in to comment.