Exemplo n.º 1
0
def test_bugfix_27():
    """NaN values should be converted to null before being sent to MAS

    https://github.com/sassoftware/python-sasctl/issues/27
    """

    import io
    from sasctl.core import RestObj
    from sasctl.services import microanalytic_score as mas
    pd = pytest.importorskip('pandas')

    df = pd.read_csv(io.StringIO('\n'.join([
        u'BAD,LOAN,MORTDUE,VALUE,REASON,JOB,YOJ,DEROG,DELINQ,CLAGE,NINQ,CLNO,DEBTINC',
        u'0,1.0,1100.0,25860.0,39025.0,HomeImp,Other,10.5,0.0,0.0,94.36666667,1.0,9.0,',
        u'1,1.0,1300.0,70053.0,68400.0,HomeImp,Other,7.0,0.0,2.0,121.8333333,0.0,14.0,'
    ])))

    with mock.patch('sasctl._services.microanalytic_score.MicroAnalyticScore.get_module') as get_module:
        get_module.return_value = RestObj({
            'name': 'Mock Module',
            'id': 'mockmodule'
        })
        with mock.patch('sasctl._services.microanalytic_score.MicroAnalyticScore.post') as post:
            x = df.iloc[0, :]
            mas.execute_module_step('module', 'step', **x)

    # Ensure we're passing NaN to execute_module_step
    assert pd.isna(x['DEBTINC'])

    # Make sure the value has been converted to None before being serialized to JSON.
    # This ensures that the JSON value will be null.
    json = post.call_args[1]['json']
    inputs = json['inputs']
    debtinc = [i for i in inputs if i['name'] == 'DEBTINC'].pop()
    assert debtinc['value'] is None
Exemplo n.º 2
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
    def test_module_execute(self, request, iris_dataset):
        # Store module name so we can retrieve it in later tests
        module_name = request.config.cache.get('CAS_MODULE_NAME', None)

        x = iris_dataset.drop('Species', axis=1).iloc[0, :]

        response = microanalytic_score.execute_module_step(
            module_name, 'score', **x.to_dict())

        assert all(k in response for k in ('P_Species0', 'P_Species1',
                                           'P_Species2', 'I_Species'))
Exemplo n.º 4
0
def run_inference_for_base64(base64img):
    host = os.getenv('OPENMM_HOST', '')
    user = os.getenv('OPENMM_USER', '')
    password = os.getenv('OPENMM_PASSWORD', '')
    model = os.getenv('OPENMM_MODEL', '')
    try:
        img = np.array(
            Image.open(BytesIO(base64.decodebytes(base64img.encode()))))

        with Session(host, user, password, verify_ssl=False, protocol='http'):
            mod = mas.get_module(model)
            response = mas.execute_module_step(mod,
                                               'score',
                                               image_names='test_image',
                                               image_strings=base64img)
            return transform_openmm_detections(response, img=img)
    except:
        e = sys.exc_info()[0]
        print(e)
Exemplo n.º 5
0
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')
Exemplo n.º 6
0
 def test_module_execute(self):
     response = microanalytic_score.execute_module_step(self.module_name,
                                                        'score',
                                                        Income=1e4,
                                                        Credit_Limit=4000)
     print('done')
Exemplo n.º 7
0
# encoding: utf-8
#
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA.  All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import swat

from sasctl import Session
from sasctl.tasks import register_model, publish_model
from sasctl.services import microanalytic_score as mas

s = swat.CAS('hostname', 'username', 'password')
s.loadactionset('decisionTree')

tbl = s.CASTable('iris')
tbl.decisiontree.gbtreetrain(target='Species',
                             inputs=['SepalLength', 'SepalWidth',
                                     'PetalLength', 'PetalWidth'],
                             savestate='gradboost_astore')

astore = s.CASTable('gradboost_astore')

with Session(s):
    model = register_model(astore, 'Gradient Boosting', 'Iris')
    module = publish_model(model, 'maslocal')
    response = mas.execute_module_step(module, 'score',
                                       SepalLength=5.1,
                                       SepalWidth=3.5,
                                       PetalLength=1.4,
                                       PetalWidth=0.2)