Skip to content

Commit

Permalink
Black reformating
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Jun 16, 2024
1 parent 3228a54 commit ddaf004
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
61 changes: 33 additions & 28 deletions pathways/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ def get_lca_matrices(
geo: Geomap = None,
remove_infrastructure: bool = False,
double_counting: dict = None,
) -> tuple[Datapackage, dict[tuple[str, str, str, str], int], dict[tuple, int], list[tuple[int, int]], dict | None]:
) -> tuple[
Datapackage,
dict[tuple[str, str, str, str], int],
dict[tuple, int],
list[tuple[int, int]],
dict | None,
]:
"""
Retrieve Life Cycle Assessment (LCA) matrices from disk.
Expand Down Expand Up @@ -171,8 +177,17 @@ def select_filepath(keyword: str, fps):

if remove_infrastructure is True and matrix_name == "technosphere_matrix":
print("--------- Removing infrastructure exchanges")
infrastructure_indices = np.array([j for i, j in technosphere_inds.items() if i[2] == "unit" and "uranium" not in i[0].lower()])
idx = np.where(np.isin(indices["row"], infrastructure_indices) & (indices["row"] != indices["col"]))
infrastructure_indices = np.array(
[
j
for i, j in technosphere_inds.items()
if i[2] == "unit" and "uranium" not in i[0].lower()
]
)
idx = np.where(
np.isin(indices["row"], infrastructure_indices)
& (indices["row"] != indices["col"])
)
data[idx] = 0

if double_counting is not None and matrix_name == "technosphere_matrix":
Expand All @@ -187,28 +202,21 @@ def select_filepath(keyword: str, fps):
idx_v = values[activity]["idx"]
amount = y[year]
# add indices to `indices`
indices = np.append(indices, np.array([(idx_k, idx_v)], dtype=bwp.INDICES_DTYPE), axis=0)
indices = np.append(
indices,
np.array([(idx_k, idx_v)], dtype=bwp.INDICES_DTYPE),
axis=0,
)
data = np.append(data, amount)
print(f"Added {amount} from {k} to {activity}")
distributions=np.append(
distributions = np.append(
distributions,
np.array(
(
0,
None,
None,
None,
None,
None,
False
),
(0, None, None, None, None, None, False),
dtype=bwp.UNCERTAINTY_DTYPE,
)
)
sign = np.append(
sign,
np.array([True])
),
)
sign = np.append(sign, np.array([True]))

if matrix_name == "technosphere_matrix":
uncertain_parameters = find_uncertain_parameters(distributions, indices)
Expand Down Expand Up @@ -352,7 +360,7 @@ def process_region(data: Tuple) -> dict[str, ndarray[Any, dtype[Any]] | list[int
debug,
use_distributions,
uncertain_parameters,
#activities_to_exclude,
# activities_to_exclude,
) = data

variables_demand = {}
Expand Down Expand Up @@ -409,10 +417,8 @@ def process_region(data: Tuple) -> dict[str, ndarray[Any, dtype[Any]] | list[int

demand = {idx: demand.values * float(unit_vector)}


lca.lci(demand=demand)


# if activities_to_exclude is not None:
# check_non_zero_values(lca.technosphere_matrix, activities_to_exclude)

Expand Down Expand Up @@ -516,7 +522,7 @@ def _calculate_year(args: tuple):
shares,
uncertain_parameters,
remove_infrastructure,
double_counting
double_counting,
) = args

print(f"------ Calculating LCA results for {year}...")
Expand Down Expand Up @@ -632,14 +638,13 @@ def _calculate_year(args: tuple):

lca.lci(factorize=True)

#infra_idx = [v for k, v in technosphere_indices.items() if k[2] == "unit"]
# infra_idx = [v for k, v in technosphere_indices.items() if k[2] == "unit"]

#lca.technosphere_matrix = remove_double_counting(
# lca.technosphere_matrix = remove_double_counting(
# technosphere_matrix=lca.technosphere_matrix,
# activities_to_zero=vars_info_idx,
# infra=infra_idx,
#)

# )

# if activities_to_exclude is not None:
# lca = remove_double_accounting(
Expand Down Expand Up @@ -725,7 +730,7 @@ def _calculate_year(args: tuple):
debug,
use_distributions,
uncertain_parameters,
#activities_to_exclude,
# activities_to_exclude,
)
)

Expand Down
9 changes: 6 additions & 3 deletions pathways/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ def calculate(

try:
_, technosphere_index, _, uncertain_parameters, _ = get_lca_matrices(
filepaths=self.filepaths, model=models[0], scenario=scenarios[0], year=years[0]
filepaths=self.filepaths,
model=models[0],
scenario=scenarios[0],
year=years[0],
)
except Exception as e:
logging.error(f"Error retrieving LCA matrices: {str(e)}")
Expand Down Expand Up @@ -392,7 +395,7 @@ def calculate(
uncertain_parameters,
shares,
remove_infrastructure,
double_counting
double_counting,
)
for year in years
]
Expand Down Expand Up @@ -431,7 +434,7 @@ def calculate(
shares or None,
uncertain_parameters,
remove_infrastructure,
double_counting
double_counting,
)
)
for year in years
Expand Down

0 comments on commit ddaf004

Please sign in to comment.