From 70df4b2e92d438d39f85da40ee8015cf0665a08e Mon Sep 17 00:00:00 2001 From: Viktor Vorobev Date: Fri, 25 Apr 2025 12:43:21 +0200 Subject: [PATCH 1/4] Add the numpy int* types to nest.serialize_data and call serialize_data before sending the response back in hl_api_server.py --- pynest/nest/lib/hl_api_types.py | 6 ++++++ pynest/nest/server/hl_api_server.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pynest/nest/lib/hl_api_types.py b/pynest/nest/lib/hl_api_types.py index 24cdfadd09..a6670f92c6 100644 --- a/pynest/nest/lib/hl_api_types.py +++ b/pynest/nest/lib/hl_api_types.py @@ -1284,6 +1284,12 @@ def serialize_data(data): Data can be encoded to JSON """ + if isinstance(data, (numpy.ndarray, NodeCollection)): + return data.tolist() + if isinstance(data, (numpy.int_, numpy.intc, numpy.intp, numpy.int8, + numpy.int16, numpy.int32, numpy.int64, numpy.uint8, + numpy.uint16, numpy.uint32, numpy.uint64)): + return int(data) if isinstance(data, (numpy.ndarray, NodeCollection)): return data.tolist() elif isinstance(data, SynapseCollection): diff --git a/pynest/nest/server/hl_api_server.py b/pynest/nest/server/hl_api_server.py index 6a5d92d9aa..1faadc9428 100644 --- a/pynest/nest/server/hl_api_server.py +++ b/pynest/nest/server/hl_api_server.py @@ -277,7 +277,7 @@ def route_exec(): if EXEC_CALL_ENABLED: args, kwargs = get_arguments(request) response = do_call("exec", args, kwargs) - return jsonify(response) + return jsonify(nest.serialize_data(response)) else: flask.abort( 403, @@ -307,7 +307,7 @@ def route_api_call(call): args, kwargs = get_arguments(request) log("route_api_call", f"call={call}, args={args}, kwargs={kwargs}") response = api_client(call, args, kwargs) - return jsonify(response) + return jsonify(nest.serialize_data(response)) # ---------------------- From 177680aa2432db3e5f098b1c9de27dc11a3b3224 Mon Sep 17 00:00:00 2001 From: Viktor Vorobev Date: Fri, 25 Apr 2025 12:47:35 +0200 Subject: [PATCH 2/4] Remove duplication --- pynest/nest/lib/hl_api_types.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pynest/nest/lib/hl_api_types.py b/pynest/nest/lib/hl_api_types.py index a6670f92c6..b0f1ea7243 100644 --- a/pynest/nest/lib/hl_api_types.py +++ b/pynest/nest/lib/hl_api_types.py @@ -1290,8 +1290,6 @@ def serialize_data(data): numpy.int16, numpy.int32, numpy.int64, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64)): return int(data) - if isinstance(data, (numpy.ndarray, NodeCollection)): - return data.tolist() elif isinstance(data, SynapseCollection): # Get full information from SynapseCollection return serialize_data(data.get()) From 2f3bb67d384f9f1f501eee81f20614af89b3d74b Mon Sep 17 00:00:00 2001 From: Viktor Vorobev Date: Mon, 28 Apr 2025 12:32:28 +0200 Subject: [PATCH 3/4] Update pynest/nest/lib/hl_api_types.py Co-authored-by: med-ayssar --- pynest/nest/lib/hl_api_types.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pynest/nest/lib/hl_api_types.py b/pynest/nest/lib/hl_api_types.py index b0f1ea7243..411fc6b574 100644 --- a/pynest/nest/lib/hl_api_types.py +++ b/pynest/nest/lib/hl_api_types.py @@ -1286,9 +1286,7 @@ def serialize_data(data): if isinstance(data, (numpy.ndarray, NodeCollection)): return data.tolist() - if isinstance(data, (numpy.int_, numpy.intc, numpy.intp, numpy.int8, - numpy.int16, numpy.int32, numpy.int64, numpy.uint8, - numpy.uint16, numpy.uint32, numpy.uint64)): + if np.issubdtype(d, np.integer): return int(data) elif isinstance(data, SynapseCollection): # Get full information from SynapseCollection From c536db04839d2ee3454e32c48eaac7406054630d Mon Sep 17 00:00:00 2001 From: Viktor Vorobev Date: Mon, 28 Apr 2025 12:34:40 +0200 Subject: [PATCH 4/4] Fix the code --- pynest/nest/lib/hl_api_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pynest/nest/lib/hl_api_types.py b/pynest/nest/lib/hl_api_types.py index 411fc6b574..a05a31557c 100644 --- a/pynest/nest/lib/hl_api_types.py +++ b/pynest/nest/lib/hl_api_types.py @@ -1286,7 +1286,7 @@ def serialize_data(data): if isinstance(data, (numpy.ndarray, NodeCollection)): return data.tolist() - if np.issubdtype(d, np.integer): + if isinstance(data, (numpy.integer)): return int(data) elif isinstance(data, SynapseCollection): # Get full information from SynapseCollection