本文整理匯總了Python中hypothesis.strategies.floats方法的典型用法代碼示例。如果您正苦於以下問題:Python strategies.floats方法的具體用法?Python strategies.floats怎麽用?Python strategies.floats使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類hypothesis.strategies
的用法示例。
在下文中一共展示了strategies.floats方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ibm_compatible_floats
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def ibm_compatible_floats(draw, min_value=None, max_value=None):
if min_value is None:
min_value = MIN_IBM_FLOAT
if max_value is None:
max_value = MAX_IBM_FLOAT
truncated_min_f = max(min_value, MIN_IBM_FLOAT)
truncated_max_f = min(max_value, MAX_IBM_FLOAT)
strategies = []
if truncated_min_f <= LARGEST_NEGATIVE_NORMAL_IBM_FLOAT <= truncated_max_f:
strategies.append(floats(truncated_min_f, LARGEST_NEGATIVE_NORMAL_IBM_FLOAT))
if truncated_min_f <= SMALLEST_POSITIVE_NORMAL_IBM_FLOAT <= truncated_max_f:
strategies.append(floats(SMALLEST_POSITIVE_NORMAL_IBM_FLOAT, truncated_max_f))
if truncated_min_f <= 0 <= truncated_max_f:
strategies.append(just(0.0))
if len(strategies) == 0:
strategies.append(floats(truncated_min_f, truncated_max_f))
ibm = draw(one_of(*strategies))
return ibm
示例2: _strategy_2d_array
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def _strategy_2d_array(dtype, minval=0, maxval=None, **kwargs):
if 'min_side' in kwargs:
min_side = kwargs.pop('min_side')
else:
min_side = 1
if 'max_side' in kwargs:
max_side = kwargs.pop('max_side')
else:
max_side = None
if dtype is np.int:
elems = st.integers(minval, maxval, **kwargs)
elif dtype is np.float:
elems = st.floats(minval, maxval, **kwargs)
elif dtype is np.str:
elems = st.text(min_size=minval, max_size=maxval, **kwargs)
else:
raise ValueError('no elements strategy for dtype', dtype)
return arrays(dtype, array_shapes(2, 2, min_side, max_side), elements=elems)
示例3: from_schema
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def from_schema(schema):
"""Returns a strategy for objects that match the given schema."""
check_schema(schema)
# TODO: actually handle constraints on number/string/array schemas
return dict(
null=st.none(),
bool=st.booleans(),
number=st.floats(allow_nan=False),
string=st.text(),
array=st.lists(st.nothing()),
)[schema["type"]]
# `@st.composite` is one way to write this - another would be to define a
# bare function, and `return st.one_of(st.none(), st.booleans(), ...)` so
# each strategy can be defined individually. Use whichever seems more
# natural to you - the important thing in tests is usually readability!
示例4: test_spherical_list
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def test_spherical_list(nev):
"""The same_len_lists can draw values that when squared overflow floats.
For regular floats, this doesn't flag, but numpy warns on it. It's
considered a caller problem if the value does overflow.
TODO
----
Consider either properly warning or consistently failing when a partial
computation overflows. A solution could be to coerce even regular floats to
numpy, and look for the warning. For now, do the python thing and just
carry on.
"""
n, e, v = nev
inc, azi = spherical(n, e, v)
assert np.all(0 <= azi)
assert np.all(azi < 360)
示例5: test_padding
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
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
示例6: test_negative_log_likelihood
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def test_negative_log_likelihood(data: st.DataObject, labels_as_tensor: bool):
s = data.draw(
hnp.arrays(
shape=hnp.array_shapes(max_side=10, min_dims=2, max_dims=2),
dtype=float,
elements=st.floats(-100, 100),
)
)
y_true = data.draw(
hnp.arrays(
shape=(s.shape[0],),
dtype=hnp.integer_dtypes(),
elements=st.integers(min_value=0, max_value=s.shape[1] - 1),
).map(Tensor if labels_as_tensor else lambda x: x)
)
scores = Tensor(s)
nll = negative_log_likelihood(mg.log(mg.nnet.softmax(scores)), y_true)
nll.backward()
cross_entropy_scores = Tensor(s)
ce = softmax_crossentropy(cross_entropy_scores, y_true)
ce.backward()
assert_allclose(nll.data, ce.data, atol=1e-5, rtol=1e-5)
assert_allclose(scores.grad, cross_entropy_scores.grad, atol=1e-5, rtol=1e-5)
示例7: arrays
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def arrays(self, i: int) -> st.SearchStrategy:
"""
Hypothesis search strategy for drawing an array y to be passed to f(x, ..., y_i,...).
By default, y is drawn to have a shape that is broadcast-compatible with x.
Parameters
----------
i : int
The argument index-location of y in the signature of f.
Returns
-------
hypothesis.searchstrategy.SearchStrategy"""
return hnp.arrays(
shape=self.index_to_arr_shapes.get(i),
dtype=float,
elements=st.floats(*self.index_to_bnds.get(i, self.default_bnds)),
)
示例8: generate_dictionary_with_fixed_tokens
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def generate_dictionary_with_fixed_tokens(draw):
"""
Builds random nested dictionary structure which is then used as JSON to
mask two fixed "token" keys.
Structure is based on TEST_JSON sample fixture defined above.
"""
base = draw(
st.fixed_dictionaries({'token': st.text(printable, min_size=10)})
)
optional = draw(
st.nothing() | st.dictionaries(
st.text(ascii_letters, min_size=1),
st.floats() | st.integers() | st.text(printable) | st.booleans()
| st.nothing(),
min_size=10,
max_size=50
)
)
return {**base, **optional}
示例9: close_enough
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def close_enough(x, y, equal_nan=False, rtol=1e-5, atol=1e-8):
# Might want to adjust rtol and atol for lower precision floats
x, y = np.asarray(x), np.asarray(y)
if x.shape != y.shape:
return False
if x.dtype != y.dtype:
return False
if x.dtype.kind == "f":
assert y.dtype.kind == "f"
# Note: equal_nan only considered in both float case!
return np.allclose(x, y, equal_nan=equal_nan, rtol=rtol, atol=atol)
return np.all(x == y)
示例10: choice
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def choice(values):
"""
One value from a limited set.
Args:
values:
Iterable with values that will be produced by strategy. Python enums
are iterable and thus can be used as arguments for this strategy.
Examples:
>>> from hypothesis import strategies as st, given
>>> from math import sin, cos
>>> @given(choice([sin, cos]), st.floats(-1000, 1000))
... def check_range(fn, x):
... assert -1 <= fn(x) <= 1
"""
values = list(values)
return st.integers(min_value=0, max_value=len(values) - 1).map(values.__getitem__)
示例11: strategy
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def strategy(self):
if self._multiple_of is not None:
maximum = self._maximum
if maximum is not None:
maximum = math.floor(maximum / self._multiple_of)
minimum = self._minimum
if minimum is not None:
minimum = math.ceil(minimum / self._multiple_of)
strategy = hy_st.integers(min_value=minimum, max_value=maximum)
strategy = strategy.map(lambda x: x * self._multiple_of)
else:
strategy = hy_st.floats(min_value=self._minimum,
max_value=self._maximum)
if self._exclusive_maximum:
strategy = strategy.filter(lambda x: x < self._maximum)
if self._exclusive_minimum:
strategy = strategy.filter(lambda x: x > self._minimum)
return strategy
示例12: enums_of_primitives
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def enums_of_primitives(draw):
"""Generate enum classes with primitive values."""
names = draw(st.sets(st.text(min_size=1), min_size=1))
n = len(names)
vals = draw(
st.one_of(
st.sets(
st.one_of(
st.integers(),
st.floats(allow_nan=False),
st.text(min_size=1),
),
min_size=n,
max_size=n,
)
)
)
return Enum("HypEnum", list(zip(names, vals)))
示例13: ibm_compatible_negative_floats
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def ibm_compatible_negative_floats(draw):
return draw(floats(MIN_IBM_FLOAT, LARGEST_NEGATIVE_NORMAL_IBM_FLOAT))
示例14: ibm_compatible_positive_floats
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def ibm_compatible_positive_floats(draw):
return draw(floats(SMALLEST_POSITIVE_NORMAL_IBM_FLOAT, MAX_IBM_FLOAT))
示例15: ibm_compatible_non_negative_floats
# 需要導入模塊: from hypothesis import strategies [as 別名]
# 或者: from hypothesis.strategies import floats [as 別名]
def ibm_compatible_non_negative_floats(draw):
return draw(one_of(
just(0.0),
floats(SMALLEST_POSITIVE_NORMAL_IBM_FLOAT, MAX_IBM_FLOAT)))