A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.
Project description
About
cobra-array is a backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across NumPy/PyTorch-style ecosystems.
- Python: 3.9+
- Runtime deps:
array-api-compat(>= 1.11.2)
Features
- 🚀 Ackend-agnostic API: Work with different array backends through one consistent interface.
- 🚀 Context-driven unification: Automatically align namespace, dtype, and device via context managers and decorators.
- 🚀 Compatibility wrappers:
CompatArrayandCompatNamespaceprovide a clean, consistent layer over native backend behavior.
Installation
Install from PyPI
pip install cobra-array
Quick Start
-
Basic conversions:
import numpy as np from cobra_array.convert import to_numpy, to_tensor, to_list data = [[1, 2], [3, 4]] arr_np = to_numpy(data, dtype=np.float32) # numpy.ndarray float32 arr_torch = to_tensor(data, device="cpu") back_to_list = to_list(arr_np) # [[1.0, 2.0], [3.0, 4.0]]
-
Context-based conversion:
import numpy as np from cobra_array import array_context, as_context, context_spec with array_context(xp="numpy", dtype=np.float32, device="cpu"): x = as_context([1, 2, 3]) y = as_context(np.array([4, 5])) spec = context_spec()
-
Auto-unify function arguments:
import numpy as np from cobra_array import unify_args @unify_args(ref=0, unify_dtype=True, unify_device=True, arraylike_only=True) def add_and_mean(a, b): c = a + b return c.mean() out = add_and_mean(np.array([1, 2, 3]), [4, 5, 6])
-
Default backend strategy:
from cobra_array.default import as_default, default_spec spec = default_spec() x = as_default([1, 2, 3], unify_dtype=True, unify_device=True)
Requirements
- Python >= 3.9
array-api-compat>= 1.11.2
License
See LICENSE in the repository.
Links
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cobra_array-0.2.0.tar.gz.
File metadata
- Download URL: cobra_array-0.2.0.tar.gz
- Upload date:
- Size: 47.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2eaa2088f5445a65c998df0c2410c1bb79bf2798fe8dd6579a044c5b56c583ba
|
|
| MD5 |
2490beb83072abb3a767133a40fab8e8
|
|
| BLAKE2b-256 |
e06d2f344151bbaece12c29db593a1a70e32b3c5ebaf7e621bce8d4b84b23a6c
|
File details
Details for the file cobra_array-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cobra_array-0.2.0-py3-none-any.whl
- Upload date:
- Size: 47.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22d0a9f30a3f5d7a0a6944cb23463f21db063702aa0d33f5bd52389f291c5203
|
|
| MD5 |
b0d685f4c595fc3b0d51b7308cccb297
|
|
| BLAKE2b-256 |
5f5869184f9fa25bff9e8408804c21aafb78807377125da9bb593db6b2a743c3
|