Best Python code snippet using hypothesis
disassemble.py
Source:disassemble.py
...256def abolsute_branch_uncondtionional(N, **kwargs):257 return [N]258def absolute_call(next_pc, N, **kwargs):259 return [next_pc, N]260def kill_branch(**kwargs):261 return []262class B(I):263 def __init__(self, pattern, name, newpc):264 super().__init__(pattern, "")265 self.newpc = newpc266 self.name = name267 def to_string(self, dict):268 if self.newpc == kill_branch:269 return self.name.format(**dict)270 if self.newpc == relative_branch or self.newpc == relative_branch_unconditional:271 dest = dict["next_pc"] + dict["S"]272 elif self.newpc == relative_call:273 dest = dict["next_pc"] + dict["S"]274 else:...
utils_lib.py
Source:utils_lib.py
1# Copyright 2021 Google LLC2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Utility library for optical flow network."""15from typing import Union, Tuple, Sequence16import tensorflow as tf17def coords_grid(18 batch_size: Union[tf.Tensor, int],19 height: Union[tf.Tensor, int],20 width: Union[tf.Tensor, int],21) -> tf.Tensor:22 """Creates a coordinate grid.23 Args:24 batch_size: A 0-D Tensor (scalar), batch size of coordinate grids.25 height: A 0-D Tensor (scalar), height of coordinate grids.26 width: A 0-D Tensor (scalar), width of coordinate grids.27 Returns:28 Coordinate grid: A Tensor of size B x H x W x 2 where B is batch size, H x W29 is the size of coordinate grid.30 """31 coords = tf.meshgrid(tf.range(height), tf.range(width), indexing='ij')32 coords = tf.cast(tf.stack(coords[::-1], axis=-1), dtype=tf.float32)33 coords = tf.expand_dims(coords, axis=0)34 coords = tf.repeat(coords, batch_size, axis=0)35 return coords36def initialize_flow(image: tf.Tensor,37 division: int = 8) -> Tuple[tf.Tensor, tf.Tensor]:38 """Creates initial coodinates of flow field before and after update.39 Args:40 image: A Tensor of size B x H x W x C, where B is batch size, H x W is the41 image size, and C is the number of channels.42 division: A integer specifying the division factor between input image and43 flow field.44 Returns:45 Coordinate of flow field before update: A Tensor of size B x H/division x46 W/division x 2, where B is batch size, H/division x W/division is the47 flow field size.48 Coordinate of flow field after update: A Tensor of size B x H/division x49 W/division x 2, where B is batch size, H/division x W/division is the50 flow field size.51 """52 batch, height, width, _ = tf.unstack(tf.shape(image))53 pre_coords = coords_grid(batch, height // division, width // division)54 post_coords = coords_grid(batch, height // division, width // division)55 return pre_coords, post_coords56def compute_upsample_flow(flow: tf.Tensor,57 size: Union[tf.Tensor, Tuple[int, int]]) -> tf.Tensor:58 """Resizes 'flow' to 'size' while rescaling flow value."""59 # For the boundary pixels, bilinear sampling will introduce incorrect value.60 # For example, the arms occluding the torso may have different flow value.61 flow_size = tf.unstack(flow.shape)62 if len(flow_size) == 3:63 flow_height = flow_size[0]64 flow_width = flow_size[1]65 else:66 flow_height = flow_size[1]67 flow_width = flow_size[2]68 upsampled_flow = tf.image.resize(flow, size)69 upsampled_x = upsampled_flow[..., 0] * tf.cast(70 size[1], dtype=tf.float32) / tf.cast(71 flow_width, dtype=tf.float32)72 upsampled_y = upsampled_flow[..., 1] * tf.cast(73 size[0], dtype=tf.float32) / tf.cast(74 flow_height, dtype=tf.float32)75 return tf.stack((upsampled_x, upsampled_y), axis=-1)76def create_path_drop_masks(p_flow: float,77 p_surface: float) -> Tuple[tf.Tensor, tf.Tensor]:78 """Determines global path drop decision based on given probabilities.79 Args:80 p_flow: A scalar of float32, probability of keeping flow feature branch81 p_surface: A scalar of float32, probability of keeping surface feature82 branch83 Returns:84 final_flow_mask: A constant tensor mask containing either one or zero85 depending on the final coin flip probability.86 final_surface_mask: A constant tensor mask containing either one or87 zero depending on the final coin flip probability.88 """89 # The logic works as follows:90 # We have flipped 3 coins, first determines the chance of keeping91 # the flow branch, second determines keeping surface branch, the third92 # makes the final decision in the case where both branches were killed93 # off, otherwise the initial flow and surface chances are kept.94 random_values = tf.random.uniform(shape=[3], minval=0.0, maxval=1.0)95 keep_branch = tf.constant(1.0)96 kill_branch = tf.constant(0.0)97 flow_chances = tf.case(98 [(tf.math.less_equal(random_values[0], p_flow), lambda: keep_branch)],99 default=lambda: kill_branch)100 surface_chances = tf.case(101 [(tf.math.less_equal(random_values[1], p_surface), lambda: keep_branch)],102 default=lambda: kill_branch)103 # Decision to determine whether both branches were killed off104 third_flip = tf.math.logical_or(105 tf.cast(flow_chances, dtype=tf.bool),106 tf.cast(surface_chances, dtype=tf.bool))107 third_flip = tf.cast(third_flip, dtype=tf.float32)108 # Make a second choice, for the third case109 # Here we use a 50/50 chance to keep either flow or surface110 # If its greater than 0.5, keep the image111 flow_second_flip = tf.case(112 [(tf.math.greater(random_values[2], 0.5), lambda: keep_branch)],113 default=lambda: kill_branch)114 # If its less than or equal to 0.5, keep surface115 surface_second_flip = tf.case(116 [(tf.math.less_equal(random_values[2], 0.5), lambda: keep_branch)],117 default=lambda: kill_branch)118 final_flow_mask = tf.case([(tf.equal(third_flip, 1), lambda: flow_chances)],119 default=lambda: flow_second_flip)120 final_surface_mask = tf.case(121 [(tf.equal(third_flip, 1), lambda: surface_chances)],122 default=lambda: surface_second_flip)123 return final_flow_mask, final_surface_mask124def build_pyramid(image: tf.Tensor,125 num_levels: int = 7,126 resize_method: str = 'bilinear',127 rescale_flow: bool = False) -> Sequence[tf.Tensor]:128 """Return list of downscaled images (level 0 = original)."""129 pyramid = [image]130 size = tf.shape(image)[-3:-1]131 for _ in range(1, num_levels):132 size = size // 2133 image = tf.image.resize(pyramid[-1], size, method=resize_method)134 if rescale_flow:135 image /= 2 # half-resolution => half-size flow136 pyramid.append(image)...
models.py
Source:models.py
...64 return json_object65 66 def __unicode__(self):67 return str(self.short_desc)68 def kill_branch(self):69 offspring = Page.objects.all().filter(parent=self)70 for page in offspring:71 page.kill_branch()72 self.delete()73class Properties(models.Model):74 user = models.OneToOneField(User)75 avatar = models.ImageField(upload_to='images/user_avatars/%Y/%m/%d') 76 77 def __unicode__(self):78 return str(self.user.username)79 def getPages(self):80 return Page.objects.all().filter(author=self.user)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!