From 348bd15c4e6060435de0f3cc19c58a820360f37c Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Tue, 18 Jun 2024 16:21:02 +0100 Subject: [PATCH 01/11] Expand test_read_trade_technodata to additional dataset attributes --- tests/test_readers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index ddf986276..b6721f690 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -1,5 +1,6 @@ from pathlib import Path +import numpy as np import toml import xarray as xr from pytest import fixture, mark, raises @@ -410,3 +411,8 @@ def test_read_trade_technodata(tmp_path): "max_capacity_growth", "total_capacity_limit", } + assert all(val == np.float64 for val in data.dtypes.values()) + assert list(data.coords["dst_region"].values) == ["R1", "R2"] + assert list(data.coords["technology"].values) == ["gassupply1"] + assert list(data.coords["region"].values) == ["R1", "R2", "R3"] + assert all(var.coords.equals(data.coords) for var in data.data_vars.values()) From ff002b4feb2cbab2d4e3f806be3dcaa7a34a6ffd Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Tue, 18 Jun 2024 16:46:58 +0100 Subject: [PATCH 02/11] Expand test_read_existing_trade --- tests/test_readers.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index b6721f690..e35f19d5d 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -390,6 +390,10 @@ def test_read_existing_trade(tmp_path): assert isinstance(data, xr.DataArray) assert set(data.dims) == {"year", "technology", "dst_region", "region"} + assert list(data.coords["year"].values) == [2010, 2020, 2030, 2040, 2050] + assert list(data.coords["technology"].values) == ["gassupply1"] + assert list(data.coords["dst_region"].values) == ["R1", "R2"] + assert list(data.coords["region"].values) == ["R1", "R2"] def test_read_trade_technodata(tmp_path): From 9ca8fb442f9b6333c86fdcaee7fd75a838e287ff Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 14:59:29 +0100 Subject: [PATCH 03/11] Add test_read_technodictionary --- tests/test_readers.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index e35f19d5d..834749b5c 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -420,3 +420,51 @@ def test_read_trade_technodata(tmp_path): assert list(data.coords["technology"].values) == ["gassupply1"] assert list(data.coords["region"].values) == ["R1", "R2", "R3"] assert all(var.coords.equals(data.coords) for var in data.data_vars.values()) + + +@fixture +def default_model(tmp_path): + from muse.examples import copy_model + + copy_model("default", tmp_path) + return tmp_path / "model" + + +def test_read_technodictionary(default_model): + from muse.readers.csv import read_technodictionary + + path = default_model / "technodata" / "residential" / "Technodata.csv" + data = read_technodictionary(path) + assert isinstance(data, xr.Dataset) + assert set(data.dims) == {"technology", "region"} + + assert dict(data.dtypes) == dict( + level=np.dtype("O"), + cap_par=np.dtype("float64"), + cap_exp=np.dtype("int64"), + fix_par=np.dtype("int64"), + fix_exp=np.dtype("int64"), + var_par=np.dtype("int64"), + interest_rate=np.dtype("float64"), + type=np.dtype("O"), + fuel=np.dtype(" Date: Mon, 24 Jun 2024 15:01:33 +0100 Subject: [PATCH 04/11] Add test_read_technodata_timeslices --- tests/test_readers.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index 834749b5c..dec37ffa1 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -468,3 +468,39 @@ def test_read_technodictionary(default_model): assert list(data.data_vars[var].coords) == ["technology"] else: assert data.data_vars[var].coords.equals(data.coords) + + +def test_read_technodata_timeslices(tmp_path): + from muse.examples import copy_model + from muse.readers.csv import read_technodata_timeslices + + copy_model("default_timeslice", tmp_path) + path = tmp_path / "model" / "technodata" / "power" / "TechnodataTimeslices.csv" + data = read_technodata_timeslices(path) + + assert isinstance(data, xr.Dataset) + assert set(data.dims) == {"technology", "region", "year", "timeslice"} + assert dict(data.dtypes) == dict( + utilization_factor=np.int64, + minimum_service_factor=np.int64, + ) + assert list(data.coords["technology"].values) == ["gasCCGT", "windturbine"] + assert list(data.coords["region"].values) == ["R1"] + assert list(data.coords["year"].values) == [2020] + month_values = ["all-year"] * 6 + day_values = ["all-week"] * 6 + hour_values = [ + "afternoon", + "early-peak", + "evening", + "late-peak", + "morning", + "night", + ] + + assert list(data.coords["timeslice"].values) == list( + zip(month_values, day_values, hour_values) + ) + assert list(data.coords["month"]) == month_values + assert list(data.coords["day"]) == day_values + assert list(data.coords["hour"]) == hour_values From 74526f7a3d29390c860eed926dd6a5767bfbc9bc Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:01:46 +0100 Subject: [PATCH 05/11] Add test_read_io_technodata --- tests/test_readers.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index dec37ffa1..be2aebfc2 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -504,3 +504,30 @@ def test_read_technodata_timeslices(tmp_path): assert list(data.coords["month"]) == month_values assert list(data.coords["day"]) == day_values assert list(data.coords["hour"]) == hour_values + + +def test_read_io_technodata(default_model): + from muse.readers.csv import read_io_technodata + + path = default_model / "technodata" / "residential" / "CommOut.csv" + data = read_io_technodata(path) + + assert isinstance(data, xr.Dataset) + assert set(data.dims) == {"technology", "region", "year", "commodity"} + assert dict(data.dtypes) == dict( + fixed=np.float64, flexible=np.float64, commodity_units=np.dtype("O") + ) + assert list(data.coords["technology"].values) == ["gasboiler", "heatpump"] + assert list(data.coords["region"].values) == ["R1"] + assert list(data.coords["year"].values) == [2020] + assert list(data.coords["commodity"].values) == [ + "electricity", + "gas", + "heat", + "CO2f", + "wind", + ] + + assert data.data_vars["fixed"].coords.equals(data.coords) + assert data.data_vars["flexible"].coords.equals(data.coords) + assert list(data.data_vars["commodity_units"].coords) == ["commodity"] From 9f16f92fde34092794c9e876fba4cb546ed233f2 Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:01:59 +0100 Subject: [PATCH 06/11] Add test_read_initial_assets --- tests/test_readers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index be2aebfc2..66bcfedad 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -531,3 +531,19 @@ def test_read_io_technodata(default_model): assert data.data_vars["fixed"].coords.equals(data.coords) assert data.data_vars["flexible"].coords.equals(data.coords) assert list(data.data_vars["commodity_units"].coords) == ["commodity"] + + +def test_read_initial_assets(default_model): + from muse.readers.csv import read_initial_assets + + path = default_model / "technodata" / "residential" / "ExistingCapacity.csv" + data = read_initial_assets(path) + + assert isinstance(data, xr.DataArray) + assert set(data.dims) == {"region", "asset", "year"} + assert data.dtype == np.int64 + + assert list(data.coords["region"].values) == ["R1"] + assert list(data.coords["technology"].values) == ["gasboiler", "heatpump"] + assert list(data.coords["installed"].values) == [2020, 2020] + assert list(data.coords["year"].values) == list(range(2020, 2055, 5)) From 3603329cb0076d1113094d44a868c55a95d53395 Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:02:10 +0100 Subject: [PATCH 07/11] Add test_glob_commodities --- tests/test_readers.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index 66bcfedad..a9da4abdd 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -547,3 +547,29 @@ def test_read_initial_assets(default_model): assert list(data.coords["technology"].values) == ["gasboiler", "heatpump"] assert list(data.coords["installed"].values) == [2020, 2020] assert list(data.coords["year"].values) == list(range(2020, 2055, 5)) + + +def test_global_commodities(default_model): + from muse.readers.csv import read_global_commodities + + path = default_model / "input" / "GlobalCommodities.csv" + data = read_global_commodities(path) + + assert isinstance(data, xr.Dataset) + assert set(data.dims) == {"commodity"} + assert dict(data.dtypes) == dict( + comm_name=np.dtype("O"), + comm_type=np.dtype("O"), + emmission_factor=np.float64, + heat_rate=np.int64, + unit=np.dtype("O"), + ) + + assert list(data.coords["commodity"].values) == [ + "electricity", + "gas", + "heat", + "wind", + "CO2f", + ] + assert all(var.coords.equals(data.coords) for var in data.data_vars.values()) From eff7af7bff19210431c5f517f0650228c7a10cc3 Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:02:22 +0100 Subject: [PATCH 08/11] Add test_read_csv_agent_parameters --- tests/test_readers.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index a9da4abdd..56831e9f0 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -573,3 +573,37 @@ def test_global_commodities(default_model): "CO2f", ] assert all(var.coords.equals(data.coords) for var in data.data_vars.values()) + + +def test_read_csv_agent_parameters(default_model): + from muse.readers.csv import read_csv_agent_parameters + + path = default_model / "technodata" / "Agents.csv" + data = read_csv_agent_parameters(path) + + assert data == [ + { + "name": "A1", + "region": "R1", + "objectives": ["LCOE"], + "search_rules": "all", + "decision": {"name": "singleObj", "parameters": [("LCOE", True, 1)]}, + "agent_type": "newcapa", + "quantity": 1, + "maturity_threshhold": -1, + "spend_limit": np.inf, + "share": "agent_share_1", + }, + { + "name": "A1", + "region": "R1", + "objectives": ["LCOE"], + "search_rules": "all", + "decision": {"name": "singleObj", "parameters": [("LCOE", True, 1)]}, + "agent_type": "retrofit", + "quantity": 1, + "maturity_threshhold": -1, + "spend_limit": np.inf, + "share": "agent_share_2", + }, + ] From 3a8ced99107441e8f01a87dc88b2cd16c1abb730 Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:02:41 +0100 Subject: [PATCH 09/11] Add test_read_initial_market --- tests/test_readers.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index 56831e9f0..e2bc08d93 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -607,3 +607,53 @@ def test_read_csv_agent_parameters(default_model): "share": "agent_share_2", }, ] + + +def test_read_initial_market(default_model): + from muse.readers.csv import read_initial_market + from muse.readers.toml import read_settings + + settings = read_settings(default_model / "settings.toml") + path = default_model / "input" / "Projections.csv" + data = read_initial_market(path, timeslices=settings.timeslices) + + assert isinstance(data, xr.Dataset) + assert set(data.dims) == {"region", "year", "commodity", "timeslice"} + assert dict(data.dtypes) == dict( + prices=np.float64, + exports=np.float64, + imports=np.float64, + static_trade=np.float64, + ) + assert list(data.coords["region"].values) == ["R1"] + assert list(data.coords["year"].values) == list(range(2010, 2105, 5)) + assert list(data.coords["commodity"].values) == [ + "electricity", + "gas", + "heat", + "CO2f", + "wind", + ] + assert ( + list(data.coords["units_prices"].values) + == ["MUS$2010/PJ"] * 3 + ["MUS$2010/kt"] * 2 + ) + month_values = ["all-year"] * 6 + day_values = ["all-week"] * 6 + hour_values = [ + "night", + "morning", + "afternoon", + "early-peak", + "late-peak", + "evening", + ] + + assert list(data.coords["timeslice"].values) == list( + zip(month_values, day_values, hour_values) + ) + assert list(data.coords["month"]) == month_values + assert list(data.coords["day"]) == day_values + assert list(data.coords["hour"]) == hour_values + + assert all(var.coords.equals(data.coords) for var in data.data_vars.values()) From ec1e1d7adb91cd1126046245196232e97bd432be Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:02:59 +0100 Subject: [PATCH 10/11] Add test_read_attribute_table --- tests/test_readers.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index e2bc08d93..be59c2b5e 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -657,3 +657,28 @@ def test_read_initial_market(default_model): assert list(data.coords["hour"]) == hour_values assert all(var.coords.equals(data.coords) for var in data.data_vars.values()) + + +def test_read_attribute_table(default_model): + from muse.readers.csv import read_attribute_table + + path = default_model / "input" / "Projections.csv" + data = read_attribute_table(path) + + assert isinstance(data, xr.DataArray) + assert data.dtype == np.float64 + + assert set(data.dims) == {"region", "year", "commodity"} + assert list(data.coords["region"].values) == ["R1"] + assert list(data.coords["year"].values) == list(range(2010, 2105, 5)) + assert list(data.coords["commodity"].values) == [ + "electricity", + "gas", + "heat", + "CO2f", + "wind", + ] + assert ( + list(data.coords["units_commodity_price"].values) + == ["MUS$2010/PJ"] * 3 + ["MUS$2010/kt"] * 2 + ) From 88b82853d267f9bc2f0d2f4f79d78133eb2b9787 Mon Sep 17 00:00:00 2001 From: Christopher Cave-Ayland Date: Mon, 24 Jun 2024 15:03:14 +0100 Subject: [PATCH 11/11] Add test_read_csv_outputs --- tests/test_readers.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_readers.py b/tests/test_readers.py index be59c2b5e..e8563fc32 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -682,3 +682,26 @@ def test_read_attribute_table(default_model): list(data.coords["units_commodity_price"].values) == ["MUS$2010/PJ"] * 3 + ["MUS$2010/kt"] * 2 ) + + +def test_read_csv_outputs(default_model): + from muse.readers.csv import read_csv_outputs + + path = default_model / "technodata" / "preset" / "*Consumption.csv" + data = read_csv_outputs(str(path)) + + assert isinstance(data, xr.DataArray) + assert data.dtype == np.float64 + + assert set(data.dims) == {"year", "commodity", "region", "process", "timeslice"} + assert list(data.coords["region"].values) == ["R1"] + assert list(data.coords["process"].values) == ["gasboiler"] + assert list(data.coords["timeslice"].values) == list(range(1, 7)) + assert list(data.coords["year"].values) == [2020, 2050] + assert list(data.coords["commodity"].values) == [ + "electricity", + "gas", + "heat", + "CO2f", + "wind", + ]