brightway-lca / brightway2-calc

The calculation engine for the Brightway2 life cycle assessment framework.
BSD 3-Clause "New" or "Revised" License
14 stars 16 forks source link

Monte Carlo Bug #1

Closed aleksandra-kim closed 3 years ago

aleksandra-kim commented 9 years ago

Original report by Caroline Nadel (Bitbucket: cazcazn, ).


Following the tutorials. Everytime I try to do a Monte Carlo as documented, an exception ValueError: left > mode is thrown. This happens with both tutorials 2 and 3 so far.

Stack trace:

ValueError Traceback (most recent call last)

in () 2 mc.load_data() 3 for x in range(10): ----> 4 print mc.next() .../env/lib/python2.7/site-packages/bw2calc/monte_carlo.pyc in next(self) 64 if not hasattr(self, "tech_rng"): 65 raise NameError("Must run `load_data` before making calculations") ---> 66 self.rebuild_technosphere_matrix(self.tech_rng.next()) 67 self.rebuild_biosphere_matrix(self.bio_rng.next()) 68 if self.lcia: .../env/lib/python2.7/site-packages/stats_arrays/random.pyc in next(self) 181 1, 182 self.random, --> 183 self.maximum_iterations 184 ) 185 if len(random_data.shape) == 2: .../env/lib/python2.7/site-packages/stats_arrays/distributions/base.pyc in bounded_random_variables(cls, params, size, seeded_random, maximum_iterations) 314 maximum_iterations=None): 315 """No bounds checking because the bounds do not exclude any of the distribution.""" --> 316 return cls.random_variables(params, size, seeded_random) 317 318 @classmethod .../env/lib/python2.7/site-packages/stats_arrays/distributions/geometric.pyc in random_variables(cls, params, size, seeded_random) 68 params['loc'], # Mode 69 params['maximum'], # Right ---> 70 size=(size, params.shape[0])).T 71 72 @classmethod mtrand.pyx in mtrand.RandomState.triangular (numpy/random/mtrand/mtrand.c:19950)() ValueError: left > mode
aleksandra-kim commented 9 years ago

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


Thanks for the bug report!

It looks like this parameter distribution is incorrect. In the triangular distribution, the "left" (minimum) must be less than or equal to the mode, which must be less or equal to the maximum ("right"). See https://en.wikipedia.org/wiki/Triangular_distribution.

What database are you working with? Or can you say which tutorial, specifically?

Thanks, Chris

aleksandra-kim commented 9 years ago

Original comment by Caroline Nadel (Bitbucket: cazcazn, ).


Like I say, it happened with both tutorials 2 and 3 after following them from 1. In order to import the US-LCI database I had to fix a few things in the code, as when checking if a category is in the biosphere, it crashed. In addition, the database folder structure has changed so it makes sense to update the tutorial.

I would make a pull request, but it entailed forcing the checker to accept some database values that might not have been correct. Import failed with "Incorrect category for biosphere flow". The changes I made to get around this were in bw2data/io/import_ecospold.py:

21:BIOSPHERE = ("air", "water", "soil", "resource", "final-waste-flow", "raw", "others", "non-material")
431:            assert exc[u"matching"][u"categories"][0].lower() in BIOSPHERE, \

Specifically, "Air" was rejected so I added a .lower, and "others" and "non-material" were rejected so I added those to the biosphere list. This may have messed up the database in some unknown way.

Perhaps you could try running through the tutorials from scratch with the latest US-LCI and see what fixes need to be made? Then once I understand the code a bit more I'm more than happy to contribute!

aleksandra-kim commented 9 years ago

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


Hi Cazcazn-

Thanks again, sorry for the delay.

I can't import the US LCI cleanly, due partly to the reasons you have noticed, but also because there are some internal inconsistencies, partly due it looks like to some entries being manually typed in (with the resulting typos), but also due to some pollution from SimaPro. I have also added the SimaPro biosphere categories (https://bitbucket.org/cmutel/brightway2-io/src/b7dca26debaaae2df834ed51df154e3192ac838f/bw2io/compatibility.py?at=default&fileviewer=file-view-default). However, I still have a number of issues with the import, as seen here: http://nbviewer.ipython.org/urls/bitbucket.org/cmutel/brightway2/raw/2.0/notebooks/IO%20-%20Importing%20US%20LCI%20database.ipynb

I would love to have an example database that could be used in the tutorials, but haven't found a good one yet. Maybe you have some ideas?

BTW, you can fix the MC problem with something like this (adapt as needed):

#!python

from brightway2 import *
from stats_arrays import *
import numpy as np

db = Database("whatever")
data = db.load()

for ds in data.values():
    for exc in ds.get('exchanges'):
        if exc['uncertainty type'] == TriangularUncertainty.id and \
                (exc['minimum'] >= exc['loc'] or exc['maximum'] <= exc['loc']):
            # Invalid triangular, switch to no uncertainty
            exc['uncertainty type'] = NoUncertainty.id
            exc['minimum'] = exc['maximum'] = np.nan

db.write(data)

In the development version (see http://brightwaylca.org/dev-docs/), it would be:

#!python

from brightway2 import *
from stats_arrays import *
import numpy as np

for ds in Database("whatever"):
    for exc in ds.exchanges():
        if exc['uncertainty type'] == TriangularUncertainty.id and \
                (exc['minimum'] >= exc['loc'] or exc['maximum'] <= exc['loc']):
            # Invalid triangular, switch to no uncertainty
            exc['uncertainty type'] = NoUncertainty.id
            exc['minimum'] = exc['maximum'] = np.nan
            exc.save()
aleksandra-kim commented 8 years ago

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


If there is a problem, it isn't with bw2calc, so closing this for now.