本文整理汇总了Python中numpy.shares_memory方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.shares_memory方法的具体用法?Python numpy.shares_memory怎么用?Python numpy.shares_memory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.shares_memory方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ensure_contiguous_ndarray_shares_memory
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_ensure_contiguous_ndarray_shares_memory():
typed_bufs = [
('u', 1, b'adsdasdas'),
('u', 1, bytes(20)),
('i', 8, np.arange(100, dtype=np.int64)),
('f', 8, np.linspace(0, 1, 100, dtype=np.float64)),
('i', 4, array.array('i', b'qwertyuiqwertyui')),
('u', 4, array.array('I', b'qwertyuiqwertyui')),
('f', 4, array.array('f', b'qwertyuiqwertyui')),
('f', 8, array.array('d', b'qwertyuiqwertyui')),
('i', 1, array.array('b', b'qwertyuiqwertyui')),
('u', 1, array.array('B', b'qwertyuiqwertyui')),
('u', 1, mmap.mmap(-1, 10))
]
for expected_kind, expected_itemsize, buf in typed_bufs:
a = ensure_contiguous_ndarray(buf)
assert isinstance(a, np.ndarray)
assert expected_kind == a.dtype.kind
if isinstance(buf, array.array):
assert buf.itemsize == a.dtype.itemsize
else:
assert expected_itemsize == a.dtype.itemsize
assert np.shares_memory(a, memoryview(buf))
示例2: test_memory_details
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_memory_details(dtype):
""" Ensure that:
- function handles non C-contiguous layouts correctly
- output is view of input
- output is not writeable"""
x = np.arange(20).reshape(2, 10).astype(dtype)
x = np.asfortranarray(x)
y = sliding_window_view(x, (5,), 5)
soln = np.array(
[
[[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]],
[[5, 6, 7, 8, 9], [15, 16, 17, 18, 19]],
]
)
assert not y.flags["WRITEABLE"]
assert_allclose(actual=y, desired=soln)
x = np.arange(20).reshape(2, 10)
x = np.ascontiguousarray(x)
y = sliding_window_view(x, (5,), 5)
assert not y.flags["WRITEABLE"]
assert np.shares_memory(x, y)
assert_allclose(actual=y, desired=soln)
示例3: test_basic_index
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_basic_index(shape: Tuple[int, ...], data: st.SearchStrategy):
min_dim = data.draw(st.integers(0, len(shape) + 2), label="min_dim")
max_dim = data.draw(st.integers(min_dim, min_dim + len(shape)), label="max_dim")
index = data.draw(
basic_indices(shape=shape, min_dims=min_dim, max_dims=max_dim), label="index"
)
x = np.zeros(shape, dtype=int)
o = x[index] # raises if invalid index
note(f"`x[index]`: {o}")
if o.size and o.ndim > 0:
assert np.shares_memory(x, o), (
"The basic index should produce a " "view of the original array."
)
assert min_dim <= o.ndim <= max_dim, (
"The dimensionality input constraints " "were not obeyed"
)
示例4: test_density_matrix_copy
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_density_matrix_copy():
sim = cirq.DensityMatrixSimulator()
q = cirq.LineQubit(0)
circuit = cirq.Circuit(cirq.H(q), cirq.H(q))
matrices = []
for step in sim.simulate_moment_steps(circuit):
matrices.append(step.density_matrix(copy=True))
assert all(np.isclose(np.trace(x), 1.0) for x in matrices)
for x, y in itertools.combinations(matrices, 2):
assert not np.shares_memory(x, y)
# If the density matrix is not copied, then applying second Hadamard
# causes old state to be modified.
matrices = []
traces = []
for step in sim.simulate_moment_steps(circuit):
matrices.append(step.density_matrix(copy=False))
traces.append(np.trace(step.density_matrix(copy=False)))
assert any(not np.isclose(np.trace(x), 1.0) for x in matrices)
assert all(np.isclose(x, 1.0) for x in traces)
assert all(not np.shares_memory(x, y)
for x, y in itertools.combinations(matrices, 2))
示例5: test_grid_field_as_array
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_grid_field_as_array():
"""Test adding an array as a grid field."""
fields = ModelDataFields()
fields.new_field_location("grid", 1)
fields.at_grid["const"] = [1.0, 2.0]
assert_array_equal(fields.at_grid["const"], [[1.0, 2.0]])
val = np.array([1.0, 2.0])
fields.at_grid["const"] = val
assert np.shares_memory(val, fields.at_grid["const"])
val.shape = (1, 1, 2, 1)
fields.at_grid["const"] = val
assert_array_equal(fields.at_grid["const"], np.array([[1.0, 2.0]]))
assert np.shares_memory(val, fields.at_grid["const"])
示例6: test_shares_memory_api
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_shares_memory_api():
x = np.zeros([4, 5, 6], dtype=np.int8)
assert_equal(np.shares_memory(x, x), True)
assert_equal(np.shares_memory(x, x.copy()), False)
a = x[:,::2,::3]
b = x[:,::3,::2]
assert_equal(np.shares_memory(a, b), True)
assert_equal(np.shares_memory(a, b, max_work=None), True)
assert_raises(np.TooHardError, np.shares_memory, a, b, max_work=1)
assert_raises(np.TooHardError, np.shares_memory, a, b, max_work=long(1))
示例7: test_may_share_memory_bad_max_work
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_may_share_memory_bad_max_work():
x = np.zeros([1])
assert_raises(OverflowError, np.may_share_memory, x, x, max_work=10**100)
assert_raises(OverflowError, np.shares_memory, x, x, max_work=10**100)
示例8: test_non_ndarray_inputs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_non_ndarray_inputs():
# Regression check for gh-5604
class MyArray(object):
def __init__(self, data):
self.data = data
@property
def __array_interface__(self):
return self.data.__array_interface__
class MyArray2(object):
def __init__(self, data):
self.data = data
def __array__(self):
return self.data
for cls in [MyArray, MyArray2]:
x = np.arange(5)
assert_(np.may_share_memory(cls(x[::2]), x[1::2]))
assert_(not np.shares_memory(cls(x[::2]), x[1::2]))
assert_(np.shares_memory(cls(x[1::3]), x[::2]))
assert_(np.may_share_memory(cls(x[1::3]), x[::2]))
示例9: test_prepare_out
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_prepare_out(self):
class with_prepare(np.ndarray):
__array_priority__ = 10
def __array_prepare__(self, arr, context):
return np.array(arr).view(type=with_prepare)
a = np.array([1]).view(type=with_prepare)
x = np.add(a, a, a)
# Returned array is new, because of the strange
# __array_prepare__ above
assert_(not np.shares_memory(x, a))
assert_equal(x, np.array([2]))
assert_equal(type(x), with_prepare)
示例10: test_constructor_copy
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_constructor_copy():
arr = np.array([0, 1])
result = PandasArray(arr, copy=True)
assert np.shares_memory(result._ndarray, arr) is False
示例11: test_to_numpy_copy
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_to_numpy_copy(arr, as_series):
obj = pd.Index(arr, copy=False)
if as_series:
obj = pd.Series(obj.values, copy=False)
# no copy by default
result = obj.to_numpy()
assert np.shares_memory(arr, result) is True
result = obj.to_numpy(copy=False)
assert np.shares_memory(arr, result) is True
# copy=True
result = obj.to_numpy(copy=True)
assert np.shares_memory(arr, result) is False
示例12: test_copy_false
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_copy_false(self):
samples_like = np.ones((5, 5))
labels = list('abcde')
arr, lab = dimod.as_samples((samples_like, labels))
np.testing.assert_array_equal(arr, np.ones((5, 5)))
self.assertEqual(lab, list('abcde'))
self.assertIs(labels, lab)
self.assertTrue(np.shares_memory(arr, samples_like))
示例13: test_view
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import shares_memory [as 别名]
def test_view(self):
# make sure that the vectors are views
ss = dimod.SampleSet.from_samples([[-1, 1], [1, 1]], dimod.SPIN, energy=[5, 5])
self.assertEqual(set(ss.data_vectors), {'energy', 'num_occurrences'})
for field, vector in ss.data_vectors.items():
np.shares_memory(vector, ss.record)