<commit_before>import numpy as np
from IPython.core.interactiveshell import InteractiveShell
from IPython.testing.decorators import skip_without
from IPython.extensions import rmagic
import nose.tools as nt

ip = get_ipython()
ip.magic('load_ext rmagic')


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'])

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)

@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')
    # Values come packaged in arrays, so we unbox them to test.
    nt.assert_equal(rm.r('df$b[1]')[0], 'bar')
    nt.assert_equal(rm.r('df$a[2]')[0], 5)
    
    missing = rm.r('df$c[1]')[0]
    assert np.isnan(missing), missing

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))

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
    v = ip.run_line_magic('Rget', '-d datapy')
    w = ip.run_line_magic('Rget', '-d datapy')
    np.testing.assert_almost_equal(w['x'], v['x'])<commit_msg>Fix test for rmagic + pandas DataFrame<commit_after>from StringIO import StringIO

import numpy as np
from IPython.core.interactiveshell import InteractiveShell
from IPython.testing.decorators import skip_without
from IPython.extensions import rmagic
from rpy2 import rinterface
import nose.tools as nt

ip = get_ipython()
ip.magic('load_ext rmagic')


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'])

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)

@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')
    
    # 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)
    
    # 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

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))

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
    v = ip.run_line_magic('Rget', '-d datapy')
    w = ip.run_line_magic('Rget', '-d datapy')
    np.testing.assert_almost_equal(w['x'], v['x'])
{'fraction_non_alphanumeric': 0.18846411804158283, 'fraction_numerical': 0.022579923988374693, 'mean_word_length': 3.8524945770065075, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1199197', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1777, 'n_words': 373}