Skip to content

Commit

Permalink
Processed comments
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
  • Loading branch information
Jerry-Jinfeng-Guo committed Mar 1, 2024
1 parent 55508e9 commit f3fbb72
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/power_grid_model_io/converters/vision_excel_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Any, Mapping, Optional, Union

from power_grid_model_io.converters.tabular_converter import TabularConverter
from power_grid_model_io.data_stores.base_data_store import LANGUAGE_EN
from power_grid_model_io.data_stores.vision_excel_file_store import VisionExcelFileStore

DEFAULT_MAPPING_FILE = Path(__file__).parent.parent / "config" / "excel" / "vision_{language:s}.yaml"
Expand All @@ -36,13 +37,13 @@ class VisionExcelConverter(TabularConverter):
def __init__(
self,
source_file: Optional[Union[Path, str]] = None,
language: str = "en",
language: str = LANGUAGE_EN,
terms_changed: Optional[dict] = None,
mapping_file: Optional[Path] = None,
mapping_file: Optional[Union[Path, str]] = None,
log_level: int = logging.INFO,
): # pylint: disable=too-many-arguments
_mapping_file = _mapping_file = (
mapping_file if mapping_file is not None else Path(str(DEFAULT_MAPPING_FILE).format(language=language))
_mapping_file = Path(
mapping_file if mapping_file is not None else str(DEFAULT_MAPPING_FILE).format(language=language)
)
if not _mapping_file.exists():
raise FileNotFoundError(f"No Vision Excel mapping available for language '{language}'")
Expand Down
15 changes: 15 additions & 0 deletions src/power_grid_model_io/data_stores/base_data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@

T = TypeVar("T")

LANGUAGE_EN = "en"
LANGUAGE_NL = "nl"
DICT_KEY_NUMBER = "key_number"
DICT_KEY_SUBNUMBER = "key_subnumber"
VISION_EXCEL_LAN_DICT = {
LANGUAGE_EN: {
DICT_KEY_NUMBER: "Number",
DICT_KEY_SUBNUMBER: "Subnumber",
},
LANGUAGE_NL: {
DICT_KEY_NUMBER: "Nummer",
DICT_KEY_SUBNUMBER: "Subnummer",
},
}


class BaseDataStore(Generic[T], ABC):
"""
Expand Down
21 changes: 16 additions & 5 deletions src/power_grid_model_io/data_stores/excel_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@

import pandas as pd

from power_grid_model_io.data_stores.base_data_store import BaseDataStore
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 (
from power_grid_model_io.utils.uuid_excel_cvtr import (
UUID2IntCvtr,
add_guid_values_to_cvtr,
special_nodes_en,
Expand Down Expand Up @@ -221,14 +228,18 @@ def _process_uuid_columns(self, data: pd.DataFrame, sheet_name: str) -> pd.DataF
guid_columns = first_level[first_level.str.endswith("GUID")]

for guid_column in guid_columns:
nr = "Number" if self._languange == "en" else "Nummer"
nr = VISION_EXCEL_LAN_DICT[self._languange][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", "Subnumber")
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", "Subnummer")
new_column_name = guid_column.replace(
"GUID", VISION_EXCEL_LAN_DICT[LANGUAGE_NL][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,7 @@
from pathlib import Path
from typing import Optional

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


class VisionExcelFileStore(ExcelFileStore):
Expand All @@ -21,7 +21,7 @@ class VisionExcelFileStore(ExcelFileStore):
def __init__(
self,
file_path: Path,
language: str = "en",
language: str = LANGUAGE_EN,
terms_changed: Optional[dict] = None,
):
"""
Expand Down
18 changes: 15 additions & 3 deletions src/power_grid_model_io/utils/uuid_excel_cvtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

import pandas as pd

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,
)

special_nodes_en = [
"Transformer loads",
"Sources",
Expand Down Expand Up @@ -162,9 +170,9 @@ def insert_or_update_number_column(
# pylint: disable=duplicate-code
if guid_column == "GUID":
if sheet_name in special_nodes_en:
new_column_name = guid_column.replace("GUID", "Subnumber")
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", "Subnummer")
new_column_name = guid_column.replace("GUID", VISION_EXCEL_LAN_DICT[LANGUAGE_NL][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 @@ -197,7 +205,11 @@ def save_df_to_excel(df: pd.DataFrame, file_name: str, sheet_name: str, i: int)
df.to_excel(writer, sheet_name=sheet_name, index=False)


def convert_guid_vision_excel(excel_file: str, number: str = "Number", terms_changed: Optional[dict] = None) -> str:
def convert_guid_vision_excel(
excel_file: 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:
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/utils/test_uuid_excel_cvtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from pathlib import Path

from power_grid_model_io.converters import VisionExcelConverter
from power_grid_model_io.data_stores.base_data_store import DICT_KEY_SUBNUMBER # pylint: disable=unused-import
from power_grid_model_io.data_stores.base_data_store import LANGUAGE_NL # pylint: disable=unused-import
from power_grid_model_io.data_stores.base_data_store import DICT_KEY_NUMBER, LANGUAGE_EN, VISION_EXCEL_LAN_DICT
from power_grid_model_io.utils.uuid_excel_cvtr import convert_guid_vision_excel

terms_chaged = {"Grounding1": "N1", "Grounding2": "N2", "Grounding3": "N3", "Load.Behaviour": "Behaviour"}
Expand All @@ -15,7 +18,9 @@


def test_convert_guid_vision_excel():
new_file = convert_guid_vision_excel(SOURCE_FILE, number="Number", terms_changed=terms_chaged)
new_file = convert_guid_vision_excel(
SOURCE_FILE, number=VISION_EXCEL_LAN_DICT[LANGUAGE_EN][DICT_KEY_NUMBER], terms_changed=terms_chaged
)
vision_cvtr_new = VisionExcelConverter(source_file=new_file)
vision_cvtr_ref = VisionExcelConverter(source_file=REFERENCE_FILE)

Expand Down
9 changes: 7 additions & 2 deletions tests/validation/converters/test_vision_excel_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from power_grid_model.data_types import SingleDataset

from power_grid_model_io.converters import VisionExcelConverter
from power_grid_model_io.data_stores.base_data_store import DICT_KEY_SUBNUMBER # pylint: disable=unused-import
from power_grid_model_io.data_stores.base_data_store import LANGUAGE_NL # pylint: disable=unused-import
from power_grid_model_io.data_stores.base_data_store import DICT_KEY_NUMBER, LANGUAGE_EN, VISION_EXCEL_LAN_DICT
from power_grid_model_io.data_types import ExtraInfo
from power_grid_model_io.utils.json import JsonEncoder
from power_grid_model_io.utils.uuid_excel_cvtr import convert_guid_vision_excel
Expand Down Expand Up @@ -304,8 +307,10 @@ def test_log_levels(capsys):


def test_uuid_excel_input():
source_file = Path(str(SOURCE_FILE_97).format(language="en"))
ref_file_97 = convert_guid_vision_excel(source_file, number="Number", terms_changed=terms_chaged)
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
)
data_native, _ = VisionExcelConverter(source_file, language="en", terms_changed=terms_chaged).load_input_data()
data_convtd, _ = VisionExcelConverter(source_file=ref_file_97).load_input_data()

Expand Down

0 comments on commit f3fbb72

Please sign in to comment.