brightway2-calc / bw2calc / tests / sensitivity.py

# -*- coding: utf-8 -*
from __future__ import division
from stats_arrays.distributions import *
from ..sensitivity import SobolSensitivity
from bw2data import *
from bw2data.tests import BW2DataTest
import numpy as np


class SobolSensitivityTestCase(BW2DataTest):
    def test_generate_matrix(self):
        params = UncertaintyBase.from_dicts(
            {'loc': 2, 'scale': 0.2, 'uncertainty_type': NormalUncertainty.id},
            {'loc': 1.5, 'minimum': 0, 'maximum': 10,
                'uncertainty_type': TriangularUncertainty.id}
        )
        ss = SobolSensitivity()
        matrix = ss.generate_matrix(params, 1000)
        self.assertEqual(matrix.shape, (2, 1000))
        self.assertTrue(1.98 < np.median(matrix[0, :]) < 2.02)

    def test_seed(self):
        params = UncertaintyBase.from_dicts(
            {'loc': 2, 'scale': 0.2, 'uncertainty_type': NormalUncertainty.id},
        )
        ss = SobolSensitivity(seed=17)
        matrix_1 = ss.generate_matrix(params, 1000)
        ss = SobolSensitivity(seed=17)
        matrix_2 = ss.generate_matrix(params, 1000)
        self.assertTrue(np.allclose(matrix_1, matrix_2))

    def test_c_matrix(self):
        a = np.random.random(size=20000).reshape((100, 200))
        b = np.random.random(size=20000).reshape((100, 200))
        ss = SobolSensitivity()
        c = ss.generate_c(a, b, 14)
        self.assertTrue(np.allclose(
            b[:, 13], c[:, 13]
        ))
        self.assertTrue(np.allclose(
            a[:, 14], c[:, 14]
        ))
        self.assertTrue(np.allclose(
            b[:, 15], c[:, 15]
        ))

    def test_evaluate_matrix(self):
        matrix = np.arange(15).reshape((3, 5))
        model = lambda x: x[1]
        ss = SobolSensitivity()
        results = ss.evaluate_matrix(matrix, model)
        self.assertTrue(np.allclose(results, [5, 6, 7, 8, 9]))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.