Module markov.api.models.model

Classes

class Model (name: str, project_id: str = '', description: str = '', model_class: ModelClass = ModelClass.CLASSIFICATION, metadata: Dict[str, str] = None)

Interface to create models under a project with MarkovML.

Every experiment creates a model. Models are used to collate the evaluation results with experiment results. A model can be used without running an experiment.

If project id is not provided, the model is created in the default project of the workspace.

Examples

>>> model = Model(name="My first model") # Creates the markov model object
>>> model.register() # Registers teh markov model object with markov backend
>>> eval_recorder1 = model.create_evaluation_recorder(evaluation_name="My first evaluation", dataset_id="your_dataset_id")
>>> eval_recorder2 = model.create_evaluation_recorder(evaluation_name="My second evaluation", dataset_id="your_dataset_id")

You can also fetch model already registered with markov backend

>>> model_from_id = Model.get_by_id(model_id="existing_model_id")
>>> model_from_name = Model.get_by_name(model_name="existing_model_name")

Constructor for markov model object

Args

name : str
Name to be given to the model
project_id : str
Id of the project under which this model needs to be registered
description : str
Optional. The description describing the model
model_class : ModelClass
ModelClass describes the type of operation being done by the model. For example, CLASSIFICATION, REGRESSION. Default: ModelClass.CLASSIFICATION

Static methods

def get_by_id(model_id: str) ‑> Optional[Model]

Fetches the model with id model_id from markov backend

Args

model_id
Id of the model to be fetched

Returns

Returns the markov model object if model found; Else returns None

def get_by_name(project_id: str, model_name: str) ‑> Optional[Model]

Fetches the model with id model_name from markov backend

Args

project_id : str
project this model belongs to
model_name
name of the model you want to retrieve from MarkovML.
>>> Model.get_by_name(project_id="markov_project_id",model_name='MY Favorite Model')

Returns

Returns the markov model object if model found; Else returns None

def get_models_by_metadata(metadata: Dict[str, str], stage: Stage = None)

Retrieves models that match the given metadata and optional stage criteria.

Description: This method retrieves models based on metadata and an optional stage criteria. There are two main scenarios for retrieval:

  1. When a stage is provided:
  2. Models matching both the provided stage and metadata are returned.
  3. The stage and metadata are used in conjunction for retrieval.

  4. When a stage is not provided (default stage is None):

  5. Models matching only the provided metadata are returned.
  6. The stage criteria is not considered for filtering in this case.

Args

metadata : Dict[str, str]
A dictionary of metadata criteria where both keys and values are strings.
stage : Stage
An optional arg of Stage class

Returns

List[Model]
A list of Model instances that match the metadata and the stage criteria.

Raises

Exception
If the response from the model dispatcher indicates a failure.

Example

>>> Model.get_models_by_metadata({'type': 'classifier'})
>>> Model.get_models_by_metadata({'type': 'classifier'}, stage=Stage.DEVELOP)
[Model(...), Model(...)]

Instance variables

prop description

Returns: associated helpful description of this model that explains its goal and scope.

prop metadata

Returns: Metadata associated to the model

prop model_class

Returns: Model class of this model

prop model_id

Returns: unique id of this model assigned by MarkovML backend.

prop name

Returns: name of this model object

prop project_id

Returns: unique id of the project this model belongs to.

Methods

def add_metadata(self, metadata: Dict[str, str])

Adds metadata to the model identified by the instance's model ID. Validates the metadata before adding.

Args

metadata : Dict[str, str]
A dictionary of metadata where both keys and values are strings.

Raises

Exception
If the response from the model dispatcher indicates a failure.

Example

>>> model_instance.add_metadata({'version': '1.0', 'type': 'classifier'})
Successfully added metadata to model (id: model_id)
def associate_experiment_to_model(self, experiment_id)

Please do not call this method directly. This method is internal method called during experiment tracking to create a place holder for model associated with the experiment.

Args

experiment_id : str
experiment_id of the experiment this model was created from.

Returns:

def create_evaluation_recorder(self, evaluation_name: str, dataset_id: str, evaluation_notes: str = '', pos_label: Union[int, str] = None, **kwargs) ‑> Optional[EvaluationRecorder]

Method to create evaluation recorder to evaluate model (which in turn is organized in a project) against the given dataset

Args

evaluation_name
Name of the evaluation
evaluation_notes
Notes for the evaluation
dataset_id
Dataset id of the dataset against this model is being evaluated
pos_label
positive label in your evaluation data. It is needed to calculate the True Positive Rate (TPR)

and the False Positive Rate (FPR) to compute Area Under The Curve (AUC).

Returns

EvaluationRecorder instance

def download_model_files(self, local_destination_path: str)

Downloads model files from a remote storage to a specified local directory. It checks if the destination path exists and is a directory. If the path does not exist or is not a directory, it raises a ValueError.

Parameters: local_destination_path (str): The local directory path where the model files will be downloaded.

Raises: ValueError: If the destination path does not exist or is not a directory. RuntimeError: If an error occurs during the fetching of the download link.

Usage Example:

>>> mkv_model = Model.get_by_name(project_id="YOUR_PROJECT_ID", model_name="YOUR_MODEL_NAME")
>>> mkv_model.download_model_files('/path/to/destination')

Links this model instance to a model registry either by the registry's unique identifier or by its name.

This method identifies the target registry using either the provided registry ID or name, then links the current model to the identified registry. The model must be registered with the Markov backend prior to linking.

Args

registry_id : str, optional
The unique identifier of the registry to which the model is to be linked. Default is an empty string.
registry_name : str, optional
The name of the registry to which the model is to be linked. Default is an empty string.

Raises

ValueError
If neither registry_id nor registry_name is provided, or if the model has not been registered with the Markov backend.

Example

>>> mkv_model = Model.get_by_name(model_name="my_model_name")
>>> mkv_model.link_to_registry(registry_id="my_registry_id")
>>> mkv_model.link_to_registry(registry_name="my_registry_name")
def register(self)

Registers the model metadata with MarkovML backend and assigns a model id to this object. Returns: model_id assigned to this model

def update_model_stage_in_registry(self, to_stage: ModelRegistryStageStates)

Updates the stage of this model in its registry to the specified stage.

This method updates the stage of the model within its linked registry. It requires that the model is already registered and linked to a registry within the Markov backend. The method communicates with the backend to update the model's stage to the desired value.

Args

to_stage : ModelRegistryStageStates
The target stage to which the model's stage should be updated.

Raises

ValueError
If the model has not been registered with the Markov backend.

Example

>>> model = Model.get_by_name(model_name="my_model_name")
>>> model.link_to_registry(registry_id="my_registry_id")
>>> model.update_model_stage_in_registry(to_stage=ModelRegistryStageStates.PRODUCTION)
def upload_model_files(self, local_file_paths: Union[str, List[str]])

Uploads local model files to a remote storage. This method accepts either a single file path or a list of file paths. It compresses the specified files into a zip and uploads them.

Parameters: local_file_paths (Union[str, List[str]]): A single file path as a string, or a list of file paths to upload.

Usage Example:

# For a single file
>>> mkv_model = Model.get_by_name(project_id="YOUR_PROJECT_ID", model_name="YOUR_MODEL_NAME")
>>> mkv_model.upload_model_files('/path/to/model/file')

# For multiple files
>>> mkv_model.upload_model_files(['/path/to/model/file1', '/path/to/model/file2'])