def test_distributions_reproducible(): dp = mc_fixture(seed=123) mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True) results = np.zeros((2, 2, 10)) results[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) results[:, :, i] = mm.matrix.toarray() given = results.sum(axis=0).sum(axis=0).ravel() print(given.shape) print(given) expected = np.array([ 21.06909828, 29.21713645, 37.0735732, 32.0954309, 22.10498503, 30.75377088, 24.87344966, 31.2384641, 27.44208816, 27.45639759, ]) assert np.allclose(expected, given)
def test_distributions_not_allowed(): dp = mc_fixture() mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=False) results = np.zeros((2, 2, 10)) results[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) results[:, :, i] = mm.matrix.toarray() assert np.unique(results).shape == (4, )
def test_distributions_seed_override(): dp = mc_fixture(seed=123) mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True) results = np.zeros((2, 2, 10)) results[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) results[:, :, i] = mm.matrix.toarray() dp = mc_fixture(seed=123) mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True, seed_override=7) first = np.zeros((2, 2, 10)) first[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) first[:, :, i] = mm.matrix.toarray() dp = mc_fixture(seed=567) mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True, seed_override=7) second = np.zeros((2, 2, 10)) second[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) second[:, :, i] = mm.matrix.toarray() assert not np.allclose(results, first) assert np.allclose(first, second)
def test_distributions_seed_in_datapackage(): dp = mc_fixture(seed=123) mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True) results = np.zeros((2, 2, 10)) results[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) results[:, :, i] = mm.matrix.toarray() dp = mc_fixture(seed=123) mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True) other = np.zeros((2, 2, 10)) other[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) other[:, :, i] = mm.matrix.toarray() assert np.allclose(results, other)
def load_lci_data(self) -> None: """Load inventory data and create technosphere and biosphere matrices.""" self.technosphere_mm = mu.MappedMatrix( packages=self.packages, matrix="technosphere_matrix", use_arrays=self.use_arrays, use_distributions=self.use_distributions, seed_override=self.seed_override, ) self.technosphere_matrix = self.technosphere_mm.matrix self.dicts.product = partial(self.technosphere_mm.row_mapper.to_dict) self.dicts.activity = partial(self.technosphere_mm.col_mapper.to_dict) if len(self.technosphere_mm.row_mapper) != len( self.technosphere_mm.col_mapper): raise NonsquareTechnosphere(( "Technosphere matrix is not square: {} activities (columns) and {} products (rows). " "Use LeastSquaresLCA to solve this system, or fix the input " "data").format( len(self.technosphere_mm.col_mapper), len(self.technosphere_mm.row_mapper), )) self.biosphere_mm = mu.MappedMatrix( packages=self.packages, matrix="biosphere_matrix", use_arrays=self.use_arrays, use_distributions=self.use_distributions, seed_override=self.seed_override, col_mapper=self.technosphere_mm.col_mapper, empty_ok=True) self.biosphere_matrix = self.biosphere_mm.matrix self.dicts.biosphere = partial(self.biosphere_mm.row_mapper.to_dict) if self.biosphere_mm.matrix.shape[0] == 0: warnings.warn( "No valid biosphere flows found. No inventory results can " "be calculated, `lcia` will raise an error")
def load_normalization_data( self, data_objs: Optional[Iterable[Union[FS, bwp.DatapackageBase]]]) -> None: """Load normalization data.""" self.normalization_mm = mu.MappedMatrix( packages=data_objs or self.packages, matrix="normalization_matrix", use_arrays=self.use_arrays, use_distributions=self.use_distributions, seed_override=self.seed_override, row_mapper=self.biosphere_mm.row_mapper, diagonal=True, ) self.normalization_matrix = self.normalization_mm.matrix
def test_distributions_only_array_present(): dp = bwp.create_datapackage(sequential=True) dp.add_persistent_array( matrix="foo", name="first", indices_array=np.array([(0, 0), (0, 1)], dtype=bwp.INDICES_DTYPE), data_array=np.array([[1, 2], [3, 4]]), ) mm = mu.MappedMatrix( packages=[dp], matrix="foo", use_distributions=True, use_arrays=True, ) assert mm.matrix.sum() == 4 next(mm) assert mm.matrix.sum() == 6 next(mm) assert mm.matrix.sum() == 4
def test_distributions_without_uncertainties(): dp = mc_fixture() mm = mu.MappedMatrix(packages=[dp], matrix="bar", use_distributions=True) results = np.zeros((5, 6, 10)) results[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) results[:, :, i] = mm.matrix.toarray() row = mm.row_mapper.to_dict() col = mm.col_mapper.to_dict() assert 50 <= np.mean(results[row[10], col[20], :]) <= 150 assert 8 <= np.mean(results[row[11], col[21], :]) <= 14 assert np.allclose(results[row[10], col[10], :], 11) assert np.allclose(results[row[18], col[7], :], 125) # Zero plus 4 fixed plus 2 variable over 10 iterations assert np.unique(results).shape == (25, )
def test_basic_distributions(): dp = mc_fixture() mm = mu.MappedMatrix(packages=[dp], matrix="foo", use_distributions=True) results = np.zeros((2, 2, 10)) results[:, :, 0] = mm.matrix.toarray() for i in range(1, 10): next(mm) results[:, :, i] = mm.matrix.toarray() row = mm.row_mapper.to_dict() col = mm.col_mapper.to_dict() assert -1 <= np.mean(results[row[0], col[0], :]) <= 1 assert 0.25 < np.std(results[row[0], col[0], :]) < 0.75 assert results[row[1], col[1], :].min() >= 0 assert results[row[1], col[1], :].min() <= 20 # mean is 35/3 assert 7.5 < np.mean(results[row[1], col[1], :]) < 16.5 assert np.unique(results).shape == (40, )
def load_lcia_data( self, data_objs: Optional[Iterable[Union[FS, bwp.DatapackageBase]]] = None ) -> None: """Load data and create characterization matrix. This method will filter out regionalized characterization factors. """ global_index, kwargs = consistent_global_index(data_objs or self.packages), {} if global_index is not None: kwargs["custom_filter"] = lambda x: x["col"] == global_index self.characterization_mm = mu.MappedMatrix( packages=data_objs or self.packages, matrix="characterization_matrix", use_arrays=self.use_arrays, use_distributions=self.use_distributions, seed_override=self.seed_override, row_mapper=self.biosphere_mm.row_mapper, diagonal=True, ) self.characterization_matrix = self.characterization_mm.matrix