-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
9 changed files
with
123 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class IncompatibleDatabase(Exception): | ||
"""Database can't be used for inventory calculations. | ||
Usually because it only has biosphere flows.""" | ||
|
||
pass | ||
|
||
|
||
class ObsoleteAggregatedDatapackage(Exception): | ||
"""The results from this aggregated datapackage are obsolete""" | ||
|
||
pass |
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,26 @@ | ||
from collections import Counter | ||
from collections.abc import Iterable | ||
from typing import Optional | ||
|
||
from bw2data import Database, databases | ||
|
||
|
||
def check_processes_in_database(database_name: str) -> bool: | ||
"""Check to make sure database is usable for aggregated calculations.""" | ||
if database_name not in databases: | ||
raise KeyError(f"{database_name} not in databases") | ||
return check_processes_in_data(Database(database_name)) | ||
|
||
|
||
def check_processes_in_data(objects: Iterable) -> bool: | ||
"""Check if any object in the input data has type `process`""" | ||
return any(obj.get("type", "process") == "process" for obj in objects) | ||
|
||
|
||
def get_process_type_counts(database_name: str) -> dict[Optional[str], int]: | ||
"""Get count of each process type in database""" | ||
if database_name not in databases: | ||
raise KeyError(f"{database_name} not in databases") | ||
return dict( | ||
Counter([obj.get("type") for obj in Database(database_name)]).most_common() | ||
) |
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,19 @@ | ||
import pytest | ||
from bw2data import Database | ||
|
||
from bw_aggregation.utils import ( | ||
check_processes_in_data, | ||
check_processes_in_database, | ||
get_process_type_counts, | ||
) | ||
|
||
|
||
def test_check_processes_in_database(background): | ||
assert check_processes_in_database("a") | ||
assert not check_processes_in_database("bio") | ||
with pytest.raises(KeyError): | ||
check_processes_in_database("missing") | ||
|
||
|
||
def test_get_process_type_counts(background): | ||
assert get_process_type_counts("a") == {None: 3, "emission": 1} |