def _create_dba(self, dbpath, shape, dtype): """ Initialize a `DBArray` with provided information. """ dba = DBArray(dbpath, self.DBTYPE) dba.set_shape(shape) dba.set_dtype(dtype) return dba
def test_attr(self): """ Set/Get attributes. """ for key, val in self.commdbs.iteritems(): dbpath = os.path.join(self.tempdir, self.DBTYPE, 'test_attr_%s.db' % key) dba = DBArray.fromndarray(val, dbpath, self.DBTYPE) data_mean = val.mean(0) int_attr = np.random.randint(100) flt_attr = np.random.rand() str_attr = 'hello world' dba['data_mean'] = data_mean dba['int_attr'] = int_attr dba['str_attr'] = str_attr # float is not support currently self.assertRaises(TypeError, dba.__setitem__, ('flt_attr', flt_attr)) self._arr_eq(dba['data_mean'], data_mean) self.assertEqual(dba['int_attr'], int_attr) self.assertEqual(dba['str_attr'], str_attr) del dba dba = DBArray(dbpath, self.DBTYPE) self._arr_eq(dba['data_mean'], data_mean) self.assertEqual(dba['int_attr'], int_attr) self.assertEqual(dba['str_attr'], str_attr)
def test_from_and_to(self): for key, val in self.commdbs.iteritems(): dbpath = os.path.join(self.tempdir, self.DBTYPE, 'test_from_%s.db' % key) dba = DBArray.fromndarray(val, dbpath, self.DBTYPE) self._info_eq(dba, val) arr = dba.tondarray() self._arr_eq(arr, val) # cloase and re-open del dba dba = DBArray(dbpath, self.DBTYPE) self._info_eq(dba, val) arr = dba.tondarray() self._arr_eq(arr, val)
def test_get_data(self): for key, val in self.commdbs.iteritems(): dbpath = os.path.join(self.tempdir, self.DBTYPE, 'test_get_data_%s.db' % key) dba = DBArray.fromndarray(val, dbpath, self.DBTYPE) self._arr_eq(dba[10], val[10]) self._arr_eq(dba[10, :], val[10, :]) self._arr_eq(dba[1:10], val[1:10]) self._arr_eq(dba[1:10, :], val[1:10, :]) self._arr_eq(dba[[1, 2, 5]], val[[1, 2, 5]])
def test_multi_handle(self): """ Test the case of open multiple handle for the same database. This test is for: https://github.com/wanji/dbarray/issues/1 """ dbpath = os.path.join(self.tempdir, self.DBTYPE, 'test_multi_handle.db') for idx in range(2): # Create random array nrows = 1 ncols = 5 dtype = np.float32 arr = np.random.random((nrows, ncols)) arr = np.require(arr, dtype) """ `DBArray` from scratch """ # Create `DBArray` dba1 = DBArray(dbpath) # Initialize dba1.set_shape((nrows, ncols)) dba1.set_dtype(dtype) # Set rows dba1[:] = arr[:] # Convert to ndarray dba1.tondarray() """ Open exsiting `DBArray` """ # Open another DB dba2 = DBArray(dbpath) dba2.tondarray() # Check if a error rises while accessing closed db dba1._storage.env.close() self.assertRaises(lmdb.Error, dba2.tondarray)