Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the nexus module #106

Merged
merged 57 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
304c042
Update water data
awais307 Jun 19, 2023
fc068b2
Migrate nexus test script from message_data
awais307 Jun 19, 2023
cec5340
add tests (incomplete) for water
awais307 Jul 26, 2023
8d87991
Migrate nexus test script from message_data
awais307 Jun 19, 2023
27096b9
add tests (incomplete) for water
awais307 Jul 26, 2023
502bf12
Apply code formatting
awais307 Sep 27, 2023
9f98a93
Apply code formatting
awais307 Sep 29, 2023
61e8ba2
Fix module import in tests
awais307 Nov 16, 2023
a533dcb
Fix module imports in nexus test
awais307 Nov 16, 2023
f82bc44
Adjust imports in tests
awais307 Nov 16, 2023
ebe5a22
Fix import of used modules in tests
awais307 Nov 16, 2023
430bad0
Apply ruff formatting
awais307 Nov 16, 2023
fd084af
Apply missing black code formatting
awais307 Nov 16, 2023
297d013
Rework a sample test for the water module
glatterf42 Nov 29, 2023
7d156a6
add revisions to testing
awais307 Feb 2, 2024
e00c20a
adjust expected result in yv_ya test
awais307 Feb 2, 2024
b3c05dd
apply ruff for code quality
awais307 Feb 2, 2024
1052190
Improve code quality and start addressing test failures
glatterf42 Feb 27, 2024
d2ead23
Use different quotation mark inside f-string
glatterf42 Feb 27, 2024
a5d1c24
confirming test_map_yv_ya_lt
adrivinca Mar 18, 2024
c13213a
Changing all .append() with pc.concat([]), plus small changes in inde…
adrivinca Mar 18, 2024
23cbcdc
adding HAS_MESSAGE_DATA to call the legacy reporting
adrivinca Mar 18, 2024
623c321
Comply with reduced complexity limit
glatterf42 Apr 2, 2024
1f684c1
Add test_build
awais307 Apr 7, 2024
bcf1f76
Fix typo in file name
glatterf42 Apr 8, 2024
d4d9b2b
Use new local legacy report version
glatterf42 Apr 8, 2024
c325cbb
Apply ruff formatting
glatterf42 Apr 8, 2024
2ab17bd
Import Context not just for type-checking
glatterf42 Apr 8, 2024
ee9ff04
Fix sets required for tests
glatterf42 Apr 8, 2024
abe3cdb
Add FIXME re:rendering of docstring
glatterf42 Apr 8, 2024
acf9af5
Adjust for pandas CoW
glatterf42 Apr 8, 2024
701dd83
Use test_context properly
glatterf42 Apr 8, 2024
a755cde
Add information for currently failing test
glatterf42 Apr 8, 2024
e402916
Mark minimum version for map_basin_region_wat and all dependent tests
glatterf42 Apr 8, 2024
db124a7
Test on actual file, not monkeypatch
glatterf42 Apr 11, 2024
35d50de
Add superficial tests for infrastructure
glatterf42 Apr 11, 2024
3f17571
Add superficial tests for water/reporting
glatterf42 Apr 11, 2024
b3e6c6f
Rename water/reporting to water/report for consistency
glatterf42 Apr 11, 2024
d1e025c
Remove superfluous comments
glatterf42 Apr 11, 2024
70e8fd8
Use test_context directly
glatterf42 Apr 11, 2024
c67b4a9
Migrate water/data tests to appropriate location
glatterf42 Apr 11, 2024
d928088
Use same name for test as for code module
glatterf42 Apr 11, 2024
8627f74
Add type hints, structure docstrings
glatterf42 Apr 11, 2024
229f530
Add superficial tests for water/data/demands
glatterf42 Apr 11, 2024
1375083
Fixes for texts in demand and cool_tec
adrivinca May 28, 2024
1bfb186
Cut length of comments
adrivinca May 28, 2024
18b3350
Remove test exclusions for now-tested modules
glatterf42 Jun 14, 2024
49fbcf8
Run add_sectoral_demands only for versions that use a fitting pandas …
glatterf42 Jun 14, 2024
3109995
Assign only requested columns
glatterf42 Jun 14, 2024
693b07d
Update data file generated automatically through tests
glatterf42 Jun 14, 2024
51b9362
Fix column names in helper function
glatterf42 Jun 14, 2024
ffd44f5
Address various mypy complaints
glatterf42 Aug 22, 2024
2cae567
Resolve errors from mypy fixes
glatterf42 Aug 22, 2024
5f69016
marking xfail for test_build and report as they required full scenarios
adrivinca Sep 10, 2024
db297cf
Handle KeyError in cooling_fr to avoid minimum version tag
glatterf42 Sep 10, 2024
a8d9136
Apply minimum version to cool_tec
glatterf42 Sep 10, 2024
5b616f0
Updating whatsnew
adrivinca Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
10 changes: 6 additions & 4 deletions message_ix_models/data/water/set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ nexus:
- all
add:
- Mf # efficient mode
- M1 # required for bare_res test

technology:
remove:
Expand Down Expand Up @@ -347,7 +348,7 @@ nexus:

map_tec_addon:
add:
- [bio_hpl,cooling__bio_hpl]
- [bio_hpl, cooling__bio_hpl]
- [bio_istig, cooling__bio_istig]
- [bio_istig_ccs, cooling__bio_istig_ccs]
- [bio_ppl,cooling__bio_ppl]
Expand Down Expand Up @@ -478,15 +479,16 @@ nexus:
- share_low_lim_GWat_share
- share_wat_recycle_total
- share_wat_recycle_share
- water_resource_extraction # required for bare_res test

cat_tec:
add:
- [share_low_lim_GWat_total, extract_surfacewater]
- [share_low_lim_GWat_total, extract_groundwater]
- [share_low_lim_GWat_share, extract_groundwater]
- [share_wat_recycle_total,urban_recycle]
- [share_wat_recycle_total,urban_discharge]
- [share_wat_recycle_share,urban_recycle]
- [share_wat_recycle_total, urban_recycle]
- [share_wat_recycle_total, urban_discharge]
- [share_wat_recycle_share, urban_recycle]
- [water_distribution, urban_t_d]
- [water_distribution, rural_t_d]
- [water_distribution, urban_unconnected]
Expand Down
3 changes: 2 additions & 1 deletion message_ix_models/model/water/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .data import demands, water_supply
from .utils import read_config

__all__ = ["read_config"]
__all__ = ["demands", "read_config", "water_supply"]
25 changes: 12 additions & 13 deletions message_ix_models/model/water/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd
from sdmx.model.v21 import Code

from message_ix_models import ScenarioInfo
from message_ix_models import Context, ScenarioInfo
from message_ix_models.model import build
from message_ix_models.model.structure import get_codes
from message_ix_models.util import package_data_path
Expand All @@ -15,7 +15,7 @@
log = logging.getLogger(__name__)


def get_spec(context) -> Mapping[str, ScenarioInfo]:
def get_spec(context: Context) -> Mapping[str, ScenarioInfo]:
"""Return the specification for nexus implementation

Parameters
Expand Down Expand Up @@ -94,7 +94,7 @@ def get_spec(context) -> Mapping[str, ScenarioInfo]:

df_share2 = pd.DataFrame(data=d2)

df_share = df_share.append(df_share2)
df_share = pd.concat([df_share, df_share2])
df_list = df_share.values.tolist()

results["map_shares_commodity_total"] = df_list
Expand Down Expand Up @@ -144,7 +144,7 @@ def get_spec(context) -> Mapping[str, ScenarioInfo]:

df_share2 = pd.DataFrame(data=d2)

df_share = df_share.append(df_share2)
df_share = pd.concat([df_share, df_share2])
df_list = df_share.values.tolist()

results["map_shares_commodity_total"] = df_list
Expand All @@ -171,8 +171,6 @@ def get_spec(context) -> Mapping[str, ScenarioInfo]:

return dict(require=require, remove=remove, add=add)

return dict(require=require, remove=remove, add=add)


@lru_cache()
def generate_set_elements(set_name, match=None):
Expand All @@ -190,7 +188,7 @@ def generate_set_elements(set_name, match=None):
return results


def map_basin(context) -> Mapping[str, ScenarioInfo]:
def map_basin(context: Context) -> Mapping[str, ScenarioInfo]:
"""Return specification for mapping basins to regions

The basins are spatially consolidated from HydroSHEDS basins delineation
Expand All @@ -217,10 +215,11 @@ def map_basin(context) -> Mapping[str, ScenarioInfo]:
# Assigning proper nomenclature
df["node"] = "B" + df["BCU_name"].astype(str)
df["mode"] = "M" + df["BCU_name"].astype(str)
if context.type_reg == "country":
df["region"] = context.map_ISO_c[context.regions]
else:
df["region"] = f"{context.regions}_" + df["REGION"].astype(str)
df["region"] = (
context.map_ISO_c[context.regions]
if context.type_reg == "country"
else f"{context.regions}_" + df["REGION"].astype(str)
)

results["node"] = df["node"]
results["mode"] = df["mode"]
Expand All @@ -236,13 +235,13 @@ def map_basin(context) -> Mapping[str, ScenarioInfo]:
context.all_nodes = df["node"]

for set_name, config in results.items():
# Sets to add
# Sets to add
add.set[set_name].extend(config)

return dict(require=require, remove=remove, add=add)


def main(context, scenario, **options):
def main(context: Context, scenario, **options):
"""Set up MESSAGEix-Nexus on `scenario`.

See also
Expand Down
19 changes: 10 additions & 9 deletions message_ix_models/model/water/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import click

from message_ix_models import Context
from message_ix_models.model.structure import get_codes
from message_ix_models.util.click import common_params

Expand All @@ -13,12 +14,12 @@
@common_params("regions")
@click.option("--time", help="Manually defined time")
@click.pass_obj
def cli(context, regions, time):
def cli(context: "Context", regions, time):
"""MESSAGEix-Water and Nexus variant."""
water_ini(context, regions, time)


def water_ini(context, regions, time):
def water_ini(context: "Context", regions, time):
"""Add components of the MESSAGEix-Nexus module

This function modifies model name & scenario name
Expand Down Expand Up @@ -61,7 +62,7 @@
context.regions = regions

# create a mapping ISO code :
# region name, for other scripts
# a region name, for other scripts
# only needed for 1-country models
nodes = get_codes(f"node/{context.regions}")
nodes = list(map(str, nodes[nodes.index("World")].child))
Expand Down Expand Up @@ -106,7 +107,7 @@
help="Defines whether the model solves with macro",
)
@common_params("regions")
def nexus_cli(context, regions, rcps, sdgs, rels, macro=False):
def nexus_cli(context: "Context", regions, rcps, sdgs, rels, macro=False):
"""
Add basin structure connected to the energy sector and
water balance linking different water demands to supply.
Expand All @@ -115,7 +116,7 @@
nexus(context, regions, rcps, sdgs, rels, macro)


def nexus(context, regions, rcps, sdgs, rels, macro=False):
def nexus(context: "Context", regions, rcps, sdgs, rels, macro=False):
"""Add basin structure connected to the energy sector and
water balance linking different water demands to supply.

Expand Down Expand Up @@ -259,7 +260,7 @@
help="Default running legacy and water (full) otherwise only water, if specified",
)
@common_params("output_model")
def report_cli(context, output_model, sdgs, water=False):
def report_cli(context: "Context", output_model, sdgs, water=False):
"""function to run the water report_full from cli to the
scenario defined by the user with --url

Expand All @@ -272,13 +273,13 @@
SDG : Str
Defines if and what water SDG measures are activated
"""
reg = context.regions
reg = context.model.regions

Check warning on line 276 in message_ix_models/model/water/cli.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/model/water/cli.py#L276

Added line #L276 was not covered by tests
sc = context.get_scenario()
if water:
from message_ix_models.model.water.reporting import report
from message_ix_models.model.water.report import report

Check warning on line 279 in message_ix_models/model/water/cli.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/model/water/cli.py#L279

Added line #L279 was not covered by tests

report(sc, reg, sdgs)
else:
from message_ix_models.model.water.reporting import report_full
from message_ix_models.model.water.report import report_full

Check warning on line 283 in message_ix_models/model/water/cli.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/model/water/cli.py#L283

Added line #L283 was not covered by tests

report_full(sc, reg, sdgs)
6 changes: 5 additions & 1 deletion message_ix_models/model/water/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Generate input data."""

import logging
from typing import TYPE_CHECKING

from message_ix_models import ScenarioInfo
from message_ix_models.util import add_par_data
Expand All @@ -11,6 +12,9 @@
from .water_for_ppl import cool_tech, non_cooling_tec
from .water_supply import add_e_flow, add_water_supply

if TYPE_CHECKING:
from message_ix_models import Context

log = logging.getLogger(__name__)

DATA_FUNCTIONS = [
Expand Down Expand Up @@ -40,7 +44,7 @@
]


def add_data(scenario, context, dry_run=False):
def add_data(scenario, context: "Context", dry_run=False):
"""Populate `scenario` with MESSAGEix-Nexus data."""

info = ScenarioInfo(scenario)
Expand Down
Loading
Loading