Repository: dwavesystems/dimod

Issue:
Can't create empty SampleSet with from_samples
We can get an empty `Response`:
```
>>> dimod.Response.from_samples([], {'energy': []}, info={}, vartype=bqm.vartype)
Response(rec.array([],
          dtype=[('sample', 'i1', (0,)), ('energy', '<f8'), ('num_occurrences', '<f8')]), [], {}, 'SPIN')
```
but not an empty `SampleSet`:
```
>>> dimod.SampleSet.from_samples([], energy=[], vartype=bqm.vartype)
Traceback (most recent call last):
    ...
    first_sample = next(itersamples)
StopIteration
```

Discussion:

Patch:
diff --git a/dimod/sampleset.py b/dimod/sampleset.py
index 88d837e9..b5a23cc0 100644
--- a/dimod/sampleset.py
+++ b/dimod/sampleset.py
@@ -119,6 +119,13 @@ def as_samples(samples_like, dtype=None):
 
         return samples, labels
 
+    # if no ` is specified and the array_like doesn't already have a dtype, we default to int8
+    if dtype is None and not hasattr(samples_like, 'dtype'):
+        dtype = np.int8
+
+    if len(samples_like) == 0:
+        return np.empty((0, 0), dtype=dtype), []
+
     if isinstance(samples_like, Mapping):
         return as_samples([samples_like], dtype=dtype)
 
@@ -128,10 +135,6 @@ def as_samples(samples_like, dtype=None):
 
     # anything else should be array_like, which covers ndarrays, lists of lists, etc
 
-    # if no ` is specified and the array_like doesn't already have a dtype, we default to int8
-    if dtype is None and not hasattr(samples_like, 'dtype'):
-        dtype = np.int8
-
     try:
         samples_like = np.asarray(samples_like, dtype=dtype)
     except (ValueError, TypeError):


Test patch:
diff --git a/tests/test_sampleset.py b/tests/test_sampleset.py
index 0766ee76..bf2d23af 100644
--- a/tests/test_sampleset.py
+++ b/tests/test_sampleset.py
@@ -108,6 +108,14 @@ class TestSampleSet(unittest.TestCase):
 
         self.assertEqual(len(list(ss.samples(n=1))), 1)
 
+    def test_from_samples_empty(self):
+
+        self.assertEqual(len(dimod.SampleSet.from_samples([], dimod.SPIN, energy=[], a=1)), 0)
+
+        self.assertEqual(len(dimod.SampleSet.from_samples({}, dimod.SPIN, energy=[], a=1)), 0)
+
+        self.assertEqual(len(dimod.SampleSet.from_samples(np.empty((0, 0)), dimod.SPIN, energy=[], a=1)), 0)
+
 
 class TestSampleSetSerialization(unittest.TestCase):