Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Apr 11, 2024
2 parents b33696f + 527b9a8 commit 85bf756
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 80 deletions.
14 changes: 10 additions & 4 deletions pathways/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

from .filesystem_constants import DIR_CACHED_DB
from .lcia import fill_characterization_factors_matrices
from .utils import get_unit_conversion_factors, fetch_indices, check_unclassified_activities, \
_group_technosphere_indices
from .utils import (
_group_technosphere_indices,
check_unclassified_activities,
fetch_indices,
get_unit_conversion_factors,
)

logging.basicConfig(
level=logging.DEBUG,
Expand Down Expand Up @@ -56,8 +60,10 @@ def read_indices_csv(file_path: Path) -> dict[tuple[str, str, str, str], int]:
try:
indices[(row[0], row[1], row[2], row[3])] = int(row[4])
except IndexError as err:
logging.error(f"Error reading row {row} from {file_path}: {err}. "
f"Could it be that the file uses commas instead of semicolons?")
logging.error(
f"Error reading row {row} from {file_path}: {err}. "
f"Could it be that the file uses commas instead of semicolons?"
)
return indices


Expand Down
43 changes: 28 additions & 15 deletions tests/test_lca.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
import pytest
from unittest.mock import mock_open, patch
from pathways.lca import read_indices_csv, load_matrix_and_index
from pathlib import Path
from unittest.mock import mock_open, patch

import numpy as np
import pytest

from pathways.lca import load_matrix_and_index, read_indices_csv


def test_read_indices_csv_success():
mock_csv_data = "activity;product;location;unit;1\nanother_activity;another_product;another_location;another_unit;2"
expected_dict = {
('activity', 'product', 'location', 'unit'): 1,
('another_activity', 'another_product', 'another_location', 'another_unit'): 2,
("activity", "product", "location", "unit"): 1,
("another_activity", "another_product", "another_location", "another_unit"): 2,
}
with patch("builtins.open", mock_open(read_data=mock_csv_data)):
result = read_indices_csv(Path("dummy_path.csv"))
assert result == expected_dict


def test_load_matrix_and_index(tmp_path):
mock_csv_data = ("row;col;value;uncertainty type;loc;scale;shape;minimum;maximum;negative;flip"
"\n1;0;3.5;3;4;5;6;7;8;0;0"
"\n1;1;0.5;3;4;5;6;7;8;0;1")
mock_csv_data = (
"row;col;value;uncertainty type;loc;scale;shape;minimum;maximum;negative;flip"
"\n1;0;3.5;3;4;5;6;7;8;0;0"
"\n1;1;0.5;3;4;5;6;7;8;0;1"
)
expected_output = (
np.array([3.5, 0.5]),
np.array([(0, 1), (1, 1)], dtype=[('row', 'i4'), ('col', 'i4')]),
np.array([(0, 1), (1, 1)], dtype=[("row", "i4"), ("col", "i4")]),
np.array([False, True]),
np.array([(3, 4.0, 5.0, 6.0, 7.0, 8.0, False), (3, 4.0, 5.0, 6.0, 7.0, 8.0, False)],
dtype=[('uncertainty_type', 'i4'), ('loc', 'f4'), ('scale', 'f4'), ('shape', 'f4'), ('minimum', 'f4'),
('maximum', 'f4'), ('negative', '?')])
np.array(
[(3, 4.0, 5.0, 6.0, 7.0, 8.0, False), (3, 4.0, 5.0, 6.0, 7.0, 8.0, False)],
dtype=[
("uncertainty_type", "i4"),
("loc", "f4"),
("scale", "f4"),
("shape", "f4"),
("minimum", "f4"),
("maximum", "f4"),
("negative", "?"),
],
),
)

# Write mock CSV data to a temporary file
temp_file = tmp_path / "temp.csv"
temp_file.write_text(mock_csv_data)

# Call the function with the path to the temporary file
data_array, indices_array, flip_array, distributions_array = load_matrix_and_index(temp_file)
data_array, indices_array, flip_array, distributions_array = load_matrix_and_index(
temp_file
)

print("distributions_array", distributions_array)
print("expected_output", expected_output[3])
Expand All @@ -46,5 +61,3 @@ def test_load_matrix_and_index(tmp_path):
assert np.array_equal(indices_array, expected_output[1])
assert np.array_equal(flip_array, expected_output[2])
assert np.array_equal(distributions_array, expected_output[3])


66 changes: 49 additions & 17 deletions tests/test_lcia.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
import pytest
import json
from unittest.mock import mock_open, patch
from pathways.lcia import get_lcia_method_names, format_lcia_method_exchanges, fill_characterization_factors_matrices
from scipy.sparse import csr_matrix

import numpy as np
import json
import pytest
from scipy.sparse import csr_matrix

from pathways.lcia import (
fill_characterization_factors_matrices,
format_lcia_method_exchanges,
get_lcia_method_names,
)


def test_get_lcia_method_names_success():
mock_data = '[{"name": ["IPCC", "2021", "Global Warming Potential"]}, {"name": ["ReCiPe", "2016", "Midpoint"]} ]'
expected_result = ["IPCC - 2021 - Global Warming Potential", "ReCiPe - 2016 - Midpoint"]
expected_result = [
"IPCC - 2021 - Global Warming Potential",
"ReCiPe - 2016 - Midpoint",
]
with patch("builtins.open", mock_open(read_data=mock_data)):
with patch("json.load", return_value=json.loads(mock_data)):
method_names = get_lcia_method_names()
assert method_names == expected_result, "Method names not correctly formatted"
assert (
method_names == expected_result
), "Method names not correctly formatted"


def test_format_lcia_method_exchanges():
method_input = {
"exchanges": [
{"name": "CO2", "categories": ["air"], "amount": 1},
{"name": "CH4", "categories": ["air", "low population density, long-term"], "amount": 25},
{
"name": "CH4",
"categories": ["air", "low population density, long-term"],
"amount": 25,
},
]
}
expected_output = {
("CO2", "air", "unspecified"): 1,
("CH4", "air", "low population density, long-term"): 25,
}
assert format_lcia_method_exchanges(method_input) == expected_output, "Exchange formatting incorrect"
assert (
format_lcia_method_exchanges(method_input) == expected_output
), "Exchange formatting incorrect"


@pytest.fixture
Expand All @@ -47,23 +64,38 @@ def mock_biosphere_data():
("CO2", "air", "unspecified"): 0,
("CH4", "air", "low population density, long-term"): 1,
}
biosphere_matrix_dict = {0: 0, 1: 1} # Mapping of biosphere_dict indices to matrix indices
biosphere_matrix_dict = {
0: 0,
1: 1,
} # Mapping of biosphere_dict indices to matrix indices
return biosphere_matrix_dict, biosphere_dict


def test_fill_characterization_factors_matrices(mock_lcia_methods_data, mock_biosphere_data):
def test_fill_characterization_factors_matrices(
mock_lcia_methods_data, mock_biosphere_data
):
methods = ["IPCC 2021 - Global Warming Potential"]
biosphere_matrix_dict, biosphere_dict = mock_biosphere_data

with patch('pathways.lcia.get_lcia_methods', return_value=mock_lcia_methods_data):
matrix = fill_characterization_factors_matrices(methods, biosphere_matrix_dict, biosphere_dict, debug=False)
with patch("pathways.lcia.get_lcia_methods", return_value=mock_lcia_methods_data):
matrix = fill_characterization_factors_matrices(
methods, biosphere_matrix_dict, biosphere_dict, debug=False
)

assert isinstance(matrix, csr_matrix), "Output is not a CSR matrix"
assert matrix.shape == (len(methods), len(biosphere_matrix_dict)), "Matrix shape is incorrect"
assert matrix.shape == (
len(methods),
len(biosphere_matrix_dict),
), "Matrix shape is incorrect"

# Verifying content of the matrix
expected_data = np.array([1, 25])
np.testing.assert_array_equal(matrix.data, expected_data, "Matrix data does not match expected values")
np.testing.assert_array_equal(matrix.indices, np.array([0, 1]), "Matrix indices do not match expected values")
np.testing.assert_array_equal(matrix.indptr, np.array([0, 2]), "Matrix indices does not match expected values")

np.testing.assert_array_equal(
matrix.data, expected_data, "Matrix data does not match expected values"
)
np.testing.assert_array_equal(
matrix.indices, np.array([0, 1]), "Matrix indices do not match expected values"
)
np.testing.assert_array_equal(
matrix.indptr, np.array([0, 2]), "Matrix indices does not match expected values"
)
16 changes: 9 additions & 7 deletions tests/test_pathways.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import pytest
from unittest.mock import Mock

import pytest

from pathways.pathways import _get_mapping
from pathways.utils import _group_technosphere_indices


def test_group_technosphere_indices():
indices = {('activity1', 'location1'): 0, ('activity2', 'location2'): 1}
indices = {("activity1", "location1"): 0, ("activity2", "location2"): 1}
group_by = lambda x: x[1] # Group by location
group_values = ['location1', 'location2']
expected = {'location1': [0], 'location2': [1]}
group_values = ["location1", "location2"]
expected = {"location1": [0], "location2": [1]}
result = _group_technosphere_indices(indices, group_by, group_values)
assert result == expected, "Grouping does not match expected output"

Expand All @@ -20,6 +21,7 @@ def test_get_mapping():
variable1:
dataset: [details]
"""
expected_mapping = {'variable1': {'dataset': ['details']}}
assert _get_mapping(mock_data) == expected_mapping, "Mapping does not match expected dictionary"

expected_mapping = {"variable1": {"dataset": ["details"]}}
assert (
_get_mapping(mock_data) == expected_mapping
), "Mapping does not match expected dictionary"
Loading

0 comments on commit 85bf756

Please sign in to comment.