Provide an intuitive way to make ndarray indexing variable.
Project description
Summary
Provide an intuitive way to make ndarray indexing variable like this:
from npindex import npidx green_idx = npidx[:, :, 1] # assume that im1 and im2 are numpy ndarray # that were return values of OpenCV's cv2.imread() function. green_of_im1 = im1[green_idx] green_of_im2 = im2[green_idx]
This is all npindex does.
Usage
The signagure of npindex.npidx
npidx(d1, d2=None, d3=None, /)
Only positional argument is permitted. And 1, 2, 3 dimentional ndarrays are only supported.
Each arguments are treated as a slice object when arguments are passed in the form of slicing notation:
npidx[1:2, 3:4, 5:6] # => (slice(1, 2, None), slice(3, 4, None), slice(5, 6, None))
Or, treated as an integer when arguments are passed just as an integer:
npidx[1, 2, 3] # => (1, 2, 3)
What is the point of that? OK, I’ll show you it in the next section.
How to use effectively
We can access to items of ndarray by passing one or more slice objects inside its square brackets like this:
import numpy as np from npindex import npidx a = np.arange(10) # a => array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) i = npidx[1:5] # i => slice(1, 5, None) a[i] # => array([1, 2, 3, 4]) a2 = a.reshape(2, 5) # a => array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) i2 = npidx[:, 2:4] # i2 => (slice(None, None, None), slice(2, 4, None)) a2[i2] # => array([[2, 3], [7, 8]])
And it is also possible to access just one value from ndarray:
import numpy as np from npindex import npidx a = np.arange(10) # a => array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) i = npidx[1] # i => 1 a[i] # => 1 a2 = a.reshape(2, 5) # a => array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) i2 = npidx[0, 4] # (0, 4) a2[i2] # => 4
When we want to access values from two or more ndarray object, npidx is useful.:
import numpy as np from npindex import npidx range = npidx[100:300, 150:250, :] small_im1 = im1[range] small_im2 = im2[range]
Install
Linux, Windows, macOS:
pip install npindex
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file npindex-0.1.1.tar.gz
.
File metadata
- Download URL: npindex-0.1.1.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.4 Darwin/20.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c35b410becf0c0c7b860eaa70a859cf203cb229945f2ce4fa106e62827c9fe4e |
|
MD5 | 5dc2e9494450b810e6d63cdb4571d3a9 |
|
BLAKE2b-256 | a5babe9217868c25965f780fa9e258c71fbeaad04038c3a91c268c538ef0497f |
File details
Details for the file npindex-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: npindex-0.1.1-py3-none-any.whl
- Upload date:
- Size: 2.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.4 Darwin/20.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 924f0dca9bba27188b831a6d30114f4f966fe5dab5a0fdd18611707a4e895513 |
|
MD5 | 8aefa7178eef2de3fe28cb63557da845 |
|
BLAKE2b-256 | 22d14d9798176571306e9d6694a054e16e23f76aa97409568b79f8e0225d7107 |