Commits
Comments (0)
Files changed (6)

+1 0bw2calc/monte_carlo.py

+9 9docs/conf.py

+162 6docs/index.rst

+1 4docs/lca.rst

+5 2docs/matrix.rst

+12 3docs/monte_carlo.rst
docs/conf.py
docs/index.rst
This is the technical documentation for Brightway2calc, part of the `Brightway2 <http://brightwaylca.org>`_ life cycle assessment calculation framework. The following online resources are available:
+This is the documentation for Brightway2calc, part of the `Brightway2 <http://brightwaylca.org>`_ life cycle assessment framework.
+Brightway2calc does calculations: static LCA calculations, stochastic LCA calculations, and traversals through the supply chain. A core part of Brightway2calc is the matrix builder, a class that convert parameter arrays into SciPy sparse matrices.
+A parameter array is a NumPy `structured or record array <http://docs.scipy.org/doc/numpy/user/basics.rec.html>`_, where each column has a label and data type. Here is an sample of the parameter array for the US LCI:
+As this is a parameter array for a LCI database, it gives values that will be inserted into the technosphere and biosphere matrices, i.e. values from the dataset exchanges.
+There are also some columns for uncertainty information, but these would only be a distraction for now. The complete spec for the uncertainty fields is given in the `stats_arrays documentation <http://statsarrays.readthedocs.org/en/latest/>`_.
+ * The `amount` column is the only one that seems reasonable, and gives the values that should be inserted into the matrix
+The ``input`` and ``output`` columns gives values for biosphere flows or transforming activity data sets. Brightway2data uses a `mapping dictionary <http://bw2data.readthedocs.org/en/latest/metadata.html#bw2data.meta.Mapping>`_ to translate keys like ``("Douglas Adams", 42)`` into integer values. So, each number uniquely identifies an activity dataset.
+If the ``input`` and ``output`` values are the same, then this is a production exchange  it describes how much product is produced by the transforming activity dataset.
+.. warning:: Integer mapping ids are not transferable from machine to machine or installation to installation, as the order of insertion (and hence the integer id) is more or less at random. Always process new datasets.
+The ``row`` and ``col`` columns have the data type *unsigned integer, 32 bit*, and the maximum value is therefore :math:`2^{32}  1`, i.e. 4294967295. This is just a dummy value telling Brightway2 to insert better data.
+The method ``MatrixBuilder.build_dictionary`` is used to take ``input`` and ``output`` values, respectively, and figure out which rows and columns they correspond to. The actual code is succinct  only one line  but what it does is:
+The method ``MatrixBuilder.add_matrix_indices`` would replace the 4294967295 values with dictionary values based on ``input`` and ``output``. At this point, we have enough to build a sparse matrix using ``MatrixBuilder.build_matrix``:
+Indeed, the `coordinate (coo) matrix <http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html>`_ takes as inputs exactly the row and column indices, and the values to insert.
+Of course, there are some details for specific matrices  technosphere matrices need to be square, and should have ones by default on the diagonal, etc. etc., but this is the general idea.
+The ``type`` column indicates whether a value should be in the technosphere or biosphere matrix: ``0`` is a transforming activity production amount, ``1`` is a technosphere exchange, and ``2`` is a biosphere exchange.
+The actual LCA class then is more of a coordinator then an accountant, as the matrix builder is doing much of the data manipulation. The :ref:`lca` class only has to do the following:
+ * Solve the linear system :math:`Ax=B` using `UMFpack <http://www.cise.ufl.edu/research/sparse/umfpack/>`_
+The LCA class also has some convenience functions for redoing some calculations with slight changes, e.g. for uncertainty and sensitivity analysis. See the "redo_*" and "rebuild_*" methods in the LCA class.
+The various stochastic Monte Carlo LCA classes function almost the same as the static LCA, and reuse most of the code. The only change is that instead of building matrices once, `random number generators from stats_arrays <http://statsarrays.readthedocs.org/en/latest/mcrng.html#montecarlorandomnumbergenerator>`_ are instantiated directly from each parameter array. For each Monte Carlo iteration, the ``amount`` column is then overwritten with the output from the random number generator, and the system solved as normal. The code to do a new Monte Iteration is quite succinct:
+Because there is a common procedure to build static and stochastic matrices, any matrix can easily support uncertainty, e.g. not just LCIA characterization factors, but also weighting, normalization, and anything else you can think of; see `tutorial 5: defining a new matrix <http://nbviewer.ipython.org/url/brightwaylca.org/tutorials/Tutorial%205%20%20Defining%20A%20New%20Matrix.ipynb>`_.
+.. note:: See also the Brightway2 `documentation on contributing <http://brightway2.readthedocs.org/en/latest/contributing.html>`_.
Install `sphinx <http://sphinx.pocoo.org/>`_, and then change to the ``docs`` directory, and run ``make html`` (or ``make.bat html`` in Windows).
docs/matrix.rst
One of the most basic and most important components of ``bw2calc`` is the ability to build a sparse matrix from a processed parameter array.
bw2calc/monte_carlo.py