本文整理汇总了Python中hail.linalg.BlockMatrix.from_numpy方法的典型用法代码示例。如果您正苦于以下问题:Python BlockMatrix.from_numpy方法的具体用法?Python BlockMatrix.from_numpy怎么用?Python BlockMatrix.from_numpy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hail.linalg.BlockMatrix
的用法示例。
在下文中一共展示了BlockMatrix.from_numpy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matrix_ops
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_matrix_ops(self):
nm = np.matrix([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
m = BlockMatrix.from_numpy(nm, block_size=2)
nrow = np.matrix([[7.0, 8.0, 9.0]])
row = BlockMatrix.from_numpy(nrow, block_size=2)
self._assert_eq(m.T, nm.T)
self._assert_eq(m.T, nm.T)
self._assert_eq(row.T, nrow.T)
self._assert_eq(m @ m.T, nm @ nm.T)
self._assert_eq(m @ nm.T, nm @ nm.T)
self._assert_eq(row @ row.T, nrow @ nrow.T)
self._assert_eq(row @ nrow.T, nrow @ nrow.T)
self._assert_eq(m.T @ m, nm.T @ nm)
self._assert_eq(m.T @ nm, nm.T @ nm)
self._assert_eq(row.T @ row, nrow.T @ nrow)
self._assert_eq(row.T @ nrow, nrow.T @ nrow)
self.assertRaises(ValueError, lambda: m @ m)
self.assertRaises(ValueError, lambda: m @ nm)
self._assert_eq(m.diagonal(), np.array([1.0, 5.0]))
self._assert_eq(m.T.diagonal(), np.array([1.0, 5.0]))
self._assert_eq((m @ m.T).diagonal(), np.array([14.0, 77.0]))
示例2: test_stage_locally
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_stage_locally(self):
nd = np.arange(0, 80, dtype=float).reshape(8, 10)
bm_uri = new_temp_file()
BlockMatrix.from_numpy(nd, block_size=3).write(bm_uri, stage_locally=True)
bm = BlockMatrix.read(bm_uri)
self._assert_eq(nd, bm)
示例3: test_sum
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_sum(self):
def sums_agree(bm, nd):
self.assertAlmostEqual(bm.sum(), np.sum(nd))
self._assert_close(bm.sum(axis=0), np.sum(nd, axis=0, keepdims=True))
self._assert_close(bm.sum(axis=1), np.sum(nd, axis=1, keepdims=True))
nd = np.random.normal(size=(11, 13))
bm = BlockMatrix.from_numpy(nd, block_size=3)
nd2 = np.zeros(shape=(5, 7))
nd2[2, 4] = 1.0
nd2[2, 5] = 2.0
nd2[3, 4] = 3.0
nd2[3, 5] = 4.0
bm2 = BlockMatrix.from_numpy(nd2, block_size=2).sparsify_rectangles([[2, 4, 4, 6]])
bm3 = BlockMatrix.from_numpy(nd2, block_size=2).sparsify_rectangles([[2, 4, 4, 6], [0, 5, 0, 1]])
bm4 = BlockMatrix.from_numpy(nd2, block_size=2).sparsify_rectangles([[2, 4, 4, 6], [0, 1, 0, 7]])
nd5 = np.zeros(shape=(5, 7))
bm5 = BlockMatrix.fill(5, 7, value=0.0, block_size=2).sparsify_rectangles([])
sums_agree(bm, nd)
sums_agree(bm2, nd2)
sums_agree(bm3, nd2)
sums_agree(bm4, nd2)
sums_agree(bm5, nd5)
示例4: test_sparsify_band
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_sparsify_band(self):
nd = np.array([[ 1.0, 2.0, 3.0, 4.0],
[ 5.0, 6.0, 7.0, 8.0],
[ 9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0]])
bm = BlockMatrix.from_numpy(nd, block_size=2)
self._assert_eq(
bm.sparsify_band(lower=-1, upper=2),
np.array([[ 1., 2., 3., 0.],
[ 5., 6., 7., 8.],
[ 0., 10., 11., 12.],
[ 0., 0., 15., 16.]]))
self._assert_eq(
bm.sparsify_band(lower=0, upper=0, blocks_only=True),
np.array([[ 1., 2., 0., 0.],
[ 5., 6., 0., 0.],
[ 0., 0., 11., 12.],
[ 0., 0., 15., 16.]]))
nd2 = np.arange(0, 80, dtype=float).reshape(8, 10)
bm2 = BlockMatrix.from_numpy(nd2, block_size=3)
for bounds in [[0, 0], [1, 1], [2, 2], [-5, 5], [-7, 0], [0, 9], [-100, 100]]:
lower, upper = bounds
actual = bm2.sparsify_band(lower, upper, blocks_only=False).to_numpy()
mask = np.fromfunction(lambda i, j: (lower <= j - i) * (j - i <= upper), (8, 10))
self._assert_eq(actual, nd2 * mask)
示例5: test_svd
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_svd(self):
def assert_same_columns_up_to_sign(a, b):
for j in range(a.shape[1]):
assert np.allclose(a[:, j], b[:, j]) or np.allclose(-a[:, j], b[:, j])
x0 = np.array([[-2.0, 0.0, 3.0],
[-1.0, 2.0, 4.0]])
u0, s0, vt0 = np.linalg.svd(x0, full_matrices=False)
x = BlockMatrix.from_numpy(x0)
# _svd
u, s, vt = x.svd()
assert_same_columns_up_to_sign(u, u0)
assert np.allclose(s, s0)
assert_same_columns_up_to_sign(vt.T, vt0.T)
s = x.svd(compute_uv=False)
assert np.allclose(s, s0)
# left _svd_gramian
u, s, vt = x.svd(complexity_bound=0)
assert_same_columns_up_to_sign(u, u0)
assert np.allclose(s, s0)
assert_same_columns_up_to_sign(vt.to_numpy().T, vt0.T)
s = x.svd(compute_uv=False, complexity_bound=0)
assert np.allclose(s, s0)
# right _svd_gramian
x = BlockMatrix.from_numpy(x0.T)
u, s, vt = x.svd(complexity_bound=0)
assert_same_columns_up_to_sign(u.to_numpy(), vt0.T)
assert np.allclose(s, s0)
assert_same_columns_up_to_sign(vt.T, u0)
s = x.svd(compute_uv=False, complexity_bound=0)
assert np.allclose(s, s0)
# left _svd_gramian when dimensions agree
x = BlockMatrix.from_numpy(x0[:, :2])
u, s, vt = x.svd(complexity_bound=0)
assert isinstance(u, np.ndarray)
assert isinstance(vt, BlockMatrix)
# rank-deficient X sets negative eigenvalues to 0.0
a = np.array([[0.0, 1.0, np.e, np.pi, 10.0, 25.0]])
x0 = a.T @ a # rank 1
e, _ = np.linalg.eigh(x0 @ x0.T)
x = BlockMatrix.from_numpy(x0)
_, s, _ = x.svd(complexity_bound=0)
assert np.all(s >= 0.0)
s = x.svd(compute_uv=False, complexity_bound=0)
assert np.all(s >= 0)
示例6: test_write_overwrite
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_write_overwrite(self):
path = new_temp_file()
bm = BlockMatrix.from_numpy(np.array([[0]]))
bm.write(path)
self.assertRaises(FatalError, lambda: bm.write(path))
bm2 = BlockMatrix.from_numpy(np.array([[1]]))
bm2.write(path, overwrite=True)
self._assert_eq(BlockMatrix.read(path), bm2)
示例7: test_export_rectangles
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_export_rectangles(self):
nd = np.arange(0, 80, dtype=float).reshape(8, 10)
rects1 = [[0, 1, 0, 1], [4, 5, 7, 8]]
rects2 = [[4, 5, 0, 10], [0, 8, 4, 5]]
rects3 = [[0, 1, 0, 1], [1, 2, 1, 2], [2, 3, 2, 3],
[3, 5, 3, 6], [3, 6, 3, 7], [3, 7, 3, 8],
[4, 5, 0, 10], [0, 8, 4, 5], [0, 8, 0, 10]]
for rects in [rects1, rects2, rects3]:
for block_size in [3, 4, 10]:
bm_uri = new_temp_file()
rect_path = new_local_temp_dir()
rect_uri = local_path_uri(rect_path)
(BlockMatrix.from_numpy(nd, block_size=block_size)
.sparsify_rectangles(rects)
.write(bm_uri, force_row_major=True))
BlockMatrix.export_rectangles(bm_uri, rect_uri, rects)
for (i, r) in enumerate(rects):
file = rect_path + '/rect-' + str(i) + '_' + '-'.join(map(str, r))
expected = nd[r[0]:r[1], r[2]:r[3]]
actual = np.loadtxt(file, ndmin = 2)
self._assert_eq(expected, actual)
rect_path_bytes = new_local_temp_dir()
rect_uri_bytes = local_path_uri(rect_path_bytes)
BlockMatrix.export_rectangles(bm_uri, rect_uri_bytes, rects, binary=True)
for (i, r) in enumerate(rects):
file = rect_path_bytes + '/rect-' + str(i) + '_' + '-'.join(map(str, r))
expected = nd[r[0]:r[1], r[2]:r[3]]
actual = np.reshape(np.fromfile(file), (r[1] - r[0], r[3] - r[2]))
self._assert_eq(expected, actual)
bm_uri = new_temp_file()
rect_uri = new_temp_file()
(BlockMatrix.from_numpy(nd, block_size=5)
.sparsify_rectangles([[0, 1, 0, 1]])
.write(bm_uri, force_row_major=True))
with self.assertRaises(FatalError) as e:
BlockMatrix.export_rectangles(bm_uri, rect_uri, [[5, 6, 5, 6]])
self.assertEquals(e.msg, 'block (1, 1) missing for rectangle 0 with bounds [5, 6, 5, 6]')
示例8: test_sparsify_triangle
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_sparsify_triangle(self):
nd = np.array([[ 1.0, 2.0, 3.0, 4.0],
[ 5.0, 6.0, 7.0, 8.0],
[ 9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0]])
bm = BlockMatrix.from_numpy(nd, block_size=2)
self.assertFalse(bm.is_sparse)
self.assertTrue(bm.sparsify_triangle().is_sparse)
self._assert_eq(
bm.sparsify_triangle(),
np.array([[ 1., 2., 3., 4.],
[ 0., 6., 7., 8.],
[ 0., 0., 11., 12.],
[ 0., 0., 0., 16.]]))
self._assert_eq(
bm.sparsify_triangle(lower=True),
np.array([[ 1., 0., 0., 0.],
[ 5., 6., 0., 0.],
[ 9., 10., 11., 0.],
[13., 14., 15., 16.]]))
self._assert_eq(
bm.sparsify_triangle(blocks_only=True),
np.array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 0., 0., 11., 12.],
[ 0., 0., 15., 16.]]))
示例9: test_export_rectangles
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_export_rectangles(self):
nd = np.arange(0, 80, dtype=float).reshape(8, 10)
rects1 = [[0, 1, 0, 1], [4, 5, 7, 8]]
rects2 = [[4, 5, 0, 10], [0, 8, 4, 5]]
rects3 = [[0, 1, 0, 1], [1, 2, 1, 2], [2, 3, 2, 3],
[3, 5, 3, 6], [3, 6, 3, 7], [3, 7, 3, 8],
[4, 5, 0, 10], [0, 8, 4, 5], [0, 8, 0, 10]]
for rects in [rects1, rects2, rects3]:
for block_size in [3, 4, 10]:
rect_path = new_local_temp_dir()
rect_uri = local_path_uri(rect_path)
bm = BlockMatrix.from_numpy(nd, block_size=block_size)
bm.export_rectangles(rect_uri, rects)
self._assert_rectangles_eq(nd, rect_path, rects)
rect_path_bytes = new_local_temp_dir()
rect_uri_bytes = local_path_uri(rect_path_bytes)
bm.export_rectangles(rect_uri_bytes, rects, binary=True)
self._assert_rectangles_eq(nd, rect_path_bytes, rects, binary=True)
示例10: test_to_from_numpy
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
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)
示例11: test_sparsify_row_intervals
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_sparsify_row_intervals(self):
nd = np.array([[ 1.0, 2.0, 3.0, 4.0],
[ 5.0, 6.0, 7.0, 8.0],
[ 9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0]])
bm = BlockMatrix.from_numpy(nd, block_size=2)
self._assert_eq(
bm.sparsify_row_intervals(
starts=[1, 0, 2, 2],
stops= [2, 0, 3, 4]),
np.array([[ 0., 2., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 11., 0.],
[ 0., 0., 15., 16.]]))
self._assert_eq(
bm.sparsify_row_intervals(
starts=[1, 0, 2, 2],
stops= [2, 0, 3, 4],
blocks_only=True),
np.array([[ 1., 2., 0., 0.],
[ 5., 6., 0., 0.],
[ 0., 0., 11., 12.],
[ 0., 0., 15., 16.]]))
nd2 = np.random.normal(size=(8, 10))
bm2 = BlockMatrix.from_numpy(nd2, block_size=3)
for bounds in [[[0, 1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7, 8]],
[[0, 0, 5, 3, 4, 5, 8, 2],
[9, 0, 5, 3, 4, 5, 9, 5]],
[[0, 5, 10, 8, 7, 6, 5, 4],
[0, 5, 10, 9, 8, 7, 6, 5]]]:
starts, stops = bounds
actual = bm2.sparsify_row_intervals(starts, stops, blocks_only=False).to_numpy()
expected = nd2.copy()
for i in range(0, 8):
for j in range(0, starts[i]):
expected[i, j] = 0.0
for j in range(stops[i], 10):
expected[i, j] = 0.0
self._assert_eq(actual, expected)
示例12: test_special_elementwise_ops
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_special_elementwise_ops(self):
nm = np.array([[1.0, 2.0, 3.0, 3.14], [4.0, 5.0, 6.0, 12.12]])
m = BlockMatrix.from_numpy(nm)
self._assert_close(m ** 3, nm ** 3)
self._assert_close(m.sqrt(), np.sqrt(nm))
self._assert_close(m.ceil(), np.ceil(nm))
self._assert_close(m.floor(), np.floor(nm))
self._assert_close(m.log(), np.log(nm))
self._assert_close((m - 4).abs(), np.abs(nm - 4))
示例13: test_export_blocks
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_export_blocks(self):
nd = np.ones(shape=(8, 10))
bm = BlockMatrix.from_numpy(nd, block_size=20)
bm_path = new_local_temp_dir()
bm_uri = local_path_uri(bm_path)
bm.export_blocks(bm_uri, binary=True)
actual = BlockMatrix.rectangles_to_numpy(bm_path, binary=True)
self._assert_eq(nd, actual)
示例14: test_sum_with_sparsify
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_sum_with_sparsify(self):
nd = np.zeros(shape=(5, 7))
nd[2, 4] = 1.0
nd[2, 5] = 2.0
nd[3, 4] = 3.0
nd[3, 5] = 4.0
bm = BlockMatrix.from_numpy(nd, block_size=2).sparsify_rectangles([[2, 4, 4, 6]])
bm2 = BlockMatrix.from_numpy(nd, block_size=2).sparsify_rectangles([[2, 4, 4, 6], [0, 5, 0, 1]])
bm3 = BlockMatrix.from_numpy(nd, block_size=2).sparsify_rectangles([[2, 4, 4, 6], [0, 1, 0, 7]])
nd4 = np.zeros(shape=(5, 7))
bm4 = BlockMatrix.fill(5, 7, value=0.0, block_size=2).sparsify_rectangles([])
self.assert_sums_agree(bm, nd)
self.assert_sums_agree(bm2, nd)
self.assert_sums_agree(bm3, nd)
self.assert_sums_agree(bm4, nd4)
示例15: test_rectangles_to_numpy
# 需要导入模块: from hail.linalg import BlockMatrix [as 别名]
# 或者: from hail.linalg.BlockMatrix import from_numpy [as 别名]
def test_rectangles_to_numpy(self):
nd = np.array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
rects = [[0, 3, 0, 1], [1, 2, 0, 2]]
rect_path = new_local_temp_dir()
rect_uri = local_path_uri(rect_path)
BlockMatrix.from_numpy(nd).export_rectangles(rect_uri, rects)
rect_bytes_path = new_local_temp_dir()
rect_bytes_uri = local_path_uri(rect_bytes_path)
BlockMatrix.from_numpy(nd).export_rectangles(rect_bytes_uri, rects, binary=True)
expected = np.array([[1.0, 0.0],
[4.0, 5.0],
[7.0, 0.0]])
self._assert_eq(expected, BlockMatrix.rectangles_to_numpy(rect_path))
self._assert_eq(expected, BlockMatrix.rectangles_to_numpy(rect_bytes_path, binary=True))