pdstools.infinity.resources.prediction_studio.local_model_utils

Attributes

Exceptions

ONNXModelCreationError

Exception for errors during ONNX conversion and save.

ONNXModelValidationError

Exception for errors during ONNX validation.

Classes

OutcomeType

Create a collection of name/value pairs.

Predictor

!!! abstract "Usage Documentation"

Output

!!! abstract "Usage Documentation"

Metadata

!!! abstract "Usage Documentation"

PMMLModel

!!! abstract "Usage Documentation"

H2OModel

!!! abstract "Usage Documentation"

ONNXModel

!!! abstract "Usage Documentation"

Module Contents

logger
PEGA_METADATA = 'pegaMetadata'
class OutcomeType(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

>>> Color.RED
<Color.RED: 1>
  • value lookup:

>>> Color(1)
<Color.RED: 1>
  • name lookup:

>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

BINARY = 'binary'
CATEGORICAL = 'categorical'
CONTINUOUS = 'continuous'
class Predictor(/, **data: Any)

Bases: pydantic.BaseModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

Attributes:

__class_vars__: The names of the class variables defined on the model. __private_attributes__: Metadata about the private attributes of the model. __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__: Whether model building is completed, or if there are still undefined fields. __pydantic_core_schema__: The core schema of the model. __pydantic_custom_init__: Whether the model has a custom __init__ function. __pydantic_decorators__: Metadata containing the decorators defined on the model.

This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to

__args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.

__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models. __pydantic_post_init__: The name of the post-init method for the model, if defined. __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model. __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra]

is set to ‘allow’.

__pydantic_fields_set__: The names of fields explicitly set during instantiation. __pydantic_private__: Values of private attributes set on the model instance.

Parameters:

data (Any)

name: str | None = None
index: int | None = None
input_name: str | None = None
validate_input_name(v)
validate_name(v)
validate_index(v)
class Output(/, **data: Any)

Bases: pydantic.BaseModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

Attributes:

__class_vars__: The names of the class variables defined on the model. __private_attributes__: Metadata about the private attributes of the model. __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__: Whether model building is completed, or if there are still undefined fields. __pydantic_core_schema__: The core schema of the model. __pydantic_custom_init__: Whether the model has a custom __init__ function. __pydantic_decorators__: Metadata containing the decorators defined on the model.

This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to

__args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.

__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models. __pydantic_post_init__: The name of the post-init method for the model, if defined. __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model. __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra]

is set to ‘allow’.

__pydantic_fields_set__: The names of fields explicitly set during instantiation. __pydantic_private__: Values of private attributes set on the model instance.

Parameters:

data (Any)

possible_values: List[str | int | float] = None
label_name: str | None = None
score_name: str | None = None
min_value: float | None = None
max_value: float | None = None
validate_label_name(v)
class Metadata(/, **data: Any)

Bases: pydantic.BaseModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

Attributes:

__class_vars__: The names of the class variables defined on the model. __private_attributes__: Metadata about the private attributes of the model. __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__: Whether model building is completed, or if there are still undefined fields. __pydantic_core_schema__: The core schema of the model. __pydantic_custom_init__: Whether the model has a custom __init__ function. __pydantic_decorators__: Metadata containing the decorators defined on the model.

This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to

__args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.

__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models. __pydantic_post_init__: The name of the post-init method for the model, if defined. __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model. __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra]

is set to ‘allow’.

__pydantic_fields_set__: The names of fields explicitly set during instantiation. __pydantic_private__: Values of private attributes set on the model instance.

Parameters:

data (Any)

predictor_list: List[Predictor] = None
modeling_technique: str | None = None
internal: bool | None = None
type: OutcomeType | None = None
output: Output | None = None
validate_type(v, values)
validate_output(v, values)
to_json() str
Return type:

str

classmethod from_json(json_str: str) Metadata
Parameters:

json_str (str)

Return type:

Metadata

static _convert_keys(data: dict, conversion_func) dict
Parameters:

data (dict)

Return type:

dict

static _to_snake_case(string: str) str
Parameters:

string (str)

Return type:

str

static _to_camel_case(string: str) str
Parameters:

string (str)

Return type:

str

class _ONNXMetadataEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.JSONEncoder

Extensible JSON <https://json.org> encoder for Python data structures.

Supports the following objects and types by default:

Python

JSON

dict

object

list, tuple

array

str

string

int, float

number

True

true

False

false

None

null

To extend this to recognize other objects, subclass and implement a .default() method with another method that returns a serializable object for o if possible, otherwise it should call the superclass implementation (to raise TypeError).

default(o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
exception ONNXModelCreationError

Bases: Exception

Exception for errors during ONNX conversion and save.

exception ONNXModelValidationError

Bases: pdstools.infinity.resources.prediction_studio.base.ModelValidationError

Exception for errors during ONNX validation.

class PMMLModel(file_path: str)

Bases: pdstools.infinity.resources.prediction_studio.base.LocalModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

Attributes:

__class_vars__: The names of the class variables defined on the model. __private_attributes__: Metadata about the private attributes of the model. __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__: Whether model building is completed, or if there are still undefined fields. __pydantic_core_schema__: The core schema of the model. __pydantic_custom_init__: Whether the model has a custom __init__ function. __pydantic_decorators__: Metadata containing the decorators defined on the model.

This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to

__args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.

__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models. __pydantic_post_init__: The name of the post-init method for the model, if defined. __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model. __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra]

is set to ‘allow’.

__pydantic_fields_set__: The names of fields explicitly set during instantiation. __pydantic_private__: Values of private attributes set on the model instance.

Parameters:

file_path (str)

file_path: str
get_file_path() str

Returns the file path of the model.

Returns:

str

Return type:

The file path of the model.

class H2OModel(file_path: str)

Bases: pdstools.infinity.resources.prediction_studio.base.LocalModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

Attributes:

__class_vars__: The names of the class variables defined on the model. __private_attributes__: Metadata about the private attributes of the model. __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__: Whether model building is completed, or if there are still undefined fields. __pydantic_core_schema__: The core schema of the model. __pydantic_custom_init__: Whether the model has a custom __init__ function. __pydantic_decorators__: Metadata containing the decorators defined on the model.

This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to

__args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.

__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models. __pydantic_post_init__: The name of the post-init method for the model, if defined. __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model. __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra]

is set to ‘allow’.

__pydantic_fields_set__: The names of fields explicitly set during instantiation. __pydantic_private__: Values of private attributes set on the model instance.

Parameters:

file_path (str)

file_path: str
get_file_path() str

Returns the file path of the model.

Returns:

str

Return type:

The file path of the model.

class ONNXModel(model: onnx.ModelProto)

Bases: pdstools.infinity.resources.prediction_studio.base.LocalModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

Attributes:

__class_vars__: The names of the class variables defined on the model. __private_attributes__: Metadata about the private attributes of the model. __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__: Whether model building is completed, or if there are still undefined fields. __pydantic_core_schema__: The core schema of the model. __pydantic_custom_init__: Whether the model has a custom __init__ function. __pydantic_decorators__: Metadata containing the decorators defined on the model.

This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to

__args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.

__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models. __pydantic_post_init__: The name of the post-init method for the model, if defined. __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model. __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra]

is set to ‘allow’.

__pydantic_fields_set__: The names of fields explicitly set during instantiation. __pydantic_private__: Values of private attributes set on the model instance.

Parameters:

model (onnx.ModelProto)

_model: onnx.ModelProto
classmethod from_onnx_proto(model: onnx.ModelProto) ONNXModel

Creates an ONNXModel object.

Parameters:

model (ModelProto) – An onnx ModelProto object

Returns:

An instance of the ONNXModel class.

Return type:

ONNXModel

Raises:
classmethod from_sklearn_pipeline(model: sklearn.pipeline.Pipeline, initial_types: list) ONNXModel

Creates an ONNXModel object.

Parameters:
  • model (Pipeline) – A sklearn Pipeline object

  • initial_types (list) – A list of initial types for the model’s input variables if the model is a Sklearn Pipeline object.

Returns:

An instance of the ONNXModel class.

Return type:

ONNXModel

Raises:
add_metadata(metadata: Metadata) ONNXModel

Adds metadata to the ONNX model.

Parameters:

metadata (Meta) – The metadata to be added.

Returns:

The ONNXModel object with the added metadata.

Return type:

ONNXModel

Raises:

ImportError – If the optional dependencies for ONNX Metadata addition are not installed.

validate() bool

Validates an ONNX model.

Raises:
  • ImportError – If the optional dependencies for ONNX Validation are not installed.:

  • ONNXModelValidationError – If the model is invalid or if the validation process fails.:

Return type:

bool

run(test_data: dict)

Run the prediction using the provided test data.

Parameters:

test_data (dict) –

The test data to be used for prediction. It is a dictionary where each key is a column name from the dataset, and each value is a NumPy array representing the column data as a vector. For example:

{

‘column1’: array([[value1], [value2], [value3]]), ‘column2’: array([[value4], [value5], [value6]]), ‘column3’: array([[value7], [value8], [value9]])

}

Returns:

The prediction result.

Return type:

Any

save(onnx_file_path: str)

Saves the ONNX model to the specified file path.

Parameters:

onnx_file_path (str) – The file path where the ONNX model should be saved.

Raises:

ImportError – If the optional dependencies for ONNX Conversion are not installed.:

__check_for_valid_input_node_structure(error_stream, session, metadata) bool

Checks if the output node structure of the ONNX model is valid.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • session (onnxruntime.InferenceSession) – The ONNX runtime session containing the model.

  • metadata (Meta) – The metadata associated with the model.

Returns:

True if the output node structure is valid, False otherwise.

Return type:

bool

__validate_input_nodes(error_stream, model_input_info, metadata) bool

Validates the input nodes of the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • model_input_info (dict) – A dictionary containing information about the model’s input nodes.

  • metadata (Meta) – The metadata associated with the model.

Returns:

True if the input nodes are valid, False otherwise.

Return type:

bool

__check_for_valid_output_node_structure(error_stream, session, metadata) bool

Checks if the output node structure of the ONNX model is valid.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • session (onnxruntime.InferenceSession) – The ONNX runtime session containing the model.

  • metadata (Meta) – The metadata associated with the model.

Returns:

True if the output node structure is valid, False otherwise.

Return type:

bool

__validate_tensor_output_node_structure(error_stream, node_name, value_info) bool

Validates the tensor output node structure of the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • node_name (str) – The name of the output node.

  • value_info (Any) – The value information of the output node.

Returns:

True if the output node is of type Tensor, False otherwise.

Return type:

bool

__validate_label_output_node_exist(error_stream, model_output_info, metadata) bool

Validates the existence of the label output node in the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • model_output_info (dict) – A dictionary containing information about the model’s output nodes.

  • metadata (Meta) – The metadata associated with the model.

Returns:

True if the label output node exists, False otherwise.

Return type:

bool

__validate_input_node_dimensions(error_stream, model_input_info) bool

Validates the dimensions of the input nodes in the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • model_input_info (dict) – A dictionary containing information about the model’s input nodes.

Returns:

True if all input nodes have valid dimensions, False otherwise.

Return type:

bool

__validate_input_node_shapes(error_stream, model_input_info) bool

Validates the dimensions of the input nodes in the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • model_input_info (dict) – A dictionary containing information about the model’s input nodes.

Returns:

True if all input nodes have valid dimensions, False otherwise.

Return type:

bool

__validate_predictor_mappings(error_stream, model_input_info, metadata) bool

Validates the predictor mappings in the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • model_input_info (dict) – A dictionary containing information about the model’s input nodes.

  • metadata (Meta) – The metadata associated with the model.

Returns:

True if all predictor mappings are valid, False otherwise.

Return type:

bool

__validate_predictor_index_mappings(error_stream, metadata) bool

Validates the predictor index mappings in the ONNX model’s metadata.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • metadata (Meta) – The metadata associated with the model.

Returns:

True if the predictor index mappings are valid, False otherwise.

Return type:

bool

__create_predictor_map(metadata)

Creates a mapping of input names to their corresponding predictors.

Parameters:

metadata (Meta) – The metadata associated with the model, containing the predictor list.

Returns:

A dictionary where the keys are input names and the values are lists of predictors.

Return type:

dict

__validate_input_node_sizes(error_stream, model_input_info, metadata) bool

Validates the sizes of the input nodes in the ONNX model.

Parameters:
  • error_stream (Any) – The stream to which error messages are written.

  • model_input_info (dict) – A dictionary containing information about the model’s input nodes.

  • metadata (Meta) – The metadata associated with the model, containing the predictor list.

Returns:

True if all input nodes have valid sizes, False otherwise.

Return type:

bool

__get_missing_predictors(model_input_info, predictor_input_names) str

Identifies the missing predictors in the ONNX model’s input nodes.

Parameters:
  • model_input_info (dict) – A dictionary containing information about the model’s input nodes.

  • predictor_input_names (list) – A list of predictor input names.

Returns:

A comma-separated string of missing predictor names.

Return type:

str