Show More
@@ -1,111 +1,123 b'' | |||||
|
1 | from StringIO import StringIO | |||
|
2 | ||||
1 | import numpy as np |
|
3 | import numpy as np | |
2 | from IPython.core.interactiveshell import InteractiveShell |
|
4 | from IPython.core.interactiveshell import InteractiveShell | |
3 | from IPython.testing.decorators import skip_without |
|
5 | from IPython.testing.decorators import skip_without | |
4 | from IPython.extensions import rmagic |
|
6 | from IPython.extensions import rmagic | |
|
7 | from rpy2 import rinterface | |||
5 | import nose.tools as nt |
|
8 | import nose.tools as nt | |
6 |
|
9 | |||
7 | ip = get_ipython() |
|
10 | ip = get_ipython() | |
8 | ip.magic('load_ext rmagic') |
|
11 | ip.magic('load_ext rmagic') | |
9 |
|
12 | |||
10 |
|
13 | |||
11 | def test_push(): |
|
14 | def test_push(): | |
12 | rm = rmagic.RMagics(ip) |
|
15 | rm = rmagic.RMagics(ip) | |
13 | ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])}) |
|
16 | ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])}) | |
14 | ip.run_line_magic('Rpush', 'X Y') |
|
17 | ip.run_line_magic('Rpush', 'X Y') | |
15 | np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X']) |
|
18 | np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X']) | |
16 | np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y']) |
|
19 | np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y']) | |
17 |
|
20 | |||
18 | def test_push_localscope(): |
|
21 | def test_push_localscope(): | |
19 | """Test that Rpush looks for variables in the local scope first.""" |
|
22 | """Test that Rpush looks for variables in the local scope first.""" | |
20 | ip.run_cell(''' |
|
23 | ip.run_cell(''' | |
21 | def rmagic_addone(u): |
|
24 | def rmagic_addone(u): | |
22 | %Rpush u |
|
25 | %Rpush u | |
23 | %R result = u+1 |
|
26 | %R result = u+1 | |
24 | %Rpull result |
|
27 | %Rpull result | |
25 | return result[0] |
|
28 | return result[0] | |
26 | u = 0 |
|
29 | u = 0 | |
27 | result = rmagic_addone(12344) |
|
30 | result = rmagic_addone(12344) | |
28 | ''') |
|
31 | ''') | |
29 | result = ip.user_ns['result'] |
|
32 | result = ip.user_ns['result'] | |
30 | np.testing.assert_equal(result, 12345) |
|
33 | np.testing.assert_equal(result, 12345) | |
31 |
|
34 | |||
32 | @skip_without('pandas') |
|
35 | @skip_without('pandas') | |
33 | def test_push_dataframe(): |
|
36 | def test_push_dataframe(): | |
34 | from pandas import DataFrame |
|
37 | from pandas import DataFrame | |
35 | rm = rmagic.RMagics(ip) |
|
38 | rm = rmagic.RMagics(ip) | |
36 | df = DataFrame([{'a': 1, 'b': 'bar'}, {'a': 5, 'b': 'foo', 'c': 20}]) |
|
39 | df = DataFrame([{'a': 1, 'b': 'bar'}, {'a': 5, 'b': 'foo', 'c': 20}]) | |
37 | ip.push({'df':df}) |
|
40 | ip.push({'df':df}) | |
38 | ip.run_line_magic('Rpush', 'df') |
|
41 | ip.run_line_magic('Rpush', 'df') | |
|
42 | ||||
|
43 | # This is converted to factors, which are currently converted back to Python | |||
|
44 | # as integers, so for now we test its representation in R. | |||
|
45 | sio = StringIO() | |||
|
46 | rinterface.set_writeconsole(sio.write) | |||
|
47 | try: | |||
|
48 | rm.r('print(df$b[1])') | |||
|
49 | nt.assert_in('[1] bar', sio.getvalue()) | |||
|
50 | finally: | |||
|
51 | rinterface.set_writeconsole(None) | |||
|
52 | ||||
39 | # Values come packaged in arrays, so we unbox them to test. |
|
53 | # Values come packaged in arrays, so we unbox them to test. | |
40 | nt.assert_equal(rm.r('df$b[1]')[0], 'bar') |
|
|||
41 | nt.assert_equal(rm.r('df$a[2]')[0], 5) |
|
54 | nt.assert_equal(rm.r('df$a[2]')[0], 5) | |
42 |
|
||||
43 | missing = rm.r('df$c[1]')[0] |
|
55 | missing = rm.r('df$c[1]')[0] | |
44 | assert np.isnan(missing), missing |
|
56 | assert np.isnan(missing), missing | |
45 |
|
57 | |||
46 | def test_pull(): |
|
58 | def test_pull(): | |
47 | rm = rmagic.RMagics(ip) |
|
59 | rm = rmagic.RMagics(ip) | |
48 | rm.r('Z=c(11:20)') |
|
60 | rm.r('Z=c(11:20)') | |
49 | ip.run_line_magic('Rpull', 'Z') |
|
61 | ip.run_line_magic('Rpull', 'Z') | |
50 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) |
|
62 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) | |
51 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) |
|
63 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) | |
52 |
|
64 | |||
53 | def test_Rconverter(): |
|
65 | def test_Rconverter(): | |
54 | datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], |
|
66 | datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], | |
55 | dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')]) |
|
67 | dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')]) | |
56 | ip.user_ns['datapy'] = datapy |
|
68 | ip.user_ns['datapy'] = datapy | |
57 | ip.run_line_magic('Rpush', 'datapy') |
|
69 | ip.run_line_magic('Rpush', 'datapy') | |
58 |
|
70 | |||
59 | # test to see if a copy is being made |
|
71 | # test to see if a copy is being made | |
60 | v = ip.run_line_magic('Rget', '-d datapy') |
|
72 | v = ip.run_line_magic('Rget', '-d datapy') | |
61 | w = ip.run_line_magic('Rget', '-d datapy') |
|
73 | w = ip.run_line_magic('Rget', '-d datapy') | |
62 | np.testing.assert_almost_equal(w['x'], v['x']) |
|
74 | np.testing.assert_almost_equal(w['x'], v['x']) | |
63 | np.testing.assert_almost_equal(w['y'], v['y']) |
|
75 | np.testing.assert_almost_equal(w['y'], v['y']) | |
64 | nt.assert_true(np.all(w['z'] == v['z'])) |
|
76 | nt.assert_true(np.all(w['z'] == v['z'])) | |
65 | np.testing.assert_equal(id(w.data), id(v.data)) |
|
77 | np.testing.assert_equal(id(w.data), id(v.data)) | |
66 | nt.assert_equal(w.dtype, v.dtype) |
|
78 | nt.assert_equal(w.dtype, v.dtype) | |
67 |
|
79 | |||
68 | ip.run_cell_magic('R', ' -d datar', 'datar=datapy') |
|
80 | ip.run_cell_magic('R', ' -d datar', 'datar=datapy') | |
69 |
|
81 | |||
70 | u = ip.run_line_magic('Rget', ' -d datar') |
|
82 | u = ip.run_line_magic('Rget', ' -d datar') | |
71 | np.testing.assert_almost_equal(u['x'], v['x']) |
|
83 | np.testing.assert_almost_equal(u['x'], v['x']) | |
72 | np.testing.assert_almost_equal(u['y'], v['y']) |
|
84 | np.testing.assert_almost_equal(u['y'], v['y']) | |
73 | nt.assert_true(np.all(u['z'] == v['z'])) |
|
85 | nt.assert_true(np.all(u['z'] == v['z'])) | |
74 | np.testing.assert_equal(id(u.data), id(v.data)) |
|
86 | np.testing.assert_equal(id(u.data), id(v.data)) | |
75 | nt.assert_equal(u.dtype, v.dtype) |
|
87 | nt.assert_equal(u.dtype, v.dtype) | |
76 |
|
88 | |||
77 |
|
89 | |||
78 | def test_cell_magic(): |
|
90 | def test_cell_magic(): | |
79 |
|
91 | |||
80 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) |
|
92 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) | |
81 | snippet = ''' |
|
93 | snippet = ''' | |
82 | print(summary(a)) |
|
94 | print(summary(a)) | |
83 | plot(x, y, pch=23, bg='orange', cex=2) |
|
95 | plot(x, y, pch=23, bg='orange', cex=2) | |
84 | plot(x, x) |
|
96 | plot(x, x) | |
85 | print(summary(x)) |
|
97 | print(summary(x)) | |
86 | r = resid(a) |
|
98 | r = resid(a) | |
87 | xc = coef(a) |
|
99 | xc = coef(a) | |
88 | ''' |
|
100 | ''' | |
89 | ip.run_cell_magic('R', '-i x,y -o r,xc -w 150 -u mm a=lm(y~x)', snippet) |
|
101 | ip.run_cell_magic('R', '-i x,y -o r,xc -w 150 -u mm a=lm(y~x)', snippet) | |
90 | np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9]) |
|
102 | np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9]) | |
91 | np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2])) |
|
103 | np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2])) | |
92 |
|
104 | |||
93 |
|
105 | |||
94 | def test_rmagic_localscope(): |
|
106 | def test_rmagic_localscope(): | |
95 | ip.push({'x':0}) |
|
107 | ip.push({'x':0}) | |
96 | ip.run_line_magic('R', '-i x -o result result <-x+1') |
|
108 | ip.run_line_magic('R', '-i x -o result result <-x+1') | |
97 | result = ip.user_ns['result'] |
|
109 | result = ip.user_ns['result'] | |
98 | nt.assert_equal(result[0], 1) |
|
110 | nt.assert_equal(result[0], 1) | |
99 |
|
111 | |||
100 | ip.run_cell('''def rmagic_addone(u): |
|
112 | ip.run_cell('''def rmagic_addone(u): | |
101 | %R -i u -o result result <- u+1 |
|
113 | %R -i u -o result result <- u+1 | |
102 | return result[0]''') |
|
114 | return result[0]''') | |
103 | ip.run_cell('result = rmagic_addone(1)') |
|
115 | ip.run_cell('result = rmagic_addone(1)') | |
104 | result = ip.user_ns['result'] |
|
116 | result = ip.user_ns['result'] | |
105 | nt.assert_equal(result, 2) |
|
117 | nt.assert_equal(result, 2) | |
106 |
|
118 | |||
107 | nt.assert_raises( |
|
119 | nt.assert_raises( | |
108 | NameError, |
|
120 | NameError, | |
109 | ip.run_line_magic, |
|
121 | ip.run_line_magic, | |
110 | "R", |
|
122 | "R", | |
111 | "-i var_not_defined 1+1") |
|
123 | "-i var_not_defined 1+1") |
General Comments 0
You need to be logged in to leave comments.
Login now