Skip to content

Commit

Permalink
map_from_legacy method with internal access to old name dict for base…
Browse files Browse the repository at this point in the history
… enum class.
  • Loading branch information
bnb32 committed May 29, 2024
1 parent a11bc33 commit fa71fd6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 51 deletions.
61 changes: 34 additions & 27 deletions reV/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,38 @@

from reV.version import __version__

OldSupplyCurveField = {
"sc_point_gid": "SC_POINT_GID",
"source_gids": "SOURCE_GIDS",
"sc_gid": "SC_GID",
"gid_counts": "GID_COUNTS",
"gid": "GID",
"n_gids": "N_GIDS",
"res_gids": "RES_GIDS",
"gen_gids": "GEN_GIDS",
"area_sq_km": "AREA_SQ_KM",
"latitude": "LATITUDE",
"longitude": "LONGITUDE",
"elevation": "ELEVATION",
"timezone": "TIMEZONE",
"county": "COUNTY",
"state": "STATE",
"country": "COUNTRY",
"mean_lcoe": "MEAN_LCOE",
"mean_res": "MEAN_RES",
"capacity": "CAPACITY",
"sc_row_ind": "SC_ROW_IND",
"sc_col_ind": "SC_COL_IND",
"mean_cf": "MEAN_CF",
"capital_cost": "CAPITAL_COST",
}


class FieldEnum(str, Enum):
"""Base Field enum with some mapping methods."""

_OLD_NAMES = OldSupplyCurveField

@classmethod
def map_to(cls, other):
"""Return a rename map from this enum to another."""
Expand All @@ -27,6 +55,12 @@ def map_from(cls, other):
'sc_gid': 'SC_GID') to this enum."""
return {name: cls[mem] for name, mem in other.items()}

@classmethod
def map_from_legacy(cls):
"""Return a rename map from a dictionary of name / member pairs (e.g.
'sc_gid': 'SC_GID') to this enum."""
return {name: cls[mem] for name, mem in cls._OLD_NAMES.items()}

def __str__(self):
return self.value

Expand Down Expand Up @@ -63,33 +97,6 @@ class ResourceMetaField(FieldEnum):
# match current naming conventions


OldSupplyCurveField = {
"sc_point_gid": "SC_POINT_GID",
"source_gids": "SOURCE_GIDS",
"sc_gid": "SC_GID",
"gid_counts": "GID_COUNTS",
"gid": "GID",
"n_gids": "N_GIDS",
"res_gids": "RES_GIDS",
"gen_gids": "GEN_GIDS",
"area_sq_km": "AREA_SQ_KM",
"latitude": "LATITUDE",
"longitude": "LONGITUDE",
"elevation": "ELEVATION",
"timezone": "TIMEZONE",
"county": "COUNTY",
"state": "STATE",
"country": "COUNTRY",
"mean_lcoe": "MEAN_LCOE",
"mean_res": "MEAN_RES",
"capacity": "CAPACITY",
"sc_row_ind": "SC_ROW_IND",
"sc_col_ind": "SC_COL_IND",
"mean_cf": "MEAN_CF",
"capital_cost": "CAPITAL_COST",
}


class OriginalSupplyCurveField(FieldEnum):
"""An enumerated map to supply curve summary/meta keys.
Expand Down
5 changes: 2 additions & 3 deletions tests/test_bespoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from reV.supply_curve.tech_mapping import TechMapping
from reV.utilities import (
ModuleName,
OldSupplyCurveField,
SiteDataField,
SupplyCurveField,
)
Expand Down Expand Up @@ -738,7 +737,7 @@ def test_collect_bespoke():
for fp in source_fps:
with Resource(fp) as source:
src_meta = source.meta.rename(
SupplyCurveField.map_from(OldSupplyCurveField), axis=1
SupplyCurveField.map_from_legacy(), axis=1
)
assert all(
np.isin(
Expand Down Expand Up @@ -812,7 +811,7 @@ def test_bespoke_supply_curve():
normal_path = os.path.join(TESTDATADIR, "sc_out/baseline_agg_summary.csv")
normal_sc_points = pd.read_csv(normal_path)
normal_sc_points = normal_sc_points.rename(
SupplyCurveField.map_from(OldSupplyCurveField), axis=1
SupplyCurveField.map_from_legacy(), axis=1
)

with tempfile.TemporaryDirectory() as td:
Expand Down
38 changes: 17 additions & 21 deletions tests/test_hybrids.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""reV hybrids tests.
"""
"""reV hybrids tests."""

import json
import os
import tempfile
Expand All @@ -13,7 +13,7 @@
from reV.cli import main
from reV.hybrids import HYBRID_METHODS, Hybridization
from reV.hybrids.hybrids import MERGE_COLUMN, OUTPUT_PROFILE_NAMES, HybridsData
from reV.utilities import ModuleName, SupplyCurveField, OldSupplyCurveField
from reV.utilities import ModuleName, SupplyCurveField
from reV.utilities.exceptions import FileInputError, InputError, OutputWarning

SOLAR_FPATH = os.path.join(
Expand All @@ -29,14 +29,10 @@
TESTDATADIR, "rep_profiles_out", "rep_profiles_solar_multiple.h5"
)
with Resource(SOLAR_FPATH) as res:
meta = res.meta.rename(
columns=SupplyCurveField.map_from(OldSupplyCurveField)
)
meta = res.meta.rename(columns=SupplyCurveField.map_from_legacy())
SOLAR_SCPGIDS = set(meta[SupplyCurveField.SC_POINT_GID])
with Resource(WIND_FPATH) as res:
meta = res.meta.rename(
columns=SupplyCurveField.map_from(OldSupplyCurveField)
)
meta = res.meta.rename(columns=SupplyCurveField.map_from_legacy())
WIND_SCPGIDS = set(meta[SupplyCurveField.SC_POINT_GID])


Expand All @@ -51,7 +47,7 @@ def test_hybridization_profile_output_single_resource():
)[0][0]

solar_cap = res.meta.loc[solar_idx, SupplyCurveField.CAPACITY]
solar_test_profile = res['rep_profiles_0', :, solar_idx]
solar_test_profile = res["rep_profiles_0", :, solar_idx]

weighted_solar = solar_cap * solar_test_profile

Expand All @@ -63,9 +59,9 @@ def test_hybridization_profile_output_single_resource():
hwp,
) = h.profiles.values()
h_meta = h.hybrid_meta
h_idx = np.where(
h_meta[SupplyCurveField.SC_POINT_GID] == sc_point_gid
)[0][0]
h_idx = np.where(h_meta[SupplyCurveField.SC_POINT_GID] == sc_point_gid)[0][
0
]

assert np.allclose(hp[:, h_idx], weighted_solar)
assert np.allclose(hsp[:, h_idx], weighted_solar)
Expand All @@ -84,7 +80,7 @@ def test_hybridization_profile_output_with_ratio_none():
)[0][0]

solar_cap = res.meta.loc[solar_idx, SupplyCurveField.CAPACITY]
solar_test_profile = res['rep_profiles_0', :, solar_idx]
solar_test_profile = res["rep_profiles_0", :, solar_idx]

weighted_solar = solar_cap * solar_test_profile

Expand All @@ -102,9 +98,9 @@ def test_hybridization_profile_output_with_ratio_none():
hwp,
) = h.profiles.values()
h_meta = h.hybrid_meta
h_idx = np.where(
h_meta[SupplyCurveField.SC_POINT_GID] == sc_point_gid
)[0][0]
h_idx = np.where(h_meta[SupplyCurveField.SC_POINT_GID] == sc_point_gid)[0][
0
]

assert np.allclose(hp[:, h_idx], weighted_solar)
assert np.allclose(hsp[:, h_idx], weighted_solar)
Expand All @@ -121,14 +117,14 @@ def test_hybridization_profile_output():
res.meta[SupplyCurveField.SC_POINT_GID] == common_sc_point_gid
)[0][0]
solar_cap = res.meta.loc[solar_idx, SupplyCurveField.CAPACITY]
solar_test_profile = res['rep_profiles_0', :, solar_idx]
solar_test_profile = res["rep_profiles_0", :, solar_idx]

with Resource(WIND_FPATH) as res:
wind_idx = np.where(
res.meta[SupplyCurveField.SC_POINT_GID] == common_sc_point_gid
)[0][0]
wind_cap = res.meta.loc[wind_idx, SupplyCurveField.CAPACITY]
wind_test_profile = res['rep_profiles_0', :, wind_idx]
wind_test_profile = res["rep_profiles_0", :, wind_idx]

weighted_solar = solar_cap * solar_test_profile
weighted_wind = wind_cap * wind_test_profile
Expand Down Expand Up @@ -281,7 +277,7 @@ def test_ratios_input(ratio_cols, ratio_bounds, bounds):
)

if SupplyCurveField.CAPACITY in ratio:
max_solar_capacities = h.hybrid_meta['hybrid_solar_capacity']
max_solar_capacities = h.hybrid_meta["hybrid_solar_capacity"]
max_solar_capacities = max_solar_capacities.values.reshape(1, -1)
assert np.all(
h.profiles["hybrid_solar_profile"] <= max_solar_capacities
Expand Down Expand Up @@ -834,7 +830,7 @@ def make_test_file(
meta.loc[0, SupplyCurveField.LATITUDE] = lat
lon = meta[SupplyCurveField.LATITUDE].iloc[-1]
meta.loc[0, SupplyCurveField.LATITUDE] = lon
shapes['meta'] = len(meta)
shapes["meta"] = len(meta)
for d in dset_names:
shapes[d] = (len(res.time_index[t_slice]), len(meta))

Expand Down

0 comments on commit fa71fd6

Please sign in to comment.