PageRenderTime 28ms CodeModel.GetById 42ms RepoModel.GetById 2ms app.codeStats 0ms

/ompclib/matpy.py

https://bitbucket.org/juricap/ompc/
Python | 826 lines | 648 code | 81 blank | 97 comment | 114 complexity | 2e7fc55a665076e1c615f2f4a17db2f2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. import numpy as _N
  2. import pylab as _P
  3. import scipy as _S
  4. from ompc import _print, _keywords
  5. # errors and warnings
  6. class OMPCException(Exception):
  7. def __init__(self,msg):
  8. self.msg = msg
  9. def __str__(self):
  10. return repr(self.msg)
  11. def error(x):
  12. raise OMPCException(x)
  13. # documentation
  14. def _func2str(f):
  15. from types import FunctionType, StringType
  16. if isinstance(f,FunctionType):
  17. fname = f.__name__
  18. elif isinstance(f,StringType):
  19. fname = f
  20. else:
  21. try:
  22. fname = str(f)
  23. except:
  24. raise error("Don't know how to lookup help for object %r"%f)
  25. return fname
  26. try:
  27. from ompcdoc import open as _webopen
  28. except:
  29. from webbrowser import open as _webopen
  30. def mhelp(f):
  31. """mhelp(func)
  32. Navigate browser to the online MATLAB(R) documentation for function
  33. specified in the 'func' parameter.
  34. func - can be either a function object or a string containing the name
  35. of the function.
  36. """
  37. fname = _func2str(f)
  38. MURL = 'http://www.mathworks.com/access/helpdesk/help/techdoc/index.html'\
  39. '?/access/helpdesk/help/techdoc/ref/%s.html'
  40. return _webopen(MURL%fname)
  41. def ohelp(func):
  42. """ohelp(func)
  43. Navigate browser to the online Octave documentation for function specified
  44. in the 'func' parameter.
  45. func - can be either a function object or a string containing the name
  46. of the function.
  47. """
  48. fname = _func2str(f)
  49. #f0 = fname[0].upper()
  50. #if f0 == '_': f0 = 'Z'
  51. #elif not f0.isalpha(): f0 = 'A'
  52. OURL = 'http://octave.sourceforge.net/doc/f/%s.html'
  53. _webopen(OURL%(fname))
  54. # helpers to allow matlab engine behavior
  55. __ompc_builtin__ = {}
  56. __ompc_whos__ = {}
  57. _MEND = None
  58. end = _MEND
  59. def _get_nargout():
  60. """Return how many values the caller is expecting."""
  61. import inspect, dis
  62. f = inspect.currentframe()
  63. f = f.f_back.f_back
  64. c = f.f_code
  65. i = f.f_lasti
  66. bytecode = c.co_code
  67. instruction = ord(bytecode[i+3])
  68. if instruction == dis.opmap['UNPACK_SEQUENCE']:
  69. howmany = ord(bytecode[i+4])
  70. return howmany
  71. elif instruction == dis.opmap['POP_TOP']:
  72. return 0
  73. return 1
  74. def _get_argout_name():
  75. """Return name of variable that return value will be assigned to."""
  76. import inspect, dis
  77. f = inspect.currentframe()
  78. f = f.f_back.f_back
  79. c = f.f_code
  80. # dis.disassemble_string(c.co_code)
  81. i = f.f_lasti
  82. bytecode = c.co_code
  83. instruction = ord(bytecode[i+3])
  84. if instruction != dis.opmap['STORE_NAME']:
  85. # POP_TOP, ROT_TWO and UNPACK_SEQUENCE are not allowed in MATLAB
  86. # fro constructors
  87. error("Construction assignment into multiple values is not allowed.")
  88. name = c.co_names[ord(bytecode[i+4])]
  89. return name
  90. def mfunction(func,globals_=[],persistent_=[]):
  91. """Decorator that populates function with MATLAB compatible interface.
  92. It adds local variables,
  93. nargin - number of actually submitted arguments,
  94. nargout - number of expected output arguments.
  95. All OMPC functions should return OMPC compatible arrays, a call is injected
  96. to transform all return values to OMPC arrays.
  97. """
  98. c = Code.from_code(func.func_code)
  99. c.code[:0] = [(LOAD_GLOBAL,'_get_nargout'),
  100. (CALL_FUNCTION,0),
  101. (STORE_FAST,'nargout')]
  102. # FIXME
  103. # the nargin is determined differently for varargin (==> *args) and
  104. # a regular list of arguments
  105. c.code[:0] = [(LOAD_GLOBAL,'len'),
  106. (LOAD_FAST,'args'),
  107. (CALL_FUNCTION,1),
  108. (STORE_FAST,'nargin'),]
  109. # replace LOAD_GLOBAL with LOAD_FAST for
  110. for i, x in enumerate(c.code):
  111. if x[0] == LOAD_GLOBAL and \
  112. (x[1] == 'nargout' or x[1] == 'nargin'):
  113. c.code[i] = (LOAD_FAST,x[1])
  114. # FIXME
  115. # inject return value conversion
  116. func.func_code = c.to_code()
  117. return func
  118. # all MATLAB
  119. # language
  120. def iskeyword(word):
  121. from ompc import _keywords
  122. return word in _keywords
  123. def who(*args,**kwargs):
  124. """Return list of variables in the current workspace."""
  125. nargin = len(args)
  126. nargout = _get_nargout()
  127. vars = []
  128. if kwargs.get('file'):
  129. # FIXME
  130. pass
  131. elif kwargs.get('regexp'):
  132. for x in __ompc_whos__.keys():
  133. if regexp(x, kwargs['regexp'], 'match'):
  134. vars.append(x)
  135. elif nargin > 0:
  136. for x in __ompc_whos__.keys():
  137. var = __ompc_whos__.get(x)
  138. if var is not None:
  139. vars.append(var)
  140. else:
  141. vars = __ompc_whos__.keys()
  142. if nargout > 0:
  143. # return as cell
  144. return mcellarray(vars)
  145. # print the results
  146. _print("Your variables are:")
  147. _print(vars)
  148. def whos(*args,**kwargs):
  149. """Pretty-print a list of variables in the current workspace."""
  150. for var in who(*args, **kwargs):
  151. _print(var, var.shape, var.size, var.dtype)
  152. return
  153. # io
  154. # look at http://mien.sourceforge.net/ they have a mat file reader too
  155. # also http://abel.ee.ucla.edu/cvxopt/examples/extra-utilities/matfile.py/view
  156. def load(fname, *args):
  157. import inspect
  158. a = _S.io.loadmat(fname, *args)
  159. f = inspect.currentframe()
  160. for var, val in a.iteritems():
  161. if var[:2] == '__': continue
  162. if args:
  163. if var in args:
  164. f.f_back.f_globals[var] = val
  165. else:
  166. f.f_back.f_globals[var] = val
  167. def save(fname, *args):
  168. import inspect
  169. f = inspect.currentframe()
  170. d = {}
  171. for var in args:
  172. d[var] = f.f_back.f_globals[var]
  173. _S.io.savemat(fname, d)
  174. def exist(name,tp=None):
  175. """ Return
  176. 1 if the name exists as a variable,
  177. 2 if the name is a file of a function file on path,
  178. 3 if the name is a binary function,
  179. 4 is not returned at the moment,
  180. 5 if the name is a built-in function,
  181. 6 if there is a .pyc file with the name,
  182. 7 if the name is a directory,
  183. 8 id the name belongs to a Python instance.
  184. Second argument kind=['builtin'|'class'|'dir'|'file'|'var']
  185. """
  186. from os.path import exists, isdir
  187. if tp is None:
  188. if name in __ompc_whos__: return 1
  189. elif exists(name): return 2
  190. elif name in __ompc_builtin__: return 5
  191. elif isdir(name): return 7
  192. elif any(map(exists, [name, name+'.m'])): return 2
  193. if tp == 'file':
  194. from os.path import exists
  195. return exists(name) and 2 or 0
  196. elif tp == 'class':
  197. # is it a Python instance ?
  198. return (name in globals() and name not in __omcp_whos__) \
  199. and 8 or 0
  200. elif tp == 'dir':
  201. return isdir(name) and 7 or 0
  202. elif tp == 'builtin':
  203. return name in __ompc_builtin__ and 5 or 0
  204. elif tp == 'var':
  205. return name in __omcp_whos__ and 1 or 0
  206. return 0
  207. class _mvar:
  208. def __init__(self, name):
  209. """
  210. """
  211. __ompc_whos__[_get_argout_name()] = self
  212. class _marray_numpy(_mvar):
  213. def __init__(self,a):
  214. _mvar.__init__(self)
  215. self._array = a
  216. def __len__(self):
  217. return len(self._str)
  218. def __call__(self,i):
  219. if isinstance(i,_mslice):
  220. return self._array[i.__slice_base0__()]
  221. elif i is _MEND:
  222. i = len(self._str)
  223. if i < 1 or i > len(self._str):
  224. raise IndexError
  225. return self._str[i-1]
  226. def __str__(self):
  227. return self._str
  228. def __repr__(self):
  229. return repr(self._str)
  230. class mcellarray(_mvar, list):
  231. def __init__(self, *args, **kwargs):
  232. list.__init__(self, *args, **kwargs)
  233. _mvar.__init__(self, _get_argout_name())
  234. # CHOOSE one of the implementations of array
  235. marray = _marray_numpy
  236. # __getslice__ is deprecated, don't even try, push people to higher Python
  237. class _m_array(_mvar):
  238. def __getitem__(self,i):
  239. if len(i) > 1:
  240. raise IndexError('String is 1D.')
  241. if isinstance(ins,slice):
  242. self.__init__(i.start,i.stop,i.step)
  243. elif isinstance(ins,int):
  244. self.__init__(i,i,1)
  245. else:
  246. raise IndexError('Index must be slice or integer.')
  247. return slice(self._start,self._stop,self._step)
  248. class _mtype(object):
  249. def __init__(self,*args):
  250. # the same parameters as zeros, a list of sizes and 'class'
  251. if type(args[-1]) is str:
  252. tp = i.pop()
  253. pass
  254. class _mtype_meta(object):
  255. def __new__(cls,names,bases,dict):
  256. return object.__new__(cls)
  257. def __getitem__(cls,i):
  258. if i is None:
  259. return _mtype([])
  260. elif operator.isSequenceType(i):
  261. # split into rows by ';'
  262. pass
  263. return _mtype(*i)
  264. class m_:
  265. __metaclass__ = _mtype_meta
  266. pass
  267. class _mslice:
  268. def __init__(self,start=0,stop=None,step=1):
  269. self._start = int(start)
  270. # dN = 1e-9
  271. # if step < 0:
  272. # self._stop = stop - (-dN > step/2.0 and dN or step/2.0)
  273. # else:
  274. # self._stop = stop + ( dN < step/2.0 and dN or step/2.0)
  275. if step < 0: self._stop = int(stop) - 1
  276. else: self._stop = stop + 1
  277. self._step = step
  278. def __getitem__(self,i):
  279. if len(i) > 1:
  280. raise IndexError('String is 1D.')
  281. if isinstance(ins,slice):
  282. self.__init__(i.start,i.stop,i.step)
  283. elif isinstance(ins,int):
  284. self.__init__(i,i,1)
  285. else:
  286. raise IndexError('Index must be slice or integer.')
  287. return slice(self._start,self._stop,self._step)
  288. def __slice__(self):
  289. # FIXME, will this work?, with the dN probably yes
  290. return slice(self._start,self._stop,self._step)
  291. def __slice_base0__(self):
  292. # FIXME, will this work?, with the dN probably yes
  293. stop = self._stop-1
  294. if stop < 0: stop = None
  295. return slice(self._start-1,stop,self._step)
  296. class _mstr(_mvar):
  297. def __init__(self,s):
  298. _mvar.__init__(self)
  299. self._str = s
  300. def __len__(self):
  301. return len(self._str)
  302. def __call__(self,i):
  303. if isinstance(i,_mslice):
  304. return self._str[i.__slice_base0__()]
  305. elif i is _MEND:
  306. i = len(self._str)
  307. if i < 1 or i > len(self._str):
  308. raise IndexError
  309. #raise error('String:')
  310. return self._str[i-1]
  311. def __str__(self):
  312. return self._str
  313. def __repr__(self):
  314. return repr(self._str)
  315. def fgetl(fid):
  316. return _mstr(fid.readline())
  317. # plotting
  318. def figure(*args,**kwargs):
  319. #arrs, kwargs = __plot_args_matlab(args,kwargs)
  320. fig = _P.figure(args[0])#kwargs)
  321. drawnow()
  322. return fig
  323. def drawnow():
  324. _P.draw()
  325. class GCAManager:
  326. def __formatpy__(self):
  327. #print "getting"
  328. return _P.gca()
  329. def __call__(self,**kwargs):
  330. return _P.gca(**kwargs)
  331. gca = GCAManager()
  332. def set(x,*args,**kwargs):
  333. fmp = getattr(x,'__formatpy__',None)
  334. if fmp is not None:
  335. o = fmp()
  336. args, kwargs = __plot_args_matlab(args,kwargs)
  337. assert len(args) == 0
  338. for k, v in kwargs.items():
  339. try: getattr(o,'set_%s'%k)(v)
  340. except: "Property '%s' not supported."%k
  341. def xlabel(*args,**kwargs):
  342. d, kwargs = __plot_args_matlab(args[1:],kwargs)
  343. return _P.xlabel(args[0], **kwargs)
  344. def ylabel(*args,**kwargs):
  345. d, kwargs = __plot_args_matlab(args[1:],kwargs)
  346. return _P.ylabel(args[0], **kwargs)
  347. def zlabel(*args,**kwargs):
  348. d, kwargs = __plot_args_matlab(args[1:],kwargs)
  349. return _P.zlabel(args[0], **kwargs)
  350. def pause():
  351. from matpy_platform import getch
  352. #print 'Press a key to continue ...'
  353. getch()
  354. # 3d
  355. def surfc(X,Y,Z,*args):
  356. return pcolor(X,Y,Z,*args)
  357. def __plot_args(args,kwargs):
  358. from sets import Set
  359. args = list(args)
  360. arrs = []
  361. colorspec = False
  362. for i in range(len(args)):
  363. arg = args.pop(0)
  364. if type(arg) is _N.ndarray:
  365. sz = size(arg)
  366. if arg.ndim == 2 and any(sz == 1):
  367. arrs += [ arg.reshape(sz.max()) ]
  368. else:
  369. arrs += [ arg ]
  370. elif type(arg) is str:
  371. if not colorspec and len(arg) <= 3 and \
  372. len(Set(arg).intersection('bgrcmykw'+'-.:,o^v<>s+xDd1234hHp|_')) > 0:
  373. arrs += [ arg ]
  374. else:
  375. args.insert(0,arg)
  376. colorspec = True
  377. break
  378. else:
  379. a = array(arg)
  380. if a.ndim == 0:
  381. a.resize(1,1)
  382. #print a
  383. elif a.ndim < 2:
  384. a.resize(len(a),1)
  385. arrs += [a]
  386. kwargs.update( zip([x.lower() for x in args[::2]],args[1::2]) )
  387. return arrs, kwargs
  388. def __plot_args_matlab(args,kwargs):
  389. from sets import Set
  390. args = list(args)
  391. arrs = [[]]
  392. colorspec = False
  393. while args:
  394. arg = args.pop(0)
  395. if type(arg) is _N.ndarray:
  396. if colorspec:
  397. #arrs += [[]]
  398. colorspec = False
  399. sz = size(arg)
  400. if arg.ndim == 2 and any(sz == 1):
  401. arrs[-1] += [ arg.reshape(sz.max()) ]
  402. else:
  403. arrs[-1] += [ arg ]
  404. elif type(arg) is str:
  405. if not colorspec and len(arg) <= 4 and \
  406. len(Set(arg).intersection('bgrcmykw'+'-.:,o^v<>s+xDd1234hHp|_')) > 0:
  407. arrs[-1] += [ arg ]
  408. else:
  409. if arg.lower() in ['XTick','YTick']:
  410. arg = arg.lower() + 's'
  411. args.insert(0,arg)
  412. break
  413. colorspec = True
  414. else:
  415. a = array(arg)
  416. if a.ndim == 0:
  417. print a.shape
  418. a.resize(0,0)
  419. #print a
  420. elif a.ndim < 2:
  421. a.shape = (len(a),1)
  422. arrs[-1] += [a]
  423. kwargs.update( zip([x.lower() for x in args[::2]],args[1::2]) )
  424. return arrs[0], kwargs
  425. def plot(*args,**kwargs):
  426. arrs, kwargs = __plot_args_matlab(args,kwargs)
  427. return _P.plot(*arrs,**kwargs)
  428. def loglog(*args,**kwargs):
  429. arrs, kwargs = __plot_args_matlab(args,kwargs)
  430. return _P.loglog(*arrs,**kwargs)
  431. def plot3(*args,**kwargs):
  432. return plot(args[0],args[1],*args[3:],**kwargs)
  433. # functions
  434. def max(X,Y=[],axis=1):
  435. axis -= 1
  436. tX, tY = X, Y
  437. if _N.iscomplex(X.flat[0]): tX = abs(X)
  438. if len(tY) > 0:
  439. if _N.iscomplex(Y.flat[0]): tY = abs(Y)
  440. return _N.maximum(tX,tY)
  441. else:
  442. nargout = _get_nargout()
  443. print nargout
  444. if nargout == 1:
  445. return _N.max(tX,axis)
  446. elif nargout == 2:
  447. # slow
  448. i = _N.argmax(tX,axis)
  449. return _N.max(tX,axis), i
  450. # i = _N.argmax(tX,axis)
  451. # sh = X.shape
  452. # index = [ slice(0,x,1) for x in sh ]
  453. # if axis == 0:
  454. # index[1] = range(sh[1])
  455. # else:
  456. # index[0] = range(sh[0])
  457. # index[axis] = i
  458. # print index
  459. # return _N.ndarray.__getslice__(index)
  460. else:
  461. raise Exception('too many output vals')
  462. def min(X,Y=[],axis=1):
  463. axis -= 1
  464. tX, tY = X, Y
  465. if _N.iscomplex(X.flat[0]): tX = abs(X)
  466. if len(tY) > 0:
  467. if _N.iscomplex(Y.flat[0]): tY = abs(Y)
  468. return _N.minimum(tX,tY)
  469. else:
  470. nargout = _get_nargout()
  471. print nargout
  472. if nargout == 1:
  473. return _N.min(tX,axis)
  474. elif nargout == 2:
  475. # slow
  476. i = _N.argmin(tX,axis)
  477. return _N.min(tX,axis), i
  478. # i = _N.argmin(tX,axis)
  479. # sh = X.shape
  480. # index = [ slice(0,x,1) for x in sh ]
  481. # if axis == 0:
  482. # index[1] = range(sh[1])
  483. # else:
  484. # index[0] = range(sh[0])
  485. # index[axis] = i
  486. # return _N.ndarray.__getslice__(index)
  487. else:
  488. raise Exception('too many output vals')
  489. def gradient(f,*args):
  490. return _N.array(_N.gradient(f,*args))
  491. gradient.__doc__ = _N.gradient.__doc__
  492. def diff(x,M=[],axis=1):
  493. return diff(x,axis-1)
  494. def fminsearch(fn, guess):
  495. return _S.optimize.fmin(fn,guess)
  496. def quad(fn, a, b):
  497. return _S.integrate.quad(fn,a,b)[0]
  498. def spline(x,y,xx=None):
  499. x = array(x,'f8')
  500. srep = None
  501. if len(y) > len(x):
  502. # WRONG
  503. # this little hack doesn't seem to help in getting the derivatives
  504. # at the right
  505. dx = 1e-3
  506. x = r_[x[0]-dx,x,x[-1]+dx]
  507. y[0] = y[1] - y[0]*dx
  508. y[-1] = y[-2] + y[-1]*dx
  509. if xx is None:
  510. # WRONG
  511. # this doesn't return what MATLAB would, but can be used with ppval
  512. return _S.interpolate.fitpack.splrep(x,y)
  513. xx = array(xx)
  514. srep = _S.interpolate.fitpack.splrep(x,y,xb=xx.min(),xe=max(xx.max(),x[-1]))
  515. return _S.interpolate.fitpack.splev(xx,srep)
  516. def ppval(pp,xx):
  517. return _S.interpolate.fitpack.splev(xx,pp)
  518. def test_spline():
  519. x = mr_[-4:4]; y = r_[0, .15, 1.12, 2.36, 2.36, 1.46, .49, .06, 0];
  520. pp = spline(x,r_[0, y, 0]);
  521. xx = linspace(-4,4,101);
  522. plot(x,y,'o',xx,ppval(pp,xx),'-');
  523. def spectrogram(x, window, Fs=1, NFFT=256, noverlap=None):
  524. """
  525. Compute a spectrogram of data in x. Data are split into NFFT
  526. length segements and the PSD of each section is computed. The
  527. windowing function window is applied to each segment, and the
  528. amount of overlap of each segment is specified with noverlap
  529. See pdf for more info.
  530. The returned times are the midpoints of the intervals over which
  531. the ffts are calculated
  532. """
  533. if not noverlap:
  534. noverlap = window/2
  535. x = _N.asarray(x)
  536. assert(NFFT>noverlap)
  537. if _N.log(NFFT)/_N.log(2) != int(_N.log(NFFT)/_N.log(2)):
  538. raise ValueError, 'NFFT must be a power of 2'
  539. # zero pad x up to NFFT if it is shorter than NFFT
  540. if len(x)<NFFT:
  541. n = len(x)
  542. x = _N.resize(x, (NFFT,))
  543. x[n:] = 0
  544. # for real x, ignore the negative frequencies
  545. if _N.iscomplex(x): numFreqs=NFFT
  546. else: numFreqs = NFFT//2+1
  547. windowVals = window(ones((NFFT,),x.dtype))
  548. step = NFFT-noverlap
  549. ind = _N.arange(0,len(x)-NFFT+1,step)
  550. n = len(ind)
  551. Pxx = _N.zeros((numFreqs,n), 'f8')
  552. # do the ffts of the slices
  553. for i in range(n):
  554. thisX = x[ind[i]:ind[i]+NFFT]
  555. thisX = windowVals*detrend(thisX)
  556. fx = _N.absolute(fft(thisX))**2
  557. # Scale the spectrum by the norm of the window to compensate for
  558. # windowing loss; see Bendat & Piersol Sec 11.5.2
  559. Pxx[:,i] = _N.divide(fx[:numFreqs], norm(windowVals)**2)
  560. t = 1/Fs*(ind+NFFT/2)
  561. freqs = Fs/NFFT*_N.arange(numFreqs)
  562. return Pxx, freqs, t
  563. # arrays
  564. class _seti:
  565. def __getitem__(self,x):
  566. if type(x) is slice:
  567. s,e,t = (x.start is not None and x.start or 1)-1,x.step-1,x.stop is None and 1 or x.stop
  568. if round(t) != t:
  569. raise Exception('The step must be integer value!')
  570. e += 1e-6*t
  571. return arange(s,e,t)
  572. return array(x)-1
  573. def __getslice__(self,start,stop):
  574. return arange(start-1,stop)
  575. mri_ = _seti()
  576. class _setitem:
  577. def __init__(self,ibase=0):
  578. self.__ibase = ibase
  579. def __len__(self):
  580. return 0
  581. def __getitem__(self,x):
  582. if type(x) is slice:
  583. step = x.stop or 1.
  584. if x.start is None:
  585. return arange(1,x.step+0.1*step,x.stop)
  586. return arange(x.start,x.step+0.1*step,x.stop)
  587. return array(x)
  588. def __getslice__(self,start,stop):
  589. return arange(start,stop+1)
  590. mr_ = _setitem()
  591. def sub2ind(sz, *args):
  592. # index goes rows, cols, then stacking
  593. error("NotImplemented")
  594. _m2d = { 'int8': 'i1' }
  595. def __mtype_to_dtype(x):
  596. return getattr(_m2d, x, 'f8')
  597. def __arraymaker_args(args):
  598. if type(args[-1]) is str:
  599. mtype = args.pop()
  600. return args,__mtype_to_dtype(mtype)
  601. return args, 'f8'
  602. def ones(*args):
  603. args, dtype = __arraymaker_args(args)
  604. return _N.ones(args,dtype)
  605. #@mfunction
  606. def zeros(*args):
  607. args, dtype = __arraymaker_args(args)
  608. return _N.zeros(args,dtype)
  609. #@mfunction
  610. def size(x,ndim=None,ibase=1):
  611. #print '---------> size x is', x
  612. sz = _N.shape(x)
  613. if len(sz) < 1:
  614. if type(x) is str:
  615. sz = (1,len(x))
  616. else:
  617. sz = (1,1)
  618. if len(sz) < 2:
  619. sz = (1,sz[0])
  620. if ndim is not None:
  621. sz = sz[ndim-ibase]
  622. #print 'size', sz, type(sz)
  623. return sz
  624. def isempty(x):
  625. return len(mat(x).A) == 0
  626. def length(x):
  627. return len(x)
  628. # cells
  629. def cell2mat(x):
  630. return mat(x)
  631. # strings
  632. def str2num(x):
  633. r = array([])
  634. try: r = int(x)
  635. except:
  636. try: r = float(x)
  637. except: pass
  638. return r
  639. def num2str(x):
  640. return str(x)
  641. def sprintf(x,*args):
  642. return x%args
  643. def strcmp(a,b):
  644. """Return bool(1) if a and b are identical strings.
  645. Should work on cellarrays and return size-matching array of bools.
  646. Return False on any non-strings.
  647. """
  648. return a == b
  649. def strcmpi(a,b):
  650. return strcmp(a.lower(), b.lower())
  651. strcmpi.__doc__ = strcmp.__doc__ + "The comparison is case-insensitive"
  652. def deblank(s):
  653. """Strip trailing spaces blanks.
  654. """
  655. return
  656. def strtrim(s):
  657. """Takes also cellarrays and applies on all of them.
  658. """
  659. # create a new string that will contain stripped "s"
  660. return s.strip()
  661. def cellstr(c):
  662. """Take all row strings in a char array, strip them of blanks and feed
  663. into a new cellarray.
  664. """
  665. ca = mcellarray()
  666. for x in c:
  667. ca.append(x.strip())
  668. return ca
  669. def isspace(s):
  670. return
  671. def isstr(x):
  672. return type(x) is str
  673. def regexp(s, expr, *args):
  674. """
  675. expr - can be a cell array (list) for multiple searches
  676. Can return the following:
  677. start, end, extents, matches, tokens, names, splits
  678. """
  679. qs = ['start','end','tokenExtents','match','tokens','names','split']
  680. opts = ['matchcase','ignorecase','dotall','dotexceptnewline',
  681. 'stringanchors','lineanchors','literalspacing','freespacing',
  682. 'once','warnings']
  683. ret_qs = [ args for x in args if x in qs ]
  684. options = [ args for x in args if x not in qs ]
  685. # http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/regexp.html
  686. # translate (?<tokenname>) to Python equivalent
  687. flags = re.S
  688. method = re.finditer
  689. if 'match' in ret_qs: re_method = re.finditer
  690. if 'split' in ret_qs: re_metho
  691. if 'matchcase' in options: flags ^= flags&re.I
  692. if 'ignorecase' in options: flags |= re.I
  693. if 'dotexceptnewline' in options: flags ^= flags&re.S
  694. if 'dotall' in options: flags |= re.S
  695. if 'literalspacing' in options: flags ^= flags&re.X
  696. if 'freespacing' in options: flags |= re.X
  697. if 'stringanchors' in options: flags ^= flags&re.M
  698. if 'lineanchors' in options: flags |= re.M
  699. import re
  700. # replace ((?-ismx))
  701. # FIXME
  702. res = re_method(expr,s,flags)
  703. for x in res:
  704. if 'tokens' in ret_qs:
  705. ret_vals.append()
  706. if 'once' in options:
  707. break
  708. return ret_vals
  709. def disp(*args):
  710. # FIXME
  711. for x in args:
  712. _print(x,end='')
  713. _print()