def session(request, credentials): import warnings from six.moves import mock from betamax.fixtures.pytest import _casette_name from sasctl import current_session # Ignore FutureWarnings from betamax to avoid cluttering test results with warnings.catch_warnings(): warnings.simplefilter('ignore') cassette_name = _casette_name(request, parametrized=False) # Need to instantiate a Session before starting Betamax recording, # but sasctl.Session makes requests (which should be recorded) during # __init__(). Mock __init__ to prevent from running and then manually # execute requests.Session.__init__() so Betamax can use the session. with mock.patch('sasctl.core.Session.__init__', return_value=None): recorded_session = Session() super(Session, recorded_session).__init__() with betamax.Betamax(recorded_session).use_cassette( cassette_name, serialize_with='prettyjson') as recorder: recorder.start() # Manually run the sasctl.Session constructor. Mock out calls to # underlying requests.Session.__init__ to prevent hooks placed by # Betamax from being reset. with mock.patch('sasctl.core.requests.Session.__init__'): recorded_session.__init__(**credentials) yield recorded_session recorder.stop() current_session(None)
def test_current_session(): assert current_session() is None # Initial session should automatically become the default with mock.patch('sasctl.core.Session.get_token'): s = Session('example.com', 'user', 'password') assert current_session() == s # Subsequent sessions should not overwrite the default with mock.patch('sasctl.core.Session.get_token'): s2 = Session('example.com', 'user2', 'password') assert current_session() != s2 assert current_session() == s # Explicitly set new current session with mock.patch('sasctl.core.Session.get_token'): s3 = current_session('example.com', 'user3', 'password') assert current_session() == s3 # Explicitly change current session with mock.patch('sasctl.core.Session.get_token'): s4 = Session('example.com', 'user4', 'password') current_session(s4) assert 'user4' == current_session().user with mock.patch('sasctl.core.Session.get_token'): with Session('example.com', 'user5', 'password') as s5: with Session('example.com', 'user6', 'password') as s6: assert current_session() == s6 assert current_session() != s5 assert current_session().user == 'user6' assert current_session().user == 'user5' assert current_session().user == 'user4'
def test_swat_connection_reuse(): import base64 swat = pytest.importorskip('swat') HOST = 'example.com' PORT = 8777 PROTOCOL = 'https' USERNAME = '******' PASSWORD = '******' mock_cas = mock.Mock(spec=swat.CAS) mock_cas._hostname = 'casserver.com' mock_cas._sw_connection = mock.Mock( spec=swat.cas.rest.connection.REST_CASConnection) mock_cas._sw_connection._auth = base64.b64encode( (USERNAME + ':' + PASSWORD).encode()) mock_cas.get_action.return_value = lambda: swat.cas.results.CASResults( port=PORT, protocol=PROTOCOL, restPrefix='/cas-shared-default-http', virtualHost=HOST) with mock.patch('sasctl.core.Session.get_token'): with Session(mock_cas) as s: # Should reuse port # from SWAT connection # Should query CAS to find the HTTP connection _settings assert HOST == s._settings['domain'] assert PORT == s._settings['port'] assert PROTOCOL == s._settings['protocol'] assert USERNAME == s._settings['username'] assert PASSWORD == s._settings['password'] assert '{}://{}:{}/test'.format(PROTOCOL, HOST, PORT) == s._build_url('/test') with Session(HOST, username=USERNAME, password=PASSWORD, protocol=PROTOCOL, port=PORT) as s: assert HOST == s._settings['domain'] assert PORT == s._settings['port'] assert PROTOCOL == s._settings['protocol'] assert USERNAME == s._settings['username'] assert PASSWORD == s._settings['password'] assert '{}://{}:{}/test'.format(PROTOCOL, HOST, PORT) == s._build_url('/test') with Session(HOST, username=USERNAME, password=PASSWORD, protocol=PROTOCOL) as s: assert HOST == s._settings['domain'] assert s._settings[ 'port'] is None # Let Requests determine default port assert PROTOCOL == s._settings['protocol'] assert USERNAME == s._settings['username'] assert PASSWORD == s._settings['password'] assert '{}://{}/test'.format(PROTOCOL, HOST) == s._build_url('/test')
def test_ssl_context(): import os from sasctl.core import SSLContextAdapter if 'CAS_CLIENT_SSL_CA_LIST' in os.environ: del os.environ['CAS_CLIENT_SSL_CA_LIST'] if 'REQUESTS_CA_BUNDLE' in os.environ: del os.environ['REQUESTS_CA_BUNDLE'] # Should default to SSLContextAdapter if no certificate paths are set with mock.patch('sasctl.core.Session.get_token', return_value='token'): s = Session('hostname', 'username', 'password') assert isinstance(s.get_adapter('https://'), SSLContextAdapter) # If only the Requests env variable is set, it should be used os.environ['REQUESTS_CA_BUNDLE'] = 'path_for_requests' with mock.patch('sasctl.core.Session.get_token', return_value='token'): s = Session('hostname', 'username', 'password') assert 'CAS_CLIENT_SSL_CA_LIST' not in os.environ assert not isinstance(s.get_adapter('https://'), SSLContextAdapter) # If SWAT env variable is set, it should override the Requests variable os.environ['CAS_CLIENT_SSL_CA_LIST'] = 'path_for_swat' with mock.patch('sasctl.core.Session.get_token', return_value='token'): s = Session('hostname', 'username', 'password') assert os.environ['CAS_CLIENT_SSL_CA_LIST'] == os.environ[ 'REQUESTS_CA_BUNDLE'] assert not isinstance(s.get_adapter('https://'), SSLContextAdapter)
def test_kerberos(): with mock.patch('sasctl.core.Session._get_token_with_kerberos', return_value='token'): s = Session('hostname') assert s.auth.token == 'token' s = Session('hostname', 'username') assert s.auth.token == 'token' s = Session('hostname', 'username@REALM') assert s.auth.token == 'token'
def test_as_swat(): """Verify correct parameters passed to CAS constructor.""" _ = pytest.importorskip('swat') HOST = 'example.com' USERNAME = '******' PASSWORD = '******' with mock.patch('sasctl.core.Session.get_token'): with Session(HOST, USERNAME, PASSWORD) as s: with mock.patch('swat.CAS') as CAS: # Verify default parameters were passed _ = s.as_swat() CAS.assert_called_with(hostname='https://%s/cas-shared-default-http/' % HOST, username=USERNAME, password=PASSWORD) # Verify connection to a non-default CAS instance SERVER_NAME = 'my-cas-server' _ = s.as_swat(SERVER_NAME) CAS.assert_called_with(hostname='https://%s/%s-http/' % (HOST, SERVER_NAME), username=USERNAME, password=PASSWORD) # Verify default parameters can be overridden _ = s.as_swat(username='******', password=None) CAS.assert_called_with(hostname='https://%s/cas-shared-default-http/' % HOST, username='******', password=None)
def test_authentication_failure(): from sasctl.exceptions import AuthenticationError with mock.patch('sasctl.core.Session.request') as request: request.return_value.status_code = 401 with pytest.raises(AuthenticationError): Session('hostname', 'username', 'password')
def run_build(args): # Read configuration logging.info('Read config file.') with open(CONFIGPATH, "r") as cf: config = yaml.load(cf, Loader=yaml.FullLoader) logging.info('Set running variables') projname = args.project_name modelcontent = [ args.requirements, args.train_script, args.configfile, args.score_script ] hostname = config['workflow']['build']['hostname'] username = config['workflow']['build']['username'] password = config['workflow']['build']['password'] modelpath = config['workflow']['build']['modelpath'] logging.info('Start Viya Session') with Session(hostname=hostname, username=username, password=password, verify_ssl=False): logging.info('Get the Project object') project = mr.get_project(projname) logging.info('Check for the Champion Model') try: 'championModelName' in project.keys() except KeyError as errorkey: print(errorkey) else: logging.info('Get the champion id') championModelName = project['championModelName'] champion_id = mr.get_model(championModelName)['id'] logging.info('Get the content list') content_list = get( f"/modelRepository/models/{champion_id}/contents") logging.info('Check for file') for file in modelcontent: try: file in content_list except IndexError as errorindex: print(errorindex) else: # Write down target file for item in content_list: if item['name'] == file: logging.info(f'Get {file}') filestr = get( f"/modelRepository/models/{champion_id}/contents/{item['id']}/content" ) outfile = open(os.path.join(modelpath, file), 'w') outfile.write(filestr) logging.info(f'{file} stored.') outfile.close()
def test_verify_ssl(): with mock.patch('sasctl.core.Session.get_token', return_value='token'): # Should verify SSL by default s = Session('hostname', 'username', 'password') assert s.verify == True # Specify true with no issues s = Session('hostname', 'username', 'password', verify_ssl=True) assert s.verify == True # Explicitly disable SSL verification s = Session('hostname', 'username', 'password', verify_ssl=False) assert s.verify == False # Reuse SWAT env variable, if specified os.environ['SSLREQCERT'] = 'NO' s = Session('hostname', 'username', 'password') assert s.verify == False os.environ['SSLREQCERT'] = 'no' s = Session('hostname', 'username', 'password') assert s.verify == False os.environ['SSLREQCERT'] = 'false' s = Session('hostname', 'username', 'password') assert s.verify == False # Explicit should take precedence over environment variables s = Session('hostname', 'username', 'password', verify_ssl=True) assert s.verify == True
def main(args=None): """Main entry point when executed as a command line utility.""" from sasctl import Session, current_session # Find all services and associated commands services = _find_services() parser = _build_parser(services) args = parser.parse_args(args) if args.verbose is None or args.verbose == 0: lvl = logging.WARNING elif args.verbose == 1: lvl = logging.INFO else: lvl = logging.DEBUG handler = logging.StreamHandler() handler.setLevel(lvl) logging.getLogger('sasctl.core').addHandler(handler) logging.getLogger('sasctl.core').setLevel(lvl) warnings.simplefilter('ignore') func = services[args.service][args.command] kwargs = vars(args).copy() # Remove args that shouldn't be passed to the underlying command for k in ['command', 'service', 'insecure', 'verbose', 'format']: kwargs.pop(k, None) username = os.environ.get('SASCTL_USER_NAME') password = os.environ.get('SASCTL_PASSWORD') server = os.environ.get('SASCTL_SERVER_NAME') if server is None: parser.error( "Hostname must be specified in the 'SASCTL_SERVER_NAME' environment variable." ) verify_ssl = not args.insecure try: # current_session() should never be set when executing from the # command line but it allows us to provide a pre-created session # during testing with current_session() or Session( server, username, password, verify_ssl=verify_ssl): result = func(**kwargs) if isinstance(result, list): pprint([str(x) for x in result]) elif isinstance(result, dict) and args.format == 'json': print(json.dumps(result, indent=2)) else: pprint(result) except RuntimeError as e: parser.error(e)
def test_session_from_url(): """Ensure domains in the format http(s)://hostname.com are handled.""" # Initial session should automatically become the default with mock.patch('sasctl.core.Session.get_token'): s = Session('http://example.com', 'user', 'password') assert s.hostname == 'example.com' assert s._settings['protocol'] == 'http' with mock.patch('sasctl.core.Session.get_token'): s = Session('http://example.com', 'user', 'password', protocol='https') assert s.hostname == 'example.com' assert s._settings['protocol'] == 'https' with mock.patch('sasctl.core.Session.get_token'): s = Session('https://example.com', 'user', 'password', protocol='http') assert s.hostname == 'example.com' assert s._settings['protocol'] == 'http'
def test_str(): import os # Remove any environment variables disabling SSL verification _ = os.environ.pop('SSLREQCERT', None) with mock.patch('sasctl.core.Session.get_token', return_value='token'): s = Session('hostname', 'username', 'password') assert str(s) == "Session(hostname='hostname', username='******', " \ "protocol='https', verify_ssl=True)"
def test_from_authinfo(tmpdir_factory): filename = str(tmpdir_factory.mktemp('tmp').join('authinfo')) HOSTNAME = 'example.com' USERNAME = '******' PASSWORD = '******' with open(filename, 'w') as f: f.write('machine %s login %s password %s' % (HOSTNAME, USERNAME, PASSWORD)) # Get username & password from matching hostname with mock.patch('sasctl.core.Session.get_token'): s = Session('http://example.com', authinfo=filename) assert s.hostname == HOSTNAME assert s.username == USERNAME assert s._settings['password'] == PASSWORD with open(filename, 'w') as f: f.write('host %s user %s password %s' % (HOSTNAME, USERNAME, PASSWORD)) with mock.patch('sasctl.core.Session.get_token'): s = Session('http://example.com', authinfo=filename) assert s.hostname == HOSTNAME assert s.username == USERNAME assert s._settings['password'] == PASSWORD with open(filename, 'w') as f: f.write('host %s user %s password %s\n' % (HOSTNAME, 'Arthur', 'kingofthebrittons')) f.write('host %s user %s password %s\n' % (HOSTNAME, USERNAME, PASSWORD)) with mock.patch('sasctl.core.Session.get_token'): s = Session('http://example.com', username=USERNAME, authinfo=filename) assert s.hostname == HOSTNAME assert s.username == USERNAME assert s._settings['password'] == PASSWORD
def test_log_filtering(caplog): caplog.set_level(logging.DEBUG, logger='sasctl.core.session') HOST = 'example.com' USERNAME = '******' PASSWORD = '******' ACCESS_TOKEN = 'secretaccesstoken' REFRESH_TOKEN = 'secretrefreshtoken' CLIENT_SECRET = 'clientpassword' CONSUL_TOKEN = 'supersecretconsultoken!' sensitive_data = [ PASSWORD, ACCESS_TOKEN, REFRESH_TOKEN, CLIENT_SECRET, CONSUL_TOKEN ] with mock.patch('requests.Session.send') as mocked: # Response to every request with a response that contains sensitive data # Access token should also be used to set session.auth mocked.return_value.status_code = 200 mocked.return_value.raise_for_status.return_value = None mocked.return_value.json.return_value = { 'access_token': ACCESS_TOKEN, 'refresh_token': REFRESH_TOKEN } mocked.return_value.url = 'http://' + HOST mocked.return_value.headers = {} mocked.return_value.body = json.dumps( mocked.return_value.json.return_value) mocked.return_value._content = mocked.return_value.body with Session(HOST, USERNAME, PASSWORD) as s: assert s.auth is not None assert mocked.return_value == s.get('/fakeurl') assert mocked.return_value == s.post( '/fakeurl', headers={'X-Consul-Token': CONSUL_TOKEN}, json={ 'client_id': 'TestClient', 'client_secret': CLIENT_SECRET }) # Correct token should have been set assert 'secretaccesstoken' == s.auth.token # No sensitive information should be contained in the log records assert len(caplog.records) > 0 for r in caplog.records: for d in sensitive_data: assert d not in r.message
def test_new_session(missing_packages): HOST = 'example.com' USERNAME = '******' PASSWORD = '******' # Ensure no dependency on swat required with missing_packages('swat'): with mock.patch('sasctl.core.Session.get_token'): s = Session(HOST, USERNAME, PASSWORD) assert USERNAME == s.user assert HOST == s.settings['domain'] assert 'https' == s.settings['protocol'] assert USERNAME == s.settings['username'] assert PASSWORD == s.settings['password'] # Tests don't reset global variables (_session) so explicitly cleanup current_session(None)
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)
def run_model_tracking (server, user, password, zipath, projectname, modelname): ''' Given server and project params, create a project and register the model in SAS Model manager :param server: :param user: :param password: :param project: :param model: :return: None ''' with Session(hostname=server, username=user, password=password, verify_ssl=False): zipfile = open(zipath, 'rb') model_repository.import_model_from_zip(modelname, projectname, file=zipfile, version='new' ) zipfile.close() return 0
def __init__(self, withLogfile=False, procId=''): os.chdir("/home/sasdemo/Viya_Spark_orchestration/") self.withLogfile = withLogfile self.startTimestamp = int(time.time()) self.log("Starting initialization...") self.protocol = 'http' self.server = 'rusretailviya.rus.sas.com' self.authUri = '/SASLogon/oauth/token' self.user = '******' self.password = '******' self.modelId = '3c3e2fa3-bbd3-4cf7-aa93-834b6c953bb3' r = requests.post( self.protocol + '://' + self.server + self.authUri, params={ 'grant_type': 'password', 'username': self.user, 'password': self.password }, headers={ 'Authorization': 'Basic %s' % base64.b64encode(b'sas.ec:').decode(encoding='utf8') }) self.token = json.loads(r.text)['access_token'] self.log("Acquired REST API token!") self.session = Session(self.server, self.user, self.password, protocol='http') self.wfProcessId = procId self.loadModelFileMeta()
#!/usr/bin/env python # encoding: utf-8 # # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import pandas as pd from sklearn import datasets from sklearn.linear_model import LogisticRegression from sasctl import Session, register_model raw = datasets.load_iris() X = pd.DataFrame(raw.data, columns=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']) y = pd.DataFrame(raw.target, columns=['Species'], dtype='category') y.Species.cat.categories = raw.target_names model = LogisticRegression() model.fit(X, y) with Session('example.com', user='******', password='******'): register_model(model, 'Logistic Regression', project='Iris', force=True)
publishdestination = 'maslocal' data_path = './data/iris.csv' model_filename = 'pylreg.pickle' ######## conn = swat.CAS( #host, port=8777, protocol = 'http', 'localhost', port=5570, ## bug on swat 1.6.0 caslib='casuser', username='******', password='******') #, session = session_id) s = Session(host, 'username', 's3cr3t!', verify_ssl=False) model = pickle.load(open(model_filename, 'rb')) ctbl = conn.read_csv(data_path, casout={'caslib': 'public'}) table = ctbl.to_frame() table ### avoid using variable names with . it will have error with DS2 inputs = table.drop('Species', axis=1) # Need one example of each var for guessing type ### can't have NaN #inputs['DEBTINC'] = .5 outputs = table.columns.to_list()[4]
modelname = 'python_jk_lreg_iris' project = 'iris_os' user = '******' password = '******' #astore_table = 'gb_astore' #astore_caslib = 'public' ################################### ####### Getting astore table ###### s = Session(host, user, password, verify_ssl=False) module = mas.get_module(modelname) module = mas.define_steps(module) steps = mas.list_module_steps(module) steps[0]['id'] steps[0]['links'] print(help(module.predict)) res = module.predict(5.0, 2.0, 3.5, 1.0) res2 = module.predict_proba(5.0, 2.0, 3.5, 1.0)
from sklearn.model_selection import train_test_split from sasctl import Session from sasctl.tasks import register_model, publish_model, update_model_performance from sasctl.services import model_repository as mr from sasctl.services import model_management as mm data = sklearn.datasets.load_boston() X = pd.DataFrame(data.data, columns=data.feature_names) y = pd.DataFrame(data.target, columns=['Price']) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # Establish a session with SAS Viya Session('dsasspre.org', 'robinswu', 'password') project = 'Los Angeles Housing' model_name = 'Los Angeles Regression' # Fit a linear regression model using sci-kit learn lm = LinearRegression() lm.fit(X_train, y_train) # Register the model in SAS Model Manager register_model(lm, model_name, input=X_train, # Use X to determine model inputs project=project, # Register in "Iris" project force=True) # Create project if it doesn't exist
from sasctl import Session from sasctl.services import folders, reports with Session("http://va85.gel.sas.com", "sbxxab", "SASlnx33"): # Retrieve folder path filter = "contains(name, 'Rep')" for folder in folders.list_folders(filter=filter): folder_path = {folder["id"]: folder["name"]} current = folder while id not in folder_path.keys(): if "parentFolderUri" in current.keys(): id = current["parentFolderUri"].split("/")[-1] current = folders.get_folder(id) folder_path[current["id"]] = current["name"] else: id = current["id"].split("/")[-1] path_components = list(folder_path.values()) path_components.reverse() path = "/" + "/".join(path_components) # print(path) # Retrieve report path filter = "contains(name, 'CarsReport')" for report in reports.list_reports(filter=filter): folder = folder.get_folder(childUri=report["id"]) print(folder)
def test_verify_ssl(missing_packages): # Clear environment variables os.environ.pop('SSLREQCERT', None) os.environ.pop('REQUESTS_CA_BUNDLE', None) with mock.patch('sasctl.core.Session.get_token', return_value='token'): # Should verify SSL by default s = Session('hostname', 'username', 'password') assert s.verify == True # Specify true with no issues s = Session('hostname', 'username', 'password', verify_ssl=True) assert s.verify == True # Explicitly disable SSL verification s = Session('hostname', 'username', 'password', verify_ssl=False) assert s.verify == False # Reuse SWAT env variable, if specified os.environ['SSLREQCERT'] = 'NO' s = Session('hostname', 'username', 'password') assert s.verify == False os.environ['SSLREQCERT'] = 'no' s = Session('hostname', 'username', 'password') assert s.verify == False os.environ['SSLREQCERT'] = 'false' s = Session('hostname', 'username', 'password') assert s.verify == False # Explicit should take precedence over environment variables s = Session('hostname', 'username', 'password', verify_ssl=True) assert s.verify == True with missing_packages('urllib3.util.ssl_'): # IP Address validation should work even if urllib3 import fails s = Session('127.0.0.1', 'username', 'password', verify_ssl=True) assert s.verify == True # Clear environment variables os.environ.pop('SSLREQCERT', None) os.environ.pop('REQUESTS_CA_BUNDLE', None) # Ensure correct verify_ssl values are passed to requests module with mock.patch('requests.Session.request') as req: req.return_value.status_code = 200 s = Session('127.0.0.1', 'username', 'password') # Check value passed to verify= parameter assert req.call_args[0][13] == True assert s.verify == True with mock.patch('requests.Session.request') as req: req.return_value.status_code = 200 s = Session('127.0.0.1', 'username', 'password', verify_ssl=False) # Check value passed to verify= parameter assert req.call_args[0][13] == False assert s.verify == False with mock.patch('requests.Session.request') as req: # Explicit verify_ssl= flag should take precedence over env vars os.environ['REQUESTS_CA_BUNDLE'] = 'dummy.crt' s = Session('127.0.0.1', 'username', 'password', verify_ssl=False) # Check value passed to verify= parameter assert req.call_args[0][13] == False assert s.verify == False
SimpleImputer(strategy='median'))]) # Preprocessing categorical variables categorical_transformer = Pipeline( steps=[('imputer', SimpleImputer(strategy='most_frequent') ), ('onehot', OneHotEncoder(handle_unknown='ignore'))]) # Create preprocessing step preprocessor = ColumnTransformer(transformers=[( 'num', numeric_transformer, dm_interval_input), ('cat', categorical_transformer, dm_class_input)]) # Define pipeline steps dm_model = Pipeline( steps=[('preprocessor', preprocessor), ('model', GradientBoostingClassifier())]) fit = dm_model.fit(X_train, y_train) # Score full data scored = dm_model.predict_proba(dm_inputdf) dm_scoreddf = pd.DataFrame( scored, columns=['P_' + dm_dec_target + level for level in dm_classtarget_level]) # Register model with Session('http://sasserver.demo.sas.com', 'username', 'password'): register_model(dm_model, 'OS Node sklearn', 'My MM Project Name', input=X_train)
# Train on the sample model.fit(data=tbl, inputs=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'], target='Species', max_epochs=50, lr=0.001) # Export the model as an ASTORE and get a reference to the new ASTORE table s.deeplearn.dlexportmodel(modelTable=model.model_table, initWeights=model.model_weights, casout='astore_table') astore = s.CASTable('astore_table') # Connect to the SAS environment with Session('dsasspre', 'robinswu', 'password'): # Register the trained model by providing: # - the ASTORE containing the model # - a name for the model # - a name for the project # # NOTE: the force=True option will create the project if it does not exist. model = register_model(astore, 'Deep Learning', 'PGV', force=True) # Publish the model to SAS® Micro Analytic Service (MAS). Specifically to # the default MAS service "maslocal". module = publish_model(model, 'maslocal') # sasctl wraps the published module with Python methods corresponding to # the various steps defined in the module (like "predict"). response = module.score(SepalLength=5.1,
# 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)
""" #Use the object created in the preceeding code snippet in the savestate argument. model_object = conn.CASTable(name='model_CAS_table') """ For example, glm = conn.CASTable(name = 'glmmodel') Use the name specified in the tbl.glm statement in the preceeding example. gbm = conn.CASTable(name = 'gbtreemodel') Use the name specified in the tbl.decisiontree.gbtreetrain statement in the preceeding example. """ #Section d: Registering the model in the model repository #For more information, see the Authentication section of the sasctl User Guide. Session(host, authinfo='~/.authinfo', protocol='http') register_model( model= model_object, # Provide the model object that stores the developed model. name= 'model_name', # Provide a model name that is unique in model repository. project= 'project_name', # Provide the name of the project in which your model will be stored in model repository. force=True) # To create a project with project_name if it doesn't exist. """ For Example register_model(model = glm, name = 'glmmodel', project = 'continuous', force=True)
# Train on the sample model.fit(data=tbl, inputs=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'], target='Species', max_epochs=50, lr=0.001) # Export the model as an ASTORE and get a reference to the new ASTORE table s.deeplearn.dlexportmodel(modelTable=model.model_table, initWeights=model.model_weights, casout='astore_table') astore = s.CASTable('astore_table') # Connect to the SAS environment with Session('hostname', 'username', 'password'): # Register the trained model by providing: # - the ASTORE containing the model # - a name for the model # - a name for the project # # NOTE: the force=True option will create the project if it does not exist. model = register_model(astore, 'Deep Learning', 'Iris', force=True) # Publish the model to SAS® Micro Analytic Service (MAS). Specifically to # the default MAS service "maslocal". module = publish_model(model, 'maslocal') # sasctl wraps the published module with Python methods corresponding to # the various steps defined in the module (like "predict"). response = module.score(SepalLength=5.1,
from pathlib import Path import pzmm import sys host = 'localhost' #host = 'rext01-0093.exnet.sas.com' publishdestination = 'localdocker' modelname = 'R_auto_docker' project_name = 'hmeq_os' algo = 'logistic' s = Session(host, 'sasdemo', 'Orion123', verify_ssl=False) ### reading data form column names data = pd.read_csv('./data/hmeq_score.csv', nrows=5) ### list inputs and outputs inputs = data[0:3].drop(['BAD'], axis=1) # Need one example of each var for guessing type ### can't have NaN inputs['DEBTINC'] = .5 outputs = data.columns.to_list()[0] outputs = pd.DataFrame(columns=[outputs, 'P_BAD0', 'P_BAD1']) outputs.loc[len(outputs)] = [1, 0.5, 0.5] path = Path.cwd()