def _random_node_pairs(nodes1, nodes2, graph, n_sample): """ return `n_sample` random negative node pairs (nodes1 vs node2) """ sampled_pairs = [] while True: if len(sampled_pairs) == n_sample: return sampled_pairs node_pair = random_product(nodes1, nodes2) if not graph.has_edge(*node_pair): sampled_pairs.append(node_pair)
def test_list_with_repeat(self): """ensure multiple items are chosen, and that they appear to be chosen from one list then the next, in proper order. """ nums = [1, 2, 3] lets = ['a', 'b', 'c'] r = list(mi.random_product(nums, lets, repeat=100)) self.assertEqual(2 * 100, len(r)) n, m = set(r[::2]), set(r[1::2]) self.assertEqual(n, set(nums)) self.assertEqual(m, set(lets)) self.assertEqual(len(n), len(nums)) self.assertEqual(len(m), len(lets))
def _sonsGen(self, point): args = [] count = 0 for item in self.stepSizes: arg = [] if count < len(self.params): if (point[count] - item) > self.params[count][0]: arg.append(point[count] - item) arg.append(point[count]) if (point[count] + item) < self.params[count][-1]: arg.append(point[count] + item) args.append(arg) count += 1 return more_itertools.random_product(*args)
def test_simple_lists(self): """Ensure that one item is chosen from each list in each pair. Also ensure that each item from each list eventually appears in the chosen combinations. Odds are roughly 1 in 7.1 * 10e16 that one item from either list will not be chosen after 100 samplings of one item from each list. Just to be safe, better use a known random seed, too. """ nums = [1, 2, 3] lets = ['a', 'b', 'c'] n, m = zip(*[mi.random_product(nums, lets) for _ in range(100)]) n, m = set(n), set(m) self.assertEqual(n, set(nums)) self.assertEqual(m, set(lets)) self.assertEqual(len(n), len(nums)) self.assertEqual(len(m), len(lets))
def _sonsGen(self, point): sons = [] args = [] count = 0 for item in self.stepSizes: arg = [] if count < len(self.params): if (point[count] - item) > self.params[count][0]: arg.append(point[count] - item) arg.append(point[count]) if (point[count] + item) < self.params[count][-1]: arg.append(point[count] + item) args.append(arg) count += 1 if self.searchCount is -1: for son in it.product(*args): sons.append(son) else: while len(sons) < self.searchCount: son = more_itertools.random_product(*args) sons.append(son) return sons