예제 #1
0
    def test_delete_project(self):
        num_projects = len(mr.list_projects())
        project = mr.get_project(self.project_name)
        mr.delete_project(project)

        project = mr.get_project(self.project_name)
        assert project is None

        all_projects = mr.list_projects()
        assert len(all_projects) == num_projects - 1
    def test_delete_project(self):
        num_projects = len(mr.list_projects())
        project = mr.get_project(self.PROJECT_NAME)
        mr.delete_project(project)

        project = mr.get_project(self.PROJECT_NAME)
        assert project is None

        all_projects = mr.list_projects()
        assert len(all_projects) == num_projects - 1
예제 #3
0
    def test_delete_project(self):
        project_count = len(mr.list_projects())
        project = mr.get_project(self.project_name)
        assert isinstance(project, RestObj)

        mr.delete_project(project)

        project = mr.get_project(self.project_name)
        assert project is None

        all_projects = mr.list_projects()
        assert len(all_projects) == project_count - 1
예제 #4
0
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()
예제 #5
0
    def test_create_performance_definition(self):
        from sasctl.services import model_repository as mr
        from sasctl.services import model_management as mm

        project = mr.get_project(self.PROJECT_NAME)
        # Update project properties
        project['function'] = 'prediction'
        project['targetLevel'] = 'interval'
        project['targetVariable'] = 'Price'
        project['predictionVariable'] = 'var1'
        project = mr.update_project(project)

        mm.create_performance_definition(self.MODEL_NAME, 'Public', 'boston')
예제 #6
0
def register_model(model,
                   name,
                   project,
                   repository=None,
                   input=None,
                   version='latest',
                   files=None,
                   force=False):
    """Register a model in the model repository.

    Parameters
    ----------
    model : swat.CASTable or sklearn.BaseEstimator
        The model to register.  If an instance of ``swat.CASTable`` the table is assumed to hold an ASTORE, which will
        be downloaded and used to construct the model to register.  If a scikit-learn estimator, the model will be
        pickled and uploaded to the registry and score code will be generated for publishing the model to MAS.
    name : str
        Designated name for the model in the repository.
    project : str or dict
        The name or id of the project, or a dictionary representation of the project.
    repository : str or dict, optional
        The name or id of the repository, or a dictionary representation of the repository.  If omitted, the default
        repository will be used.
    input
    version : {'new', 'latest', int}, optional
        Version number of the project in which the model should be created.
    files :
    force : bool, optional
        Create dependencies such as projects and repositories if they do not already exist.

    Returns
    -------
    model : RestObj
        The newly registered model as an instance of ``RestObj``

    Notes
    -----
    If the specified model is a CAS table the model data and metadata will be written to a temporary zip file and then
    imported using model_repository.import_model_from_zip.

    If the specified model is from the Scikit-Learn package, the model will be created using
    model_repository.create_model and any additional files will be uploaded as content.

    Examples
    --------

    """

    # TODO: Create new version if model already exists
    # TODO: Allow file info to be specified
    # TODO: Performance stats

    files = files or []

    # Find the project if it already exists
    p = mr.get_project(project) if project is not None else None

    # Do we need to create the project first?
    create_project = True if p is None and force else False

    if p is None and not create_project:
        raise ValueError("Project '{}' not found".format(project))

    repository = mr.default_repository(
    ) if repository is None else mr.get_repository(repository)

    # Unable to find or create the repo.
    if repository is None:
        raise ValueError("Unable to find repository '{}'".format(repository))

    # If model is a CASTable then assume it holds an ASTORE model.
    # Import these via a ZIP file.
    if 'swat.cas.table.CASTable' in str(type(model)):
        zipfile = utils.create_package_from_astore(model)

        if create_project:
            project = mr.create_project(project, repository)

        model = mr.import_model_from_zip(name,
                                         project,
                                         zipfile,
                                         version=version)
        return model

    # If the model is an scikit-learn model, generate the model dictionary from it and pickle the model for storage
    elif all(
            hasattr(model, attr)
            for attr in ['_estimator_type', 'get_params']):
        # Pickle the model so we can store it
        model_pkl = pickle.dumps(model)
        files.append({
            'name': 'model.pkl',
            'file': model_pkl,
            'role': 'Python Pickle'
        })

        # Extract model properties
        model = _sklearn_to_dict(model)
        model['name'] = name

        # Generate PyMAS wrapper
        try:
            mas_module = from_pickle(model_pkl,
                                     'predict',
                                     input_types=input,
                                     array_input=True)
            assert isinstance(mas_module, PyMAS)

            # Include score code files from ESP and MAS
            files.append({
                'name': 'dmcas_packagescorecode.sas',
                'file': mas_module.score_code(),
                'role': 'Score Code'
            })
            files.append({
                'name': 'dmcas_espscorecode.sas',
                'file': mas_module.score_code(dest='ESP'),
                'role': 'Score Code'
            })

            model['inputVariables'] = [
                var.as_model_metadata() for var in mas_module.variables
                if not var.out
            ]
            model['outputVariables'] = [
                var.as_model_metadata() for var in mas_module.variables
                if var.out
            ]
        except ValueError:
            # PyMAS creation failed, most likely because input data wasn't provided
            warnings.warn(
                'Unable to determine input/output variables.  Model variables will not be specified.'
            )

    else:
        # Otherwise, the model better be a dictionary of metadata
        assert isinstance(model, dict)

    if create_project:
        vars = model.get('inputVariables', []) + model.get(
            'outputVariables', [])
        target_level = 'Interval' if model.get(
            'function') == 'Regression' else None
        project = mr.create_project(project,
                                    repository,
                                    variables=vars,
                                    targetLevel=target_level)

    model = mr.create_model(model, project)

    assert isinstance(model, RestObj)

    # Upload any additional files
    for file in files:
        if isinstance(file, dict):
            mr.add_model_content(model, **file)
        else:
            mr.add_model_content(model, file)

    return model
예제 #7
0
    def test_created_project(self):
        from sasctl.services import model_repository as mr

        project = mr.get_project(PROJECT_NAME)
        assert project.function.lower() == 'classification'
        assert 'Species' in project.eventProbabilityVariable
예제 #8
0
# 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

# Update project properties.  Target variable must be set before performance
# definitions can be created.
project = mr.get_project(project)
project['targetVariable'] = 'Price'
project = mr.update_project(project)

# Instruct the project to look for tables in the "Public" CAS library with
# names starting with "boston_" and use these tables to track model
# performance over time.
mm.create_performance_definition(model_name, 'Public', 'boston')

# Publish the model to the real-time scoring engine
module_lm = publish_model(model_name, 'maslocal')

# Select the first row of testing data
x = X_test.iloc[0, :]

# Call the published module and score the record