cobra-array 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. cobra_array-0.1.0/LICENSE +21 -0
  2. cobra_array-0.1.0/PKG-INFO +137 -0
  3. cobra_array-0.1.0/README.md +112 -0
  4. cobra_array-0.1.0/pyproject.toml +45 -0
  5. cobra_array-0.1.0/setup.cfg +4 -0
  6. cobra_array-0.1.0/src/cobra_array/__init__.py +105 -0
  7. cobra_array-0.1.0/src/cobra_array/_core.py +392 -0
  8. cobra_array-0.1.0/src/cobra_array/_utils.py +93 -0
  9. cobra_array-0.1.0/src/cobra_array/array_api.py +117 -0
  10. cobra_array-0.1.0/src/cobra_array/compat/__init__.py +23 -0
  11. cobra_array-0.1.0/src/cobra_array/compat/_array.py +499 -0
  12. cobra_array-0.1.0/src/cobra_array/compat/_array.pyi +1816 -0
  13. cobra_array-0.1.0/src/cobra_array/compat/_base.py +53 -0
  14. cobra_array-0.1.0/src/cobra_array/compat/_namespace.py +305 -0
  15. cobra_array-0.1.0/src/cobra_array/compat/_namespace.pyi +833 -0
  16. cobra_array-0.1.0/src/cobra_array/convert.py +334 -0
  17. cobra_array-0.1.0/src/cobra_array/convert.pyi +73 -0
  18. cobra_array-0.1.0/src/cobra_array/default.py +142 -0
  19. cobra_array-0.1.0/src/cobra_array/exceptions.py +74 -0
  20. cobra_array-0.1.0/src/cobra_array/types.py +68 -0
  21. cobra_array-0.1.0/src/cobra_array.egg-info/PKG-INFO +137 -0
  22. cobra_array-0.1.0/src/cobra_array.egg-info/SOURCES.txt +27 -0
  23. cobra_array-0.1.0/src/cobra_array.egg-info/dependency_links.txt +1 -0
  24. cobra_array-0.1.0/src/cobra_array.egg-info/requires.txt +6 -0
  25. cobra_array-0.1.0/src/cobra_array.egg-info/top_level.txt +1 -0
  26. cobra_array-0.1.0/tests/test_backend.py +139 -0
  27. cobra_array-0.1.0/tests/test_convert.py +101 -0
  28. cobra_array-0.1.0/tests/test_default.py +42 -0
  29. cobra_array-0.1.0/tests/test_wrap.py +58 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Cathie Li
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,137 @@
1
+ Metadata-Version: 2.4
2
+ Name: cobra-array
3
+ Version: 0.1.0
4
+ Summary: A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.
5
+ Author-email: Zhen Tian <zhen.tian.cs@gmail.com>
6
+ Project-URL: Homepage, https://github.com/tinchen777/cobra-array.git
7
+ Project-URL: Repository, https://github.com/tinchen777/cobra-array.git
8
+ Project-URL: Issues, https://github.com/tinchen777/cobra-array.git/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Programming Language :: Python :: 3.14
16
+ Requires-Python: >=3.9
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: array-api-compat>=1.11.2
20
+ Provides-Extra: dev
21
+ Requires-Dist: pytest; extra == "dev"
22
+ Requires-Dist: pytest-cov; extra == "dev"
23
+ Requires-Dist: numpy<2.0,>=1.21; extra == "dev"
24
+ Dynamic: license-file
25
+
26
+ <div align="center">
27
+
28
+ <h2 id="title">
29
+ 🍁 cobra-array 🍁<br>
30
+ <sub>Unified Array Utilities with Python Array API Compatibility</sub>
31
+ </h2>
32
+
33
+ [![PyPI version](https://img.shields.io/pypi/v/cobra-array.svg)](https://pypi.org/project/cobra-array/)
34
+ ![Python](https://img.shields.io/pypi/pyversions/cobra-array?color=brightgreen)
35
+ [![codecov](https://codecov.io/gh/tinchen777/cobra-array/branch/main/graph/badge.svg)](https://codecov.io/gh/tinchen777/cobra-array)
36
+ ![License](https://img.shields.io/github/license/tinchen777/cobra-array.svg)
37
+
38
+ [![Tests](https://github.com/tinchen777/cobra-array/actions/workflows/test.yml/badge.svg)](https://github.com/tinchen777/cobra-array/actions/workflows/test.yml)
39
+ ![Github stars](https://img.shields.io/github/stars/tinchen777/cobra-array.svg)
40
+
41
+ </div>
42
+
43
+ ## About
44
+
45
+ `cobra-array` is a backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.
46
+
47
+ - Python: 3.9+
48
+ - Runtime deps: `array-api-compat` (>= 1.11.2)
49
+
50
+ ## Features
51
+
52
+ - 🚀 **Ackend-agnostic API**: Work with different array backends through one consistent interface.
53
+ - 🚀 **Context-driven unification**: Automatically align namespace, dtype, and device via context managers and decorators.
54
+ - 🚀 **Compatibility wrappers**: `CompatArray` and `CompatNamespace` provide a clean, consistent layer over native backend behavior.
55
+
56
+ ## Installation
57
+
58
+ ### Install from PyPI
59
+
60
+ ```bash
61
+ pip install cobra-array
62
+ ```
63
+
64
+ ## Quick Start
65
+
66
+ - Basic conversions:
67
+
68
+ ```python
69
+ import numpy as np
70
+ from cobra_array.convert import to_numpy, to_tensor, to_list
71
+
72
+ data = [[1, 2], [3, 4]]
73
+
74
+ arr_np = to_numpy(data, dtype=np.float32)
75
+ print(type(arr_np), arr_np.dtype) # numpy.ndarray float32
76
+
77
+ arr_torch = to_tensor(data, device="cpu")
78
+ print(type(arr_torch), arr_torch.device)
79
+
80
+ back_to_list = to_list(arr_np)
81
+ print(back_to_list) # [[1.0, 2.0], [3.0, 4.0]]
82
+ ```
83
+
84
+ - Context-based conversion:
85
+
86
+ ```python
87
+ import numpy as np
88
+ from cobra_array import array_context, as_context, context_spec
89
+
90
+ with array_context(xp="numpy", dtype=np.float32, device="cpu"):
91
+ x = as_context([1, 2, 3])
92
+ y = as_context(np.array([4, 5]))
93
+ spec = context_spec()
94
+ print(spec.cxp.xp_name, spec.dtype, spec.device)
95
+ print(x, y)
96
+ ```
97
+
98
+ - Auto-unify function arguments:
99
+
100
+ ```python
101
+ import numpy as np
102
+ from cobra_array import unify_args
103
+
104
+ @unify_args(ref=0, unify_dtype=True, unify_device=True, arraylike_only=True)
105
+ def add_and_mean(a, b):
106
+ c = a + b
107
+ return c.mean()
108
+
109
+ out = add_and_mean(np.array([1, 2, 3]), [4, 5, 6])
110
+ print(out)
111
+ ```
112
+
113
+ - Default backend strategy:
114
+
115
+ ```python
116
+ from cobra_array.default import as_default, default_spec
117
+
118
+ spec = default_spec()
119
+ print(spec.cxp.xp_name, spec.dtype, spec.device)
120
+
121
+ x = as_default([1, 2, 3], unify_dtype=True, unify_device=True)
122
+ print(x, x.dtype)
123
+ ```
124
+
125
+ ## Requirements
126
+
127
+ - Python >= 3.9
128
+ - `array-api-compat` >= 1.11.2
129
+
130
+ ## License
131
+
132
+ See LICENSE in the repository.
133
+
134
+ ## Links
135
+
136
+ - [Homepage/Repo](https://github.com/tinchen777/cobra-array.git)
137
+ - [Issues](https://github.com/tinchen777/cobra-array.git/issues)
@@ -0,0 +1,112 @@
1
+ <div align="center">
2
+
3
+ <h2 id="title">
4
+ 🍁 cobra-array 🍁<br>
5
+ <sub>Unified Array Utilities with Python Array API Compatibility</sub>
6
+ </h2>
7
+
8
+ [![PyPI version](https://img.shields.io/pypi/v/cobra-array.svg)](https://pypi.org/project/cobra-array/)
9
+ ![Python](https://img.shields.io/pypi/pyversions/cobra-array?color=brightgreen)
10
+ [![codecov](https://codecov.io/gh/tinchen777/cobra-array/branch/main/graph/badge.svg)](https://codecov.io/gh/tinchen777/cobra-array)
11
+ ![License](https://img.shields.io/github/license/tinchen777/cobra-array.svg)
12
+
13
+ [![Tests](https://github.com/tinchen777/cobra-array/actions/workflows/test.yml/badge.svg)](https://github.com/tinchen777/cobra-array/actions/workflows/test.yml)
14
+ ![Github stars](https://img.shields.io/github/stars/tinchen777/cobra-array.svg)
15
+
16
+ </div>
17
+
18
+ ## About
19
+
20
+ `cobra-array` is a backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.
21
+
22
+ - Python: 3.9+
23
+ - Runtime deps: `array-api-compat` (>= 1.11.2)
24
+
25
+ ## Features
26
+
27
+ - 🚀 **Ackend-agnostic API**: Work with different array backends through one consistent interface.
28
+ - 🚀 **Context-driven unification**: Automatically align namespace, dtype, and device via context managers and decorators.
29
+ - 🚀 **Compatibility wrappers**: `CompatArray` and `CompatNamespace` provide a clean, consistent layer over native backend behavior.
30
+
31
+ ## Installation
32
+
33
+ ### Install from PyPI
34
+
35
+ ```bash
36
+ pip install cobra-array
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ - Basic conversions:
42
+
43
+ ```python
44
+ import numpy as np
45
+ from cobra_array.convert import to_numpy, to_tensor, to_list
46
+
47
+ data = [[1, 2], [3, 4]]
48
+
49
+ arr_np = to_numpy(data, dtype=np.float32)
50
+ print(type(arr_np), arr_np.dtype) # numpy.ndarray float32
51
+
52
+ arr_torch = to_tensor(data, device="cpu")
53
+ print(type(arr_torch), arr_torch.device)
54
+
55
+ back_to_list = to_list(arr_np)
56
+ print(back_to_list) # [[1.0, 2.0], [3.0, 4.0]]
57
+ ```
58
+
59
+ - Context-based conversion:
60
+
61
+ ```python
62
+ import numpy as np
63
+ from cobra_array import array_context, as_context, context_spec
64
+
65
+ with array_context(xp="numpy", dtype=np.float32, device="cpu"):
66
+ x = as_context([1, 2, 3])
67
+ y = as_context(np.array([4, 5]))
68
+ spec = context_spec()
69
+ print(spec.cxp.xp_name, spec.dtype, spec.device)
70
+ print(x, y)
71
+ ```
72
+
73
+ - Auto-unify function arguments:
74
+
75
+ ```python
76
+ import numpy as np
77
+ from cobra_array import unify_args
78
+
79
+ @unify_args(ref=0, unify_dtype=True, unify_device=True, arraylike_only=True)
80
+ def add_and_mean(a, b):
81
+ c = a + b
82
+ return c.mean()
83
+
84
+ out = add_and_mean(np.array([1, 2, 3]), [4, 5, 6])
85
+ print(out)
86
+ ```
87
+
88
+ - Default backend strategy:
89
+
90
+ ```python
91
+ from cobra_array.default import as_default, default_spec
92
+
93
+ spec = default_spec()
94
+ print(spec.cxp.xp_name, spec.dtype, spec.device)
95
+
96
+ x = as_default([1, 2, 3], unify_dtype=True, unify_device=True)
97
+ print(x, x.dtype)
98
+ ```
99
+
100
+ ## Requirements
101
+
102
+ - Python >= 3.9
103
+ - `array-api-compat` >= 1.11.2
104
+
105
+ ## License
106
+
107
+ See LICENSE in the repository.
108
+
109
+ ## Links
110
+
111
+ - [Homepage/Repo](https://github.com/tinchen777/cobra-array.git)
112
+ - [Issues](https://github.com/tinchen777/cobra-array.git/issues)
@@ -0,0 +1,45 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "cobra-array"
7
+ dynamic = ["version"]
8
+ description = "A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems."
9
+ readme = "README.md"
10
+ license-files = ["LICENSE"]
11
+ authors = [
12
+ {name = "Zhen Tian", email = "zhen.tian.cs@gmail.com"}
13
+ ]
14
+ requires-python = ">=3.9"
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3.9",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ "Programming Language :: Python :: 3.14",
23
+ ]
24
+ dependencies = [
25
+ "array-api-compat>=1.11.2"
26
+ ]
27
+
28
+ [project.optional-dependencies]
29
+ dev = [
30
+ "pytest",
31
+ "pytest-cov",
32
+ "numpy>=1.21,<2.0",
33
+ ]
34
+
35
+ [project.urls]
36
+ Homepage = "https://github.com/tinchen777/cobra-array.git"
37
+ Repository = "https://github.com/tinchen777/cobra-array.git"
38
+ Issues = "https://github.com/tinchen777/cobra-array.git/issues"
39
+
40
+ [tool.setuptools.dynamic]
41
+ version = {attr = "cobra_array.__version__"}
42
+
43
+ [tool.setuptools.packages.find]
44
+ where = ["src"]
45
+ include = ["cobra_array*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,105 @@
1
+ # src/cobra_array/__init__.py
2
+ """
3
+ cobra-array
4
+ ===========
5
+
6
+ A unified array interface for multiple array libraries, providing seamless interoperability and convenient utilities for array manipulation and conversion.
7
+ A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across NumPy/PyTorch-style ecosystems.
8
+
9
+ Modules
10
+ -------
11
+ - :mod:`cobra_array.compat`: Compatibility utilities for `compatibility namespaces` and `compatibility arrays`.
12
+ - :mod:`cobra_array.convert`: Utilities for converting between different array types and namespaces.
13
+ - :mod:`cobra_array.default`: Default utilities for array specifications and namespaces.
14
+ - :mod:`cobra_array.array_api`: Utilities for working with array namespaces and devices.
15
+ Functions
16
+ ---------
17
+ - :func:`array_spec`: Get the array specification of an object.
18
+ - :func:`context_spec`: Get the context specification of an object.
19
+ - :func:`as_context`: Convert an array-like object to an array in the determined context namespace.
20
+ - :func:`array_context`: Get the context namespace for a given array or set of arrays.
21
+ - :func:`unify_args`: A decorator to unify the array arguments of a function to the same namespace, dtype, and device.
22
+ - :func:`array_namespace_alias`: Get the alias of an array namespace if it exists.
23
+ - :func:`is_compat_namespace`: Check if an object is a `compatibility namespace`.
24
+ - :func:`is_array_namespace`: Check if an object is an `array namespace`.
25
+
26
+ Examples
27
+ --------
28
+ Basic conversions::
29
+
30
+ import numpy as np
31
+ from cobra_array.convert import to_numpy, to_tensor, to_list
32
+
33
+ data = [[1, 2], [3, 4]]
34
+
35
+ arr_np = to_numpy(data, dtype=np.float32)
36
+ print(type(arr_np), arr_np.dtype) # numpy.ndarray float32
37
+
38
+ arr_torch = to_tensor(data, device="cpu")
39
+ print(type(arr_torch), arr_torch.device)
40
+
41
+ back_to_list = to_list(arr_np)
42
+ print(back_to_list) # [[1.0, 2.0], [3.0, 4.0]]
43
+
44
+ Context-based conversion::
45
+
46
+ import numpy as np
47
+ from cobra_array import array_context, as_context, context_spec
48
+
49
+ with array_context(xp="numpy", dtype=np.float32, device="cpu"):
50
+ x = as_context([1, 2, 3])
51
+ y = as_context(np.array([4, 5]))
52
+ spec = context_spec()
53
+ print(spec.cxp.xp_name, spec.dtype, spec.device)
54
+ print(x, y)
55
+
56
+ Auto-unify function arguments::
57
+
58
+ import numpy as np
59
+ from cobra_array import unify_args
60
+
61
+ @unify_args(ref=0, unify_dtype=True, unify_device=True, arraylike_only=True)
62
+ def add_and_mean(a, b):
63
+ c = a + b
64
+ return c.mean()
65
+
66
+ out = add_and_mean(np.array([1, 2, 3]), [4, 5, 6])
67
+ print(out)
68
+
69
+ Default backend strategy::
70
+
71
+ from cobra_array.default import as_default, default_spec
72
+
73
+ spec = default_spec()
74
+ print(spec.cxp.xp_name, spec.dtype, spec.device)
75
+
76
+ x = as_default([1, 2, 3], unify_dtype=True, unify_device=True)
77
+ print(x, x.dtype)
78
+ """
79
+
80
+ from ._core import (
81
+ array_spec,
82
+ context_spec,
83
+ as_context,
84
+ array_context,
85
+ unify_args
86
+ )
87
+ from ._utils import (
88
+ array_namespace_alias,
89
+ is_compat_namespace,
90
+ is_array_namespace
91
+ )
92
+
93
+ __author__ = "Zhen Tian"
94
+ __version__ = "0.1.0"
95
+
96
+ __all__ = [
97
+ "array_spec",
98
+ "context_spec",
99
+ "as_context",
100
+ "array_context",
101
+ "unify_args",
102
+ "array_namespace_alias",
103
+ "is_compat_namespace",
104
+ "is_array_namespace"
105
+ ]