Python numpy.broadcast_to() Examples

The following are 30 code examples for showing how to use numpy.broadcast_to(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    License: Apache License 2.0 6 votes vote down vote up
def _prepare_value_nd(self, value, vshape):
        """Given value and vshape, create an `NDArray` from value with the same
        context and dtype as the current one and broadcast it to vshape."""
        if isinstance(value, numeric_types):
            value_nd = full(shape=vshape, val=value, ctx=self.context, dtype=self.dtype)
        elif isinstance(value, NDArray):
            value_nd = value.as_in_context(self.context)
            if value_nd.dtype != self.dtype:
                value_nd = value_nd.astype(self.dtype)
        else:
            try:
                value_nd = array(value, ctx=self.context, dtype=self.dtype)
            except:
                raise TypeError('NDArray does not support assignment with non-array-like'
                                ' object %s of type %s' % (str(value), str(type(value))))
        if value_nd.shape != vshape:
            value_nd = value_nd.broadcast_to(vshape)
        return value_nd 
Example 2
Project: chainerrl   Author: chainer   File: test_action_value.py    License: MIT License 6 votes vote down vote up
def test_max_unbounded(self):
        n_batch = 7
        ndim_action = 3
        mu = np.random.randn(n_batch, ndim_action).astype(np.float32)
        mat = np.broadcast_to(
            np.eye(ndim_action, dtype=np.float32)[None],
            (n_batch, ndim_action, ndim_action))
        v = np.random.randn(n_batch).astype(np.float32)
        q_out = action_value.QuadraticActionValue(
            chainer.Variable(mu),
            chainer.Variable(mat),
            chainer.Variable(v))

        v_out = q_out.max
        self.assertIsInstance(v_out, chainer.Variable)
        v_out = v_out.array

        np.testing.assert_almost_equal(v_out, v) 
Example 3
Project: chainerrl   Author: chainer   File: test_action_value.py    License: MIT License 6 votes vote down vote up
def test_getitem(self):
        n_batch = 7
        ndim_action = 3
        mu = np.random.randn(n_batch, ndim_action).astype(np.float32)
        mat = np.broadcast_to(
            np.eye(ndim_action, dtype=np.float32)[None],
            (n_batch, ndim_action, ndim_action))
        v = np.random.randn(n_batch).astype(np.float32)
        min_action, max_action = -1, 1
        qout = action_value.QuadraticActionValue(
            chainer.Variable(mu),
            chainer.Variable(mat),
            chainer.Variable(v),
            min_action,
            max_action,
        )
        sliced = qout[:3]
        np.testing.assert_equal(sliced.mu.array, mu[:3])
        np.testing.assert_equal(sliced.mat.array, mat[:3])
        np.testing.assert_equal(sliced.v.array, v[:3])
        np.testing.assert_equal(sliced.min_action, min_action)
        np.testing.assert_equal(sliced.max_action, max_action) 
Example 4
Project: tangent   Author: google   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def unreduce_array(array, shape, axis, keepdims):
  """Reverse summing over a dimension, NumPy implementation.

  Args:
    array: The array that was reduced.
    shape: The original shape of the array before reduction.
    axis: The axis or axes that were summed.
    keepdims: Whether these axes were kept as singleton axes.

  Returns:
    An array with axes broadcast to match the shape of the original array.
  """
  # NumPy uses a special default value for keepdims, which is equivalent to
  # False.
  if axis is not None and (not keepdims or keepdims is numpy._NoValue):  # pylint: disable=protected-access
    if isinstance(axis, int):
      axis = axis,
    for ax in sorted(axis):
      array = numpy.expand_dims(array, ax)
  return numpy.broadcast_to(array, shape)


# The values are unary functions. 
Example 5
Project: opencv_transforms   Author: jbohnslav   File: functional.py    License: MIT License 6 votes vote down vote up
def to_grayscale(img, num_output_channels=1):
    """Convert image to grayscale version of image.
    Args:
        img (numpy ndarray): Image to be converted to grayscale.
    Returns:
        numpy ndarray: Grayscale version of the image.
            if num_output_channels = 1 : returned image is single channel
            if num_output_channels = 3 : returned image is 3 channel with r = g = b
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be numpy ndarray. Got {}'.format(type(img)))

    if num_output_channels==1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis]
    elif num_output_channels==3:
        # much faster than doing cvtColor to go back to gray
        img = np.broadcast_to(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis], img.shape) 
    return img 
Example 6
Project: recruit   Author: Frank-qlu   File: stride_tricks.py    License: Apache License 2.0 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        return ()
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example 7
Project: deep-smoke-machine   Author: CMU-CREATE-Lab   File: opencv_functional.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def to_grayscale(img, num_output_channels=1):
    """Convert image to grayscale version of image.
    Args:
        img (numpy ndarray): Image to be converted to grayscale.
    Returns:
        numpy ndarray: Grayscale version of the image.
            if num_output_channels = 1 : returned image is single channel
            if num_output_channels = 3 : returned image is 3 channel with r = g = b
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be numpy ndarray. Got {}'.format(type(img)))

    if num_output_channels==1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis]
    elif num_output_channels==3:
        # much faster than doing cvtColor to go back to gray
        img = np.broadcast_to(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis], img.shape)
    return img 
Example 8
Project: knmt   Author: fabiencro   File: utils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def make_batch_mask(mb_size, n_head, max_length_1, max_length_2, 
                    key_seq_lengths=None,
                    future_mask=False,
                    mask_value=-10000):
    
    if future_mask:
        assert max_length_1 == max_length_2
        mask = np.array(
                np.broadcast_to(( (-mask_value) * (np.tri(max_length_1, dtype = np.float32)-1))[None,None,:,:], 
                                (mb_size, n_head, max_length_1, max_length_2))
                )
    else:
        mask = np.zeros((mb_size, n_head, max_length_1, max_length_2), dtype = np.float32)
        
    if key_seq_lengths is not None:
        assert mb_size == len(key_seq_lengths)
        assert min(key_seq_lengths) > 0
        assert max(key_seq_lengths) <= max_length_2
        for num_batch, length in enumerate(key_seq_lengths):
            mask[num_batch, :, :, length:] = mask_value

    return mask 
Example 9
Project: lingvo   Author: tensorflow   File: builder_lib_test.py    License: Apache License 2.0 6 votes vote down vote up
def testBroadcastConcat(self):
    b = builder_lib.ModelBuilderBase()
    p = b._BroadcastConcat('p', b._ArgIdx('arg0', [0]), b._ArgIdx('arg1', [1]))
    l = p.Instantiate()

    # 2 x 1 x 3
    x1 = np.asarray([[[1, 2, 3]], [[4, 5, 6]]])
    self.assertEqual(x1.shape, (2, 1, 3))
    # 1 x 4 x 2
    x2 = np.asarray([[[7, 8], [9, 10], [11, 12], [13, 14]]])
    self.assertEqual(x2.shape, (1, 4, 2))

    y = l.FPropDefaultTheta(tf.constant(x1), tf.constant(x2))

    expected_y = np.concatenate([
        np.broadcast_to(x1, [2, 4, 3]),
        np.broadcast_to(x2, [2, 4, 2]),
    ], axis=-1)  # pyformat: disable
    with self.session():
      actual_y = self.evaluate(y)
      # x1 will be broadcasted to [2, 4, 3]
      # x2 will be broadcasted to [2, 4, 2]
      # Concatenation on axis=-1 should result in a tensor of shape [2, 4, 5]
      self.assertEqual(actual_y.shape, (2, 4, 5))
      self.assertAllEqual(actual_y, expected_y) 
Example 10
Project: lambda-packs   Author: ryfeus   File: stride_tricks.py    License: MIT License 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        return ()
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example 11
Project: tf2rl   Author: keiohta   File: normalizer.py    License: MIT License 6 votes vote down vote up
def __call__(self, x, update=True):
        """Normalize mean and variance of values based on emprical values.
        Args:
            x (ndarray or Variable): Input values
            update (bool): Flag to learn the input values
        Returns:
            ndarray or Variable: Normalized output values
        """
        if self.count == 0:
            return x

        mean = np.broadcast_to(self._mean, x.shape)
        std_inv = np.broadcast_to(self._std_inverse, x.shape)

        if update:
            self.experience(x)

        normalized = (x - mean) * std_inv
        if self.clip_threshold is not None:
            normalized = np.clip(
                normalized, -self.clip_threshold, self.clip_threshold)
        return normalized 
Example 12
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: stride_tricks.py    License: MIT License 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the ararys that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        raise ValueError('must provide at least one argument')
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example 13
Project: safelife   Author: PartnershipOnAI   File: render_text.py    License: Apache License 2.0 6 votes vote down vote up
def render_board(board, goals=0, orientation=0, edit_loc=None, edit_color=0):
    """
    Just render the board itself. Doesn't require game state.
    """
    if edit_loc and (edit_loc[0] >= board.shape[0] or edit_loc[1] >= board.shape[1]):
        edit_loc = None
    goals = np.broadcast_to(goals, board.shape)

    screen = np.empty((board.shape[0]+2, board.shape[1]+3,), dtype=object)
    screen[:] = ''
    screen[0] = screen[-1] = ' -'
    screen[:,0] = screen[:,-2] = ' |'
    screen[:,-1] = '\n'
    screen[0,0] = screen[0,-2] = screen[-1,0] = screen[-1,-2] = ' +'
    screen[1:-1,1:-2] = render_cell(board, goals, orientation)

    if edit_loc:
        x1, y1 = edit_loc
        val = render_cell(board[y1, x1], goals[y1, x1], orientation, edit_color)
        screen[y1+1, x1+1] = str(val)
    return ''.join(screen.ravel()) 
Example 14
Project: safelife   Author: PartnershipOnAI   File: helper_utils.py    License: Apache License 2.0 6 votes vote down vote up
def __getitem__(self, items):
        # To make life easier, we only wrap when `items` is a tuple of slices.
        # It'd be nifty if they could be integers or work on arbitrary arrays,
        # but that's more work and it won't be used.
        if not (isinstance(items, tuple) and len(items) == 2 and
                isinstance(items[0], slice) and isinstance(items[1], slice)):
            return super().__getitem__(items)
        rows, cols = items
        rows = np.arange(
            rows.start or 0,
            rows.stop if rows.stop is not None else self.shape[0],
            rows.step or 1
        ) % self.shape[0]
        cols = np.arange(
            cols.start or 0,
            cols.stop if cols.stop is not None else self.shape[0],
            cols.step or 1
        ) % self.shape[1]
        nrows = len(rows)
        ncols = len(cols)
        rows = np.broadcast_to(rows[:,None], (nrows, ncols))
        cols = np.broadcast_to(cols[None,:], (nrows, ncols))
        return super().__getitem__((rows, cols)) 
Example 15
Project: vnpy_crypto   Author: birforce   File: stride_tricks.py    License: MIT License 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        return ()
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example 16
Project: BrainSpace   Author: MICA-MNI   File: utils.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _dominant_set_sparse(s, k, is_thresh=False, norm=False):
    """Compute dominant set for a sparse matrix."""
    if is_thresh:
        mask = s > k
        idx, data = np.where(mask), s[mask]
        s = ssp.coo_matrix((data, idx), shape=s.shape)

    else:  # keep top k
        nr, nc = s.shape
        idx = np.argpartition(s, nc - k, axis=1)
        col = idx[:, -k:].ravel()  # idx largest
        row = np.broadcast_to(np.arange(nr)[:, None], (nr, k)).ravel()
        data = s[row, col].ravel()
        s = ssp.coo_matrix((data, (row, col)), shape=s.shape)

    if norm:
        s.data /= s.sum(axis=1).A1[s.row]

    return s.tocsr(copy=False) 
Example 17
Project: youtube-video-face-swap   Author: DerWaldi   File: image_augmentation.py    License: MIT License 6 votes vote down vote up
def random_warp( image ):
    assert image.shape == (256,256,3)
    range_ = numpy.linspace( 128-80, 128+80, 5 )
    mapx = numpy.broadcast_to( range_, (5,5) )
    mapy = mapx.T

    mapx = mapx + numpy.random.normal( size=(5,5), scale=5 )
    mapy = mapy + numpy.random.normal( size=(5,5), scale=5 )

    interp_mapx = cv2.resize( mapx, (80,80) )[8:72,8:72].astype('float32')
    interp_mapy = cv2.resize( mapy, (80,80) )[8:72,8:72].astype('float32')

    warped_image = cv2.remap( image, interp_mapx, interp_mapy, cv2.INTER_LINEAR )

    src_points = numpy.stack( [ mapx.ravel(), mapy.ravel() ], axis=-1 )
    dst_points = numpy.mgrid[0:65:16,0:65:16].T.reshape(-1,2)
    mat = umeyama( src_points, dst_points, True )[0:2]

    target_image = cv2.warpAffine( image, mat, (64,64) )

    return warped_image, target_image 
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    License: Apache License 2.0 5 votes vote down vote up
def broadcast_like(self, other):
        """Broadcasts the input array to the shape of other.

        Broadcasting is only allowed on axes with size 1. The new shape cannot change
        the number of dimensions.
        For example, you could broadcast from shape (2, 1) to (2, 3), but not from
        shape (2, 3) to (2, 3, 3).

        Parameters
        ----------
        other : NDArray
            Array with shape of the desired array.

        Returns
        -------
        NDArray
            A NDArray with the desired shape that is not sharing data with this
            array, even if the new shape is the same as ``self.shape``.

        Examples
        --------
        >>> x = mx.nd.arange(0,3).reshape((1,3,1))
        >>> x.asnumpy()
        array([[[ 0.],
                [ 1.],
                [ 2.]]], dtype=float32)
        >>> y = x.broadcast_like(mx.nd.ones((2,3,3)))
        >>> y.asnumpy()
        array([[[ 0.,  0.,  0.],
                [ 1.,  1.,  1.],
                [ 2.,  2.,  2.]],
        <BLANKLINE>
               [[ 0.,  0.,  0.],
                [ 1.,  1.,  1.],
                [ 2.,  2.,  2.]]], dtype=float32)
        """
        return self.broadcast_to(other.shape) 
Example 19
def test_arange():
    for i in range(5):
        start = np.random.rand() * 10
        stop = start + np.random.rand() * 100
        step = np.random.rand() * 4
        repeat = int(np.random.rand() * 5) + 1
        gt = np.arange(start=start, stop=stop, step=step)
        gt = np.broadcast_to(gt.reshape((gt.shape[0], 1)), shape=(gt.shape[0], repeat)).ravel()
        pred = mx.nd.arange(start=start, stop=stop, step=step, repeat=repeat).asnumpy()
        assert_almost_equal(pred, gt)
    gt = np.arange(start=0, stop=10000**2, step=10001, dtype=np.int32)
    pred = mx.nd.arange(start=0, stop=10000**2, step=10001,
                        dtype="int32").asnumpy()
    assert_almost_equal(pred, gt) 
Example 20
Project: chainerrl   Author: chainer   File: test_action_value.py    License: MIT License 5 votes vote down vote up
def test_max_bounded(self):
        n_batch = 20
        ndim_action = 3
        mu = np.random.randn(n_batch, ndim_action).astype(np.float32)
        mat = np.broadcast_to(
            np.eye(ndim_action, dtype=np.float32)[None],
            (n_batch, ndim_action, ndim_action))
        v = np.random.randn(n_batch).astype(np.float32)
        min_action, max_action = -1.3, 1.3
        q_out = action_value.QuadraticActionValue(
            chainer.Variable(mu),
            chainer.Variable(mat),
            chainer.Variable(v),
            min_action, max_action)

        v_out = q_out.max
        self.assertIsInstance(v_out, chainer.Variable)
        v_out = v_out.array

        # If mu[i] is an valid action, v_out[i] should be v[i]
        mu_is_allowed = np.all(
            (min_action < mu) * (mu < max_action),
            axis=1)
        np.testing.assert_almost_equal(v_out[mu_is_allowed], v[mu_is_allowed])

        # Otherwise, v_out[i] should be less than v[i]
        mu_is_not_allowed = ~np.all(
            (min_action - 1e-2 < mu) * (mu < max_action + 1e-2),
            axis=1)
        np.testing.assert_array_less(
            v_out[mu_is_not_allowed],
            v[mu_is_not_allowed]) 
Example 21
Project: baseband   Author: mhvk   File: frame.py    License: GNU General Public License v3.0 5 votes vote down vote up
def valid(self, valid):
        valid = np.broadcast_to(valid, (len(self.frames),))
        for f, v in zip(self.frames, valid):
            f.valid = v 
Example 22
Project: baseband   Author: mhvk   File: frame.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, item, data):
        if isinstance(item, str):
            # Headers behave as dictionaries; except for thread_id and
            # invalid_data, assume set base properties all to the same value.
            data = np.broadcast_to(data, (len(self.frames),))
            if item == 'thread_id':
                if len(np.unique(data)) != len(self.frames):
                    raise ValueError("all thread ids should be unique.")
            elif (item != 'invalid_data'
                  and item in VDIFBaseHeader._header_parser.keys()):
                if data.strides != (0,) and len(np.unique(data)) > 1:
                    raise ValueError("base header keys should be identical.")

            for f, value in zip(self.frames, data):
                f.header[item] = value
            return

        (frames, frame_item,
         single_sample, single_frame, single_channel) = self._get_frames(item)

        if single_frame:
            frames[0][frame_item] = data
            return

        data = np.asanyarray(data)
        if single_channel:
            if single_sample or data.ndim <= 1:
                swapped = np.broadcast_to(data, (len(frames),))
            else:
                new_shape = (data.shape[0], len(frames))
                swapped = np.broadcast_to(data, new_shape).swapaxes(0, 1)
        else:
            if single_sample or data.ndim <= 2:
                new_shape = (len(frames),) + data.shape[1:]
                swapped = np.broadcast_to(data, new_shape)
            else:
                new_shape = (data.shape[0], len(frames)) + data.shape[2:]
                swapped = np.broadcast_to(data, new_shape).swapaxes(0, 1)

        for frame, frame_data in zip(frames, swapped):
            frame[frame_item] = frame_data 
Example 23
Project: astropy-healpix   Author: astropy   File: test_healpy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vec2ang(vectors, lonlat, ndim):
    vectors = np.broadcast_to(vectors, (2,) * ndim + (3,))
    theta1, phi1 = hp_compat.vec2ang(vectors, lonlat=lonlat)
    theta2, phi2 = hp.vec2ang(vectors, lonlat=lonlat)
    # Healpy sometimes returns NaNs for phi (somewhat incorrectly)
    phi2 = np.nan_to_num(phi2)
    assert_allclose(theta1, theta1, atol=1e-10)
    assert_allclose(phi1, phi2, atol=1e-10) 
Example 24
Project: vampyre   Author: GAMPTeam   File: linear_two.py    License: MIT License 5 votes vote down vote up
def get_tgt_vec(self,r):
        """
        Computes the target vector `g` in the above description
        """
        r0,r1 = r
        g0 = 1/self.rsqrt0*r0
        if self.wvar_pos:
            gout = 1/self.wsqrt*np.broadcast_to(self.b,self.shape1)
            g1 = 1/self.rsqrt1*r1            
            g = np.hstack((gout.ravel(),g0.ravel(),g1.ravel()))
        else:
            g1 = 1/self.rsqrt1*(r1-self.b)
            g = np.hstack((g0.ravel(),g1.ravel()))
        return g 
Example 25
Project: recruit   Author: Frank-qlu   File: stride_tricks.py    License: Apache License 2.0 5 votes vote down vote up
def broadcast_to(array, shape, subok=False):
    """Broadcast an array to a new shape.

    Parameters
    ----------
    array : array_like
        The array to broadcast.
    shape : tuple
        The shape of the desired array.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).

    Returns
    -------
    broadcast : array
        A readonly view on the original array with the given shape. It is
        typically not contiguous. Furthermore, more than one element of a
        broadcasted array may refer to a single memory location.

    Raises
    ------
    ValueError
        If the array is not compatible with the new shape according to NumPy's
        broadcasting rules.

    Notes
    -----
    .. versionadded:: 1.10.0

    Examples
    --------
    >>> x = np.array([1, 2, 3])
    >>> np.broadcast_to(x, (3, 3))
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    """
    return _broadcast_to(array, shape, subok=subok, readonly=True) 
Example 26
Project: recruit   Author: Frank-qlu   File: test_shape_base.py    License: Apache License 2.0 5 votes vote down vote up
def test_integer_split_2D_rows_greater_max_int32(self):
        a = np.broadcast_to([0], (1 << 32, 2))
        res = array_split(a, 4)
        chunk = np.broadcast_to([0], (1 << 30, 2))
        tgt = [chunk] * 4
        for i in range(len(tgt)):
            assert_equal(res[i].shape, tgt[i].shape) 
Example 27
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 5 votes vote down vote up
def test_indexing_array_weird_strides(self):
        # See also gh-6221
        # the shapes used here come from the issue and create the correct
        # size for the iterator buffering size.
        x = np.ones(10)
        x2 = np.ones((10, 2))
        ind = np.arange(10)[:, None, None, None]
        ind = np.broadcast_to(ind, (10, 55, 4, 4))

        # single advanced index case
        assert_array_equal(x[ind], x[ind.copy()])
        # higher dimensional advanced index
        zind = np.zeros(4, dtype=np.intp)
        assert_array_equal(x2[ind, zind], x2[ind.copy(), zind]) 
Example 28
Project: df   Author: dfaker   File: image_augmentation.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def random_warp( in_image ):
    assert in_image.shape[:2] == (256,256)

    image = in_image.copy()


    scale = 5

    range_ = numpy.linspace( 128-120, 128+120, scale )
    mapx = numpy.broadcast_to( range_, (scale,scale) )
    mapy = mapx.T

    mapx = mapx + numpy.random.normal( size=(scale,scale), scale= 6 )
    mapy = mapy + numpy.random.normal( size=(scale,scale), scale= 6 )

    interp_mapx = cv2.resize( mapx, (80,80) )[8:72,8:72].astype('float32')
    interp_mapy = cv2.resize( mapy, (80,80) )[8:72,8:72].astype('float32')

    warped_image = cv2.remap( image[:,:,:3], interp_mapx, interp_mapy, cv2.INTER_CUBIC )

    src_points = numpy.stack( [ mapx.ravel(), mapy.ravel() ], axis=-1 )
    dst_points = numpy.mgrid[0:65:16,0:65:16].T.reshape(-1,2)
    mat = umeyama( src_points, dst_points, True )[0:2]

    target_image = cv2.warpAffine( image, mat, (64,64) )

    target_mask = target_image[:,:,3].reshape((64,64,1))
    target_image = target_image[:,:,:3]


    if len(target_image.shape)>2:
      return ( warped_image, 
               target_image, 
               target_mask )
    else:
      return ( warped_image, 
               target_image ) 
Example 29
Project: df   Author: dfaker   File: exampleTrainer.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def random_warp(image):
  range_ = numpy.linspace(0, 256, 20)
  mapx = numpy.broadcast_to(range_, (20, 20))
  mapy = mapx.T
  numpy.random.seed( int(time.time()) )
  mapx = mapx + numpy.random.normal(size=(20, 20), scale=5)
  mapy = mapy + numpy.random.normal(size=(20, 20), scale=5)

  interp_mapx = cv2.resize(mapx, (256, 256)).astype('float32')
  interp_mapy = cv2.resize(mapy, (256, 256)).astype('float32')


  return cv2.remap(image, interp_mapx, interp_mapy, cv2.INTER_LINEAR) 
Example 30
Project: typhon   Author: atmtools   File: geodesy.py    License: MIT License 5 votes vote down vote up
def _broadcast(*args):
    """ Similar to broadcast_arrays in numpy but with minimum output size (1,)
    """
    shape = np.broadcast(*args).shape
    if not shape:
        shape = (1,)
    return [np.broadcast_to(array, shape) for array in args]