pdstools.infinity.resources.prediction_studio.local_model_utils =============================================================== .. py:module:: pdstools.infinity.resources.prediction_studio.local_model_utils Attributes ---------- .. autoapisummary:: pdstools.infinity.resources.prediction_studio.local_model_utils.logger pdstools.infinity.resources.prediction_studio.local_model_utils.PEGA_METADATA Exceptions ---------- .. autoapisummary:: pdstools.infinity.resources.prediction_studio.local_model_utils.ONNXModelCreationError pdstools.infinity.resources.prediction_studio.local_model_utils.ONNXModelValidationError Classes ------- .. autoapisummary:: pdstools.infinity.resources.prediction_studio.local_model_utils.OutcomeType pdstools.infinity.resources.prediction_studio.local_model_utils.Predictor pdstools.infinity.resources.prediction_studio.local_model_utils.Output pdstools.infinity.resources.prediction_studio.local_model_utils.Metadata pdstools.infinity.resources.prediction_studio.local_model_utils.PMMLModel pdstools.infinity.resources.prediction_studio.local_model_utils.H2OModel pdstools.infinity.resources.prediction_studio.local_model_utils.ONNXModel Module Contents --------------- .. py:data:: logger .. py:data:: PEGA_METADATA :value: 'pegaMetadata' .. py:class:: OutcomeType(*args, **kwds) Bases: :py:obj:`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 - value lookup: >>> Color(1) - name lookup: >>> Color['RED'] Enumerations can be iterated over, and know how many members they have: >>> len(Color) 3 >>> list(Color) [, , ] Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details. .. py:attribute:: BINARY :value: 'binary' .. py:attribute:: CATEGORICAL :value: 'categorical' .. py:attribute:: CONTINUOUS :value: 'continuous' .. py:class:: Predictor(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Usage docs: https://docs.pydantic.dev/2.10/concepts/models/ 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. .. py:attribute:: name :type: Optional[str] :value: None .. py:attribute:: index :type: Optional[int] :value: None .. py:attribute:: input_name :type: Optional[str] :value: None .. py:method:: validate_input_name(v) .. py:method:: validate_name(v) .. py:method:: validate_index(v) .. py:class:: Output(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Usage docs: https://docs.pydantic.dev/2.10/concepts/models/ 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. .. py:attribute:: possible_values :type: List[Union[str, int, float]] :value: None .. py:attribute:: label_name :type: Optional[str] :value: None .. py:attribute:: score_name :type: Optional[str] :value: None .. py:attribute:: min_value :type: Optional[float] :value: None .. py:attribute:: max_value :type: Optional[float] :value: None .. py:method:: validate_label_name(v) .. py:class:: Metadata(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Usage docs: https://docs.pydantic.dev/2.10/concepts/models/ 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. .. py:attribute:: predictor_list :type: List[Predictor] :value: None .. py:attribute:: modeling_technique :type: Optional[str] :value: None .. py:attribute:: internal :type: Optional[bool] :value: None .. py:attribute:: type :type: Optional[OutcomeType] :value: None .. py:attribute:: output :type: Optional[Output] :value: None .. py:method:: validate_type(v, values) .. py:method:: validate_output(v, values) .. py:method:: to_json() -> str .. py:method:: from_json(json_str: str) -> Metadata :classmethod: .. py:method:: _convert_keys(data: dict, conversion_func) -> dict :staticmethod: .. py:method:: _to_snake_case(string: str) -> str :staticmethod: .. py:method:: _to_camel_case(string: str) -> str :staticmethod: .. py:class:: _ONNXMetadataEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None) Bases: :py:obj:`json.JSONEncoder` Extensible JSON 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``). .. py:method:: 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) .. py:exception:: ONNXModelCreationError Bases: :py:obj:`Exception` Exception for errors during ONNX conversion and save. .. py:exception:: ONNXModelValidationError Bases: :py:obj:`pdstools.infinity.resources.prediction_studio.base.ModelValidationError` Exception for errors during ONNX validation. .. py:class:: PMMLModel(file_path: str) Bases: :py:obj:`pdstools.infinity.resources.prediction_studio.base.LocalModel` Usage docs: https://docs.pydantic.dev/2.10/concepts/models/ 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. .. py:attribute:: file_path :type: str .. py:method:: get_file_path() -> str Returns the file path of the model. :returns: **str** :rtype: The file path of the model. .. py:class:: H2OModel(file_path: str) Bases: :py:obj:`pdstools.infinity.resources.prediction_studio.base.LocalModel` Usage docs: https://docs.pydantic.dev/2.10/concepts/models/ 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. .. py:attribute:: file_path :type: str .. py:method:: get_file_path() -> str Returns the file path of the model. :returns: **str** :rtype: The file path of the model. .. py:class:: ONNXModel(model: onnx.ModelProto) Bases: :py:obj:`pdstools.infinity.resources.prediction_studio.base.LocalModel` Usage docs: https://docs.pydantic.dev/2.10/concepts/models/ 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. .. py:attribute:: _model :type: onnx.ModelProto .. py:method:: from_onnx_proto(model: onnx.ModelProto) -> ONNXModel :classmethod: Creates an ONNXModel object. :param model: An onnx ModelProto object :type model: ModelProto :returns: An instance of the ONNXModel class. :rtype: ONNXModel :raises ImportError: If the optional dependencies for ONNX Conversion are not installed. :raises ONNXModelCreationError: If the ONNXModel object cannot be created. .. py:method:: from_sklearn_pipeline(model: sklearn.pipeline.Pipeline, initial_types: list) -> ONNXModel :classmethod: Creates an ONNXModel object. :param model: A sklearn Pipeline object :type model: Pipeline :param initial_types: A list of initial types for the model's input variables if the model is a Sklearn Pipeline object. :type initial_types: list :returns: An instance of the ONNXModel class. :rtype: ONNXModel :raises ImportError: If the optional dependencies for ONNX Conversion are not installed. :raises ONNXModelCreationError: If the ONNXModel object cannot be created. .. py:method:: add_metadata(metadata: Metadata) -> ONNXModel Adds metadata to the ONNX model. :param metadata: The metadata to be added. :type metadata: Meta :returns: The ONNXModel object with the added metadata. :rtype: ONNXModel :raises ImportError: If the optional dependencies for ONNX Metadata addition are not installed. .. py:method:: validate() -> bool Validates an ONNX model. :raises ImportError: If the optional dependencies for ONNX Validation are not installed.: :raises ONNXModelValidationError: If the model is invalid or if the validation process fails.: .. py:method:: run(test_data: dict) Run the prediction using the provided test data. :param test_data: 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]]) } :type test_data: dict :returns: The prediction result. :rtype: Any .. py:method:: save(onnx_file_path: str) Saves the ONNX model to the specified file path. :param onnx_file_path: The file path where the ONNX model should be saved. :type onnx_file_path: str :raises ImportError: If the optional dependencies for ONNX Conversion are not installed.: .. py:method:: __check_for_valid_input_node_structure(error_stream, session, metadata) -> bool Checks if the output node structure of the ONNX model is valid. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param session: The ONNX runtime session containing the model. :type session: onnxruntime.InferenceSession :param metadata: The metadata associated with the model. :type metadata: Meta :returns: True if the output node structure is valid, False otherwise. :rtype: bool .. py:method:: __validate_input_nodes(error_stream, model_input_info, metadata) -> bool Validates the input nodes of the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param model_input_info: A dictionary containing information about the model's input nodes. :type model_input_info: dict :param metadata: The metadata associated with the model. :type metadata: Meta :returns: True if the input nodes are valid, False otherwise. :rtype: bool .. py:method:: __check_for_valid_output_node_structure(error_stream, session, metadata) -> bool Checks if the output node structure of the ONNX model is valid. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param session: The ONNX runtime session containing the model. :type session: onnxruntime.InferenceSession :param metadata: The metadata associated with the model. :type metadata: Meta :returns: True if the output node structure is valid, False otherwise. :rtype: bool .. py:method:: __validate_tensor_output_node_structure(error_stream, node_name, value_info) -> bool Validates the tensor output node structure of the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param node_name: The name of the output node. :type node_name: str :param value_info: The value information of the output node. :type value_info: Any :returns: True if the output node is of type Tensor, False otherwise. :rtype: bool .. py:method:: __validate_label_output_node_exist(error_stream, model_output_info, metadata) -> bool Validates the existence of the label output node in the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param model_output_info: A dictionary containing information about the model's output nodes. :type model_output_info: dict :param metadata: The metadata associated with the model. :type metadata: Meta :returns: True if the label output node exists, False otherwise. :rtype: bool .. py:method:: __validate_input_node_dimensions(error_stream, model_input_info) -> bool Validates the dimensions of the input nodes in the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param model_input_info: A dictionary containing information about the model's input nodes. :type model_input_info: dict :returns: True if all input nodes have valid dimensions, False otherwise. :rtype: bool .. py:method:: __validate_input_node_shapes(error_stream, model_input_info) -> bool Validates the dimensions of the input nodes in the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param model_input_info: A dictionary containing information about the model's input nodes. :type model_input_info: dict :returns: True if all input nodes have valid dimensions, False otherwise. :rtype: bool .. py:method:: __validate_predictor_mappings(error_stream, model_input_info, metadata) -> bool Validates the predictor mappings in the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param model_input_info: A dictionary containing information about the model's input nodes. :type model_input_info: dict :param metadata: The metadata associated with the model. :type metadata: Meta :returns: True if all predictor mappings are valid, False otherwise. :rtype: bool .. py:method:: __validate_predictor_index_mappings(error_stream, metadata) -> bool Validates the predictor index mappings in the ONNX model's metadata. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param metadata: The metadata associated with the model. :type metadata: Meta :returns: True if the predictor index mappings are valid, False otherwise. :rtype: bool .. py:method:: __create_predictor_map(metadata) Creates a mapping of input names to their corresponding predictors. :param metadata: The metadata associated with the model, containing the predictor list. :type metadata: Meta :returns: A dictionary where the keys are input names and the values are lists of predictors. :rtype: dict .. py:method:: __validate_input_node_sizes(error_stream, model_input_info, metadata) -> bool Validates the sizes of the input nodes in the ONNX model. :param error_stream: The stream to which error messages are written. :type error_stream: Any :param model_input_info: A dictionary containing information about the model's input nodes. :type model_input_info: dict :param metadata: The metadata associated with the model, containing the predictor list. :type metadata: Meta :returns: True if all input nodes have valid sizes, False otherwise. :rtype: bool .. py:method:: __get_missing_predictors(model_input_info, predictor_input_names) -> str Identifies the missing predictors in the ONNX model's input nodes. :param model_input_info: A dictionary containing information about the model's input nodes. :type model_input_info: dict :param predictor_input_names: A list of predictor input names. :type predictor_input_names: list :returns: A comma-separated string of missing predictor names. :rtype: str