def anyarray( draw, min_dims: int = 0, max_dims: int = 2, include_complex_numbers: bool = True, dtype: Optional[np.dtype] = None, ): if dtype is None: if include_complex_numbers: dtype = one_of( integer_dtypes(), floating_dtypes(), complex_number_dtypes() ) else: dtype = one_of(integer_dtypes(), floating_dtypes()) arr = draw( arrays( dtype=dtype, shape=array_shapes(min_dims=min_dims, max_dims=max_dims), ) ) assume(not np.any(np.isnan(arr))) assume(np.all(np.isfinite(arr))) return arr
def test_weighted_negative_log_likelihood_vs_softmax_cross_entropy( data: st.DataObject, labels_as_tensor: bool): s = data.draw( hnp.arrays( shape=hnp.array_shapes(min_side=1, 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)) weights = data.draw( hnp.arrays( shape=(s.shape[1], ), dtype=float, elements=st.floats(1e-8, 100), )) scores = Tensor(s) weights = Tensor(weights) for score, y in zip(scores, y_true): score = mg.log(mg.nnet.softmax(score.reshape(1, -1))) y = y.reshape(-1) nll = negative_log_likelihood(score, y) weighted_nll = negative_log_likelihood(score, y, weights=weights) assert np.isclose(weighted_nll.data, weights[y.data].data * nll.data)
def test_softmax_crossentropy(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) softmax_cross = softmax_crossentropy(scores, y_true, constant=False) softmax_cross.backward() mygrad_scores = Tensor(s) probs = softmax(mygrad_scores) correct_labels = (range(len(y_true)), y_true.data if labels_as_tensor else y_true) truth = np.zeros(mygrad_scores.shape) truth[correct_labels] = 1 mygrad_cross = (-1 / s.shape[0]) * (log(probs) * truth).sum() mygrad_cross.backward() assert_allclose(softmax_cross.data, mygrad_cross.data, atol=1e-5, rtol=1e-5) assert_allclose(scores.grad, mygrad_scores.grad, atol=1e-5, rtol=1e-5)
def test_multiclass_hinge(data): """ Test the built-in implementation of multiclass hinge against the pure pygrad version""" 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), )) l = data.draw( hnp.arrays( shape=(s.shape[0], ), dtype=hnp.integer_dtypes(), elements=st.integers(min_value=0, max_value=s.shape[1] - 1), )) hinge_scores = Tensor(s) hinge_loss = multiclass_hinge(hinge_scores, l, constant=False) hinge_loss.backward() pygrad_scores = Tensor(s) correct_labels = (range(len(l)), l) correct_class_scores = pygrad_scores[correct_labels] # Nx1 Lij = pygrad_scores - correct_class_scores[:, np.newaxis] + 1.0 # NxC margins Lij[Lij <= 0] = 0 Lij[correct_labels] = 0 pygrad_loss = Lij.sum() / pygrad_scores.shape[0] pygrad_loss.backward() assert_allclose(hinge_loss.data, pygrad_loss.data) assert_allclose(pygrad_scores.grad, hinge_scores.grad)
def test_softmax_crossentropy(data): """ Test the built-in implementation of multiclass hinge against the pure pygrad version""" 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), )) l = data.draw( hnp.arrays( shape=(s.shape[0], ), dtype=hnp.integer_dtypes(), elements=st.integers(min_value=0, max_value=s.shape[1] - 1), )) scores = Tensor(s) softmax_cross = softmax_crossentropy(scores, l, constant=False) softmax_cross.backward() pygrad_scores = Tensor(s) probs = softmax(pygrad_scores) correct_labels = (range(len(l)), l) truth = np.zeros(pygrad_scores.shape) truth[correct_labels] = 1 pygrad_cross = (-1 / s.shape[0]) * (log(probs) * truth).sum() pygrad_cross.backward() assert_allclose(softmax_cross.data, pygrad_cross.data, atol=1e-5, rtol=1e-5) assert_allclose(scores.grad, pygrad_scores.grad, atol=1e-5, rtol=1e-5)
def test_ranked_margin(shape, margin, data): x1 = data.draw( hnp.arrays(shape=shape, dtype=float, elements=st.floats(-1000, 1000)), label="x1", ) x2 = data.draw( hnp.arrays(shape=shape, dtype=float, elements=st.floats(-1000, 1000)), label="x2", ) y = data.draw( st.sampled_from((-1, 1)) | hnp.arrays( shape=shape[:1], dtype=hnp.integer_dtypes(), elements=st.sampled_from((-1, 1)), ), label="y", ) x1_copy = np.copy(x1) x2_copy = np.copy(x2) y_copy = np.copy(y) x1_dum = mg.Tensor(x1) x2_dum = mg.Tensor(x2) x1_real = mg.Tensor(x1) x2_real = mg.Tensor(x2) loss_dum = simple_loss(x1_dum, x2_dum, y, margin) loss_real = margin_ranking_loss(x1_real, x2_real, y, margin) assert_allclose(actual=loss_real.data, desired=loss_dum.data, err_msg="losses don't match") assert_array_equal(x1, x1_copy, err_msg="`x1` was mutated by forward") assert_array_equal(x2, x2_copy, err_msg="`x2` was mutated by forward") if isinstance(y, np.ndarray): assert_array_equal(y, y_copy, err_msg="`y` was mutated by forward") loss_dum.backward() loss_real.backward() assert_allclose(actual=x1_real.grad, desired=x1_dum.grad, err_msg="x1.grad doesn't match") assert_allclose(actual=x2_real.grad, desired=x2_dum.grad, err_msg="x2.grad doesn't match") assert_array_equal(x1, x1_copy, err_msg="`x1` was mutated by backward") assert_array_equal(x2, x2_copy, err_msg="`x2` was mutated by backward") if isinstance(y, np.ndarray): assert_array_equal(y, y_copy, err_msg="`y` was mutated by backward") loss_real.null_gradients() assert x1_real.grad is None assert x2_real.grad is None
def get_scalar_dtype_strategy(exclude=None): """ A `hypothesis` strategy yielding """ possible_strategies = { "datetime": hyp_np.datetime64_dtypes(max_period="ms", min_period="ns"), "uint": hyp_np.unsigned_integer_dtypes(), "int": hyp_np.integer_dtypes(), "float": hyp_np.floating_dtypes(), "byte": hyp_np.byte_string_dtypes(), "unicode": hyp_np.unicode_string_dtypes(), } if exclude is None: exclude = {} elif not isinstance(exclude, list): exclude = [exclude] for ex in exclude: if ex in possible_strategies: del possible_strategies[ex] else: raise ValueError( "Strategy {} unknown. Possible values are {}".format( ex, possible_strategies.keys() ) ) return hyp_st.one_of(*list(possible_strategies.values()))
def test_can_restrict_endianness(data, byteorder): dtype = data.draw( nps.integer_dtypes(endianness=byteorder, sizes=(16, 32, 64))) if byteorder == ("<" if sys.byteorder == "little" else ">"): assert dtype.byteorder == "=" else: assert dtype.byteorder == byteorder
def test_negative_log_likelihood_vs_softmax_cross_entropy( 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)
def ints_floats_datetimes_and_timedeltas(draw): dtypes = ( unsigned_integer_dtypes(endianness="="), integer_dtypes(endianness="="), floating_dtypes(endianness="="), datetime64_dtypes(endianness="="), timedelta64_dtypes(endianness="="), ) return draw(one_of(dtypes))
def ints_floats_complex_or_booleans(draw): dtypes = ( unsigned_integer_dtypes(endianness="="), integer_dtypes(endianness="="), floating_dtypes(endianness="="), # complex_number_dtypes(endianness="="), boolean_dtypes(), ) return draw(one_of(dtypes))
def ints_floats_or_complex_dtypes(draw): # Endianness needs to be specified for now, otherwise the byte-order may get flipped # https://jira/browse/SOQTEST-6478 dtypes = ( unsigned_integer_dtypes(endianness="="), integer_dtypes(endianness="="), floating_dtypes(endianness="="), # complex_number_dtypes(endianness="="), ) return draw(one_of(dtypes))
def ints_or_floats_dtypes(draw): # Endianness needs to be specified for now, otherwise the byte-order may get flipped # https://jira/browse/SOQTEST-6478 # Half floats are not supported. dtypes = ( unsigned_integer_dtypes(endianness="="), integer_dtypes(endianness="="), floating_dtypes(endianness="=", sizes=(32, 64)), ) return draw(one_of(dtypes))
def array_with_two_entries(draw, array_length=10_000): length = draw(integers(1, max_value=array_length)) arr = draw( arrays( dtype=one_of(integer_dtypes(), floating_dtypes()), shape=(length, 2), )) assume(not np.any(np.isnan(arr))) assume(np.all(np.isfinite(arr))) return arr
def gen_codes_for_fmt(fmt, include_nas=False, valid_only=True): elems = None if not valid_only \ else hs.sampled_from( sorted(fmt.keys()) + [np.nan] if include_nas \ else sorted(fmt.keys())) dtypes = hnp.floating_dtypes(endianness='=') \ if include_nas or valid_only \ else hs.one_of(hnp.floating_dtypes(endianness='='), hnp.integer_dtypes(endianness='=')) return hnp.arrays(dtype=dtypes, shape=hnp.array_shapes(max_dims=1, max_side=1000), elements=elems)
def test_generate_arbitrary_indices(data): min_size = data.draw(st.integers(0, 10), "min_size") max_size = data.draw(st.none() | st.integers(min_size, min_size + 10), "max_size") unique = data.draw(st.booleans(), "unique") dtype = data.draw( st.one_of( npst.boolean_dtypes(), npst.integer_dtypes(endianness="="), npst.floating_dtypes(endianness="="), npst.complex_number_dtypes(endianness="="), npst.datetime64_dtypes(endianness="="), npst.timedelta64_dtypes(endianness="="), ).filter(supported_by_pandas), "dtype", ) pass_elements = data.draw(st.booleans(), "pass_elements") converted_dtype = pandas.Index([], dtype=dtype).dtype try: inferred_dtype = pandas.Index([data.draw(npst.from_dtype(dtype)) ]).dtype if pass_elements: elements = npst.from_dtype(dtype) dtype = None else: elements = None index = data.draw( pdst.indexes( elements=elements, dtype=dtype, min_size=min_size, max_size=max_size, unique=unique, )) except Exception as e: if type(e).__name__ == "OutOfBoundsDatetime": # See https://github.com/HypothesisWorks/hypothesis-python/pull/826 reject() else: raise if dtype is None: assert index.dtype == inferred_dtype else: assert index.dtype == converted_dtype if unique: assert len(set(index.values)) == len(index)
def get_nll_args(*arrs): (s, ) = arrs y_true = hnp.arrays( shape=(s.shape[0], ), dtype=hnp.integer_dtypes(), elements=st.integers(min_value=0, max_value=s.shape[1] - 1), ) weights = st.none() | hnp.arrays( shape=(s.shape[1], ), dtype=float, elements=st.floats(1e-8, 100), ) return st.fixed_dictionaries(dict(y_true=y_true, weights=weights))
def test_eager_flat_strict_upper_int_verification(self, data): with eager_mode(): # sample square matrix n = data.draw(st.integers(min_value=3, max_value=10)) input_ = data.draw( np_st.arrays(dtype=np_st.integer_dtypes(), shape=(n, n))) output = np.array(utils.flat_strict_upper(input_)) # verify k = 0 for i in range(n): for j in range(i + 1, n): assert output[k] == input_[i, j] k += 1
def one_of_supported_dtypes(draw): # A strategy that selects a dtype that riptable is known to handle. # dtype size 16-bit is not supported # little endian is not supported return one_of( boolean_dtypes(), integer_dtypes(endianness="=", sizes=(8, 32, 64)), unsigned_integer_dtypes(endianness="=", sizes=(8, 32, 64)), floating_dtypes(endianness="=", sizes=(32, 64)), byte_string_dtypes(endianness="="), unicode_string_dtypes(endianness="="), # the following dtypes are not supported # complex_number_dtypes(), # datetime64_dtypes(), # timedelta64_dtypes(), )
def numpy_number(draw, min_val, max_val): dtype = draw(st.one_of(nps.integer_dtypes(), nps.unsigned_integer_dtypes(), nps.floating_dtypes())) if 'f' in dtype.str: if min_val < np.finfo(dtype).min: min_val = np.finfo(dtype).min if max_val > np.finfo(dtype).max: max_val = np.finfo(dtype).max number = draw(st.floats(min_val, max_val, allow_nan=False, allow_infinity=False)) else: if min_val < np.iinfo(dtype).min: min_val = np.iinfo(dtype).min if max_val > np.iinfo(dtype).max: max_val = np.iinfo(dtype).max number = draw(st.integers(min_val, max_val)) return np.array([number], dtype)[0]
def numpy_number(draw, min_val, max_val): dtype = draw( st.one_of(nps.integer_dtypes(), nps.unsigned_integer_dtypes(), nps.floating_dtypes())) if 'f' in dtype.str: if min_val < np.finfo(dtype).min: min_val = np.finfo(dtype).min if max_val > np.finfo(dtype).max: max_val = np.finfo(dtype).max number = draw( st.floats(min_val, max_val, allow_nan=False, allow_infinity=False)) else: min_val, max_val = np.ceil(min_val), np.floor(max_val) if min_val < np.iinfo(dtype).min: min_val = np.iinfo(dtype).min if max_val > np.iinfo(dtype).max: max_val = np.iinfo(dtype).max number = draw(st.integers(min_val, max_val)) return np.array([number], dtype)[0]
def dataframe(draw): n_cols = draw(integers(min_value=1, max_value=20)) dtypes = draw( lists( one_of( np_strategies.floating_dtypes(), np_strategies.integer_dtypes(), np_strategies.unicode_string_dtypes(), ), min_size=n_cols, max_size=n_cols, )) colnames = draw( lists(text() | integers(), min_size=n_cols, max_size=n_cols, unique=True)) return draw( data_frames(columns=[ column(name=name, dtype=dtype) for dtype, name in zip(dtypes, colnames) ]))
except ValueError: try: slow_result = slow_func(arr, axis=axis) assert False except ValueError: return slow_result = slow_func(arr, axis=axis) assert_array_almost_equal(bn_result, slow_result) @pytest.mark.parametrize("func", (bn.nanmin, bn.nanmax, bn.anynan, bn.allnan), ids=lambda x: x.__name__) @hypothesis.given(array=hy_arrays( dtype=one_of(integer_dtypes(sizes=(32, 64)), floating_dtypes(sizes=(32, 64))), shape=array_shapes(), )) def test_reduce_hypothesis(func, array): _hypothesis_helper(func, array) @pytest.mark.parametrize("func", (bn.nanargmin, bn.nanargmax), ids=lambda x: x.__name__) @hypothesis.given(array=hy_arrays( dtype=one_of(integer_dtypes(sizes=(32, 64)), floating_dtypes(sizes=(32, 64))), shape=array_shapes(), )) def test_reduce_hypothesis_errata(func, array):
from hypothesis import given import hypothesis.strategies as st import hypothesis.extra.numpy as hnp import numpy as np import pytest from mygrad import Tensor from mygrad.nnet.initializers import normal @given(dtype=hnp.unsigned_integer_dtypes() | hnp.integer_dtypes() | hnp.complex_number_dtypes()) def test_normal_dtype_validation(dtype): with pytest.raises(ValueError): normal(1, dtype=dtype) @given(std=st.floats(-1000, 0, exclude_max=True)) def test_normal_std_validation(std): with pytest.raises(ValueError): normal(1, std=std) _array_shapes = ((1000000, ), (1000, 100, 10), (10, 10, 10, 10, 10, 10)) @given( shape=st.sampled_from(_array_shapes), mean=st.floats(-100, 100), std=st.floats(0, 5), )
import hypothesis.extra.numpy as npst import hypothesis.strategies as st from hypothesis import given, settings import xarray as xr # Run for a while - arrays are a bigger search space than usual settings.register_profile("ci", deadline=None) settings.load_profile("ci") an_array = npst.arrays( dtype=st.one_of( npst.unsigned_integer_dtypes(), npst.integer_dtypes(), npst.floating_dtypes(), ), shape=npst.array_shapes(max_side=3), # max_side specified for performance ) @given(st.data(), an_array) def test_CFMask_coder_roundtrip(data, arr): names = data.draw(st.lists(st.text(), min_size=arr.ndim, max_size=arr.ndim, unique=True).map(tuple)) original = xr.Variable(names, arr) coder = xr.coding.variables.CFMaskCoder() roundtripped = coder.decode(coder.encode(original)) xr.testing.assert_identical(original, roundtripped)
def test_can_restrict_endianness(data, byteorder): dtype = data.draw(nps.integer_dtypes(byteorder, sizes=(16, 32, 64))) if byteorder == ('<' if sys.byteorder == 'little' else '>'): assert dtype.byteorder == '=' else: assert dtype.byteorder == byteorder
def test_can_turn_off_subarrays(dt): for field, _ in dt.fields.values(): assert field.shape == () @pytest.mark.parametrize('byteorder', ['<', '>']) @given(data=st.data()) def test_can_restrict_endianness(data, byteorder): dtype = data.draw(nps.integer_dtypes(byteorder, sizes=(16, 32, 64))) if byteorder == ('<' if sys.byteorder == 'little' else '>'): assert dtype.byteorder == '=' else: assert dtype.byteorder == byteorder @given(nps.integer_dtypes(sizes=8)) def test_can_specify_size_as_an_int(dt): assert dt.itemsize == 1 @given(st.data()) def test_can_draw_shapeless_from_scalars(data): dt = data.draw(nps.scalar_dtypes()) result = data.draw(nps.arrays(dtype=dt, shape=())) assert isinstance(result, dt.type) @given(st.data()) def test_unicode_string_dtypes_generate_unicode_strings(data): dt = data.draw(nps.unicode_string_dtypes()) result = data.draw(nps.from_dtype(dt))
from functools import partial import numpy as np import pandas as pd import pytest import xarray as xr pytest.importorskip("hypothesis") import hypothesis.extra.numpy as npst # isort:skip import hypothesis.extra.pandas as pdst # isort:skip import hypothesis.strategies as st # isort:skip from hypothesis import given # isort:skip numeric_dtypes = st.one_of(npst.unsigned_integer_dtypes(), npst.integer_dtypes(), npst.floating_dtypes()) numeric_series = numeric_dtypes.flatmap(lambda dt: pdst.series(dtype=dt)) an_array = npst.arrays( dtype=numeric_dtypes, shape=npst.array_shapes(max_dims=2), # can only convert 1D/2D to pandas ) @st.composite def datasets_1d_vars(draw) -> xr.Dataset: """Generate datasets with only 1D variables Suitable for converting to pandas dataframes. """
def test_can_turn_off_subarrays(dt): for field, _ in dt.fields.values(): assert field.shape == () @pytest.mark.parametrize("byteorder", ["<", ">"]) @given(data=st.data()) def test_can_restrict_endianness(data, byteorder): dtype = data.draw(nps.integer_dtypes(byteorder, sizes=(16, 32, 64))) if byteorder == ("<" if sys.byteorder == "little" else ">"): assert dtype.byteorder == "=" else: assert dtype.byteorder == byteorder @given(nps.integer_dtypes(sizes=8)) def test_can_specify_size_as_an_int(dt): assert dt.itemsize == 1 @given(st.data()) def test_can_draw_arrays_from_scalars(data): dt = data.draw(nps.scalar_dtypes()) result = data.draw(nps.arrays(dtype=dt, shape=())) assert isinstance(result, np.ndarray) assert result.dtype == dt @given(st.data()) def test_can_cast_for_scalars(data): # Note: this only passes with castable datatypes, certain dtype
def test_array_dtypes_may_have_field_titles(): find_any(nps.array_dtypes(), lambda dt: len(dt.fields) > len(dt.names)) @pytest.mark.parametrize("byteorder", ["<", ">"]) @given(data=st.data()) def test_can_restrict_endianness(data, byteorder): dtype = data.draw(nps.integer_dtypes(endianness=byteorder, sizes=(16, 32, 64))) if byteorder == ("<" if sys.byteorder == "little" else ">"): assert dtype.byteorder == "=" else: assert dtype.byteorder == byteorder @given(nps.integer_dtypes(sizes=8)) def test_can_specify_size_as_an_int(dt): assert dt.itemsize == 1 @given(st.data()) def test_can_draw_arrays_from_scalars(data): dt = data.draw(nps.scalar_dtypes()) result = data.draw(nps.arrays(dtype=dt, shape=())) assert isinstance(result, np.ndarray) assert result.dtype == dt @given(st.data()) def test_can_cast_for_arrays(data): # Note: this only passes with castable datatypes, certain dtype
from hypothesis import given from hypothesis.extra.numpy import from_dtype, integer_dtypes from hypothesis.strategies import data, floats, integers @given(floats(width=32)) def test_float32_exactly_representable(x): clipped = np.dtype("float32").type(x) if np.isnan(x): assert np.isnan(clipped) else: assert x == float(clipped) @given(floats(width=16)) def test_float16_exactly_representable(x): clipped = np.dtype("float16").type(x) if np.isnan(x): assert np.isnan(clipped) else: assert x == float(clipped) @given(data=data(), dtype=integer_dtypes()) def test_floor_ceil_lossless(data, dtype): # Regression test for issue #1667; ceil converting numpy integers # to float and back to int with loss of exact value. x = data.draw(from_dtype(dtype)) assert data.draw(integers(x, x)) == x
def test_minimise_array_strategy(): smallest = minimal(nps.arrays( nps.nested_dtypes(max_itemsize=settings.default.buffer_size // 3**3), nps.array_shapes(max_dims=3, max_side=3))) assert smallest.dtype == np.dtype(u'bool') and not smallest.any() @given(nps.array_dtypes(allow_subarrays=False)) def test_can_turn_off_subarrays(dt): for field, _ in dt.fields.values(): assert field.shape == () @given(nps.integer_dtypes(endianness='>')) def test_can_restrict_endianness(dt): if dt.itemsize == 1: assert dt.byteorder == '|' else: assert dt.byteorder == '>' @given(nps.integer_dtypes(sizes=8)) def test_can_specify_size_as_an_int(dt): assert dt.itemsize == 1 @given(st.data()) def test_can_draw_shapeless_from_scalars(data): dt = data.draw(nps.scalar_dtypes())
pytest.importorskip("hypothesis") import hypothesis.extra.numpy as npst import hypothesis.strategies as st from hypothesis import given, settings import xarray as xr # Run for a while - arrays are a bigger search space than usual settings.register_profile("ci", deadline=None) settings.load_profile("ci") an_array = npst.arrays( dtype=st.one_of( npst.unsigned_integer_dtypes(), npst.integer_dtypes(), npst.floating_dtypes() ), shape=npst.array_shapes(max_side=3), # max_side specified for performance ) @pytest.mark.slow @given(st.data(), an_array) def test_CFMask_coder_roundtrip(data, arr): names = data.draw( st.lists(st.text(), min_size=arr.ndim, max_size=arr.ndim, unique=True).map( tuple ) ) original = xr.Variable(names, arr) coder = xr.coding.variables.CFMaskCoder()
from typing import Optional import hypothesis.extra.numpy as hnp import hypothesis.strategies as st import numpy as np import pytest from hypothesis import given, settings from numpy.testing import assert_array_equal from mygrad import Tensor real_types = (hnp.integer_dtypes() | hnp.unsigned_integer_dtypes() | hnp.floating_dtypes()) @given( tensor=st.tuples( hnp.arrays(shape=hnp.array_shapes(), dtype=real_types), st.booleans(), ).map(lambda x: Tensor(x[0], constant=x[1])), dest_type=real_types, constant=st.booleans() | st.none(), ) def test_astype(tensor: Tensor, dest_type: type, constant: Optional[bool]): tensor = tensor * 1 # give tensor a creator new_tensor = tensor.astype(dest_type, constant=constant) assert new_tensor.constant is (tensor.constant if constant is None else constant) assert tensor.creator is not None assert new_tensor.creator is None
floats, integers, lists, one_of, text, tuples, ) from superintendent.distributed.dbqueue import DatabaseQueue guaranteed_dtypes = one_of( np_strategies.scalar_dtypes(), np_strategies.unsigned_integer_dtypes(), np_strategies.datetime64_dtypes(), np_strategies.floating_dtypes(), np_strategies.integer_dtypes(), ) @composite def dataframe(draw): n_cols = draw(integers(min_value=1, max_value=20)) dtypes = draw( lists( one_of( np_strategies.floating_dtypes(), np_strategies.integer_dtypes(), np_strategies.unicode_string_dtypes(), ), min_size=n_cols, max_size=n_cols,
dictionaries, floats, integers, lists, one_of, recursive, text, ) import superintendent.prioritisation from superintendent import SemiSupervisor primitive_strategy = text() | integers() | floats(allow_nan=False) | booleans() guaranteed_dtypes = (boolean_dtypes() | integer_dtypes() | floating_dtypes() | unicode_string_dtypes()) container_strategy = dictionaries( text(), primitive_strategy) | lists(primitive_strategy) nested_strategy = recursive( container_strategy, lambda children: lists(children) | dictionaries(text(), children), ) container_strategy = dictionaries( text(), primitive_strategy) | lists(primitive_strategy) nested_strategy = recursive(
with Tiff(filename, "w") as handle: handle.write(img, method="tile") with Tiff(filename) as handle: data = handle[:] assert np.all(img == data[:, :, :3]) with Tiff(filename, "w") as handle: handle.write(img, method="scanline") with Tiff(filename) as handle: data = handle[:] assert np.all(img == data[:, :, :3]) # scanline integer tests @settings(buffer_size=11000000) @given(data=hnp.arrays(dtype=st.one_of(hnp.integer_dtypes(endianness="="), hnp.unsigned_integer_dtypes(endianness="=")), shape=hnp.array_shapes(min_dims=2, max_dims=2, min_side=10, max_side=50))) def test_write_int_scanline(data, tmpdir_factory): filename = str(tmpdir_factory.mktemp("write").join("int_img.tif")) with Tiff(filename, "w") as handle: handle.write(data, method="scanline") with tifffile.TiffFile(filename) as handle: img = handle.asarray() np.testing.assert_array_equal(data, img) @settings(buffer_size=11000000) @given(data=hnp.arrays(dtype=st.one_of(hnp.integer_dtypes(endianness="="), hnp.unsigned_integer_dtypes(endianness="=")), shape=hnp.array_shapes(min_dims=2, max_dims=2, min_side=10, max_side=50))) def test_write_int_scanline_set_rows_per_strip(data, tmpdir_factory): filename = str(tmpdir_factory.mktemp("write").join("int_img.tif"))