Skip to content

Commit

Permalink
Fix bug when serializing line parameters with numpy values
Browse files Browse the repository at this point in the history
Fixes #174
  • Loading branch information
alihamdan committed Feb 5, 2024
1 parent 4126d04 commit ad23f2c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Starting with version 0.7.0, Roseau Load Flow will no longer be supplied as a Sa
a standalone Python library.
```

- {gh-pr}`175` {gh-issue}`174` Fix JSON serialization of network with line parameters created from the
catalogue.
- {gh-pr}`173` Remove the conda installation option.
- {gh-pr}`168` {gh-issue}`166` Fix initial potentials' propagation.
- {gh-pr}`167` {gh-issue}`161` Add a catalogue of lines using the IEC standards. You can use the method
Expand Down
5 changes: 5 additions & 0 deletions roseau/load_flow/models/lines/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,11 @@ def to_dict(self, *, _lf_only: bool = False) -> JsonDict:
res["insulator_type"] = self._insulator_type.name
if not _lf_only and self._section is not None:
res["section"] = self._section
for k, v in res.items():
if isinstance(v, np.integer):
res[k] = int(v)
elif isinstance(v, np.floating):
res[k] = float(v)
return res

def _results_to_dict(self, warning: bool) -> NoReturn:
Expand Down
10 changes: 10 additions & 0 deletions roseau/load_flow/models/tests/test_line_parameters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import re

import numpy as np
Expand Down Expand Up @@ -495,3 +496,12 @@ def test_max_current():

lp.max_current = Q_(3, "kA")
assert lp.max_current == Q_(3_000, "A")


def test_json_serialization():
lp = LineParameters("test", z_line=np.eye(3), max_current=np.int64(100), section=np.float64(150))
lp_dict = lp.to_dict()
assert isinstance(lp_dict["z_line"], list)
assert isinstance(lp_dict["max_current"], int)
assert isinstance(lp_dict["section"], float)
json.dumps(lp_dict)
5 changes: 5 additions & 0 deletions roseau/load_flow/models/transformers/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ def to_dict(self, *, _lf_only: bool = False) -> JsonDict:
}
if not _lf_only and self.max_power is not None:
res["max_power"] = self.max_power.magnitude
for k, v in res.items():
if isinstance(v, np.integer):
res[k] = int(v)
elif isinstance(v, np.floating):
res[k] = float(v)
return res

def _results_to_dict(self, warning: bool) -> NoReturn:
Expand Down

0 comments on commit ad23f2c

Please sign in to comment.