def test_asarrayremoveoldmetadata(self): dar = asarray(path=self.tempdirname1, array=[1,2], metadata={'a':1}, overwrite=True) dar = asarray(path=self.tempdirname1, array=[1, 2], overwrite=True) self.assertDictEqual(dict(dar.metadata), {})
def test_truncatetolen0(self): with tempdirfile() as filename: a = asarray(path=filename, array=[0, 1, 2, 3, 4], dtype='int64', accessmode='r+') truncate_array(a, 0) self.assertEqual(len(a), 0) ra = Array(filename) self.assertEqual(len(a), 0)
def test_truncatebydirname(self): with tempdirfile() as filename: a = np.array([0, 1, 2, 3, 4], dtype='int64') dar = asarray(path=filename, array=a, accessmode='r+') truncate_array(filename, 2) a = Array(filename) self.assertArrayIdentical(a[:], np.array([0, 1], dtype=a.dtype))
def check_arrayequaltoasarray(self, ndarray): """Tests if asarray creates an array of same shape and dtype and same contents as input.""" dar = asarray(path=self.tempdirname1, array=ndarray, overwrite=True) ndarray = np.asarray(ndarray) # could be list or tuple self.assertArrayIdentical(dar[:], ndarray) self.assertEqual(dar.dtype, ndarray.dtype) self.assertEqual(dar.shape, ndarray.shape)
def test_asarraysequenceofzerodimnumpyscalars(self): def a(): yield np.float32(0) yield np.float32(1) yield np.float32(2) dar = asarray(path=self.tempdirname1, array=a(), overwrite=True) self.assertArrayIdentical(dar[:], np.array([0,1,2], dtype=np.float32))
def test_truncateindextoohigh(self): with tempdirfile() as filename: a = np.array([0, 1, 2, 3, 4], dtype='int64') dar = asarray(path=filename, array=a, overwrite=True, accessmode='r+') self.assertRaises(IndexError, truncate_array, dar, 10)
def test_truncateinvalidindextype(self): with tempdirfile() as filename: a = np.array([0, 1, 2, 3, 4], dtype='int64') dar = asarray(path=filename, array=a, accessmode='r+') self.assertRaises(TypeError, truncate_array, dar, 'a')
def test_asarraynumberfloat(self): dar = asarray(path=self.tempdirname1, array=1.0, overwrite=True) self.assertEqual(dar[0], 1.0)
def test_asarrayarratosamepath(self): dar = asarray(path=self.tempdirname1, array=[0,1], overwrite=True) self.assertRaises(ValueError, asarray, path=self.tempdirname1, array=dar, overwrite=True)
def test_asarraykeepattrs(self): class AttrList(list): attrs = {'a': 1, 'b': 2} a = AttrList([0, 0, 0, 0]) dar = asarray(path=self.tempdirname1, array=a, overwrite=True) self.assertEqual(dict(dar.metadata), AttrList.attrs)
def test_asarrayarray(self): a = np.arange(1024, dtype='int64').reshape(2, -1) dar1 = asarray(path=self.tempdirname1, array=a, overwrite=True) dar2 = asarray(path=self.tempdirname2, array=a, chunklen=5, overwrite=True) self.assertArrayIdentical(dar1[:], dar2[:])
def test_asarraywritinglargerthanlenchunks(self): a = np.arange(1024, dtype='int64').reshape(2, -1) dar = asarray(path=self.tempdirname1, array=a, chunklen=4096, overwrite=True) self.assertArrayIdentical(a, dar[:])
def test_asarraysequencesmallchunklen(self): a = [1, 2, 3, 4, 5] dar = asarray(path=self.tempdirname1, array=a, chunklen=3, overwrite=True) self.assertArrayIdentical(np.array(a), dar[:])
def test_asarrayoverwrite(self): a = np.zeros((5,), dtype='float64') _ = asarray(path=self.tempdirname1, array=a, overwrite=True) b = np.ones((4,2), dtype='uint8') dar = asarray(path=self.tempdirname1, array=b, overwrite=True) self.assertArrayIdentical(dar[:], b)