Basic

Overview

The Basic class of sientia_tracker module provides a generic tracker object with no additional requirements or parameters. It is designed and wrapped to interact with some open-source Machine Learning tracking libraries for logging experiments, models, and metrics.

BaseTracker

Basic Tracker object that don't have any requirements nor parameters to be used

Source code in sientia_tracker/basic.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class BaseTracker:
    """
    Basic Tracker object that don't have any requirements nor parameters to be used
    """
    def __init__(self, tracking_uri:str, username: str = None, password: str = None)-> None:
        """
        Initialize the tracker object

        Parameters:
            tracking_uri: URI to the MLflow server
            username: Username to access the MLflow server
            password: Password to access the MLflow server

        Returns:
            None
        """

        mlflow.set_tracking_uri(tracking_uri)

        os.environ['MLFLOW_TRACKING_USERNAME'] = username
        os.environ['MLFLOW_TRACKING_PASSWORD'] = password
        # Create an MLflow client
        self.client = mlflow.tracking.MlflowClient()

    def log_model(self, sk_model: Any, artifact_path: Any, extra_pip_requirements: Any | None = None,**kwargs):
        """
        Log a model to MLflow.

        Parameters:
            sk_model: scikit-learn model to be saved
            artifact_path: name of the model
            extra_pip_requirements: additional pip requirements to be installed

        Returns:
            None
        """
        mlflow.sklearn.log_model(sk_model, artifact_path, extra_pip_requirements,**kwargs)

    def log_params(self, params: Dict[str, Any], **kwargs) -> None:
        """
        Log parameters to MLflow.

        Parameters:
            params: Dict with the parameters to log.

        Returns:
            None
        """
        mlflow.log_params(params, **kwargs)

    def log_metrics(self, params: Dict[str, float], **kwargs) -> None:
        """
        Log metrics to MLflow.

        Parameters:
            params: Dict with the metrics to log.

        Returns:
            None
        """
        mlflow.log_metrics(params, **kwargs)

    def log_artifact(self, local_path: str, artifact_path: str | None = None, **kwargs) -> None:
        """
        Log an artifact to MLflow.

        Parameters:
            local_path: Path to the file to write.
            artifact_path: If provided, the directory in artifact_uri to write to.

        Returns:
            None
        """
        mlflow.log_artifact(local_path, artifact_path, **kwargs)

    def set_project(self, project_name: str)-> None:
        """
        Check if the experiment already exists; if not, create it

        Parameters:
            project_name (str): The name of the project.

        Returns:
            None
        """
        project = mlflow.get_experiment_by_name(
            project_name)
        if project is None:
            mlflow.create_experiment(name=project_name)
            self.project_name = project_name
            print(f"Experiment {project_name} created")

        else:
            # Activate the experiment for tracking
            mlflow.set_experiment(project_name)
            self.project_name = project_name
            print(f"Experiment {project_name} already exists")

    def save_experiment(self):
        """
        Start a run in MLflow.

        Parameters:
            **kwargs: The parameters to log.

        Returns:
            None
        """
        print("Saving experiment", self.project_name)
        runs = mlflow.search_runs(experiment_names=[
            self.project_name], order_by=["start_time desc"])
        next_run_number = len(runs) + 1
        active_run = mlflow.start_run(run_name=f"{self.project_name}-{next_run_number}")
        return active_run

    def get_model_run_id(self, model_name: str, stage: str = "Production"):
        """
        Get the run_id of a model based on its name and stage.

        Parameters:
            model_name (str): The name of the model.
            stage (str): The stage of the model.

        Returns:
            str: The run_id of the model.
        """
        latest_versions = self.client.get_latest_versions(
            name=model_name, stages=[stage])
        run_id = latest_versions[0].source.split("/")
        return run_id[2]

    def get_model_experiment_id(self, model_name: str):
        """
        Get the project name associated with a model.

        Parameters:
            model_name (str): The name of the model.

        Returns:
            str: The project name.
        """
        latest_production_id = self.get_model_run_id(model_name=model_name, stage="Production")
        run_info = mlflow.get_run(latest_production_id)
        return run_info.info.experiment_id

    def get_run_name(self, run_id: str):
        """
        Get the run name associated with a run ID.

        Parameters:
            run_id (str): Run ID.

        Returns:
            str: Run name.
        """
        if run_id:
            run_info = mlflow.get_run(run_id)
            run_name = run_info.info.run_name
        else:
            run_name = None
        return run_name

__init__(tracking_uri, username=None, password=None)

Initialize the tracker object

Parameters:
  • tracking_uri (str) –

    URI to the MLflow server

  • username (str, default: None ) –

    Username to access the MLflow server

  • password (str, default: None ) –

    Password to access the MLflow server

Returns:
  • None

    None

Source code in sientia_tracker/basic.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def __init__(self, tracking_uri:str, username: str = None, password: str = None)-> None:
    """
    Initialize the tracker object

    Parameters:
        tracking_uri: URI to the MLflow server
        username: Username to access the MLflow server
        password: Password to access the MLflow server

    Returns:
        None
    """

    mlflow.set_tracking_uri(tracking_uri)

    os.environ['MLFLOW_TRACKING_USERNAME'] = username
    os.environ['MLFLOW_TRACKING_PASSWORD'] = password
    # Create an MLflow client
    self.client = mlflow.tracking.MlflowClient()

get_model_experiment_id(model_name)

Get the project name associated with a model.

Parameters:
  • model_name (str) –

    The name of the model.

Returns:
  • str

    The project name.

Source code in sientia_tracker/basic.py
141
142
143
144
145
146
147
148
149
150
151
152
153
def get_model_experiment_id(self, model_name: str):
    """
    Get the project name associated with a model.

    Parameters:
        model_name (str): The name of the model.

    Returns:
        str: The project name.
    """
    latest_production_id = self.get_model_run_id(model_name=model_name, stage="Production")
    run_info = mlflow.get_run(latest_production_id)
    return run_info.info.experiment_id

get_model_run_id(model_name, stage='Production')

Get the run_id of a model based on its name and stage.

Parameters:
  • model_name (str) –

    The name of the model.

  • stage (str, default: 'Production' ) –

    The stage of the model.

Returns:
  • str

    The run_id of the model.

Source code in sientia_tracker/basic.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def get_model_run_id(self, model_name: str, stage: str = "Production"):
    """
    Get the run_id of a model based on its name and stage.

    Parameters:
        model_name (str): The name of the model.
        stage (str): The stage of the model.

    Returns:
        str: The run_id of the model.
    """
    latest_versions = self.client.get_latest_versions(
        name=model_name, stages=[stage])
    run_id = latest_versions[0].source.split("/")
    return run_id[2]

get_run_name(run_id)

Get the run name associated with a run ID.

Parameters:
  • run_id (str) –

    Run ID.

Returns:
  • str

    Run name.

Source code in sientia_tracker/basic.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def get_run_name(self, run_id: str):
    """
    Get the run name associated with a run ID.

    Parameters:
        run_id (str): Run ID.

    Returns:
        str: Run name.
    """
    if run_id:
        run_info = mlflow.get_run(run_id)
        run_name = run_info.info.run_name
    else:
        run_name = None
    return run_name

log_artifact(local_path, artifact_path=None, **kwargs)

Log an artifact to MLflow.

Parameters:
  • local_path (str) –

    Path to the file to write.

  • artifact_path (str | None, default: None ) –

    If provided, the directory in artifact_uri to write to.

Returns:
  • None

    None

Source code in sientia_tracker/basic.py
72
73
74
75
76
77
78
79
80
81
82
83
def log_artifact(self, local_path: str, artifact_path: str | None = None, **kwargs) -> None:
    """
    Log an artifact to MLflow.

    Parameters:
        local_path: Path to the file to write.
        artifact_path: If provided, the directory in artifact_uri to write to.

    Returns:
        None
    """
    mlflow.log_artifact(local_path, artifact_path, **kwargs)

log_metrics(params, **kwargs)

Log metrics to MLflow.

Parameters:
  • params (Dict[str, float]) –

    Dict with the metrics to log.

Returns:
  • None

    None

Source code in sientia_tracker/basic.py
60
61
62
63
64
65
66
67
68
69
70
def log_metrics(self, params: Dict[str, float], **kwargs) -> None:
    """
    Log metrics to MLflow.

    Parameters:
        params: Dict with the metrics to log.

    Returns:
        None
    """
    mlflow.log_metrics(params, **kwargs)

log_model(sk_model, artifact_path, extra_pip_requirements=None, **kwargs)

Log a model to MLflow.

Parameters:
  • sk_model (Any) –

    scikit-learn model to be saved

  • artifact_path (Any) –

    name of the model

  • extra_pip_requirements (Any | None, default: None ) –

    additional pip requirements to be installed

Returns:
  • None

Source code in sientia_tracker/basic.py
34
35
36
37
38
39
40
41
42
43
44
45
46
def log_model(self, sk_model: Any, artifact_path: Any, extra_pip_requirements: Any | None = None,**kwargs):
    """
    Log a model to MLflow.

    Parameters:
        sk_model: scikit-learn model to be saved
        artifact_path: name of the model
        extra_pip_requirements: additional pip requirements to be installed

    Returns:
        None
    """
    mlflow.sklearn.log_model(sk_model, artifact_path, extra_pip_requirements,**kwargs)

log_params(params, **kwargs)

Log parameters to MLflow.

Parameters:
  • params (Dict[str, Any]) –

    Dict with the parameters to log.

Returns:
  • None

    None

Source code in sientia_tracker/basic.py
48
49
50
51
52
53
54
55
56
57
58
def log_params(self, params: Dict[str, Any], **kwargs) -> None:
    """
    Log parameters to MLflow.

    Parameters:
        params: Dict with the parameters to log.

    Returns:
        None
    """
    mlflow.log_params(params, **kwargs)

save_experiment()

Start a run in MLflow.

Parameters:
  • **kwargs

    The parameters to log.

Returns:
  • None

Source code in sientia_tracker/basic.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def save_experiment(self):
    """
    Start a run in MLflow.

    Parameters:
        **kwargs: The parameters to log.

    Returns:
        None
    """
    print("Saving experiment", self.project_name)
    runs = mlflow.search_runs(experiment_names=[
        self.project_name], order_by=["start_time desc"])
    next_run_number = len(runs) + 1
    active_run = mlflow.start_run(run_name=f"{self.project_name}-{next_run_number}")
    return active_run

set_project(project_name)

Check if the experiment already exists; if not, create it

Parameters:
  • project_name (str) –

    The name of the project.

Returns:
  • None

    None

Source code in sientia_tracker/basic.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def set_project(self, project_name: str)-> None:
    """
    Check if the experiment already exists; if not, create it

    Parameters:
        project_name (str): The name of the project.

    Returns:
        None
    """
    project = mlflow.get_experiment_by_name(
        project_name)
    if project is None:
        mlflow.create_experiment(name=project_name)
        self.project_name = project_name
        print(f"Experiment {project_name} created")

    else:
        # Activate the experiment for tracking
        mlflow.set_experiment(project_name)
        self.project_name = project_name
        print(f"Experiment {project_name} already exists")