Exemple #1
0
    def index_arrays(self):
        """Add row and column values to the indices.

        Copied from the PackagesDataLoader class.
        """
        for samples, indices, metadata in self.matrix_data:
            # Allow for iterative indexing, starting with inventory
            if metadata.get("indexed"):
                # Already indexed
                continue
            elif not hasattr(self.lca, metadata["row dict"]):
                # This dictionary not yet built
                continue
            elif "col dict" in metadata and not hasattr(
                    self.lca, metadata["col dict"]):
                # This dictionary not yet built
                continue

            index_with_arrays(indices[metadata["row from label"]],
                              indices[metadata["row to label"]],
                              getattr(self.lca, metadata["row dict"]))
            if "col dict" in metadata:
                index_with_arrays(indices[metadata["col from label"]],
                                  indices[metadata["col to label"]],
                                  getattr(self.lca, metadata["col dict"]))
            metadata["indexed"] = True
def test_index_with_arrays():
    inpt = np.array([1, 2, 3, 6, 9, 12, 9, 6, 5])
    mapping = {1: 0, 3: 2, 5: 3, 6: 4, 9: 5}
    expected = np.array([0, MAX_INT_32, 2, 4, 5, MAX_INT_32, 5, 4, 3])
    output = np.zeros(inpt.size)
    index_with_arrays(inpt, output, mapping)
    assert np.allclose(output, expected)
Exemple #3
0
    def index_arrays(self, lca):
        """Add row and column values to the indices.

        As this function can be called multiple times, we check for each element
        if it has already been called, and whether the required mapping
        dictionary is present."""

        from bw2calc.indexing import index_with_arrays

        for obj in self.matrix_data_loaded:
            for elem in obj["matrix-data"]:
                # Allow for iterative indexing, starting with inventory
                if elem.get('indexed'):
                    # Already indexed
                    continue
                elif not hasattr(lca, elem['row dict']):
                    # This dictionary not yet built
                    continue
                elif "col dict" in elem and not hasattr(lca, elem['col dict']):
                    # This dictionary not yet built
                    continue

                index_with_arrays(elem['indices'][elem['row from label']],
                                  elem['indices'][elem['row to label']],
                                  getattr(lca, elem['row dict']))
                if "col dict" in elem:
                    index_with_arrays(elem['indices'][elem['col from label']],
                                      elem['indices'][elem['col to label']],
                                      getattr(lca, elem['col dict']))
                elem['indexed'] = True
def test_index_with_arrays_negative_error():
    inpt = np.array([1, 2, 3, 6, 9, 12, 9, 6, 5])
    mapping = {-1: 0, 3: 2, 5: 3, 6: 4, 9: 5}
    output = np.zeros(inpt.size)
    with pytest.raises(ValueError):
        index_with_arrays(inpt, output, mapping)
def test_index_with_arrays_preserves_dtype():
    inpt = np.array([1, 2, 3, 6, 9, 12, 9, 6, 5])
    mapping = {1: 0, 3: 2, 5: 3, 6: 4, 9: 5}
    output = np.zeros(inpt.size, dtype=np.uint32)
    index_with_arrays(inpt, output, mapping)
    assert output.dtype == np.uint32