Python numpy.ndindex() Examples
The following are 30 code examples for showing how to use numpy.ndindex(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: deep-smoke-machine Author: CMU-CREATE-Lab File: compute_dark_channel.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_dark_channel(I, w=15): """Get the dark channel prior in the (RGB) image data. Parameters ----------- I: an M * N * 3 numpy array containing data ([0, 255]) in the image where M is the height, N is the width, 3 represents R/G/B channels. w: window size Return ----------- An M * N array for the dark channel prior ([0, 255]). """ M, N, _ = I.shape padded = np.pad(I, ((w // 2, w // 2), (w // 2, w // 2), (0, 0)), 'edge') darkch = np.zeros((M, N), dtype=I.dtype) for i, j in np.ndindex(darkch.shape): # This is from equation 5 in the above mentioned dark channel paper darkch[i, j] = np.min(padded[i:i + w, j:j + w, :]) return darkch
Example 2
Project: aetros-cli Author: aetros File: image.py License: MIT License | 6 votes |
def upscale(image, ratio): """ return upscaled image array Arguments: image -- a (H,W,C) numpy.ndarray ratio -- scaling factor (>1) """ if not isinstance(image, np.ndarray): raise ValueError('Expected ndarray') if ratio < 1: raise ValueError('Ratio must be greater than 1 (ratio=%f)' % ratio) width = int(math.floor(image.shape[1] * ratio)) height = int(math.floor(image.shape[0] * ratio)) channels = image.shape[2] out = np.ndarray((height, width, channels), dtype=np.uint8) for x, y in np.ndindex((width, height)): out[y, x] = image[int(math.floor(y / ratio)), int(math.floor(x / ratio))] return out
Example 3
Project: westpa Author: westpa File: assign.py License: MIT License | 6 votes |
def boundaries(self, boundaries): del self._boundaries, self.labels self._boundaries = [] self.labels = labels = [] for boundset in boundaries: boundarray = numpy.asarray(boundset,dtype=coord_dtype, order='C') db = numpy.diff(boundarray) if (db <= 0).any(): raise ValueError('boundary set must be strictly monotonically increasing') self._boundaries.append(boundarray) self._boundlens = numpy.array([len(boundset) for boundset in self._boundaries], dtype=index_dtype) self.ndim = len(self._boundaries) self.nbins = numpy.multiply.accumulate([1] + [len(bounds)-1 for bounds in self._boundaries])[-1] _boundaries = self._boundaries binspace_shape = tuple(self._boundlens[:]-1) for index in numpy.ndindex(binspace_shape): bounds = [(_boundaries[idim][index[idim]], boundaries[idim][index[idim]+1]) for idim in range(len(_boundaries))] labels.append(repr(bounds))
Example 4
Project: OpenCV-Python-Tutorial Author: makelove File: video.py License: MIT License | 6 votes |
def __init__(self, **kw): super(Chess, self).__init__(**kw) w, h = self.frame_size self.grid_size = sx, sy = 10, 7 white_quads = [] black_quads = [] for i, j in np.ndindex(sy, sx): q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]] [white_quads, black_quads][(i + j) % 2].append(q) self.white_quads = np.float32(white_quads) self.black_quads = np.float32(black_quads) fx = 0.9 self.K = np.float64([[fx*w, 0, 0.5*(w-1)], [0, fx*w, 0.5*(h-1)], [0.0,0.0, 1.0]]) self.dist_coef = np.float64([-0.2, 0.1, 0, 0]) self.t = 0
Example 5
Project: tensor2tensor Author: tensorflow File: ngram.py License: Apache License 2.0 | 6 votes |
def call(self, inputs): batch_shape = tf.shape(inputs)[:-1] length = tf.shape(inputs)[-1] ngram_range_counts = [] for n in range(self.minval, self.maxval): # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping # remainder elements. Each n-vector is an ngram. reshaped_inputs = tf.reshape( inputs[..., :(n * (length // n))], tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0)) # Count the number of times each ngram appears in the input. We do so by # checking whether each n-vector in the input is equal to each n-vector # in a Tensor of all possible ngrams. The comparison is batched between # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor # of shape [..., input_dim**n, 1, n]. ngrams = tf.reshape( list(np.ndindex((self.input_dim,) * n)), [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n]) is_ngram = tf.equal( tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32), axis=-1), n) ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1) ngram_range_counts.append(ngram_counts) return tf.concat(ngram_range_counts, axis=-1)
Example 6
Project: BERT Author: yyht File: ngram.py License: Apache License 2.0 | 6 votes |
def call(self, inputs): batch_shape = tf.shape(inputs)[:-1] length = tf.shape(inputs)[-1] ngram_range_counts = [] for n in range(self.minval, self.maxval): # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping # remainder elements. Each n-vector is an ngram. reshaped_inputs = tf.reshape( inputs[..., :(n * (length // n))], tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0)) # Count the number of times each ngram appears in the input. We do so by # checking whether each n-vector in the input is equal to each n-vector # in a Tensor of all possible ngrams. The comparison is batched between # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor # of shape [..., input_dim**n, 1, n]. ngrams = tf.reshape( list(np.ndindex((self.input_dim,) * n)), [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n]) is_ngram = tf.equal( tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32), axis=-1), n) ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1) ngram_range_counts.append(ngram_counts) return tf.concat(ngram_range_counts, axis=-1)
Example 7
Project: MachineLearning Author: mengli File: video.py License: Apache License 2.0 | 6 votes |
def __init__(self, **kw): super(Chess, self).__init__(**kw) w, h = self.frame_size self.grid_size = sx, sy = 10, 7 white_quads = [] black_quads = [] for i, j in np.ndindex(sy, sx): q = [[j, i, 0], [j + 1, i, 0], [j + 1, i + 1, 0], [j, i + 1, 0]] [white_quads, black_quads][(i + j) % 2].append(q) self.white_quads = np.float32(white_quads) self.black_quads = np.float32(black_quads) fx = 0.9 self.K = np.float64([[fx * w, 0, 0.5 * (w - 1)], [0, fx * w, 0.5 * (h - 1)], [0.0, 0.0, 1.0]]) self.dist_coef = np.float64([-0.2, 0.1, 0, 0]) self.t = 0
Example 8
Project: OpenFermion Author: quantumlib File: _quadratic_hamiltonian_test.py License: Apache License 2.0 | 6 votes |
def test_add_chemical_potential(self): """Test adding a chemical potential.""" self.quad_ham_npc.add_chemical_potential(2.4) combined_hermitian_part = self.quad_ham_npc.combined_hermitian_part hermitian_part = self.quad_ham_npc.hermitian_part want_combined = (self.combined_hermitian - 2.4 * numpy.eye(self.n_qubits)) want_hermitian = self.hermitian_mat for i in numpy.ndindex(combined_hermitian_part.shape): self.assertAlmostEqual(combined_hermitian_part[i], want_combined[i]) for i in numpy.ndindex(hermitian_part.shape): self.assertAlmostEqual(hermitian_part[i], want_hermitian[i]) self.assertAlmostEqual(2.4 + self.chemical_potential, self.quad_ham_npc.chemical_potential)
Example 9
Project: Computable Author: ktraunmueller File: test_index_tricks.py License: MIT License | 6 votes |
def test_ndindex(): x = list(np.ndindex(1, 2, 3)) expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))] assert_array_equal(x, expected) x = list(np.ndindex((1, 2, 3))) assert_array_equal(x, expected) # Test use of scalars and tuples x = list(np.ndindex((3,))) assert_array_equal(x, list(np.ndindex(3))) # Make sure size argument is optional x = list(np.ndindex()) assert_equal(x, [()]) x = list(np.ndindex(())) assert_equal(x, [()]) # Make sure 0-sized ndindex works correctly x = list(np.ndindex(*[0])) assert_equal(x, [])
Example 10
Project: tfdeploy Author: riga File: tfdeploy.py License: MIT License | 6 votes |
def _pool_patches(a, k, strides, padding): f = np.ones(k[1:] + [a.shape[-1]]) out_shape, src = _prepare_patches(a, f, strides, padding, "edge") patches = np.empty(tuple(out_shape) + f.shape).astype(a.dtype) s = (slice(None),) e = (Ellipsis,) en = (Ellipsis, np.newaxis) for coord in np.ndindex(*out_shape[1:]): pos = np.array(strides[1:]) * coord patches[s + coord + e] = \ src[s + tuple(slice(*tpl) for tpl in zip(pos, pos + f.shape[:-1]))][en] * f return patches
Example 11
Project: chainer Author: chainer File: test_softmax_cross_entropy.py License: MIT License | 6 votes |
def expected_forward_without_reduce(self, x_data, t_data, class_weight): x = numpy.rollaxis(x_data, 1, x_data.ndim).reshape( (t_data.size, x_data.shape[1])) t = t_data.ravel() loss_shape = x_data.shape[0:1] + x_data.shape[2:] loss_expect = numpy.zeros(loss_shape, x_data.dtype) for i, (ti, loss_idx) in enumerate(zip(t, numpy.ndindex(*loss_shape))): xi = x[i] if ti == -1: continue log_z = numpy.ufunc.reduce(numpy.logaddexp, xi) if class_weight is None: loss_expect[loss_idx] = -(xi - log_z)[ti] else: loss_expect[loss_idx] = -(xi - log_z)[ti] * class_weight[ti] return numpy.asarray(loss_expect, dtype=x.dtype)
Example 12
Project: chainer Author: chainer File: test_upsampling_2d.py License: MIT License | 6 votes |
def check_forward(self, y): y = F.upsampling_2d( self.pooled_y, self.indices, ksize=self.ksize, stride=self.stride, outsize=self.in_shape[2:]) if isinstance(y.array, numpy.ndarray): y = conv.im2col_cpu( y.array, self.ksize, self.ksize, self.stride, self.stride, 0, 0) else: y = conv.im2col_gpu( y.array, self.ksize, self.ksize, self.stride, self.stride, 0, 0) for i in numpy.ndindex(y.shape): n, c, ky, kx, oy, ox = i up_y = y[n, c, ky, kx, oy, ox] if ky * y.shape[3] + kx == self.indices[n, c, oy, ox]: in_y = self.pooled_y.array[n, c, oy, ox] testing.assert_allclose(in_y, up_y) else: testing.assert_allclose(up_y, 0)
Example 13
Project: training_results_v0.5 Author: mlperf File: ngram.py License: Apache License 2.0 | 6 votes |
def call(self, inputs): batch_shape = tf.shape(inputs)[:-1] length = tf.shape(inputs)[-1] ngram_range_counts = [] for n in range(self.minval, self.maxval): # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping # remainder elements. Each n-vector is an ngram. reshaped_inputs = tf.reshape( inputs[..., :(n * (length // n))], tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0)) # Count the number of times each ngram appears in the input. We do so by # checking whether each n-vector in the input is equal to each n-vector # in a Tensor of all possible ngrams. The comparison is batched between # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor # of shape [..., input_dim**n, 1, n]. ngrams = tf.reshape( list(np.ndindex((self.input_dim,) * n)), [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n]) is_ngram = tf.equal( tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32), axis=-1), n) ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1) ngram_range_counts.append(ngram_counts) return tf.concat(ngram_range_counts, axis=-1)
Example 14
Project: mne-features Author: mne-tools File: test_utils.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_idxiter(): n_channels = data.shape[0] # Upper-triangular part, including diag idx0, idx1 = np.triu_indices(n_channels) triu_indices = np.array([np.arange(idx0.size), idx0, idx1]) triu_indices2 = np.array(list(_idxiter(n_channels, include_diag=True))) # Upper-triangular part, without diag idx2, idx3 = np.triu_indices(n_channels, 1) triu_indices_nodiag = np.array([np.arange(idx2.size), idx2, idx3]) triu_indices2_nodiag = np.array(list(_idxiter(n_channels, include_diag=False))) assert_almost_equal(triu_indices, triu_indices2.transpose()) assert_almost_equal(triu_indices_nodiag, triu_indices2_nodiag.transpose()) # Upper and lower-triangular parts, without diag expected = [(i, j) for _, (i, j) in enumerate(np.ndindex((n_channels, n_channels))) if i != j] assert_equal(np.array([(i, j) for _, i, j in _idxiter(n_channels, triu=False)]), expected)
Example 15
Project: symengine.py Author: symengine File: test_lambdify.py License: MIT License | 6 votes |
def _get_Ndim_args_exprs_funcs(order): args = x, y = se.symbols('x y') # Higher dimensional inputs def f_a(index, _x, _y): a, b, c, d = index return _x**a + _y**b + (_x+_y)**-d nd_exprs_a = np.zeros((3, 5, 1, 4), dtype=object, order=order) for index in np.ndindex(*nd_exprs_a.shape): nd_exprs_a[index] = f_a(index, x, y) def f_b(index, _x, _y): a, b, c = index return b/(_x + _y) nd_exprs_b = np.zeros((1, 7, 1), dtype=object, order=order) for index in np.ndindex(*nd_exprs_b.shape): nd_exprs_b[index] = f_b(index, x, y) return args, nd_exprs_a, nd_exprs_b, f_a, f_b
Example 16
Project: symengine.py Author: symengine File: test_lambdify.py License: MIT License | 6 votes |
def test_Lambdify_Ndimensional_order_C(): args, nd_exprs_a, nd_exprs_b, f_a, f_b = _get_Ndim_args_exprs_funcs(order='C') lmb4 = se.Lambdify(args, nd_exprs_a, nd_exprs_b, order='C') nargs = len(args) inp_extra_shape = (3, 5, 4) inp_shape = inp_extra_shape + (nargs,) inp4 = np.arange(reduce(mul, inp_shape)*1.0).reshape(inp_shape, order='C') out4a, out4b = lmb4(inp4) assert out4a.ndim == 7 assert out4a.shape == inp_extra_shape + nd_exprs_a.shape assert out4b.ndim == 6 assert out4b.shape == inp_extra_shape + nd_exprs_b.shape raises(ValueError, lambda: (lmb4(inp4.T))) for b, c, d in np.ndindex(inp_extra_shape): _x, _y = inp4[b, c, d, :] for index in np.ndindex(*nd_exprs_a.shape): assert np.isclose(out4a[(b, c, d) + index], f_a(index, _x, _y)) for index in np.ndindex(*nd_exprs_b.shape): assert np.isclose(out4b[(b, c, d) + index], f_b(index, _x, _y))
Example 17
Project: symengine.py Author: symengine File: test_lambdify.py License: MIT License | 6 votes |
def test_Lambdify_Ndimensional_order_F(): args, nd_exprs_a, nd_exprs_b, f_a, f_b = _get_Ndim_args_exprs_funcs(order='F') lmb4 = se.Lambdify(args, nd_exprs_a, nd_exprs_b, order='F') nargs = len(args) inp_extra_shape = (3, 5, 4) inp_shape = (nargs,)+inp_extra_shape inp4 = np.arange(reduce(mul, inp_shape)*1.0).reshape(inp_shape, order='F') out4a, out4b = lmb4(inp4) assert out4a.ndim == 7 assert out4a.shape == nd_exprs_a.shape + inp_extra_shape assert out4b.ndim == 6 assert out4b.shape == nd_exprs_b.shape + inp_extra_shape raises(ValueError, lambda: (lmb4(inp4.T))) for b, c, d in np.ndindex(inp_extra_shape): _x, _y = inp4[:, b, c, d] for index in np.ndindex(*nd_exprs_a.shape): assert np.isclose(out4a[index + (b, c, d)], f_a(index, _x, _y)) for index in np.ndindex(*nd_exprs_b.shape): assert np.isclose(out4b[index + (b, c, d)], f_b(index, _x, _y))
Example 18
Project: recruit Author: Frank-qlu File: test_shape_base.py License: Apache License 2.0 | 5 votes |
def test_with_iterable_object(self): # from issue 5248 d = np.array([ [{1, 11}, {2, 22}, {3, 33}], [{4, 44}, {5, 55}, {6, 66}] ]) actual = np.apply_along_axis(lambda a: set.union(*a), 0, d) expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}]) assert_equal(actual, expected) # issue 8642 - assert_equal doesn't detect this! for i in np.ndindex(actual.shape): assert_equal(type(actual[i]), type(expected[i]))
Example 19
Project: python-netsurv Author: sofia-netsurv File: python_api.py License: MIT License | 5 votes |
def _yield_comparisons(self, actual): import numpy as np # `actual` can either be a numpy array or a scalar, it is treated in # `__eq__` before being passed to `ApproxBase.__eq__`, which is the # only method that calls this one. if np.isscalar(actual): for i in np.ndindex(self.expected.shape): yield actual, self.expected[i].item() else: for i in np.ndindex(self.expected.shape): yield actual[i].item(), self.expected[i].item()
Example 20
Project: python-netsurv Author: sofia-netsurv File: python_api.py License: MIT License | 5 votes |
def _yield_comparisons(self, actual): import numpy as np # `actual` can either be a numpy array or a scalar, it is treated in # `__eq__` before being passed to `ApproxBase.__eq__`, which is the # only method that calls this one. if np.isscalar(actual): for i in np.ndindex(self.expected.shape): yield actual, self.expected[i].item() else: for i in np.ndindex(self.expected.shape): yield actual[i].item(), self.expected[i].item()
Example 21
Project: rlgraph Author: rlgraph File: test_dqn_agent_functionality.py License: Apache License 2.0 | 5 votes |
def _helper_update_matrix(self, expected_batch, matrix1_qnet, matrix2_qnet, matrix1_target_net, matrix2_target_net, agent, loss_func): # Calculate gradient per weight based on the above batch. q_s = self._helper_get_q_values(expected_batch["states"], matrix1_qnet, matrix2_qnet) q_sp = self._helper_get_q_values(expected_batch["next_states"], matrix1_qnet, matrix2_qnet) qt_sp = self._helper_get_q_values(expected_batch["next_states"], matrix1_target_net, matrix2_target_net) # The loss without weight changes. loss = np.mean(loss_func._graph_fn_loss_per_item( q_s, expected_batch["actions"], expected_batch["rewards"], expected_batch["terminals"], qt_sp, q_sp )) # Calculate the dLoss/dw for all individual weights (w) and apply [- LR * dLoss/dw] to each weight. # Then check again against the actual, now optimized weights. mat_updated = list() for i, mat in enumerate([matrix1_qnet, matrix2_qnet]): mat_updated.append(mat.copy()) for index in np.ndindex(mat.shape): mat_w_plus_d = mat.copy() mat_w_plus_d[index] += 0.0001 if i == 0: q_s_plus_d = self._helper_get_q_values(expected_batch["states"], mat_w_plus_d, matrix2_qnet) q_sp_plus_d = self._helper_get_q_values(expected_batch["next_states"], mat_w_plus_d, matrix2_qnet) else: q_s_plus_d = self._helper_get_q_values(expected_batch["states"], matrix1_qnet, mat_w_plus_d) q_sp_plus_d = self._helper_get_q_values(expected_batch["next_states"], matrix1_qnet, mat_w_plus_d) loss_w_plus_d = np.mean(loss_func._graph_fn_loss_per_item( q_s_plus_d, expected_batch["actions"], expected_batch["rewards"], expected_batch["terminals"], qt_sp, q_sp_plus_d )) dl_over_dw = (loss - loss_w_plus_d) / 0.0001 # Apply the changes to our matrices, then check their actual values. mat_updated[i][index] += agent.optimizer.learning_rate * dl_over_dw return mat_updated
Example 22
Project: OpenCV-Python-Tutorial Author: makelove File: watershed.py License: MIT License | 5 votes |
def __init__(self, fn): self.img = cv2.imread(fn) if self.img is None: raise Exception('Failed to load image file: %s' % fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255 self.auto_update = True self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)
Example 23
Project: OpenCV-Python-Tutorial Author: makelove File: digits_adjust.py License: MIT License | 5 votes |
def adjust_SVM(self): Cs = np.logspace(0, 10, 15, base=2) gammas = np.logspace(-7, 4, 15, base=2) scores = np.zeros((len(Cs), len(gammas))) scores[:] = np.nan print('adjusting SVM (may take a long time) ...') def f(job): i, j = job samples, labels = self.get_dataset() params = dict(C = Cs[i], gamma=gammas[j]) score = cross_validate(SVM, params, samples, labels) return i, j, score ires = self.run_jobs(f, np.ndindex(*scores.shape)) for count, (i, j, score) in enumerate(ires): scores[i, j] = score print('%d / %d (best error: %.2f %%, last: %.2f %%)' % (count+1, scores.size, np.nanmin(scores)*100, score*100)) print(scores) print('writing score table to "svm_scores.npz"') np.savez('svm_scores.npz', scores=scores, Cs=Cs, gammas=gammas) i, j = np.unravel_index(scores.argmin(), scores.shape) best_params = dict(C = Cs[i], gamma=gammas[j]) print('best params:', best_params) print('best error: %.2f %%' % (scores.min()*100)) return best_params
Example 24
Project: D-VAE Author: muhanzhang File: test_pool.py License: MIT License | 5 votes |
def numpy_max_pool_2d(input, ds, ignore_border=False, mode='max'): '''Helper function, implementing pool_2d in pure numpy''' if len(input.shape) < 2: raise NotImplementedError('input should have at least 2 dim,' ' shape is %s' % str(input.shape)) xi = 0 yi = 0 if not ignore_border: if input.shape[-2] % ds[0]: xi += 1 if input.shape[-1] % ds[1]: yi += 1 out_shp = list(input.shape[:-2]) out_shp.append(input.shape[-2] / ds[0] + xi) out_shp.append(input.shape[-1] / ds[1] + yi) output_val = numpy.zeros(out_shp) func = numpy.max if mode == 'sum': func = numpy.sum elif mode != 'max': func = numpy.average for k in numpy.ndindex(*input.shape[:-2]): for i in range(output_val.shape[-2]): ii = i * ds[0] for j in range(output_val.shape[-1]): jj = j * ds[1] patch = input[k][ii:ii + ds[0], jj:jj + ds[1]] output_val[k][i, j] = func(patch) return output_val
Example 25
Project: D-VAE Author: muhanzhang File: raw_random.py License: MIT License | 5 votes |
def permutation_helper(random_state, n, shape): """ Helper function to generate permutations from integers. permutation_helper(random_state, n, (1,)) will generate a permutation of integers 0..n-1. In general, it will generate as many such permutation as required by shape. For instance, if shape=(p,q), p*q permutations will be generated, and the output shape will be (p,q,n), because each permutation is of size n. If you wish to perform a permutation of the elements of an existing vector, see shuffle_row_elements. This is a generalization of numpy.random.permutation to tensors. Otherwise it behaves the same. """ # n should be a 0-dimension array assert n.shape == () # Note that it is important to convert `n` into an integer, because if it # is a long, the numpy permutation function will crash on Windows. n = int(n.item()) if shape is None: # Draw only one permutation, equivalent to shape = () shape = () out_shape = list(shape) out_shape.append(n) out = numpy.empty(out_shape, int) for i in numpy.ndindex(*shape): out[i] = random_state.permutation(n) # print 'RETURNING', out.shape return out
Example 26
Project: vnpy_crypto Author: birforce File: test_shape_base.py License: MIT License | 5 votes |
def test_with_iterable_object(self): # from issue 5248 d = np.array([ [set([1, 11]), set([2, 22]), set([3, 33])], [set([4, 44]), set([5, 55]), set([6, 66])] ]) actual = np.apply_along_axis(lambda a: set.union(*a), 0, d) expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}]) assert_equal(actual, expected) # issue 8642 - assert_equal doesn't detect this! for i in np.ndindex(actual.shape): assert_equal(type(actual[i]), type(expected[i]))
Example 27
Project: gordo Author: equinor File: test_filter_rows.py License: GNU Affero General Public License v3.0 | 5 votes |
def test_filter_rows_basic(): df = pd.DataFrame(list(np.ndindex((10, 2))), columns=["Tag 1", "Tag 2"]) assert len(pandas_filter_rows(df, "`Tag 1` <= `Tag 2`")) == 3 assert len(pandas_filter_rows(df, "`Tag 1` == `Tag 2`")) == 2 assert len(pandas_filter_rows(df, "(`Tag 1` <= `Tag 2`) | (`Tag 2` < 2)")) == 20 assert len(pandas_filter_rows(df, "(`Tag 1` <= `Tag 2`) | (`Tag 2` < 0.9)")) == 12 assert len(pandas_filter_rows(df, "(`Tag 1` > 0) & (`Tag 2` > 0)")) == 9 assert len(pandas_filter_rows(df, ["`Tag 1` > 0", "`Tag 2` > 0"])) == 9 assert_frame_equal( pandas_filter_rows(df, "(`Tag 1` <= `Tag 2`)"), pandas_filter_rows(df, "~(`Tag 1` > `Tag 2`)"), )
Example 28
Project: gordo Author: equinor File: test_filter_rows.py License: GNU Affero General Public License v3.0 | 5 votes |
def test_filter_rows_catches_illegal(): df = pd.DataFrame(list(np.ndindex((10, 2))), columns=["Tag 1", "Tag 2"]) with pytest.raises(UndefinedVariableError): pandas_filter_rows(df, "sys.exit(0)") with pytest.raises(NotImplementedError): pandas_filter_rows(df, "lambda x:x") with pytest.raises(ValueError): pandas_filter_rows(df, "__import__('os').system('clear')"), ValueError
Example 29
Project: OpenFermion Author: quantumlib File: _quadratic_hamiltonian_test.py License: Apache License 2.0 | 5 votes |
def test_combined_hermitian_part(self): """Test getting the combined Hermitian part.""" combined_hermitian_part = self.quad_ham_pc.combined_hermitian_part for i in numpy.ndindex(combined_hermitian_part.shape): self.assertAlmostEqual(self.hermitian_mat[i], combined_hermitian_part[i]) combined_hermitian_part = self.quad_ham_npc.combined_hermitian_part for i in numpy.ndindex(combined_hermitian_part.shape): self.assertAlmostEqual(self.combined_hermitian[i], combined_hermitian_part[i])
Example 30
Project: OpenFermion Author: quantumlib File: _quadratic_hamiltonian_test.py License: Apache License 2.0 | 5 votes |
def test_hermitian_part(self): """Test getting the Hermitian part.""" hermitian_part = self.quad_ham_pc.hermitian_part for i in numpy.ndindex(hermitian_part.shape): self.assertAlmostEqual(self.hermitian_mat[i], hermitian_part[i]) hermitian_part = self.quad_ham_npc.hermitian_part for i in numpy.ndindex(hermitian_part.shape): self.assertAlmostEqual(self.hermitian_mat[i], hermitian_part[i])