dimod icon indicating copy to clipboard operation
dimod copied to clipboard

Cannot concatenate sample sets of unlike types

Open arcondello opened this issue 5 years ago • 1 comments

minimal failing example:

import dimod
import numpy as np

sample = [-1, 1, 1]

ss_int = dimod.SampleSet.from_samples(np.asarray(sample, dtype=np.int), energy=6, vartype='SPIN')
ss_float = dimod.SampleSet.from_samples(np.asarray(sample, dtype=np.float), energy=6, vartype='SPIN')

dimod.sampleset.concatenate([ss_int, ss_float])

arcondello avatar Sep 11 '20 21:09 arcondello

The obvious fix is to use autoconvert=True in https://github.com/dwavesystems/dimod/blob/12ba0c93a48c39029d8dd7ada9600d30051d73ba/dimod/sampleset.py#L237 but there appears to be a bug in numpy, or at the very least a documentation hole. Example:

import numpy as np

from numpy.lib.recfunctions import stack_arrays

arr1 = np.ones(3, dtype=[('a', np.int), ('b', np.int)])
arr2 = np.ones(3, dtype=[('a', np.float), ('b', np.int)])

print(stack_arrays([arr1, arr2], autoconvert=True).dtype)

arr3 = np.ones(3, dtype=[('a', np.int, 2), ('b', np.int)])
arr4 = np.ones(3, dtype=[('a', np.float, 2), ('b', np.int)])

print(stack_arrays([arr3, arr4], autoconvert=True).dtype)
print(stack_arrays([arr4, arr3], autoconvert=True).dtype)

gives

[('a', '<f8'), ('b', '<i8')]
[('a', '<i8', (2,)), ('b', '<i8')]
[('a', '<f8', (2,)), ('b', '<i8')]

notice that the second from the bottom has <i8 for 'a'.

I'll dig into it a bit more and possibly make a PR to numpy.

arcondello avatar Sep 22 '20 19:09 arcondello