Ejemplo n.º 1
0
    def testForceTask(self):
        '''Test the execution of tasks with -s force'''
        # FIXME This test will not work because there is no frontend in the
        # test so there is no way to pass the --resume option to the kernel
        # to actually rerun the scripts.
        with sos_kernel() as kc:
            # the cell will actually be executed several times
            # with automatic-reexecution
            code = """
%set -v1
%run -s force
[10]
input: for_each={'i': range(1)}
task:
run: expand=True
   echo this is "{i}"
   sleep {i}

[20]
input: for_each={'i': range(2)}
task:
run: expand=True
   echo this aa is "{i}"
   sleep {i}

"""
            # these should be automatically rerun by the frontend
            long_execute(kc=kc, code=code)
            wait_for_idle(kc)
Ejemplo n.º 2
0
    def testPendingTask(self):
        '''Test the execution of tasks with -s force'''
        with sos_kernel() as kc:
            # the cell will actually be executed several times
            # with automatic-reexecution
            code = """
%run -s force -W
[10]
input: for_each={'i': range(2)}
task:
run: expand=True
   echo this is jupyter pending test "{i}"
   sleep  {10+i}

"""
            # these should be automatically rerun by the frontend
            execute(kc=kc, code=code)
            wait_for_idle(kc)
            # check for task?
            execute(kc=kc, code='%tasks')
            res = get_display_data(kc.iopub_channel, 'text/html')
            # get IDs
            # table_localhost_ac755352394584f797cebddf2c0b8ca7"
            tid = res.split('table_localhost_')[-1].split('"')[0]
            # now we have the tid, we can check task info
            execute(kc=kc, code='%taskinfo ' + tid)
            res = get_display_data(kc.iopub_channel, 'text/html')
            self.assertTrue(tid in res)
Ejemplo n.º 3
0
 def testShell(self):
     with sos_kernel() as kc:
         iopub = kc.iopub_channel
         execute(kc=kc, code="!echo ha ha")
         stdout, stderr = get_std_output(iopub)
         self.assertTrue('ha ha' in stdout, "GOT ERROR {}".format(stderr))
         self.assertEqual(stderr, '')
Ejemplo n.º 4
0
    def testRemoteTaskFromJupyter(self):
        '''Test the execution of tasks with -q '''
        from ipykernel.tests.utils import execute
        from sos_notebook.test_utils import sos_kernel, get_display_data
        subprocess.call(['sos', 'purge'])
        with sos_kernel() as kc:
            # the cell will actually be executed several times
            # with automatic-reexecution
            code = """
%run -s force -W -q ts -c ~/docker.yml
[10]
input: for_each={'i': range(2)}
task:
run: expand=True
   echo this is jupyter pending test "{i}"
   sleep  {10+i}

"""
            # these should be automatically rerun by the frontend
            execute(kc=kc, code=code)
            res = get_display_data(kc.iopub_channel, 'text/html')
            # check for task?
            execute(kc=kc, code='%tasks -q ts')
            res = get_display_data(kc.iopub_channel, 'text/html')
            self.assertTrue('table_ts_' in res, 'Got {}'.format(res))
            # get IDs
            # table_localhost_ac755352394584f797cebddf2c0b8ca7"
            tid = res.split('table_ts_')[-1].split('"')[0]
            # now we have the tid, we can check task info
            execute(kc=kc, code='%taskinfo -q ts ' + tid)
            res = get_display_data(kc.iopub_channel, 'text/html')
            self.assertTrue(tid in res, 'expect {} in {}'.format(tid, res))
            # there should be two tasks
            lines = subprocess.check_output(['sos', 'status', '-q', 'ts', '-c', '~/docker.yml']).decode().splitlines()
            self.assertGreaterEqual(len(lines), 2)
 def testCompleter(self):
     with sos_kernel() as kc:
         # match magics
         ins_print = inspect(kc, 'print')['data']['text/plain']
         self.assertTrue('print' in ins_print,
                         'Returned: {}'.format(ins_print))
         wait_for_idle(kc)
         #
         # keywords
         ins_depends = inspect(kc, 'depends:')['data']['text/plain']
         self.assertTrue('dependent targets' in ins_depends,
                         'Returned: {}'.format(ins_depends))
         wait_for_idle(kc)
         #
         execute(kc=kc, code='alpha=5')
         wait_for_idle(kc)
         execute(kc=kc, code='%use Python3')
         wait_for_idle(kc)
         # action
         ins_run = inspect(kc, 'run:')['data']['text/plain']
         self.assertTrue('sos.actions' in ins_run,
                         'Returned: {}'.format(ins_run))
         wait_for_idle(kc)
         #
         ins_alpha = inspect(kc, 'alpha')['data']['text/plain']
         self.assertTrue('5' in ins_alpha, 'Returned: {}'.format(ins_alpha))
         wait_for_idle(kc)
         for magic in ('get', 'run', 'set', 'sosrun', 'rerun', 'toc'):
             ins_magic = inspect(kc, '%' + magic, 2)['data']['text/plain']
             self.assertTrue('usage: %' + magic in ins_magic,
                             'Returned: {}'.format(ins_magic))
         wait_for_idle(kc)
         execute(kc=kc, code='%use SoS')
         wait_for_idle(kc)
Ejemplo n.º 6
0
 def testCompleter(self):
     with sos_kernel() as kc:
         # match magics
         ins_print = inspect(kc, "print")["data"]["text/plain"]
         self.assertTrue("print" in ins_print,
                         "Returned: {}".format(ins_print))
         wait_for_idle(kc)
         #
         # keywords
         ins_depends = inspect(kc, "depends:")["data"]["text/plain"]
         self.assertTrue("dependent targets" in ins_depends,
                         "Returned: {}".format(ins_depends))
         wait_for_idle(kc)
         #
         execute(kc=kc, code="alpha=5")
         wait_for_idle(kc)
         execute(kc=kc, code="%use Python3")
         wait_for_idle(kc)
         # action
         ins_run = inspect(kc, "run:")["data"]["text/plain"]
         self.assertTrue("sos.actions" in ins_run,
                         "Returned: {}".format(ins_run))
         wait_for_idle(kc)
         #
         ins_alpha = inspect(kc, "alpha")["data"]["text/plain"]
         self.assertTrue("5" in ins_alpha, "Returned: {}".format(ins_alpha))
         wait_for_idle(kc)
         for magic in ("get", "run", "set", "sosrun", "toc"):
             ins_magic = inspect(kc, "%" + magic, 2)["data"]["text/plain"]
             self.assertTrue("usage: %" + magic in ins_magic,
                             "Returned: {}".format(ins_magic))
         wait_for_idle(kc)
         execute(kc=kc, code="%use SoS")
         wait_for_idle(kc)
Ejemplo n.º 7
0
    def testPythonToJavaScalars(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            execute(kc=kc,
                    code='''
                import numpy as np
                int1 = 10
                int2 = 1000000000000000000
                int4 = np.intc(20)
                float1 = 0.1
                float2 = 1e+50
                string1 = 'abc'
                bool1 = True
                ''')
            wait_for_idle(kc)

            execute(kc=kc, code='%use Java')
            wait_for_idle(kc)

            execute(kc=kc,
                    code='%get int1 int2 int4 float1 float2 string1 bool1')
            wait_for_idle(kc)

            #Test int1
            execute(kc=kc, code='System.out.println(int1);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), '10')

            #Test int2
            execute(kc=kc, code='System.out.println(int2);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), '1000000000000000000')

            #Test int4
            execute(kc=kc, code='System.out.println(int4);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), '20')

            #Test float1
            execute(kc=kc, code='System.out.println(float1);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip()[:3], '0.1')

            #Test float2
            execute(kc=kc, code='System.out.println(float2);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), '1.0E50')

            #Test string1
            execute(kc=kc, code='System.out.println(string1);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), 'abc')

            #Test bool1
            execute(kc=kc, code='System.out.println(bool1);')
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), 'true')

            execute(kc=kc, code="%use sos")
            wait_for_idle(kc)
Ejemplo n.º 8
0
    def testGetPythonDictWithSpecialKeyFromR(self):
        # Python -> R
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            # create a data frame
            execute(kc=kc,
                    code='''
special_dict = {}
special_dict['11111'] = 1
special_dict['_1111'] = 'a'
special_dict[11112] = 2
special_dict[(1,2)] = 3
''')
            wait_for_idle(kc)
            execute(kc=kc,
                    code='''%use R
%get special_dict
keys = names(special_dict)
''')
            wait_for_idle(kc)
            execute(kc=kc,
                    code='''%use sos
%get keys --from R            
%dict keys
''')
            res = get_result(iopub)['keys']
            for key in ["X11111", "X_1111", "X11112", "X_1__2_"]:
                self.assertTrue(key in res, f"Expecting {key}")
Ejemplo n.º 9
0
 def testMagicUse(self):
     with sos_kernel() as kc:
         iopub = kc.iopub_channel
         execute(kc=kc, code="%use R0 -l sos_r.kernel:sos_R -c #CCCCCC")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc,
                 code="%use R1 -l sos_r.kernel:sos_R -k ir -c #CCCCCC")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc, code="%use R2 -k ir")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc, code="a <- 1024")
         wait_for_idle(kc)
         execute(kc=kc, code="a")
         res = get_display_data(iopub)
         self.assertEqual(res, '[1] 1024')
         execute(kc=kc, code="%use R3 -k ir -l R")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc, code="a <- 233")
         wait_for_idle(kc)
         execute(kc=kc, code="a")
         res = get_display_data(iopub)
         self.assertEqual(res, '[1] 233')
         execute(kc=kc, code="%use R2 -c red")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc, code="a")
         res = get_display_data(iopub)
         self.assertEqual(res, '[1] 1024')
         execute(kc=kc, code="%use sos")
         wait_for_idle(kc)
Ejemplo n.º 10
0
    def testPythonToJavaDataframe(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            execute(kc=kc,
                    code='''
                import numpy as np
                import pandas as pd
                dataframe = pd.DataFrame(np.random.randn(1000,4), columns=list('ABCD'))
                ''')

            wait_for_idle(kc)
            execute(kc=kc, code='%use Java')
            wait_for_idle(kc)
            execute(kc=kc, code='%get dataframe')
            wait_for_idle(kc)

            execute(
                kc=kc,
                code=
                'System.out.println(dataframe.rowCount() * dataframe.columnCount());'
            )
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip(), '4000')

            execute(kc=kc, code="%use sos")
            wait_for_idle(kc)
Ejemplo n.º 11
0
    def testGetPythonDataFrameFromRuby(self):
        # Python -> Ruby
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            # create a data frame
            execute(kc=kc, code='''
import pandas as pd
import numpy as np
arr = np.random.randn(1000)
arr[::10] = np.nan
df = pd.DataFrame({'column_{0}'.format(i): arr for i in range(10)})
''')
            clear_channels(iopub)
            execute(kc=kc, code="%use Ruby")
            _, stderr = get_std_output(iopub)
            self.assertEqual(stderr, '', "GOT ERROR {}".format(stderr))
            execute(kc=kc, code="%get df")
            wait_for_idle(kc)
            execute(kc=kc, code="df.size()")
            res = get_display_data(iopub)
            self.assertEqual(res, '1000')
            execute(kc=kc, code="df.vectors().to_a().size()")
            res = get_display_data(iopub)
            self.assertEqual(res, '10')
            execute(kc=kc, code="%use sos")
            wait_for_idle(kc)
Ejemplo n.º 12
0
    def testReverseSharedVariable(self):
        '''Test shared variables defined in auxiliary steps'''
        if file_target('a.txt').exists():
            file_target('a.txt').unlink()
        script = r'''
%run B
[A: shared='b', provides='a.txt']
b = 1
run:
    touch a.txt

[B_1]
depends: 'a.txt'

[B_2]
print(b)

'''
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            execute(kc=kc, code=script)
            wait_for_idle(kc)
            self.assertTrue(os.path.isfile('a.txt'))
            execute(kc=kc, code="b")
            res = get_result(iopub)
            self.assertEqual(res, 1)
Ejemplo n.º 13
0
    def testGetPythonDataFrameFromMatlab(self):
        # Python -> Matlab/Matlab
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            # create a data frame
            execute(kc=kc,
                    code='''
import pandas as pd
import numpy as np
import scipy.io as sio
arr = np.random.randn(1000)
arr[::10] = np.nan
df = pd.DataFrame({'column_{0}'.format(i): arr for i in range(10)})
''')
            clear_channels(iopub)
            execute(kc=kc, code="%use MATLAB")
            wait_for_idle(kc)
            #_, stderr = assemble_output(iopub)
            #self.assertEqual(stderr, '')
            execute(kc=kc, code="%get df")
            wait_for_idle(kc)
            execute(kc=kc, code="display(size(df))")
            stdout, _ = assemble_output(iopub)
            self.assertEqual(stdout.strip().split(), ['1000', '10'])
            execute(kc=kc, code="%use sos")
            wait_for_idle(kc)
Ejemplo n.º 14
0
    def testPutMatlabDataToPython(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            execute(kc=kc,
                    code="""
%use MATLAB
null_var = NaN
num_var = 123
num_arr_var = [1, 2, 3]
logic_var = true
logic_arr_var = [true, false, true]
char_var = '1"23'
char_arr_var = ['1'; '2'; '3']
mat_var = [1:3; 2:4]
""")
            wait_for_idle(kc)
            execute(kc=kc,
                    code="""
%dict -r
%put null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var
%dict null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var
""")
            res = get_result(iopub)
            self.assertEqual(res['null_var'], None)
            self.assertEqual(res['num_var'], 123)
            self.assertEqual(list(res['num_arr_var']), [1, 2, 3])
            self.assertEqual(res['logic_var'], True)
            self.assertEqual(res['logic_arr_var'], [True, False, True])
            self.assertEqual(res['char_var'], '1"23')
            #self.assertEqual(res['named_list_var'], {'a': 1, 'b': 2, 'c': '3'})
            self.assertEqual(res['mat_var'].shape, (2, 3))
            self.assertEqual(res['char_arr_var'], ['1', '2', '3'])
            #self.assertEqual(res['recursive_var'], {'a': 1, 'b': {'c': 3, 'd': 'whatever'}})
            execute(kc=kc, code="%use sos")
            wait_for_idle(kc)
Ejemplo n.º 15
0
    def testMagicDebug(self):
        with sos_kernel() as kc:
            # preview variable
            execute(kc=kc, code='''
%debug on
%debug off
''')
            wait_for_idle(kc)
Ejemplo n.º 16
0
 def testKernel(self):
     with sos_kernel() as kc:
         execute(kc=kc, code='a = 1')
         stdout, stderr = assemble_output(kc.get_iopub_msg)
         self.assertEqual(stdout.strip(), '',
                          f'Stdout is not empty, "{stdout}" received')
         self.assertEqual(stderr.strip(), '',
                          f'Stderr is not empty, "{stderr}" received')
Ejemplo n.º 17
0
    def testMagicSessioninfo(self):
        with sos_kernel() as kc:
            # preview variable
            execute(kc=kc, code='''
%use Python3
%use SoS
%sessioninfo
''')
            wait_for_idle(kc)
Ejemplo n.º 18
0
 def testMagicPut(self):
     with sos_kernel() as kc:
         iopub = kc.iopub_channel
         execute(kc=kc, code="%use R")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc, code="a <- 1024")
         wait_for_idle(kc)
         execute(kc=kc, code="%put a")
         wait_for_idle(kc)
         #execute(kc=kc, code="%put __k_k")
         #wait_for_idle(kc)
         execute(kc=kc, code="%use sos")
         wait_for_idle(kc)
         execute(kc=kc, code="a")
         res = get_result(iopub)
         self.assertEqual(res, 1024)
         # strange name
         execute(kc=kc, code="%use R")
         wait_for_idle(kc)
         execute(kc=kc, code=".a.b <- 22")
         wait_for_idle(kc)
         execute(kc=kc, code="%put .a.b")
         wait_for_idle(kc)
         execute(kc=kc, code="%use sos")
         wait_for_idle(kc)
         execute(kc=kc, code="_a_b")
         res = get_result(iopub)
         self.assertEqual(res, 22)
         #
         # test to yet another kernel
         #
         execute(kc=kc, code="%put --to Python3 _a_b")
         wait_for_idle(kc)
         execute(kc=kc, code="%use Python3")
         wait_for_idle(kc)
         execute(kc=kc, code="_a_b")
         res = get_result(iopub)
         self.assertEqual(res, 22)
         #
         execute(kc=kc, code="kkk = 'ast'")
         wait_for_idle(kc)
         execute(kc=kc, code="%put --to R kkk")
         res = get_result(iopub)
         execute(kc=kc, code="%use R")
         wait_for_idle(kc)
         execute(kc=kc, code="kkk <- paste0(kkk, '1')")
         wait_for_idle(kc)
         execute(kc=kc, code="%put --to Python3 kkk")
         wait_for_idle(kc)
         execute(kc=kc, code="%use Python3")
         wait_for_idle(kc)
         execute(kc=kc, code="kkk")
         res = get_result(iopub)
         self.assertEqual(res, 'ast1')
         execute(kc=kc, code="%use sos")
         wait_for_idle(kc)
Ejemplo n.º 19
0
    def testGetPythonDataFromR(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            execute(kc=kc,
                    code='''
null_var = None
num_var = 123
import numpy
import pandas
num_arr_var = numpy.array([1, 2, 3])
logic_var = True
logic_arr_var = [True, False, True]
char_var = '1"23'
char_arr_var = ['1', '2', '3']
list_var = [1, 2, '3']
dict_var = dict(a=1, b=2, c='3')
set_var = {1, 2, '3'}
mat_var = numpy.matrix([[1,2],[3,4]])
recursive_var = {'a': {'b': 123}, 'c': True}
comp_var = 1+2j
seri_var = pandas.Series([1,2,3,3,3,3])
''')
            wait_for_idle(kc)
            execute(kc=kc,
                    code='''\
%use R
%get null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var set_var list_var dict_var recursive_var comp_var seri_var
%dict -r
%put null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var set_var list_var dict_var recursive_var comp_var seri_var
%use sos
seri_var = list(seri_var)
''')
            wait_for_idle(kc)
            execute(kc=kc,
                    code='''\
%dict null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var set_var list_var dict_var recursive_var comp_var seri_var
''')
            res = get_result(iopub)
            self.assertEqual(res['null_var'], None)
            self.assertEqual(res['num_var'], 123)
            self.assertEqual(res['num_arr_var'].tolist(), [1, 2, 3])
            self.assertEqual(res['logic_var'], True)
            self.assertEqual(res['logic_arr_var'], [True, False, True])
            self.assertEqual(res['char_var'], '1"23')
            self.assertEqual(res['char_arr_var'], ['1', '2', '3'])
            self.assertEqual(res['list_var'], [1, 2, '3'])
            self.assertEqual(res['dict_var'], {'a': 1, 'b': 2, 'c': '3'})
            self.assertEqual(res['mat_var'].shape, (2, 2))
            self.assertEqual(res['recursive_var'], {
                'a': {
                    'b': 123
                },
                'c': True
            })
            self.assertEqual(res['comp_var'], 1 + 2j)
            self.assertEqual(res['seri_var'], [1, 2, 3, 3, 3, 3])
Ejemplo n.º 20
0
    def testPutRDataToPython(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            # create a data frame
            execute(kc=kc,
                    code="""\
%use R
null_var = NULL
num_var = 123
num_arr_var = c(1, 2, 3)
logic_var = TRUE
logic_arr_var = c(TRUE, FALSE, TRUE)
char_var = '1\"23'
char_arr_var = c(1, 2, '3')
list_var = list(1, 2, '3')
named_list_var = list(a=1, b=2, c='3')
mat_var = matrix(c(1,2,3,4), nrow=2)
recursive_var = list(a=1, b=list(c=3, d='whatever'))
comp_var = 1+2i
seri_var = setNames(c(1,2,3,3,3,3),c(0:5))
""")
            wait_for_idle(kc)
            execute(kc=kc,
                    code='''\
%put null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var list_var named_list_var recursive_var comp_var seri_var
%use sos
seri_var = list(seri_var)
''')
            wait_for_idle(kc)
            execute(
                kc=kc,
                code=
                "%dict null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var list_var named_list_var recursive_var comp_var seri_var"
            )
            res = get_result(iopub)
            self.assertEqual(res['null_var'], None)
            self.assertEqual(res['num_var'], 123)
            self.assertEqual(res['num_arr_var'], [1, 2, 3])
            self.assertEqual(res['logic_var'], True)
            self.assertEqual(res['logic_arr_var'], [True, False, True])
            self.assertEqual(res['char_var'], '1"23')
            self.assertEqual(res['char_arr_var'], ['1', '2', '3'])
            self.assertEqual(res['list_var'], [1, 2, '3'])
            self.assertEqual(res['named_list_var'], {'a': 1, 'b': 2, 'c': '3'})
            self.assertEqual(res['mat_var'].shape, (2, 2))
            self.assertEqual(res['recursive_var'], {
                'a': 1,
                'b': {
                    'c': 3,
                    'd': 'whatever'
                }
            })
            self.assertEqual(res['comp_var'], 1 + 2j)
            self.assertEqual(res['seri_var'], [1, 2, 3, 3, 3, 3])
            execute(kc=kc, code="%use sos")
            wait_for_idle(kc)
Ejemplo n.º 21
0
 def testCD(self):
     with sos_kernel() as kc:
         iopub = kc.iopub_channel
         execute(kc=kc, code="%cd ..")
         wait_for_idle(kc)
         execute(kc=kc, code="print(os.getcwd())")
         stdout, stderr = get_std_output(iopub)
         self.assertFalse(stdout.strip().endswith('jupyter'))
         self.assertEqual(stderr, '')
         execute(kc=kc, code="%cd jupyter")
Ejemplo n.º 22
0
    def testMagicRevisions(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            # preview variable
            execute(kc=kc, code='''
%revisions
''')
            _, stderr = get_std_output(iopub)
            self.assertEqual(stderr, '',
                             f"Get error {stderr} for magic revisions")
Ejemplo n.º 23
0
    def testMagicPreview(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            #
            execute(kc=kc, code='''
%preview mtcars
%use R
''')
            wait_for_idle(kc)
            execute(kc=kc, code='''%use sos''')
            wait_for_idle(kc)
            # preview figure
            execute(kc=kc,
                    code='''\
%preview -n a.png
R:
    png('a.png')
    plot(0)
    dev.off()
''')
            res = get_display_data(iopub, 'image/png')
            self.assertGreater(len(res), 1000, 'Expect a image {}'.format(res))
            # preview jpg
            execute(kc=kc,
                    code='''\
%preview a.jp*
R:
    jpeg('a.jpg')
    plot(0)
    dev.off()
''')
            res = get_display_data(iopub, 'image/jpeg')
            self.assertGreater(len(res), 1000, 'Expect a image {}'.format(res))
            # preview pdf in iframe (by default)
            execute(kc=kc,
                    code='''
%preview a.pdf
R:
    pdf('a.pdf')
    plot(0)
    dev.off()
''')
            # or png (which requires imagemagick
            wait_for_idle(kc)
            execute(kc=kc, code='''\
%preview a.pdf -s png
''')
            # could return html or image depending on configuration
            res = get_display_data(iopub, ('text/html', 'image/png'))
            self.assertTrue('iframe' in res or len(res) > 1000,
                            'Expect a image {}'.format(res))
            #
            # switch back
            execute(kc=kc, code='%use SoS')
            wait_for_idle(kc)
Ejemplo n.º 24
0
    def testMagicSandbox(self):
        with sos_kernel() as kc:
            # preview variable
            execute(kc=kc,
                    code='''
%sandbox
with open('test_blah.txt', 'w') as tb:
    tb.write('a')
''')
            wait_for_idle(kc)
            self.assertFalse(os.path.exists('test_blah.txt'))
Ejemplo n.º 25
0
    def testGetPythonDataFromMatlab(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            execute(kc=kc,
                    code='''
null_var = None
num_var = 123
import numpy
import scipy.io as sio
num_arr_var = numpy.array([1, 2, 3])
logic_var = True
logic_arr_var = [True, False, True]
float_var = [1.2, 2.1]
char_var = '1"23'
char_arr_var = ['1', '2', '3']
list_var = [1, 2, '3']
dict_var = dict(a=1, b=2, c='3')
set_var = {1, 2, '3'}
mat_var = numpy.matrix([[1,2],[3,4]])
recursive_var = {'a': {'b': 123}, 'c': True}
''')
            wait_for_idle(kc)
            execute(kc=kc,
                    code='''
%use MATLAB
%get null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var set_var list_var dict_var recursive_var float_var
%dict -r
%put null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var set_var list_var dict_var recursive_var float_var
%use sos
%dict null_var num_var num_arr_var logic_var logic_arr_var char_var char_arr_var mat_var set_var list_var dict_var recursive_var float_var
                ''')
            res = get_result(iopub)
            self.assertEqual(res['null_var'], None)
            self.assertEqual(res['num_var'], 123)
            self.assertEqual(list(res['num_arr_var']), [1, 2, 3],
                             "Got {}".format(res['num_arr_var']))
            self.assertEqual(res['logic_var'], True)
            self.assertEqual(res['logic_arr_var'], [True, False, True])
            self.assertEqual(res['char_var'], '1"23')
            self.assertEqual(list(res['float_var']), [1.2, 2.1])
            self.assertEqual(list(res['list_var']), [1, 2, '3'],
                             'Got {}'.format(res['list_var']))
            self.assertEqual(res['dict_var'], {'a': 1, 'b': 2, 'c': '3'})
            self.assertTrue(
                len(res['set_var']) == 3 and '3' in res['set_var']
                and 1 in res['set_var'] and 2 in res['set_var'])

            self.assertEqual(res['mat_var'].shape, (2, 2))
            self.assertEqual(res['recursive_var'], {
                'a': {
                    'b': 123
                },
                'c': True
            })
Ejemplo n.º 26
0
    def testMagicSessionInfo(self):
        with sos_kernel() as kc:
            iopub = kc.iopub_channel
            # preview variable
            execute(kc=kc, code='''
%use Python3
%use SoS
%sessioninfo
''')
            _, stderr = get_std_output(iopub)
            self.assertEqual(stderr, '',
                             f"Get error {stderr} for magic sessioninfo")
Ejemplo n.º 27
0
 def testSubKernel(self):
     with sos_kernel() as kc:
         iopub = kc.iopub_channel
         execute(kc=kc, code="%use R")
         _, stderr = get_std_output(iopub)
         self.assertEqual(stderr, '')
         execute(kc=kc, code="a <- 1024")
         wait_for_idle(kc)
         execute(kc=kc, code="a")
         res = get_display_data(iopub)
         self.assertEqual(res, '[1] 1024')
         execute(kc=kc, code="%use sos")
         wait_for_idle(kc)
Ejemplo n.º 28
0
    def testMagicSave(self):
        with sos_kernel() as kc:
            if os.path.isfile('test.txt'):
                os.remove('test.txt')
            execute(kc=kc,
                    code='''\
%preview ~/test.txt
%save ~/test.txt
a=1
''')
            wait_for_idle(kc)
            with open(os.path.join(os.path.expanduser('~'), 'test.txt')) as tt:
                self.assertEqual(tt.read(), 'a=1\n')
Ejemplo n.º 29
0
    def testMagicMatplotlib(self):
        with sos_kernel() as kc:
            # create a data frame
            execute(kc=kc,
                    code='''
%matplotlib inline
In [59]:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10)
plt.plot(x, np.sin(x), '--', linewidth=2)
plt.show()''')
            wait_for_idle(kc)
Ejemplo n.º 30
0
 def testKernel(self):
     with sos_kernel() as kc:
         execute(kc=kc, code='a = 1')
         stdout, stderr = assemble_output(kc.iopub_channel)
         self.assertEqual(stdout.strip(), '',
                          f'Stdout is not empty, "{stdout}" received')
         self.assertEqual(stderr.strip(), '',
                          f'Stderr is not empty, "{stderr}" received')
         execute(kc=kc, code='%use Python3\n%get a\nprint(a)')
         stdout, stderr = assemble_output(kc.iopub_channel)
         self.assertEqual(stderr.strip(), '',
                          f'Stderr is not empty, "{stderr}" received')
         self.assertEqual(stdout.strip(), '1',
                          f'Stdout should be 1, "{stdout}" received')