Skip to content

Commit 5614fab

Browse files
committed
Simplify and rename function
1 parent 6a795c7 commit 5614fab

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/muse/readers/csv.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"read_initial_market",
1414
"read_attribute_table",
1515
"read_regression_parameters",
16-
"read_csv_outputs",
16+
"read_presets",
1717
]
1818

1919
from collections.abc import Sequence
@@ -788,13 +788,13 @@ def read_regression_parameters(path: Union[str, Path]) -> xr.Dataset:
788788
return coeffs
789789

790790

791-
def read_csv_outputs(
791+
def read_presets(
792792
paths: Union[str, Path, Sequence[Union[str, Path]]],
793793
columns: str = "commodity",
794-
indices: Sequence[str] = ("RegionName", "ProcessName", "Timeslice"),
794+
indices: Sequence[str] = ("RegionName", "Timeslice"),
795795
drop: Sequence[str] = ("Unnamed: 0",),
796796
) -> xr.Dataset:
797-
"""Read standard MUSE output files for consumption or supply."""
797+
"""Read consumption or supply files for preset sectors."""
798798
from re import match
799799

800800
from muse.readers import camel_to_snake
@@ -816,12 +816,22 @@ def expand_paths(path):
816816
datas = {}
817817
for path in allfiles:
818818
data = pd.read_csv(path, low_memory=False)
819-
index_columns = [u for u in indices if u in data.columns]
819+
assert all(u in data.columns for u in indices)
820+
821+
# Legacy: drop ProcessName column and sum data (PR #448)
822+
if "ProcessName" in data.columns:
823+
data = (
824+
data.drop(columns=["ProcessName"])
825+
.groupby(list(indices))
826+
.sum()
827+
.reset_index()
828+
)
829+
820830
data = data.drop(columns=[k for k in drop if k in data.columns])
821-
data.index = pd.MultiIndex.from_arrays([data[u] for u in index_columns])
831+
data.index = pd.MultiIndex.from_arrays([data[u] for u in indices])
822832
data.index.name = "asset"
823833
data.columns.name = columns
824-
data = data.drop(columns=list(index_columns))
834+
data = data.drop(columns=list(indices))
825835

826836
reyear = match(r"\S*.(\d{4})\S*\.csv", path.name)
827837
if reyear is None:
@@ -838,7 +848,7 @@ def expand_paths(path):
838848
.sortby("year")
839849
.fillna(0)
840850
.unstack("asset")
841-
.rename({k: k.replace("Name", "").lower() for k in index_columns})
851+
.rename({k: k.replace("Name", "").lower() for k in indices})
842852
)
843853

844854
if "commodity" in result.coords:

src/muse/sectors/preset_sector.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def factory(cls, name: str, settings: Any) -> PresetSector:
2424
from muse.commodities import CommodityUsage
2525
from muse.readers import (
2626
read_attribute_table,
27-
read_csv_outputs,
2827
read_macro_drivers,
28+
read_presets,
2929
read_regression_parameters,
3030
read_timeslice_shares,
3131
read_timeslices,
@@ -40,7 +40,7 @@ def factory(cls, name: str, settings: Any) -> PresetSector:
4040
getattr(sector_conf, "timeslice_levels", None)
4141
).timeslice
4242
if getattr(sector_conf, "consumption_path", None) is not None:
43-
consumption = read_csv_outputs(sector_conf.consumption_path)
43+
consumption = read_presets(sector_conf.consumption_path)
4444
presets["consumption"] = consumption.assign_coords(timeslice=timeslice)
4545
elif getattr(sector_conf, "demand_path", None) is not None:
4646
presets["consumption"] = read_attribute_table(sector_conf.demand_path)
@@ -89,7 +89,7 @@ def factory(cls, name: str, settings: Any) -> PresetSector:
8989
)
9090

9191
if getattr(sector_conf, "supply_path", None) is not None:
92-
supply = read_csv_outputs(sector_conf.supply_path)
92+
supply = read_presets(sector_conf.supply_path)
9393
supply.coords["timeslice"] = presets.timeslice
9494
presets["supply"] = supply
9595

@@ -99,9 +99,9 @@ def factory(cls, name: str, settings: Any) -> PresetSector:
9999
getattr(sector_conf, "lcoe_path", None) is not None and "supply" in presets
100100
):
101101
costs = (
102-
read_csv_outputs(
102+
read_presets(
103103
sector_conf.lcoe_path,
104-
indices=("RegionName", "ProcessName"),
104+
indices=("RegionName",),
105105
columns="timeslices",
106106
)
107107
* presets["supply"]
@@ -131,9 +131,6 @@ def factory(cls, name: str, settings: Any) -> PresetSector:
131131
[CommodityUsage.PRODUCT if u else CommodityUsage.OTHER for u in comm_usage],
132132
)
133133
presets = presets.set_coords("comm_usage")
134-
if "process" in presets.dims:
135-
presets = presets.sum("process")
136-
137134
interpolation_mode = getattr(sector_conf, "interpolation_mode", "linear")
138135
return cls(presets, interpolation_mode=interpolation_mode, name=name)
139136

tests/test_readers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,11 +673,11 @@ def test_read_attribute_table(default_model):
673673
)
674674

675675

676-
def test_read_csv_outputs(default_model):
677-
from muse.readers.csv import read_csv_outputs
676+
def test_read_presets(default_model):
677+
from muse.readers.csv import read_presets
678678

679679
path = default_model / "technodata" / "preset" / "*Consumption.csv"
680-
data = read_csv_outputs(str(path))
680+
data = read_presets(str(path))
681681

682682
assert isinstance(data, xr.DataArray)
683683
assert data.dtype == np.float64

0 commit comments

Comments
 (0)