Python hypothesis.strategies.data() Examples
The following are 30
code examples of hypothesis.strategies.data().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
hypothesis.strategies
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: test_persistence.py From flocker with Apache License 2.0 | 6 votes |
def test_can_create_latest_golden(self): """ The latest golden files should be identical to ones generated from HEAD. """ configs_dir = FilePath(__file__).sibling('configurations') for i, deployment in enumerate(TEST_DEPLOYMENTS, start=1): encoding = wire_encode( Configuration(version=_CONFIG_VERSION, deployment=deployment) ) path = configs_dir.child( b"configuration_%d_v%d.json" % (i, _CONFIG_VERSION) ) self.assertEqual( json.loads(encoding), json.loads(path.getContent().rstrip()), "Golden test file %s can not be generated from HEAD. Please " "review the python files in that directory to re-generate " "that file if you have intentionally changed the backing test " "data. You might need to update the model version and write " "an upgrade test if you are intentionally changing the " "model." % (path.path,) )
Example #2
Source File: test_hub.py From bricknil with Apache License 2.0 | 6 votes |
def test_run_hub(self, data): Hub.hubs = [] sensor_name = 'sensor' sensor = data.draw(st.sampled_from(self.sensor_list)) capabilities = self._draw_capabilities(data, sensor) hub_type = data.draw(st.sampled_from(self.hub_list)) TestHub, stop_evt = self._get_hub_class(hub_type, sensor, sensor_name, capabilities) hub = TestHub('test_hub') # Start the hub #kernel.run(self._emit_control(TestHub)) with patch('Adafruit_BluefruitLE.get_provider') as ble,\ patch('bricknil.ble_queue.USE_BLEAK', False) as use_bleak: ble.return_value = MockBLE(hub) sensor_obj = getattr(hub, sensor_name) sensor_obj.send_message = Mock(side_effect=coroutine(lambda x,y: "the awaitable should return this")) kernel.run(self._emit_control, data, hub, stop_evt, ble(), sensor_obj) #start(system)
Example #3
Source File: test_persistence.py From flocker with Apache License 2.0 | 6 votes |
def test_no_arbitrary_decoding(self): """ ``wire_decode`` will not decode classes that are not in ``SERIALIZABLE_CLASSES``. """ class Temp(PClass): """A class.""" SERIALIZABLE_CLASSES.append(Temp) def cleanup(): if Temp in SERIALIZABLE_CLASSES: SERIALIZABLE_CLASSES.remove(Temp) self.addCleanup(cleanup) data = wire_encode(Temp()) SERIALIZABLE_CLASSES.remove(Temp) # Possibly future versions might throw exception, the key point is # that the returned object is not a Temp instance. self.assertFalse(isinstance(wire_decode(data), Temp))
Example #4
Source File: test_conv.py From MyGrad with MIT License | 6 votes |
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
Example #5
Source File: test_toolkit.py From segpy with GNU Affero General Public License v3.0 | 6 votes |
def test_read_truncated_header_raises_error(self, data, count, encoding, random): written_headers = data.draw(extended_textual_header(count=count)) with BytesIO() as fh: for header in written_headers: for line in header: fh.write(line.encode(encoding)) fh.seek(0, os.SEEK_END) length = fh.tell() truncate_pos = random.randrange(0, length - 1) truncated_buffer = fh.getbuffer()[:truncate_pos] with BytesIO(truncated_buffer): with raises(EOFError): toolkit.read_extended_headers_counted(fh, count, encoding=encoding) del truncated_buffer
Example #6
Source File: test_negative_log_likelihood.py From MyGrad with MIT License | 6 votes |
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)
Example #7
Source File: test_strategies.py From linehaul with Apache License 2.0 | 6 votes |
def test_mixture(self, data, versions, digits): min_version, max_version = versions min_digits, max_digits = digits # Check that the our minimum version doesn't have too many digits. # TODO: Can we remove these assumptions? assume(len(min_version.split(".")) <= max_digits) assume(len(max_version.split(".")) <= max_digits) version = data.draw( st_version( min_digits=min_digits, max_digits=max_digits, min_version=min_version, max_version=max_version, ) ) assert ( self._ver_2_list(min_version) <= self._ver_2_list(version) <= self._ver_2_list(max_version) ) assert min_digits <= len(version.split(".")) <= max_digits
Example #8
Source File: test_margin_ranking.py From MyGrad with MIT License | 6 votes |
def simple_loss(x1, x2, y, margin): """ x1 : mygrad.Tensor, shape=(N, D) x2 : mygrad.Tensor, shape=(N, D) y : Union[int, numpy.ndarray, mygrad.Tensor], scalar or shape=(N,) margin : float Returns ------- mygrad.Tensor, shape=() """ if isinstance(y, mg.Tensor): y = y.data y = np.asarray(y) if y.ndim: assert y.size == 1 or len(y) == len(x1) if x1.ndim == 2: y = y.reshape(-1, 1) return mg.mean(mg.maximum(0, margin - y * (x1 - x2)))
Example #9
Source File: test_nondifferentiable.py From MyGrad with MIT License | 5 votes |
def test_argmin(a, data): axis = data.draw(valid_axes(ndim=a.ndim, single_axis_only=True), label="axis") tensor = Tensor(a) # tensor input assert_allclose(mg.argmin(tensor, axis=axis), np.argmin(a, axis=axis)) # tensor method assert_allclose(tensor.argmin(axis=axis), a.argmin(axis=axis)) # array input assert_allclose(mg.argmin(a, axis=axis), np.argmin(a, axis=axis))
Example #10
Source File: test_tensor_manip.py From MyGrad with MIT License | 5 votes |
def test_transpose_method(): dat = np.arange(24).reshape(2, 3, 4) for axes in permutations(range(3)): # passing tuple of integers x = Tensor(dat) f = x.transpose(axes) f.backward(dat.transpose(axes)) assert_allclose(f.data, dat.transpose(axes)) assert_allclose(x.grad, dat) # passing integers directly x = Tensor(dat) f = x.transpose(*axes) f.backward(dat.transpose(axes)) assert_allclose(f.data, dat.transpose(axes), err_msg="{}".format(axes)) assert_allclose(x.grad, dat, err_msg="{}".format(axes)) # passing integers directly x = Tensor(dat) f = x.transpose() f.backward(dat.transpose()) assert_allclose(f.data, dat.transpose()) assert_allclose(x.grad, dat) # check that constant=True works x = Tensor(dat) f = x.transpose(constant=True) assert f.constant and not x.constant f = x.transpose(1, 0, 2, constant=True) assert f.constant and not x.constant
Example #11
Source File: test_nondifferentiable.py From MyGrad with MIT License | 5 votes |
def test_argmax(a, data): axis = data.draw(valid_axes(ndim=a.ndim, single_axis_only=True), label="axis") tensor = Tensor(a) # tensor input assert_allclose(mg.argmax(tensor, axis=axis), np.argmax(a, axis=axis)) # tensor method assert_allclose(tensor.argmax(axis=axis), a.argmax(axis=axis)) # array input assert_allclose(mg.argmax(a, axis=axis), np.argmax(a, axis=axis))
Example #12
Source File: test_scipy_mirror.py From MyGrad with MIT License | 5 votes |
def test_logsumexp(data: st.SearchStrategy, x: np.ndarray, keepdims: bool): axes = data.draw(valid_axes(ndim=x.ndim), label="axes") mygrad_result = logsumexp(x, axis=axes, keepdims=keepdims) scipy_result = special.logsumexp(x, axis=axes, keepdims=keepdims) assert_array_equal( mygrad_result, scipy_result, err_msg="mygrad's implementation of logsumexp does " "not match that of scipy's", )
Example #13
Source File: test_sliding_window.py From MyGrad with MIT License | 5 votes |
def test_input_validation(args: dict, data: st.DataObject): kwargs = dict( arr=np.arange(36).reshape(6, 6), window_shape=(1, 1), step=1, dilation=None ) kwargs.update( (k, (data.draw(v, label=k)) if isinstance(v, st.SearchStrategy) else v) for k, v in args.items() ) with pytest.raises((ValueError, TypeError)): sliding_window_view(**kwargs)
Example #14
Source File: test_tensor_manip.py From MyGrad with MIT License | 5 votes |
def test_transpose(x, data): axes = data.draw( valid_axes(x.ndim, min_dim=x.ndim, max_dim=x.ndim).map( lambda out: (out,) if isinstance(out, int) else out ), label="axes", ) x_arr = Tensor(np.copy(x)) o = transpose(x_arr, axes, constant=False) grad = data.draw( hnp.arrays(shape=o.shape, dtype=float, elements=st.floats(1, 10), unique=True), label="grad", ) o.backward(grad) def f(x): return np.transpose(x, axes) assert_allclose(o.data, f(x)) (dx,) = numerical_gradient_full(f, x, back_grad=grad) assert_allclose(x_arr.grad, dx) out = transpose(x, constant=True) assert out.constant and not x_arr.constant
Example #15
Source File: test_softmaxcrossentropy.py From MyGrad with MIT License | 5 votes |
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)
Example #16
Source File: test_negative_log_likelihood.py From MyGrad with MIT License | 5 votes |
def test_input_validation(data, labels, weights): with raises((ValueError, TypeError)): negative_log_likelihood(data, labels, weights=weights)
Example #17
Source File: test_utils.py From MyGrad with MIT License | 5 votes |
def test_finite_difference_no_broadcast(data, x): atol, rtol = (1e-2, 1e-2) y = data.draw( hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(-100, 100)), label="y" ) z = data.draw( hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(-100, 100)), label="z" ) grad = data.draw( hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(-100, 100)), label="grad", ) # check variable-selection assert finite_difference(unary_func, x, back_grad=grad, vary_ind=[])[0] is None # no broadcast (dx,) = finite_difference(unary_func, x, back_grad=grad) assert_allclose(dx, grad * 2 * x, atol=atol, rtol=rtol) dx, dy = numerical_gradient(binary_func, x, y, back_grad=grad) assert_allclose(dx, grad * y ** 2, atol=atol, rtol=rtol) assert_allclose(dy, grad * 2 * x * y, atol=atol, rtol=rtol) dx, dy, dz = numerical_gradient(ternary_func, x, y, z, back_grad=grad) assert_allclose(dx, grad * z * y ** 2, atol=atol, rtol=rtol) assert_allclose(dy, grad * z * 2 * x * y, atol=atol, rtol=rtol) assert_allclose(dz, grad * x * y ** 2, atol=atol, rtol=rtol)
Example #18
Source File: test_utils.py From MyGrad with MIT License | 5 votes |
def test_numerical_gradient_no_broadcast(data, x): atol, rtol = (1e-7, 1e-7) y = data.draw( hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(-100, 100)), label="y" ) z = data.draw( hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(-100, 100)), label="z" ) grad = data.draw( hnp.arrays(shape=x.shape, dtype=float, elements=st.floats(-100, 100)), label="grad", ) # check variable-selection assert numerical_gradient(unary_func, x, back_grad=grad, vary_ind=[])[0] is None # no broadcast (dx,) = numerical_gradient(unary_func, x, back_grad=grad) assert_allclose(dx, grad * 2 * x, atol=atol, rtol=rtol) dx, dy = numerical_gradient(binary_func, x, y, back_grad=grad) assert_allclose(dx, grad * y ** 2, atol=atol, rtol=rtol) assert_allclose(dy, grad * 2 * x * y, atol=atol, rtol=rtol) dx, dy, dz = numerical_gradient(ternary_func, x, y, z, back_grad=grad) assert_allclose(dx, grad * z * y ** 2, atol=atol, rtol=rtol) assert_allclose(dy, grad * z * 2 * x * y, atol=atol, rtol=rtol) assert_allclose(dz, grad * x * y ** 2, atol=atol, rtol=rtol)
Example #19
Source File: test_conv.py From MyGrad with MIT License | 5 votes |
def get_outshape(x_shape, w_shape, stride, dilation): """ Compute the shape of the output tensor given an input shape, convolutional filter shape, and stride. Parameters ---------- x_shape : Tuple[int, ...] The shape of the input tensor. w_shape : Tuple[int, ...] The shape of the convolutional filter. stride : Tuple[int, ...] The stride at which to apply the convolutional filter to the input. dilation : Tuple[int, ...] The dilation used to form each window over the data. Returns ------- numpy.ndarray[int], shape=(num_conv,) The shape of the output tensor resulting from convolving a tensor of shape `x_shape` with a tensor of shape `w_shape`. Returns `None` if an invalid combination of shapes are provided. """ x_shape = np.array(x_shape) w_shape = np.array(w_shape) stride = np.array(stride) dilation = np.array(dilation) out_shape = (x_shape - ((w_shape - 1) * dilation + 1)) / stride + 1 if not all(i.is_integer() and i > 0 for i in out_shape): msg = "Stride and kernel dimensions are incompatible: \n" msg += "Input dimensions: {}\n".format(tuple(x_shape)) msg += "Stride dimensions: {}\n".format(tuple(stride)) msg += "Kernel dimensions: {}\n".format(tuple(w_shape)) msg += "Dilation dimensions: {}\n".format(tuple(dilation)) return None return out_shape.astype(np.int32)
Example #20
Source File: test_clip_sampler.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_clip_sampler_is_deterministic_in_test_mode(self, data): clip_length = data.draw(st.integers(1, 1000)) video_length = data.draw(st.integers(1, 1000)) sampler = ClipSampler(clip_length=clip_length, test=True) sample_count = 10 frame_idx = [sampler.sample(video_length) for _ in range(sample_count)] for i in range(1, sample_count): assert frame_idx[i - 1] == frame_idx[i]
Example #21
Source File: test_clip_sampler.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_clip_is_oversampled_when_video_is_shorter_than_clip_length(self, data): clip_length = data.draw(st.integers(2, 1000)) video_length = data.draw(st.integers(1, clip_length - 1)) sampler = ClipSampler(clip_length=clip_length) frame_idx = sampler.sample(video_length) frame_idx = frame_idx_to_list(frame_idx) assert_elems_lt(frame_idx, clip_length - 1) assert_elems_gte(frame_idx, 0)
Example #22
Source File: test_clip_sampler.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_clip_is_subsampled_from_video_when_video_is_longer_than_clip(self, data): clip_length = data.draw(st.integers(1, 1000)) video_length = data.draw(st.integers(clip_length, 1000)) sampler = ClipSampler(clip_length=clip_length) frame_idx = sampler.sample(video_length) assert frame_idx.step == 1 assert (frame_idx.stop - frame_idx.start) == clip_length
Example #23
Source File: test_temporal_segment_sampler.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def draw_sampler_parameters(data): segment_count = data.draw(st.integers(1, 100), label="segment_count") snippet_length = data.draw(st.integers(1, 100), label="snippet_length") video_length = data.draw(st.integers(1, 10000), label="video_length") return segment_count, snippet_length, video_length
Example #24
Source File: test_temporal_segment_sampler.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_fuzz_sampler_test(self, data): segment_count, snippet_length, video_length = self.draw_sampler_parameters(data) frame_idx = self.sample(video_length, segment_count, snippet_length, test=True) assert_valid_frame_index( frame_idx, segment_count * snippet_length, video_length )
Example #25
Source File: tset_random_resized_crop_video.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_resulting_video_are_specified_size(self, video, interpolation, data): width, height = video[0].size expected_width = data.draw(st.integers(min_value=1, max_value=width)) expected_height = data.draw(st.integers(min_value=1, max_value=height)) transform = RandomResizedCropVideo( (expected_height, expected_width), interpolation=interpolation ) transformed_video = list(transform(video)) assert len(transformed_video) == len(video) for frame in transformed_video: assert (expected_width, expected_height) == frame.size
Example #26
Source File: test_resize_video.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_resizes_to_given_size(self, video, interpolation, data): width, height = video[0].size expected_width = data.draw(st.integers(min_value=1, max_value=width)) expected_height = data.draw(st.integers(min_value=1, max_value=height)) transform = ResizeVideo( size=(expected_height, expected_width), interpolation=interpolation ) transformed_video = list(transform(video)) assert len(transformed_video) == len(video) for frame in transformed_video: assert (expected_width, expected_height) == frame.size
Example #27
Source File: test_multiscale_crop_video.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_transform_always_yields_crops_of_the_correct_size(self, data): crop_height = data.draw(st.integers(1, 10)) crop_width = data.draw(st.integers(1, 10)) duration = data.draw(st.integers(1, 10)) fixed_crops = data.draw(st.booleans()) if fixed_crops: more_fixed_crops = data.draw(st.booleans()) else: more_fixed_crops = False height = data.draw(st.integers(crop_height, crop_height * 100)) width = data.draw(st.integers(crop_width, crop_width * 100)) video_shape = (duration, height, width, 3) scale_strategy = st.floats( min_value=min(crop_width, crop_height) / min(height, width), max_value=1 ) scales = data.draw(st.lists(scale_strategy, min_size=1, max_size=5)) max_distortion = data.draw(st.integers(0, len(scales))) video = NDArrayToPILVideo()(np.ones(video_shape, dtype=np.uint8)) transform = MultiScaleCropVideo( size=ImageShape(height=crop_height, width=crop_width), scales=scales, max_distortion=max_distortion, fixed_crops=fixed_crops, more_fixed_crops=more_fixed_crops, ) transformed_video = list(transform(video)) print("video_shape", video_shape) print("scales", scales) print("max_distortion", max_distortion) print("fixed_crops", fixed_crops) print("more_fixed_crops", more_fixed_crops) assert len(transformed_video) == duration for frame in transformed_video: print("crop_size", np.array(frame).shape) np.testing.assert_allclose(np.array(frame), np.ones_like(frame)) assert frame.height == crop_height assert frame.width == crop_width
Example #28
Source File: test_normalize_video.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def test_preserves_channel_count(self, data): video = data.draw(tensor_video()) input_channel_count = video.size(0) mean = np.random.randn(input_channel_count) std = np.random.randn(input_channel_count) note(mean) note(std) transform = NormalizeVideo(mean, std) transformed_video = transform(video) output_channel_count = transformed_video.size(0) assert input_channel_count == output_channel_count
Example #29
Source File: test_transpiler_equivalence.py From qiskit-terra with Apache License 2.0 | 5 votes |
def add_c_if_last_gate(self, carg, data): """Modify the last gate to be conditional on a classical register.""" creg = carg.register val = data.draw(st.integers(min_value=0, max_value=2**len(creg)-1)) last_gate = self.qc.data[-1] # Conditional instructions are not supported assume(isinstance(last_gate[0], Gate)) last_gate[0].c_if(creg, val) # Properties to check
Example #30
Source File: test_persistence.py From flocker with Apache License 2.0 | 5 votes |
def test_no_hash_collisions(self, data): """ Hashes of different deployments do not have hash collisions, hashes of the same object have the same hash. """ # With 128 bits of hash, a collision here indicates a fault in the # algorithm. # Generate the first deployment. deployment_a = data.draw(deployment_strategy()) # Decide if we want to generate a second deployment, or just compare # the first deployment to a re-serialized version of itself: simple_comparison = data.draw(st.booleans()) if simple_comparison: deployment_b = wire_decode(wire_encode(deployment_a)) else: deployment_b = data.draw(deployment_strategy()) should_be_equal = (deployment_a == deployment_b) if simple_comparison: self.assertThat( should_be_equal, Is(True) ) hash_a = generation_hash(deployment_a) hash_b = generation_hash(deployment_b) if should_be_equal: self.assertThat( hash_a, Equals(hash_b) ) else: self.assertThat( hash_a, Not(Equals(hash_b)) )