sasctl.utils.pymas package#
Submodules#
sasctl.utils.pymas.core module#
Contains utilities for wrapping Python models in DS2 for publishing.
- class sasctl.utils.pymas.core.PyMAS(target_function, variables, python_source, return_code=None, return_msg=None, func_prefix=None, **kwargs)[source]#
Bases:
object
- Parameters:
target_function (str) – The Python function to be executed.
variables (list of DS2Variable) – The input/ouput variables be declared in the module.
python_source (str) – Additional Python code to be executed during setup.
return_code (bool) – Deprecated. Whether the DS2-generated return code should be included.
return_msg (bool) – Deprecated. Whether the DS2-generated return message should be included.
**kwargs – Passed to
build_wrapper_function()
.
- score_code(input_table=None, output_table=None, columns=None, dest='MAS')[source]#
Generate DS2 score code
- Parameters:
input_table (str) – The name of the table to execute the function against
output_table (str) – The name of the table where execution results will be written
columns (list of str) – Names of the columns from table that will be passed to func
dest (str) – Choose from
{'MAS', 'EP', 'CAS', 'Python'}
. Specifies the publishing destination for the score code to ensure that compatible code is generated.
- Returns:
str – Score code
Changed in version 1.4: Added dest=’Python’ option
- sasctl.utils.pymas.core.build_wrapper_function(func, variables, array_input, name='wrapper', setup=None, return_msg=None)[source]#
Wraps a function to ensure compatibility when called by PyMAS.
PyMAS has strict expectations regarding the format of any function called directly by PyMAS. Isolating the desired function inside a wrapping function provides a simple way to ensure that functions called by PyMAS are compliant.
- Parameters:
func (Callable or str) – Function name or an instance of Function which will be wrapped
variables (list of DS2Variable)
array_input (bool) – Whether variables should be combined into a single array before passing to func
name (str, optional) – Name of the generated wrapper function. Defaults to ‘wrapper’.
setup (Iterable) – Python source code lines to be executed during package setup
return_msg (bool, optional) – Deprecated. Whether error messages will be captured and returned as an additional output variable. Defaults to True.
- Returns:
str – The Python definition for the wrapper function.
Notes
The format for the `# Output: ` is very strict. It must be exactly “# Output: <var>, <var>”. Any changes to spelling, capitalization, punctuation, or spacing will result in an error when the DS2 code is executed.
Changed in version 1.5: Added name parameter.
- sasctl.utils.pymas.core.from_inline(func, input_types=None, array_input=False, return_code=None, return_message=None)[source]#
Creates a PyMAS wrapper to execute the inline python function.
- Parameters:
func (Callable) – A Python function object to be used
input_types (list of type, optional) – The expected type for each input value of func. Can be ommitted if func includes type hints.
array_input (bool) – Whether the function inputs should be treated as an array instead of individual parameters
return_code (bool) – Deprecated. Whether the DS2-generated return code should be included
return_message (bool) – Deprecated. Whether the DS2-generated return message should be included
- Returns:
PyMAS – Generated DS2 code which can be executed in a SAS scoring environment
Changed in version 1.5: Return code and message are disabled by default.
- sasctl.utils.pymas.core.from_pickle(file, func_name=None, input_types=None, array_input=False, return_code=None, return_message=None)[source]#
Create a deployable DS2 package from a Python pickle file.
- Parameters:
file (str or bytes or io.BytesIO) – Pickled object to use. String is assumed to be a path to a picked file, file_like is assumed to be an open file handle to a pickle object, and bytes is assumed to be the raw pickled bytes.
func_name (str) – Name of the target function to call
input_types (pandas.DataFrame, type, list of type, or dict of str, optional) – The expected type for each input value of the target function. Can be omitted if target function includes type hints. If a DataFrame is provided, the columns will be inspected to determine type information. If a single type is provided, all columns will be assumed to be that type, otherwise a list of column types or a dictionary of column_name: type may be provided.
array_input (bool) – Whether the function inputs should be treated as an array instead of individual parameters
return_code (bool) – Deprecated. Whether the DS2-generated return code should be included
return_message (bool) – Deprecated. Whether the DS2-generated return message should be included
- Returns:
PyMAS – Generated DS2 code which can be executed in a SAS scoring environment
Changed in version 1.5: Return code and message are disabled by default.
- sasctl.utils.pymas.core.from_python_file(file, func_name=None, input_types=None, array_input=False, return_code=None, return_message=None)[source]#
Creates a PyMAS wrapper to execute a function defined in an external .py file.
- Parameters:
file (str) – The path to a python source code file
func_name (str) – Name of the target function to call
input_types (list of type, optional) – The expected type for each input value of the target function. Can be ommitted if target function includes type hints.
array_input (bool) – Whether the function inputs should be treated as an array instead of individual parameters
return_code (bool) – Deprecated Whether the DS2-generated return code should be included
return_message (bool) – Deprecated Whether the DS2-generated return message should be included
- Returns:
PyMAS – Generated DS2 code which can be executed in a SAS scoring environment
Changed in version 1.5: Return code and message are disabled by default.
- sasctl.utils.pymas.core.wrap_predict_method(func, variables, **kwargs)[source]#
Create a PyMAS wrapper designed for Sci-kit’s .predict methods.
- Parameters:
func (Callable or str) – Function name or an instance of Function which will be wrapped. Assumed to behave as .predict() methods.
variables (list of DS2Variable) – Input and output variables for the function
**kwargs – Will be passed to
build_wrapper_function()
.
- Returns:
str – the Python definition for the wrapper function.
See also
Added in version 1.5.
- sasctl.utils.pymas.core.wrap_predict_proba_method(func, variables, **kwargs)[source]#
Create a PyMAS wrapper designed for Sci-kit’s .predict_proba methods.
- Parameters:
func (Callable or str) – Function name or an instance of Function which will be wrapped. Assumed to behave as .predict_proba() methods.
variables (list of DS2Variable) – Input and output variables for the function
**kwargs – Will be passed to build_wrapper_function.
- Returns:
str – the Python definition for the wrapper function.
See also
Added in version 1.5.
sasctl.utils.pymas.ds2 module#
The ds2 module contains classes representing DS2 code constructs.
- class sasctl.utils.pymas.ds2.DS2BaseMethod(name, variables, body=None)[source]#
Bases:
object
- property name#
- class sasctl.utils.pymas.ds2.DS2BasePackage(code=None)[source]#
Bases:
object
Defines a DS2 package.
- Parameters:
code (str) – Any code to include in the body of the package.
- Variables:
methods (list) – A collection of
DS2BaseMethod
instances that will be included in the package definition.
Added in version 1.5.
- property id#
Unique identifier generated for the package.
- property name#
Unique name generated for the package.
- sasctl.utils.pymas.ds2.DS2Method(variables, code, target='wrapper')[source]#
Deprecated since version 1.5.
- sasctl.utils.pymas.ds2.DS2Package(variables, code=None, return_code=True, return_message=True, target=None)[source]#
Deprecated since version 1.5: Use DS2PyMASPackage instead.
- class sasctl.utils.pymas.ds2.DS2PyMASMethod(name, variables, python_code, return_code=None, return_message=None, target='wrapper', method_name='score')[source]#
Bases:
DS2BaseMethod
A DS2 method that builds a PyMAS object.
- Parameters:
name (str) – Name of Python model to define
variables
python_code
return_code
return_message
target
method_name
- class sasctl.utils.pymas.ds2.DS2PyMASPackage(code=None)[source]#
Bases:
DS2BasePackage
A DS2 package that uses PyMAS to invoke Python code.
- Parameters:
code
Added in version 1.5.
- add_method(name, target, variables)[source]#
Add a DS2 method that calls a Python function defined by the package.
- Parameters:
name (str) – Name of the DS2 method to create.
target (str) – Name of the Python method to call
variables (list of DS2Variable) – List of input and output variables for the method.
- Returns:
None
- sasctl.utils.pymas.ds2.DS2ScoreMethod(variables, return_code=True, return_message=True, target='wrapper')[source]#
Deprecated since version 1.5.
- class sasctl.utils.pymas.ds2.DS2Thread(variables, table, column_names=None, return_code=True, return_message=True, package=None, method=None)[source]#
Bases:
object
- property id#
- property name#
- class sasctl.utils.pymas.ds2.DS2Variable(*args, **kwargs)[source]#
Bases:
Ds2Variable
- DS2_TYPE_TO_VIYA = {'char': 'string', 'double': 'decimal', 'varchar': 'string'}#
- PY_TYPE_TO_DS2 = {'double': 'double', 'double32': 'double', 'double64': 'double', 'float': 'double', 'float32': 'double', 'float64': 'double', 'int': 'integer', 'int32': 'integer', 'int64': 'integer', 'integer': 'integer', 'integer32': 'integer', 'integer64': 'integer', 'str': 'char', 'string': 'char', 'uint16': 'integer', 'uint32': 'integer', 'uint64': 'integer', 'uint8': 'integer', 'varchar': 'char'}#
- property is_array#
- pymas_statement(python_var_name=None)[source]#
Returns appropriate PyMAS get/set statements to move values between a Python variable and the DS2 variable.
- Parameters:
python_var_name (str) – Python variable name.
- Raises:
- property size#
sasctl.utils.pymas.python module#
- sasctl.utils.pymas.python.ds2_variables(input, output_vars=False, names=None)[source]#
Generate a collection of DS2Variable instances corresponding to the input
- Parameters:
input (Callable or OrderedDict[str, tuple] or OrderedDict[str, type] or pandas.DataFrame) – a function or mapping parameter names to (type, is_output)
output_vars (bool) – Whether or not to treat all variables from input as output variables
names (list of str) – List of variable names to used. If a single string is specified it will be used as a prefix and variable names in the format “prefixNN” will be generated.
- Returns:
list of DS2Variable
Examples
>>> ds2_variables(OrderedDict(a=int, c=float)) [DS2Variable(name='a', type='integer', out=False), DS2Variable(name='c', type='double', out=False)]
>>> ds2_variables({'x': (float, True)}) [DS2Variable(name='x', type='double', out=True)]
Changed in version 1.5: names parameter added.
- sasctl.utils.pymas.python.parse_type_hints(func, skip_var='self')[source]#
Attempt to discern types for the input and output variable(s).
DS2 is a strongly-typed language but Python is not. Need to determine the types for input/output variables so they can be correctly mapped to DS2 types.
- Parameters:
func (Callable) – the object to inspect for parameters
skip_var (str) – name of the variable assumed to be the instance reference, if any. Will be ignored during parameter parsing.
- Returns:
params (OrderedDict) – dictionary of str : (str, bool) mapping param_name : (param_type, is_return_val)
- Raises:
ValueError – If the type of one or more variables could not be determined.