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)
python类ndindex()的实例源码
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
test_local_response_normalization.py 文件源码
项目:chainer-deconv
作者: germanRos
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
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 __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, 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 __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 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
testWsDtSegmentation.py 文件源码
项目:nature_methods_multicut_pipeline
作者: ilastik
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
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 )
testWsDtSegmentation.py 文件源码
项目:nature_methods_multicut_pipeline
作者: ilastik
项目源码
文件源码
阅读 38
收藏 0
点赞 0
评论 0
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__(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 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)