Comments (4)
Hi Yalin,
Whether 5, 10, or 1000 evaluations failed may or may not be of concern, but flexibility and control in how errors are handled would be nice (agreed!).
Thanks for your suggestions. Here are a few of my thoughts:
- I have tried error logs before and they were not terribly useful... if the traceback is not included, its hard to discern much about the error. It's also odd to go back with a scenario index and think about why did that scenario failed using the error log.
- I think a
kwarg
to decide whether the system is interrupted by errors may be useful, but I would like to take this a step further. Perhaps the user could decide what to do given information about the scenario and the exception.
Here is my solution. An exception_hook
attribute that decides what to do when an evaluation fails. The exception hook is a function called after a failed evaluation. The exception hook should return either None or metric values given the model, exception and sample.
Here are a couple of use cases:
import biosteam as bst
import pytest
from biorefineries import lipidcane as lc
from chaospy import distributions as shape
from warnings import simplefilter
import numpy as np
simplefilter("ignore")
IRR_metric = bst.Metric('Internal rate of return', lc.lipidcane_tea.solve_IRR)
metrics = [IRR_metric]
lipidcane_model = bst.Model(lc.lipidcane_sys, metrics)
baseline = lc.lipidcane.F_mass
distribution = shape.Triangle(-baseline , baseline , 2*baseline ) # Negative value should fail
@lipidcane_model.parameter(element=lc.lipidcane, distribution=distribution, units='kg/hr')
def set_lipidcane_flow_rate(flow_rate):
lc.lipidcane.F_mass = flow_rate
samples = lipidcane_model.sample(15, 'L')
lipidcane_model.load_samples(samples)
# Without an exception hook, the same behavior will result (NaN values for failed evaluations)
lipidcane_model.evaluate()
assert np.isnan(lipidcane_model.table.values).any()
InfeasibleRegion = bst.exceptions.InfeasibleRegion
# This will provide a more understandable IRR result for infeasible regions
def exception_hook(exception, sample):
if isinstance(exception, InfeasibleRegion):
return [0]
lipidcane_model.exception_hook = exception_hook
lipidcane_model.evaluate()
assert not np.isnan(lipidcane_model.table.values).any()
# This will raise an exception due to negative flow rates
def exception_hook(exception, sample):
if isinstance(exception, InfeasibleRegion):
raise exception
lipidcane_model.exception_hook = exception_hook
with pytest.raises(InfeasibleRegion): lipidcane_model.evaluate()
# This will raise an exception regardless
def exception_hook(exception, sample):
raise exception
lipidcane_model.exception_hook = exception_hook
with pytest.raises(InfeasibleRegion): lipidcane_model.evaluate()
# Here is another cool thing we could do in the case where
# some metrics are expected to fail
bad_metric = bst.Metric('bad metric', lambda: 1/0)
lipidcane_model.metrics = (IRR_metric, bad_metric)
lipidcane_model.load_samples(samples) # Metrics changed, so need to reload sample
def exception_hook(exception, sample):
if not isinstance(exception, ZeroDivisionError): return
lc.lipidcane_sys.simulate()
values = []
for i in lipidcane_model.metrics:
try: x = i()
except: x = None
values.append(x)
return values
lipidcane_model.exception_hook = exception_hook
lipidcane_model.evaluate()
bad_metric_results = lipidcane_model.table[bad_metric.index]
IRR_metric_results = lipidcane_model.table[IRR_metric.index]
assert np.isnan(bad_metric_results).all()
assert not np.isnan(IRR_metric_results).all()
# Note that the possibilities are infinite here...
I went ahead and already added this new functionality in BioSTEAM. This is actually the test for the model exception hook in BioSTEAM.
Hope this helps,
Thanks!
from biosteam.
Oh the exception_hook
is cool! It's not that straightforward but has much more flexibility. I'll play around with it 😃 Thanks as always!
from biosteam.
@yalinli2, I just removed the model argument from the exception_hook for simplification (sorry if you have to git pull again)
from biosteam.
Oh the
exception_hook
is cool! It's not that straightforward but has much more flexibility. I'll play around with it 😃 Thanks as always!
Thanks! Yeah, the use cases I made up are not straight forward, but the hook itself shouldn't be too hard to use... it just takes in two parameters now, the exception and the sample at which it occurred.
from biosteam.
Related Issues (20)
- High CAPEX cost at low pumping rates HOT 4
- Small error in `BoilerTurbogenerator` design cause negative work/cost HOT 2
- Negative water flows in permeate of AnMBR in high-rate WWT system HOT 4
- Notice: Moving to Python 3.11 in October
- AttributeError: 'Stream' object has no attribute 'indices' HOT 1
- Kinetic model does not take 't' into account HOT 1
- Unable to get graphic data like Diagrams and graphs HOT 1
- numba inlines biosteam.CE value into compiled code HOT 4
- Better batch design HOT 3
- Power consumption default in AeratedBioReactor needs more detail HOT 2
- Biogas upgrading HOT 1
- Depreciable capital in NPV calculation HOT 24
- Valve node has no label
- Unit z_mol_in and z_mol_out properties don't work
- Compressor type and adding Cooling Agent HOT 15
- Implement Blower as a Compressor type
- Include special-case utilities in utility worksheet.
- Sub class modelling of CSTR HOT 1
- Reactor duty for sub class modelling of CSTR
- Minor issues with high-rate WWT HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from biosteam.