Skip to content

Commit ada3ce5

Browse files
authored
Update/bw25 (#43)
* (update) Impact proxies are functional with bw25 but necessitate EcoInvent. Automatically add production exchange for every foreground activity. Determine free symbols in models without lcaa. Fix package versions in requirements. * (update) Can create methods and impact proxies without ecoinvent. Update tests. Fix samples.
1 parent 71f88fe commit ada3ce5

27 files changed

Lines changed: 1064 additions & 578 deletions

appabuild/cli/lca.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ def build(
3232
An AppaBuild environment is initialized (background and foreground databases), unless --no-init is specified.
3333
3434
"""
35-
try:
36-
foreground_database = None
37-
if init:
38-
if not appabuild_config_path:
39-
logger.error(
40-
"AppaBuild configuration file and LCA config file are required for initialization",
41-
exc_info=True,
42-
)
43-
raise ValueError()
44-
foreground_database = setup.initialize(appabuild_config_path)
35+
# try:
36+
foreground_database = None
37+
if init:
38+
if not appabuild_config_path:
39+
logger.error(
40+
"AppaBuild configuration file and LCA config file are required for initialization",
41+
exc_info=True,
42+
)
43+
raise ValueError()
44+
foreground_database = setup.initialize(appabuild_config_path)
4545

46+
try:
4647
setup.build(lca_config_path, foreground_database)
4748
except (ValueError, ValidationError, BwDatabaseError):
4849
sys.exit(1)

appabuild/config/appa_lca.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import annotations
66

77
import os
8-
from typing import Dict
8+
from typing import Dict, Optional, Union
99

1010
import yaml
1111
from pydantic import BaseModel, ValidationError, field_validator
@@ -14,7 +14,17 @@
1414
from appabuild.logger import log_validation_error, logger
1515

1616

17-
class DatabaseConfig(BaseModel):
17+
class DatabasesConfig(BaseModel):
18+
foreground: ForegroundDatabaseConfig
19+
ecoinvent: Optional[EcoInventDatabaseConfig] = None
20+
21+
22+
class EcoInventDatabaseConfig(BaseModel):
23+
version: str
24+
system_model: str
25+
26+
27+
class ForegroundDatabaseConfig(BaseModel):
1828
"""
1929
Database entry in an Appa LCA configuration.
2030
@@ -45,7 +55,8 @@ class AppaLCAConfig(BaseModel):
4555
"""
4656

4757
project_name: str
48-
databases: Dict[str, DatabaseConfig]
58+
replace_bg: Optional[bool] = False
59+
databases: DatabasesConfig
4960

5061
@field_validator("databases", mode="before")
5162
@classmethod

appabuild/database/bw_databases.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import re
88
from typing import List, Optional, Union
99

10-
import brightway2 as bw
10+
import bw2data as bd
1111
from pydantic import BaseModel
1212

1313
from appabuild.database.serialized_data import ActivityIdentifier
14-
from appabuild.exceptions import BwDatabaseError
14+
from appabuild.exceptions import BwDatabaseError, SerializedDataError
15+
from appabuild.logger import logger
1516

1617

1718
class BwDatabase(BaseModel):
@@ -27,7 +28,7 @@ def database(self):
2728
Brightway database object
2829
:return:
2930
"""
30-
return bw.Database(self.name)
31+
return bd.Database(self.name)
3132

3233
def search_activity(
3334
self, regexes: dict, must_find_only_one: Optional[bool] = False
@@ -48,14 +49,13 @@ def search_activity(
4849
)
4950
matching_activities = list(set.intersection(*map(set, matching_acts)))
5051
if must_find_only_one and len(matching_activities) < 1:
51-
raise BwDatabaseError(
52-
f"Cannot find any activity resolving the following regexes: {regexes}."
53-
)
52+
e = f"Cannot find any activity resolving the following regexes: {regexes}."
53+
logger.exception(e)
54+
raise BwDatabaseError(e)
5455
if must_find_only_one and len(matching_activities) > 1:
55-
raise BwDatabaseError(
56-
f"Too many activity matching the following regexes {regexes}. Matches "
57-
f"are {matching_activities}."
58-
)
56+
e = f"Too many activity matching the following regexes {regexes}. Matches are {matching_activities}."
57+
logger.exception(e)
58+
raise BwDatabaseError(e)
5959
if must_find_only_one:
6060
return ActivityIdentifier(
6161
database=matching_activities[0][0], uuid=matching_activities[0][1]
@@ -79,10 +79,9 @@ def resolve_activity_identifier(
7979
regexes = unresolved_activity_identifier.model_dump()
8080
database = regexes.pop("database")
8181
if database != self.name:
82-
raise ValueError(
83-
f"Cannot search activity on a different database ({database} != "
84-
f"{self.name})."
85-
)
82+
e = f"Cannot search activity on a different database ({database} != {self.name})."
83+
logger.exception(e)
84+
raise SerializedDataError(e)
8685
activity_identifier = self.search_activity(
8786
regexes=regexes, must_find_only_one=True
8887
)

0 commit comments

Comments
 (0)