From ad23f2c4a67b400c253591dfa6076609ec293baa Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Mon, 5 Feb 2024 15:51:17 +0100 Subject: [PATCH] Fix bug when serializing line parameters with numpy values Fixes #174 --- doc/Changelog.md | 2 ++ roseau/load_flow/models/lines/parameters.py | 5 +++++ roseau/load_flow/models/tests/test_line_parameters.py | 10 ++++++++++ roseau/load_flow/models/transformers/parameters.py | 5 +++++ 4 files changed, 22 insertions(+) diff --git a/doc/Changelog.md b/doc/Changelog.md index 7396ba59..89c25663 100644 --- a/doc/Changelog.md +++ b/doc/Changelog.md @@ -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 diff --git a/roseau/load_flow/models/lines/parameters.py b/roseau/load_flow/models/lines/parameters.py index f5c60061..9f68fea7 100644 --- a/roseau/load_flow/models/lines/parameters.py +++ b/roseau/load_flow/models/lines/parameters.py @@ -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: diff --git a/roseau/load_flow/models/tests/test_line_parameters.py b/roseau/load_flow/models/tests/test_line_parameters.py index c7b24da1..0d9d7b7f 100644 --- a/roseau/load_flow/models/tests/test_line_parameters.py +++ b/roseau/load_flow/models/tests/test_line_parameters.py @@ -1,3 +1,4 @@ +import json import re import numpy as np @@ -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) diff --git a/roseau/load_flow/models/transformers/parameters.py b/roseau/load_flow/models/transformers/parameters.py index 9ee332e2..935e7fb4 100644 --- a/roseau/load_flow/models/transformers/parameters.py +++ b/roseau/load_flow/models/transformers/parameters.py @@ -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: