Skip to content

Commit 9215d49

Browse files
authored
Merge PR #55 DSSExtensibleDict → DSSInternalDict
from fix/dss70-internal-dict
2 parents 87adece + a0b8f19 commit 9215d49

File tree

2 files changed

+25
-101
lines changed

2 files changed

+25
-101
lines changed

dataikuapi/dss/ml.py

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from ..utils import DataikuException
22
from ..utils import DataikuUTF8CSVReader
33
from ..utils import DataikuStreamedHttpUTF8CSVReader
4-
from ..utils import DSSExtensibleDict
4+
from ..utils import DSSInternalDict
55
import json
66
import time
77
from .metrics import ComputedMetrics
@@ -723,7 +723,7 @@ def get_partial_dependencies(self):
723723
return DSSPartialDependencies(data)
724724

725725

726-
class DSSSubpopulationGlobal(DSSExtensibleDict):
726+
class DSSSubpopulationGlobal(DSSInternalDict):
727727
"""
728728
Object to read details of performance on global population used for subpopulation analyses.
729729
@@ -761,7 +761,7 @@ def get_prediction_info(self):
761761
}
762762

763763

764-
class DSSSubpopulationModality(DSSExtensibleDict):
764+
class DSSSubpopulationModality(DSSInternalDict):
765765
"""
766766
Object to read details of a subpopulation analysis modality
767767
@@ -777,12 +777,6 @@ def __init__(self, feature_name, computed_as_type, data, prediction_type):
777777
elif computed_as_type == "NUMERIC":
778778
self.definition = DSSSubpopulationNumericModalityDefinition(feature_name, data)
779779

780-
def get_raw(self):
781-
"""
782-
Gets the raw dictionary of the subpopulation analysis modality
783-
"""
784-
return self.internal_dict
785-
786780
def get_definition(self):
787781
"""
788782
Gets the definition of the subpopulation analysis modality
@@ -890,7 +884,7 @@ def __repr__(self):
890884
return "DSSSubpopulationCategoryModalityDefinition(%s='%s')" % (self.feature_name, self.value)
891885

892886

893-
class DSSSubpopulationAnalysis(DSSExtensibleDict):
887+
class DSSSubpopulationAnalysis(DSSInternalDict):
894888
"""
895889
Object to read details of a subpopulation analysis of a trained model
896890
@@ -942,22 +936,16 @@ def get_modality_data(self, definition=None):
942936
if isinstance(definition, DSSSubpopulationModalityDefinition):
943937
modality_candidates = [m for m in self.modalities if m.definition.index == definition.index]
944938
if len(modality_candidates) == 0:
945-
raise ValueError("Modality with index '%s' not found" % modality["index"])
939+
raise ValueError("Modality with index '%s' not found" % definition.index)
946940
return modality_candidates[0]
947941

948942
for m in self.modalities:
949943
if m.definition.contains(definition):
950944
return m
951945
raise ValueError("Modality not found: %s" % definition)
952946

953-
def get_raw(self):
954-
"""
955-
Gets the raw dictionary of the subpopulation analysis
956-
"""
957-
return self.internal_dict
958-
959947

960-
class DSSSubpopulationAnalyses(DSSExtensibleDict):
948+
class DSSSubpopulationAnalyses(DSSInternalDict):
961949
"""
962950
Object to read details of subpopulation analyses of a trained model
963951
@@ -971,12 +959,6 @@ def __init__(self, data, prediction_type):
971959
for analysis in data.get("subpopulationAnalyses", []):
972960
self.analyses.append(DSSSubpopulationAnalysis(analysis, prediction_type))
973961

974-
def get_raw(self):
975-
"""
976-
Gets the raw dictionary of subpopulation analyses
977-
"""
978-
return self.internal_dict
979-
980962
def get_global(self):
981963
"""
982964
Retrieves information and performance on the full dataset used to compute the subpopulation analyses
@@ -987,19 +969,19 @@ def list_analyses(self):
987969
"""
988970
Lists all features on which subpopulation analyses have been computed
989971
"""
990-
return [analysis["feature"] for analysis in self.analyses]
972+
return [analysis.get("feature") for analysis in self.analyses]
991973

992974
def get_analysis(self, feature):
993975
"""
994976
Retrieves the subpopulation analysis for a particular feature
995977
"""
996978
try:
997-
return next(analysis for analysis in self.analyses if analysis["feature"] == feature)
979+
return next(analysis for analysis in self.analyses if analysis.get("feature") == feature)
998980
except StopIteration:
999981
raise ValueError("Subpopulation analysis for feature '%s' cannot be found" % feature)
1000982

1001983

1002-
class DSSPartialDependence(DSSExtensibleDict):
984+
class DSSPartialDependence(DSSInternalDict):
1003985
"""
1004986
Object to read details of partial dependence of a trained model
1005987
@@ -1019,14 +1001,8 @@ def get_computation_params(self):
10191001
"onSample": self.get("onSample")
10201002
}
10211003

1022-
def get_raw(self):
1023-
"""
1024-
Gets the raw dictionary of the partial dependence
1025-
"""
1026-
return self.internal_dict
1027-
10281004

1029-
class DSSPartialDependencies(DSSExtensibleDict):
1005+
class DSSPartialDependencies(DSSInternalDict):
10301006
"""
10311007
Object to read details of partial dependencies of a trained model
10321008
@@ -1039,24 +1015,18 @@ def __init__(self, data):
10391015
for pd in data.get("partialDependencies", []):
10401016
self.partial_dependencies.append(DSSPartialDependence(pd))
10411017

1042-
def get_raw(self):
1043-
"""
1044-
Gets the raw dictionary of partial dependencies
1045-
"""
1046-
return self.internal_dict
1047-
10481018
def list_features(self):
10491019
"""
10501020
Lists all features on which partial dependencies have been computed
10511021
"""
1052-
return [partial_dep["feature"] for partial_dep in self.partial_dependencies]
1022+
return [partial_dep.get("feature") for partial_dep in self.partial_dependencies]
10531023

10541024
def get_partial_dependence(self, feature):
10551025
"""
10561026
Retrieves the partial dependencies for a particular feature
10571027
"""
10581028
try:
1059-
return next(pd for pd in self.partial_dependencies if pd["feature"] == feature)
1029+
return next(pd for pd in self.partial_dependencies if pd.get("feature") == feature)
10601030
except StopIteration:
10611031
raise ValueError("Partial dependence for feature '%s' cannot be found" % feature)
10621032

dataikuapi/utils.py

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -95,73 +95,27 @@ def str_to_bool(s):
9595
yield [none_if_throws(caster)(val)
9696
for (caster, val) in dku_zip_longest(casters, uncasted_tuple)]
9797

98-
class DSSExtensibleDict(dict):
98+
class DSSInternalDict(object):
9999
"""
100-
Utility to define dict-like objects that can be sub-classed.
101-
102-
Behaves like dict for most common operations. In particular,
103-
it is possible to update an instance of `:class:`dataikuapi.dss.ml.DSSExtensibleDict`
104-
with either a dict or another instance of `:class:`dataikuapi.dss.ml.DSSExtensibleDict`.
105-
106-
Provides an `internal_dict` dict field that is the actual holder of the data.
100+
Class that provides some helpers and an `_internal_dict` dict field that is the actual holder of the data.
107101
"""
108102

109103
def __init__(self, orig_dict=None):
110104
if orig_dict is None:
111-
self.internal_dict = dict()
105+
self._internal_dict = dict()
112106
else:
113-
self.internal_dict = orig_dict
107+
self._internal_dict = orig_dict
114108

115-
def __getitem__(self, key):
116-
return self.internal_dict[key]
109+
def get(self, name, default=None):
110+
return self._internal_dict.get(name, default)
117111

118-
def __iter__(self):
119-
return self.internal_dict.__iter__()
112+
def get_raw(self):
113+
"""
114+
Gets the raw dictionary of the actual data
120115
121-
def __setitem__(self, key, value):
122-
self.internal_dict[key] = value
116+
:rtype: dict
117+
"""
118+
return self._internal_dict
123119

124120
def __repr__(self):
125-
return self.__class__.__name__ + "(" + self.internal_dict.__repr__() + ")"
126-
127-
def __len__(self):
128-
return self.internal_dict.__len__()
129-
130-
def clear(self):
131-
self.internal_dict.clear()
132-
133-
def __contains__(self, key):
134-
return self.internal_dict.__contains__(key)
135-
136-
def copy(self):
137-
return self.internal_dict.copy()
138-
139-
def fromkeys(self, sequence, value=None):
140-
return self.internal_dict.fromkeys(sequence, value)
141-
142-
def get(self, key, value=None):
143-
return self.internal_dict.get(key, value)
144-
145-
def items(self):
146-
return self.internal_dict.items()
147-
148-
def keys(self):
149-
return self.internal_dict.keys()
150-
151-
def popitem(self):
152-
return self.internal_dict.popitem()
153-
154-
def pop(self, key, *argv):
155-
return self.internal_dict.pop(key, *argv)
156-
157-
def setdefault(self, key, default_value=None):
158-
return self.internal_dict.setdefault(key, default_value)
159-
160-
def update(self, *args, **kwargs):
161-
if len(args) == 1 and isinstance(args[0], DSSExtensibleDict):
162-
self.internal_dict.update(args[0].internal_dict, **kwargs)
163-
else:
164-
self.internal_dict.update(*args, **kwargs)
165-
166-
def values(self):
167-
return self.internal_dict.values()
121+
return self.__class__.__name__ + "(" + self._internal_dict.__repr__() + ")"

0 commit comments

Comments
 (0)