-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstatistics.py
288 lines (223 loc) · 10.4 KB
/
statistics.py
1
2
3
4
5
6
7
8
9
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from .utils import DSSDatasetSelectionBuilder
from .future import DSSFuture
class DSSStatisticsWorksheet(object):
"""
A handle to interact with a worksheet.
.. important::
Do not create this class directly, instead use :meth:`dataikuapi.dss.dataset.DSSDataset.get_statistics_worksheet`
"""
def __init__(self, client, project_key, dataset_name, worksheet_id):
self.client = client
self.project_key = project_key
self.dataset_name = dataset_name
self.worksheet_id = worksheet_id
def delete(self):
"""
Deletes the worksheet.
"""
self.client._perform_empty(
"DELETE", "/projects/%s/datasets/%s/statistics/worksheets/%s" % (self.project_key, self.dataset_name, self.worksheet_id))
def get_settings(self):
"""
Gets the worksheet settings.
:returns: a handle for the worksheet settings
:rtype: :class:`DSSStatisticsWorksheetSettings`
"""
worksheet_json = self.client._perform_json(
"GET", "/projects/%s/datasets/%s/statistics/worksheets/%s" % (
self.project_key, self.dataset_name, self.worksheet_id)
)
return DSSStatisticsWorksheetSettings(self.client, self.project_key,
self.dataset_name, self.worksheet_id, worksheet_json)
def run_worksheet(self, wait=True):
"""
Computes the result of the whole worksheet.
When `wait` is `True`, the method waits for the computation to complete and returns the corresponding result,
otherwise it returns immediately a handle for a :class:`~dataikuapi.dss.future.DSSFuture` result.
:param bool wait: a flag to wait for the computation to complete (defaults to **True**)
:returns: the corresponding card result or a handle for the future result
:rtype: :class:`~DSSStatisticsCardResult` or :class:`~dataikuapi.dss.future.DSSFuture`
"""
root_card = self.get_settings().get_raw()['rootCard']
return self.run_card(root_card, wait=wait)
def run_card(self, card, wait=True):
"""
Computes the result of a card in the context of the worksheet.
When `wait` is `True`, the method waits for the computation to complete and returns the corresponding result,
otherwise it returns immediately a handle for a :class:`~dataikuapi.dss.future.DSSFuture` result.
.. note::
The card does not need to belong to the worksheet.
:param card: the card to compute
:type card: :class:`~DSSStatisticsCardSettings` or dict obtained from :meth:`DSSStatisticsCardSettings.get_raw`
:param bool wait: a flag to wait for the computations to complete (defaults to **True**)
:returns: the corresponding card result or a handle for the future result
:rtype: :class:`DSSStatisticsCardResult` or :class:`~dataikuapi.dss.future.DSSFuture`
"""
card = DSSStatisticsCardSettings._from_card_or_dict(self.client, card)
future_response = self.client._perform_json(
"POST",
"/projects/%s/datasets/%s/statistics/worksheets/%s/actions/run-card" % (
self.project_key, self.dataset_name, self.worksheet_id),
body=card.get_raw()
)
future = DSSFuture(self.client, future_response.get("jobId", None), future_response,
result_wrapper=lambda raw_result: DSSStatisticsCardResult(raw_result))
return future.wait_for_result() if wait else future
def run_computation(self, computation, wait=True):
"""
Runs a computation in the context of the worksheet.
When `wait` is `True`, the method waits for the computation to complete and returns the corresponding result,
otherwise it returns immediately a handle for a :class:`~dataikuapi.dss.future.DSSFuture` result.
:param computation: the computation to perform
:type computation: :class:`DSSStatisticsComputationSettings` or dict obtained from
:meth:`DSSStatisticsComputationSettings.get_raw`
:param bool wait: a flag to wait for the computations to complete (defaults to **True**)
:returns: the corresponding computation result or a handle for the future result
:returns: :class:`DSSStatisticsComputationResult` or :class:`~dataikuapi.dss.future.DSSFuture`
"""
computation = DSSStatisticsComputationSettings._from_computation_or_dict(
computation)
future_response = self.client._perform_json(
"POST",
"/projects/%s/datasets/%s/statistics/worksheets/%s/actions/run-computation" % (
self.project_key, self.dataset_name, self.worksheet_id),
body=computation.get_raw()
)
future = DSSFuture(self.client, future_response.get("jobId", None), future_response,
result_wrapper=lambda raw_result: DSSStatisticsComputationResult(raw_result))
return future.wait_for_result() if wait else future
class DSSStatisticsWorksheetSettings(object):
"""
A handle to interact with the worksheet settings.
.. important::
Do not create this class directly, instead use :meth:`DSSStatisticsWorksheet.get_settings`
"""
def __init__(self, client, project_key, dataset_name, worksheet_id, worksheet_definition):
self._worksheet_definition = worksheet_definition
self.client = client
self.project_key = project_key
self.dataset_name = dataset_name
self.worksheet_id = worksheet_id
def add_card(self, card):
"""
Adds a new card to the worksheet.
:param card: the card to add
:type card: :class:`DSSStatisticsCardSettings` or dict obtained from :meth:`DSSStatisticsCardSettings.get_raw`
"""
card = DSSStatisticsCardSettings._from_card_or_dict(self.client, card)
self._worksheet_definition['rootCard']['cards'].append(card.get_raw())
def list_cards(self):
"""
Lists the cards for the worksheet.
:returns: a list of card handles
:rtype: list of :class:`DSSStatisticsCardSettings`
"""
return [DSSStatisticsCardSettings(self.client, card_definition)
for card_definition in self._worksheet_definition['rootCard']['cards']]
def get_raw(self):
"""
Gets a reference to the raw representation of the worksheet settings.
:returns: the worksheet settings
:rtype: dict
"""
return self._worksheet_definition
def set_sampling_settings(self, selection):
"""
Sets the worksheet sampling settings.
:param selection: the sampling settings
:type selection: :class:`~dataikuapi.dss.utils.DSSDatasetSelectionBuilder` or dict obtained from
:meth:`get_raw_sampling_settings`
"""
raw_selection = selection.build() if isinstance(
selection, DSSDatasetSelectionBuilder) else selection
self._worksheet_definition['dataSpec']['datasetSelection'] = raw_selection
def get_raw_sampling_settings(self):
"""
Gets a reference to the raw representation of the worksheet sampling settings.
:returns: the sampling settings
:rtype: dict
"""
return self._worksheet_definition['dataSpec']['datasetSelection']
def save(self):
"""
Saves the settings of the worksheet.
"""
self._worksheet_definition = self.client._perform_json(
"PUT",
"/projects/%s/datasets/%s/statistics/worksheets/%s" % (
self.project_key, self.dataset_name, self.worksheet_id),
body=self._worksheet_definition
)
class DSSStatisticsCardSettings(object):
"""
A handle to interact with the card settings.
"""
def __init__(self, client, card_definition):
self.client = client
self._card_definition = card_definition
def get_raw(self):
"""
Gets a reference to the raw representation of the card settings.
:returns: the card settings
:rtype: dict
"""
return self._card_definition
def compile(self):
"""
Gets the computation used to compute the card result.
:returns: the computation settings
:rtype: :class:`DSSStatisticsComputationSettings`
"""
computation_json = self.client._perform_json(
"POST", "/statistics/cards/compile", body=self._card_definition
)
return DSSStatisticsComputationSettings(computation_json)
@staticmethod
def _from_card_or_dict(client, card_or_dict):
if isinstance(card_or_dict, DSSStatisticsCardSettings):
card_or_dict = card_or_dict.get_raw()
return DSSStatisticsCardSettings(client, card_or_dict)
class DSSStatisticsCardResult(object):
"""
A handle to interact with the computed result of a :class:`DSSStatisticsCardSettings`.
"""
def __init__(self, card_result):
self._card_result = card_result
def get_raw(self):
"""
Gets a reference to the raw representation of the card result.
:returns: the card result
:rtype: dict
"""
return self._card_result
class DSSStatisticsComputationSettings(object):
"""
A handle to interact with the computation settings.
"""
def __init__(self, computation_definition):
self._computation_definition = computation_definition
def get_raw(self):
"""
Gets a reference to the raw representation of the computation settings.
:returns: the computation settings
:rtype: dict
"""
return self._computation_definition
@staticmethod
def _from_computation_or_dict(computation_or_dict):
if isinstance(computation_or_dict, DSSStatisticsComputationSettings):
computation_or_dict = computation_or_dict.get_raw()
return DSSStatisticsComputationSettings(computation_or_dict)
class DSSStatisticsComputationResult(object):
"""
A handle to interact with the computed result of a :class:`DSSStatisticsComputationSettings`.
"""
def __init__(self, computation_result):
self._computation_result = computation_result
def get_raw(self):
"""
Gets a reference to the raw representation of the computation result.
:returns: the computation result
:rtype: dict
"""
return self._computation_result