3
3
import numpy as np
4
4
5
5
from ._array import Array
6
- from .levels import Level , LevelFormat , LevelProperties , StorageFormat , get_storage_format
6
+ from .formats import ConcreteFormat , Coo , Csf , Dense , Level , LevelFormat
7
7
8
8
try :
9
9
import scipy .sparse as sps
@@ -31,26 +31,17 @@ def _from_numpy(arr: np.ndarray, copy: bool | None = None) -> Array:
31
31
if copy :
32
32
arr = arr .copy (order = "C" )
33
33
arr_flat = np .ascontiguousarray (arr ).reshape (- 1 )
34
- levels = (Level (LevelFormat .Dense ),) * arr .ndim
35
- dense_format = get_storage_format (
36
- levels = levels ,
37
- order = "C" ,
38
- pos_width = 64 ,
39
- crd_width = 64 ,
40
- dtype = arr .dtype ,
41
- )
34
+ dense_format = Dense ().with_ndim (arr .ndim ).with_dtype (arr .dtype ).build ()
42
35
return from_constituent_arrays (format = dense_format , arrays = (arr_flat ,), shape = arr .shape )
43
36
44
37
45
38
def to_numpy (arr : Array ) -> np .ndarray :
46
- storage_format : StorageFormat = arr .format
47
-
48
- if not all (LevelFormat .Dense == level .format for level in storage_format .levels ):
49
- raise TypeError (f"Cannot convert a non-dense array to NumPy. `{ storage_format = } `" )
39
+ if not Dense .is_this_format (arr .format ):
40
+ raise TypeError (f"Cannot convert a non-dense array to NumPy. `{ arr .format = } `" )
50
41
51
42
(data ,) = arr .get_constituent_arrays ()
52
- arg_order = [0 ] * storage_format .storage_rank
53
- for i , o in enumerate (storage_format .order ):
43
+ arg_order = [0 ] * arr . format .storage_rank
44
+ for i , o in enumerate (arr . format .order ):
54
45
arg_order [o ] = i
55
46
arg_order = tuple (arg_order )
56
47
storage_shape = tuple (int (arr .shape [o ]) for o in arg_order )
@@ -63,22 +54,17 @@ def _from_scipy(arr: ScipySparseArray, copy: bool | None = None) -> Array:
63
54
raise TypeError (f"`arr` is not a `scipy.sparse` array, `{ type (arr )= } `." )
64
55
match arr .format :
65
56
case "csr" | "csc" :
57
+ order = (0 , 1 ) if arr .format == "csr" else (1 , 0 )
66
58
pos_width = arr .indptr .dtype .itemsize * 8
67
59
crd_width = arr .indices .dtype .itemsize * 8
68
- csx_format = get_storage_format (
69
- levels = (
70
- Level (LevelFormat .Dense ),
71
- Level (
72
- LevelFormat .Compressed ,
73
- LevelProperties (0 )
74
- if arr .has_canonical_format
75
- else LevelProperties .NonUnique | LevelProperties .NonOrdered ,
76
- ),
77
- ),
78
- order = (0 , 1 ) if arr .format == "csr" else (1 , 0 ),
79
- pos_width = pos_width ,
80
- crd_width = crd_width ,
81
- dtype = arr .dtype ,
60
+ csx_format = (
61
+ Csf ()
62
+ .with_ndim (2 , canonical = arr .has_canonical_format )
63
+ .with_dtype (arr .dtype )
64
+ .with_crd_width (crd_width )
65
+ .with_pos_width (pos_width )
66
+ .with_order (order )
67
+ .build ()
82
68
)
83
69
84
70
indptr = arr .indptr
@@ -108,19 +94,13 @@ def _from_scipy(arr: ScipySparseArray, copy: bool | None = None) -> Array:
108
94
row = row .copy ()
109
95
col = col .copy ()
110
96
111
- level_props = LevelProperties (0 )
112
- if not arr .has_canonical_format :
113
- level_props |= LevelProperties .NonOrdered
114
-
115
- coo_format = get_storage_format (
116
- levels = (
117
- Level (LevelFormat .Compressed , level_props | LevelProperties .NonUnique ),
118
- Level (LevelFormat .Singleton , level_props | LevelProperties .SOA ),
119
- ),
120
- order = (0 , 1 ),
121
- pos_width = pos_width ,
122
- crd_width = crd_width ,
123
- dtype = arr .dtype ,
97
+ coo_format = (
98
+ Coo ()
99
+ .with_ndim (2 , canonical = arr .has_canonical_format )
100
+ .with_dtype (arr .dtype )
101
+ .with_pos_width (pos_width )
102
+ .with_crd_width (crd_width )
103
+ .build ()
124
104
)
125
105
126
106
return from_constituent_arrays (format = coo_format , arrays = (pos , row , col , data ), shape = arr .shape )
@@ -162,6 +142,6 @@ def asarray(arr, copy: bool | None = None) -> Array:
162
142
return _from_numpy (np .asarray (arr ), copy = copy )
163
143
164
144
165
- def from_constituent_arrays (* , format : StorageFormat , arrays : tuple [np .ndarray , ...], shape : tuple [int , ...]) -> Array :
145
+ def from_constituent_arrays (* , format : ConcreteFormat , arrays : tuple [np .ndarray , ...], shape : tuple [int , ...]) -> Array :
166
146
storage = format ._get_ctypes_type ().from_constituent_arrays (arrays )
167
147
return Array (storage = storage , shape = shape )
0 commit comments