# Locates values in NumPy Arrays with Cython
## pip install cythonflatiter
### Tested against Windows / Python 3.11 / Anaconda
## Cython (and a C/C++ compiler) must be installed
FlatIterArray is a utility class for efficiently searching multi-dimensional array data.
```python
| __init__(self, a, dtype=<class 'numpy.int64'>, unordered=True)
| Initializes a FlatIterArray instance.
|
| Parameters:
| - a (numpy.ndarray): The input array.
| - dtype (numpy.dtype, optional): The data type of the index array that will be created. Defaults to np.int64. (It's better not to change that, because it corresponds to cython.Py_ssize_t)
| - unordered (bool, optional): Flag indicating whether to use unordered iterations. Defaults to True. (index array will be created using multiprocessing)
|
| get_flat_pointer_array_from_orig_data(self)
| Returns a flat pointer array from the original data.
| If you change data here, it changes also in the original array
|
| Returns:
| - numpy.ndarray: Flat pointer array.
|
| search_multiple_values_in_array(self, values)
| Searches for multiple values in the array and returns indices and values.
|
| Parameters:
| - values (list or numpy.ndarray): List of values to search for.
|
| Returns:
| - tuple: Array of indices and array of found values.
|
| search_single_value_in_array(self, value)
| Searches for a single value in the array and returns indices.
|
| Parameters:
| - value: The value to search for.
|
| Returns:
| - numpy.ndarray: Array of indices.
|
| sequence_is_in_dimension(self, seq, last_dim)
| Checks if a sequence is in a dimension
|
| Parameters:
| - seq (list or numpy.ndarray): List of values representing the sequence.
| - last_dim: The dimension to search in.
|
| Returns:
| - numpy.ndarray: Array of indices.
|
| update_iterarray(self, dtype=<class 'numpy.int64'>, unordered=True)
| Updates the iterray attribute with new parameters.
|
| Parameters:
| - dtype (numpy.dtype, optional): The data type of the index array that will be created. Defaults to np.int64.
| - unordered (bool, optional): Flag indicating whether to use unordered iterations. Defaults to True. (index array will be created using multiprocessing)
|
| value_is_in_dimension(self, value, last_dim)
| Checks if a value is in a dimension
|
| Parameters:
| - value: The value to search for.
| - last_dim: The dimension to search in.
|
| Returns:
| - numpy.ndarray: Array of indices.
import numpy as np
import cv2
from cythonflatiter import FlatIterArray
data = cv2.imread(r"C:\Users\hansc\Desktop\2023-08-29xx16_07_30-Window.png")
f = FlatIterArray(data, dtype=np.int64, unordered=True)
results255inarray = f.search_single_value_in_array(255)
# results255inarray
# Out[6]:
# array([[195330, 34, 0, 0],
# [201075, 35, 0, 0],
# [206820, 36, 0, 0],
# ...,
# [488324, 84, 1914, 2],
# [494069, 85, 1914, 2],
# [499814, 86, 1914, 2]], dtype=int64)
# print(data[84,1914,2])
# print(data[34,0,0])
# 255
# 255
indices, found_values = f.search_multiple_values_in_array(values=[255, 11, 0])
# indices,found_values
# Out[12]:
# (array([[195330, 34, 0, 0],
# [201075, 35, 0, 0],
# [206820, 36, 0, 0],
# ...,
# [488324, 84, 1914, 2],
# [494069, 85, 1914, 2],
# [499814, 86, 1914, 2]], dtype=int64),
# array([255, 255, 255, ..., 255, 255, 255], dtype=uint8))
concat_values = np.hstack([indices, found_values.reshape((-1, 1))])
# print(concat_values)
# [[195330 34 0 0 255]
# [201075 35 0 0 255]
# [206820 36 0 0 255]
# ...
# [488324 84 1914 2 255]
# [494069 85 1914 2 255]
# [499814 86 1914 2 255]]
# print(data[85,1914,2])
# print(data[36,0,0])
lastdimwith255 = f.value_is_in_dimension(255, 3)
# lastdimwith255
# Out[31]:
# array([[195330, 34, 0],
# [201075, 35, 0],
# [206820, 36, 0],
# ...,
# [488322, 84, 1914],
# [494067, 85, 1914],
# [499812, 86, 1914]], dtype=int64)
# print(data[86,1914])
# print(data[34,0])
# [255 255 255]
# [255 255 255]
penultimatedimwith255255 = f.sequence_is_in_dimension([255, 255, 255], 2)
# penultimatedimwith255255
# Out[38]:
# array([[ 0, 0],
# [ 5745, 1],
# [ 11490, 2],
# ...,
# [5538180, 964],
# [5543925, 965],
# [5549670, 966]], dtype=int64)
# data[964]
# Out[40]:
# array([[255, 255, 255],
# [255, 255, 255],
# [255, 255, 255],
# ...,
# [ 43, 43, 43],
# [ 43, 43, 43],
# [ 43, 43, 43]], dtype=uint8)
ultimatedimwith255255255 = f.sequence_is_in_dimension([255, 255, 255], 3)
# ultimatedimwith255255255
# Out[42]:
# array([[195330, 34, 0],
# [201075, 35, 0],
# [206820, 36, 0],
# ...,
# [488322, 84, 1914],
# [494067, 85, 1914],
# [499812, 86, 1914]], dtype=int64)
# [750 433 0]
# [751 433 0]
# [752 433 0]
# [753 433 0]
# [754 433 0]
# [755 433 0]
# [756 433 0]
# [757 433 0]
# [758 433 0]
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/cythonflatiter",
"name": "cythonflatiter",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cython,arrays",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3f/95/c0f03d90f29cf0d097c1179826dcf381489a40c14037a4fc2ef8df0d5ca1/cythonflatiter-0.10.tar.gz",
"platform": null,
"description": "\r\n# Locates values in NumPy Arrays with Cython\r\n\r\n\r\n## pip install cythonflatiter\r\n\r\n### Tested against Windows / Python 3.11 / Anaconda\r\n\r\n\r\n## Cython (and a C/C++ compiler) must be installed\r\n\r\nFlatIterArray is a utility class for efficiently searching multi-dimensional array data.\r\n\r\n```python\r\n | __init__(self, a, dtype=<class 'numpy.int64'>, unordered=True)\r\n | Initializes a FlatIterArray instance.\r\n | \r\n | Parameters:\r\n | - a (numpy.ndarray): The input array.\r\n | - dtype (numpy.dtype, optional): The data type of the index array that will be created. Defaults to np.int64. (It's better not to change that, because it corresponds to cython.Py_ssize_t)\r\n | - unordered (bool, optional): Flag indicating whether to use unordered iterations. Defaults to True. (index array will be created using multiprocessing)\r\n | \r\n | get_flat_pointer_array_from_orig_data(self)\r\n | Returns a flat pointer array from the original data.\r\n | If you change data here, it changes also in the original array\r\n | \r\n | Returns:\r\n | - numpy.ndarray: Flat pointer array.\r\n | \r\n | search_multiple_values_in_array(self, values)\r\n | Searches for multiple values in the array and returns indices and values.\r\n | \r\n | Parameters:\r\n | - values (list or numpy.ndarray): List of values to search for.\r\n | \r\n | Returns:\r\n | - tuple: Array of indices and array of found values.\r\n | \r\n | search_single_value_in_array(self, value)\r\n | Searches for a single value in the array and returns indices.\r\n | \r\n | Parameters:\r\n | - value: The value to search for.\r\n | \r\n | Returns:\r\n | - numpy.ndarray: Array of indices.\r\n | \r\n | sequence_is_in_dimension(self, seq, last_dim)\r\n | Checks if a sequence is in a dimension\r\n | \r\n | Parameters:\r\n | - seq (list or numpy.ndarray): List of values representing the sequence.\r\n | - last_dim: The dimension to search in.\r\n | \r\n | Returns:\r\n | - numpy.ndarray: Array of indices.\r\n | \r\n | update_iterarray(self, dtype=<class 'numpy.int64'>, unordered=True)\r\n | Updates the iterray attribute with new parameters.\r\n | \r\n | Parameters:\r\n | - dtype (numpy.dtype, optional): The data type of the index array that will be created. Defaults to np.int64.\r\n | - unordered (bool, optional): Flag indicating whether to use unordered iterations. Defaults to True. (index array will be created using multiprocessing)\r\n | \r\n | value_is_in_dimension(self, value, last_dim)\r\n | Checks if a value is in a dimension\r\n | \r\n | Parameters:\r\n | - value: The value to search for.\r\n | - last_dim: The dimension to search in.\r\n | \r\n | Returns:\r\n | - numpy.ndarray: Array of indices.\r\n \r\n import numpy as np\r\nimport cv2\r\nfrom cythonflatiter import FlatIterArray\r\n\r\ndata = cv2.imread(r\"C:\\Users\\hansc\\Desktop\\2023-08-29xx16_07_30-Window.png\")\r\nf = FlatIterArray(data, dtype=np.int64, unordered=True)\r\nresults255inarray = f.search_single_value_in_array(255)\r\n# results255inarray\r\n# Out[6]:\r\n# array([[195330, 34, 0, 0],\r\n# [201075, 35, 0, 0],\r\n# [206820, 36, 0, 0],\r\n# ...,\r\n# [488324, 84, 1914, 2],\r\n# [494069, 85, 1914, 2],\r\n# [499814, 86, 1914, 2]], dtype=int64)\r\n# print(data[84,1914,2])\r\n# print(data[34,0,0])\r\n# 255\r\n# 255\r\nindices, found_values = f.search_multiple_values_in_array(values=[255, 11, 0])\r\n# indices,found_values\r\n# Out[12]:\r\n# (array([[195330, 34, 0, 0],\r\n# [201075, 35, 0, 0],\r\n# [206820, 36, 0, 0],\r\n# ...,\r\n# [488324, 84, 1914, 2],\r\n# [494069, 85, 1914, 2],\r\n# [499814, 86, 1914, 2]], dtype=int64),\r\n# array([255, 255, 255, ..., 255, 255, 255], dtype=uint8))\r\nconcat_values = np.hstack([indices, found_values.reshape((-1, 1))])\r\n# print(concat_values)\r\n# [[195330 34 0 0 255]\r\n# [201075 35 0 0 255]\r\n# [206820 36 0 0 255]\r\n# ...\r\n# [488324 84 1914 2 255]\r\n# [494069 85 1914 2 255]\r\n# [499814 86 1914 2 255]]\r\n# print(data[85,1914,2])\r\n# print(data[36,0,0])\r\nlastdimwith255 = f.value_is_in_dimension(255, 3)\r\n# lastdimwith255\r\n# Out[31]:\r\n# array([[195330, 34, 0],\r\n# [201075, 35, 0],\r\n# [206820, 36, 0],\r\n# ...,\r\n# [488322, 84, 1914],\r\n# [494067, 85, 1914],\r\n# [499812, 86, 1914]], dtype=int64)\r\n# print(data[86,1914])\r\n# print(data[34,0])\r\n# [255 255 255]\r\n# [255 255 255]\r\npenultimatedimwith255255 = f.sequence_is_in_dimension([255, 255, 255], 2)\r\n# penultimatedimwith255255\r\n# Out[38]:\r\n# array([[ 0, 0],\r\n# [ 5745, 1],\r\n# [ 11490, 2],\r\n# ...,\r\n# [5538180, 964],\r\n# [5543925, 965],\r\n# [5549670, 966]], dtype=int64)\r\n# data[964]\r\n# Out[40]:\r\n# array([[255, 255, 255],\r\n# [255, 255, 255],\r\n# [255, 255, 255],\r\n# ...,\r\n# [ 43, 43, 43],\r\n# [ 43, 43, 43],\r\n# [ 43, 43, 43]], dtype=uint8)\r\n\r\nultimatedimwith255255255 = f.sequence_is_in_dimension([255, 255, 255], 3)\r\n# ultimatedimwith255255255\r\n# Out[42]:\r\n# array([[195330, 34, 0],\r\n# [201075, 35, 0],\r\n# [206820, 36, 0],\r\n# ...,\r\n# [488322, 84, 1914],\r\n# [494067, 85, 1914],\r\n# [499812, 86, 1914]], dtype=int64)\r\n\r\n# [750 433 0]\r\n# [751 433 0]\r\n# [752 433 0]\r\n# [753 433 0]\r\n# [754 433 0]\r\n# [755 433 0]\r\n# [756 433 0]\r\n# [757 433 0]\r\n# [758 433 0]\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Locates values in NumPy Arrays with Cython",
"version": "0.10",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/cythonflatiter"
},
"split_keywords": [
"cython",
"arrays"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8fc9eb93c3ce0aaa2c8ccf7f89021ebbdf79b3f5fc62553b61955f716956f39e",
"md5": "2999777af6a533655b22db0e25d63574",
"sha256": "39631ce324276653b66a22e20e6e27df743b718072805ebc3736b2e20826284e"
},
"downloads": -1,
"filename": "cythonflatiter-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2999777af6a533655b22db0e25d63574",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 25261,
"upload_time": "2023-12-09T00:27:13",
"upload_time_iso_8601": "2023-12-09T00:27:13.175963Z",
"url": "https://files.pythonhosted.org/packages/8f/c9/eb93c3ce0aaa2c8ccf7f89021ebbdf79b3f5fc62553b61955f716956f39e/cythonflatiter-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3f95c0f03d90f29cf0d097c1179826dcf381489a40c14037a4fc2ef8df0d5ca1",
"md5": "0b99bd271f99330167f22dc3499bc43f",
"sha256": "297c93bb911bd1e632d26b53aa556c2b48961aa5ad3bd5970dc252ddd2d53f0e"
},
"downloads": -1,
"filename": "cythonflatiter-0.10.tar.gz",
"has_sig": false,
"md5_digest": "0b99bd271f99330167f22dc3499bc43f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 24975,
"upload_time": "2023-12-09T00:27:14",
"upload_time_iso_8601": "2023-12-09T00:27:14.882824Z",
"url": "https://files.pythonhosted.org/packages/3f/95/c0f03d90f29cf0d097c1179826dcf381489a40c14037a4fc2ef8df0d5ca1/cythonflatiter-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-09 00:27:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "cythonflatiter",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "cythonflatiter"
}