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