Skip to content

Commit

Permalink
Merge pull request #266 from PowerGridModel/fix/empty-array-remove
Browse files Browse the repository at this point in the history
Dont return empty array on filters
  • Loading branch information
nitbharambe authored Jul 26, 2024
2 parents c89d49a + bf085c1 commit 2f566ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/power_grid_model_io/converters/tabular_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def _convert_table_to_component(

if "filters" in attributes:
table_mask = self._parse_table_filters(data=data, table=table, filtering_functions=attributes["filters"])
if table_mask is not None and not table_mask.any():
return None
else:
table_mask = None

Expand Down
33 changes: 29 additions & 4 deletions tests/unit/converters/test_tabular_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ def tabular_data():
)
lines = pd.DataFrame(data=[[1, 100], [2, 200]], columns=["id_number", "from_node_side"])
loads = pd.DataFrame(data=[[1, 1, 1], [2, 2, 0]], columns=["id_number", "node_id", "switching_status"])
tabular_data = TabularData(nodes=nodes, lines=lines, loads=loads)
return tabular_data
sources = pd.DataFrame(columns=["id_number", "node_side"])
return TabularData(nodes=nodes, lines=lines, loads=loads, sources=sources)


@pytest.fixture
def tabular_data_no_units_no_substitutions():
nodes = pd.DataFrame(data=[[1, 10.5e3], [2, 400.0]], columns=["id_number", "u_nom"])
lines = pd.DataFrame(data=[[1, 2], [3, 1]], columns=["id_number", "from_node_side"])
tabular_data = TabularData(nodes=nodes, lines=lines)
return tabular_data
sources = pd.DataFrame(columns=["id_number", "node_side"])
return TabularData(nodes=nodes, lines=lines, sources=sources)


def test_set_mapping_file(converter: TabularConverter):
Expand Down Expand Up @@ -175,6 +175,31 @@ def test_convert_table_to_component__filters(
)


def test_convert_table_to_component__filters_all_false(
converter: TabularConverter, tabular_data_no_units_no_substitutions: TabularData
):
converter._convert_col_def_to_attribute = MagicMock()
converter._parse_table_filters = MagicMock()
converter._parse_table_filters.side_effect = np.array([False, False])
node_attributes_with_filter = {"id": "id_number", "u_rated": "u_nom", "filters": [{"test_fn": {}}]}
actual = converter._convert_table_to_component(
data=tabular_data_no_units_no_substitutions,
data_type="input",
table="nodes",
component="node",
attributes=node_attributes_with_filter,
extra_info=None,
)

assert actual is None
converter._parse_table_filters.assert_called_once_with(
data=tabular_data_no_units_no_substitutions,
table="nodes",
filtering_functions=node_attributes_with_filter["filters"],
)
converter._convert_col_def_to_attribute.assert_not_called()


def test_convert_col_def_to_attribute(
converter: TabularConverter,
tabular_data_no_units_no_substitutions: TabularData,
Expand Down

0 comments on commit 2f566ef

Please sign in to comment.