Python numpy.moveaxis() Examples
The following are 30
code examples of numpy.moveaxis().
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 also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: base.py From DSFD-Pytorch-Inference with Apache License 2.0 | 7 votes |
def _pre_process(self, image: np.ndarray, shrink: float) -> torch.Tensor: """Takes N RGB image and performs and returns a set of bounding boxes as detections Args: image (np.ndarray): shape [N, height, width, 3] Returns: torch.Tensor: shape [N, 3, height, width] """ assert image.dtype == np.uint8 height, width = image.shape[1:3] image = image.astype(np.float32) - self.mean image = np.moveaxis(image, -1, 1) image = torch.from_numpy(image) if self.max_resolution is not None: shrink_factor = self.max_resolution / max((height, width)) if shrink_factor <= shrink: shrink = shrink_factor image = torch.nn.functional.interpolate(image, scale_factor=shrink) image = image.to(self.device) return image
Example #2
Source File: nanfunctions.py From lambda-packs with MIT License | 6 votes |
def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear'): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None or a.ndim == 1: part = a.ravel() result = _nanquantile_1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanquantile_1d, axis, a, q, overwrite_input, interpolation) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: result = np.moveaxis(result, axis, 0) if out is not None: out[...] = result return result
Example #3
Source File: nanfunctions.py From vnpy_crypto with MIT License | 6 votes |
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear'): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None or a.ndim == 1: part = a.ravel() result = _nanpercentile1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanpercentile1d, axis, a, q, overwrite_input, interpolation) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: result = np.moveaxis(result, axis, 0) if out is not None: out[...] = result return result
Example #4
Source File: math_op.py From ocelot with GNU General Public License v3.0 | 6 votes |
def n_moment(x, counts, c, n): x = np.squeeze(x) if x.ndim is not 1: raise ValueError("scale of x should be 1-dimensional") if x.size not in counts.shape: raise ValueError("operands could not be broadcast together with shapes %s %s" %(str(x.shape), str(counts.shape))) if np.sum(counts)==0: return 0 else: if x.ndim == 1 and counts.ndim == 1: return (np.sum((x-c)**n*counts) / np.sum(counts))**(1./n) else: if x.size in counts.shape: dim_ = [i for i, v in enumerate(counts.shape) if v == x.size] counts = np.moveaxis(counts, dim_, -1) return (np.sum((x-c)**n*counts, axis=-1) / np.sum(counts, axis=-1))**(1./n)
Example #5
Source File: tensor.py From discopy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, *dim): """ >>> Id(1) Tensor(dom=Dim(1), cod=Dim(1), array=[1]) >>> list(Id(2).array.flatten()) [1.0, 0.0, 0.0, 1.0] >>> Id(2).array.shape (2, 2) >>> list(Id(2, 2).array.flatten())[:8] [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] >>> list(Id(2, 2).array.flatten())[8:] [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] """ dim = dim[0] if isinstance(dim[0], Dim) else Dim(*dim) array = functools.reduce( lambda a, x: np.tensordot(a, np.identity(x), 0) if a.shape else np.identity(x), dim, np.array(1)) array = np.moveaxis( array, [2 * i for i in range(len(dim))], list(range(len(dim)))) super().__init__(dim, dim, array)
Example #6
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_errors(self): x = np.random.randn(1, 2, 3) assert_raises_regex(ValueError, 'invalid axis .* `source`', np.moveaxis, x, 3, 0) assert_raises_regex(ValueError, 'invalid axis .* `source`', np.moveaxis, x, -4, 0) assert_raises_regex(ValueError, 'invalid axis .* `destination`', np.moveaxis, x, 0, 5) assert_raises_regex(ValueError, 'repeated axis in `source`', np.moveaxis, x, [0, 0], [0, 1]) assert_raises_regex(ValueError, 'repeated axis in `destination`', np.moveaxis, x, [0, 1], [1, 1]) assert_raises_regex(ValueError, 'must have the same number', np.moveaxis, x, 0, [0, 1]) assert_raises_regex(ValueError, 'must have the same number', np.moveaxis, x, [0, 1], [0])
Example #7
Source File: nanfunctions.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear'): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None or a.ndim == 1: part = a.ravel() result = _nanquantile_1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanquantile_1d, axis, a, q, overwrite_input, interpolation) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: result = np.moveaxis(result, axis, 0) if out is not None: out[...] = result return result
Example #8
Source File: test_function_base.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_extended_axis(self): o = np.random.normal(size=(71, 23)) x = np.dstack([o] * 10) assert_equal(np.percentile(x, 30, axis=(0, 1)), np.percentile(o, 30)) x = np.moveaxis(x, -1, 0) assert_equal(np.percentile(x, 30, axis=(-2, -1)), np.percentile(o, 30)) x = x.swapaxes(0, 1).copy() assert_equal(np.percentile(x, 30, axis=(0, -1)), np.percentile(o, 30)) x = x.swapaxes(0, 1).copy() assert_equal(np.percentile(x, [25, 60], axis=(0, 1, 2)), np.percentile(x, [25, 60], axis=None)) assert_equal(np.percentile(x, [25, 60], axis=(0,)), np.percentile(x, [25, 60], axis=0)) d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11)) np.random.shuffle(d.ravel()) assert_equal(np.percentile(d, 25, axis=(0, 1, 2))[0], np.percentile(d[:,:,:, 0].flatten(), 25)) assert_equal(np.percentile(d, [10, 90], axis=(0, 1, 3))[:, 1], np.percentile(d[:,:, 1,:].flatten(), [10, 90])) assert_equal(np.percentile(d, 25, axis=(3, 1, -4))[2], np.percentile(d[:,:, 2,:].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(3, 1, 2))[2], np.percentile(d[2,:,:,:].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(3, 2))[2, 1], np.percentile(d[2, 1,:,:].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(1, -2))[2, 1], np.percentile(d[2,:,:, 1].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(1, 3))[2, 2], np.percentile(d[2,:, 2,:].flatten(), 25))
Example #9
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_preserve_order(self): x = np.zeros((1, 2, 3, 4)) for source, destination in [ (0, 0), (3, -1), (-1, 3), ([0, -1], [0, -1]), ([2, 0], [2, 0]), (range(4), range(4)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, (1, 2, 3, 4))
Example #10
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_move_multiples(self): x = np.zeros((0, 1, 2, 3)) for source, destination, expected in [ ([0, 1], [2, 3], (2, 3, 0, 1)), ([2, 3], [0, 1], (2, 3, 0, 1)), ([0, 1, 2], [2, 3, 0], (2, 3, 0, 1)), ([3, 0], [1, 0], (0, 3, 1, 2)), ([0, 3], [0, 1], (0, 3, 1, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
Example #11
Source File: test_function_base.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_extended_axis(self): o = np.random.normal(size=(71, 23)) x = np.dstack([o] * 10) assert_equal(np.median(x, axis=(0, 1)), np.median(o)) x = np.moveaxis(x, -1, 0) assert_equal(np.median(x, axis=(-2, -1)), np.median(o)) x = x.swapaxes(0, 1).copy() assert_equal(np.median(x, axis=(0, -1)), np.median(o)) assert_equal(np.median(x, axis=(0, 1, 2)), np.median(x, axis=None)) assert_equal(np.median(x, axis=(0, )), np.median(x, axis=0)) assert_equal(np.median(x, axis=(-1, )), np.median(x, axis=-1)) d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11)) np.random.shuffle(d.ravel()) assert_equal(np.median(d, axis=(0, 1, 2))[0], np.median(d[:,:,:, 0].flatten())) assert_equal(np.median(d, axis=(0, 1, 3))[1], np.median(d[:,:, 1,:].flatten())) assert_equal(np.median(d, axis=(3, 1, -4))[2], np.median(d[:,:, 2,:].flatten())) assert_equal(np.median(d, axis=(3, 1, 2))[2], np.median(d[2,:,:,:].flatten())) assert_equal(np.median(d, axis=(3, 2))[2, 1], np.median(d[2, 1,:,:].flatten())) assert_equal(np.median(d, axis=(1, -2))[2, 1], np.median(d[2,:,:, 1].flatten())) assert_equal(np.median(d, axis=(1, 3))[2, 2], np.median(d[2,:, 2,:].flatten()))
Example #12
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_array_likes(self): x = np.ma.zeros((1, 2, 3)) result = np.moveaxis(x, 0, 0) assert_(x.shape, result.shape) assert_(isinstance(result, np.ma.MaskedArray)) x = [1, 2, 3] result = np.moveaxis(x, 0, 0) assert_(x, list(result)) assert_(isinstance(result, np.ndarray))
Example #13
Source File: test_shape_base.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_exceptions(self): # test axis must be in bounds for ndim in [1, 2, 3]: a = np.ones((1,)*ndim) np.concatenate((a, a), axis=0) # OK assert_raises(np.AxisError, np.concatenate, (a, a), axis=ndim) assert_raises(np.AxisError, np.concatenate, (a, a), axis=-(ndim + 1)) # Scalars cannot be concatenated assert_raises(ValueError, concatenate, (0,)) assert_raises(ValueError, concatenate, (np.array(0),)) # test shapes must match except for concatenation axis a = np.ones((1, 2, 3)) b = np.ones((2, 2, 3)) axis = list(range(3)) for i in range(3): np.concatenate((a, b), axis=axis[0]) # OK assert_raises(ValueError, np.concatenate, (a, b), axis=axis[1]) assert_raises(ValueError, np.concatenate, (a, b), axis=axis[2]) a = np.moveaxis(a, -1, 0) b = np.moveaxis(b, -1, 0) axis.append(axis.pop(0)) # No arrays to concatenate raises ValueError assert_raises(ValueError, concatenate, ())
Example #14
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_move_to_end(self): x = np.random.randn(5, 6, 7) for source, expected in [(0, (6, 7, 5)), (1, (5, 7, 6)), (2, (5, 6, 7)), (-1, (5, 6, 7))]: actual = np.moveaxis(x, source, -1).shape assert_(actual, expected)
Example #15
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_move_new_position(self): x = np.random.randn(1, 2, 3, 4) for source, destination, expected in [ (0, 1, (2, 1, 3, 4)), (1, 2, (1, 3, 2, 4)), (1, -1, (1, 3, 4, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
Example #16
Source File: test_numeric.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_move_to_end(self): x = np.random.randn(5, 6, 7) for source, expected in [(0, (6, 7, 5)), (1, (5, 7, 6)), (2, (5, 6, 7)), (-1, (5, 6, 7))]: actual = np.moveaxis(x, source, -1).shape assert_(actual, expected)
Example #17
Source File: test_numeric.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_move_new_position(self): x = np.random.randn(1, 2, 3, 4) for source, destination, expected in [ (0, 1, (2, 1, 3, 4)), (1, 2, (1, 3, 2, 4)), (1, -1, (1, 3, 4, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
Example #18
Source File: test_numeric.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_preserve_order(self): x = np.zeros((1, 2, 3, 4)) for source, destination in [ (0, 0), (3, -1), (-1, 3), ([0, -1], [0, -1]), ([2, 0], [2, 0]), (range(4), range(4)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, (1, 2, 3, 4))
Example #19
Source File: test_numeric.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_move_multiples(self): x = np.zeros((0, 1, 2, 3)) for source, destination, expected in [ ([0, 1], [2, 3], (2, 3, 0, 1)), ([2, 3], [0, 1], (2, 3, 0, 1)), ([0, 1, 2], [2, 3, 0], (2, 3, 0, 1)), ([3, 0], [1, 0], (0, 3, 1, 2)), ([0, 3], [0, 1], (0, 3, 1, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
Example #20
Source File: models.py From rainymotion with MIT License | 5 votes |
def run(self): """ Run nowcasting calculations. Returns ------- nowcasts : 3D numpy array of shape (lead_steps, dim_x, dim_y). """ last_frame = self.input_data[-1, :, :] forecast = np.dstack([last_frame for i in range(self.lead_steps)]) return np.moveaxis(forecast, -1, 0).copy()
Example #21
Source File: test_shape_base.py From vnpy_crypto with MIT License | 5 votes |
def test_exceptions(self): # test axis must be in bounds for ndim in [1, 2, 3]: a = np.ones((1,)*ndim) np.concatenate((a, a), axis=0) # OK assert_raises(np.AxisError, np.concatenate, (a, a), axis=ndim) assert_raises(np.AxisError, np.concatenate, (a, a), axis=-(ndim + 1)) # Scalars cannot be concatenated assert_raises(ValueError, concatenate, (0,)) assert_raises(ValueError, concatenate, (np.array(0),)) # test shapes must match except for concatenation axis a = np.ones((1, 2, 3)) b = np.ones((2, 2, 3)) axis = list(range(3)) for i in range(3): np.concatenate((a, b), axis=axis[0]) # OK assert_raises(ValueError, np.concatenate, (a, b), axis=axis[1]) assert_raises(ValueError, np.concatenate, (a, b), axis=axis[2]) a = np.moveaxis(a, -1, 0) b = np.moveaxis(b, -1, 0) axis.append(axis.pop(0)) # No arrays to concatenate raises ValueError assert_raises(ValueError, concatenate, ())
Example #22
Source File: test_function_base.py From vnpy_crypto with MIT License | 5 votes |
def test_extended_axis(self): o = np.random.normal(size=(71, 23)) x = np.dstack([o] * 10) assert_equal(np.median(x, axis=(0, 1)), np.median(o)) x = np.moveaxis(x, -1, 0) assert_equal(np.median(x, axis=(-2, -1)), np.median(o)) x = x.swapaxes(0, 1).copy() assert_equal(np.median(x, axis=(0, -1)), np.median(o)) assert_equal(np.median(x, axis=(0, 1, 2)), np.median(x, axis=None)) assert_equal(np.median(x, axis=(0, )), np.median(x, axis=0)) assert_equal(np.median(x, axis=(-1, )), np.median(x, axis=-1)) d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11)) np.random.shuffle(d.ravel()) assert_equal(np.median(d, axis=(0, 1, 2))[0], np.median(d[:,:,:, 0].flatten())) assert_equal(np.median(d, axis=(0, 1, 3))[1], np.median(d[:,:, 1,:].flatten())) assert_equal(np.median(d, axis=(3, 1, -4))[2], np.median(d[:,:, 2,:].flatten())) assert_equal(np.median(d, axis=(3, 1, 2))[2], np.median(d[2,:,:,:].flatten())) assert_equal(np.median(d, axis=(3, 2))[2, 1], np.median(d[2, 1,:,:].flatten())) assert_equal(np.median(d, axis=(1, -2))[2, 1], np.median(d[2,:,:, 1].flatten())) assert_equal(np.median(d, axis=(1, 3))[2, 2], np.median(d[2,:, 2,:].flatten()))
Example #23
Source File: test_function_base.py From vnpy_crypto with MIT License | 5 votes |
def test_extended_axis(self): o = np.random.normal(size=(71, 23)) x = np.dstack([o] * 10) assert_equal(np.percentile(x, 30, axis=(0, 1)), np.percentile(o, 30)) x = np.moveaxis(x, -1, 0) assert_equal(np.percentile(x, 30, axis=(-2, -1)), np.percentile(o, 30)) x = x.swapaxes(0, 1).copy() assert_equal(np.percentile(x, 30, axis=(0, -1)), np.percentile(o, 30)) x = x.swapaxes(0, 1).copy() assert_equal(np.percentile(x, [25, 60], axis=(0, 1, 2)), np.percentile(x, [25, 60], axis=None)) assert_equal(np.percentile(x, [25, 60], axis=(0,)), np.percentile(x, [25, 60], axis=0)) d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11)) np.random.shuffle(d.ravel()) assert_equal(np.percentile(d, 25, axis=(0, 1, 2))[0], np.percentile(d[:,:,:, 0].flatten(), 25)) assert_equal(np.percentile(d, [10, 90], axis=(0, 1, 3))[:, 1], np.percentile(d[:,:, 1,:].flatten(), [10, 90])) assert_equal(np.percentile(d, 25, axis=(3, 1, -4))[2], np.percentile(d[:,:, 2,:].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(3, 1, 2))[2], np.percentile(d[2,:,:,:].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(3, 2))[2, 1], np.percentile(d[2, 1,:,:].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(1, -2))[2, 1], np.percentile(d[2,:,:, 1].flatten(), 25)) assert_equal(np.percentile(d, 25, axis=(1, 3))[2, 2], np.percentile(d[2,:, 2,:].flatten(), 25))
Example #24
Source File: tensor.py From discopy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __call__(self, diagram): if isinstance(diagram, Ty): return sum(map(self, diagram.objects), Dim(1)) if isinstance(diagram, Ob): return Dim(self.ob[Ty(Ob(diagram.name, z=0))]) if isinstance(diagram, Cup): return Tensor.cups(self(diagram.dom[0]), self(diagram.dom[1])) if isinstance(diagram, Cap): return Tensor.caps(self(diagram.cod[0]), self(diagram.cod[1])) if isinstance(diagram, Box): if diagram.is_dagger: return self(diagram.dagger()).dagger() return Tensor(self(diagram.dom), self(diagram.cod), self.ar[diagram]) if not isinstance(diagram, Diagram): raise TypeError(messages.type_err(Diagram, diagram)) def dim(scan): return len(self(scan)) scan, array = diagram.dom, Id(self(diagram.dom)).array for box, off in zip(diagram.boxes, diagram.offsets): left = dim(scan[:off]) if array.shape and self(box).array.shape: source = list(range(dim(diagram.dom) + left, dim(diagram.dom) + left + dim(box.dom))) target = list(range(dim(box.dom))) array = np.tensordot(array, self(box).array, (source, target)) else: array = array * self(box).array source = range(len(array.shape) - dim(box.cod), len(array.shape)) target = range(dim(diagram.dom) + left, dim(diagram.dom) + left + dim(box.cod)) array = np.moveaxis(array, list(source), list(target)) scan = scan[:off] + box.cod + scan[off + len(box.dom):] return Tensor(self(diagram.dom), self(diagram.cod), array)
Example #25
Source File: tensor.py From discopy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def dagger(self): array = np.moveaxis( self.array, range(len(self.dom + self.cod)), [i + len(self.cod) if i < len(self.dom) else i - len(self.dom) for i in range(len(self.dom + self.cod))]) return Tensor(self.cod, self.dom, np.conjugate(array))
Example #26
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_move_multiples(self): x = np.zeros((0, 1, 2, 3)) for source, destination, expected in [ ([0, 1], [2, 3], (2, 3, 0, 1)), ([2, 3], [0, 1], (2, 3, 0, 1)), ([0, 1, 2], [2, 3, 0], (2, 3, 0, 1)), ([3, 0], [1, 0], (0, 3, 1, 2)), ([0, 3], [0, 1], (0, 3, 1, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
Example #27
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_preserve_order(self): x = np.zeros((1, 2, 3, 4)) for source, destination in [ (0, 0), (3, -1), (-1, 3), ([0, -1], [0, -1]), ([2, 0], [2, 0]), (range(4), range(4)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, (1, 2, 3, 4))
Example #28
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_move_new_position(self): x = np.random.randn(1, 2, 3, 4) for source, destination, expected in [ (0, 1, (2, 1, 3, 4)), (1, 2, (1, 3, 2, 4)), (1, -1, (1, 3, 4, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
Example #29
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_move_to_end(self): x = np.random.randn(5, 6, 7) for source, expected in [(0, (6, 7, 5)), (1, (5, 7, 6)), (2, (5, 6, 7)), (-1, (5, 6, 7))]: actual = np.moveaxis(x, source, -1).shape assert_(actual, expected)
Example #30
Source File: numeric.py From lambda-packs with MIT License | 5 votes |
def _move_axis_to_0(a, axis): return moveaxis(a, axis, 0)