You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2017/12/30 18:47:12 UTC

[GitHub] szha closed pull request #9228: raise err in io.NDArrayIter for invalid usecase

szha closed pull request #9228: raise err in io.NDArrayIter for invalid usecase
URL: https://github.com/apache/incubator-mxnet/pull/9228
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/mxnet/io.py b/python/mxnet/io.py
index 25a95be787..b07f7c1bea 100644
--- a/python/mxnet/io.py
+++ b/python/mxnet/io.py
@@ -515,17 +515,13 @@ def _init_data(data, allow_empty, default_name):
     return list(data.items())
 
 def _has_instance(data, dtype):
-    """return True if data has instance of dtype"""
-    if isinstance(data, dtype):
-        return True
-    if isinstance(data, list):
-        for v in data:
-            if isinstance(v, dtype):
-                return True
-    if isinstance(data, dict):
-        for v in data.values():
-            if isinstance(v, dtype):
-                return True
+    """Return True if ``data`` has instance of ``dtype``.
+    This function is called after _init_data.
+    ``data`` is a list of (str, NDArray)"""
+    for item in data:
+        _, arr = item
+        if isinstance(arr, dtype):
+            return True
     return False
 
 def _shuffle(data, idx):
@@ -544,7 +540,7 @@ def _shuffle(data, idx):
 
 class NDArrayIter(DataIter):
     """Returns an iterator for ``mx.nd.NDArray``, ``numpy.ndarray``, ``h5py.Dataset``
-    or ``mx.nd.sparse.CSRNDArray``.
+    ``mx.nd.sparse.CSRNDArray`` or ``scipy.sparse.csr_matrix``.
 
     Example usage:
     ----------
@@ -644,12 +640,13 @@ def __init__(self, data, label=None, batch_size=1, shuffle=False,
                  label_name='softmax_label'):
         super(NDArrayIter, self).__init__(batch_size)
 
-        if ((_has_instance(data, CSRNDArray) or _has_instance(label, CSRNDArray)) and
+        self.data = _init_data(data, allow_empty=False, default_name=data_name)
+        self.label = _init_data(label, allow_empty=True, default_name=label_name)
+
+        if ((_has_instance(self.data, CSRNDArray) or _has_instance(self.label, CSRNDArray)) and
                 (last_batch_handle != 'discard')):
             raise NotImplementedError("`NDArrayIter` only supports ``CSRNDArray``" \
                                       " with `last_batch_handle` set to `discard`.")
-        self.data = _init_data(data, allow_empty=False, default_name=data_name)
-        self.label = _init_data(label, allow_empty=True, default_name=label_name)
 
         self.idx = np.arange(self.data[0][1].shape[0])
         # shuffle data
diff --git a/tests/python/unittest/test_io.py b/tests/python/unittest/test_io.py
index fa314e0f8b..fd3c96443b 100644
--- a/tests/python/unittest/test_io.py
+++ b/tests/python/unittest/test_io.py
@@ -162,9 +162,14 @@ def test_NDArrayIter_csr():
     csr, _ = rand_sparse_ndarray(shape, 'csr')
     dns = csr.asnumpy()
 
-    # CSRNDArray with last_batch_handle not equal to 'discard' will throw NotImplementedError
-    assertRaises(NotImplementedError, mx.io.NDArrayIter, {'data': csr}, dns, batch_size,
-                 last_batch_handle='pad')
+    # CSRNDArray or scipy.sparse.csr_matrix with last_batch_handle not equal to 'discard' will throw NotImplementedError
+    assertRaises(NotImplementedError, mx.io.NDArrayIter, {'data': csr}, dns, batch_size)
+    try:
+        import scipy.sparse as spsp
+        train_data = spsp.csr_matrix(dns)
+        assertRaises(NotImplementedError, mx.io.NDArrayIter, {'data': train_data}, dns, batch_size)
+    except ImportError:
+        pass
 
     # CSRNDArray with shuffle
     csr_iter = iter(mx.io.NDArrayIter({'csr_data': csr, 'dns_data': dns}, dns, batch_size,


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services