def test_to_from_numpy(self): n_rows = 10 n_cols = 11 data = np.random.rand(n_rows * n_cols) bm = BlockMatrix._create_block_matrix(n_rows, n_cols, data.tolist(), row_major=True, block_size=4) a = data.reshape((n_rows, n_cols)) with tempfile.NamedTemporaryFile() as bm_f: with tempfile.NamedTemporaryFile() as a_f: bm.tofile(bm_f.name) a.tofile(a_f.name) a1 = bm.to_numpy() a2 = BlockMatrix.from_numpy(a, block_size=5).to_numpy() a3 = np.fromfile(bm_f.name).reshape((n_rows, n_cols)) a4 = BlockMatrix.fromfile(a_f.name, n_rows, n_cols, block_size=3).to_numpy() a5 = BlockMatrix.fromfile(bm_f.name, n_rows, n_cols).to_numpy() self.assertTrue(np.array_equal(a1, a)) self.assertTrue(np.array_equal(a2, a)) self.assertTrue(np.array_equal(a3, a)) self.assertTrue(np.array_equal(a4, a)) self.assertTrue(np.array_equal(a5, a)) bmT = bm.T aT = a.T with tempfile.NamedTemporaryFile() as bmT_f: with tempfile.NamedTemporaryFile() as aT_f: bmT.tofile(bmT_f.name) aT.tofile(aT_f.name) aT1 = bmT.to_numpy() aT2 = BlockMatrix.from_numpy(aT).to_numpy() aT3 = np.fromfile(bmT_f.name).reshape((n_cols, n_rows)) aT4 = BlockMatrix.fromfile(aT_f.name, n_cols, n_rows).to_numpy() aT5 = BlockMatrix.fromfile(bmT_f.name, n_cols, n_rows).to_numpy() self.assertTrue(np.array_equal(aT1, aT)) self.assertTrue(np.array_equal(aT2, aT)) self.assertTrue(np.array_equal(aT3, aT)) self.assertTrue(np.array_equal(aT4, aT)) self.assertTrue(np.array_equal(aT5, aT))
def test_to_from_numpy(self): n_rows = 10 n_cols = 11 data = np.random.rand(n_rows * n_cols) bm = BlockMatrix._create(n_rows, n_cols, data.tolist(), row_major=True, block_size=4) a = data.reshape((n_rows, n_cols)) with tempfile.NamedTemporaryFile() as bm_f: with tempfile.NamedTemporaryFile() as a_f: bm.tofile(bm_f.name) a.tofile(a_f.name) a1 = bm.to_numpy() a2 = BlockMatrix.from_numpy(a, block_size=5).to_numpy() a3 = np.fromfile(bm_f.name).reshape((n_rows, n_cols)) a4 = BlockMatrix.fromfile(a_f.name, n_rows, n_cols, block_size=3).to_numpy() a5 = BlockMatrix.fromfile(bm_f.name, n_rows, n_cols).to_numpy() self._assert_eq(a1, a) self._assert_eq(a2, a) self._assert_eq(a3, a) self._assert_eq(a4, a) self._assert_eq(a5, a) bmt = bm.T at = a.T with tempfile.NamedTemporaryFile() as bmt_f: with tempfile.NamedTemporaryFile() as at_f: bmt.tofile(bmt_f.name) at.tofile(at_f.name) at1 = bmt.to_numpy() at2 = BlockMatrix.from_numpy(at).to_numpy() at3 = np.fromfile(bmt_f.name).reshape((n_cols, n_rows)) at4 = BlockMatrix.fromfile(at_f.name, n_cols, n_rows).to_numpy() at5 = BlockMatrix.fromfile(bmt_f.name, n_cols, n_rows).to_numpy() self._assert_eq(at1, at) self._assert_eq(at2, at) self._assert_eq(at3, at) self._assert_eq(at4, at) self._assert_eq(at5, at)
def test_to_from_numpy(self): n_rows = 10 n_cols = 11 data = np.random.rand(n_rows * n_cols) bm = BlockMatrix._create(n_rows, n_cols, data.tolist(), block_size=4) a = data.reshape((n_rows, n_cols)) with hl.TemporaryFilename() as bm_f, hl.TemporaryFilename() as a_f: bm.tofile(bm_f) a.tofile(a_f) a1 = bm.to_numpy() a2 = BlockMatrix.from_numpy(a, block_size=5).to_numpy() a3 = np.frombuffer( hl.current_backend().fs.open(bm_f, mode='rb').read() ).reshape((n_rows, n_cols)) a4 = BlockMatrix.fromfile(a_f, n_rows, n_cols, block_size=3).to_numpy() a5 = BlockMatrix.fromfile(bm_f, n_rows, n_cols).to_numpy() self._assert_eq(a1, a) self._assert_eq(a2, a) self._assert_eq(a3, a) self._assert_eq(a4, a) self._assert_eq(a5, a) bmt = bm.T at = a.T with hl.TemporaryFilename() as bmt_f, hl.TemporaryFilename() as at_f: bmt.tofile(bmt_f) at.tofile(at_f) at1 = bmt.to_numpy() at2 = BlockMatrix.from_numpy(at).to_numpy() at3 = np.frombuffer( hl.current_backend().fs.open(bmt_f, mode='rb').read() ).reshape((n_cols, n_rows)) at4 = BlockMatrix.fromfile(at_f, n_cols, n_rows).to_numpy() at5 = BlockMatrix.fromfile(bmt_f, n_cols, n_rows).to_numpy() self._assert_eq(at1, at) self._assert_eq(at2, at) self._assert_eq(at3, at) self._assert_eq(at4, at) self._assert_eq(at5, at) self._assert_eq(bm.to_numpy(_force_blocking=True), a)
def test_to_from_numpy(self): n_rows = 10 n_cols = 11 data = np.random.rand(n_rows * n_cols) bm = BlockMatrix._create(n_rows, n_cols, data.tolist(), block_size=4) a = data.reshape((n_rows, n_cols)) with tempfile.NamedTemporaryFile() as bm_f: with tempfile.NamedTemporaryFile() as a_f: bm.tofile(bm_f.name) a.tofile(a_f.name) a1 = bm.to_numpy() a2 = BlockMatrix.from_numpy(a, block_size=5).to_numpy() a3 = np.fromfile(bm_f.name).reshape((n_rows, n_cols)) a4 = BlockMatrix.fromfile(a_f.name, n_rows, n_cols, block_size=3).to_numpy() a5 = BlockMatrix.fromfile(bm_f.name, n_rows, n_cols).to_numpy() self._assert_eq(a1, a) self._assert_eq(a2, a) self._assert_eq(a3, a) self._assert_eq(a4, a) self._assert_eq(a5, a) bmt = bm.T at = a.T with tempfile.NamedTemporaryFile() as bmt_f: with tempfile.NamedTemporaryFile() as at_f: bmt.tofile(bmt_f.name) at.tofile(at_f.name) at1 = bmt.to_numpy() at2 = BlockMatrix.from_numpy(at).to_numpy() at3 = np.fromfile(bmt_f.name).reshape((n_cols, n_rows)) at4 = BlockMatrix.fromfile(at_f.name, n_cols, n_rows).to_numpy() at5 = BlockMatrix.fromfile(bmt_f.name, n_cols, n_rows).to_numpy() self._assert_eq(at1, at) self._assert_eq(at2, at) self._assert_eq(at3, at) self._assert_eq(at4, at) self._assert_eq(at5, at) self._assert_eq(bm.to_numpy(_force_blocking=True), a)