The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
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
Example 2
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
Example 3
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
Example 4
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)
Example 5
def get_pvlist_from_post( p, t, binstep=100, detx=256, dety=256 ): '''[email protected] Nov, 2017 to get a pos, val list of phonton hitting detector by giving p (photon hit pos_x * detx + y (photon hit pos_y), t (photon hit time), and the time bin The most important function for timepix Input: p: array, int64, coordinate-x * det_x + coordinate-y t: list, int64, photon hit time binstep: int, binstep (in t unit) period detx,dety: int/int, the detector size in x and y Output: positions: int array, (x*detx +y) vals: int array, counts of that positions counts: int array, counts of that positions in each binstep ''' v = ( t - t[0])//binstep L= np.max( v ) + 1 arr = np.ravel_multi_index( [ p, v ], [detx * dety,L ] ) uval, ind, count = np.unique( arr, return_counts=True, return_index=True) ind2 = np.lexsort( ( p[ind], v[ind] ) ) ps = (p[ind])[ind2] vs = count[ind2] cs = np.bincount(v[ind]) return ps,vs,cs
Example 6
def histogram_pt( p, t, binstep=100, detx=256, dety=256 ): '''[email protected] Nov, 2017 to get a histogram of phonton counts by giving p (photon hit pos_x * detx + y (photon hit pos_y), t (photon hit time), and the time bin The most important function for timepix Input: p: coordinate-x * det_x + coordinate-y t: photon hit time bin t in binstep (in t unit) period detx,dety: the detector size in x and y Output: the hitorgram of photons with bins as binstep (in time unit) ''' L= np.max( (t-t[0])//binstep ) + 1 #print(L,x,y, (t-t[0])//binstep) arr = np.ravel_multi_index( [ p, (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
Example 7
def histogram_xyt( x, y, t, binstep=100, detx=256, dety=256 ): '''[email protected] Mar, 2017 to get a histogram of phonton counts by giving x (photon hit pos_x), y (photon hit pos_y), t (photon hit time), and the time bin The most important function for timepix Input: x: coordinate-x y: coordinate-y t: photon hit time bin t in binstep (in t unit) period detx,dety: the detector size in x and y Output: the hitorgram of photons with bins as binstep (in time unit) ''' L= np.max( (t-t[0])//binstep ) + 1 #print(L,x,y, (t-t[0])//binstep) 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
Example 8
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 ######################################################
Example 9
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
Example 10
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
Example 11
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
Example 12
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)]
Example 13
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
Example 14
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))
Example 15
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)
Example 16
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
Example 17
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)]
Example 18
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)
Example 19
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')
Example 20
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)
Example 21
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))
Example 22
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
Example 23
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
Example 24
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)
Example 25
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])
Example 26
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'))
Example 27
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))
Example 28
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
Example 29
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)
Example 30
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)
Example 31
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)]
Example 32
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)]
Example 33
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)
Example 34
def get_ranking_scores(matched_predictions, feedback_data, switch_positive, alternative=True): users_num, topk, holdout = matched_predictions.shape ideal_scores_idx = np.argsort(feedback_data, axis=1)[:, ::-1] #returns column index only ideal_scores_idx = np.ravel_multi_index((np.arange(feedback_data.shape[0])[:, None], ideal_scores_idx), dims=feedback_data.shape) where = np.ma.where if np.ma.is_masked(feedback_data) else np.where is_positive = feedback_data >= switch_positive positive_feedback = where(is_positive, feedback_data, 0) negative_feedback = where(~is_positive, -feedback_data, 0) relevance_scores_pos = (matched_predictions * positive_feedback[:, None, :]).sum(axis=2) relevance_scores_neg = (matched_predictions * negative_feedback[:, None, :]).sum(axis=2) ideal_scores_pos = positive_feedback.ravel()[ideal_scores_idx] ideal_scores_neg = negative_feedback.ravel()[ideal_scores_idx] discount_num = max(holdout, topk) if alternative: discount = np.log2(np.arange(2, discount_num+2)) relevance_scores_pos = 2**relevance_scores_pos - 1 relevance_scores_neg = 2**relevance_scores_neg - 1 ideal_scores_pos = 2**ideal_scores_pos - 1 ideal_scores_neg = 2**ideal_scores_neg - 1 else: discount = np.hstack([1, np.log(np.arange(2, discount_num+1))]) dcg = (relevance_scores_pos / discount[:topk]).sum(axis=1) dcl = (relevance_scores_neg / -discount[:topk]).sum(axis=1) idcg = (ideal_scores_pos / discount[:holdout]).sum(axis=1) idcl = (ideal_scores_neg / -discount[:holdout]).sum(axis=1) with np.errstate(invalid='ignore'): ndcg = unmask(np.nansum(dcg / idcg) / users_num) ndcl = unmask(np.nansum(dcl / idcl) / users_num) ranking_score = namedtuple('Ranking', ['nDCG', 'nDCL'])._make([ndcg, ndcl]) return ranking_score
Example 35
def downvote_seen_items(recs, idx_seen): # NOTE for sparse scores matrix this method can lead to a slightly worse # results (comparing to the same method but with "densified" scores matrix) # models with sparse scores can alleviate that by extending recommendations # list with most popular items or items generated by a more sophisticated logic idx_seen = idx_seen[:2] # need only users and items if sp.sparse.issparse(recs): # No need to create 2 idx sets form idx lists. # When creating a set have to iterate over list (O(n)). # Intersecting set with list gives the same O(n). # So there's no performance gain in converting large list into set! # Moreover, large set creates additional memory overhead. Hence, # need only to create set from the test idx and calc intersection. recs_idx = pd.lib.fast_zip(list(recs.nonzero())) #larger seen_idx = pd.lib.fast_zip(list(idx_seen)) #smaller idx_seen_bool = np.in1d(recs_idx, set(seen_idx)) # sparse data may have no intersections with seen items if idx_seen_bool.any(): seen_data = recs.data[idx_seen_bool] # move seen items scores below minimum value # if not enough data, seen items won't be filtered out lowered = recs.data.min() - (seen_data.max() - seen_data) - 1 recs.data[idx_seen_bool] = lowered else: try: idx_seen_flat = np.ravel_multi_index(idx_seen, recs.shape) except ValueError: # make compatible for single user recommendations idx_seen_flat = idx_seen seen_data = recs.flat[idx_seen_flat] # move seen items scores below minimum value lowered = recs.min() - (seen_data.max() - seen_data) - 1 recs.flat[idx_seen_flat] = lowered
Example 36
def get_test_tensor(self, test_data, shape, start, end): slice_idx = self._slice_test_data(test_data, start, end) num_users = end - start num_items = shape[1] num_fdbks = shape[2] slice_shp = (num_users, num_items, num_fdbks) idx_flat = np.ravel_multi_index(slice_idx, slice_shp) shp_flat = (num_users*num_items, num_fdbks) idx = np.unravel_index(idx_flat, shp_flat) val = np.ones_like(slice_idx[2]) test_tensor_unfolded = csr_matrix((val, idx), shape=shp_flat, dtype=val.dtype) return test_tensor_unfolded, slice_idx
Example 37
def _remap_factors(entity_mapping, entity_factors, num_entities, num_factors): shape = (num_entities, num_factors) entity_id = np.repeat(entity_mapping.loc[:, 1].values, num_factors, axis=0).astype(np.int64) factor_id = entity_factors['col2'].values.astype(np.int64) entity_factors_idx = np.ravel_multi_index((entity_id, factor_id), dims=shape) entity_factors_new = np.zeros(shape) np.put(entity_factors_new, entity_factors_idx, entity_factors['col3'].values) return entity_factors_new
Example 38
def test_tiny_cycle(): g, idxs, degimg = csr.skeleton_to_csgraph(tinycycle) expected_indptr = [0, 0, 2, 4, 6, 8] expected_indices = [2, 3, 1, 4, 1, 4, 2, 3] expected_data = np.sqrt(2) assert_equal(g.indptr, expected_indptr) assert_equal(g.indices, expected_indices) assert_almost_equal(g.data, expected_data) expected_degrees = np.array([[0, 2, 0], [2, 0, 2], [0, 2, 0]]) assert_equal(degimg, expected_degrees) assert_equal(np.ravel_multi_index(idxs.astype(int).T, tinycycle.shape), [0, 1, 3, 5, 7])
Example 39
def __init__(self): self.shape = (4, 12) self.start_state_index = np.ravel_multi_index((3, 0), self.shape) 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 and rewards 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]) # Calculate initial state distribution # We always start in state (3, 0) isd = np.zeros(nS) isd[self.start_state_index] = 1.0 super(CliffWalkingEnv, self).__init__(nS, nA, P, isd)
Example 40
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])
Example 41
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'))
Example 42
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))
Example 43
def get_variable_by_index(self, var, index): """ index = index arr of quads (maskedarray only) var = ndarray/ma.array returns ndarray/ma.array ordering is idx, idx+[0,1], idx+[1,1], idx+[1,0] masked values from var remain masked Function to get the node values of a given face index. Emulates the 'self.grid.nodes[self.grid.nodes.faces[index]]' paradigm of unstructured grids. """ var = var[:] if isinstance(var, np.ma.MaskedArray) or isinstance(index, np.ma.MaskedArray): rv = np.ma.empty((index.shape[0], 4), dtype=np.float64) if index.mask is not np.bool_(): # because False is not False. Thanks numpy rv.mask = np.zeros_like(rv, dtype=bool) rv.mask[:] = index.mask[:, 0][:, np.newaxis] rv.harden_mask() else: rv = np.zeros((index.shape[0], 4), dtype=np.float64) raw = np.ravel_multi_index(index.T, var.shape, mode='clip') rv[:, 0] = np.take(var, raw) raw += np.array(var.shape[1], dtype=np.int32) rv[:, 1] = np.take(var, raw) raw += 1 rv[:, 2] = np.take(var, raw) raw -= np.array(var.shape[1], dtype=np.int32) rv[:, 3] = np.take(var, raw) return rv
Example 44
def get_variable_at_index(self, var, index): var = var[:] rv = np.zeros((index.shape[0], 1), dtype=np.float64) mask = np.zeros((index.shape[0], 1), dtype=bool) raw = np.ravel_multi_index(index.T, var.shape, mode='clip') rv[:, 0] = np.take(var, raw) mask[:, 0] = np.take(var.mask, raw) return np.ma.array(rv, mask=mask)
Example 45
def extract_patch_coordinates(d1,d2,rf=(7,7),stride = (2,2)): """ Function that partition the FOV in patches and return the indexed in 2D and 1D (flatten, order='F') formats Parameters ---------- d1,d2: int dimensions of the original matrix that will be divided in patches rf: int radius of receptive field, corresponds to half the size of the square patch stride: int degree of overlap of the patches """ coords_flat=[] coords_2d=[] rf1,rf2 = rf stride1,stride2 = stride for xx in range(rf1,d1-rf1,2*rf1-stride1)+[d1-rf1]: for yy in range(rf2,d2-rf2,2*rf2-stride2)+[d2-rf2]: coords_x=np.array(range(xx - rf1, xx + rf1 + 1)) coords_y=np.array(range(yy - rf2, yy + rf2 + 1)) print([xx - rf1, xx + rf1 + 1,yy - rf2, yy + rf2 + 1]) coords_y = coords_y[(coords_y >= 0) & (coords_y < d2)] coords_x = coords_x[(coords_x >= 0) & (coords_x < d1)] idxs = np.meshgrid( coords_x,coords_y) coords_2d.append(idxs) coords_ =np.ravel_multi_index(idxs,(d1,d2),order='F') coords_flat.append(coords_.flatten()) return coords_flat,coords_2d #%%
Example 46
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])
Example 47
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'))
Example 48
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))
Example 49
def TwoPeriodSun(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] # Finally calculate resulting values Vals = np.zeros(B1.shape) Vals.flat[ID] = (IntegrateSlope(constant,A1.flat[ID],B2.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID]) + IntegrateSlope(constant,B1.flat[ID],A2.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID])) ID = np.ravel_multi_index(np.where(Vals == 0),a.shape) Vals.flat[ID] = IntegrateSlope(constant,A1.flat[ID],A2.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID]) return(Vals)
Example 50
def tensor_recommender(self): userid, itemid, contextid, values = self.fields v = self._items_factors w = self._context_factors #TODO: split calculation into batches of users so that it doesn't #blow computer memory out. test_shp = (self.test.testset[userid].max()+1, v.shape[0], w.shape[0]) idx_data = self.test.testset.loc[:, [userid, itemid, contextid]].values.T.astype(np.int64) idx_flat = np.ravel_multi_index(idx_data, test_shp) shp_flat = (test_shp[0]*test_shp[1], test_shp[2]) idx = np.unravel_index(idx_flat, shp_flat) #values are assumed to be contextualized already val = self.test.testset[values].values test_tensor_mat = sp.sparse.coo_matrix((val, idx), shape=shp_flat).tocsr() tensor_scores = np.empty((test_shp[0], test_shp[1])) chunk = self._chunk for i in xrange(0, test_shp[0], chunk): start = i stop = min(i+chunk, test_shp[0]) test_slice = test_tensor_mat[start*test_shp[1]:stop*test_shp[1], :] slice_scores = test_slice.dot(w).reshape(stop-start, test_shp[1], w.shape[1]) slice_scores = np.tensordot(slice_scores, v, axes=(1, 0)) slice_scores = np.tensordot(np.tensordot(slice_scores, v, axes=(2, 1)), w, axes=(1, 1)) tensor_scores[start:stop, :] = slice_scores.max(axis=2) return tensor_scores