##// END OF EJS Templates
Add units option to Rmagic test
Thomas Kluyver -
Show More
@@ -1,82 +1,82 b''
1 import numpy as np
1 import numpy as np
2 from IPython.core.interactiveshell import InteractiveShell
2 from IPython.core.interactiveshell import InteractiveShell
3 from IPython.extensions import rmagic
3 from IPython.extensions import rmagic
4 import nose.tools as nt
4 import nose.tools as nt
5
5
6 ip = get_ipython()
6 ip = get_ipython()
7 ip.magic('load_ext rmagic')
7 ip.magic('load_ext rmagic')
8
8
9
9
10 def test_push():
10 def test_push():
11 rm = rmagic.RMagics(ip)
11 rm = rmagic.RMagics(ip)
12 ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])})
12 ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])})
13 ip.run_line_magic('Rpush', 'X Y')
13 ip.run_line_magic('Rpush', 'X Y')
14 np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X'])
14 np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X'])
15 np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y'])
15 np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y'])
16
16
17 def test_pull():
17 def test_pull():
18 rm = rmagic.RMagics(ip)
18 rm = rmagic.RMagics(ip)
19 rm.r('Z=c(11:20)')
19 rm.r('Z=c(11:20)')
20 ip.run_line_magic('Rpull', 'Z')
20 ip.run_line_magic('Rpull', 'Z')
21 np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z'])
21 np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z'])
22 np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21))
22 np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21))
23
23
24 def test_Rconverter():
24 def test_Rconverter():
25 datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')],
25 datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')],
26 dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')])
26 dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')])
27 ip.user_ns['datapy'] = datapy
27 ip.user_ns['datapy'] = datapy
28 ip.run_line_magic('Rpush', 'datapy')
28 ip.run_line_magic('Rpush', 'datapy')
29
29
30 # test to see if a copy is being made
30 # test to see if a copy is being made
31 v = ip.run_line_magic('Rget', '-d datapy')
31 v = ip.run_line_magic('Rget', '-d datapy')
32 w = ip.run_line_magic('Rget', '-d datapy')
32 w = ip.run_line_magic('Rget', '-d datapy')
33 np.testing.assert_almost_equal(w['x'], v['x'])
33 np.testing.assert_almost_equal(w['x'], v['x'])
34 np.testing.assert_almost_equal(w['y'], v['y'])
34 np.testing.assert_almost_equal(w['y'], v['y'])
35 nt.assert_true(np.all(w['z'] == v['z']))
35 nt.assert_true(np.all(w['z'] == v['z']))
36 np.testing.assert_equal(id(w.data), id(v.data))
36 np.testing.assert_equal(id(w.data), id(v.data))
37 nt.assert_equal(w.dtype, v.dtype)
37 nt.assert_equal(w.dtype, v.dtype)
38
38
39 ip.run_cell_magic('R', ' -d datar datar=datapy', '')
39 ip.run_cell_magic('R', ' -d datar datar=datapy', '')
40
40
41 u = ip.run_line_magic('Rget', ' -d datar')
41 u = ip.run_line_magic('Rget', ' -d datar')
42 np.testing.assert_almost_equal(u['x'], v['x'])
42 np.testing.assert_almost_equal(u['x'], v['x'])
43 np.testing.assert_almost_equal(u['y'], v['y'])
43 np.testing.assert_almost_equal(u['y'], v['y'])
44 nt.assert_true(np.all(u['z'] == v['z']))
44 nt.assert_true(np.all(u['z'] == v['z']))
45 np.testing.assert_equal(id(u.data), id(v.data))
45 np.testing.assert_equal(id(u.data), id(v.data))
46 nt.assert_equal(u.dtype, v.dtype)
46 nt.assert_equal(u.dtype, v.dtype)
47
47
48
48
49 def test_cell_magic():
49 def test_cell_magic():
50
50
51 ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])})
51 ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])})
52 snippet = '''
52 snippet = '''
53 print(summary(a))
53 print(summary(a))
54 plot(x, y, pch=23, bg='orange', cex=2)
54 plot(x, y, pch=23, bg='orange', cex=2)
55 plot(x, x)
55 plot(x, x)
56 print(summary(x))
56 print(summary(x))
57 r = resid(a)
57 r = resid(a)
58 xc = coef(a)
58 xc = coef(a)
59 '''
59 '''
60 ip.run_cell_magic('R', '-i x,y -o r,xc a=lm(y~x)', snippet)
60 ip.run_cell_magic('R', '-i x,y -o r,xc -w 150 -u mm a=lm(y~x)', snippet)
61 np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9])
61 np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9])
62 np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2]))
62 np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2]))
63
63
64
64
65 def test_rmagic_localscope():
65 def test_rmagic_localscope():
66 ip.push({'x':0})
66 ip.push({'x':0})
67 ip.run_line_magic('R', '-i x -o result result <-x+1')
67 ip.run_line_magic('R', '-i x -o result result <-x+1')
68 result = ip.user_ns['result']
68 result = ip.user_ns['result']
69 nt.assert_equal(result[0], 1)
69 nt.assert_equal(result[0], 1)
70
70
71 ip.run_cell('''def rmagic_addone(u):
71 ip.run_cell('''def rmagic_addone(u):
72 %R -i u -o result result <- u+1
72 %R -i u -o result result <- u+1
73 return result[0]''')
73 return result[0]''')
74 ip.run_cell('result = rmagic_addone(1)')
74 ip.run_cell('result = rmagic_addone(1)')
75 result = ip.user_ns['result']
75 result = ip.user_ns['result']
76 nt.assert_equal(result, 2)
76 nt.assert_equal(result, 2)
77
77
78 nt.assert_raises(
78 nt.assert_raises(
79 KeyError,
79 KeyError,
80 ip.run_line_magic,
80 ip.run_line_magic,
81 "R",
81 "R",
82 "-i var_not_defined 1+1")
82 "-i var_not_defined 1+1")
General Comments 0
You need to be logged in to leave comments. Login now