|
| 1 | +from dataikuapi.dss.discussion import DSSObjectDiscussions |
| 2 | + |
| 3 | + |
| 4 | +class DSSModelComparison(object): |
| 5 | + """ |
| 6 | + A handle to interact with a model comparison on the DSS instance |
| 7 | +
|
| 8 | + Do not create this directly, use :meth:`dataikuapi.dss.DSSProject.get_model_comparison` |
| 9 | + """ |
| 10 | + def __init__(self, client, project_key, mec_id): |
| 11 | + self.client = client |
| 12 | + self.project = client.get_project(project_key) |
| 13 | + self.project_key = project_key |
| 14 | + self.mec_id = mec_id |
| 15 | + |
| 16 | + @property |
| 17 | + def id(self): |
| 18 | + return self.mec_id |
| 19 | + |
| 20 | + def get_settings(self): |
| 21 | + """ |
| 22 | + Returns the settings of this model comparison |
| 23 | +
|
| 24 | + :rtype: :class:`dataikuapi.dss.modelcomparison.DSSModelComparisonSettings` |
| 25 | + """ |
| 26 | + data = self.client._perform_json( |
| 27 | + "GET", "/projects/%s/modelcomparisons/%s" % (self.project_key, self.mec_id)) |
| 28 | + return DSSModelComparisonSettings(self, data) |
| 29 | + |
| 30 | + def get_object_discussions(self): |
| 31 | + """ |
| 32 | + Get a handle to manage discussions on the model comparison |
| 33 | +
|
| 34 | + :returns: the handle to manage discussions |
| 35 | + :rtype: :class:`dataikuapi.discussion.DSSObjectDiscussions` |
| 36 | + """ |
| 37 | + return DSSObjectDiscussions(self.client, self.project_key, "MODEL_COMPARISON", self.mec_id) |
| 38 | + |
| 39 | + ######################################################## |
| 40 | + # Deletion |
| 41 | + ######################################################## |
| 42 | + |
| 43 | + def delete(self): |
| 44 | + """ |
| 45 | + Delete the model comparison |
| 46 | +
|
| 47 | + """ |
| 48 | + return self.client._perform_empty("DELETE", "/projects/%s/modelcomparisons/%s" % (self.project_key, self.mec_id)) |
| 49 | + |
| 50 | + |
| 51 | +class DSSModelComparisonSettings(object): |
| 52 | + """ |
| 53 | + A handle on the settings of a model comparison |
| 54 | +
|
| 55 | + A model comparison has: |
| 56 | + - a display name ; |
| 57 | + - a prediction type ; |
| 58 | + - a list of full ids of items to compare |
| 59 | +
|
| 60 | + The prediction type can be: |
| 61 | + - BINARY_CLASSIFICATION, |
| 62 | + - REGRESSION, |
| 63 | + - MULTICLASS |
| 64 | +
|
| 65 | + The full ids are: |
| 66 | + - the model id of a Lab Model, |
| 67 | + - the model id of a saved model version, |
| 68 | + - the model evaluation id of a model evaluation. |
| 69 | +
|
| 70 | +
|
| 71 | + Do not create this class directly, instead use :meth:`dataikuapi.dss.DSSModelComparison.get_settings` |
| 72 | + """ |
| 73 | + def __init__(self, model_comparison, settings): |
| 74 | + self.model_comparison = model_comparison |
| 75 | + self.settings = settings |
| 76 | + |
| 77 | + def get_raw(self): |
| 78 | + """ |
| 79 | + Get raw settings of a model comparison |
| 80 | +
|
| 81 | + :return: the raw settings of comparison, as a dict. Modifications made to the returned object |
| 82 | + are reflected when saving |
| 83 | + :rtype: dict |
| 84 | + """ |
| 85 | + return self.settings |
| 86 | + |
| 87 | + def add_compared_item(self, full_id): |
| 88 | + """ |
| 89 | + Add an item to the list of compared items |
| 90 | +
|
| 91 | + :param full_id: full id of the item (lab model, saved model version, model evaluation) to add |
| 92 | + """ |
| 93 | + if "comparedModels" not in self.settings: |
| 94 | + self.settings["comparedModels"] = [] |
| 95 | + self.settings["comparedModels"].append({ |
| 96 | + "refId": full_id |
| 97 | + }) |
| 98 | + |
| 99 | + def remove_compared_item(self, full_id): |
| 100 | + """ |
| 101 | + Remove an item from the list of compared items |
| 102 | +
|
| 103 | + :param full_id: full id of the item (lab model, saved model version, model evaluation) to remove |
| 104 | + """ |
| 105 | + if not self.settings["comparedModels"]: |
| 106 | + return |
| 107 | + self.settings["comparedModels"] = filter(lambda x: x["refId"] != full_id, self.settings["comparedModels"]) |
| 108 | + |
| 109 | + def get_compared_items(self): |
| 110 | + """ |
| 111 | + Get the full ids of items compared in this comparison |
| 112 | +
|
| 113 | + :return: the list of the full ids of compared items |
| 114 | + :rtype: list[str] |
| 115 | + """ |
| 116 | + if not self.settings["comparedModels"]: |
| 117 | + return [] |
| 118 | + return [x["refId"] for x in self.settings["comparedModels"]] |
| 119 | + |
| 120 | + @property |
| 121 | + def prediction_type(self): |
| 122 | + """ |
| 123 | + Get the prediction type of this comparison |
| 124 | +
|
| 125 | + :return: str |
| 126 | + """ |
| 127 | + return self.settings["predictionType"] |
| 128 | + |
| 129 | + @prediction_type.setter |
| 130 | + def prediction_type(self, prediction_type): |
| 131 | + """ |
| 132 | + Set the prediction type of this comparison. Must be consistent |
| 133 | + with the prediction types of compared items. |
| 134 | +
|
| 135 | + :param prediction_type: |
| 136 | + """ |
| 137 | + self.settings["predictionType"] = prediction_type |
| 138 | + |
| 139 | + @property |
| 140 | + def display_name(self): |
| 141 | + """ |
| 142 | + Human readable name of this comparison |
| 143 | +
|
| 144 | + :return: str |
| 145 | + """ |
| 146 | + return self.settings["displayName"] |
| 147 | + |
| 148 | + @display_name.setter |
| 149 | + def display_name(self, display_name): |
| 150 | + """ |
| 151 | + Set the human readable name of this comparison |
| 152 | +
|
| 153 | + :param display_name: |
| 154 | + """ |
| 155 | + self.settings["displayName"] = display_name |
| 156 | + |
| 157 | + def save(self): |
| 158 | + """ |
| 159 | + Save settings modifications |
| 160 | + """ |
| 161 | + self.model_comparison.client._perform_empty( |
| 162 | + "PUT", "/projects/%s/modelcomparisons/%s" % (self.model_comparison.project_key, self.model_comparison.mec_id), |
| 163 | + body=self.settings) |
| 164 | + |
0 commit comments