Skip to content

Commit 9ccf2d7

Browse files
committed
_more tests, fixed arange and floor
1 parent 95e155d commit 9ccf2d7

File tree

12 files changed

+136
-44
lines changed

12 files changed

+136
-44
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ else
1010
endif
1111

1212
all:
13-
python $(MAIN) _tests/test_class1.m
13+
python $(MAIN) _tests/files/test_class1.m
1414

1515
install:
1616
python setup.py install

_tests/files/__init__.py

Whitespace-only changes.

_tests/files/fSpectrum.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function [Sp,vf]=fSpectrum(y,N,fs)
2+
y = y(:)';
3+
vf = (0:N/2)*fs/N ;
4+
vf = (0:floor(N/2))*fs/N ;
5+
Sp = abs(fft(y)).^2 / (N*fs);
6+
Sp = Sp(1:floor(N/2)+1) ;
7+

_tests/files/run_all.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
%% Initialization
2+
clear all; close all; clc; % addpath()
3+
4+
%% Spectrum
5+
6+
dt=0.1
7+
t=0:dt:1;
8+
y=sin(t)
9+
[S,f]=fSpectrum(y,length(y),1/dt);
10+
11+
S_ref=[0.2285364 0.0258482 0.0066528 0.0033719 0.0023203 0.0019575]
12+
f_ref=[0.00000 0.90909 1.81818 2.72727 3.63636 4.54545]

_tests/files/test_Zall_ref.py

Whitespace-only changes.

_tests/files/test_class1.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
classdef MyClass < handle;
2+
% Documentation:
3+
properties
4+
% Public
5+
prop_pub;
6+
end
7+
properties(SetAccess = private, Hidden = true)
8+
prop_priv=-1;
9+
end
10+
methods
11+
function o=MyClass(varargin)
12+
o.pro_pub=1
13+
end
14+
function read(o,value)
15+
o.check_extension();
16+
o.prop_pub=value;
17+
end
18+
end % methods
19+
end % class

_tests/test_common.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,6 @@
99
from matlabparser.parsing_tools import TestParsingTools
1010
from matlabparser.parser import matlablines2python
1111

12-
class TestCommon(unittest.TestCase):
13-
pass
14-
# def test_common(self):
15-
# print('hi')
16-
#def assertEqual(self, first, second, msg=None):
17-
# #print('>',first,'<',' >',second,'<')
18-
# super(TestCommon, self).assertEqual(first, second, msg)
19-
#
20-
#def test_unit(self):
21-
# self.assertEqual(unit ('speed [m/s]'),'m/s' )
22-
# self.assertEqual(unit ('speed [m/s' ),'m/s' ) # ...
23-
# self.assertEqual(no_unit('speed [m/s]'),'speed')
24-
#
25-
#def test_ellude(self):
26-
# self.assertListEqual(ellude_common(['AAA','ABA']),['A','B'])
27-
28-
# # unit test for #25
29-
# S=ellude_common(['A.txt','A_.txt'])
30-
# if any([len(s)<=1 for s in S]):
31-
# raise Exception('[FAIL] ellude common with underscore difference, Bug #25')
32-
3312
# --------------------------------------------------------------------------------}
3413
# ---
3514
# --------------------------------------------------------------------------------{
@@ -38,7 +17,9 @@ class TestMatlab2Python(unittest.TestCase):
3817
def ExecAsserX(self, Lines, Expected, msg=None):
3918
#print('>',first,'<',' >',second,'<')
4019
A=matlablines2python(Lines)
41-
print(A)
20+
#print('')
21+
#print('Matlab:',Lines)
22+
#print('Python:',A)
4223
d={}
4324
exec(A,d)
4425
np.testing.assert_array_equal(d['x'], Expected)
@@ -51,9 +32,16 @@ def test_numpy(self):
5132
# simple
5233
self.ExecAsserX("""x=1""",1)
5334
self.ExecAsserX("""x=ceil(0.5)""",1)
35+
self.ExecAsserX("""x=floor(10/2)""",5)
36+
self.ExecAsserX("""x=floor(11/2)""",5)
37+
self.ExecAsserX("""x=floor(12/2)""",6)
5438
# linspace
5539
self.ExecAsserX("""x=linspace(0,1,2)""",[0,1])
5640
self.ExecAsserX("""x=linspace(0,1,3)""",[0,0.5,1])
41+
self.ExecAsserX("""x=0:0.5:1""",[0,0.5,1])
42+
self.ExecAsserX("""x=0:3""",[0,1,2,3])
43+
self.ExecAsserX("""x=3:-1:1""",[3,2,1])
44+
self.ExecAsserX("""x=1:-0.5:0""",[1,0.5,0])
5745
#zeros, ones, nan
5846
self.ExecAsserX("""x=zeros(3,2)""",np.zeros((3,2)))
5947
self.ExecAsserX("""x=ones(3)""" ,np.ones((3,3)))
@@ -67,8 +55,8 @@ def test_numpy(self):
6755
self.ExecAsserX("""x=sum(eye(1,2),2)""",1)
6856
self.ExecAsserX("""x=sum(eye(1,2),1)""",[1,0])
6957

70-
print('>>>>>>>>>>')
71-
print('>>>>>>>>>>')
58+
#print('>>>>>>>>>>')
59+
#print('>>>>>>>>>>')
7260
#self.Eval(""" """)
7361

7462
if __name__ == '__main__':

_tests/test_files.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import unittest
2+
import sys
3+
import os
4+
import numpy as np
5+
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
6+
7+
from matlabparser import *
8+
from matlabparser import parser as mparser
9+
10+
# --------------------------------------------------------------------------------}
11+
# --- Convert matlab files to python files
12+
# --------------------------------------------------------------------------------{
13+
14+
class options:
15+
def __init__(self,output=None):
16+
self.numbers=False
17+
self.no_comments=False
18+
self.no_resolve=False
19+
self.smop=False
20+
self.output=output
21+
22+
def convert(mfile):
23+
opts = options(mfile.replace('.m','.py'))
24+
#print('>>>>>>')
25+
#print('>>>>>> Converting:',mfile)
26+
# opts.smop=True
27+
mparser.matlab2python(mfile,opts)
28+
29+
def compare_to_ref(mfile):
30+
opts = options(mfile.replace('.m','.py'))
31+
ref_output =mfile.replace('.m','_ref.py')
32+
#print('>>>>>> Converting:',mfile)
33+
mparser.matlab2python(mfile,opts)
34+
#print('>>>>>>')
35+
36+
class TestMatlab2Python(unittest.TestCase):
37+
38+
39+
def test_spectrum(self):
40+
convert('_tests/files/run_all.m')
41+
compare_to_ref('_tests/files/fSpectrum.m')
42+
43+
44+
# --------------------------------------------------------------------------------}
45+
# --- Try to run the converted files
46+
# --------------------------------------------------------------------------------{
47+
48+
class TestZAllConverted(unittest.TestCase):
49+
def test_Zspectrum(self):
50+
import _tests.files
51+
from _tests.files.fSpectrum import fSpectrum
52+
dt = 0.1
53+
t = np.arange(0,1+dt,dt)
54+
y = np.sin(t)
55+
#S,f = fSpectrum(y,len(y),1 / dt)
56+
S_ref = np.array([0.2285364,0.0258482,0.0066528,0.0033719,0.0023203,0.0019575])
57+
f_ref = np.array([0.0,0.90909,1.81818,2.72727,3.63636,4.54545])
58+
#print(S-S_ref)
59+
#print(f-f_ref)
60+
61+
62+
63+
64+
if __name__ == '__main__':
65+
unittest.main()

matlab2python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def main(argv):
1010

1111
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,description='matlab2python')
12-
parser.add_argument("--smop", action="store_true", help=""" omit code generation """)
12+
parser.add_argument("--smop", action="store_true", help=""" use smop backend """)
1313
#parser.add_argument("-A","--no-analysis", action="store_true", help=""" skip analysis """)
1414
#parser.add_argument("-B","--no-backend", action="store_true", help=""" omit code generation """)
1515
parser.add_argument("-C","--no-comments", action="store_true", help=""" discard multiline comments""")

smop/backend.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _backend(self,level=0):
100100

101101
@extend(node.cellarrayref)
102102
def _backend(self,level=0):
103-
return "%s[%s-1]" % (self.func_expr._backend(),
103+
return "%s[%s]" % (self.func_expr._backend(),
104104
self.args._backend())
105105

106106
@extend(node.comment_stmt)
@@ -156,7 +156,7 @@ def _backend(self,level=0):
156156
self.args[2]._backend(),
157157
self.args[1]._backend())
158158
if self.op == ":":
159-
return ":%s" % self.args._backend()
159+
return "arange(%s)" % self.args._backend()
160160

161161
if self.op == "end":
162162
# if self.args:
@@ -217,17 +217,17 @@ def _backend(self,level=0):
217217

218218
@extend(node.func_stmt)
219219
def _backend(self,level=0):
220-
# self.args.append(node.ident("*args"))
221-
# self.args.append(node.ident("**kwargs"))
222-
# varargin = %s.varargin
223-
# nargin = %s.nargin
220+
self.args.append(node.ident("*args"))
221+
self.args.append(node.ident("**kwargs"))
224222
s = """
225223
@function
226224
def %s(%s):
225+
varargin = %s.varargin
226+
nargin = %s.nargin
227227
""" % (self.ident._backend(),
228-
self.args._backend())
229-
# self.ident._backend(),
230-
# self.ident._backend())
228+
self.args._backend(),
229+
self.ident._backend(),
230+
self.ident._backend())
231231
return s
232232

233233
@extend(node.funcall)
@@ -324,7 +324,7 @@ def _backend(self,level=0):
324324
return " + ".join(a._backend() for a in self.args)
325325
else:
326326
#import pdb; pdb.set_trace()
327-
return "np.array([%s])" % self.args[0]._backend()
327+
return "concat([%s])" % self.args[0]._backend()
328328

329329
@extend(node.null_stmt)
330330
def _backend(self,level=0):

0 commit comments

Comments
 (0)