Skip to content

Commit

Permalink
Adding todo and fixing mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
pesap committed Aug 23, 2024
1 parent a5c35b9 commit 2e7dbc5
Showing 1 changed file with 76 additions and 75 deletions.
151 changes: 76 additions & 75 deletions src/r2x/parser/plexos.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,31 @@
"m12",
]
DEFAULT_QUERY_COLUMNS_SCHEMA = { # NOTE: Order matters
"membership_id": pl.Int64,
"parent_object_id": pl.Int32,
"parent_object_name": pl.String,
"parent_class_name": pl.String,
"child_class_name": pl.String,
"category": pl.String,
"object_id": pl.Int32,
"name": pl.String,
"property_name": pl.String,
"property_unit": pl.String,
"property_value": pl.Float32,
"band": pl.Int32,
"date_to": pl.String,
"date_from": pl.String,
"memo": pl.String,
"scenario_category": pl.String,
"scenario": pl.String,
"action": pl.String,
"data_file_tag": pl.String,
"data_file": pl.String,
"variable_tag": pl.String,
# "variable": pl.String,
"timeslice_tag": pl.String,
"timeslice": pl.String,
}
"membership_id": pl.Int64,
"parent_object_id": pl.Int32,
"parent_object_name": pl.String,
"parent_class_name": pl.String,
"child_class_name": pl.String,
"category": pl.String,
"object_id": pl.Int32,
"name": pl.String,
"property_name": pl.String,
"property_unit": pl.String,
"property_value": pl.Float32,
"band": pl.Int32,
"date_to": pl.String,
"date_from": pl.String,
"memo": pl.String,
"scenario_category": pl.String,
"scenario": pl.String,
"action": pl.String,
"data_file_tag": pl.String,
"data_file": pl.String,
"variable_tag": pl.String,
# "variable": pl.String,
"timeslice_tag": pl.String,
"timeslice": pl.String,
}
COLUMNS = [
"name",
"property_name",
Expand Down Expand Up @@ -127,6 +127,7 @@ def __init__(self, *args, xml_file: str | None = None, **kwargs) -> None:
super().__init__(*args, **kwargs)
assert self.config.run_folder
self.run_folder = Path(self.config.run_folder)
self.system = System(name=self.config.name)
self.property_map = self.config.defaults["plexos_property_map"]
self.device_map = self.config.defaults["plexos_device_map"]
self.fuel_map = self.config.defaults["plexos_fuel_map"]
Expand All @@ -138,7 +139,7 @@ def __init__(self, *args, xml_file: str | None = None, **kwargs) -> None:
if not self.fuel_map and not self.device_map and not self.device_match_string:
msg = (
"Neither `plexos_fuel_map` or `plexos_device_map` or `device_match_string` was provided. "
"To fix, provide any of the mappings."
"To fix, provide any of the mappings."
)
raise ParserError(msg)

Expand Down Expand Up @@ -264,11 +265,11 @@ def _construct_load_zones(self, default_model=LoadZone) -> None:
regions = self._get_model_data(system_regions)

region_pivot = regions.pivot( # noqa: PD010
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
for region in region_pivot.iter_rows(named=True):
valid_fields = {
k: v for k, v in region.items() if k in default_model.model_fields if v is not None
Expand All @@ -292,11 +293,11 @@ def _construct_buses(self, default_model=ACBus) -> None:
system_buses = self._get_model_data(system_buses)
buses_region = self._get_model_data(region_buses)
buses = system_buses.pivot( # noqa: PD010
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
for idx, bus in enumerate(buses.iter_rows(named=True)):
mapped_bus = {self.property_map.get(key, key): value for key, value in bus.items()}
valid_fields = {
Expand Down Expand Up @@ -330,11 +331,11 @@ def _construct_reserves(self, default_model=Reserve):
system_reserves = self._get_model_data(system_reserves)

reserve_pivot = system_reserves.pivot( # noqa: PD010
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
for reserve in reserve_pivot.iter_rows(named=True):
mapped_reserve = {self.property_map.get(key, key): value for key, value in reserve.items()}
valid_fields = {
Expand Down Expand Up @@ -378,11 +379,11 @@ def _construct_branches(self, default_model=MonitoredLine):
)
system_lines = self._get_model_data(system_lines)
lines_pivot = system_lines.pivot( # noqa: PD010
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)

lines_pivot_memberships = self.db.get_memberships(
*lines_pivot["name"].to_list(), object_class=ClassEnum.Line
Expand Down Expand Up @@ -512,17 +513,17 @@ def _construct_generators(self):
# Handle properties from plexos assuming that same property can
# appear multiple times on different bands.
property_records = generator_data[
[
"band",
"property_name",
"property_value",
"property_unit",
"data_file",
"variable",
"action",
"variable_tag",
]
].to_dicts()
[
"band",
"property_name",
"property_value",
"property_unit",
"data_file",
"variable",
"action",
"variable_tag",
]
].to_dicts()

mapped_records, multi_band_records = self._parse_property_data(property_records, generator_name)
mapped_records["name"] = generator_name
Expand Down Expand Up @@ -632,7 +633,7 @@ def _construct_batteries(self):
logger.debug("Creating battery objects")
system_batteries = self._get_model_data(
(pl.col("child_class_name") == ClassEnum.Battery.name)
& (pl.col("parent_class_name") == ClassEnum.System.name)
& (pl.col("parent_class_name") == ClassEnum.System.name)
)
required_fields = {
key: value for key, value in GenericBattery.model_fields.items() if value.is_required()
Expand All @@ -641,17 +642,17 @@ def _construct_batteries(self):
battery_name = battery_name[0]
logger.trace("Parsing battery = {}", battery_name)
property_records = battery_data[
[
"band",
"property_name",
"property_value",
"property_unit",
"data_file",
"variable",
"action",
"variable_tag",
]
].to_dicts()
[
"band",
"property_name",
"property_value",
"property_unit",
"data_file",
"variable",
"action",
"variable_tag",
]
].to_dicts()

# logger.debug("Parsing battery = {}", battery_name)
mapped_records, _ = self._parse_property_data(property_records, battery_name)
Expand Down Expand Up @@ -774,11 +775,11 @@ def _construct_interfaces(self, default_model=TransmissionInterface):
)
system_interfaces = self._get_model_data(system_interfaces_mask)
interfaces = system_interfaces.pivot( # noqa: PD010
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)
index=DEFAULT_INDEX,
columns="property_name",
values="property_value",
aggregate_function="first",
)

interface_property_map = {
v: k
Expand Down Expand Up @@ -845,7 +846,7 @@ def _construct_interfaces(self, default_model=TransmissionInterface):
return

def _select_model_name(self):
# TODO(pesap): Add fail mechanism
# TODO(pesap): Handle exception if no model name found
# https://github.com/NREL/R2X/issues/10
query = f"""
select obj.name
Expand Down

0 comments on commit 2e7dbc5

Please sign in to comment.