Permalink
Browse files

ENH test input validation code on memmap arrays

  • Loading branch information...
1 parent 71d5243 commit 1f2549d11dcfebc56ee9f6486e21f2bf8e90974d @larsmans larsmans committed Oct 25, 2011
Showing with 21 additions and 1 deletion.
  1. +21 −1 sklearn/utils/tests/test___init__.py
@@ -1,7 +1,9 @@
import numpy as np
+from numpy.testing import assert_array_equal
import scipy.sparse as sp
+from tempfile import NamedTemporaryFile
-from .. import as_float_array, atleast2d_or_csr, safe_asarray
+from .. import array2d, as_float_array, atleast2d_or_csr, safe_asarray
def test_as_float_array():
@@ -42,3 +44,21 @@ def test_np_matrix():
assert not isinstance(safe_asarray(X), np.matrix)
assert not isinstance(safe_asarray(np.matrix(X)), np.matrix)
assert not isinstance(safe_asarray(sp.lil_matrix(X)), np.matrix)
+
+
+def test_memmap():
+ """
+ Confirm that input validation code doesn't copy memory mapped arrays
+ """
+
+ asflt = lambda x: as_float_array(x, copy=False)
+
+ with NamedTemporaryFile(prefix='sklearn-test') as tmp:
+ M = np.memmap(tmp, shape=100, dtype=np.float32)
+ M[:] = 0
+
+ for f in (array2d, np.asarray, asflt, safe_asarray):
+ X = f(M)
+ X[:] = 1
+ assert_array_equal(X.ravel(), M)
+ X[:] = 0

0 comments on commit 1f2549d

Please sign in to comment.