33import numpy as np
44
55from ._array import Array
6- from .levels import Level , LevelFormat , LevelProperties , StorageFormat , get_storage_format
6+ from .formats import ConcreteFormat , Coo , Csf , Dense , Level , LevelFormat
77
88try :
99 import scipy .sparse as sps
@@ -31,26 +31,17 @@ def _from_numpy(arr: np.ndarray, copy: bool | None = None) -> Array:
3131 if copy :
3232 arr = arr .copy (order = "C" )
3333 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 ()
4235 return from_constituent_arrays (format = dense_format , arrays = (arr_flat ,), shape = arr .shape )
4336
4437
4538def 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 = } `" )
5041
5142 (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 ):
5445 arg_order [o ] = i
5546 arg_order = tuple (arg_order )
5647 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:
6354 raise TypeError (f"`arr` is not a `scipy.sparse` array, `{ type (arr )= } `." )
6455 match arr .format :
6556 case "csr" | "csc" :
57+ order = (0 , 1 ) if arr .format == "csr" else (1 , 0 )
6658 pos_width = arr .indptr .dtype .itemsize * 8
6759 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 ()
8268 )
8369
8470 indptr = arr .indptr
@@ -108,19 +94,13 @@ def _from_scipy(arr: ScipySparseArray, copy: bool | None = None) -> Array:
10894 row = row .copy ()
10995 col = col .copy ()
11096
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 ()
124104 )
125105
126106 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:
162142 return _from_numpy (np .asarray (arr ), copy = copy )
163143
164144
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 :
166146 storage = format ._get_ctypes_type ().from_constituent_arrays (arrays )
167147 return Array (storage = storage , shape = shape )
0 commit comments