Skip to main content

A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.

Project description

🍁 cobra-array 🍁
Unified Array Utilities with Python Array API Compatibility

PyPI version Python codecov License

Tests Github stars

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: CompatArray and CompatNamespace provide 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)
    print(type(arr_np), arr_np.dtype)  # numpy.ndarray float32
    
    arr_torch = to_tensor(data, device="cpu")
    print(type(arr_torch), arr_torch.device)
    
    back_to_list = to_list(arr_np)
    print(back_to_list)  # [[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()
        print(spec.cxp.xp_name, spec.dtype, spec.device)
        print(x, y)
    
  • 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])
    print(out)
    
  • Default backend strategy:

    from cobra_array.default import as_default, default_spec
    
    spec = default_spec()
    print(spec.cxp.xp_name, spec.dtype, spec.device)
    
    x = as_default([1, 2, 3], unify_dtype=True, unify_device=True)
    print(x, x.dtype)
    

Requirements

  • Python >= 3.9
  • array-api-compat >= 1.11.2

License

See LICENSE in the repository.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cobra_array-0.1.1.tar.gz (43.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cobra_array-0.1.1-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

Details for the file cobra_array-0.1.1.tar.gz.

File metadata

  • Download URL: cobra_array-0.1.1.tar.gz
  • Upload date:
  • Size: 43.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for cobra_array-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5d1ff9f61883e5403860c91fd39cefb1c873bec2adeba93a93796c98c4553af4
MD5 0557cc16fe81d565557df1e8beca23ab
BLAKE2b-256 a18b9d9fe66931321fc6a63157a43cba8ff23902100631f73eed44a33e17dc55

See more details on using hashes here.

File details

Details for the file cobra_array-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cobra_array-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 44.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for cobra_array-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 faeed6b4b6d29c560aef31ce9236abc5fa054e0f54b9b22959a2c8cffbfcfb24
MD5 b30854b7d8e257e0fc40aa558657522e
BLAKE2b-256 636b2611ccb0cbc2f3b1b301921a881e12e4fb3a1083770b4da6bffa49d7bbaf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page