Skip to content
Snippets Groups Projects
Commit 788950ca authored by Rob Moss's avatar Rob Moss
Browse files

Warn when repacking arrays with non-float fields

The values in each non-float field will be converted to floats, which
may or may not be desirable. This is likely only sensible for, e.g.,
integer fields.
parent f248130f
No related branches found
No related tags found
No related merge requests found
Pipeline #6559 passed
......@@ -3,6 +3,7 @@
import logging
import numpy as np
import numpy.lib.recfunctions
import warnings
def is_history_matrix(ctx, arr):
......@@ -172,8 +173,23 @@ def earlier_states(hist, ix, steps):
def repack(svec):
"""
Return a copy of the array ``svec`` where the fields are contiguous.
Return a copy of the array ``svec`` where the fields are contiguous and
viewed as a regular Numpy array of floats.
:raises UserWarning: if ``svec`` contains any non-float fields, each of
which will be converted to floats.
"""
svec = numpy.lib.recfunctions.repack_fields(svec)
# Convert any non-float fields (e.g., integers) into floats.
cols = [(name, svec.dtype.fields[name][0]) for name in svec.dtype.names]
all_floats = all(np.issubdtype(col[1], float) for col in cols)
if not all_floats:
warnings.warn('Repacking array with non-float fields',
stacklevel=2)
new_dtype = [
(col[0], col[1] if np.issubdtype(col[1], float) else float,
col[1].shape)
for col in cols]
svec = np.array(svec, dtype=new_dtype)
svec = svec.view((float, len(svec.dtype.names)))
return svec
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment