Skip to content

Commit ee8c2e7

Browse files
lpenetcbrun-dku
andauthored
MEC public API (#160)
* MEC public API * Applying name change to model comparisons * Naming, wording. * Removing id examples and info on location. * Turning prediction_type and display_name into properties. * Removing no longer needed import. * Nitpick wording and punctuation. * Use [ ... for ... in ] rather than map (python 2/3 consistency) Co-authored-by: Clément BRUN <57680293+cbrun-dku@users.noreply.github.com> Co-authored-by: Clément BRUN <57680293+cbrun-dku@users.noreply.github.com>
1 parent bda26be commit ee8c2e7

File tree

3 files changed

+215
-4
lines changed

3 files changed

+215
-4
lines changed

dataikuapi/dss/modelcomparison.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+

dataikuapi/dss/modelevaluationstore.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ def get_full_info(self):
278278
"GET", "/projects/%s/modelevaluationstores/%s/runs/%s" % (self.project_key, self.mes_id, self.run_id))
279279
return DSSModelEvaluationFullInfo(self, data)
280280

281+
def get_full_id(self):
282+
return "ME-{}-{}-{}".format(self.project_key, self.mes_id, self.run_id)
283+
281284
def delete(self):
282285
"""
283286
Remove this model evaluation

dataikuapi/dss/project.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time, warnings, sys, os.path as osp
22
from .dataset import DSSDataset, DSSDatasetListItem, DSSManagedDatasetCreationHelper
3+
from .modelcomparison import DSSModelComparison
34
from .jupyternotebook import DSSJupyterNotebook, DSSJupyterNotebookListItem
45
from .notebook import DSSNotebook
56
from .streaming_endpoint import DSSStreamingEndpoint, DSSStreamingEndpointListItem, DSSManagedStreamingEndpointCreationHelper
@@ -783,17 +784,15 @@ def get_model_evaluation_store(self, mes_id):
783784
"""
784785
return DSSModelEvaluationStore(self.client, self.project_key, mes_id)
785786

786-
def create_model_evaluation_store(self, name, mes_id=None):
787+
def create_model_evaluation_store(self, name):
787788
"""
788789
Create a new model evaluation store in the project, and return a handle to interact with it.
789790
790791
:param string name: the name for the new model evaluation store
791-
:param string mes_id optional: the id for the new model evaluation store
792-
792+
793793
:returns: A :class:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluationStore` model evaluation store handle
794794
"""
795795
obj = {
796-
"id" : mes_id,
797796
"projectKey" : self.project_key,
798797
"name" : name
799798
}
@@ -802,6 +801,51 @@ def create_model_evaluation_store(self, name, mes_id=None):
802801
mes_id = res['id']
803802
return DSSModelEvaluationStore(self.client, self.project_key, mes_id)
804803

804+
########################################################
805+
# Model comparisons
806+
########################################################
807+
808+
def list_model_comparisons(self):
809+
"""
810+
List the model comparisons in this project.
811+
812+
:returns: The list of the model comparisons
813+
:rtype: list
814+
"""
815+
items = self.client._perform_json("GET", "/projects/%s/modelcomparisons/" % self.project_key)
816+
return [DSSModelComparison(self.client, self.project_key, item["id"]) for item in items]
817+
818+
def get_model_comparison(self, mec_id):
819+
"""
820+
Get a handle to interact with a specific model comparison
821+
822+
:param string mec_id: the id of the desired model comparison
823+
824+
:returns: A handle on a model comparison
825+
:rtype: :class:`dataikuapi.dss.modelcomparison.DSSModelComparison`
826+
"""
827+
return DSSModelComparison(self.client, self.project_key, mec_id)
828+
829+
def create_model_comparison(self, name, prediction_type):
830+
"""
831+
Create a new model comparison in the project, and return a handle to interact with it.
832+
833+
:param string name: the name for the new model comparison
834+
:param string prediction_type: one of BINARY_CLASSIFICATION, REGRESSION and MULTICLASS
835+
836+
:returns: A handle on a new model comparison
837+
:rtype: :class:`dataikuapi.dss.modelcomparison.DSSModelComparison`
838+
"""
839+
obj = {
840+
"projectKey": self.project_key,
841+
"displayName": name,
842+
"predictionType": prediction_type
843+
}
844+
res = self.client._perform_json("POST", "/projects/%s/modelcomparisons/" % self.project_key,
845+
body = obj)
846+
mec_id = res['id']
847+
return DSSModelComparison(self.client, self.project_key, mec_id)
848+
805849
########################################################
806850
# Jobs
807851
########################################################

0 commit comments

Comments
 (0)