numpy.ravel_multi_index - python examples

Here are the examples of the python api numpy.ravel_multi_index taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

30 Examples 7

3 View Complete Implementation : test_index_tricks.py
Copyright MIT License
Author : abhisuri97
    def test_clipmodes(self):
        # Test clipmodes
        astert_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)))
        astert_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)))
        astert_raises(
            ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))

3 View Complete Implementation : text_handler.py
Copyright GNU General Public License v3.0
Author : CIRCL
    def get_hist_count_colors(self, img, startX, startY, endX, endY):
        crop_img = img[startX:endX, startY:endY]  # H,W

        a2D = crop_img.reshape(-1, crop_img.shape[-1])
        col_range = (256, 256, 256)  # generically : a2D.max(0)+1
        a1D = np.ravel_multi_index(a2D.T, col_range)
        return np.unravel_index(np.bincount(a1D).argmax(), col_range)

3 View Complete Implementation : adapter.py
Copyright MIT License
Author : crypt3lx2k
def move_to_label_flat (move):
    """Maps move to flat 1d-label."""
    return np.ravel_multi_index(
        move_to_label(move),
        config.clastes_shape
    )

3 View Complete Implementation : test_index_tricks.py
Copyright Apache License 2.0
Author : dnanexus
    def test_clipmodes(self):
        # Test clipmodes
        astert_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)))
        astert_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)))
        astert_raises(ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))

3 View Complete Implementation : sparse.py
Copyright MIT License
Author : evfro
def unfold_tensor_coordinates(index, shape, mode):
    # TODO implement direct calculation w/o intermediate flattening
    modes = [m for m in [0, 1, 2] if m != mode] + [mode,]
    mode_shape = tuple(shape[m] for m in modes)
    mode_index = tuple(index[m] for m in modes)
    flat_index = np.ravel_multi_index(mode_index, mode_shape)

    unfold_shape = (mode_shape[0]*mode_shape[1], mode_shape[2])
    unfold_index = np.unravel_index(flat_index, unfold_shape)
    return unfold_index, unfold_shape

3 View Complete Implementation : models.py
Copyright MIT License
Author : evfro
    def upvote_context_items(self, context, scores, test_users):
        if context is None:
            return

        userid = self.data.fields.userid
        itemid = self.data.fields.itemid
        context_data = self.data.context_data[context]
        try:
            upvote_items = context_data[userid].loc[test_users].map(context_data[itemid])
        except:
            print(f'Unable to upvote items in context "{context}"')
            return
        upvote_index = zip(*[(i, el) for i, l in enumerate(upvote_items) for el in l])

        context_idx_flat = np.ravel_multi_index(list(upvote_index), scores.shape)
        context_scores = scores.flat[context_idx_flat]

        upscored = scores.max() + context_scores + 1
        scores.flat[context_idx_flat] = upscored

3 View Complete Implementation : move_to_beacon_1d.py
Copyright Apache License 2.0
Author : islamelnabarawy
    def get_action(self, env, obs):
        neutral_y, neutral_x = (obs[0] == _PLAYER_NEUTRAL).nonzero()
        if not neutral_y.any():
            raise Exception('Beacon not found!')
        target = [int(neutral_x.mean()), int(neutral_y.mean())]
        target = np.ravel_multi_index(target, obs.shape[1:])
        return target

3 View Complete Implementation : gridworld.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : kentsommer
    def get_coords(self, states):
        # Given a state or states, returns
        #  [row,col] pairs for the state(s)
        non_obstacles = np.ravel_multi_index(
            [self.freespace[0], self.freespace[1]], (self.n_row, self.n_col),
            order='F')
        non_obstacles = np.sort(non_obstacles)
        states = states.astype(int)
        r, c = np.unravel_index(
            non_obstacles[states], (self.n_col, self.n_row), order='F')
        return r, c

3 View Complete Implementation : envs.py
Copyright MIT License
Author : paulorauber
    def distance(self, orig, dest):
        shape = self.layout.shape

        o_index = np.ravel_multi_index((int(orig[0]), int(orig[1])), shape)
        d_index = np.ravel_multi_index((int(dest[0]), int(dest[1])), shape)

        distance = self.dist_matrix[o_index, d_index]
        if not np.isfinite(distance):
            raise Exception('There is no path between origin and destination.')

        return distance

3 View Complete Implementation : dnn.py
Copyright MIT License
Author : splendor-kill
    def forge(self, row):
        board = row[:Board.BOARD_SIZE_SQ]
        image, _ = self.adapt_state(board)

        move = tuple(row[-4:-2].astype(int))
        move = np.ravel_multi_index(move, (Board.BOARD_SIZE, Board.BOARD_SIZE))

        return image, move