Skip to content

Commit

Permalink
Final retouch
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Guo <[email protected]>
  • Loading branch information
Jerry-Jinfeng-Guo committed Mar 1, 2024
1 parent 0efe1b3 commit 6addd62
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
23 changes: 10 additions & 13 deletions src/power_grid_model_io/data_stores/excel_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
from power_grid_model_io.data_stores.base_data_store import (
DICT_KEY_NUMBER,
DICT_KEY_SUBNUMBER,
LANGUAGE_EN,
LANGUAGE_NL,
VISION_EXCEL_LAN_DICT,
BaseDataStore,
)
from power_grid_model_io.data_types import LazyDataFrame, TabularData
from power_grid_model_io.utils.uuid_excel_cvtr import (
UUID2IntCvtr,
add_guid_values_to_cvtr,
get_special_key_map,
special_nodes_en,
special_nodes_nl,
update_column_names,
Expand Down Expand Up @@ -66,7 +65,8 @@ def __init__(
raise ValueError(f"{name} file should be a .xls or .xlsx file, {path.suffix} provided.")

self._header_rows: List[int] = [0]
self._languange = language
self._language = language
self._vision_excel_key_mapping = VISION_EXCEL_LAN_DICT[self._language]
self._terms_changed = terms_changed if terms_changed is not None else {}
self._uuid_cvtr = UUID2IntCvtr()

Expand Down Expand Up @@ -227,19 +227,16 @@ def _process_uuid_columns(self, data: pd.DataFrame, sheet_name: str) -> pd.DataF
first_level = data.columns.get_level_values(0)
guid_columns = first_level[first_level.str.endswith("GUID")]

sheet_key_mapping = get_special_key_map(
sheet_name=sheet_name, nodes_en=special_nodes_en, nodes_nl=special_nodes_nl
)

for guid_column in guid_columns:
nr = VISION_EXCEL_LAN_DICT[self._languange][DICT_KEY_NUMBER]
nr = VISION_EXCEL_LAN_DICT[self._language][DICT_KEY_NUMBER]
add_guid_values_to_cvtr(data, guid_column, self._uuid_cvtr)
new_column_name = guid_column.replace("GUID", nr)
if guid_column == "GUID":
if sheet_name in special_nodes_en:
new_column_name = guid_column.replace(
"GUID", VISION_EXCEL_LAN_DICT[LANGUAGE_EN][DICT_KEY_SUBNUMBER]
)
elif sheet_name in special_nodes_nl:
new_column_name = guid_column.replace(
"GUID", VISION_EXCEL_LAN_DICT[LANGUAGE_NL][DICT_KEY_SUBNUMBER]
)
if guid_column == "GUID" and sheet_key_mapping not in (None, {}):
new_column_name = guid_column.replace("GUID", sheet_key_mapping[DICT_KEY_SUBNUMBER])
guid_column_pos = first_level.tolist().index(guid_column)
try:
data.insert(guid_column_pos + 1, new_column_name, data[guid_column].apply(self._uuid_cvtr.query))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from pathlib import Path
from typing import Optional

from power_grid_model_io.data_stores.excel_file_store import LANGUAGE_EN, ExcelFileStore
from power_grid_model_io.data_stores.base_data_store import LANGUAGE_EN
from power_grid_model_io.data_stores.excel_file_store import ExcelFileStore


class VisionExcelFileStore(ExcelFileStore):
Expand Down
32 changes: 23 additions & 9 deletions src/power_grid_model_io/utils/uuid_excel_cvtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

import os
import re
from typing import Optional
from pathlib import Path
from typing import Optional, Union

import pandas as pd

Expand Down Expand Up @@ -119,7 +120,7 @@ def get_size(self) -> int:
return self._counter


def load_excel_file(file_name: str) -> pd.ExcelFile:
def load_excel_file(file_name: Union[Path, str]) -> pd.ExcelFile:
"""Load an excel file
Args:
Expand Down Expand Up @@ -155,6 +156,20 @@ def add_guid_values_to_cvtr(df: pd.DataFrame, guid_column: str, cvtr: UUID2IntCv
cvtr.add_list(df[guid_column].tolist())


def get_special_key_map(sheet_name: str, nodes_en: list[str], nodes_nl: list[str]) -> dict:
"""Get the special nodes for English and Dutch
Args:
sheet_name (str): the sheet name
mapping (dict): the mapping dictionary
"""
if sheet_name in nodes_en:
return VISION_EXCEL_LAN_DICT[LANGUAGE_EN]
if sheet_name in nodes_nl:
return VISION_EXCEL_LAN_DICT[LANGUAGE_NL]
return {}


def insert_or_update_number_column(
df: pd.DataFrame, guid_column: str, sheet_name: str, cvtr: UUID2IntCvtr, number: str
) -> None:
Expand All @@ -167,11 +182,10 @@ def insert_or_update_number_column(
number (str): "Number" or "Nummer" depending on the language
"""
new_column_name = guid_column.replace("GUID", number)
if guid_column == "GUID":
if sheet_name in special_nodes_en:
new_column_name = guid_column.replace("GUID", VISION_EXCEL_LAN_DICT[LANGUAGE_EN][DICT_KEY_SUBNUMBER])
elif sheet_name in special_nodes_nl:
new_column_name = guid_column.replace("GUID", VISION_EXCEL_LAN_DICT[LANGUAGE_NL][DICT_KEY_SUBNUMBER])
special_key_mapping = get_special_key_map(sheet_name, special_nodes_en, special_nodes_nl)

if guid_column == "GUID" and special_key_mapping not in (None, {}):
new_column_name = guid_column.replace("GUID", special_key_mapping[DICT_KEY_SUBNUMBER])
try:
df.insert(df.columns.get_loc(guid_column) + 1, new_column_name, df[guid_column].apply(cvtr.query))
except ValueError:
Expand Down Expand Up @@ -205,14 +219,14 @@ def save_df_to_excel(df: pd.DataFrame, file_name: str, sheet_name: str, i: int)


def convert_guid_vision_excel(
excel_file: str,
excel_file: Union[Path, str],
number: str = VISION_EXCEL_LAN_DICT[LANGUAGE_EN][DICT_KEY_NUMBER],
terms_changed: Optional[dict] = None,
) -> str:
"""Main entry function. Convert the GUID based Vision excel files to a number based format
Args:
excel_file (str): Vision excel file name
excel_file (Path | str): Vision excel file name
number (str): "Number" or "Nummer" depending on the language. Defaults to "Number".
terms_changed (dict): the dictionary containing the terms to be changed. Defaults to {}.
Expand Down
6 changes: 3 additions & 3 deletions tests/validation/converters/test_vision_excel_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
LANGUAGES_97 = ["en"]
VALIDATION_EN = Path(str(VALIDATION_FILE).format(language="en"))
CUSTOM_MAPPING_FILE = DATA_PATH / "vision_9_5_{language:s}.yaml"
terms_chaged = {"Grounding1": "N1", "Grounding2": "N2", "Grounding3": "N3", "Load.Behaviour": "Behaviour"}
terms_changed = {"Grounding1": "N1", "Grounding2": "N2", "Grounding3": "N3", "Load.Behaviour": "Behaviour"}


@lru_cache
Expand Down Expand Up @@ -309,9 +309,9 @@ def test_log_levels(capsys):
def test_uuid_excel_input():
source_file = Path(str(SOURCE_FILE_97).format(language=LANGUAGE_EN))
ref_file_97 = convert_guid_vision_excel(
source_file, number=VISION_EXCEL_LAN_DICT[LANGUAGE_EN][DICT_KEY_NUMBER], terms_changed=terms_chaged
excel_file=source_file, number=VISION_EXCEL_LAN_DICT[LANGUAGE_EN][DICT_KEY_NUMBER], terms_changed=terms_changed
)
data_native, _ = VisionExcelConverter(source_file, language="en", terms_changed=terms_chaged).load_input_data()
data_native, _ = VisionExcelConverter(source_file, language="en", terms_changed=terms_changed).load_input_data()
data_convtd, _ = VisionExcelConverter(source_file=ref_file_97).load_input_data()

assert len(data_native) == len(data_convtd)

0 comments on commit 6addd62

Please sign in to comment.