示例#1
0
def test_publish_and_execute(tmpdir):
    import pickle
    from sasctl.utils.pymas import from_pickle
    from sasctl.services import microanalytic_score as mas

    sklearn = pytest.importorskip('sklearn')
    from sklearn import datasets
    from sklearn.linear_model import LinearRegression
    pd = pytest.importorskip('pandas')
    data = sklearn.datasets.load_boston()
    X = pd.DataFrame(data.data, columns=data.feature_names)
    y = pd.DataFrame(data.target, columns=['Price'])

    lm = LinearRegression()
    lm.fit(X, y)
    pkl = pickle.dumps(lm)
    p = from_pickle(pkl, 'predict', X, array_input=True)

    mas.create_module('sasctl_test', source=p.score_code(), language='ds2')
    x1 = {k.lower(): v for k, v in X.iloc[0, :].items()}
    result = mas.execute_module_step('sasctl_test', 'score', **x1)

    assert result['rc'] == 0
    assert result['var1'] == 24
    assert result['msg'] is None
示例#2
0
def test_create_python_module():
    with mock.patch('sasctl.services.microanalytic_score.post') as post:
        with pytest.raises(ValueError):
            mas.create_module()  # Source code is required

    with mock.patch('sasctl.services.microanalytic_score.post') as post:
        source = '\n'.join(
            ("def testMethod(var1, var2):", "    'Output: out1, out2'",
             "    out1 = var1 + 5", "    out2 = var2.upper()",
             "    return out1, out2"))
        mas.create_module(source=source)

        assert post.call_count == 1
        json = post.call_args[1].get('json', {})
        assert 'text/x-python' == json['type']
        assert 'public' == json['scope']
def test_publish_python_module(caplog):
    from sasctl.services.microanalytic_score import create_module
    import logging
    caplog.set_level(logging.DEBUG, logger='sasctl.core')

    source = '\n'.join(("def myfunction(var1, var2):",
                        "    'Output: out1, out2'", "    out1 = var1 + 5",
                        "    out2 = var2.upper()", "    return out1, out2"))

    r = create_module(source=source, name='sasctl_testmethod')
    assert 'sasctl_testmethod' == r.id
    assert 'public' == r.scope
    def test_create_python_module(self):
        source = '\n'.join(("def myfunction(var1, var2):",
                            "    'Output: out1, out2'",
                            "    out1 = var1 + 5",
                            "    out2 = var2.upper()",
                            "    return out1, out2",
                            "def myfunction2(var1, var2):",
                            "    'Output: out1'",
                            "    return var1 + var2"
                            ))

        r = mas.create_module(source=source, name=self.MODULE_NAME)
        assert self.MODULE_NAME == r.id
        assert 'public' == r.scope
def test_publish_and_execute(tmpdir, boston_dataset):
    import pickle
    from sasctl.utils.pymas import from_pickle
    from sasctl.services import microanalytic_score as mas
    from sklearn.linear_model import LinearRegression

    X = boston_dataset[boston_dataset.columns[:-1]]
    y = boston_dataset[boston_dataset.columns[-1]]

    lm = LinearRegression()
    lm.fit(X, y)
    pkl = pickle.dumps(lm)
    p = from_pickle(pkl, 'predict', X, array_input=True)

    # Generate the score code & publish as a model
    code = p.score_code()
    mas.create_module('sasctl_test', source=code, language='ds2')

    x1 = {k.lower(): v for k, v in X.iloc[0, :].items()}
    result = mas.execute_module_step('sasctl_test', 'predict', **x1)

    assert round(result['var1'], 3) == 30.004

    mas.delete_module('sasctl_test')