-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
romainsacchi
authored and
romainsacchi
committed
Apr 11, 2024
1 parent
7ee5e96
commit 7c5d05c
Showing
9 changed files
with
341 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
from unittest.mock import mock_open, patch | ||
from pathways.lca import read_indices_csv, load_matrix_and_index | ||
from pathlib import Path | ||
import numpy as np | ||
|
||
|
||
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, | ||
} | ||
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") | ||
expected_output = ( | ||
np.array([3.5, 0.5]), | ||
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', '?')]) | ||
) | ||
|
||
# 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) | ||
|
||
print("distributions_array", distributions_array) | ||
print("expected_output", expected_output[3]) | ||
|
||
# Check that the output matches the expected output | ||
# but they have different dtypes | ||
|
||
assert np.allclose(data_array, expected_output[0]) | ||
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]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import pytest | ||
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 | ||
|
||
|
||
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"] | ||
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" | ||
|
||
|
||
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}, | ||
] | ||
} | ||
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" | ||
|
||
|
||
@pytest.fixture | ||
def mock_lcia_methods_data(): | ||
"""Returns mock LCIA methods similar to what get_lcia_methods would return.""" | ||
return { | ||
"IPCC 2021 - Global Warming Potential": { | ||
("CO2", "air", "unspecified"): 1, | ||
("CH4", "air", "low population density, long-term"): 25, | ||
} | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def mock_biosphere_data(): | ||
"""Returns mock biosphere dictionary and matrix dict for testing.""" | ||
biosphere_dict = { | ||
("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 | ||
return biosphere_matrix_dict, biosphere_dict | ||
|
||
|
||
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) | ||
|
||
assert isinstance(matrix, csr_matrix), "Output is not a CSR matrix" | ||
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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
from unittest.mock import Mock | ||
|
||
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} | ||
group_by = lambda x: x[1] # Group by location | ||
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" | ||
|
||
|
||
def test_get_mapping(): | ||
mock_data = Mock() | ||
mock_data.get_resource.return_value.raw_read.return_value = """ | ||
variable1: | ||
dataset: [details] | ||
""" | ||
expected_mapping = {'variable1': {'dataset': ['details']}} | ||
assert _get_mapping(mock_data) == expected_mapping, "Mapping does not match expected dictionary" | ||
|
Oops, something went wrong.