Best Python code snippet using avocado_python
test_neighborhoods.py
Source:test_neighborhoods.py
...30 # son 5 entradas en el array porque se cuenta a la celula como un31 # vecino32 mask = np.array([[1, 0, 0, 0, 0]], dtype=bool)3334 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))3536 def test_get_mask_case_2(self):37 """38 Este metodo testea el metodo get_mask de la clase LeftCellNeighborhood39 """40 neighborhood = LeftCellNeighborhood(3, inclusive=True)41 mask = np.array([[1, 0, 0, 1]], dtype=bool)4243 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))4445 def test_get_offset_case_1(self):46 """47 Este metodo testea el metodo get_mask de la clase LeftCellNeighborhood48 """49 neighborhood = LeftCellNeighborhood(4)50 offset = (0, -4)5152 self.assertEqual(neighborhood.get_offset(), offset)5354 def test_get_offset_case_2(self):55 """56 Este metodo testea el metodo get_mask de la clase LeftCellNeighborhood57 """58 neighborhood = LeftCellNeighborhood(3)59 offset = (0, -3)6061 self.assertEqual(neighborhood.get_offset(), offset)626364class TestRightCellNeighborhood(unittest.TestCase):65 """66 Tests para RightCellNeighborhood67 """6869 def test_get_mask_case_1(self):70 """71 Este metodo testea el metodo get_mask de la clase RightCellNeighborhood72 """73 neighborhood = RightCellNeighborhood(4, inclusive=False)74 # son 5 entradas en el array porque se cuenta a la celula como un75 # vecino76 mask = np.array([[0, 0, 0, 0, 1]], dtype=bool)7778 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))7980 def test_get_mask_case_2(self):81 """82 Este metodo testea el metodo get_mask de la clase RightCellNeighborhood83 """84 neighborhood = RightCellNeighborhood(3, inclusive=True)85 mask = np.array([[1, 0, 0, 1]], dtype=bool)8687 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))8889 def test_get_offset_case_1(self):90 """91 Este metodo testea el metodo get_mask de la clase RightCellNeighborhood92 """93 neighborhood = RightCellNeighborhood(4)94 offset = (0, 0)9596 self.assertEqual(neighborhood.get_offset(), offset)979899class TestIntervalCellNeighborhood(unittest.TestCase):100 """101 Tests para IntervalCellNeighborhood102 """103104 def test_get_mask_case_1(self):105 """106 Este metodo testea el metodo get_mask de la clase107 IntervalCellNeighborhood108 """109 neighborhood = IntervalCellNeighborhood(4, 3, inclusive=False)110 # son 5 entradas en el array porque se cuenta a la celula como un111 # vecino112 mask = np.array([[1, 0, 0, 0, 0, 0, 0, 1]], dtype=bool)113114 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))115116 def test_get_mask_case_2(self):117 """118 Este metodo testea el metodo get_mask de la clase119 IntervalCellNeighborhood120 """121 neighborhood = IntervalCellNeighborhood(3, 4, inclusive=True)122 mask = np.array([[1, 0, 0, 1, 0, 0, 0, 1]], dtype=bool)123124 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))125126 def test_get_offset_case_1(self):127 """128 Este metodo testea el metodo get_mask de la clase129 IntervalCellNeighborhood130 """131 neighborhood = IntervalCellNeighborhood(4, 3)132 offset = (0, -4)133134 self.assertEqual(neighborhood.get_offset(), offset)135136 def test_get_offset_case_2(self):137 """138 Este metodo testea el metodo get_mask de la clase139 IntervalCellNeighborhood140 """141 neighborhood = IntervalCellNeighborhood(3, 4)142 offset = (0, -3)143144 self.assertEqual(neighborhood.get_offset(), offset)145146147class TestLeftSideNeighborhood(unittest.TestCase):148 """149 Tests para LeftSideNeighborhood150 """151152 def test_get_mask_case_1(self):153 """154 Este metodo testea el metodo get_mask de la clase155 LeftSideNeighborhood156 """157 neighborhood = LeftSideNeighborhood(4, inclusive=False)158 # son 5 entradas en el array porque se cuenta a la celula como un159 # vecino160 mask = np.array([[1, 1, 1, 1, 0]], dtype=bool)161162 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))163164 def test_get_mask_case_2(self):165 """166 Este metodo testea el metodo get_mask de la clase167 LeftSideNeighborhood168 """169 neighborhood = LeftSideNeighborhood(3, inclusive=True)170 mask = np.array([[1, 1, 1, 1]], dtype=bool)171172 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))173174 def test_get_offset_case_1(self):175 """176 Este metodo testea el metodo get_mask de la clase177 LeftSideNeighborhood178 """179 neighborhood = LeftSideNeighborhood(4)180 offset = (0, -4)181182 self.assertEqual(neighborhood.get_offset(), offset)183184 def test_get_offset_case_2(self):185 """186 Este metodo testea el metodo get_mask de la clase187 LeftSideNeighborhood188 """189 neighborhood = LeftSideNeighborhood(3)190 offset = (0, -3)191192 self.assertEqual(neighborhood.get_offset(), offset)193194195class TestRightSideNeighborhood(unittest.TestCase):196 """197 Tests para RightSideNeighborhood198 """199200 def test_get_mask_case_1(self):201 """202 Este metodo testea el metodo get_mask de la clase RightSideNeighborhood203 """204 neighborhood = RightSideNeighborhood(4, inclusive=False)205 # son 5 entradas en el array porque se cuenta a la celula como un206 # vecino207 mask = np.array([[0, 1, 1, 1, 1]], dtype=bool)208209 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))210211 def test_get_mask_case_2(self):212 """213 Este metodo testea el metodo get_mask de la clase RightSideNeighborhood214 """215 neighborhood = RightSideNeighborhood(3, inclusive=True)216 mask = np.array([[1, 1, 1, 1]], dtype=bool)217218 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))219220 def test_get_offset_case_1(self):221 """222 Este metodo testea el metodo get_mask de la clase RightSideNeighborhood223 """224 neighborhood = RightSideNeighborhood(4)225 offset = (0, 0)226227 self.assertEqual(neighborhood.get_offset(), offset)228229230class TestBothSideNeighborhood(unittest.TestCase):231 """232 Tests para BothSideNeighborhood233 """234235 def test_get_mask_case_1(self):236 """237 Este metodo testea el metodo get_mask de la clase238 BothSideNeighborhood239 """240 neighborhood = BothSideNeighborhood(4, 3, inclusive=False)241 # son 5 entradas en el array porque se cuenta a la celula como un242 # vecino243 mask = np.array([[1, 1, 1, 1, 0, 1, 1, 1]], dtype=bool)244245 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))246247 def test_get_mask_case_2(self):248 """249 Este metodo testea el metodo get_mask de la clase250 BothSideNeighborhood251 """252 neighborhood = BothSideNeighborhood(3, 4, inclusive=True)253 mask = np.array([[1, 1, 1, 1, 1, 1, 1, 1]], dtype=bool)254255 self.assertTrue(np.array_equal(neighborhood.get_mask(), mask))256257 def test_get_offset_case_1(self):258 """259 Este metodo testea el metodo get_mask de la clase260 BothSideNeighborhood261 """262 neighborhood = BothSideNeighborhood(4, 3)263 offset = (0, -4)264265 self.assertEqual(neighborhood.get_offset(), offset)266267 def test_get_offset_case_2(self):268 """269 Este metodo testea el metodo get_mask de la clase
...
splits.py
Source:splits.py
...19 for c in range(data.y.max() + 1):20 class_idx = development_idx[np.where(data.y[development_idx].cpu() == c)[0]]21 train_idx.extend(rnd_state.choice(class_idx, min(num_per_class, ceil(len(class_idx) * 0.5)), replace=False))22 val_idx = [i for i in development_idx if i not in train_idx]23 def get_mask(idx):24 mask = torch.zeros(num_nodes, dtype=torch.bool)25 mask[idx] = 126 return mask27 data.train_mask = get_mask(train_idx)28 data.val_mask = get_mask(val_idx)29 data.test_mask = get_mask(test_idx)30 return data31 32def set_train_val_test_split_frac(seed: int, data: Data, val_frac: float, test_frac: float):33 num_nodes = data.y.shape[0]34 val_size = ceil(val_frac * num_nodes)35 test_size = ceil(test_frac * num_nodes)36 train_size = num_nodes - val_size - test_size37 nodes = list(range(num_nodes))38 # Take same test set every time using development seed for robustness39 random.seed(development_seed)40 random.shuffle(nodes)41 test_idx = sorted(nodes[:test_size])42 nodes = [x for x in nodes if x not in test_idx]43 # Take train / val split according to seed44 random.seed(seed)45 random.shuffle(nodes)46 train_idx = sorted(nodes[:train_size])47 val_idx = sorted(nodes[train_size:])48 49 assert len(train_idx) + len(val_idx) + len(test_idx) == num_nodes50 def get_mask(idx):51 mask = torch.zeros(num_nodes, dtype=torch.bool)52 mask[idx] = 153 return mask54 data.train_mask = get_mask(train_idx)55 data.val_mask = get_mask(val_idx)56 data.test_mask = get_mask(test_idx)57 return data58# def set_train_val_test_split_classic(seed: int, data: Data, val_frac: float, test_frac: float):59# random.seed(seed)60# num_nodes = data.y.shape[0]61# val_size = ceil(val_frac * num_nodes)62# test_size = ceil(test_frac * num_nodes)63# train_size = num_nodes - val_size - test_size64# nodes = list(range(num_nodes))65# random.shuffle(nodes)66# train_idx = sorted(nodes[:train_size])67# val_idx = sorted(nodes[train_size:train_size+val_size])68# test_idx = sorted(nodes[train_size+val_size:])69# def get_mask(idx):70# mask = torch.zeros(num_nodes, dtype=torch.bool)71# mask[idx] = 172# return mask73# data.train_mask = get_mask(train_idx)74# data.val_mask = get_mask(val_idx)75# data.test_mask = get_mask(test_idx)76# return data77 78# def set_train_val_test_split_robust(seed: int, data: Data, val_frac: float, test_frac: float):79# num_nodes = data.y.shape[0]80# val_size = ceil(val_frac * num_nodes)81# test_size = ceil(test_frac * num_nodes)82# train_size = num_nodes - val_size - test_size83# nodes = list(range(num_nodes))84# # Take same test set every time using development seed for robustness85# random.seed(development_seed)86# random.shuffle(nodes)87# test_idx = sorted(nodes[:test_size])88# nodes = [x for x in nodes if x not in test_idx]89# # Take train / val split according to seed90# random.seed(seed)91# random.shuffle(nodes)92# train_idx = sorted(nodes[:train_size])93# val_idx = sorted(nodes[train_size:])94 95# assert len(train_idx) + len(val_idx) + len(test_idx) == num_nodes96# def get_mask(idx):97# mask = torch.zeros(num_nodes, dtype=torch.bool)98# mask[idx] = 199# return mask100# data.train_mask = get_mask(train_idx)101# data.val_mask = get_mask(val_idx)102# data.test_mask = get_mask(test_idx)103# return data104# def set_train_val_test_split(105# seed: int, data: Data, num_development: int = 1500, num_per_class: int = 20106# ) -> Data:107# rnd_state = np.random.RandomState(development_seed)108# num_nodes = data.y.shape[0]109# development_idx = rnd_state.choice(num_nodes, num_development, replace=False)110# test_idx = [i for i in np.arange(num_nodes) if i not in development_idx]111# train_idx = []112# rnd_state = np.random.RandomState(seed)113# for c in range(data.y.max() + 1):114# class_idx = development_idx[np.where(data.y[development_idx].cpu() == c)[0]]115# train_idx.extend(116# rnd_state.choice(117# class_idx, min(num_per_class, int(len(class_idx) * 0.7)), replace=False118# )119# )120# val_idx = [i for i in development_idx if i not in train_idx]121# def get_mask(idx):122# mask = torch.zeros(num_nodes, dtype=torch.bool)123# mask[idx] = 1124# return mask125# data.train_mask = get_mask(train_idx)126# data.val_mask = get_mask(val_idx)127# data.test_mask = get_mask(test_idx)128# return data129# def set_train_val_test_split_webkb(130# seed: int,131# data: Data,132# num_development: int = 1500,133# num_per_class: int = 20,134# train_proportion: float = None,135# ) -> Data:136# rnd_state = np.random.RandomState(development_seed)137# num_nodes = data.y.shape[0]138# development_idx = rnd_state.choice(num_nodes, num_development, replace=False)139# test_idx = [i for i in np.arange(num_nodes) if i not in development_idx]140# rnd_state = np.random.RandomState(seed)141# if train_proportion:142# train_idx = rnd_state.choice(143# development_idx, int(train_proportion * len(development_idx)), replace=False144# )145# else:146# train_idx = []147# for c in range(data.y.max() + 1):148# class_idx = development_idx[np.where(data.y[development_idx].cpu() == c)[0]]149# train_idx.extend(rnd_state.choice(class_idx, num_per_class, replace=False))150# val_idx = [i for i in development_idx if i not in train_idx]151# def get_mask(idx):152# mask = torch.zeros(num_nodes, dtype=torch.bool)153# mask[idx] = 1154# return mask155# data.train_mask = get_mask(train_idx)156# data.val_mask = get_mask(val_idx)157# data.test_mask = get_mask(test_idx)...
test_boolean_mask.py
Source:test_boolean_mask.py
...9LOGGING = False10MASK_SIZE = 1611class TestBooleanMasks(unittest.TestCase):12 def test_boolean_mask_is_all_false(self):13 self.assertFalse(get_mask(MASK_SIZE, type='empty').any())14 def test_boolean_mask_is_all_true(self):15 self.assertTrue(get_mask(MASK_SIZE, type='full').all())16 def test_boolean_mask_random(self):17 mask = get_mask(MASK_SIZE, type='random')18 print(f'A random mask is {mask}\n')19 self.assertTrue(True)20 def test_operator_and__on_boolean_mask(self):21 empty_mask = get_mask(MASK_SIZE, type='empty')22 full_mask = get_mask(MASK_SIZE, type='full')23 np.testing.assert_array_equal(empty_mask, np.logical_and(empty_mask, full_mask))24 def test_operator_or__on_boolean_mask(self):25 empty_mask = get_mask(MASK_SIZE, type='empty')26 full_mask = get_mask(MASK_SIZE, type='full')27 np.testing.assert_array_equal(full_mask, np.logical_or(empty_mask, full_mask))28 def test_boolean_mask_for_half(self):29 exp_mask = np.array([True, True, False, False])30 act_mask_0 = get_mask(4, type='half_0')31 act_mask_1 = get_mask(4, type='half_1')32 self.assertFalse(np.logical_xor(act_mask_0, exp_mask).all())33 self.assertFalse(np.logical_xor(act_mask_1, np.logical_not(exp_mask)).all())34 def test_boolean_operator_and_mask_repetitively (self):35 mask_set = self.get_set()36 b = bool_and(get_mask(MASK_SIZE, type='random'), get_mask(MASK_SIZE, type='random'))37 for i in range(100):38 b = bool_and(b, get_mask(MASK_SIZE, type='random'))39 if LOGGING:40 print(f'After multiple operation we have {b}')41 self.assertTrue(True)42 def test_random_boolean_operator_and_mask_repetitively (self):43 function_set = self.get_functions()44 function = random.choice(function_set)45 b = function(random.choice(get_mask(MASK_SIZE, type='random')), get_mask(MASK_SIZE, type='random'))46 for i in range(100):47 print(b)48 function = random.choice(function_set)49 if function == bool_not:50 b = function(b)51 else:52 b = function(b, get_mask(MASK_SIZE, type='random'))53 if LOGGING:54 print(f'After multiple operation we have {b}')55 self.assertTrue(True)56 @staticmethod57 def get_set():58 empty_mask = get_mask(MASK_SIZE, type='empty')59 full_mask = get_mask(MASK_SIZE, type='full')60 act_mask_0 = get_mask(MASK_SIZE, type='half_0')61 act_mask_1 = get_mask(MASK_SIZE, type='half_1')62 return [empty_mask, full_mask, act_mask_0, act_mask_1]63 @staticmethod64 def get_functions():65 return [bool_and, bool_or, bool_xor, bool_not]66 def test_get_mask_from_string_using_1111(self):67 full_mask = get_mask(4, type='full')68 np.testing.assert_array_equal(full_mask, get_mask_from_string("1111"))69 def test_get_mask_from_string_using_0000(self):70 empty_mask = get_mask(4, type='empty')71 np.testing.assert_array_equal(empty_mask, get_mask_from_string("0000"))72 def test_get_mask_from_string_using_1100(self):73 half_0_mask = get_mask(4, type='half_0')74 np.testing.assert_array_equal(half_0_mask, get_mask_from_string("1100"))75 def test_get_mask_from_string_using_0011(self):76 half_1_mask = get_mask(4, type='half_1')77 np.testing.assert_array_equal(half_1_mask, get_mask_from_string("0011"))78 def test_mask_to_string_from_1111(self):79 full_mask = get_mask(4, type='full')80 self.assertEqual("1111", mask_to_string(full_mask))81 def test_mask_to_string_from_0000(self):82 empty_mask = get_mask(4, type='empty')83 self.assertEqual("0000", mask_to_string(empty_mask))84 def test_mask_to_string_from_1100(self):85 half_0_mask = get_mask(4, type='half_0')86 self.assertEqual("1100", mask_to_string(half_0_mask))87 def test_mask_to_string_from_0011(self):88 half_1_mask = get_mask(4, type='half_1')...
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!!