-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodelevaluationstore.py
761 lines (612 loc) · 30.7 KB
/
modelevaluationstore.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
import json
import warnings
from io import BytesIO
from .metrics import ComputedMetrics
from .discussion import DSSObjectDiscussions
from .future import DSSFuture
try:
basestring
except NameError:
basestring = str
class DSSModelEvaluationStore(object):
"""
A handle to interact with a model evaluation store on the DSS instance.
.. warning::
Do not create this directly, use :meth:`dataikuapi.dss.project.DSSProject.get_model_evaluation_store`
"""
def __init__(self, client, project_key, mes_id):
self.client = client
self.project = client.get_project(project_key)
self.project_key = project_key
self.mes_id = mes_id
@property
def id(self):
return self.mes_id
def get_settings(self):
"""
Returns the settings of this model evaluation store.
:rtype: DSSModelEvaluationStoreSettings
"""
data = self.client._perform_json(
"GET", "/projects/%s/modelevaluationstores/%s" % (self.project_key, self.mes_id))
return DSSModelEvaluationStoreSettings(self, data)
########################################################
# Misc
########################################################
def get_zone(self):
"""
Gets the flow zone of this model evaluation store
:rtype: :class:`dataikuapi.dss.flow.DSSFlowZone`
"""
return self.project.get_flow().get_zone_of_object(self)
def move_to_zone(self, zone):
"""
Moves this object to a flow zone
:param object zone: a :class:`dataikuapi.dss.flow.DSSFlowZone` where to move the object
"""
if isinstance(zone, basestring):
zone = self.project.get_flow().get_zone(zone)
zone.add_item(self)
def share_to_zone(self, zone):
"""
Share this object to a flow zone
:param object zone: a :class:`dataikuapi.dss.flow.DSSFlowZone` where to share the object
"""
if isinstance(zone, basestring):
zone = self.project.get_flow().get_zone(zone)
zone.add_shared(self)
def unshare_from_zone(self, zone):
"""
Unshare this object from a flow zone
:param object zone: a :class:`dataikuapi.dss.flow.DSSFlowZone` from where to unshare the object
"""
if isinstance(zone, basestring):
zone = self.project.get_flow().get_zone(zone)
zone.remove_shared(self)
def get_usages(self):
"""
Get the recipes referencing this model evaluation store
:return: a list of usages
"""
return self.client._perform_json("GET", "/projects/%s/modelevaluationstores/%s/usages" % (self.project_key, self.mes_id))
def get_object_discussions(self):
"""
Get a handle to manage discussions on the model evaluation store
:returns: the handle to manage discussions
:rtype: :class:`dataikuapi.discussion.DSSObjectDiscussions`
"""
return DSSObjectDiscussions(self.client, self.project_key, "MODEL_EVALUATION_STORE", self.mes_id)
########################################################
# Deletion
########################################################
def delete(self):
"""
Delete the model evaluation store
"""
return self.client._perform_empty("DELETE", "/projects/%s/modelevaluationstores/%s" % (self.project_key, self.mes_id))
########################################################
# Model evaluations
########################################################
def list_model_evaluations(self):
"""
List the model evaluations in this model evaluation store. The list is sorted
by ME creation date.
:returns: The list of the model evaluations
:rtype: list of :class:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluation`
"""
items = self.client._perform_json("GET", "/projects/%s/modelevaluationstores/%s/evaluations/" % (self.project_key, self.mes_id))
return [DSSModelEvaluation(self, item["ref"]["evaluationId"]) for item in items]
def get_model_evaluation(self, evaluation_id):
"""
Get a handle to interact with a specific model evaluation
:param string evaluation_id: the id of the desired model evaluation
:returns: A :class:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluation` model evaluation handle
"""
return DSSModelEvaluation(self, evaluation_id)
def get_latest_model_evaluation(self):
"""
Get a handle to interact with the latest model evaluation computed
:returns: A :class:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluation` model evaluation handle
if the store is not empty, else None
"""
latest_evaluation_id = self.client._perform_text(
"GET", "/projects/%s/modelevaluationstores/%s/latestEvaluationId" % (self.project_key, self.mes_id))
if not latest_evaluation_id:
return None
return DSSModelEvaluation(self, latest_evaluation_id)
def delete_model_evaluations(self, evaluations):
"""
Remove model evaluations from this store
"""
obj = []
for evaluation in evaluations:
if isinstance(evaluation, DSSModelEvaluation):
obj.append(evaluation.evaluation_id)
elif isinstance(evaluation, dict):
obj.append(evaluation['evaluation_id'])
else:
obj.append(evaluation)
self.client._perform_json(
"DELETE", "/projects/%s/modelevaluationstores/%s/evaluations/" % (self.project_key, self.mes_id), body=obj)
def build(self, job_type="NON_RECURSIVE_FORCED_BUILD", wait=True, no_fail=False):
"""
Starts a new job to build this model evaluation store and wait for it to complete.
Raises if the job failed.
.. code-block:: python
job = mes.build()
print("Job %s done" % job.id)
:param job_type: The job type. One of RECURSIVE_BUILD, NON_RECURSIVE_FORCED_BUILD or RECURSIVE_FORCED_BUILD
:param wait: wait for the build to finish before returning
:param no_fail: if True, does not raise if the job failed. Valid only when wait is True
:return: the :class:`dataikuapi.dss.job.DSSJob` job handle corresponding to the built job
:rtype: :class:`dataikuapi.dss.job.DSSJob`
"""
jd = self.project.new_job(job_type)
jd.with_output(self.mes_id, object_type="MODEL_EVALUATION_STORE")
if wait:
return jd.start_and_wait(no_fail)
else:
return jd.start(allowFail=not no_fail)
########################################################
# Metrics
########################################################
def get_last_metric_values(self):
"""
Get the metrics of the latest model evaluation built
:returns:
a list of metric objects and their value
"""
return ComputedMetrics(self.client._perform_json(
"GET", "/projects/%s/modelevaluationstores/%s/metrics/last" % (self.project_key, self.mes_id)))
def get_metric_history(self, metric):
"""
Get the history of the values of the metric on this model evaluation store
:returns:
an object containing the values of the metric, cast to the appropriate type (double, boolean,...)
"""
return self.client._perform_json(
"GET", "/projects/%s/modelevaluationstores/%s/metrics/history" % (self.project_key, self.mes_id),
params={'metricLookup': metric if isinstance(metric, str)or isinstance(metric, unicode)
else json.dumps(metric)})
def compute_metrics(self, metric_ids=None, probes=None):
"""
Compute metrics on this model evaluation store. If the metrics are not specified, the metrics
setup on the model evaluation store are used.
"""
url = "/projects/%s/modelevaluationstores/%s/actions" % (self.project_key, self.mes_id)
if metric_ids is not None:
return self.client._perform_json(
"POST" , "%s/computeMetricsFromIds" % url,
body={"metricIds" : metric_ids})
elif probes is not None:
return self.client._perform_json(
"POST" , "%s/computeMetrics" % url,
body=probes)
else:
return self.client._perform_json(
"POST" , "%s/computeMetrics" % url)
def run_checks(self, evaluation_id='', checks=None):
"""
Run checks on a partition of this model evaluation store.
If the checks are not specified, the checks
setup on the model evaluation store are used.
:param str evaluation_id: (optional) id of evaluation on which checks should be run. Last evaluation is used if not specified.
:param list[string] checks: (optional) ids of the checks to run.
:returns: a checks computation report, as a dict.
:rtype: dict
"""
if checks is None:
return self.client._perform_json(
"POST", "/projects/%s/modelevaluationstores/%s/actions/runChecks" %(self.project_key, self.mes_id),
params={'evaluationId': evaluation_id})
else:
return self.client._perform_json(
"POST", "/projects/%s/modelevaluationstores/%s/actions/runChecks" %(self.project_key, self.mes_id),
params={'evaluationId': evaluation_id}, body=checks)
class MetricDefinition(dict):
def __init__(self, code, value, name=None, description=None):
dict.__init__(self, {"metricCode": code, "value": value, "name": name, "description": description})
class LabelDefinition(dict):
def __init__(self, key, value):
dict.__init__(self, {"key": key, "value": value})
def add_custom_model_evaluation(self, metrics, evaluation_id=None, name=None, labels=None, model=None):
"""
Adds a model evaluation with custom metrics to the model evaluation store.
:param list[DSSModelEvaluationStore.MetricDefinition] metrics: the metrics to add.
:param str evaluation_id: the id of the evaluation (optional)
:param str name: the human-readable name of the evaluation (optional)
:param list[DSSModelEvaluationStore.LabelDefinition] labels: labels to set on the model evaluation (optionam). See below.
:param model: saved model version (full ID or DSSTrainedPredictionModelDetails) of the evaluated model (optional)
:type model: Union[str, DSSTrainedPredictionModelDetails]
Code sample:
.. code-block:: python
import dataiku
from dataikuapi.dss.modelevaluationstore import DSSModelEvaluationStore
client=dataiku.api_client()
project=client.get_default_project()
mes=project.get_model_evaluation_store("7vFZWNck")
accuracy = DSSModelEvaluationStore.MetricDefinition("accuracy", 0.95, "Accuracy")
other = DSSModelEvaluationStore.MetricDefinition("other", 42, "Other", "Other metric desc")
label = DSSModelEvaluationStore.LabelDefinition("custom:myLabel", "myValue")
mes.add_custom_model_evaluation([accuracy, pouet], labels=[label])
mes.run_checks()
"""
if hasattr(model, 'full_id'):
model = model.full_id
url = "/projects/%s/modelevaluationstores/%s/evaluations" % (self.project_key, self.mes_id)
return self.client._perform_json(
"POST", url,
body={
"evaluationId": evaluation_id,
"name": name,
"metrics": metrics,
"labels": labels,
"fullModelId": model
})
class DSSModelEvaluationStoreSettings:
"""
A handle on the settings of a model evaluation store
.. warning::
Do not create this class directly, instead use :meth:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluationStore.get_settings`
"""
def __init__(self, model_evaluation_store, settings):
self.model_evaluation_store = model_evaluation_store
self.settings = settings
def get_raw(self):
return self.settings
def save(self):
self.model_evaluation_store.client._perform_empty(
"PUT", "/projects/%s/modelevaluationstores/%s" % (self.model_evaluation_store.project_key, self.model_evaluation_store.mes_id),
body=self.settings)
class DSSModelEvaluation:
"""
A handle on a model evaluation
.. warning::
Do not create this class directly, instead use :meth:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluationStore.get_model_evaluation`
"""
def __init__(self, model_evaluation_store, evaluation_id):
self.model_evaluation_store = model_evaluation_store
self.client = model_evaluation_store.client
# unpack some fields
self.evaluation_id = evaluation_id
self.project_key = model_evaluation_store.project_key
self.mes_id = model_evaluation_store.mes_id
def get_full_info(self):
"""
Retrieve the model evaluation with its performance data
:return: the model evaluation full info, as a :class:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluationFullInfo`
"""
data = self.client._perform_json(
"GET", "/projects/%s/modelevaluationstores/%s/evaluations/%s" % (self.project_key, self.mes_id, self.evaluation_id))
return DSSModelEvaluationFullInfo(self, data)
def get_full_id(self):
return "ME-{}-{}-{}".format(self.project_key, self.mes_id, self.evaluation_id)
def delete(self):
"""
Remove this model evaluation
"""
obj = [self.evaluation_id]
self.client._perform_json(
"DELETE", "/projects/%s/modelevaluationstores/%s/evaluations/" % (self.project_key, self.mes_id), body=obj)
@property
def full_id(self):
return "ME-%s-%s-%s"%(self.project_key, self.mes_id, self.evaluation_id)
def compute_data_drift(self, reference=None, data_drift_params=None, wait=True):
"""
Compute data drift against a reference model or model evaluation. The reference is determined automatically unless specified.
.. attention::
Deprecated. Use :meth:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluationStore.compute_drift` instead
:param reference: saved model version (full ID or DSSTrainedPredictionModelDetails)
or model evaluation (full ID or DSSModelEvaluation) to use as reference (optional)
:type reference: Union[str, DSSModelEvaluation, DSSTrainedPredictionModelDetails]
:param data_drift_params: data drift computation settings as a :class:`dataikuapi.dss.modelevaluationstore.DataDriftParams` (optional)
:type data_drift_params: DataDriftParams
:param wait: data drift computation settings (optional)
:returns: a :class:`dataikuapi.dss.modelevaluationstore.DataDriftResult` containing data drift analysis results if `wait` is `True`, or a :class:`~dataikuapi.dss.future.DSSFuture` handle otherwise
"""
warnings.warn("This method is deprecated. Use DSSModelEvaluationStore.compute_drift instead", DeprecationWarning)
if hasattr(reference, 'full_id'):
reference = reference.full_id
if data_drift_params:
data_drift_params = data_drift_params.data
future_response = self.client._perform_json(
"POST", "/projects/%s/modelevaluationstores/%s/evaluations/%s/computeDataDrift" % (self.project_key, self.mes_id, self.evaluation_id),
body={
"referenceId": reference,
"dataDriftParams": data_drift_params
})
future = DSSFuture(self.client, future_response.get('jobId', None), future_response, result_wrapper=DataDriftResult)
return future.wait_for_result() if wait else future
def compute_drift(self, reference=None, drift_params=None, wait=True):
"""
Compute drift against a reference model or model evaluation. The reference is determined automatically unless specified.
:param reference: saved model version (full ID or DSSTrainedPredictionModelDetails)
or model evaluation (full ID or DSSModelEvaluation) to use as reference (optional)
:type reference: Union[str, DSSModelEvaluation, DSSTrainedPredictionModelDetails]
:param drift_params: drift computation settings as a :class:`dataikuapi.dss.modelevaluationstore.DriftParams` (optional)
:type drift_params: DriftParams
:param wait: data drift computation settings (optional)
:returns: a :class:`dataikuapi.dss.modelevaluationstore.DriftResult` containing data drift analysis results if `wait` is `True`, or a :class:`~dataikuapi.dss.future.DSSFuture` handle otherwise
"""
if hasattr(reference, 'full_id'):
reference = reference.full_id
if drift_params:
drift_params = drift_params.data
future_response = self.client._perform_json(
"POST", "/projects/%s/modelevaluationstores/%s/evaluations/%s/computeDrift" % (self.project_key, self.mes_id, self.evaluation_id),
body={
"referenceId": reference,
"driftParams": drift_params
})
future = DSSFuture(self.client, future_response.get('jobId', None), future_response, result_wrapper=DriftResult)
return future.wait_for_result() if wait else future
def get_metrics(self):
"""
Get the metrics for this model evaluation. Metrics must be understood here as Metrics in DSS Metrics & Checks
:return: the metrics, as a JSON object
"""
return self.client._perform_json(
"GET", "/projects/%s/modelevaluationstores/%s/evaluations/%s/metrics" % (self.project_key, self.mes_id, self.evaluation_id))
def get_sample_df(self):
"""
Get the sample of the evaluation dataset on which the evaluation was performed
:return:
the sample content, as a :class:`pandas.DataFrame`
"""
buf = BytesIO()
with self.client._perform_raw(
"GET",
"/projects/%s/modelevaluationstores/%s/evaluations/%s/sample" % (self.project_key, self.mes_id, self.evaluation_id)
).raw as f:
buf.write(f.read())
schema_txt = self.client._perform_raw(
"GET",
"/projects/%s/modelevaluationstores/%s/evaluations/%s/schema" % (self.project_key, self.mes_id, self.evaluation_id)
).text
schema = json.loads(schema_txt)
import pandas as pd
return pd.read_csv(BytesIO(buf.getvalue()), compression='gzip', sep='\t', header=None, names=[c["name"] for c in schema["columns"]])
class DSSModelEvaluationFullInfo:
"""
A handle on the full information on a model evaluation.
Includes information such as the full id of the evaluated model, the evaluation params,
the performance and drift metrics, if any, etc.
.. warning::
Do not create this class directly, instead use :meth:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluation.get_full_info`
"""
def __init__(self, model_evaluation, full_info):
self.model_evaluation = model_evaluation
self.full_info = full_info
self.metrics = self.full_info["metrics"] # type: dict
"""The performance and data drift metric, if any."""
self.creation_date = self.full_info["evaluation"]["created"] # type: int
"""The date and time of the creation of the model evaluation, as an epoch."""
self.full_id = self.full_info["evaluation"]["ref"]["fullId"] # type: str
if "modelRef" in self.full_info["evaluation"]:
self.model_full_id = self.full_info["evaluation"]["modelRef"]["fullId"] # type: str
else:
self.model_full_id = None
self.prediction_type = self.full_info["evaluation"].get("predictionType") # type: str
self.prediction_variable = self.full_info["evaluation"].get("predictionVariable") # type: str
self.target_variable = self.full_info["evaluation"].get("targetVariable") # type: str
self.user_meta = self.full_info["evaluation"]["userMeta"] # type: dict
self.has_model = self.full_info["evaluation"].get("hasModel")
"""The user-accessible metadata (name, labels)
Returns the original object, not a copy. Changes to the returned object are persisted to DSS by calling :meth:`save_user_meta`."""
def get_raw(self):
return self.full_info
def save_user_meta(self):
return self.model_evaluation.client._perform_text(
"PUT", "/projects/%s/modelevaluationstores/%s/evaluations/%s/user-meta" %
(self.model_evaluation.project_key, self.model_evaluation.mes_id, self.model_evaluation.evaluation_id), body=self.user_meta)
class DataDriftParams(object):
"""
Object that represents parameters for data drift computation.
.. warning::
Do not create this object directly, use :meth:`dataikuapi.dss.modelevaluationstore.DataDriftParams.from_params` instead.
.. attention::
Deprecated. Use :class:`dataikuapi.dss.modelevaluationstore.DriftParams` instead
"""
def __init__(self, data):
self.data = data
def __repr__(self):
return u"{}({})".format(self.__class__.__name__, self.data)
@staticmethod
def from_params(per_column_settings, nb_bins=10, compute_histograms=True, confidence_level=0.95):
"""
Creates parameters for data drift computation from columns, number of bins, compute histograms and confidence level
:param dict per_column_settings: A dict representing the per column settings.
You should use a :class:`~dataikuapi.dss.modelevaluationstore.PerColumnDriftParamBuilder` to build it.
:param int nb_bins: (optional) Nb. bins in histograms (apply to all columns) - default: 10
:param bool compute_histograms: (optional) Enable/disable histograms - default: True
:param float confidence_level: (optional) Used to compute confidence interval on drift's model accuracy - default: 0.95
:rtype: :class:`dataikuapi.dss.modelevaluationstore.DataDriftParams`
"""
warnings.warn("This method is deprecated. Use DriftParams.from_params() instead", DeprecationWarning)
return DataDriftParams({
"columns": per_column_settings,
"nbBins": nb_bins,
"computeHistograms": compute_histograms,
"confidenceLevel": confidence_level
})
class DriftParams(object):
"""
Object that represents parameters for drift computation.
.. warning::
Do not create this object directly, use :meth:`dataikuapi.dss.modelevaluationstore.DriftParams.from_params` instead.
"""
def __init__(self, data):
self.data = data
def __repr__(self):
return u"{}({})".format(self.__class__.__name__, self.data)
@staticmethod
def from_params(per_column_settings, nb_bins=10, compute_histograms=True, confidence_level=0.95):
"""
Creates parameters for drift computation from columns, number of bins, compute histograms and confidence level
:param dict per_column_settings: A dict representing the per column settings.
You should use a :class:`~dataikuapi.dss.modelevaluationstore.PerColumnDriftParamBuilder` to build it.
:param int nb_bins: (optional) Nb. bins in histograms (apply to all columns) - default: 10
:param bool compute_histograms: (optional) Enable/disable histograms - default: True
:param float confidence_level: (optional) Used to compute confidence interval on drift's model accuracy - default: 0.95
:rtype: :class:`dataikuapi.dss.modelevaluationstore.DriftParams`
"""
return DriftParams({
"columns": per_column_settings,
"nbBins": nb_bins,
"computeHistograms": compute_histograms,
"confidenceLevel": confidence_level
})
class PerColumnDriftParamBuilder(object):
"""
Builder for a map of per column drift params settings.
Used as a helper before computing data drift to build columns param expected in
:meth:`dataikuapi.dss.modelevaluationstore.DataDriftParams.from_params`.
"""
def __init__(self):
self.columns = {}
def build(self):
"""Returns the built dict for per column drift params settings"""
return self.columns
def with_column_drift_param(self, name, handling="AUTO", enabled=True):
"""
Sets the drift params settings for given column name.
:param: string name: The name of the column
:param: string handling: (optional) The column type, should be either NUMERICAL, CATEGORICAL or AUTO (default: AUTO)
:param: bool enabled: (optional) False means the column is ignored in drift computation (default: True)
"""
self.columns[name] = {
"handling": handling,
"enabled": enabled
}
return self
class DataDriftResult(object):
"""
A handle on the data drift result of a model evaluation.
.. warning::
Do not create this class directly, instead use :meth:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluation.compute_data_drift`
"""
def __init__(self, data):
self.data = data
self.drift_model_result = DriftModelResult(self.data["driftModelResult"])
"""Drift analysis based on drift modeling."""
self.univariate_drift_result = UnivariateDriftResult(self.data["univariateDriftResult"])
"""Per-column drift analysis based on pairwise comparison of distributions."""
self.per_column_settings = [ColumnSettings(cs) for cs in self.data["perColumnSettings"]]
"""Information about column handling that has been used (errors, types, etc)."""
def get_raw(self):
"""
:return: the raw data drift result
:rtype: dict
"""
return self.data
class DriftResult(object):
"""
A handle on the drift result of a model evaluation.
.. warning::
Do not create this class directly, instead use :meth:`dataikuapi.dss.modelevaluationstore.DSSModelEvaluation.compute_drift`
"""
def __init__(self, data):
self.data = data
self.drift_model_result = DriftModelResult(self.data["driftModelResult"]) if "driftModelResult" in self.data else None
"""Drift analysis based on drift modeling."""
self.univariate_drift_result = UnivariateDriftResult(self.data["univariateDriftResult"]) if "univariateDriftResult" in self.data else None
"""Per-column drift analysis based on pairwise comparison of distributions."""
self.per_column_settings = [ColumnSettings(cs) for cs in self.data["perColumnSettings"]] if "perColumnSettings" in self.data else None
"""Information about column handling that has been used (errors, types, etc)."""
self.prediction_drift_result = PredictionDriftResult(self.data["predictionDriftResult"]) if "predictionDriftResult" in self.data else None
"""Drift analysis based on the prediction column"""
def get_raw(self):
"""
:return: the raw data drift result
:rtype: dict
"""
return self.data
class DriftModelResult(object):
"""
A handle on the drift model result.
.. warning::
Do not create this class directly, instead use :attr:`dataikuapi.dss.modelevaluationstore.DriftResult.drift_model_result`
"""
def __init__(self, data):
self.data = data
self.drift_model_accuracy = DriftModelAccuracy(self.data["driftModelAccuracy"])
self.feature_drift_importance = self.data["driftVersusImportance"] # type: dict
def get_raw(self):
"""
:return: the raw drift model result
:rtype: dict
"""
return self.data
class UnivariateDriftResult(object):
"""
A handle on the univariate data drift.
.. warning::
Do not create this class directly, instead use :attr:`dataikuapi.dss.modelevaluationstore.DriftResult.univariate_drift_result`
"""
def __init__(self, data):
self.data = data
self.per_column_drift_data = self.data["columns"] # type: dict
"""Drift data per column, as a dict of column name -> drift data."""
def get_raw(self):
"""
:return: the raw univariate data drift
:rtype: dict
"""
return self.data
class PredictionDriftResult(object):
"""
A handle on the prediction drift result.
.. warning::
Do not create this class directly, instead use :attr:`dataikuapi.dss.modelevaluationstore.DriftResult.prediction_drift_result`
"""
def __init__(self, data):
self.data = data
self.chiSquare = self.data.get('chiSquareTestPvalue', None)
self.ks = self.data.get('ksTestPvalue', None)
self.psi = self.data.get('populationStabilityIndex', None)
def get_raw(self):
"""
:return: the raw prediction drift
:rtype: dict
"""
return self.data
class ColumnSettings(object):
"""
A handle on column handling information.
.. warning::
Do not create this class directly, instead use :meth:`dataikuapi.dss.modelevaluationstore.DriftResult.get_per_column_settings`
"""
def __init__(self, data):
self.data = data
self.name = self.data["name"] # type: str
self.actual_column_handling = self.data["actualHandling"] # type: str
"""The actual column handling (either forced via drift params or inferred from model evaluation preprocessings).
It can be any of NUMERICAL, CATEGORICAL, or IGNORED."""
self.default_column_handling = self.data["defaultHandling"] # type: str
"""The default column handling (based on model evaluation preprocessing only).
It can be any of NUMERICAL, CATEGORICAL, or IGNORED."""
self.error_message = self.data.get("errorMessage", None)
def get_raw(self):
"""
:return: the raw column handling information
:rtype: dict
"""
return self.data
class DriftModelAccuracy(object):
"""
A handle on the drift model accuracy.
.. warning::
Do not create this class directly, instead use :attr:`dataikuapi.dss.modelevaluationstore.DriftModelResult.drift_model_accuracy`
"""
def __init__(self, data):
self.data = data
self.value = self.data["value"] # type: float
self.lower_confidence_interval = self.data["lower"] # type: float
self.upper_confidence_interval = self.data["upper"] # type: float
self.pvalue = self.data["pvalue"] # type: float
def get_raw(self):
"""
:return: the drift model accuracy data
:rtype: dict
"""
return self.data