Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concatenate and merge info, preserving conflicts as lists. #691

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion dimod/sampleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,16 @@ def concatenate(samplesets, defaults=None):
record = recfunctions.stack_arrays(records, defaults=defaults,
asrecarray=True, usemask=False)

return SampleSet(record, variables, {}, vartype)
# Merge info, preserving conflicts as lists
info = {}
for k in set().union(*[s.info for s in samplesets]):
info[k] = []
for s in samplesets:
if k in s.info and s.info[k] not in info[k]:
info[k] += [s.info[k]]
info[k] = info[k] if len(info[k])>1 else info[k].pop()

return SampleSet(record, variables, info, vartype)


def _iter_records(samplesets, vartype, variables):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_sampleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ def test_simple(self):
out = dimod.SampleSet.from_samples([[-1, +1], [+1, -1], [+1, +1], [-1, -1]], dimod.SPIN, energy=[-1, -1, 1, 1])

self.assertEqual(comb, out)
self.assertEqual(comb.info, {})
np.testing.assert_array_equal(comb.record.sample, out.record.sample)

def test_variables_order(self):
Expand Down Expand Up @@ -941,6 +942,17 @@ def test_variables_order_and_vartype(self):
self.assertEqual(comb, out)
np.testing.assert_array_equal(comb.record.sample, out.record.sample)

def test_info(self):
ss0 = dimod.SampleSet.from_samples(([-1, +1], 'ab'), dimod.SPIN, info={}, energy=-1)
ss1 = dimod.SampleSet.from_samples(([-1, +1], 'ba'), dimod.SPIN, info={1:'a',2:['b','c']}, energy=-1)
ss2 = dimod.SampleSet.from_samples(([+1, +1], 'ab'), dimod.SPIN, info={3:'e',2:'d',4:[]}, energy=+1)

comb = dimod.concatenate((ss0, ss1, ss2))

out_info = {1:'a',2:[['b','c'],'d'],3:'e',4:[]}

self.assertEqual(comb.info, out_info)

def test_empty(self):
with self.assertRaises(ValueError):
dimod.concatenate([])
Expand Down