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