Skip to content

Commit f865fb3

Browse files
author
Thierry RAMORASOAVINA
committed
Change the behavior of all the getters in the public API when the corresponding key is not found
Instead of raising a KeyError, a ``None`` value is returned (like the dict behavior)
1 parent b919396 commit f865fb3

5 files changed

Lines changed: 125 additions & 181 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
- (`core`) New way to add a variable to a dictionary using a complete specification.
1616
- (`sklearn`) `Text` Khiops type support at the estimator level.
1717

18+
### Changed
19+
- (`core`) Dictionary API and analysis and coclustering reports,
20+
when a requested key is not found in getters, return a ``None`` value instead
21+
of raising a `KeyError` exception.
22+
1823
### Fixed
1924
- (General) Inconsistency between the `tools.download_datasets` function and the
2025
current samples directory according to `core.api.get_samples_dir()`.

khiops/core/analysis_results.py

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,9 @@ def get_variable_statistics(self, variable_name):
576576
-------
577577
`VariableStatistics`
578578
The statistics of the specified variable.
579-
580-
Raises
581-
------
582-
`KeyError`
583-
If no variable with the specified names exist.
579+
A ``None`` value is returned if the variable name is not found.
584580
"""
585-
return self._variables_statistics_by_name[variable_name]
581+
return self._variables_statistics_by_name.get(variable_name)
586582

587583
def get_tree(self, tree_name):
588584
"""Returns the tree with the specified name
@@ -596,13 +592,9 @@ def get_tree(self, tree_name):
596592
-------
597593
`Tree`
598594
The tree which has the specified name.
599-
600-
Raises
601-
------
602-
`KeyError`
603-
If no tree with the specified name exists.
595+
A ``None`` value is returned if the tree name is not found.
604596
"""
605-
return self._trees_by_name[tree_name]
597+
return self._trees_by_name.get(tree_name)
606598

607599
def to_dict(self):
608600
"""Transforms this instance to a dict with the Khiops JSON file structure"""
@@ -1045,15 +1037,12 @@ def get_variable_pair_statistics(self, variable_name_1, variable_name_2):
10451037
-------
10461038
`VariablePairStatistics`
10471039
The statistics of the specified pair of variables.
1048-
1049-
Raises
1050-
------
1051-
`KeyError`
1052-
If no pair with the specified names exist.
1040+
A ``None`` value is returned if no pair with the
1041+
specified names exist.
10531042
"""
1054-
return self._variables_pairs_statistics_by_name[
1043+
return self._variables_pairs_statistics_by_name.get(
10551044
(variable_name_1, variable_name_2)
1056-
]
1045+
)
10571046

10581047
def to_dict(self):
10591048
"""Transforms this instance to a dict with the Khiops JSON file structure"""
@@ -1307,13 +1296,9 @@ def get_predictor(self, predictor_name):
13071296
-------
13081297
`TrainedPredictor`
13091298
The predictor object for the specified name.
1310-
1311-
Raises
1312-
------
1313-
`KeyError`
1314-
If there is no predictor with the specified name.
1299+
A ``None`` value is returned if the predictor name is not found.
13151300
"""
1316-
return self._trained_predictors_by_name[predictor_name]
1301+
return self._trained_predictors_by_name.get(predictor_name)
13171302

13181303
def get_snb_predictor(self):
13191304
"""Returns the Selective Naive Bayes predictor
@@ -1322,11 +1307,8 @@ def get_snb_predictor(self):
13221307
-------
13231308
`TrainedPredictor`
13241309
The predictor object for "Selective Naive Bayes".
1325-
1326-
Raises
1327-
------
1328-
`KeyError`
1329-
If there is no predictor named "Selective Naive Bayes".
1310+
A ``None`` value is returned
1311+
if there is no predictor named "Selective Naive Bayes".
13301312
"""
13311313
return self.get_predictor("Selective Naive Bayes")
13321314

@@ -1589,13 +1571,9 @@ def get_predictor_performance(self, predictor_name):
15891571
-------
15901572
`PredictorPerformance`
15911573
The performance metrics for the specified predictor.
1592-
1593-
Raises
1594-
------
1595-
`KeyError`
1596-
If no predictor with the specified name exists.
1574+
A ``None`` value is returned if the predictor name is not found.
15971575
"""
1598-
return self._predictors_performance_by_name[predictor_name]
1576+
return self._predictors_performance_by_name.get(predictor_name)
15991577

16001578
def get_snb_performance(self):
16011579
"""Returns the performance metrics for the Selective Naive Bayes predictor
@@ -1625,21 +1603,20 @@ def get_regressor_rec_curve(self, regressor_name):
16251603
Returns
16261604
-------
16271605
`PredictorCurve`
1628-
The REC curve for the specified regressor.
1606+
The REC curve for the specified regressor. A ``None`` value is
1607+
returned if the regressor name is not found.
16291608
16301609
Raises
16311610
------
16321611
`ValueError`
1633-
If no regressor curves available. (
1634-
`KeyError`
1635-
If no regressor with the specified name exists.
1612+
If no regressor curves available.
16361613
"""
16371614
if self.learning_task != "Regression analysis":
16381615
raise ValueError("REC curves are available only for regression")
16391616
for curve in self.regression_rec_curves:
16401617
if curve.name == regressor_name:
16411618
return curve
1642-
raise KeyError(regressor_name)
1619+
return None
16431620

16441621
def get_snb_rec_curve(self):
16451622
"""Returns the REC curve for the Selective Naive Bayes regressor
@@ -1675,11 +1652,8 @@ def get_classifier_lift_curve(self, classifier_name, target_value):
16751652
-------
16761653
`PredictorCurve`
16771654
The lift curve for the specified classifier and target value.
1678-
1679-
Raises
1680-
------
1681-
`KeyError`
1682-
If no classifier with the specified exists or no target value with the
1655+
A ``None`` value is returned
1656+
if no classifier with the specified exists or no target value with the
16831657
specified name exists.
16841658
"""
16851659
if self.learning_task != "Classification analysis":
@@ -1701,8 +1675,7 @@ def get_classifier_lift_curve(self, classifier_name, target_value):
17011675
for lift_curve in self.classification_lift_curves[i]:
17021676
if lift_curve.name == classifier_name:
17031677
return lift_curve
1704-
raise KeyError(classifier_name)
1705-
raise KeyError(target_value)
1678+
return None
17061679

17071680
def get_snb_lift_curve(self, target_value):
17081681
"""Returns lift curve for the Selective Naive Bayes clf. given a target value
@@ -1716,14 +1689,8 @@ def get_snb_lift_curve(self, target_value):
17161689
-------
17171690
`PredictorCurve`
17181691
The lift curve of the Selective Naive Bayes classifier for the specified
1719-
target value.
1720-
1721-
Raises
1722-
------
1723-
`ValueError`
1724-
If the Selective Naive Bayes classifier information is not available.
1725-
`KeyError`
1726-
If no target value with the specified name exists.
1692+
target value. A ``None`` value is returned if no Selective Naive Bayes
1693+
classifier information is available.
17271694
"""
17281695
if self.learning_task != "Classification analysis":
17291696
raise ValueError("Lift curves are available only for classification")
@@ -1732,10 +1699,7 @@ def get_snb_lift_curve(self, target_value):
17321699
for lift_curve in self.classification_lift_curves[i]:
17331700
if lift_curve.name == "Selective Naive Bayes":
17341701
return lift_curve
1735-
raise ValueError(
1736-
"Selective Naive Bayes classifier information not available"
1737-
)
1738-
raise KeyError(target_value)
1702+
return None
17391703

17401704
def to_dict(self):
17411705
"""Transforms this instance to a dict with the Khiops JSON file structure"""

khiops/core/coclustering_results.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,10 @@ def get_dimension(self, dimension_name):
479479
Returns
480480
-------
481481
`CoclusteringDimension`
482-
The specified dimension.
483-
484-
Raises
485-
------
486-
`KeyError`
487-
If no dimension with the specified names exist.
482+
The specified dimension. A ``None`` value is returned
483+
if the dimension name is not found.
488484
"""
489-
return self._dimensions_by_name[dimension_name]
485+
return self._dimensions_by_name.get(dimension_name)
490486

491487
def to_dict(self):
492488
"""Transforms this instance to a dict with the Khiops JSON file structure"""
@@ -1062,14 +1058,10 @@ def get_part(self, part_name):
10621058
Returns
10631059
-------
10641060
`CoclusteringDimensionPart`
1065-
The part with the specified name.
1066-
1067-
Raises
1068-
------
1069-
`KeyError`
1070-
If there is no part with the specified name.
1061+
The part with the specified name. A ``None`` value is returned
1062+
if the part name is not found.
10711063
"""
1072-
return self._parts_by_name[part_name]
1064+
return self._parts_by_name.get(part_name)
10731065

10741066
def get_cluster(self, cluster_name):
10751067
"""Returns the specified cluster
@@ -1082,14 +1074,10 @@ def get_cluster(self, cluster_name):
10821074
Returns
10831075
-------
10841076
`CoclusteringCluster`
1085-
The specified cluster.
1086-
1087-
Raises
1088-
------
1089-
`KeyError`
1090-
If there is no cluster with the specified name.
1077+
The specified cluster. A ``None`` value is returned
1078+
if the cluster name is not found.
10911079
"""
1092-
return self._clusters_by_name[cluster_name]
1080+
return self._clusters_by_name.get(cluster_name)
10931081

10941082
def to_dict(self, report_type):
10951083
"""Transforms this instance to a dict with the Khiops JSON file structure

0 commit comments

Comments
 (0)