Fix HTML table display on Numpy 2 #4970
Merged
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.
HTML tables in result web pages are broken with Numpy 2. This should fix it.
Standard information about the request
This is a bug fix.
This bug involves anything that makes HTML tables of data (this includes at least the all-sky offline search and PyGRB) but the fix not impact anything.
Motivation
The problem is described in #4968, but to summarize, HTML tables show up empty under Numpy 2 because the data array in JavaScript ends up being filled with strings like
'np.float64(7.1)'
instead of proper numbers like7.1
.Contents
I added an explicit conversion of Numpy's float and integer types to Python native floats and ints respectively, before they are passed to the template that renders them into JavaScript values.
Links to any issues or associated PRs
Fixes #4968.
Testing performed
I used the following code:
Before this fix, this produces HTML files that actually show up blank with both Numpy 1.23.5 and 2.1.2, i.e. this test uncovered an additional bug with complex arrays breaking the tables (admittely not a likely use case though):
http://leela.fisica.indivia.net/~tito/pycbc/test_html_tables_numpy_2/table_before_fix_numpy_1.23.5.html
http://leela.fisica.indivia.net/~tito/pycbc/test_html_tables_numpy_2/table_before_fix_numpy_2.1.2.html
After this fix, I get identical-looking tables with both Numpy versions:
http://leela.fisica.indivia.net/~tito/pycbc/test_html_tables_numpy_2/table_after_fix_numpy_1.23.5.html
http://leela.fisica.indivia.net/~tito/pycbc/test_html_tables_numpy_2/table_after_fix_numpy_2.1.2.html
Additional notes
N/A