Python itertools.product() Examples
The following are 30
code examples of itertools.product().
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
itertools
, or try the search function
.
Example #1
Source File: objective_test.py From OpenFermion-Cirq with Apache License 2.0 | 6 votes |
def test_get_matrix_of_eigs(): """ Generate the matrix of [exp(i (li - lj)) - 1] / (i(li - lj) :return: """ lam_vals = np.random.randn(4) + 1j * np.random.randn(4) mat_eigs = np.zeros((lam_vals.shape[0], lam_vals.shape[0]), dtype=np.complex128) for i, j in product(range(lam_vals.shape[0]), repeat=2): if np.isclose(abs(lam_vals[i] - lam_vals[j]), 0): mat_eigs[i, j] = 1 else: mat_eigs[i, j] = (np.exp(1j * (lam_vals[i] - lam_vals[j])) - 1) / ( 1j * (lam_vals[i] - lam_vals[j])) test_mat_eigs = get_matrix_of_eigs(lam_vals) assert np.allclose(test_mat_eigs, mat_eigs)
Example #2
Source File: ecs.py From aegea with Apache License 2.0 | 6 votes |
def tasks(args): list_clusters = clients.ecs.get_paginator("list_clusters") list_tasks = clients.ecs.get_paginator("list_tasks") def list_tasks_worker(worker_args): cluster, status = worker_args return cluster, status, list(paginate(list_tasks, cluster=cluster, desiredStatus=status)) def describe_tasks_worker(t, cluster=None): return clients.ecs.describe_tasks(cluster=cluster, tasks=t)["tasks"] if t else [] task_descs = [] if args.clusters is None: args.clusters = [__name__.replace(".", "_")] if args.tasks else list(paginate(list_clusters)) if args.tasks: task_descs = describe_tasks_worker(args.tasks, cluster=args.clusters[0]) else: with ThreadPoolExecutor() as executor: for cluster, status, tasks in executor.map(list_tasks_worker, product(args.clusters, args.desired_status)): worker = partial(describe_tasks_worker, cluster=cluster) descs = executor.map(worker, (tasks[pos:pos + 100] for pos in range(0, len(tasks), 100))) task_descs += sum(descs, []) page_output(tabulate(task_descs, args))
Example #3
Source File: kccsd_t_rhf.py From pyscf with Apache License 2.0 | 6 votes |
def transpose_t2(t2, nkpts, nocc, nvir, kconserv, out=None): '''Creates t2.transpose(2,3,1,0).''' if out is None: out = np.empty((nkpts,nkpts,nkpts,nvir,nvir,nocc,nocc), dtype=t2.dtype) # Check if it's stored in lower triangular form if len(t2.shape) == 7 and t2.shape[:2] == (nkpts, nkpts): for ki, kj, ka in product(range(nkpts), repeat=3): kb = kconserv[ki,ka,kj] out[ka,kb,kj] = t2[ki,kj,ka].transpose(2,3,1,0) elif len(t2.shape) == 6 and t2.shape[:2] == (nkpts*(nkpts+1)//2, nkpts): for ki, kj, ka in product(range(nkpts), repeat=3): kb = kconserv[ki,ka,kj] # t2[ki,kj,ka] = t2[tril_index(ki,kj),ka] ki<kj # t2[kj,ki,kb] = t2[ki,kj,ka].transpose(1,0,3,2) ki<kj # = t2[tril_index(ki,kj),ka].transpose(1,0,3,2) if ki <= kj: tril_idx = (kj*(kj+1))//2 + ki out[ka,kb,kj] = t2[tril_idx,ka].transpose(2,3,1,0).copy() out[kb,ka,ki] = out[ka,kb,kj].transpose(1,0,3,2) else: raise ValueError('No known conversion for t2 shape %s' % t2.shape) return out
Example #4
Source File: many_to_one.py From matchpy with MIT License | 6 votes |
def _match_with_bipartite( self, subject_ids: MultisetOfInt, pattern_set: MultisetOfInt, substitution: Substitution, ) -> Iterator[Tuple[Substitution, MultisetOfInt]]: bipartite = self._build_bipartite(subject_ids, pattern_set) for matching in enum_maximum_matchings_iter(bipartite): if len(matching) < len(pattern_set): break if not self._is_canonical_matching(matching): continue for substs in itertools.product(*(bipartite[edge] for edge in matching.items())): try: bipartite_substitution = substitution.union(*substs) except ValueError: continue matched_subjects = Multiset(subexpression for subexpression, _ in matching) yield bipartite_substitution, matched_subjects
Example #5
Source File: GameOfLife.py From BiblioPixelAnimations with MIT License | 6 votes |
def __init__(self, width, height, depth, rand_max, table=None): self.toroidal = True self._rand_max = rand_max if table: self.table = table self.depth = len(table) self.height = len(table[0]) self.width = len(table[0][0]) else: self.height = height self.width = width self.depth = depth self.genNewTable() self._oldStates = deque() for i in range(3): self._oldStates.append([]) self.offsets = list(itertools.product([-1, 0, 1], repeat=3)) self.offsets.remove((0, 0, 0)) # remove center point
Example #6
Source File: eom_kccsd_ghf.py From pyscf with Apache License 2.0 | 6 votes |
def vector_to_amplitudes_ea(vector, kshift, nkpts, nmo, nocc, kconserv): nvir = nmo - nocc r1 = vector[:nvir].copy() r2_tril = vector[nvir:].copy().reshape(nocc*nkpts*nvir*(nkpts*nvir-1)//2) r2 = np.zeros((nkpts,nkpts,nocc,nvir,nvir), dtype=vector.dtype) index = 0 for kj, ka in itertools.product(range(nkpts), repeat=2): kb = kconserv[kshift,ka,kj] if ka < kb: idx, idy = np.tril_indices(nvir, 0) else: idx, idy = np.tril_indices(nvir, -1) tmp = r2_tril[index:index + nocc*len(idy)].reshape(-1,nocc) r2[kj,ka,:,idx,idy] = tmp r2[kj,kb,:,idy,idx] = -tmp index = index + nocc*len(idy) return [r1,r2]
Example #7
Source File: kccsd_t_rhf.py From pyscf with Apache License 2.0 | 6 votes |
def create_eris_vooo(ooov, nkpts, nocc, nvir, kconserv, out=None): '''Creates vooo from ooov array. This is not exactly chemist's notation, but close. Here a chemist notation vooo is created from physicist ooov, and then the last two indices of vooo are swapped. ''' assert(ooov.shape == (nkpts,nkpts,nkpts,nocc,nocc,nocc,nvir)) if out is None: out = np.empty((nkpts,nkpts,nkpts,nvir,nocc,nocc,nocc), dtype=ooov.dtype) for ki, kj, ka in product(range(nkpts), repeat=3): kb = kconserv[ki,kj,ka] # <bj|ai> -> (ba|ji) (Physicist->Chemist) # (ij|ab) = (ba|ij)* (Permutational symmetry) # out = (ij|ab).transpose(0,1,3,2) out[ki,kj,kb] = ooov[kb,kj,ka].conj().transpose(3,1,0,2) return out
Example #8
Source File: test_parameters.py From pywr with GNU General Public License v3.0 | 6 votes |
def test_load(self, simple_linear_model): """ Test load from JSON dict""" model = simple_linear_model data = { "type": "aggregated", "agg_func": "product", "parameters": [ 0.8, { "type": "monthlyprofile", "values": list(range(12)) } ] } p = load_parameter(model, data) # Correct instance is loaded assert isinstance(p, AggregatedParameter) @assert_rec(model, p) def expected(timestep, scenario_index): return (timestep.month - 1) * 0.8 model.run()
Example #9
Source File: test_wrappers.py From mmdetection with Apache License 2.0 | 6 votes |
def test_max_pool_2d(): test_cases = OrderedDict([('in_w', [10, 20]), ('in_h', [10, 20]), ('in_channel', [1, 3]), ('out_channel', [1, 3]), ('kernel_size', [3, 5]), ('stride', [1, 2]), ('padding', [0, 1]), ('dilation', [1, 2])]) for in_h, in_w, in_cha, out_cha, k, s, p, d in product( *list(test_cases.values())): # wrapper op with 0-dim input x_empty = torch.randn(0, in_cha, in_h, in_w, requires_grad=True) wrapper = MaxPool2d(k, stride=s, padding=p, dilation=d) wrapper_out = wrapper(x_empty) # torch op with 3-dim input as shape reference x_normal = torch.randn(3, in_cha, in_h, in_w) ref = nn.MaxPool2d(k, stride=s, padding=p, dilation=d) ref_out = ref(x_normal) assert wrapper_out.shape[0] == 0 assert wrapper_out.shape[1:] == ref_out.shape[1:] assert torch.equal(wrapper(x_normal), ref_out)
Example #10
Source File: kccsd_t_rhf.py From pyscf with Apache License 2.0 | 6 votes |
def _tile_list(kpt_indices): '''Similar to a cartesian product but for a list of kpoint indices for a 3-particle operator.''' max_length = 0 out_indices = [0]*6 for ix, x in enumerate(kpt_indices): if hasattr(x, '__len__'): max_length = max(max_length, len(x)) if max_length == 0: return kpt_indices else: for ix, x in enumerate(kpt_indices): if isinstance(x, (int, np.int)): out_indices[ix] = [x] * max_length else: out_indices[ix] = x return map(list, zip(*out_indices))
Example #11
Source File: image_batches.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def init_from_dataset_and_submissions_write_to_datastore( self, dataset_batches, attack_submission_ids): """Init list of adversarial batches from dataset batches and submissions. Args: dataset_batches: instances of DatasetBatches attack_submission_ids: iterable with IDs of all (targeted and nontargeted) attack submissions, could be obtains as CompetitionSubmissions.get_all_attack_ids() """ batches_x_attacks = itertools.product(dataset_batches.data.keys(), attack_submission_ids) for idx, (dataset_batch_id, attack_id) in enumerate(batches_x_attacks): adv_batch_id = ADVERSARIAL_BATCH_ID_PATTERN.format(idx) self.add_batch(adv_batch_id, {'dataset_batch_id': dataset_batch_id, 'submission_id': attack_id}) self.write_to_datastore()
Example #12
Source File: test_parameters.py From pywr with GNU General Public License v3.0 | 6 votes |
def test_parameter_constant_scenario(simple_linear_model): """ Test ConstantScenarioIndexParameter """ model = simple_linear_model # Add two scenarios scA = Scenario(model, 'Scenario A', size=2) scB = Scenario(model, 'Scenario B', size=5) p = ConstantScenarioIndexParameter(model, scB, np.arange(scB.size, dtype=np.int32)) model.setup() ts = model.timestepper.current # Now ensure the appropriate value is returned for the Scenario B indices. for i, (a, b) in enumerate(itertools.product(range(scA.size), range(scB.size))): si = ScenarioIndex(i, np.array([a, b], dtype=np.int32)) np.testing.assert_allclose(p.index(ts, si), b)
Example #13
Source File: test_parameters.py From pywr with GNU General Public License v3.0 | 6 votes |
def test_parameter_constant_scenario(simple_linear_model): """ Test ConstantScenarioParameter """ model = simple_linear_model # Add two scenarios scA = Scenario(model, 'Scenario A', size=2) scB = Scenario(model, 'Scenario B', size=5) p = ConstantScenarioParameter(model, scB, np.arange(scB.size, dtype=np.float64)) model.setup() ts = model.timestepper.current # Now ensure the appropriate value is returned for the Scenario B indices. for i, (a, b) in enumerate(itertools.product(range(scA.size), range(scB.size))): si = ScenarioIndex(i, np.array([a, b], dtype=np.int32)) np.testing.assert_allclose(p.value(ts, si), float(b))
Example #14
Source File: test_parameters.py From pywr with GNU General Public License v3.0 | 6 votes |
def test_basic_use(self, simple_linear_model): """ Test the basic use of `ConstantParameter` using the Python API """ model = simple_linear_model # Add two scenarios scA = Scenario(model, 'Scenario A', size=2) scB = Scenario(model, 'Scenario B', size=5) p = ConstantParameter(model, np.pi, name='pi', comment='Mmmmm Pi!') assert not p.is_variable assert p.double_size == 1 assert p.integer_size == 0 model.setup() ts = model.timestepper.current # Now ensure the appropriate value is returned for all scenarios for i, (a, b) in enumerate(itertools.product(range(scA.size), range(scB.size))): si = ScenarioIndex(i, np.array([a, b], dtype=np.int32)) np.testing.assert_allclose(p.value(ts, si), np.pi)
Example #15
Source File: rnn.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def main(): # testing configurations cell_types = [gluon.rnn.RNNCell, gluon.rnn.GRUCell, gluon.rnn.LSTMCell] ctxs = [mx.cpu(0)] if args.gpu: ctxs = ctxs + [mx.gpu(i) for i in _get_gpus()] seq_lens = [100] batch_sizes = [1, 32] hidden_dims = [512] print("--------------------------------------") print("Benchmarking", args.benchmark) for cell_type, ctx, seq_len, batch_size, hidden_dim in product( \ cell_types, ctxs, seq_lens, batch_sizes, hidden_dims): print("--------------------------------------") print("cell: %s ctx: %s length: %d batch size: %d dim: %d" % \ (cell_type.__name__, str(ctx), seq_len, batch_size, hidden_dim)) run_benchmark(cell_type, ctx, seq_len, batch_size, hidden_dim)
Example #16
Source File: test_assertions.py From jawfish with MIT License | 6 votes |
def assertMessagesCM(self, methodName, args, func, errors): """ Check that the correct error messages are raised while executing: with method(*args): func() *errors* should be a list of 4 regex that match the error when: 1) longMessage = False and no msg passed; 2) longMessage = False and msg passed; 3) longMessage = True and no msg passed; 4) longMessage = True and msg passed; """ p = product((self.testableFalse, self.testableTrue), ({}, {"msg": "oops"})) for (cls, kwargs), err in zip(p, errors): method = getattr(cls, methodName) with self.assertRaisesRegex(cls.failureException, err): with method(*args, **kwargs) as cm: func()
Example #17
Source File: visual_test.py From toolium with Apache License 2.0 | 6 votes |
def exclude_elements(self, img, web_elements): """Modify image hiding elements with a black rectangle :param img: image object :param web_elements: WebElement objects to be excluded """ if web_elements and len(web_elements) > 0: img = img.convert("RGBA") pixel_data = img.load() for web_element in web_elements: element_box = self.get_element_box(web_element) for x, y in itertools.product(xrange(element_box[0], element_box[2]), xrange(element_box[1], element_box[3])): try: pixel_data[x, y] = (0, 0, 0, 255) except IndexError: pass return img
Example #18
Source File: minitaur_terrain_randomizer.py From soccer-matlab with BSD 2-Clause "Simplified" License | 6 votes |
def _is_close_to_existing_points(self, point): """Checks if the point is close to any already sampled (and stored) points. Args: point: A 2D point (list) described by its coordinates (x, y). Returns: True iff the distance of the point to any existing points is smaller than the min_radius """ px, py = self._point_to_index_2d(point) # Now we can check nearby cells for existing points for neighbor_cell in itertools.product( xrange(px - 1, px + 2), xrange(py - 1, py + 2)): if not self._is_in_range(neighbor_cell): continue maybe_a_point = self._grid[self._index_2d_to_1d(neighbor_cell)] if maybe_a_point is not None and np.linalg.norm( maybe_a_point - point) < self._min_radius: return True return False
Example #19
Source File: config_generator.py From MySQL-AutoXtraBackup with MIT License | 6 votes |
def options_combination_generator(initial_str): ''' Option parser method for creating option combinarotics :param initial_str -> mysql_options initial string from config file :return List of tuples with option combinations ''' separated_values_list = [] for i in initial_str.split(','): separated_values_list.append(i.split('=')) all_new_list = [] for i in separated_values_list: k = ["{}={}".format(i[0], j) for j in i[1].split()] all_new_list.append(k) option_combinations = [] for i in product(*all_new_list): option_combinations.append(i) return option_combinations
Example #20
Source File: optimal_givens_decomposition_test.py From OpenFermion-Cirq with Apache License 2.0 | 6 votes |
def test_circuit_generation_and_accuracy(): for dim in range(2, 10): qubits = cirq.LineQubit.range(dim) u_generator = numpy.random.random( (dim, dim)) + 1j * numpy.random.random((dim, dim)) u_generator = u_generator - numpy.conj(u_generator).T assert numpy.allclose(-1 * u_generator, numpy.conj(u_generator).T) unitary = scipy.linalg.expm(u_generator) circuit = cirq.Circuit() circuit.append(optimal_givens_decomposition(qubits, unitary)) fermion_generator = QubitOperator(()) * 0.0 for i, j in product(range(dim), repeat=2): fermion_generator += jordan_wigner( FermionOperator(((i, 1), (j, 0)), u_generator[i, j])) true_unitary = scipy.linalg.expm( get_sparse_operator(fermion_generator).toarray()) assert numpy.allclose(true_unitary.conj().T.dot(true_unitary), numpy.eye(2 ** dim, dtype=complex)) test_unitary = cirq.unitary(circuit) assert numpy.isclose( abs(numpy.trace(true_unitary.conj().T.dot(test_unitary))), 2 ** dim)
Example #21
Source File: higham_test.py From OpenFermion-Cirq with Apache License 2.0 | 6 votes |
def test_matrix_2_tensor(): dim = 10 np.random.seed(42) mat = np.random.random((dim**2, dim**2)) mat = 0.5 * (mat + mat.T) tensor = map_to_tensor(mat) for p, q, r, s in product(range(dim), repeat=4): assert np.isclose(tensor[p, q, r, s], mat[p * dim + q, r * dim + s]) test_mat = map_to_matrix(tensor) assert np.allclose(test_mat, mat) with pytest.raises(TypeError): map_to_tensor(np.zeros((4, 4, 4, 4))) with pytest.raises(TypeError): map_to_matrix(np.zeros((4, 4)))
Example #22
Source File: objective.py From OpenFermion-Cirq with Apache License 2.0 | 6 votes |
def get_matrix_of_eigs(w: np.ndarray) -> np.ndarray: """ Transform the eigenvalues for getting the gradient .. math: f(w) \rightarrow \frac{e^{i (\lambda_{i} - \lambda_{j})}{i (\lambda_{i} - \lambda_{j})} :param w: eigenvalues of C-matrix :return: new array of transformed eigenvalues """ transform_eigs = np.zeros((w.shape[0], w.shape[0]), dtype=np.complex128) for i, j in product(range(w.shape[0]), repeat=2): if np.isclose(abs(w[i] - w[j]), 0): transform_eigs[i, j] = 1 else: transform_eigs[i, j] = (np.exp(1j * (w[i] - w[j])) - 1) / ( 1j * (w[i] - w[j])) return transform_eigs
Example #23
Source File: segmentation.py From steppy-toolkit with MIT License | 6 votes |
def _get_tta_data(self, i, row): original_specs = {'ud_flip': False, 'lr_flip': False, 'rotation': 0, 'color_shift': False} tta_specs = [original_specs] ud_options = [True, False] if self.tta_transformations.flip_ud else [False] lr_options = [True, False] if self.tta_transformations.flip_lr else [False] rot_options = [0, 90, 180, 270] if self.tta_transformations.rotation else [0] if self.tta_transformations.color_shift_runs: color_shift_options = list(range(1, self.tta_transformations.color_shift_runs + 1, 1)) else: color_shift_options = [False] for ud, lr, rot, color in product(ud_options, lr_options, rot_options, color_shift_options): if ud is False and lr is False and rot == 0 and color is False: continue else: tta_specs.append({'ud_flip': ud, 'lr_flip': lr, 'rotation': rot, 'color_shift': color}) img_ids = [i] * len(tta_specs) X_rows = [row] * len(tta_specs) return X_rows, tta_specs, img_ids
Example #24
Source File: segmentation.py From steppy-toolkit with MIT License | 6 votes |
def _get_tta_data(self, i, row): original_specs = {'ud_flip': False, 'lr_flip': False, 'rotation': 0, 'color_shift': False} tta_specs = [original_specs] ud_options = [True, False] if self.tta_transformations.flip_ud else [False] lr_options = [True, False] if self.tta_transformations.flip_lr else [False] rot_options = [0, 90, 180, 270] if self.tta_transformations.rotation else [0] if self.tta_transformations.color_shift_runs: color_shift_options = list(range(1, self.tta_transformations.color_shift_runs + 1, 1)) else: color_shift_options = [False] for ud, lr, rot, color in product(ud_options, lr_options, rot_options, color_shift_options): if ud is False and lr is False and rot == 0 and color is False: continue else: tta_specs.append({'ud_flip': ud, 'lr_flip': lr, 'rotation': rot, 'color_shift': color}) img_ids = [i] * len(tta_specs) X_rows = [row] * len(tta_specs) return X_rows, tta_specs, img_ids
Example #25
Source File: models.py From pinax-documents with MIT License | 6 votes |
def share(self, users): """ Ensures self is shared with given users (can accept users who are already shared on self). """ users = [u for u in users if not self.shared_with(user=u)] if users: members = [self] + self.members(direct=False) FM, DM = self.shared_user_model(), Document.shared_user_model() fm, dm = [], [] for member, user in itertools.product(members, users): if user.pk == member.author_id: continue if isinstance(member, Folder): fm.append(FM(**{FM.obj_attr: member, "user": user})) if isinstance(member, Document): dm.append(DM(**{DM.obj_attr: member, "user": user})) FM._default_manager.bulk_create(fm) DM._default_manager.bulk_create(dm)
Example #26
Source File: bit_counter.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def _new_sym_idxs_itr(syms_shape, ctx_size): D, H, W = syms_shape pad = ctx_size // 2 return itertools.product( range(pad, D), # D dimension is not padded range(pad, H - pad), range(pad, W - pad)) # yields tuples (d, h, w)
Example #27
Source File: SubtypeCompleteness.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def comp_calc2(self, smin, smax, dMag_min, dMag_max, subpop=-2): """Calculates completeness for given minimum and maximum separations and dMag Note: this method assumes scaling orbits when scaleOrbits == True has already occurred for smin, smax, dMag inputs Args: smin (float ndarray): Minimum separation(s) in AU smax (float ndarray): Maximum separation(s) in AU dMag_min (float ndarray): Minimum difference in brightness magnitude dMag_max (float ndarray): Maximum difference in brightness magnitude subpop (int): planet subtype to use for calculation of comp0 -2 - planet population -1 - earthLike population (i,j) - kopparapu planet subtypes Returns: float ndarray: Completeness values """ if subpop == -2: comp = self.EVPOC_pop(smin, smax, dMag_min, dMag_max) elif subpop == -1: comp = self.EVPOC_earthlike(smin, smax, dMag_min, dMag_max) else: #for ii,j in itertools.product(np.arange(len(self.Rp_hi)),np.arange(len(self.L_lo))): comp = self.EVPOC_hs[subpop[0],subpop[1]](smin, smax, dMag_min, dMag_max) # remove small values comp[comp<1e-6] = 0. return comp
Example #28
Source File: test_bipartite.py From matchpy with MIT License | 5 votes |
def test_completeness(n, m): graph = BipartiteGraph(map(lambda x: (x, True), itertools.product(range(n), range(m)))) count = sum(1 for _ in enum_maximum_matchings_iter(graph)) expected_count = m > 0 and math.factorial(n) / math.factorial(n - m) or 0 assert count == expected_count
Example #29
Source File: utils.py From matchpy with MIT License | 5 votes |
def optional_iter(remaining, count): for p in itertools.product([0, 1], repeat=count): yield remaining - sum(p), p
Example #30
Source File: api.py From mutatest with MIT License | 5 votes |
def targets(self) -> Set[GenomeGroupTarget]: """All mutation targets in the group, returned as tuples of ``source_file`` and location indices in a single set. Returns: Set of tuples of ``source_file`` and location index for all targets in the group. These are ``GenomeGroupTargets`` to make attribute access easier. """ targets = set() for k, v in self.items(): targets.update(set(itertools.product([k], v.targets))) return {GenomeGroupTarget(*t) for t in targets}