Bug: Numpy type represented as "np.float64(1.234)" instead of "1.234" #121
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is a bug in the
amr_wind_standin
toemulator' communication.
amr_wind_standincreates a list of numbers (some are python native floats and some are numpy floats) called
message_from_client_array, which is cast as a string before being passed to
emulator' via helics. Inemulator
this message is read usingast.literal_eval()
, which parses the string into a list of only native python types.Casting a python list to string calls the
__repr__()
(meaning representation, I think) method on each element in the list. At some point numpy changed this behavior so that instead of returning"1.234"
the__repr__
method returns"np.float64(1.234)"
. This is what was causing the error in theast.literal_eval
call inemulator
. The error message was:error: malformed node or string on line 1: <ast.Call object at 0x2971aafb0>
Originally, I was going to replace the
ast.literal_eval
call witheval
but then I learned this might be dangerous ( https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html ).I fixed the bug by casting the
amr_wind_standin
message to native python floats before sending through helics but a similar issue may pop up again elsewhere.https://numpy.org/neps/nep-0051-scalar-representation.html#alternatives