|
| 1 | +import json |
| 2 | + |
| 3 | +class DSSMLflowExtension(object): |
| 4 | + """ |
| 5 | + A handle to interact with specific endpoints of the DSS MLflow integration. |
| 6 | +
|
| 7 | + Do not create this directly, use :meth:`dataikuapi.dss.DSSProject.get_mlflow_extension` |
| 8 | + """ |
| 9 | + |
| 10 | + def __init__(self, client, project_key): |
| 11 | + self.client = client |
| 12 | + self.project = client.get_project(project_key) |
| 13 | + self.project_key = project_key |
| 14 | + |
| 15 | + def list_models(self, run_id): |
| 16 | + """ |
| 17 | + Returns the list of models of given run |
| 18 | +
|
| 19 | + :param run_id: run_id for which to return a list of models |
| 20 | + :type run_id: str |
| 21 | + """ |
| 22 | + response = self.client._perform_http( |
| 23 | + "GET", "/api/2.0/mlflow/extension/models/{}".format(run_id), |
| 24 | + headers={"x-dku-mlflow-project-key": self.project_key} |
| 25 | + ) |
| 26 | + return response.json() |
| 27 | + |
| 28 | + def list_experiments(self, view_type="ACTIVE_ONLY", max_results=1000): |
| 29 | + """ |
| 30 | + Returns the list of experiments in the DSS project for which MLflow integration |
| 31 | + is setup |
| 32 | +
|
| 33 | + :param view_type: ACTIVE_ONLY, DELETED_ONLY or ALL |
| 34 | + :type view_type: str |
| 35 | + :param max_results: max results count |
| 36 | + :type max_results: int |
| 37 | + :rtype: dict |
| 38 | + """ |
| 39 | + response = self.client._perform_http( |
| 40 | + "GET", "/api/2.0/mlflow/experiments/list?view_type={view_type}&max_results={max_results}".format(view_type=view_type, max_results=max_results), |
| 41 | + headers={"x-dku-mlflow-project-key": self.project_key} |
| 42 | + ) |
| 43 | + return response.json() |
| 44 | + |
| 45 | + def rename_experiment(self, experiment_id, new_name): |
| 46 | + """ |
| 47 | + Renames an experiment |
| 48 | +
|
| 49 | + :param experiment_id: experiment id |
| 50 | + :type experiment_id: str |
| 51 | + :param new_name: new name |
| 52 | + :type new_name: str |
| 53 | + """ |
| 54 | + response = self.client._perform_http( |
| 55 | + "POST", "/api/2.0/mlflow/experiments/update", |
| 56 | + headers={"x-dku-mlflow-project-key": self.project_key}, |
| 57 | + body={"experiment_id": experiment_id, "new_name": new_name} |
| 58 | + ) |
| 59 | + return response.json() |
| 60 | + |
| 61 | + def restore_experiment(self, experiment_id): |
| 62 | + """ |
| 63 | + Restores a deleted experiment |
| 64 | +
|
| 65 | + :param experiment_id: experiment id |
| 66 | + :type experiment_id: str |
| 67 | + """ |
| 68 | + response = self.client._perform_http( |
| 69 | + "POST", "/api/2.0/mlflow/experiments/restore", |
| 70 | + headers={"x-dku-mlflow-project-key": self.project_key}, |
| 71 | + body={"experiment_id": experiment_id} |
| 72 | + ) |
| 73 | + return response.json() |
| 74 | + |
| 75 | + def restore_run(self, run_id): |
| 76 | + """ |
| 77 | + Restores a deleted run |
| 78 | +
|
| 79 | + :param run_id: run id |
| 80 | + :type run_id: str |
| 81 | + """ |
| 82 | + response = self.client._perform_http( |
| 83 | + "POST", "/api/2.0/mlflow/runs/restore", |
| 84 | + headers={"x-dku-mlflow-project-key": self.project_key}, |
| 85 | + body={"run_id": run_id} |
| 86 | + ) |
| 87 | + return response.json() |
| 88 | + |
| 89 | + def garbage_collect(self): |
| 90 | + """ |
| 91 | + Permanently deletes the experiments and runs marked as "Deleted" |
| 92 | + """ |
| 93 | + self.client._perform_http( |
| 94 | + "GET", "/api/2.0/mlflow/extension/garbage-collect", |
| 95 | + headers={"x-dku-mlflow-project-key": self.project_key} |
| 96 | + ) |
| 97 | + |
| 98 | + def create_experiment_tracking_dataset(self, dataset_name, experiment_ids=[], view_type="ACTIVE_ONLY", filter_expr="", order_by=[], format="LONG"): |
| 99 | + """ |
| 100 | +
|
| 101 | + Creates a virtual dataset exposing experiment tracking data. |
| 102 | +
|
| 103 | + :param dataset_name: name of the dataset |
| 104 | + :type dataset_name: str |
| 105 | + :param experiment_ids: list of ids of experiments to filter on. No filtering if empty |
| 106 | + :type experiment_ids: list(str) |
| 107 | + :param view_type: one of ACTIVE_ONLY, DELETED_ONLY and ALL. Default is ACTIVE_ONLY |
| 108 | + :type view_type: str |
| 109 | + :param filter_expr: MLflow search expression |
| 110 | + :type filter_expr: str |
| 111 | + :param order_by: list of order by clauses. Default is ordered by start_time, then runId |
| 112 | + :type order_by: list(str) |
| 113 | + :param format: LONG or JSON. Default is LONG |
| 114 | + :type format: str |
| 115 | + """ |
| 116 | + self.client._perform_http( |
| 117 | + "POST", "/api/2.0/mlflow/extension/create-project-experiments-dataset", |
| 118 | + headers={"x-dku-mlflow-project-key": self.project_key}, |
| 119 | + body={ |
| 120 | + "datasetName": dataset_name, |
| 121 | + "experimentIds": experiment_ids, |
| 122 | + "viewType": view_type, |
| 123 | + "filter": filter_expr, |
| 124 | + "orderBy": order_by, |
| 125 | + "format": format |
| 126 | + } |
| 127 | + ) |
| 128 | + |
| 129 | + def clean_experiment_tracking_db(self): |
| 130 | + """ |
| 131 | + Cleans the experiments, runs, params, metrics, tags, etc. for this project |
| 132 | +
|
| 133 | + This call requires an API key with admin rights |
| 134 | + """ |
| 135 | + self.client._perform_raw("DELETE", "/api/2.0/mlflow/extension/clean-db/%s" % self.project_key) |
| 136 | + |
| 137 | + def set_run_inference_info(self, run_id, model_type, classes=None, code_env_name=None, target=None): |
| 138 | + """ |
| 139 | + Sets the type of the model, and optionally other information useful to deploy or evaluate it. |
| 140 | +
|
| 141 | + model_type must be one of: |
| 142 | + - REGRESSION |
| 143 | + - BINARY_CLASSIFICATION |
| 144 | + - MULTICLASS |
| 145 | + - OTHER |
| 146 | +
|
| 147 | + Classes must be specified if and only if the model is a BINARY_CLASSIFICATION or MULTICLASS model. |
| 148 | +
|
| 149 | + This information is leveraged to filter saved models on their prediction type and prefill the classes |
| 150 | + when deploying using the GUI an MLflow model as a version of a DSS Saved Model. |
| 151 | +
|
| 152 | + :param model_type: prediction type (see doc) |
| 153 | + :type model_type: str |
| 154 | + :param run_id: run_id for which to set the classes |
| 155 | + :type run_id: str |
| 156 | + :param classes: ordered list of classes (not for all prediction types, see doc) |
| 157 | + :type classes: list(str) |
| 158 | + :param code_env_name: name of an adequate DSS python code environment |
| 159 | + :type code_env_name: str |
| 160 | + :param target: name of the target |
| 161 | + :type target: str |
| 162 | + """ |
| 163 | + if model_type not in {"REGRESSION", "BINARY_CLASSIFICATION", "MULTICLASS", "OTHER"}: |
| 164 | + raise ValueError('Invalid prediction type: {}'.format(model_type)) |
| 165 | + |
| 166 | + if classes and model_type not in {"BINARY_CLASSIFICATION", "MULTICLASS"}: |
| 167 | + raise ValueError('Classes can be specified only for BINARY_CLASSIFICATION or MULTICLASS prediction types') |
| 168 | + if model_type in {"BINARY_CLASSIFICATION", "MULTICLASS"}: |
| 169 | + if not classes: |
| 170 | + raise ValueError('Classes must be specified for {} prediction type'.format(model_type)) |
| 171 | + if not isinstance(classes, list): |
| 172 | + raise ValueError('Wrong type for classes: {}'.format(type(classes))) |
| 173 | + for cur_class in classes: |
| 174 | + if cur_class is None: |
| 175 | + raise ValueError('class can not be None') |
| 176 | + if not isinstance(cur_class, str): |
| 177 | + raise ValueError('Wrong type for class {}: {}'.format(cur_class, type(cur_class))) |
| 178 | + |
| 179 | + if code_env_name and not isinstance(code_env_name, str): |
| 180 | + raise ValueError('code_env_name must be a string') |
| 181 | + if target and not isinstance(target, str): |
| 182 | + raise ValueError('target must be a string') |
| 183 | + |
| 184 | + params = { |
| 185 | + "run_id": run_id, |
| 186 | + "prediction_type": model_type |
| 187 | + } |
| 188 | + |
| 189 | + if classes: |
| 190 | + params["classes"] = json.dumps(classes) |
| 191 | + if code_env_name: |
| 192 | + params["code_env_name"] = code_env_name |
| 193 | + if target: |
| 194 | + params["target"] = target |
| 195 | + |
| 196 | + self.client._perform_http( |
| 197 | + "POST", "/api/2.0/mlflow/extension/set-run-inference-info", |
| 198 | + headers={"x-dku-mlflow-project-key": self.project_key}, |
| 199 | + body=params |
| 200 | + ) |
0 commit comments