Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/converters/vision_converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ Vision introduced UUID based identifier system since version 9.7. It is implemen

An examplery usage can be found in the example notebook as well as in the test cases.

## Optional extra columns

When working with Vision Excel exports, some metadata columns (like `GUID` or `StationID`) may not always be present, especially in partial exports. The `optional_extra` feature allows you to specify columns that should be included in `extra_info` if present, but won't cause conversion failure if missing.

**Syntax:**
```yaml
grid:
Transformers:
transformer:
id:
auto_id:
key: Number
# ... other fields ...
extra:
- ID # Required - fails if missing
- Name # Required - fails if missing
- optional_extra:
Comment on lines +68 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if an element is specified in both the required and optional?

      extra:
        - ID            # Required - fails if missing
        - optional_extra:
          - ID          # Optional - skipped if missing

I believe that the default should be that required precedes optional, so maybe we need to add an explicit test case for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the behaviour previously when specifying

extra:
  - ID
  - ID

? Did it append a separate column or did it combine the two?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one would appear. In this case with the second ID it won't do anything since it already exists.

Copy link
Member

@mgovers mgovers Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case, it may be possible to achieve the same result (namely that the required column is always leading) with less new code. Note that that does mean that the optional_extra needs to be parsed after the regular extra things to ensure this works:

extra:
  - optional_extra:
    - ID
  - ID

in addition, the following two should also be equivalent or the first one should be explicitly rejected:

extra:
  - optional_extra:
    - ID
  - optional_extra:
    - GUID
extra:
  - optional_extra:
    - ID
    - GUID

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- GUID # Optional - skipped if missing
- StationID # Optional - skipped if missing
```

**Behavior:**
- Required columns (listed directly under `extra`) will cause a KeyError if missing
- Optional columns (nested under `optional_extra`) are silently skipped if not found
- If some optional columns are present and others missing, only the present ones are included in `extra_info`
- This feature is particularly useful for handling different Vision export configurations or versions

## Common/Known issues related to Vision
So far we have the following issue known to us related to Vision exported spread sheets. We provide a solution from user perspective to the best of our knowledge.

Expand Down
42 changes: 39 additions & 3 deletions src/power_grid_model_io/converters/tabular_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def _parse_col_def( # pylint: disable = too-many-arguments,too-many-positional-
col_def: Any,
table_mask: Optional[np.ndarray],
extra_info: Optional[ExtraInfo],
allow_missing: bool = False,
) -> pd.DataFrame:
"""Interpret the column definition and extract/convert/create the data as a pandas DataFrame.

Expand All @@ -404,15 +405,27 @@ def _parse_col_def( # pylint: disable = too-many-arguments,too-many-positional-
table: str:
col_def: Any:
extra_info: Optional[ExtraInfo]:
allow_missing: bool: If True, missing columns will return empty DataFrame instead of raising KeyError

Returns:

"""
if isinstance(col_def, (int, float)):
return self._parse_col_def_const(data=data, table=table, col_def=col_def, table_mask=table_mask)
if isinstance(col_def, str):
return self._parse_col_def_column_name(data=data, table=table, col_def=col_def, table_mask=table_mask)
return self._parse_col_def_column_name(
data=data, table=table, col_def=col_def, table_mask=table_mask, allow_missing=allow_missing
)
if isinstance(col_def, dict):
# Check if this is an optional_extra wrapper
if len(col_def) == 1 and "optional_extra" in col_def:
# Extract the list of optional columns and parse as composite with allow_missing=True
optional_cols = col_def["optional_extra"]
if not isinstance(optional_cols, list):
raise TypeError(f"optional_extra value must be a list, got {type(optional_cols).__name__}")
return self._parse_col_def_composite(
data=data, table=table, col_def=optional_cols, table_mask=table_mask, allow_missing=True
)
return self._parse_col_def_filter(
data=data,
table=table,
Expand All @@ -421,7 +434,9 @@ def _parse_col_def( # pylint: disable = too-many-arguments,too-many-positional-
extra_info=extra_info,
)
if isinstance(col_def, list):
return self._parse_col_def_composite(data=data, table=table, col_def=col_def, table_mask=table_mask)
return self._parse_col_def_composite(
data=data, table=table, col_def=col_def, table_mask=table_mask, allow_missing=allow_missing
)
raise TypeError(f"Invalid column definition: {col_def}")

@staticmethod
Expand Down Expand Up @@ -454,6 +469,7 @@ def _parse_col_def_column_name(
table: str,
col_def: str,
table_mask: Optional[np.ndarray] = None,
allow_missing: bool = False,
) -> pd.DataFrame:
"""Extract a column from the data. If the column doesn't exist, check if the col_def is a special float value,
like 'inf'. If that's the case, create a single column pandas DataFrame containing the const value.
Expand All @@ -462,6 +478,7 @@ def _parse_col_def_column_name(
data: TabularData:
table: str:
col_def: str:
allow_missing: bool: If True, return empty DataFrame when column is missing instead of raising KeyError

Returns:

Expand All @@ -486,6 +503,14 @@ def _parse_col_def_column_name(
const_value = float(col_def)
except ValueError:
# pylint: disable=raise-missing-from
if allow_missing:
# Return empty DataFrame with correct number of rows when column is optional and missing
self._log.debug(
"Optional column not found",
table=table,
columns=" or ".join(f"'{col_name}'" for col_name in columns),
)
return pd.DataFrame(index=table_data.index)
columns_str = " and ".join(f"'{col_name}'" for col_name in columns)
raise KeyError(f"Could not find column {columns_str} on table '{table}'")

Expand Down Expand Up @@ -778,13 +803,15 @@ def _parse_col_def_composite(
table: str,
col_def: list,
table_mask: Optional[np.ndarray],
allow_missing: bool = False,
) -> pd.DataFrame:
"""Select multiple columns (each is created from a column definition) and return them as a new DataFrame.

Args:
data: TabularData:
table: str:
col_def: list:
allow_missing: bool: If True, skip missing columns instead of raising errors

Returns:

Expand All @@ -797,10 +824,19 @@ def _parse_col_def_composite(
col_def=sub_def,
table_mask=table_mask,
extra_info=None,
allow_missing=allow_missing,
)
for sub_def in col_def
]
return pd.concat(columns, axis=1)
# Filter out DataFrames with no columns (from missing optional columns)
non_empty_columns = [col for col in columns if len(col.columns) > 0]
if not non_empty_columns:
# If all columns are missing, return an empty DataFrame with the correct number of rows
table_data = data[table]
if table_mask is not None:
table_data = table_data[table_mask]
return pd.DataFrame(index=table_data.index)
return pd.concat(non_empty_columns, axis=1)

def _get_id(self, table: str, key: Mapping[str, int], name: Optional[str]) -> int:
"""
Expand Down
24 changes: 24 additions & 0 deletions tests/data/config/test_optional_extra_mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
#
# SPDX-License-Identifier: MPL-2.0
---
# Test mapping file for optional_extra feature
grid:
nodes:
node:
id:
auto_id:
key: node_id
u_rated: voltage
extra:
- ID
- Name
- optional_extra:
- GUID
- StationID

units:
V:
kV: 1000.0

substitutions: {}
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/data/vision/vision_optional_extra_full.xlsx.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>

SPDX-License-Identifier: MPL-2.0
30 changes: 30 additions & 0 deletions tests/data/vision/vision_optional_extra_mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
#
# SPDX-License-Identifier: MPL-2.0
---
# Test mapping file for optional_extra feature with Vision Excel format
id_reference:
nodes_table: Nodes
number: Number
node_number: Node.Number
sub_number: Subnumber

grid:
Nodes:
node:
id:
auto_id:
key: Number
u_rated: Unom
extra:
- ID
- Name
- optional_extra:
- GUID
- StationID

units:
V:
kV: 1000.0

substitutions: {}
3 changes: 3 additions & 0 deletions tests/data/vision/vision_optional_extra_mapping.yaml.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>

SPDX-License-Identifier: MPL-2.0
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/data/vision/vision_optional_extra_minimal.xlsx.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>

SPDX-License-Identifier: MPL-2.0
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/data/vision/vision_optional_extra_partial.xlsx.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>

SPDX-License-Identifier: MPL-2.0
Loading