Skip to content

Commit c9e8224

Browse files
committed
utils: replace DSSExtensibleDict with DSSInternalDict
1 parent 87adece commit c9e8224

File tree

2 files changed

+24
-78
lines changed

2 files changed

+24
-78
lines changed

dataikuapi/dss/ml.py

Lines changed: 17 additions & 17 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
@@ -738,7 +738,7 @@ def get_performance_metrics(self):
738738
"""
739739
Gets the performance results of the global population used for the subpopulation analysis
740740
"""
741-
return self.get("performanceMetrics")
741+
return self._internal_dict.get("performanceMetrics")
742742

743743
def get_prediction_info(self):
744744
"""
@@ -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
@@ -781,7 +781,7 @@ def get_raw(self):
781781
"""
782782
Gets the raw dictionary of the subpopulation analysis modality
783783
"""
784-
return self.internal_dict
784+
return self._internal_dict
785785

786786
def get_definition(self):
787787
"""
@@ -890,7 +890,7 @@ def __repr__(self):
890890
return "DSSSubpopulationCategoryModalityDefinition(%s='%s')" % (self.feature_name, self.value)
891891

892892

893-
class DSSSubpopulationAnalysis(DSSExtensibleDict):
893+
class DSSSubpopulationAnalysis(DSSInternalDict):
894894
"""
895895
Object to read details of a subpopulation analysis of a trained model
896896
@@ -954,10 +954,10 @@ def get_raw(self):
954954
"""
955955
Gets the raw dictionary of the subpopulation analysis
956956
"""
957-
return self.internal_dict
957+
return self._internal_dict
958958

959959

960-
class DSSSubpopulationAnalyses(DSSExtensibleDict):
960+
class DSSSubpopulationAnalyses(DSSInternalDict):
961961
"""
962962
Object to read details of subpopulation analyses of a trained model
963963
@@ -975,7 +975,7 @@ def get_raw(self):
975975
"""
976976
Gets the raw dictionary of subpopulation analyses
977977
"""
978-
return self.internal_dict
978+
return self._internal_dict
979979

980980
def get_global(self):
981981
"""
@@ -987,19 +987,19 @@ def list_analyses(self):
987987
"""
988988
Lists all features on which subpopulation analyses have been computed
989989
"""
990-
return [analysis["feature"] for analysis in self.analyses]
990+
return [analysis.get("feature") for analysis in self.analyses]
991991

992992
def get_analysis(self, feature):
993993
"""
994994
Retrieves the subpopulation analysis for a particular feature
995995
"""
996996
try:
997-
return next(analysis for analysis in self.analyses if analysis["feature"] == feature)
997+
return next(analysis for analysis in self.analyses if analysis.get("feature") == feature)
998998
except StopIteration:
999999
raise ValueError("Subpopulation analysis for feature '%s' cannot be found" % feature)
10001000

10011001

1002-
class DSSPartialDependence(DSSExtensibleDict):
1002+
class DSSPartialDependence(DSSInternalDict):
10031003
"""
10041004
Object to read details of partial dependence of a trained model
10051005
@@ -1023,10 +1023,10 @@ def get_raw(self):
10231023
"""
10241024
Gets the raw dictionary of the partial dependence
10251025
"""
1026-
return self.internal_dict
1026+
return self._internal_dict
10271027

10281028

1029-
class DSSPartialDependencies(DSSExtensibleDict):
1029+
class DSSPartialDependencies(DSSInternalDict):
10301030
"""
10311031
Object to read details of partial dependencies of a trained model
10321032
@@ -1043,20 +1043,20 @@ def get_raw(self):
10431043
"""
10441044
Gets the raw dictionary of partial dependencies
10451045
"""
1046-
return self.internal_dict
1046+
return self._internal_dict
10471047

10481048
def list_features(self):
10491049
"""
10501050
Lists all features on which partial dependencies have been computed
10511051
"""
1052-
return [partial_dep["feature"] for partial_dep in self.partial_dependencies]
1052+
return [partial_dep.get("feature") for partial_dep in self.partial_dependencies]
10531053

10541054
def get_partial_dependence(self, feature):
10551055
"""
10561056
Retrieves the partial dependencies for a particular feature
10571057
"""
10581058
try:
1059-
return next(pd for pd in self.partial_dependencies if pd["feature"] == feature)
1059+
return next(pd for pd in self.partial_dependencies if pd.get("feature") == feature)
10601060
except StopIteration:
10611061
raise ValueError("Partial dependence for feature '%s' cannot be found" % feature)
10621062

dataikuapi/utils.py

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -95,73 +95,19 @@ 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
114-
115-
def __getitem__(self, key):
116-
return self.internal_dict[key]
117-
118-
def __iter__(self):
119-
return self.internal_dict.__iter__()
107+
self._internal_dict = orig_dict
120108

121-
def __setitem__(self, key, value):
122-
self.internal_dict[key] = value
109+
def get(self, name, default=None):
110+
return self._internal_dict.get(name, default=default)
123111

124112
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()
113+
return self.__class__.__name__ + "(" + self._internal_dict.__repr__() + ")"

0 commit comments

Comments
 (0)