我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.ndindex()。
def _compare_xlsx(self, file1, file2, rtol=1e-02, atol=1e-03): # print("requested compare: {} and {}".format(file1, file2)) xl1 = pd.ExcelFile(file1) xl2 = pd.ExcelFile(file2) self.assertEqual(xl1.sheet_names, xl2.sheet_names) for sheet in xl1.sheet_names: # print("Prrocessing sheet {}".format(sheet)) df1 = xl1.parse(sheet) df2 = xl2.parse(sheet) columns1 = list(df1) columns2 = list(df2) self.assertEqual(len(columns1), len(columns2)) arr1 = df1.values arr2 = df2.values self.assertEqual(arr1.shape, arr2.shape) for x, y in np.ndindex(arr1.shape): v1 = arr1[x, y] v2 = arr2[x, y] # print("{}: ({}, {}): {} vs {}".format(sheet, x, y, v1, v2)) if isinstance(v1, six.string_types) or isinstance(v2, six.string_types): self.assertEqual(v1, v2) else: npt.assert_allclose(v1, v2, rtol=rtol, atol=atol)
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
def check_forward(self, y): y = upsampling_2d.upsampling_2d( self.pooled_y, self.p.indexes, ksize=(self.p.kh, self.p.kw), stride=(self.p.sy, self.p.sx), pad=(self.p.ph, self.p.pw), outsize=self.in_shape[2:], cover_all=self.p.cover_all) if isinstance(y.data, numpy.ndarray): y = conv.im2col_cpu(y.data, self.p.kh, self.p.kw, self.p.sy, self.p.sx, self.p.ph, self.p.pw) else: y = conv.im2col_gpu(y.data, self.p.kh, self.p.kw, self.p.sy, self.p.sx, self.p.ph, self.p.pw) 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.p.indexes[n, c, oy, ox]: in_y = self.pooled_y.data[n, c, oy, ox] testing.assert_allclose(in_y, up_y) else: testing.assert_allclose(up_y, 0)
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
def explicit_score(sess, model, dist, data, tf_X): logprobs = 0 squared_err = 0 indices = np.array(list(np.ndindex(dist._num_classes))) n = 0 for X, y in data: for i in xrange(len(X)): feed_dict = test_dict(model, dist, X[i:i+1], y[i:i+1]) feed_dict[tf_X] = X[i:i+1] density = sess.run(dist.density, feed_dict=feed_dict)[0] logprobs += np.log(density[tuple(y[i])]) prediction = np.array([density[tuple(idx)] * idx for idx in indices]).sum(axis=0) squared_err += np.linalg.norm(y[i] - prediction)**2 n += 1 rmse = np.sqrt(squared_err / float(n)) print 'Explicit logprobs: {0} RMSE: {1}'.format(logprobs, rmse) return logprobs, rmse
def check_forward(self, x_data): x = chainer.Variable(x_data) y = functions.local_response_normalization(x) self.assertEqual(y.data.dtype, self.dtype) y_data = cuda.to_cpu(y.data) # Naive implementation y_expect = numpy.zeros_like(self.x) for n, c, h, w in numpy.ndindex(self.x.shape): s = 0 for i in six.moves.range(max(0, c - 2), min(7, c + 2)): s += self.x[n, i, h, w] ** 2 denom = (2 + 1e-4 * s) ** .75 y_expect[n, c, h, w] = self.x[n, c, h, w] / denom gradient_check.assert_allclose( y_expect, y_data, **self.check_forward_optionss)
def check_forward(self, x_data, t_data, v_data, use_visibility): x = chainer.Variable(x_data) t = chainer.Variable(t_data) v = chainer.Variable(v_data) loss = mean_squared_error(x, t, v, use_visibility) loss_value = cuda.to_cpu(loss.data) eq_(loss_value.dtype, np.float32) eq_(loss_value.shape, ()) # compute expected value. loss_expect = 0. for i in np.ndindex(self.x.shape): diff = self.x[i] - self.t[i] if use_visibility: diff *= self.v[i[:-1]] loss_expect += diff**2 if use_visibility: N = self.v.sum()/2 else: N = self.x.size/2 loss_expect /= N self.assertAlmostEqual(loss_expect, loss_value, places=5)
def run_test(G, expect): N = G.shape[0] clofunc = partial(clo.epclosuress, G, retpaths=True) o, p = zip(*map(clofunc, xrange(N))) o = np.round(o, 2) # check capacities match assert np.allclose(o, expect) # check paths match with computed capacities for s, t in np.ndindex(G.shape): if (s == t) or G[s, t] > 0 or (o[s, t] == 0): # path must be empty assert len(p[s][t]) == 0 else: # minimum on path must correspond to computed capacity path = p[s][t] weights = np.ravel(G[path[:-1], path[1:]])[:-1] weights = np.round(weights, 2) assert o[s, t] == np.min(weights)
def dot(a, b): if a.ndim != 2: raise Exception("dot expects its arguments to be 2-dimensional, but " "a.ndim = {}.".format(a.ndim)) if b.ndim != 2: raise Exception("dot expects its arguments to be 2-dimensional, but " "b.ndim = {}.".format(b.ndim)) if a.shape[1] != b.shape[0]: raise Exception("dot expects a.shape[1] to equal b.shape[0], but " "a.shape = {} and b.shape = {}.".format(a.shape, b.shape)) shape = [a.shape[0], b.shape[1]] result = DistArray(shape) for (i, j) in np.ndindex(*result.num_blocks): args = list(a.objectids[i, :]) + list(b.objectids[:, j]) result.objectids[i, j] = blockwise_dot.remote(*args) return result
def __init__(self, dimensions=(3, 4), start_state=(0, 0), end_states=[(0, 3), (1, 3)], nonstates = [(1, 1)], state_rewards = {(0, 3): 1, (1, 3): -1}, step_reward = -0.1, max_steps = 100): self.dimensions = dimensions self.states = set(np.ndindex(dimensions)).difference(set(nonstates)) self.start_state = start_state self.end_states = end_states self.state_rewards = state_rewards self.step_reward = step_reward self.max_steps = max_steps assert self.is_state(self.start_state) assert all(self.is_state(state) for state in self.end_states) assert all(not self.is_state(state) for state in nonstates) self.start_episode()
def __init__(self, dimensions=(4, 12), start_state=(3, 0), goal_state=(3, 11), cliff_states = [(3, x) for x in xrange(1, 11)], cliff_reward = -100, step_reward = -1, max_steps = 100): self.dimensions = dimensions self.states = set(np.ndindex(dimensions)) self.start_state = start_state self.goal_state = goal_state self.cliff_states = cliff_states self.cliff_reward = cliff_reward self.step_reward = step_reward self.max_steps = max_steps assert self.is_state(self.start_state) assert self.is_state(self.goal_state) assert all(self.is_state(state) for state in self.cliff_states) assert self.start_state not in self.cliff_states self.start_episode()
def __new__(cls, array): array = asarray(array) assert numpy.prod(array.shape) # Handle children with shape child_shape = array.flat[0].shape assert all(elem.shape == child_shape for elem in array.flat) if child_shape: # Destroy structure direct_array = numpy.empty(array.shape + child_shape, dtype=object) for alpha in numpy.ndindex(array.shape): for beta in numpy.ndindex(child_shape): direct_array[alpha + beta] = Indexed(array[alpha], beta) array = direct_array # Constant folding if all(isinstance(elem, Constant) for elem in array.flat): return Literal(numpy.vectorize(attrgetter('value'))(array)) self = super(ListTensor, cls).__new__(cls) self.array = array return self
def test_simple_3D(self): pmap = self._gen_input_data(3) debug_results = {} ws_output = wsDtSegmentation(pmap, 0.5, 0, 10, 0.1, 0.1, groupSeeds=False, out_debug_image_dict=debug_results) seeds = debug_results['seeds'][:] assert seeds.max() == 8 assert ws_output.max() == 8 # Expect seeds at (25,25,25), (25,25,75), (25,75,25), etc... expected_seed_coords = list(np.ndindex((2,2,2))) expected_seed_coords = 50*np.array(expected_seed_coords) + 25 #print "EXPECTED:\n", expected_seed_coords #print "SEEDS:\n", np.array(np.where(seeds)).transpose() for seed_coord in expected_seed_coords: assert seeds[tuple(seed_coord)], "No seed at: {}".format( seed_coord )
def test_simple_2D(self): pmap = self._gen_input_data(2) debug_results = {} ws_output = wsDtSegmentation(pmap, 0.5, 0, 10, 0.1, 0.1, groupSeeds=False, out_debug_image_dict=debug_results) seeds = debug_results['seeds'][:] assert seeds.max() == 4 assert ws_output.max() == 4 # Expect seeds at (25,25,25), (25,25,75), (25,75,25), etc... expected_seed_coords = list(np.ndindex((2,2))) expected_seed_coords = 50*np.array(expected_seed_coords) + 25 #print "EXPECTED:\n", expected_seed_coords #print "SEEDS:\n", np.array(np.where(seeds)).transpose() for seed_coord in expected_seed_coords: assert seeds[tuple(seed_coord)], "No seed at: {}".format( seed_coord )
def init_material_ex(self): """Set up the update mechanism for Ex field. Set up the update mechanism for Ex field and stores the result at self.pw_material[Ex]. """ self.pw_material[Ex] = {} shape = self.ex.shape for idx in ndindex(shape): spc = self.space.ex_index_to_space(*idx) mat_obj, underneath = self.geom_tree.material_of_point(spc) if idx[1] == shape[1] - 1 or idx[2] == shape[2] - 1: mat_obj = Dummy(mat_obj.eps_inf, mat_obj.mu_inf) pw_obj = mat_obj.get_pw_material_ex(idx, spc, underneath, self.cmplx) if self.pw_material[Ex].has_key(type(pw_obj)): self.pw_material[Ex][type(pw_obj)].merge(pw_obj) else: self.pw_material[Ex][type(pw_obj)] = pw_obj
def init_material_ey(self): """Set up the update mechanism for Ey field. Set up the update mechanism for Ey field and stores the result at self.pw_material[Ey]. """ self.pw_material[Ey] = {} shape = self.ey.shape for idx in ndindex(shape): spc = self.space.ey_index_to_space(*idx) mat_obj, underneath = self.geom_tree.material_of_point(spc) if idx[2] == shape[2] - 1 or idx[0] == shape[0] - 1: mat_obj = Dummy(mat_obj.eps_inf, mat_obj.mu_inf) pw_obj = mat_obj.get_pw_material_ey(idx, spc, underneath, self.cmplx) if self.pw_material[Ey].has_key(type(pw_obj)): self.pw_material[Ey][type(pw_obj)].merge(pw_obj) else: self.pw_material[Ey][type(pw_obj)] = pw_obj
def init_material_ez(self): """Set up the update mechanism for Ez field. Set up the update mechanism for Ez field and stores the result at self.pw_material[Ez]. """ self.pw_material[Ez] = {} shape = self.ez.shape for idx in ndindex(shape): spc = self.space.ez_index_to_space(*idx) mat_obj, underneath = self.geom_tree.material_of_point(spc) if idx[0] == shape[0] - 1 or idx[1] == shape[1] - 1: mat_obj = Dummy(mat_obj.eps_inf, mat_obj.mu_inf) pw_obj = mat_obj.get_pw_material_ez(idx, spc, underneath, self.cmplx) if self.pw_material[Ez].has_key(type(pw_obj)): self.pw_material[Ez][type(pw_obj)].merge(pw_obj) else: self.pw_material[Ez][type(pw_obj)] = pw_obj
def init_material_hx(self): """Set up the update mechanism for Hx field. Set up the update mechanism for Hx field and stores the result at self.pw_material[Hx]. """ self.pw_material[Hx] = {} shape = self.hx.shape for idx in ndindex(shape): spc = self.space.hx_index_to_space(*idx) mat_obj, underneath = self.geom_tree.material_of_point(spc) if idx[1] == 0 or idx[2] == 0: mat_obj = Dummy(mat_obj.eps_inf, mat_obj.mu_inf) pw_obj = mat_obj.get_pw_material_hx(idx, spc, underneath, self.cmplx) if self.pw_material[Hx].has_key(type(pw_obj)): self.pw_material[Hx][type(pw_obj)].merge(pw_obj) else: self.pw_material[Hx][type(pw_obj)] = pw_obj
def init_material_hy(self): """Set up the update mechanism for Hy field. Set up the update mechanism for Hy field and stores the result at self.pw_material[Hy]. """ self.pw_material[Hy] = {} shape = self.hy.shape for idx in ndindex(shape): spc = self.space.hy_index_to_space(*idx) mat_obj, underneath = self.geom_tree.material_of_point(spc) if idx[2] == 0 or idx[0] == 0: mat_obj = Dummy(mat_obj.eps_inf, mat_obj.mu_inf) pw_obj = mat_obj.get_pw_material_hy(idx, spc, underneath, self.cmplx) if self.pw_material[Hy].has_key(type(pw_obj)): self.pw_material[Hy][type(pw_obj)].merge(pw_obj) else: self.pw_material[Hy][type(pw_obj)] = pw_obj
def testEyCmplx(self): sample = \ self.cpml.get_pw_material_ey(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.cpml.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ey = hx = hz = np.zeros((3,3,3), complex) dz = dx = dt = 1 n = 0 sample.update_all(ey, hx, hz, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ey[idx], 0j)
def testEzCmplx(self): sample = \ self.cpml.get_pw_material_ez(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.cpml.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ez = hy = hx = np.zeros((3,3,3), complex) dx = dy = dt = 1 n = 0 sample.update_all(ez, hy, hx, dx, dy, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ez[idx], 0j)
def testHxCmplx(self): sample = \ self.cpml.get_pw_material_hx(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.cpml.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hx = ez = ey = np.zeros((3,3,3), complex) dy = dz = dt = 1 n = 0 sample.update_all(hx, ez, ey, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(hx[idx], 0j)
def testHyCmplx(self): sample = \ self.cpml.get_pw_material_hy(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.cpml.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hy = ex = ez = np.zeros((3,3,3), complex) dz = dx = dt = 1 n = 0 sample.update_all(hy, ex, ez, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(hy[idx], 0j)
def testExCmplx(self): sample = \ self.meep.get_pw_material_ex(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.meep.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ex = hz = hy = np.zeros((3,3,3), complex) dy = dz = dt = 1 n = 0 sample.update_all(ex, hz, hy, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ex[idx], 0j)
def testEyCmplx(self): sample = \ self.meep.get_pw_material_ey(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.meep.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ey = hx = hz = np.zeros((3,3,3), complex) dz = dx = dt = 1 n = 0 sample.update_all(ey, hx, hz, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ey[idx], 0j)
def testEzCmplx(self): sample = \ self.meep.get_pw_material_ez(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.meep.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ez = hy = hx = np.zeros((3,3,3), complex) dx = dy = dt = 1 n = 0 sample.update_all(ez, hy, hx, dx, dy, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ez[idx], 0j)
def testHxCmplx(self): sample = \ self.meep.get_pw_material_hx(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.meep.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hx = ez = ey = np.zeros((3,3,3), complex) dy = dz = dt = 1 n = 0 sample.update_all(hx, ez, ey, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(hx[idx], 0j)
def testHyCmplx(self): sample = \ self.meep.get_pw_material_hy(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.meep.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hy = ex = ez = np.zeros((3,3,3), complex) dz = dx = dt = 1 n = 0 sample.update_all(hy, ex, ez, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(hy[idx], 0j)
def testExCmplx(self): sample = \ self.upml.get_pw_material_ex(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.upml.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ex = hz = hy = np.zeros((3,3,3), complex) dy = dz = dt = 1 n = 0 sample.update_all(ex, hz, hy, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ex[idx], 0j)
def testEyCmplx(self): sample = \ self.upml.get_pw_material_ey(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.upml.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ey = hx = hz = np.zeros((3,3,3), complex) dz = dx = dt = 1 n = 0 sample.update_all(ey, hx, hz, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ey[idx], 0j)
def testEzCmplx(self): sample = \ self.upml.get_pw_material_ez(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.upml.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ez = hy = hx = np.zeros((3,3,3), complex) dx = dy = dt = 1 n = 0 sample.update_all(ez, hy, hx, dx, dy, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(ez[idx], 0j)
def testHxCmplx(self): sample = \ self.upml.get_pw_material_hx(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.upml.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hx = ez = ey = np.zeros((3,3,3), complex) dy = dz = dt = 1 n = 0 sample.update_all(hx, ez, ey, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(hx[idx], 0j)
def testHyCmplx(self): sample = \ self.upml.get_pw_material_hy(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.upml.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hy = ex = ez = np.zeros((3,3,3), complex) dz = dx = dt = 1 n = 0 sample.update_all(hy, ex, ez, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): self.assertEqual(hy[idx], 0j)
def testExReal(self): sample = \ self.const_real.get_pw_material_ex(self.idx, (0,0,0), cmplx=False) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.const_real.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ex = hz = hy = np.zeros((3,3,3)) dy = dz = dt = self.spc.dt n = 0 sample.update_all(ex, hz, hy, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(ex[idx], self.const_real.value) else: self.assertEqual(ex[idx], 0)
def testEyReal(self): sample = \ self.const_real.get_pw_material_ey(self.idx, (0,0,0), cmplx=False) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.const_real.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ey = hx = hz = np.zeros((3,3,3)) dz = dx = dt = 1 n = 0 sample.update_all(ey, hx, hz, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(ey[idx], self.const_real.value) else: self.assertEqual(ey[idx], 0)
def testEzReal(self): sample = \ self.const_real.get_pw_material_ez(self.idx, (0,0,0), cmplx=False) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.const_real.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ez = hy = hx = np.zeros((3,3,3)) dx = dy = dt = 1 n = 0 sample.update_all(ez, hy, hx, dx, dy, dt, n) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(ez[idx], self.const_real.value) else: self.assertEqual(ez[idx], 0)
def testHxReal(self): sample = \ self.const_real.get_pw_material_hx(self.idx, (0,0,0), cmplx=False) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.const_real.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hx = ez = ey = np.zeros((3,3,3)) dy = dz = dt = 1 n = 0 sample.update_all(hx, ez, ey, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(hx[idx], self.const_real.value) else: self.assertEqual(hx[idx], 0)
def testHyReal(self): sample = \ self.const_real.get_pw_material_hy(self.idx, (0,0,0), cmplx=False) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_mu_inf(idx), self.const_real.mu_inf) else: self.assertEqual(sample.get_mu_inf(idx), 0) hy = ex = ez = np.zeros((3,3,3)) dz = dx = dt = 1 n = 0 sample.update_all(hy, ex, ez, dz, dx, dt, n) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(hy[idx], self.const_real.value) else: self.assertEqual(hy[idx], 0)
def testExCmplx(self): sample = \ self.const_cmplx.get_pw_material_ex(self.idx, (0,0,0), cmplx=True) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(sample.get_eps_inf(idx), self.const_cmplx.eps_inf) else: self.assertEqual(sample.get_eps_inf(idx), 0) ex = hz = hy = np.zeros((3,3,3), complex) dy = dz = dt = 1 n = 0 sample.update_all(ex, hz, hy, dy, dz, dt, n) for idx in np.ndindex(3, 3, 3): if idx == self.idx: self.assertEqual(ex[idx], self.const_cmplx.value) else: self.assertEqual(ex[idx], 0j)
def zero_pad(x, n): """ Return *x* zero padded to the dimensions specified in *n*. """ y = NP.zeros(n) for index in NP.ndindex(x.shape): y[index] = x[index] return y
def correlate_frames(self, method='gaussian'): """Correlation of all grid points, creating a velocity field. :param str method: Method of the peak finding algorithm """ for i, j in np.ndindex(self.grid_spec.get_grid_shape()): window_a, window_b = self._get_window_frames(i, j) displacement = (self._correlator.get_displacement(window_a, window_b, subpixel_method=method)) self.u[i, j] += displacement[0] self.v[i, j] += displacement[1] return self.u, self.v
def distances_numba_array(cluster): # Original: diff = cluster[:, np.newaxis, :] - cluster[np.newaxis, :, :] # Since np.newaxis is not supported, we use reshape to do this diff = (cluster.reshape(cluster.shape[0], 1, cluster.shape[1]) - cluster.reshape(1, cluster.shape[0], cluster.shape[1])) mat = (diff * diff) # Original: mat = mat.sum(-1) # Since axis argument is not supported, we write the loop out out = np.empty(mat.shape[:2], dtype=mat.dtype) for i in np.ndindex(out.shape): out[i] = mat[i].sum() return np.sqrt(out)
def slogdet(a): """Returns sign and logarithm of the determinat of an array. It calculates the natural logarithm of the deteminant of a given value. Args: a (cupy.ndarray): The input matrix with dimension ``(..., N, N)``. Returns: tuple of :class:`~cupy.ndarray`: It returns a tuple ``(sign, logdet)``. ``sign`` represents each sign of the deteminant as a real number ``0``, ``1`` or ``-1``. 'logdet' represents the natural logarithm of the absolute of the deteminant. If the deteninant is zero, ``sign`` will be ``0`` and ``logdet`` will be ``-inf``. The shapes of both ``sign`` and ``logdet`` are equal to ``a.shape[:-2]``. .. seealso:: :func:`numpy.linalg.slogdet` """ if not cuda.cusolver_enabled: raise RuntimeError('Current cupy only supports cusolver in CUDA 8.0') if a.ndim < 2: msg = ('%d-dimensional array given. ' 'Array must be at least two-dimensional' % a.ndim) raise linalg.LinAlgError(msg) dtype = numpy.find_common_type((a.dtype.char, 'f'), ()) shape = a.shape[:-2] sign = cupy.empty(shape, dtype) logdet = cupy.empty(shape, dtype) a = a.astype(dtype) for index in numpy.ndindex(*shape): s, l = _slogdet_one(a[index]) sign[index] = s logdet[index] = l return sign, logdet
def query_ball_point(self, x, r, p=2., eps=0): """Find all points within r of x Parameters ========== x : array_like, shape tuple + (self.m,) The point or points to search for neighbors of r : positive float The radius of points to return p : float 1<=p<=infinity Which Minkowski p-norm to use eps : nonnegative float Approximate search. Branches of the tree are not explored if their nearest points are further than r/(1+eps), and branches are added in bulk if their furthest points are nearer than r*(1+eps). Returns ======= results : list or array of lists If x is a single point, returns a list of the indices of the neighbors of x. If x is an array of points, returns an object array of shape tuple containing lists of neighbors. Note: if you have many points whose neighbors you want to find, you may save substantial amounts of time by putting them in a KDTree and using query_ball_tree. """ x = np.asarray(x) if x.shape[-1]!=self.m: raise ValueError("Searching for a %d-dimensional point in a %d-dimensional KDTree" % (x.shape[-1],self.m)) if len(x.shape)==1: return self.__query_ball_point(x,r,p,eps) else: retshape = x.shape[:-1] result = np.empty(retshape,dtype=np.object) for c in np.ndindex(retshape): result[c] = self.__query_ball_point(x[c], r, p=p, eps=eps) return result
def __getitem__ (self, key): import numpy as np # Coerce key into a tuple of slice objects. if not isinstance(key,tuple): if hasattr(key,'__len__'): key = tuple(key) else: key = (key,) if len(key) == 1 and hasattr(key[0],'__len__'): key = tuple(key[0]) if Ellipsis in key: i = key.index(Ellipsis) key = key[:i] + (slice(None),)*(self.ndim-len(key)+1) + key[i+1:] key = key + (slice(None),)*(self.ndim-len(key)) if len(key) > self.ndim: raise ValueError(("Too many dimensions for slicing.")) final_shape = tuple(len(np.arange(n)[k]) for n,k in zip(self.shape,key) if not isinstance(k,int)) data = np.ma.empty(final_shape, dtype=self.dtype) outer_ndim = self._record_id.ndim record_id = self._record_id.__getitem__(key[:outer_ndim]) # Final shape of each record (in case we did any reshaping along ni,nj,nk # dimensions). record_shape = self.shape[self._record_id.ndim:] # Iterate of each record. for ind in np.ndindex(record_id.shape): r = int(record_id[ind]) if r >= 0: data[ind] = self._buffer._fstluk(r)['d'].transpose().reshape(record_shape)[(Ellipsis,)+key[outer_ndim:]] else: data.mask = np.ma.getmaskarray(data) data.mask[ind] = True return data
def _calc(self, cars, nrb, ret_obj): # Assume that an nD nrb should be averaged to be 1D nrb = _mean_nd_to_1d(nrb) shp = cars.shape[0:-2] # Step row-by-row through image for idx in _np.ndindex(shp): if self.rng is None: kkd = _kkrelation(bg=nrb + self.nrb_amp_offset, cri=cars[idx] + self.cars_amp_offset, phase_offset=self.phase_offset, norm_by_bg=self.norm_to_nrb, pad_factor=self.pad_factor) else: kkd = _kkrelation(bg=nrb[self.rng] + self.nrb_amp_offset, cri=cars[idx][..., self.rng] + self.cars_amp_offset, phase_offset=self.phase_offset, norm_by_bg=self.norm_to_nrb, pad_factor=self.pad_factor) try: ret_obj[idx] *= 0 if self.rng is None: ret_obj[idx] += kkd elif ret_obj[idx].size == kkd.size: ret_obj[idx] += kkd else: ret_obj[idx][..., self.rng] += kkd except: return False else: pass return True
def _calc(self, data, ret_obj, **kwargs): self._inst_als = _AlsCvxopt(**kwargs) try: shp = data.shape[0:-2] total_num = _np.array(shp).prod() counter = 1 for idx in _np.ndindex(shp): print('Detrended iteration {} / {}'.format(counter, total_num)) ph = _np.unwrap(_np.angle(data[idx])) if self.rng is None: err_phase = self._inst_als.calculate(ph) else: err_phase = self._inst_als.calculate(ph[..., self.rng]) h = _np.zeros(err_phase.shape) h += _hilbert(err_phase) correction_factor = 1/_np.exp(h) * _np.exp(-1j*err_phase) if self.rng is None: ret_obj[idx] *= correction_factor else: ret_obj[idx][..., self.rng] *= correction_factor counter += 1 except: return False else: # print(self._inst_als.__dict__) return True