def randomly_delete_pages(self): for _ in range(R(1, 6)): if not self._expected_pages: break idx = R(0, len(self._expected_pages)) del self._expected_pages[idx] del self.flipbook.pages[idx]
def randomly_insert_images(self): if not self._expected_pages: return page_idx = R(0, len(self._expected_pages)) page, expected_page = self.flipbook.pages[page_idx], self._expected_pages[page_idx] images = [self.images[R(0, len(self.images))] for _ in range(R(1,6))] image_idx = R(0, len(expected_page)) if expected_page else 0 page[image_idx:image_idx] = images expected_page[image_idx:image_idx] = images
def randomly_delete_images(self): if not self._expected_pages: return page_idx = R(0, len(self._expected_pages)) page, expected_page = self.flipbook.pages[page_idx], self._expected_pages[page_idx] if not expected_page: return imcount = R(1, len(expected_page)+1) im_idx = R(0, len(expected_page)) del page[im_idx:(im_idx+imcount)] del expected_page[im_idx:(im_idx+imcount)]
def give(dim, n, channels, out_size=10): if dim == 1: x = R(seed).random((ds_size, n, channels)) x = x.reshape(x.shape[0], n, channels) else: x = R(seed).random((ds_size, n, n, channels)) x = x.reshape(x.shape[0], n, n, channels) y = R(seed).randint(0, out_size, ds_size) y = tf.keras.utils.to_categorical(y, out_size) dataset = tf.data.Dataset.from_tensor_slices((x, y)) return dataset
def give(dim, n, channels, out_size=10): if dim == 1: x = R(seed).random((ds_size, channels, n)) x = x.reshape(x.shape[0], channels, n) else: x = R(seed).random((ds_size, channels, n, n)) x = x.reshape(x.shape[0], channels, n, n) y = R(seed).randint(0, out_size, ds_size) x = torch.from_numpy(x).type(torch.FloatTensor) y = torch.from_numpy(y).type(torch.LongTensor) train_data = torch.utils.data.TensorDataset(x, y) return train_data
def do_random_operations(self): for _ in range(self.number_of_operations_spinner.value()): op, op_name = self._random_operations[R(0, len(self._random_operations))] op() if not self.check_state(op_name): break
def randomly_merge_pages(self): if len(self._expected_pages) < 2: return idxs = sorted(numpy.random.choice([i for i in range(len(self._expected_pages))], min(R(2,6), len(self._expected_pages)), False)) sm = self.flipbook.pages_view.selectionModel() m = self.flipbook.pages_view.model() sm.clearSelection() for idx in idxs: sm.select(m.index(idx, 0), Qt.QItemSelectionModel.Select) self.flipbook.merge_selected() target_idx = idxs.pop(0) expected_target_page = self._expected_pages[target_idx] for idx in idxs: expected_target_page.extend(self._expected_pages[idx]) for idx in reversed(idxs): del self._expected_pages[idx]
def randomly_insert_pages(self): for _ in range(R(1, 6)): page_idx = R(0, len(self._expected_pages)) if self._expected_pages else 0 images = [self.images[R(0, len(self.images))] for _ in range(R(1,6))] self.flipbook.pages.insert(page_idx, images) self._expected_pages.insert(page_idx, images)
def _test_plain_list_behavior_fidelity(cls, num_iterations=40, stuff_max_len=20, verbose=False): """For development and testing purposes, this function performs the same operations on a SignalingList and a plain list, and verifies that their contents remain identical. The operations themselves are primarily random extended slicing operations, with the occassional extend call, randomly strided delete of a random range, and insert call with a random index and value. Verbose output for any of the three occassional test operations is prepended with a * for visibility.""" import numpy import numpy.random from numpy.random import randint as R sl = cls() l = [] mb = lambda r: R(len(l)+1) if r else R(-2*len(l)-10, 2*len(l)+10) for iteration in range(num_iterations): stuff = list(R(1024, size=(R(stuff_max_len),))) rb, re = R(2, size=(2,)) b, e = mb(rb), mb(re) if R(2) and b > e: b, e = e, b ol = list(l) func = R(100) if func < 96: l[b:e] = stuff sl[b:e] = stuff if l != sl: raise RuntimeError('{}[{}:{}] = {}:\n{} !=\n{}'.format(ol, b, e, stuff, sl._list, l)) if verbose: print('{}[{}:{}] = {}'.format(ol, b, e, stuff)) elif func < 97: s = (1 if R(3) else -1) * (1 if R(5) else R(5)+1) del l[b:e:s] del sl[b:e:s] if l != sl: raise RuntimeError('del {}[{}:{}:{}]:\n{} !=\n{}'.format(ol, b, e, s, sl._list, l)) if verbose: print('* del {}[{}:{}:{}]'.format(ol, b, e, s)) elif func < 98: l.extend(stuff) sl.extend(stuff) if l != sl: raise RuntimeError('{}.extend({}):\n{} !=\n{}'.format(ol, stuff, sl._list, l)) if verbose: print('* {}.extend({})'.format(ol, stuff)) else: stuff = R(1024) l.insert(b, stuff) sl.insert(b, stuff) if l != sl: raise RuntimeError('{}.insert({}, {}):\n{} !=\n{}'.format(ol, b, stuff, sl._list, l)) if verbose: print('* {}.insert({}, {})'.format(ol, b, stuff))