def pack_samples(self, samples, dtype=None):
"""Pack samples into one integer per sample
Store one sample in a single integer instead of a list of
integers with length `len(self.nsoutdims)`. Example:
>>> p = pauli_mpp(nr_sites=2, local_dim=2)
>>> p.outdims
(6, 6)
>>> p.pack_samples(np.array([[0, 1], [1, 0], [1, 2], [5, 5]]))
array([ 1, 6, 8, 35])
"""
assert samples.ndim == 2
assert samples.shape[1] == len(self.nsoutdims)
samples = np.ravel_multi_index(samples.T, self.nsoutdims)
if dtype not in (True, False, None) and issubclass(dtype, np.integer):
info = np.iinfo(dtype)
assert samples.min() >= info.min
assert samples.max() <= info.max
samples = samples.astype(dtype)
return samples
python类ravel_multi_index()的实例源码
def add_obstacle(self, x, y):
msg = str(x)+" "+str(y)
# Request a cell update
print("[INFO] Sending cell update request")
self.socket.send(b"update")
# Get the reply.
errors = False
if (self.socket.recv() != "go"):
print("[ERROR] Socket could not process update request.")
errors = True
self.socket.send(b"")
else:
self.socket.send(msg)
if (self.socket.recv() != "ok"):
print("[ERROR] Socket was not able to update given cell.")
errors = True
else:
index = np.ravel_multi_index((x, y), self.imsize, order='C')
print("[INFO] Updating new obstacle")
self.grid[index] = 0
return errors
def asarray(self, memmap=False, *args, **kwargs):
"""Read image data from all files and return as single numpy array.
If memmap is True, return an array stored in a binary file on disk.
The args and kwargs parameters are passed to the imread function.
Raise IndexError or ValueError if image shapes don't match.
"""
im = self.imread(self.files[0], *args, **kwargs)
shape = self.shape + im.shape
if memmap:
with tempfile.NamedTemporaryFile() as fh:
result = numpy.memmap(fh, dtype=im.dtype, shape=shape)
else:
result = numpy.zeros(shape, dtype=im.dtype)
result = result.reshape(-1, *im.shape)
for index, fname in zip(self._indices, self.files):
index = [i-j for i, j in zip(index, self._start_index)]
index = numpy.ravel_multi_index(index, self.shape)
im = self.imread(fname, *args, **kwargs)
result[index] = im
result.shape = shape
return result
def __init__(self):
self.shape = (4, 12)
nS = np.prod(self.shape)
nA = 4
# Cliff Location
self._cliff = np.zeros(self.shape, dtype=np.bool)
self._cliff[3, 1:-1] = True
# Calculate transition probabilities
P = {}
for s in range(nS):
position = np.unravel_index(s, self.shape)
P[s] = { a : [] for a in range(nA) }
P[s][UP] = self._calculate_transition_prob(position, [-1, 0])
P[s][RIGHT] = self._calculate_transition_prob(position, [0, 1])
P[s][DOWN] = self._calculate_transition_prob(position, [1, 0])
P[s][LEFT] = self._calculate_transition_prob(position, [0, -1])
# We always start in state (3, 0)
isd = np.zeros(nS)
isd[np.ravel_multi_index((3,0), self.shape)] = 1.0
super(CliffWalkingEnv, self).__init__(nS, nA, P, isd)
def histogram_xyt( x, y, t, binstep=100, detx=256, dety=256 ):
'''x: coordinate-x
y: coordinate-y
t: photon hit time
bin t in binstep (in t unit) period
'''
L= np.max( (t-t[0])//binstep ) + 1
arr = np.ravel_multi_index( [x, y, (t-t[0])//binstep ], [detx, dety,L ] )
M,N = arr.max(),arr.min()
da = np.zeros( [detx, dety, L ] )
da.flat[np.arange(N, M ) ] = np.bincount( arr- N )
return da
######################################################
def getdiag(x):
shp = x.shape
ndim = len(shp)
diag = np.zeros(shp)
if ndim == 1:
diag = 0
elif ndim == 2:
i = np.arange(min(shp))
ii = (i, i)
diag = np.ravel_multi_index(ii, shp)
elif ndim == 3:
i = np.arange(min(shp))
iii = (i, i, i)
diag = np.ravel_multi_index(iii, shp)
return diag
def get_cell_for_tile(self, tile):
"""
Returns the cell corresponding to the given GLCF tile. The tile
is identified by its UTM identifier (e.g. HG5152)
"""
assert self.cell_width == self.GLCF_tile_width
assert self.cell_height == self.GLCF_tile_height
row_from = tile[0]
row_to = tile[1]
col_from = int(tile[2:4])
col_to = int(tile[4:6])
i = self.ROW_MAP[row_from.upper()] / 2
j = (col_from - 1) / 2
# print "GLCF cell ", i, j
fracnum = np.ravel_multi_index((i, j), (self.n_cells_y, self.n_cells_x))
return fracnum
def get_cells_for_tile(self, tile_h, tile_v):
"""
Returns the list of cells covered by the given modis tile. The tile
is identified by its MODIS grid coordinates
"""
range_x = np.arange(tile_h * self.n_cells_per_tile_x,
(tile_h + 1) * self.n_cells_per_tile_x)
range_y = np.arange(tile_v * self.n_cells_per_tile_y,
(tile_v + 1) * self.n_cells_per_tile_y)
cells_ij = np.dstack(
np.meshgrid(range_y, range_x, indexing='ij')).reshape(-1, 2)
cells = np.ravel_multi_index(
(cells_ij[:, 0], cells_ij[:, 1]),
(self.n_cells_y, self.n_cells_x)
)
# sanity check
assert len(cells) == self.n_cells_per_tile_x * self.n_cells_per_tile_y
return cells
def _calculate_transition_prob(self, current, delta):
"""
Determine the outcome for an action. Transition Prob is always 1.0.
:param current: Current position on the grid as (row, col)
:param delta: Change in position for transition
:return: (1.0, new_state, reward, done)
"""
new_position = np.array(current) + np.array(delta)
new_position = self._limit_coordinates(new_position).astype(int)
new_state = np.ravel_multi_index(tuple(new_position), self.shape)
if self._cliff[tuple(new_position)]:
return [(1.0, self.start_state_index, -100, False)]
terminal_state = (self.shape[0] - 1, self.shape[1] - 1)
is_done = tuple(new_position) == terminal_state
return [(1.0, new_state, -1, is_done)]
def _get_cut_mask(self, grid):
points_in_grid = np.all(self.positions > grid.LeftEdge, axis=1) & \
np.all(self.positions <= grid.RightEdge, axis=1)
pids = np.where(points_in_grid)[0]
mask = np.zeros(points_in_grid.sum(), dtype='int')
dts = np.zeros(points_in_grid.sum(), dtype='float64')
ts = np.zeros(points_in_grid.sum(), dtype='float64')
for mi, (i, pos) in enumerate(zip(pids, self.positions[points_in_grid])):
if not points_in_grid[i]: continue
ci = ((pos - grid.LeftEdge)/grid.dds).astype('int')
if grid.child_mask[ci[0], ci[1], ci[2]] == 0: continue
for j in range(3):
ci[j] = min(ci[j], grid.ActiveDimensions[j]-1)
mask[mi] = np.ravel_multi_index(ci, grid.ActiveDimensions)
dts[mi] = self.dts[i]
ts[mi] = self.ts[i]
self._dts[grid.id] = dts
self._ts[grid.id] = ts
return mask
def test_learn_codes():
"""Test learning of codes."""
thresh = 0.25
X, ds, z = simulate_data(n_trials, n_times, n_times_atom, n_atoms)
for solver in ('l_bfgs', 'ista', 'fista'):
z_hat = update_z(X, ds, reg, n_times_atom, solver=solver,
solver_kwargs=dict(factr=1e11, max_iter=50))
X_hat = construct_X(z_hat, ds)
assert_true(np.corrcoef(X.ravel(), X_hat.ravel())[1, 1] > 0.99)
assert_true(np.max(X - X_hat) < 0.1)
# Find position of non-zero entries
idx = np.ravel_multi_index(z[0].nonzero(), z[0].shape)
loc_x, loc_y = np.where(z_hat[0] > thresh)
# shift position by half the length of atom
idx_hat = np.ravel_multi_index((loc_x, loc_y), z_hat[0].shape)
# make sure that the positions are a subset of the positions
# in the original z
mask = np.in1d(idx_hat, idx)
assert_equal(np.sum(mask), len(mask))
def __getitem__(self, slc):
if isinstance(slc, slice):
slc = [slc]
start, shape = [], []
for s, n in zip(slc, self.shape):
if isinstance(s, int):
s = slice(s, s+1)
b, e = s.start or 0, s.stop or n
if b < 0: b = n+b # remove neg begining indices
if e < 0: e = n+e # remove neg ending indices
if e < b: e = b # disallow negative sizes
if e > n: e = n # fix over-slices
start.append(b)
shape.append(e-b)
idx = np.ravel_multi_index(start, self.shape, order='F')
ptr = self._arr.value + idx * np.dtype(self.dtype).itemsize
ptr = c_ulong(ptr)
ld = self._leading_dim
return self._backend.dndarray(self._backend, tuple(shape),
self.dtype, ld=ld, own=False, data=ptr)
def TwoPeriodSunnyTimes(constant,delta,slope,slopedir,lat):
# First derive A1 and A2 from the normal procedure
A1,A2 = SunHours(delta,slope,slopedir,lat)
# Then calculate the other two functions.
# Initialize function
a,b,c = Constants(delta,slope,slopedir,lat)
riseSlope, setSlope = BoundsSlope(a,b,c)
B1 = np.maximum(riseSlope,setSlope)
B2 = np.minimum(riseSlope,setSlope)
Angle_B1 = AngleSlope(a,b,c,B1)
Angle_B2 = AngleSlope(a,b,c,B2)
B1[abs(Angle_B1) > 0.001] = np.pi - B1[abs(Angle_B1) > 0.001]
B2[abs(Angle_B2) > 0.001] = -np.pi - B2[abs(Angle_B2) > 0.001]
# Check if two periods really exist
ID = np.ravel_multi_index(np.where(np.logical_and(B2 >= A1, B1 <= A2) == True),a.shape)
Val = IntegrateSlope(constant,B2.flat[ID],B1.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID])
ID = ID[Val < 0]
return A1,A2,B1,B2
def _calculate_transition_prob(self, current, delta):
"""
Determine the outcome for an action. Transition Prob is always 1.0.
:param current: Current position on the grid as (row, col)
:param delta: Change in position for transition
:return: (1.0, new_state, reward, done)
"""
new_position = np.array(current) + np.array(delta)
new_position = self._limit_coordinates(new_position).astype(int)
new_state = np.ravel_multi_index(tuple(new_position), self.shape)
if self._cliff[tuple(new_position)]:
return [(1.0, self.start_state_index, -100, False)]
terminal_state = (self.shape[0] - 1, self.shape[1] - 1)
is_done = tuple(new_position) == terminal_state
return [(1.0, new_state, -1, is_done)]
def get_target(self, model, samples, metas):
yt_index=[]
if len(self.output_shape) == 2:
for b in range(len(metas)):
yt_index.append(numpy.ravel_multi_index((b, metas[b]["image_class"]), self.output_shape))
elif len(self.valid) > 0:
for b in range(len(metas)):
for v in range(len(self.valid)):
yt_index.append(numpy.ravel_multi_index((b, metas[b]["image_class"], v), self.output_shape))
else:
for b in range(len(metas)):
cls = metas[b]["image_class"]
for y in range(self.output_shape[2]):
for x in range(self.output_shape[3]):
yt_index.append(numpy.ravel_multi_index((b, metas[b]["image_class"], y, x), self.output_shape))
return numpy.array(yt_index, dtype=numpy.int64), numpy.array([], dtype=theano.config.floatX)
#return negative log-likelihood training cost (scalar)
def test_big_indices(self):
# ravel_multi_index for big indices (issue #7546)
if np.intp == np.int64:
arr = ([1, 29], [3, 5], [3, 117], [19, 2],
[2379, 1284], [2, 2], [0, 1])
assert_equal(
np.ravel_multi_index(arr, (41, 7, 120, 36, 2706, 8, 6)),
[5627771580, 117259570957])
# test overflow checking for too big array (issue #7546)
dummy_arr = ([0],[0])
half_max = np.iinfo(np.intp).max // 2
assert_equal(
np.ravel_multi_index(dummy_arr, (half_max, 2)), [0])
assert_raises(ValueError,
np.ravel_multi_index, dummy_arr, (half_max+1, 2))
assert_equal(
np.ravel_multi_index(dummy_arr, (half_max, 2), order='F'), [0])
assert_raises(ValueError,
np.ravel_multi_index, dummy_arr, (half_max+1, 2), order='F')
cliff_walking.py 文件源码
项目:Dropout-Uncertainty-Exploration-DQN
作者: Riashat
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def __init__(self):
self.shape = (4, 12)
nS = np.prod(self.shape)
nA = 4
# Cliff Location
self._cliff = np.zeros(self.shape, dtype=np.bool)
self._cliff[3, 1:-1] = True
# Calculate transition probabilities
P = {}
for s in range(nS):
position = np.unravel_index(s, self.shape)
P[s] = { a : [] for a in range(nA) }
P[s][UP] = self._calculate_transition_prob(position, [-1, 0])
P[s][RIGHT] = self._calculate_transition_prob(position, [0, 1])
P[s][DOWN] = self._calculate_transition_prob(position, [1, 0])
P[s][LEFT] = self._calculate_transition_prob(position, [0, -1])
# We always start in state (3, 0)
isd = np.zeros(nS)
isd[np.ravel_multi_index((3,0), self.shape)] = 1.0
super(CliffWalkingEnv, self).__init__(nS, nA, P, isd)
solve_foreground_background.py 文件源码
项目:closed-form-matting
作者: MarcoForte
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def get_grad_operator(mask):
"""Returns sparse matrix computing horizontal, vertical, and two diagonal gradients."""
horizontal_left = np.ravel_multi_index(np.nonzero(mask[:, :-1] | mask[:, 1:]), mask.shape)
horizontal_right = horizontal_left + 1
vertical_top = np.ravel_multi_index(np.nonzero(mask[:-1, :] | mask[1:, :]), mask.shape)
vertical_bottom = vertical_top + mask.shape[1]
diag_main_1 = np.ravel_multi_index(np.nonzero(mask[:-1, :-1] | mask[1:, 1:]), mask.shape)
diag_main_2 = diag_main_1 + mask.shape[1] + 1
diag_sub_1 = np.ravel_multi_index(np.nonzero(mask[:-1, 1:] | mask[1:, :-1]), mask.shape) + 1
diag_sub_2 = diag_sub_1 + mask.shape[1] - 1
indices = np.stack((
np.concatenate((horizontal_left, vertical_top, diag_main_1, diag_sub_1)),
np.concatenate((horizontal_right, vertical_bottom, diag_main_2, diag_sub_2))
), axis=-1)
return scipy.sparse.coo_matrix(
(np.tile([-1, 1], len(indices)), (np.arange(indices.size) // 2, indices.flatten())),
shape=(len(indices), mask.size))
def toa_3D_bundle(d, x, y, inliers):
(I, J) = inliers.nonzero()
ind = np.ravel_multi_index((I, J), dims=d.shape)
D = d[ind]
xopt, yopt, res, jac = bundletoa(D, I, J, x, y)
return xopt, yopt, res, jac
toa_3D_bundle_with_smoother.py 文件源码
项目:lps-anchor-pos-estimator
作者: bitcraze
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def toa_3D_bundle_with_smoother(d, x, y, inliers=0, opts=[]):
(I, J) = inliers.nonzero()
ind = np.ravel_multi_index((I, J), dims=d.shape, order='F')
D = d[ind]
xopt, yopt, res, jac = bundletoa(D, I, J, x, y, 0, opts)
return xopt, yopt, res, jac
def sub2ind(shape, subs):
"""From the given shape, returns the index of the given subscript"""
if len(shape) == 1:
return subs
if type(subs) is not np.ndarray:
subs = np.array(subs)
if len(subs.shape) == 1:
subs = subs[np.newaxis, :]
assert subs.shape[1] == len(shape), (
'Indexing must be done as a column vectors. e.g. [[3,6],[6,2],...]'
)
inds = np.ravel_multi_index(subs.T, shape, order='F')
return mkvc(inds)
def test_basic(self):
assert_equal(np.unravel_index(2, (2, 2)), (1, 0))
assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2)
assert_equal(np.unravel_index(254, (17, 94)), (2, 66))
assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254)
assert_raises(ValueError, np.unravel_index, -1, (2, 2))
assert_raises(TypeError, np.unravel_index, 0.5, (2, 2))
assert_raises(ValueError, np.unravel_index, 4, (2, 2))
assert_raises(ValueError, np.ravel_multi_index, (-3, 1), (2, 2))
assert_raises(ValueError, np.ravel_multi_index, (2, 1), (2, 2))
assert_raises(ValueError, np.ravel_multi_index, (0, -3), (2, 2))
assert_raises(ValueError, np.ravel_multi_index, (0, 2), (2, 2))
assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.), (2, 2))
assert_equal(np.unravel_index((2*3 + 1)*6 + 4, (4, 3, 6)), [2, 1, 4])
assert_equal(
np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2*3 + 1)*6 + 4)
arr = np.array([[3, 6, 6], [4, 5, 1]])
assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37])
assert_equal(
np.ravel_multi_index(arr, (7, 6), order='F'), [31, 41, 13])
assert_equal(
np.ravel_multi_index(arr, (4, 6), mode='clip'), [22, 23, 19])
assert_equal(np.ravel_multi_index(arr, (4, 4), mode=('clip', 'wrap')),
[12, 13, 13])
assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621)
assert_equal(np.unravel_index(np.array([22, 41, 37]), (7, 6)),
[[3, 6, 6], [4, 5, 1]])
assert_equal(
np.unravel_index(np.array([31, 41, 13]), (7, 6), order='F'),
[[3, 6, 6], [4, 5, 1]])
assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1])
def test_dtypes(self):
# Test with different data types
for dtype in [np.int16, np.uint16, np.int32,
np.uint32, np.int64, np.uint64]:
coords = np.array(
[[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype)
shape = (5, 8)
uncoords = 8*coords[0]+coords[1]
assert_equal(np.ravel_multi_index(coords, shape), uncoords)
assert_equal(coords, np.unravel_index(uncoords, shape))
uncoords = coords[0]+5*coords[1]
assert_equal(
np.ravel_multi_index(coords, shape, order='F'), uncoords)
assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
coords = np.array(
[[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]],
dtype=dtype)
shape = (5, 8, 10)
uncoords = 10*(8*coords[0]+coords[1])+coords[2]
assert_equal(np.ravel_multi_index(coords, shape), uncoords)
assert_equal(coords, np.unravel_index(uncoords, shape))
uncoords = coords[0]+5*(coords[1]+8*coords[2])
assert_equal(
np.ravel_multi_index(coords, shape, order='F'), uncoords)
assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
def test_clipmodes(self):
# Test clipmodes
assert_equal(
np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode='wrap'),
np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12)))
assert_equal(np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12),
mode=(
'wrap', 'raise', 'clip', 'raise')),
np.ravel_multi_index([1, 1, 0, 2], (4, 3, 7, 12)))
assert_raises(
ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))
def act(self, observation, reward):
"""
Interact with and learn from the environment.
Returns the suggested control vector.
"""
observation = np.ravel_multi_index(observation, self.input_shape)
self.xp_q.update_reward(reward)
action = self.best_action(observation)
self.xp_q.add(observation, action)
action = np.unravel_index(action, self.output_shape)
return action
def _diagonal_idx_array(batch_size, n):
idx_offsets = np.arange(
start=0, stop=batch_size * n * n, step=n * n, dtype=np.int32).reshape(
(batch_size, 1))
idx = np.ravel_multi_index(
np.diag_indices(n), (n, n)).reshape((1, n)).astype(np.int32)
return cuda.to_gpu(idx + idx_offsets)
def _non_diagonal_idx_array(batch_size, n):
idx_offsets = np.arange(
start=0, stop=batch_size * n * n, step=n * n, dtype=np.int32).reshape(
(batch_size, 1))
idx = np.ravel_multi_index(
np.tril_indices(n, -1), (n, n)).reshape((1, -1)).astype(np.int32)
return cuda.to_gpu(idx + idx_offsets)
def _calculate_transition_prob(self, current, delta):
new_position = np.array(current) + np.array(delta)
new_position = self._limit_coordinates(new_position).astype(int)
new_state = np.ravel_multi_index(tuple(new_position), self.shape)
# Newer version of rewards/costs from G-learning paper
# reward = -100.0 if self._cliff[tuple(new_position)] else -1.0
reward = -1.0
if self._cliff[tuple(new_position)]:
reward = -100.0
elif tuple(new_position) == (3,11):
reward = 0.0
is_done = self._cliff[tuple(new_position)] or (tuple(new_position) == (3,11))
return [(1.0, new_state, reward, is_done)]
def _calculate_transition_prob(self, current, delta, winds):
new_position = np.array(current) + np.array(delta) + np.array([-1, 0]) * winds[tuple(current)]
new_position = self._limit_coordinates(new_position).astype(int)
new_state = np.ravel_multi_index(tuple(new_position), self.shape)
is_done = tuple(new_position) == (3, 7)
return [(1.0, new_state, -1.0, is_done)]
def __init__(self):
self.shape = (7, 10)
nS = np.prod(self.shape)
nA = 4
# Wind strength
winds = np.zeros(self.shape)
winds[:,[3,4,5,8]] = 1
winds[:,[6,7]] = 2
# Calculate transition probabilities
P = {}
for s in range(nS):
position = np.unravel_index(s, self.shape)
P[s] = { a : [] for a in range(nA) }
P[s][UP] = self._calculate_transition_prob(position, [-1, 0], winds)
P[s][RIGHT] = self._calculate_transition_prob(position, [0, 1], winds)
P[s][DOWN] = self._calculate_transition_prob(position, [1, 0], winds)
P[s][LEFT] = self._calculate_transition_prob(position, [0, -1], winds)
# We always start in state (3, 0)
isd = np.zeros(nS)
isd[np.ravel_multi_index((3,0), self.shape)] = 1.0
super(WindyGridworldEnv, self).__init__(nS, nA, P, isd)