Skip to content

Commit 10ba51c

Browse files
committed
Add parser tests
Signed-off-by: Thijs Baaijen <[email protected]>
1 parent 40e9761 commit 10ba51c

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

src/power_grid_model_ds/_core/visualizer/parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def parse_node_array(nodes: NodeArray) -> list[dict[str, Any]]:
2121
cyto_elements["data"]["id"] = str(node.id.item())
2222
cyto_elements["data"]["group"] = "node"
2323
if with_coords:
24-
cyto_elements["position"] = {"x": node.x.item(), "y": -node.y.item()}
24+
cyto_elements["position"] = {"x": node.x.item(), "y": -node.y.item()} # invert y-axis for visualization
2525
parsed_nodes.append(cyto_elements)
2626
return parsed_nodes
2727

tests/integration/visualizer_tests.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
from power_grid_model_ds.generators import RadialGridGenerator
1414

1515

16-
class TopoNodeArray(NodeArray):
16+
class CoordinatedNodeArray(NodeArray):
1717
x: NDArray[np.float64]
1818
y: NDArray[np.float64]
1919

2020

2121
@dataclass
22-
class TopoGrid(Grid):
23-
node: TopoNodeArray
22+
class CoordinatedGrid(Grid):
23+
node: CoordinatedNodeArray
2424

2525

2626
def get_radial_grid() -> Grid:
2727
return RadialGridGenerator(Grid).run()
2828

2929

30-
def get_topo_grid() -> TopoGrid:
30+
def get_coordinated_grid() -> CoordinatedGrid:
3131
scale = 500
32-
grid = TopoGrid.from_txt("S1 2 open", "2 3", "3 4", "S1 500000000", "500000000 6", "6 7 transformer")
32+
grid = CoordinatedGrid.from_txt("S1 2 open", "2 3", "3 4", "S1 500000000", "500000000 6", "6 7 transformer")
3333
grid.node.x = [3, 2.5, 2, 1.5, 3.5, 4, 4.5]
3434
grid.node.x *= scale
3535
grid.node.y = [3, 4, 3, 4, 3, 4, 3]
@@ -41,13 +41,13 @@ def visualize_grid():
4141
visualize(grid=get_radial_grid(), debug=True)
4242

4343

44-
def visualize_topo_grid():
44+
def visualize_coordinated_grid():
4545
visualize(
46-
grid=get_topo_grid(),
46+
grid=get_coordinated_grid(),
4747
debug=True,
4848
)
4949

5050

5151
if __name__ == "__main__":
5252
visualize_grid()
53-
# visualize_topo_grid()
53+
# visualize_coordinated_grid()

tests/unit/visualizer/__init__.py

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from power_grid_model_ds._core.model.arrays import NodeArray, LineArray
2+
from power_grid_model_ds._core.visualizer.parsers import parse_node_array, parse_branch_array
3+
from tests.integration.visualizer_tests import CoordinatedNodeArray
4+
5+
6+
class TestParseNodeArray:
7+
8+
def test_parse_node_array(self):
9+
nodes = NodeArray.zeros(3)
10+
nodes['id'] = [1, 2, 3]
11+
nodes["u_rated"] = [10, 20.4, 30.99]
12+
13+
parsed = parse_node_array(nodes)
14+
assert len(parsed) == 3
15+
16+
node_1_data = parsed[0]['data']
17+
node_2_data = parsed[1]['data']
18+
node_3_data = parsed[2]['data']
19+
20+
assert node_1_data['group'] == "node"
21+
assert parsed[0].get('position') is None # no coordinates
22+
23+
assert node_1_data['id'] == '1' # ids are converted to strings
24+
assert node_2_data['id'] == '2'
25+
assert node_3_data['id'] == '3'
26+
27+
assert node_1_data['u_rated'] == 10
28+
assert node_2_data['u_rated'] == 20.4
29+
assert node_3_data['u_rated'] == 30.99
30+
31+
32+
def test_parse_coordinated_node_array(self):
33+
nodes = CoordinatedNodeArray.zeros(3)
34+
nodes['id'] = [1, 2, 3]
35+
nodes["x"] = [10, 20, 30]
36+
nodes["y"] = [99, 88, 77]
37+
38+
parsed = parse_node_array(nodes)
39+
position = parsed[0].get('position')
40+
assert position is not None
41+
assert position['x'] == 10
42+
assert position['y'] == -99 # coordinates are inverted on y-axis
43+
44+
45+
class TestParseBranches:
46+
47+
def test_parse_line_array(self):
48+
lines = LineArray.zeros(3)
49+
lines['id'] = [100, 101, 102]
50+
lines['from_node'] = [1, 2, 3]
51+
lines['to_node'] = [4, 5, 6]
52+
parsed = parse_branch_array(lines, "line")
53+
54+
assert len(parsed) == 3
55+
assert parsed[0]['data']['id'] == '100'
56+
assert parsed[0]['data']['source'] == '1'
57+
assert parsed[0]['data']['target'] == '4'
58+
assert parsed[0]['data']['group'] == "line"
59+

0 commit comments

Comments
 (0)