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