Skip to main content

Pass arrays of any dimension to Cython as memory views (no Global Interpreter Lock (GIL))

pip install cythonanyarray

Tested against Windows / Python 3.11 / Anaconda

Cython (and a C/C++ compiler) must be installed

This module provides utility functions for creating and manipulating flattened indices and pointers for multi-dimensional arrays in Cython. It is designed to facilitate passing arrays of any dimension to Cython as memory views, enabling the release of the Global Interpreter Lock (GIL) for improved performance in parallel processing.

# Functions:
- get_pointer_array: Get a flat pointer array from the original array. # if you change it, the original data changes too
- get_iterarray: Get an array of flattened indices along with a flat pointer array for an input array.
- get_flat_iter_for_cython: Get an array of flattened indices and a flat pointer array suitable for use in Cython.
- get_iterarray_shape: Get an array of flattened indices with a specified last dimension.

import cv2
import numpy as np
from cythonanyarray import get_flat_iter_for_cython

data = cv2.imread(r"C:\Users\hansc\Pictures\socialmediaiconsmatching.png")
indexarray, flatpointerarray = get_flat_iter_for_cython(data, dtype=np.int64, unordered=True)
print(flatpointerarray)
# [[      0       0       0       0]
#  [   5745       1       0       0]
#  [  11490       2       0       0]
#  ...
#  [5543924     964    1914       2]
#  [5549669     965    1914       2]
#  [5555414     966    1914       2]]
# [51 51 51 ... 43 43 43]
# Iterate through the flattened indices - Blueprint for Cython
# This snipped is crap in Python but in Cython it is pure gold (don't forget to add types!)
flatiter = len(indexarray)
for i in range(flatiter):
    if flatpointerarray[indexarray[i][0]] == 255:
        print(indexarray[i][1:])
        break

# Accessing the original array using the flattened indices
print(data[34, 0, 0])  # Output: [34, 0, 0]

# Another example with a multi-dimensional array
data = np.arange(10 * 4 * 12 * 12 * 32).reshape((10, 4, 12, 12, 32)).astype(np.float64)
indexarray, flatpointerarray = get_flat_iter_for_cython(data, dtype=np.int64, unordered=True)
print(flatpointerarray)
# [[     0      0      0      0      0      0]
#  [ 18432      1      0      0      0      0]
#  [ 36864      2      0      0      0      0]
#  ...
#  [147455      7      3     11     11     31]
#  [165887      8      3     11     11     31]
#  [184319      9      3     11     11     31]]
# [0.00000e+00 1.00000e+00 2.00000e+00 ... 1.84317e+05 1.84318e+05
#  1.84319e+05]
# Iterate through the flattened indices
flatiter = len(indexarray)
for i in range(flatiter):
    if flatpointerarray[indexarray[i][0]] == 255.0:
        print(indexarray[i][1:])
        break

# Accessing the original array using the flattened indices
# Output: [0, 0, 0, 7, 31]


# Changing values

data = np.arange(100 * 100 * 100).reshape((100, 100, 100)).astype(np.int32)
indexarray, flatpointerarray = get_flat_iter_for_cython(data, dtype=np.int64, unordered=True)
print(flatpointerarray)
flatiter = len(indexarray)
for i in range(flatiter):
    if indexarray[i][3] % 2 == 0:
        flatpointerarray[indexarray[i][0]] = 10000000
print(data)
# [[[10000000        1 10000000 ...       97 10000000       99]
#   [10000000      101 10000000 ...      197 10000000      199]
#   [10000000      201 10000000 ...      297 10000000      299]
#   ...
#   [10000000     9701 10000000 ...     9797 10000000     9799]
#   [10000000     9801 10000000 ...     9897 10000000     9899]
#   [10000000     9901 10000000 ...     9997 10000000     9999]]

Metadata

Release files for cythonanyarray 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cythonanyarray 0.10
File Size Uploaded Provenance Actions
cythonanyarray-0.10.tar.gz 23.8 kB Without provenance Details for cythonanyarray-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cythonanyarray 0.10
File Interpreter ABI Platform Actions
cythonanyarray-0.10-py3-none-any.whl Python 3 none any Details for cythonanyarray-0.10-py3-none-any.whl

Total release size: 47.6 kB

Release files / cythonanyarray-0.10.tar.gz

Download URL cythonanyarray-0.10.tar.gz
Size 23.8 kB
Tags Source
SHA-256 checksum
How to use checksums
95ddca0e97c54c3427d99a19d77bb4ab317f6caf4088df6884d8bb4facf23407
BLAKE2b-256 checksum
How to use checksums
869291f91a760c1dc6c9bd576373d834edf40acbd3ece6cac8b94641cf3bd5f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / cythonanyarray-0.10-py3-none-any.whl

Download URL cythonanyarray-0.10-py3-none-any.whl
Size 23.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1826ddfc6ede32c7651efc3ce0f3d454481801263d0eb0d67447a1763d0f02b7
BLAKE2b-256 checksum
How to use checksums
2d000803e47bdcd5a55bb616cb978e78deec67c46a2e4693ed7379250846509d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for cythonanyarray 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page