Show More
@@ -1,82 +1,96 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_push_localscope(): | |||
|
18 | """Test that Rpush looks for variables in the local scope first.""" | |||
|
19 | ip.run_cell(''' | |||
|
20 | def rmagic_addone(u): | |||
|
21 | %Rpush u | |||
|
22 | %R result = u+1 | |||
|
23 | %Rpull result | |||
|
24 | return result[0] | |||
|
25 | u = 0 | |||
|
26 | result = rmagic_addone(12344) | |||
|
27 | ''') | |||
|
28 | result = ip.user_ns['result'] | |||
|
29 | np.testing.assert_equal(result, 12345) | |||
|
30 | ||||
17 | def test_pull(): |
|
31 | def test_pull(): | |
18 | rm = rmagic.RMagics(ip) |
|
32 | rm = rmagic.RMagics(ip) | |
19 | rm.r('Z=c(11:20)') |
|
33 | rm.r('Z=c(11:20)') | |
20 | ip.run_line_magic('Rpull', 'Z') |
|
34 | ip.run_line_magic('Rpull', 'Z') | |
21 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) |
|
35 | 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)) |
|
36 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) | |
23 |
|
37 | |||
24 | def test_Rconverter(): |
|
38 | def test_Rconverter(): | |
25 | datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], |
|
39 | datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], | |
26 | dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')]) |
|
40 | dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')]) | |
27 | ip.user_ns['datapy'] = datapy |
|
41 | ip.user_ns['datapy'] = datapy | |
28 | ip.run_line_magic('Rpush', 'datapy') |
|
42 | ip.run_line_magic('Rpush', 'datapy') | |
29 |
|
43 | |||
30 | # test to see if a copy is being made |
|
44 | # test to see if a copy is being made | |
31 | v = ip.run_line_magic('Rget', '-d datapy') |
|
45 | v = ip.run_line_magic('Rget', '-d datapy') | |
32 | w = ip.run_line_magic('Rget', '-d datapy') |
|
46 | w = ip.run_line_magic('Rget', '-d datapy') | |
33 | np.testing.assert_almost_equal(w['x'], v['x']) |
|
47 | np.testing.assert_almost_equal(w['x'], v['x']) | |
34 | np.testing.assert_almost_equal(w['y'], v['y']) |
|
48 | np.testing.assert_almost_equal(w['y'], v['y']) | |
35 | nt.assert_true(np.all(w['z'] == v['z'])) |
|
49 | nt.assert_true(np.all(w['z'] == v['z'])) | |
36 | np.testing.assert_equal(id(w.data), id(v.data)) |
|
50 | np.testing.assert_equal(id(w.data), id(v.data)) | |
37 | nt.assert_equal(w.dtype, v.dtype) |
|
51 | nt.assert_equal(w.dtype, v.dtype) | |
38 |
|
52 | |||
39 | ip.run_cell_magic('R', ' -d datar datar=datapy', '') |
|
53 | ip.run_cell_magic('R', ' -d datar datar=datapy', '') | |
40 |
|
54 | |||
41 | u = ip.run_line_magic('Rget', ' -d datar') |
|
55 | u = ip.run_line_magic('Rget', ' -d datar') | |
42 | np.testing.assert_almost_equal(u['x'], v['x']) |
|
56 | np.testing.assert_almost_equal(u['x'], v['x']) | |
43 | np.testing.assert_almost_equal(u['y'], v['y']) |
|
57 | np.testing.assert_almost_equal(u['y'], v['y']) | |
44 | nt.assert_true(np.all(u['z'] == v['z'])) |
|
58 | nt.assert_true(np.all(u['z'] == v['z'])) | |
45 | np.testing.assert_equal(id(u.data), id(v.data)) |
|
59 | np.testing.assert_equal(id(u.data), id(v.data)) | |
46 | nt.assert_equal(u.dtype, v.dtype) |
|
60 | nt.assert_equal(u.dtype, v.dtype) | |
47 |
|
61 | |||
48 |
|
62 | |||
49 | def test_cell_magic(): |
|
63 | def test_cell_magic(): | |
50 |
|
64 | |||
51 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) |
|
65 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) | |
52 | snippet = ''' |
|
66 | snippet = ''' | |
53 | print(summary(a)) |
|
67 | print(summary(a)) | |
54 | plot(x, y, pch=23, bg='orange', cex=2) |
|
68 | plot(x, y, pch=23, bg='orange', cex=2) | |
55 | plot(x, x) |
|
69 | plot(x, x) | |
56 | print(summary(x)) |
|
70 | print(summary(x)) | |
57 | r = resid(a) |
|
71 | r = resid(a) | |
58 | xc = coef(a) |
|
72 | xc = coef(a) | |
59 | ''' |
|
73 | ''' | |
60 | ip.run_cell_magic('R', '-i x,y -o r,xc a=lm(y~x)', snippet) |
|
74 | ip.run_cell_magic('R', '-i x,y -o r,xc a=lm(y~x)', snippet) | |
61 | np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9]) |
|
75 | 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])) |
|
76 | np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2])) | |
63 |
|
77 | |||
64 |
|
78 | |||
65 | def test_rmagic_localscope(): |
|
79 | def test_rmagic_localscope(): | |
66 | ip.push({'x':0}) |
|
80 | ip.push({'x':0}) | |
67 | ip.run_line_magic('R', '-i x -o result result <-x+1') |
|
81 | ip.run_line_magic('R', '-i x -o result result <-x+1') | |
68 | result = ip.user_ns['result'] |
|
82 | result = ip.user_ns['result'] | |
69 | nt.assert_equal(result[0], 1) |
|
83 | nt.assert_equal(result[0], 1) | |
70 |
|
84 | |||
71 | ip.run_cell('''def rmagic_addone(u): |
|
85 | ip.run_cell('''def rmagic_addone(u): | |
72 | %R -i u -o result result <- u+1 |
|
86 | %R -i u -o result result <- u+1 | |
73 | return result[0]''') |
|
87 | return result[0]''') | |
74 | ip.run_cell('result = rmagic_addone(1)') |
|
88 | ip.run_cell('result = rmagic_addone(1)') | |
75 | result = ip.user_ns['result'] |
|
89 | result = ip.user_ns['result'] | |
76 | nt.assert_equal(result, 2) |
|
90 | nt.assert_equal(result, 2) | |
77 |
|
91 | |||
78 | nt.assert_raises( |
|
92 | nt.assert_raises( | |
79 | KeyError, |
|
93 | KeyError, | |
80 | ip.run_line_magic, |
|
94 | ip.run_line_magic, | |
81 | "R", |
|
95 | "R", | |
82 | "-i var_not_defined 1+1") |
|
96 | "-i var_not_defined 1+1") |
General Comments 0
You need to be logged in to leave comments.
Login now