Skip to content

Commit

Permalink
Black reformating
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Mar 20, 2024
1 parent c1535d9 commit 92bfc36
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
11 changes: 7 additions & 4 deletions dev/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
p.calculate(
methods=[
"RELICS - metals extraction - Lithium",
#"RELICS - metals extraction - Molybdenum",
# "RELICS - metals extraction - Molybdenum",
],
regions=[
"WEU", "USA",
"WEU",
"USA",
],
scenarios=[scenario],
years=[
2010, 2020, 2030,
2010,
2020,
2030,
],
variables=[
v
for v in p.scenarios.coords["variables"].values
if any(i in v for i in ["Industry", "Transport", "Heating"])
],
demand_cutoff=0.01,
multiprocessing=True
multiprocessing=True,
)
arr = p.display_results()
arr.to_netcdf(f"results_image_{scenario}.nc")
49 changes: 34 additions & 15 deletions pathways/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import pyprind
import xarray as xr
import yaml
from numpy import ndarray, dtype
from scipy import sparse
from datapackage import DataPackage
from numpy import dtype, ndarray
from premise.geomap import Geomap
from scipy import sparse

from . import DATA_DIR
from .data_validation import validate_datapackage
Expand Down Expand Up @@ -227,7 +227,7 @@ def process_region(data: Tuple) -> dict[str, ndarray[Any, dtype[Any]] | list[int
locations,
location_to_index,
rev_A_index,
lca
lca,
) = data

FU = []
Expand Down Expand Up @@ -280,7 +280,11 @@ def process_region(data: Tuple) -> dict[str, ndarray[Any, dtype[Any]] | list[int

# concatenate the list of sparse matrices and
# add a third dimension and concatenate along it
return {"data": np.column_stack(d), "variables": variables_demand, "variables_idx": variables_idx}
return {
"data": np.column_stack(d),
"variables": variables_demand,
"variables_idx": variables_idx,
}


def _calculate_year(args):
Expand Down Expand Up @@ -314,15 +318,11 @@ def _calculate_year(args):

except FileNotFoundError:
# If LCA matrices can't be loaded, skip to the next iteration
print(
"LCA matrices not found for the given model, scenario, and year."
)
print("LCA matrices not found for the given model, scenario, and year.")
return

# Fetch indices
vars_info = fetch_indices(
mapping, regions, variables, technosphere_indices, geo
)
vars_info = fetch_indices(mapping, regions, variables, technosphere_indices, geo)

# Remove contribution from activities in other activities
# A = remove_double_counting(A, vars_info)
Expand All @@ -344,7 +344,9 @@ def _calculate_year(args):

lca = bc.LCA(
demand={0: 1},
data_objs=[bw_datapackage,],
data_objs=[
bw_datapackage,
],
)
lca.lci(factorize=True)

Expand Down Expand Up @@ -375,7 +377,7 @@ def _calculate_year(args):
locations,
location_to_index,
rev_technosphere_index,
lca
lca,
)
)

Expand All @@ -393,15 +395,31 @@ def _calculate_year(args):
category_idx = []
for cat in lca_results.coords["act_category"].values:
category_idx.append(
[int(technosphere_indices[a]) for a in reverse_classifications[cat] if a in technosphere_indices]
[
int(technosphere_indices[a])
for a in reverse_classifications[cat]
if a in technosphere_indices
]
)

mask = np.array([act[-1] in act_locs for act in rev_technosphere_index.values()])
loc_idx = np.array(
[location_to_index[act[-1]] for act in rev_technosphere_index.values() if act[-1] in act_locs])
[
location_to_index[act[-1]]
for act in rev_technosphere_index.values()
if act[-1] in act_locs
]
)

# Initialize the new array with zeros for missing data
E = np.zeros((len(technosphere_indices), len(locations), len(impact_categories), len(variables)))
E = np.zeros(
(
len(technosphere_indices),
len(locations),
len(impact_categories),
len(variables),
)
)
print("E.shape", E.shape)

for region, result in results.items():
Expand All @@ -428,6 +446,7 @@ def _calculate_year(args):

return lca_results


class Pathways:
"""The Pathways class reads in a datapackage that contains scenario data,
mapping between scenario variables and LCA datasets, and LCA matrices.
Expand Down

0 comments on commit 92bfc36

Please sign in to comment.