2
2
3
3
import numpy as np
4
4
5
- from ..utils import _import_matlab , _matlab_array_types
5
+ from ..utils import _import_matlab , _matlab_array_types , DelayedImport
6
+
7
+
8
+ class _imports (DelayedImport ):
9
+ Array = 'mpython.array.Array'
10
+ Cell = 'mpython.cell.Cell'
11
+ Struct = 'mpython.struct.Struct'
12
+ SparseArray = 'mpython.sparse_array.SparseArray'
13
+ MatlabClass = 'mpython.matlab_class.MatlabClass'
14
+ MatlabFunction = 'mpython.matlab_function.MatlabFunction'
15
+ AnyDelayedArray = 'mpython.core.delayed_types.AnyDelayedArray'
6
16
7
17
8
18
class MatlabType :
@@ -16,16 +26,14 @@ def from_any(cls, other, **kwargs):
16
26
17
27
!!! warning "Conversion is performed in-place when possible."
18
28
"""
19
- # FIXME: Circular import
20
- from ..array import Array
21
-
22
- # FIXME: Circular import
23
- from ..cell import Cell
24
- from ..matlab_class import MatlabClass
25
- from ..matlab_function import MatlabFunction
26
- from ..sparse_array import SparseArray
27
- from ..struct import Struct
28
- from .delayed_types import AnyDelayedArray
29
+ # Circular import
30
+ Array = _imports .Array
31
+ Cell = _imports .Cell
32
+ MatlabClass = _imports .MatlabClass
33
+ MatlabFunction = _imports .MatlabFunction
34
+ SparseArray = _imports .SparseArray
35
+ Struct = _imports .Struct
36
+ AnyDelayedArray = _imports .AnyDelayedArray
29
37
30
38
# Conversion rules:
31
39
# - we do not convert to matlab's own array types
@@ -34,7 +42,7 @@ def from_any(cls, other, **kwargs):
34
42
# the matlab runtime;
35
43
# - instead, we convert to python types that mimic matlab types.
36
44
_from_any = partial (cls .from_any , ** kwargs )
37
- _from_runtime = kwargs .pop ("_from_runtime " , False )
45
+ _runtime = kwargs .pop ("_runtime " , None )
38
46
39
47
if isinstance (other , MatlabType ):
40
48
if isinstance (other , AnyDelayedArray ):
@@ -56,21 +64,21 @@ def from_any(cls, other, **kwargs):
56
64
elif type__ == "structarray" :
57
65
# MPython returns a list of dictionaries in data__
58
66
# and the array shape in size__.
59
- return Struct ._from_runtime (other )
67
+ return Struct ._from_runtime (other , _runtime )
60
68
61
69
elif type__ == "cell" :
62
70
# MPython returns a list of dictionaries in data__
63
71
# and the array shape in size__.
64
- return Cell ._from_runtime (other )
72
+ return Cell ._from_runtime (other , _runtime )
65
73
66
74
elif type__ == "object" :
67
75
# MPython returns the object's fields serialized
68
76
# in a dictionary.
69
- return MatlabClass ._from_runtime (other )
77
+ return MatlabClass ._from_runtime (other , _runtime )
70
78
71
79
elif type__ == "sparse" :
72
80
# MPython returns the coordinates and values in a dict.
73
- return SparseArray ._from_runtime (other )
81
+ return SparseArray ._from_runtime (other , _runtime )
74
82
75
83
elif type__ == "char" :
76
84
# Character array that is not a row vector
@@ -82,26 +90,28 @@ def from_any(cls, other, **kwargs):
82
90
size = size [:- 1 ] + [1 ]
83
91
other ["type__" ] = "cell"
84
92
other ["size__" ] = np .asarray ([size ])
85
- return Cell ._from_runtime (other )
93
+ return Cell ._from_runtime (other , _runtime )
86
94
87
95
else :
88
96
raise ValueError ("Don't know what to do with type" , type__ )
89
97
90
98
else :
91
- other = type (other )(zip (other .keys (), map (_from_any , other .values ())))
99
+ other = type (other )(
100
+ zip (other .keys (), map (_from_any , other .values ()))
101
+ )
92
102
return Struct .from_any (other )
93
103
94
104
if isinstance (other , (list , tuple , set )):
95
105
# nested tuples are cells of cells, not cell arrays
96
- if _from_runtime :
97
- return Cell ._from_runtime (other )
106
+ if _runtime :
107
+ return Cell ._from_runtime (other , _runtime )
98
108
else :
99
109
return Cell .from_any (other )
100
110
101
111
if isinstance (other , (np .ndarray , int , float , complex , bool )):
102
112
# [array of] numbers -> Array
103
- if _from_runtime :
104
- return Array ._from_runtime (other )
113
+ if _runtime :
114
+ return Array ._from_runtime (other , _runtime )
105
115
else :
106
116
return Array .from_any (other )
107
117
@@ -117,20 +127,20 @@ def from_any(cls, other, **kwargs):
117
127
118
128
matlab = _import_matlab ()
119
129
if matlab and isinstance (other , matlab .object ):
120
- return MatlabFunction .from_any (other )
130
+ return MatlabFunction ._from_runtime (other , _runtime )
121
131
122
132
if type (other ) in _matlab_array_types ():
123
- return Array ._from_runtime (other )
133
+ return Array ._from_runtime (other , _runtime )
124
134
125
135
if hasattr (other , "__iter__" ):
126
136
# Iterable -> let's try to make it a cell
127
- return cls .from_any (list (other ), _from_runtime = _from_runtime )
137
+ return cls .from_any (list (other ), _runtime = _runtime )
128
138
129
139
raise TypeError (f"Cannot convert { type (other )} into a matlab object." )
130
140
131
141
@classmethod
132
- def _from_runtime (cls , obj ):
133
- return cls .from_any (obj , _from_runtime = True )
142
+ def _from_runtime (cls , obj , _runtime ):
143
+ return cls .from_any (obj , _runtime = _runtime )
134
144
135
145
@classmethod
136
146
def _to_runtime (cls , obj ):
@@ -162,8 +172,7 @@ def _to_runtime(cls, obj):
162
172
return obj
163
173
164
174
elif sparse and isinstance (obj , sparse .sparray ):
165
- from .SparseArray import SparseArray
166
-
175
+ SparseArray = _imports .SparseArray
167
176
return SparseArray .from_any (obj )._as_runtime ()
168
177
169
178
else :
@@ -192,14 +201,20 @@ class AnyMatlabArray(MatlabType):
192
201
193
202
@property
194
203
def as_num (self ):
195
- raise TypeError (f"Cannot interpret a { type (self ).__name__ } as a numeric array" )
204
+ raise TypeError (
205
+ f"Cannot interpret a { type (self ).__name__ } as a numeric array"
206
+ )
196
207
197
208
@property
198
209
def as_cell (self ):
199
- raise TypeError (f"Cannot interpret a { type (self ).__name__ } as a cell" )
210
+ raise TypeError (
211
+ f"Cannot interpret a { type (self ).__name__ } as a cell"
212
+ )
200
213
201
214
@property
202
215
def as_struct (self ):
203
- raise TypeError (f"Cannot interpret a { type (self ).__name__ } as a struct" )
216
+ raise TypeError (
217
+ f"Cannot interpret a { type (self ).__name__ } as a struct"
218
+ )
204
219
205
220
# TODO: `as_obj` for object arrays?
0 commit comments