Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
x0 = np.copy(x)
y0 = np.copy(y)
x_arr = Tensor(np.copy(x))
y_arr = Tensor(np.copy(y))
x1_arr = x_arr[:]
try:
x0[index] = y0 # don't permit invalid set-items
except ValueError:
assume(False)
return
grad = data.draw(
hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(1, 10), unique=True),
label="grad",
)
x1_arr[index] = y_arr
(x1_arr * grad).sum().backward()
assert_allclose(x1_arr.data, x0)
assert_allclose(y_arr.data, y0)
dx, dy = numerical_gradient_full(
setitem, x, y, back_grad=grad, kwargs=dict(index=index)
)
assert_allclose(x_arr.grad, dx)
assert_allclose(y_arr.grad, dy)
@given(arrays(dtype=np.float32,
shape=array_shapes(),
elements=st.floats(allow_nan=False, allow_infinity=False, max_value=1e14, min_value=-1e14)))
def test_array_softmax(data):
array_softmax(data)
nps.arrays(object, (2, 2), st.lists(st.integers(), min_size=1, max_size=1))
)
def test_does_not_flatten_arrays_of_lists(arr):
assert isinstance(arr[0][0], list)
@given(arrays(np.object, array_shapes(), elements=mjd_strs()))
def test_str_to_mjds_array(s):
i, f = str_to_mjds(s)
assert np.shape(i) == np.shape(f) == np.shape(s)
for i_i, f_i, s_i in np.nditer([i, f, s], flags=["refs_ok"]):
assert i_i, f_i == str_to_mjds(s_i)
r_range = tabulation[Delhommeau_f90][0]
Z_range = tabulation[Delhommeau_f90][1]
# Make 2D arrays
r = r_range[:, None] * np.ones_like(Z_range)[None, :]
Z = np.ones_like(r_range)[:, None] * Z_range[None, :]
R1 = np.sqrt(np.square(r) + np.square(Z))
# Compare Z1, for great values of Z, where both methods are as accurate
Del = tabulation[Delhommeau_f90][2][:, :, 0, 1] - pi/R1
Xie = tabulation[XieDelhommeau_f90][2][:, :, 0, 1]
assert np.allclose(Del[abs(Z) > 1], Xie[abs(Z) > 1], atol=1e-3)
points = arrays(np.float, (3,),
elements=floats(min_value=-1e5, max_value=1e5, allow_infinity=False, allow_nan=False)
).filter(lambda x: x[2] < -1e-4)
cores = one_of(just(Delhommeau_f90), just(XieDelhommeau_f90))
frequencies = floats(min_value=1e-1, max_value=1e1)
depths = one_of(floats(min_value=10.0, max_value=100.0), just(np.infty))
gravity = 9.8
def wave_part_Green_function(Xi, Xj, omega, depth, core=Delhommeau_f90):
if depth == np.infty:
wavenumber = omega**2 / gravity
else:
wavenumber = newton(lambda x: x*np.tanh(x) - omega**2*depth/gravity, x0=1.0)/depth
if depth < np.infty:
ambda, ar, nexp = core.old_prony_decomposition.lisc(omega**2 * depth/gravity, wavenumber * depth)
base_kernel, kernel_variable_names = draw(kernels(
batch_shape=batch_shape,
event_dim=event_dim,
feature_dim=feature_dim,
feature_ndims=feature_ndims,
enable_vars=False,
depth=depth-1))
# SchurComplement requires the inputs to have one example dimension.
fixed_inputs = draw(kernel_input(
batch_shape=batch_shape,
example_ndims=1,
feature_dim=feature_dim,
feature_ndims=feature_ndims))
# Positive shift to ensure the divisor matrix is PD.
diag_shift = np.float64(draw(hpnp.arrays(
dtype=np.float64,
shape=tensorshape_util.as_list(batch_shape),
elements=hps.floats(1, 100, allow_nan=False, allow_infinity=False))))
hp.note('Forming SchurComplement kernel with fixed_inputs: {} '
'and diag_shift: {}'.format(fixed_inputs, diag_shift))
schur_complement_params = {
'fixed_inputs': fixed_inputs,
'diag_shift': diag_shift
}
for param_name in schur_complement_params:
if enable_vars and draw(hps.booleans()):
kernel_variable_names.append(param_name)
schur_complement_params[param_name] = tf.Variable(
schur_complement_params[param_name], name=param_name)
x=hnp.arrays(
dtype=dtype_strat_numpy,
shape=hnp.array_shapes(max_dims=5, min_dims=1, max_side=20),
),
)
def test_sliding_window(data, x):
""" Test variations of window-shape, step, and dilation for sliding window
view of N-dimensional array."""
win_dim = data.draw(st.integers(1, x.ndim), label="win_dim")
win_shape = data.draw(
st.tuples(*(st.integers(1, s) for s in x.shape[-win_dim:])), label="win_shape"
)
step = data.draw(
st.tuples(*(st.integers(1, s) for s in x.shape[-win_dim:])), label="step"
)
condition=hnp.arrays(shape=hnp.array_shapes(min_dims=1), dtype=bool),
x=st.none() | hnp.arrays(shape=hnp.array_shapes(min_dims=1), dtype=int,),
y=st.none() | hnp.arrays(shape=hnp.array_shapes(min_dims=1), dtype=int,),
)
def test_where_input_validation(condition, x, y):
args = [i for i in (x, y) if i is not None]
try:
np.where(condition, *args)
except Exception as e:
with pytest.raises(type(e)):
where(condition, *args)
return
def test_padding(ndim: int, data: st.DataObject):
"""Ensure that convolving a padding-only image with a commensurate kernel yields the single entry: 0"""
padding = data.draw(
st.integers(1, 3) | st.tuples(*[st.integers(1, 3)] * ndim), label="padding"
)
x = Tensor(
data.draw(
hnp.arrays(shape=(1, 1) + (0,) * ndim, dtype=float, elements=st.floats()),
label="x",
)
)
pad_tuple = padding if isinstance(padding, tuple) else (padding,) * ndim
kernel = data.draw(
hnp.arrays(
shape=(1, 1) + tuple(2 * p for p in pad_tuple),
dtype=float,
elements=st.floats(allow_nan=False, allow_infinity=False),
)
)
out = conv_nd(x, kernel, padding=padding, stride=1)
assert out.shape == (1,) * x.ndim
assert out.item() == 0.0
out.sum().backward()
assert x.grad.shape == x.shape
def test_setitem_broadcast_bool_index(x, data):
""" index mixes boolean and int-array indexing"""
rows = np.array([False, True, False, True])
columns = np.array([0, 2], dtype=np.intp)
index = np.ix_(rows, columns)
o = np.asarray(x[index])
y = data.draw(
hnp.arrays(
shape=broadcastable_shapes(o.shape, max_dims=o.ndim),
dtype=float,
elements=st.floats(-10.0, 10.0),
),
label="y",
)
grad = data.draw(
hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(1, 10), unique=True),
label="grad",
)
x0 = np.copy(x)
y0 = np.copy(y)
x_arr = Tensor(np.copy(x))