-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutumnBridge.py
371 lines (340 loc) · 12.1 KB
/
AutumnBridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from matlab.engine import start_matlab, connect_matlab
from typing import Optional
from scipy.io import savemat, loadmat
import numpy as np
from os import remove
from os.path import exists
from os import getcwd
def RandomBridge():
return 'B' + str(np.random.randint(1000000, 9999999))
def _MakeArgs(num: int, sym: str, br: str = ''):
"""
Input Num, Return Args Name list like [a1,a2,...,an]
"""
return ['%s%s%d' % (sym, br, i) for i in range(num)]
def _Del(fname: str):
if exists(fname):
remove(fname)
class MatlabError(Exception):
def __init__(self, err='Matlab Error'):
Exception.__init__(self, err)
class AutumnBridge:
def __init__(self, connect=False, desktop=False, background=False, MoreOpt=None, ID=None):
"""
Initialize the bridge between python and matlab.
:param connect: bool
If True, AutumnBridge will connect to an opened Matlab.
Otherwise, AutumnBridge will start a new Matlab.
:param desktop: bool
Avaliable only when connect==False
If True, Matlab will be start in desktop mode.
:param background: bool
Avaliable only when connect==False
If True, Matlab will start in async mode.
:param MoreOpt: str = None
Avaliable only when connect==False
More parameters avaliable in Matlab Options.
:param ID: int
The ID of the bridge.
If None, the ID will be id(self).
"""
opt = '-desktop' if desktop else '-nodesktop'
self._floatformat = np.float64
self._connect = connect
self._ID = ID if ID is not None else id(self)
if MoreOpt is not None:
opt = opt + ' ' + MoreOpt
self.eng = connect_matlab() if connect \
else start_matlab(opt, background=background)
self.eng.cd(getcwd())
def __del__(self):
"""
Quit Matlab.
"""
if self._connect is False:
self.eng.quit()
""" Dispatched
def __getattr__(self, item):
'''
The same function as eng.__getattr__
'''
return self.eng.__getattr__(item)
"""
def E(self, command: str, nargout=0, raise_error=False):
"""
A fast eval method.
:param command: str
matlab command.
:param nargout: int
Number of output arguments.
:param raise_error: bool
If False, the function will not raise an error, and give plain text warning.
If True, the function will raise an error, without more text warning.
:return:
Output arguments
"""
try:
return self.eng.eval(command, nargout=nargout)
except Exception as e:
if raise_error:
raise MatlabError(str(e))
def R(self, command: str, nargout=1, raise_error=False):
"""
A fast eval method, but return with scipy.io.
It can return big matrix.
:param command: str
matlab command.
:param nargout: int
Number of output arguments.
:param raise_error: bool
If False, the function will not raise an error, and give plain text warning.
If True, the function will raise an error, without more text warning.
:return:
Output arguments
"""
BR = RandomBridge()
ArgOuts = _MakeArgs(nargout, 'Out', BR)
fname = self._BridgeName(BR, '_out.mat')
try:
self.E('[%s]=%s;' % (','.join(ArgOuts), command), raise_error=True)
if nargout > 0:
self.E('save %s %s' % (fname, ' '.join(ArgOuts)), raise_error=True)
self.E('clear %s' % ' '.join(ArgOuts), raise_error=True)
except MatlabError as e:
if raise_error:
raise e
else:
return tuple([None for _ in range(nargout)]) if nargout >= 2 else None
except Exception as e:
raise e
else:
P = self._FromMat(BR, nargout)
if len(P) == 1:
return P[0]
else:
return P
finally:
self._DelInOut(BR)
def __getitem__(self, item: str):
"""
Get an item from workspace
:param item: str
Variable.
"""
if self.__contains__(item):
# item exist.
RB = RandomBridge()
fname = self._BridgeName(RB, '_out.mat')
try:
self.E('save %s %s' % (fname, item), raise_error=True)
except MatlabError as e:
raise MatlabError('Error occured when saving ' + item + ': ' + str(e))
else:
P = self._FromMat(RB, 1, [item])
if len(P) == 1:
return P[0]
else:
return P
finally:
self._DelInOut(RB)
else:
raise KeyError(item)
def __setitem__(self, item: str, value):
"""
Set an item into workspace
:param item: str
Variable
:param value:
any value to set to variable.
"""
RB = RandomBridge()
try:
self._ToMat(RB, value, argname=[item])
fname = self._BridgeName(RB, '_in.mat')
self.E('load %s' % fname, raise_error=True)
except MatlabError as e:
raise e
finally:
self._DelInOut(RB)
def __contains__(self, item):
"""
Check if item in workspace
:param item: str
Variable
:return: bool
True if workspace contains item
"""
P = self.E("exist('%s')" % item, 1, raise_error=True) == 1
return P
def __iter__(self):
A = self.A()
for i in A:
yield i
def S(self, item, raise_error=False):
"""
Show the shape / size of an item
:param item: str
Variable
:param raise_error: bool
If False, the function will not raise an error, and give plain text warning.
If True, the function will raise an error, without more text warning.
:return: tuple
shape or size
"""
if not self.__contains__(item):
if raise_error:
raise KeyError(item)
else:
print('ERROR: ', item, ' not exist.')
T = tuple(self.R('size(%s)' % item, raise_error=raise_error))
return T
def A(self, raise_error=False):
"""
Get all the variables' names.
:param raise_error: bool
If False, the function will not raise an error, and give plain text warning.
If True, the function will raise an error, without more text warning.
:return:
names of the variables
"""
try:
P = self.R('whos()', raise_error=True)
return self.R('whos()', raise_error=True)['name']
except IndexError:
return np.array([])
except MatlabError as e:
if raise_error:
raise e
else:
return np.array([])
def show(self):
"""
Equals to E('whos')
"""
self.E('whos')
def _BridgeName(self, bridge: str, suf: str):
"""
Return The Name Of the Bridge File
"""
return "AutumnBridge_%s_%s%s" % (str(self._ID), str(bridge), suf)
def BuildBridge(self, func: str, bridge: str, nargin: int, nargout: int):
"""
Build .m Bridge.
:param func: str
Name of the function that will be wrapped.
:param bridge: str
Name of the bridge
:param nargin: int
Number of input arguments
:param nargout: int
Number of output arguments
"""
fname = self._BridgeName(bridge, '.m')
funname = self._BridgeName(bridge, '')
inmatname = self._BridgeName(bridge, '_in.mat')
outmatname = self._BridgeName(bridge, '_out.mat')
OutArgs = _MakeArgs(nargout, 'Out', bridge)
InArgs = _MakeArgs(nargin, 'In', bridge)
with open(fname, 'w') as f:
P = []
P += ['function []=%s()\n' % funname]
P += ['\tload %s\n' % inmatname]
P += ['\t[%s]=%s(%s);\n' % (','.join(OutArgs), func, ','.join(InArgs))]
P += ['\tsave %s %s\n' % (outmatname, ' '.join(OutArgs))]
P += ['end\n']
f.writelines(P)
def DelBridge(self, bridge: str):
"""
Delete Bridge file.
:param bridge: str
The name of bridge
:return:
"""
fname = self._BridgeName(bridge, '.m')
_Del(fname)
def _DelInOut(self, bridge: str):
inmatname = self._BridgeName(bridge, '_in.mat')
outmatname = self._BridgeName(bridge, '_out.mat')
_Del(inmatname)
_Del(outmatname)
def _ToMat(self, bridge: str, *argin, argname=None):
inmatname = self._BridgeName(bridge, '_in.mat')
if argname is None:
InArgs = _MakeArgs(len(argin), 'In', bridge)
else:
InArgs = argname
argin = list(argin)
for i, j in enumerate(argin):
if type(argin[i]) is int:
argin[i] = float(argin[i])
if type(j) in [list, tuple]:
argin[i] = np.array(j)
if type(argin[i]) is np.ndarray and 'int' in argin[i].dtype.name:
argin[i] = argin[i].astype(self._floatformat)
savemat(inmatname, dict(zip(InArgs, argin)))
def _FromMat(self, bridge: str, nargout: int, argname=None):
outmatname = self._BridgeName(bridge, '_out.mat')
if argname is None:
OutArgs = _MakeArgs(nargout, 'Out', bridge)
else:
OutArgs = argname
D = loadmat(outmatname, squeeze_me=True, struct_as_record=True, chars_as_strings=True)
L = []
for i in OutArgs:
L = L + [D[i]]
return tuple(L)
def __call__(self, func: str, *argin, nargout=1, bridge: Optional[str] = None, NewBridge='auto', delete='auto'):
"""
Wrap a matlab function with Bridge, and transfer data/parameters much more faster
using scipy.io.
:param func: str
the function to be wrapped
:param argin: Any type that can be recognized by scipy.io.savemat
arguments or data that will be transfered to the matlab function
:param nargout: int
The number of output arguments
:param bridge: str
The name of bridge.
If None: the name will be random int.
:param NewBridge: bool or 'auto'
If True, the bridge .m file will be create anyhow.
If False, the bridge .m file will not be created, and make sure
you have ever created one with certain ID and Bridge name.
If 'auto', the bridge .m file will be create if there doesn't
exist one.
:param delete: bool or 'auto'
If True, the bridge .m file will be delete after function calling.
If False, the bridge .m file will not be delete.
If 'auto', delete=True if bridge is not None.
:return:
Tuple(arg0,arg1,...,argn), n=nargout
"""
if delete == 'auto':
delete = bridge is not None
else:
assert type(delete) is bool
if bridge is None:
bridge = RandomBridge()
fname = self._BridgeName(bridge, '.m')
funname = self._BridgeName(bridge, '')
if NewBridge == 'auto':
NewBridge = not exists(fname)
else:
assert type(NewBridge) is bool
if NewBridge:
self.BuildBridge(func, bridge, len(argin), nargout)
try:
self._ToMat(bridge, *argin)
self.eng.__getattr__(funname)(nargout=0)
T = self._FromMat(bridge, nargout)
except Exception as e:
raise e
else:
if len(T) == 1:
return T[0]
else:
return T
finally:
if delete:
self.DelBridge(bridge)
self._DelInOut(bridge)