Giter Club home page Giter Club logo

Comments (4)

yoelcortes avatar yoelcortes commented on July 25, 2024 1

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:

  1. 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.
  2. 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.

yalinli2 avatar yalinli2 commented on July 25, 2024

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.

yoelcortes avatar yoelcortes commented on July 25, 2024

@yalinli2, I just removed the model argument from the exception_hook for simplification (sorry if you have to git pull again)

from biosteam.

yoelcortes avatar yoelcortes commented on July 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.