Show More
@@ -1,43 +1,41 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 |
|
4 | |||
5 | ip = None |
|
5 | ip = get_ipython() | |
6 | def setup(): |
|
6 | ip.magic('load_ext rmagic') | |
7 | global ip |
|
7 | ||
8 | ip = InteractiveShell() |
|
|||
9 | ip.magic('load_ext rmagic') |
|
|||
10 |
|
8 | |||
11 | def test_push(): |
|
9 | def test_push(): | |
12 | rm = rmagic.RMagics(ip) |
|
10 | rm = rmagic.RMagics(ip) | |
13 | ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])}) |
|
11 | ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])}) | |
14 | ip.run_line_magic('Rpush', 'X Y') |
|
12 | ip.run_line_magic('Rpush', 'X Y') | |
15 | np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X']) |
|
13 | 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']) |
|
14 | np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y']) | |
17 |
|
15 | |||
18 | def test_pull(): |
|
16 | def test_pull(): | |
19 | rm = rmagic.RMagics(ip) |
|
17 | rm = rmagic.RMagics(ip) | |
20 | rm.r('Z=c(11:20)') |
|
18 | rm.r('Z=c(11:20)') | |
21 | ip.run_line_magic('Rpull', 'Z') |
|
19 | ip.run_line_magic('Rpull', 'Z') | |
22 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) |
|
20 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) | |
23 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) |
|
21 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) | |
24 |
|
22 | |||
25 | # def test_inline(): |
|
23 | # def test_inline(): | |
26 | # rm = rmagic.RMagics(ip) |
|
24 | # rm = rmagic.RMagics(ip) | |
27 | # c = ip.run_line_magic('Rinline', 'lm(Y~X)$coef') |
|
25 | # c = ip.run_line_magic('Rinline', 'lm(Y~X)$coef') | |
28 | # np.testing.assert_almost_equal(c, [3.2, 0.9]) |
|
26 | # np.testing.assert_almost_equal(c, [3.2, 0.9]) | |
29 |
|
27 | |||
30 | def test_cell_magic(): |
|
28 | def test_cell_magic(): | |
31 |
|
29 | |||
32 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) |
|
30 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) | |
33 | snippet = ''' |
|
31 | snippet = ''' | |
34 | print(summary(a)) |
|
32 | print(summary(a)) | |
35 | plot(X, Y, pch=23, bg='orange', cex=2) |
|
33 | plot(X, Y, pch=23, bg='orange', cex=2) | |
36 | plot(Y, X) |
|
34 | plot(Y, X) | |
37 | print(summary(X)) |
|
35 | print(summary(X)) | |
38 | r = resid(a) |
|
36 | r = resid(a) | |
39 | xc = coef(a) |
|
37 | xc = coef(a) | |
40 | ''' |
|
38 | ''' | |
41 | ip.run_cell_magic('R', '-i x,y -o r,xc a=lm(y~x)', snippet) |
|
39 | ip.run_cell_magic('R', '-i x,y -o r,xc a=lm(y~x)', snippet) | |
42 | np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9]) |
|
40 | np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9]) | |
43 | np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2])) |
|
41 | np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2])) |
General Comments 0
You need to be logged in to leave comments.
Login now