Skip to content

Commit

Permalink
Update library to reflect the renaming from rmnd_lca to premise.
Browse files Browse the repository at this point in the history
  • Loading branch information
Loisel committed Jan 4, 2021
1 parent 5383e87 commit 02d753d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 65 deletions.
6 changes: 3 additions & 3 deletions lca2rmnd/activity_select.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rmnd_lca import InventorySet
from premise import InventorySet
from bw2data.backends.peewee.proxies import Activity, ActivityDataset as Act
from functools import reduce

class ActivitySelector():
"""
Maps ecoinvent activites to REMIND technologies.
Uses filter definitions from :class:`rmnd_lca.InventorySet`.
Uses filter definitions from :class:`premise.InventorySet`.
:param db: A lice cycle inventory database
:type db: brightway2 database object
Expand All @@ -16,7 +16,7 @@ def create_expr(self, fltr={}, mask={}, filter_exact=False, mask_exact=False):
Create a :class:`peewee.Expression` from a filter dictionary.
For the specification of the filter dictionary please refer to the
documentation of :class:`rmnd_lca.InventorySet`.
documentation of :class:`premise.InventorySet`.
:param fltr: string, list of strings or dictionary.
If a string is provided, it is used to match the name field from the start (*startswith*).
Expand Down
16 changes: 9 additions & 7 deletions lca2rmnd/prepare_inventories.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

import rmnd_lca
import premise
from bw2data.utils import merge_databases

from carculator import CarInputParameters, \
Expand All @@ -29,6 +29,8 @@
import brightway2 as bw
import numpy as np

fpei36 = "/home/alois/ecoinvent/ecoinvent 3.6_cut-off_ecoSpold02/datasets/"
model = "remind"

def create_project(project_name, ecoinvent_path,
years, scenario, remind_data_path, from_scratch=True):
Expand All @@ -37,7 +39,7 @@ def create_project(project_name, ecoinvent_path,
inventories for electricity markets according to
REMIND data.
Relies on `rmnd_lca.NewDatabase`. Existing databases are
Relies on `premise.NewDatabase`. Existing databases are
deleted.
:param str project_name: name of the brightway2 project to modify
Expand All @@ -64,20 +66,20 @@ def create_project(project_name, ecoinvent_path,
print("Database has already been imported")
else:
ei36 = bw.SingleOutputEcospold2Importer(
ecoinvent_path, 'ecoinvent 3.6 cutoff')
fpei36, 'ecoinvent 3.6 cutoff')
ei36.apply_strategies()
ei36.statistics()
ei36.write_database()

for year in years:
print("Create modified database for scenario {} and year {}"
.format(scenario, year))
ndb = rmnd_lca.NewDatabase(
ndb = premise.NewDatabase(
scenario=scenario,
year=year,
source_db='ecoinvent 3.6 cutoff',
source_version=3.6,
filepath_to_remind_files=remind_data_path)
filepath_to_iam_files=remind_data_path)
ndb.update_all()
ndb.write_db_to_brightway()

Expand Down Expand Up @@ -117,7 +119,7 @@ def relink_electricity_demand(scenario, year):
:param year: REMIND year.
"""
eidb = bw.Database(rmnd_lca.utils.eidb_label(scenario, year))
eidb = bw.Database(premise.utils.eidb_label(model, scenario, year))
remind_regions = [
'LAM', 'OAS', 'SSA', 'EUR',
'NEU', 'MEA', 'REF', 'CAZ',
Expand Down Expand Up @@ -193,7 +195,7 @@ def load_and_merge(scenario, years, relink=True):
from market groups in REMIND regions
"""
for year in years:
eidb = rmnd_lca.utils.eidb_label(scenario, year)
eidb = premise.utils.eidb_label(model, scenario, year)
inv = load_car_activities(np.array([year]))
inv.apply_strategies()

Expand Down
127 changes: 78 additions & 49 deletions lca2rmnd/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from .activity_select import ActivitySelector
from .utils import project_string

from rmnd_lca import Geomap, InventorySet
from rmnd_lca.utils import eidb_label
from premise import Geomap, InventorySet
from premise.utils import eidb_label

from bw2data.backends.peewee.proxies import Activity, ActivityDataset as Act
import brightway2 as bw
Expand Down Expand Up @@ -34,10 +34,12 @@ class LCAReporting():
:vartype source_db: str
"""
def __init__(self, scenario, years, project,
remind_output_folder,
remind_output_folder, regions,
indicatorgroup='ReCiPe Midpoint (H) V1.13'):
self.years = years
self.scenario = scenario
self.model = "remind"
self.regions = regions
bw.projects.set_current(project)
self.selector = ActivitySelector()
self.methods = [m for m in bw.methods if m[0] == indicatorgroup
Expand All @@ -59,6 +61,8 @@ def __init__(self, scenario, years, project,
rdc = RemindDataCollection(self.scenario, remind_output_folder)
self.data = rdc.data[rdc.data.Year.isin(self.years) &
(rdc.data.Region != "World")]
# all regions there?
assert self.regions in self.data.Region.unique()
self.geo = Geomap()


Expand Down Expand Up @@ -92,26 +96,56 @@ def _act_from_variable(self, variable, db, year, region, scale=1):
"""
Find the activity for a given REMIND transport reporting variable.
"""
def get_act(variable):
techmap = {
"BEV": "BEV",
"FCEV": "FCEV",
"Gases": "ICEV-g",
"Hybrid Electric": "PHEV-p",
"Hybrid Liquids": "HEV-p",
"Liquids": "ICEV-p"
techmap = {
"BEV": "BEV",
"FCEV": "FCEV",
"Gases": "ICEV-g",
"Hybrid Electric": {
"diesel": "PHEV-d",
"petrol": "PHEV-p"
},
"Hybrid Liquids": {
"diesel": "HEV-d",
"petrol": "HEV-p"
},
"Liquids": {
"diesel": "ICEV-d",
"petrol": "ICEV-p"
}
return Activity(
Act.get((Act.name.startswith(
"Passenger car, fleet average, {}, {}".format(
variable.split("|")[-1], year)
))
& (Act.location == region)
& (Act.database == db.name)))

return {
get_act(variable): scale
}
tech = variable.split("|")[-1]
liq_share = {
"diesel": 0.4,
"petrol": 0.6
}
if tech in ["Hybrid Electric", "Hybrid Liquids", "Liquids"]:
if region in ["CHA", "REF", "IND"]:
demand = {
Activity(Act.get(
(Act.name == "transport, passenger car, fleet average, {}, {}".format(
techmap[tech]["petrol"], year))
& (Act.location == region)
& (Act.database == db.name))): scale
}
else:
demand = {
Activity(Act.get(
(Act.name == "transport, passenger car, fleet average, {}, {}".format(
techmap[tech][liq], year))
& (Act.location == region)
& (Act.database == db.name))): scale * liq_share[liq]
for liq in ["diesel", "petrol"]
}
return demand
else:
return {
Activity(
Act.get(
(Act.name == "transport, passenger car, fleet average, {}, {}".format(
techmap[tech], year))
& (Act.location == region)
& (Act.database == db.name))): scale
}

def report_LDV_LCA(self):
"""
Expand All @@ -134,12 +168,13 @@ def report_LDV_LCA(self):

df.set_index(["Year", "Region", "Variable", "Method"], inplace=True)
start = time.time()

# calc score
for year in self.years:
# find activities which at the moment do not depend
# on regions
db = bw.Database(eidb_label(self.scenario, year))
for region in df.index.get_level_values(1).unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
for var in (df.loc[(year, region)]
.index.get_level_values(0)
.unique()):
Expand Down Expand Up @@ -168,15 +203,15 @@ def _get_material_bioflows_for_bev(self):
method = ('ILCD 2.0 2018 midpoint',
'resources', 'minerals and metals')
year = self.years[0]
act_str = "Passenger car, fleet average, BEV, {}".format(year)
act_str = "transport, passenger car, fleet average, BEV, {}".format(year)

# upstream material demands are the same for all regions
# so we can use GLO here
act = Activity(
Act.get((Act.name == act_str)
& (Act.database == eidb_label(
self.scenario, year))
& (Act.location == "RER")))
self.model, self.scenario, year))
& (Act.location == "EUR")))
lca = bw.LCA({act: 1}, method=method)
lca.lci()
lca.lcia()
Expand All @@ -199,12 +234,13 @@ def report_materials(self):
df = self.data[self.data.Variable.isin(self.variables)]

df.set_index(["Year", "Region", "Variable"], inplace=True)

start = time.time()
result = {}
# calc score
for year in self.years:
db = bw.Database(eidb_label(self.scenario, year))
for region in df.index.get_level_values(1).unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
# create large lca demand object
demand = [
self._act_from_variable(
Expand Down Expand Up @@ -242,12 +278,13 @@ def report_direct_emissions(self):
df = self.data[self.data.Variable.isin(self.variables)]

df.set_index(["Year", "Region", "Variable"], inplace=True)

start = time.time()
result = {}
# calc score
for year in self.years:
db = bw.Database(eidb_label(self.scenario, year))
for region in df.index.get_level_values(1).unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
for var in (df.loc[(year, region)]
.index.get_level_values(0)
.unique()):
Expand Down Expand Up @@ -282,8 +319,8 @@ def report_endpoint(self):
result = {}
# calc score
for year in self.years:
db = bw.Database(eidb_label(self.scenario, year))
for region in df.index.get_level_values(1).unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
# create large lca demand object
demand = [
self._act_from_variable(
Expand Down Expand Up @@ -326,8 +363,8 @@ def report_midpoint(self):
result = {}
# calc score
for year in self.years:
db = bw.Database(eidb_label(self.scenario, year))
for region in df.index.get_level_values(1).unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
# create large lca demand object
demand = [
self._act_from_variable(
Expand Down Expand Up @@ -376,8 +413,8 @@ def report_midpoint_to_endpoint(self):
result = {}
# calc score
for year in self.years:
db = bw.Database(eidb_label(self.scenario, year))
for region in df.index.get_level_values(1).unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
# create large lca demand object
demand = [
self._act_from_variable(
Expand Down Expand Up @@ -483,8 +520,8 @@ def _sum_variables_and_add_scores(self, market, variables):

# calc score
for year in self.years:
db = bw.Database(eidb_label(self.scenario, year))
for region in df.Region.unique():
db = bw.Database(eidb_label(self.model, self.scenario, year))
for region in self.regions:
# import ipdb;ipdb.set_trace()
# find activity
act = [a for a in db if a["name"] == market and
Expand Down Expand Up @@ -521,14 +558,13 @@ def report_tech_LCA(self, year):

db = bw.Database("_".join(["ecoinvent", self.scenario, str(year)]))

regions = self._get_rmnd_regions()
result = self._cartesian_product({
"region": regions,
"region": self.regions,
"tech": list(tecdict.keys()),
"method": self.methods
}).sort_index()

for region in regions:
for region in self.regions:
# read the ecoinvent techs for the entries
shares = self.supplier_shares(db, region)

Expand Down Expand Up @@ -559,13 +595,6 @@ def _cartesian_product(self, idx):
index = pd.MultiIndex.from_product(idx.values(), names=idx.keys())
return pd.DataFrame(index=index)

def _get_rmnd_regions(self):
"""Obtain a list of REMIND regions."""
regionmap = pd.read_csv(
DATA_DIR/"remind/regionmappingH12.csv",
sep=";")
return regionmap.RegionCode.unique()

def _find_suppliers(self, db, expr, locs):
"""
Return a list of supplier activites in locations `locs` matching
Expand Down Expand Up @@ -609,7 +638,7 @@ def supplier_shares(self, db, region):
vols = pd.read_csv(DATA_DIR/"electricity_production_volumes_per_tech.csv",
sep=";", index_col=["dataset", "location"])

# the filters come from the rmnd_lca package
# the filters come from the premise package
# this package is also used to modify the techs in the first place
fltrs = InventorySet(db).powerplant_filters
act_shares = {}
Expand Down
10 changes: 5 additions & 5 deletions tests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import math

from lca2rmnd.reporting import ElectricityLCAReporting, TransportLCAReporting
from rmnd_lca import InventorySet
from rmnd_lca.utils import eidb_label
from premise import InventorySet
from premise.utils import eidb_label

remind_regions = [
'LAM', 'OAS', 'SSA', 'EUR',
Expand All @@ -18,7 +18,7 @@

years = [2050]
scenario = "BAU"

model = "remind"

def test_electricity_sectoral_reporting():
rep = ElectricityLCAReporting(scenario, years)
Expand All @@ -32,7 +32,7 @@ def test_electricity_supplier_shares_random():
yr = random.choice(years)
region = random.choice(remind_regions)

db = bw.Database(eidb_label(scenario, yr))
db = bw.Database(eidb_label(model, scenario, yr))

shares = rep.supplier_shares(db, region)

Expand All @@ -48,7 +48,7 @@ def test_electricity_tech_reporting():
yr = random.choice(years)
region = random.choice(remind_regions)

db = bw.Database(eidb_label(scenario, yr))
db = bw.Database(eidb_label(model, scenario, yr))
fltrs = InventorySet(db).powerplant_filters
tech = random.choice(list(fltrs.keys()))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_selector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# content of test_activity_maps.py
from lca2rmnd.activity_select import ActivitySelector

from rmnd_lca import InventorySet, Geomap
from premise import InventorySet, Geomap
from bw2data.database import DatabaseChooser
from bw2data.backends.peewee.proxies import ActivityDataset as Act

Expand Down

0 comments on commit 02d753d

Please sign in to comment.