I use special np.dtypes for a program that have a structure like so:
POINT = np.dtype([('vertices', '<f4', 2), ('center', '<f4', 2), ('bbox', '<f4', 4)])
I need to specify another np.dtype that only uses the last field above, like so:
MBR = np.dtype([('bbox', '<f4', 4)])
This is so I can later access that field of both arrays like so:
def intersection(s, t):
sxmin, sxmax, symin, sxmax = s['bbox']
txmin, txmax, tymin, tymax = t['bbox']
# do stuff
However, when I create the following array, it is being expanded and I'm unsure why:
box = np.array([1, 2, 3, 4], dtype=MBR)
# expected output...
array([1., 2., 3., 4.], dtype=[('bbox', '<f4', 4)])
# actual output...
array([([1., 1., 1., 1.],), ..., ([4., 4., 4., 4.],)], dtype=[('bbox', '<f4', 4)])
A quick test returns what I had expected...
np.empty([], dtype=MBR)
array(([nan, nan, inf, nan],), dtype=[('bbox', '<f4', 4)])
Edit:
Doing the following returns the expected result:
box = np.array(([1, 2, 3, 4],), dtype=MBR)
So now the question: Why do I have to wrap it in a tuple to conform to the dtype?