##// END OF EJS Templates
Merge pull request #3382 from minrk/culldigest...
Merge pull request #3382 from minrk/culldigest cull Session digest history a randomly selected ten percent is culled from the history prevents infinite growth for long running sessions of many messages, while maintaining reasonable replay protection. Size of digest is Configurable. related to #1131

File last commit:

r10018:3a166bae
r10831:d3cef7f1 merge
Show More
test_rmagic.py
123 lines | 3.8 KiB | text/x-python | PythonLexer
Thomas Kluyver
Fix test for rmagic + pandas DataFrame
r10018 from StringIO import StringIO
Jonathan Taylor
added tests of R line and cell magics
r7215 import numpy as np
from IPython.core.interactiveshell import InteractiveShell
Thomas Kluyver
Allow pushing pandas DataFrames to R
r9495 from IPython.testing.decorators import skip_without
Jonathan Taylor
added tests of R line and cell magics
r7215 from IPython.extensions import rmagic
Thomas Kluyver
Fix test for rmagic + pandas DataFrame
r10018 from rpy2 import rinterface
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 import nose.tools as nt
Jonathan Taylor
added tests of R line and cell magics
r7215
Jonathan Taylor
fixing InteractiveShell instantiation in test
r7222 ip = get_ipython()
ip.magic('load_ext rmagic')
Jonathan Taylor
added tests of R line and cell magics
r7215
def test_push():
rm = rmagic.RMagics(ip)
ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])})
ip.run_line_magic('Rpush', 'X Y')
np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X'])
np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y'])
Robert McGibbon
added tests for new feature
r8957 def test_push_localscope():
"""Test that Rpush looks for variables in the local scope first."""
ip.run_cell('''
def rmagic_addone(u):
%Rpush u
%R result = u+1
%Rpull result
return result[0]
u = 0
result = rmagic_addone(12344)
''')
result = ip.user_ns['result']
np.testing.assert_equal(result, 12345)
Thomas Kluyver
Allow pushing pandas DataFrames to R
r9495 @skip_without('pandas')
def test_push_dataframe():
from pandas import DataFrame
rm = rmagic.RMagics(ip)
df = DataFrame([{'a': 1, 'b': 'bar'}, {'a': 5, 'b': 'foo', 'c': 20}])
ip.push({'df':df})
ip.run_line_magic('Rpush', 'df')
Thomas Kluyver
Fix test for rmagic + pandas DataFrame
r10018
# This is converted to factors, which are currently converted back to Python
# as integers, so for now we test its representation in R.
sio = StringIO()
rinterface.set_writeconsole(sio.write)
try:
rm.r('print(df$b[1])')
nt.assert_in('[1] bar', sio.getvalue())
finally:
rinterface.set_writeconsole(None)
Thomas Kluyver
Allow pushing pandas DataFrames to R
r9495 # Values come packaged in arrays, so we unbox them to test.
nt.assert_equal(rm.r('df$a[2]')[0], 5)
missing = rm.r('df$c[1]')[0]
assert np.isnan(missing), missing
Jonathan Taylor
added tests of R line and cell magics
r7215 def test_pull():
rm = rmagic.RMagics(ip)
rm.r('Z=c(11:20)')
ip.run_line_magic('Rpull', 'Z')
np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z'])
np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21))
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 def test_Rconverter():
datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')],
dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')])
ip.user_ns['datapy'] = datapy
ip.run_line_magic('Rpush', 'datapy')
# test to see if a copy is being made
Jonathan Taylor
added Rget, and options to try to return strucutred array
r7276 v = ip.run_line_magic('Rget', '-d datapy')
w = ip.run_line_magic('Rget', '-d datapy')
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 np.testing.assert_almost_equal(w['x'], v['x'])
np.testing.assert_almost_equal(w['y'], v['y'])
nt.assert_true(np.all(w['z'] == v['z']))
np.testing.assert_equal(id(w.data), id(v.data))
nt.assert_equal(w.dtype, v.dtype)
Thomas Kluyver
Fix test of R magic
r9496 ip.run_cell_magic('R', ' -d datar', 'datar=datapy')
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268
Jonathan Taylor
added Rget, and options to try to return strucutred array
r7276 u = ip.run_line_magic('Rget', ' -d datar')
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 np.testing.assert_almost_equal(u['x'], v['x'])
np.testing.assert_almost_equal(u['y'], v['y'])
nt.assert_true(np.all(u['z'] == v['z']))
np.testing.assert_equal(id(u.data), id(v.data))
nt.assert_equal(u.dtype, v.dtype)
Jonathan Taylor
added tests of R line and cell magics
r7215
def test_cell_magic():
ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])})
snippet = '''
print(summary(a))
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 plot(x, y, pch=23, bg='orange', cex=2)
plot(x, x)
print(summary(x))
Jonathan Taylor
added tests of R line and cell magics
r7215 r = resid(a)
xc = coef(a)
'''
Thomas Kluyver
Add units option to Rmagic test
r9007 ip.run_cell_magic('R', '-i x,y -o r,xc -w 150 -u mm a=lm(y~x)', snippet)
Jonathan Taylor
added tests of R line and cell magics
r7215 np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9])
np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2]))
Guy Haskin Fernald
Added functionality to %R and %octave magics so that -i first looks in local...
r8262
def test_rmagic_localscope():
ip.push({'x':0})
ip.run_line_magic('R', '-i x -o result result <-x+1')
result = ip.user_ns['result']
nt.assert_equal(result[0], 1)
ip.run_cell('''def rmagic_addone(u):
%R -i u -o result result <- u+1
return result[0]''')
ip.run_cell('result = rmagic_addone(1)')
result = ip.user_ns['result']
nt.assert_equal(result, 2)
nt.assert_raises(
Jens H. Nielsen
Following #2737 this error is now a name error
r8963 NameError,
Guy Haskin Fernald
Added functionality to %R and %octave magics so that -i first looks in local...
r8262 ip.run_line_magic,
"R",
"-i var_not_defined 1+1")