Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
- (`core`) New way to add a variable to a dictionary using a complete specification.
- (`sklearn`) `Text` Khiops type support at the estimator level.

### Changed
- (`core`) Dictionary API and analysis and coclustering reports,
when a requested key is not found in getters, return a ``None`` value instead
of raising a `KeyError` exception.

### Fixed
- (General) Inconsistency between the `tools.download_datasets` function and the
current samples directory according to `core.api.get_samples_dir()`.
Expand Down
94 changes: 28 additions & 66 deletions khiops/core/analysis_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,10 @@ def get_variable_statistics(self, variable_name):
Returns
-------
`VariableStatistics`
The statistics of the specified variable.

Raises
------
`KeyError`
If no variable with the specified names exist.
The statistics of the specified variable. A ``None`` value is returned
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/A None value/None/

if the variable name is not found.
"""
return self._variables_statistics_by_name[variable_name]
return self._variables_statistics_by_name.get(variable_name)

def get_tree(self, tree_name):
"""Returns the tree with the specified name
Expand All @@ -595,14 +591,10 @@ def get_tree(self, tree_name):
Returns
-------
`Tree`
The tree which has the specified name.

Raises
------
`KeyError`
If no tree with the specified name exists.
The tree which has the specified name. A ``None`` value is returned
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/A None value/None/

if the tree name is not found.
"""
return self._trees_by_name[tree_name]
return self._trees_by_name.get(tree_name)

def to_dict(self):
"""Transforms this instance to a dict with the Khiops JSON file structure"""
Expand Down Expand Up @@ -1045,15 +1037,12 @@ def get_variable_pair_statistics(self, variable_name_1, variable_name_2):
-------
`VariablePairStatistics`
The statistics of the specified pair of variables.

Raises
------
`KeyError`
If no pair with the specified names exist.
A ``None`` value is returned if no pair with the
specified names exist.
"""
return self._variables_pairs_statistics_by_name[
return self._variables_pairs_statistics_by_name.get(
(variable_name_1, variable_name_2)
]
)

def to_dict(self):
"""Transforms this instance to a dict with the Khiops JSON file structure"""
Expand Down Expand Up @@ -1306,27 +1295,19 @@ def get_predictor(self, predictor_name):
Returns
-------
`TrainedPredictor`
The predictor object for the specified name.

Raises
------
`KeyError`
If there is no predictor with the specified name.
The predictor object for the specified name. A ``None`` value is
returned if the predictor name is not found.
"""
return self._trained_predictors_by_name[predictor_name]
return self._trained_predictors_by_name.get(predictor_name)

def get_snb_predictor(self):
"""Returns the Selective Naive Bayes predictor

Returns
-------
`TrainedPredictor`
The predictor object for "Selective Naive Bayes".

Raises
------
`KeyError`
If there is no predictor named "Selective Naive Bayes".
The predictor object for "Selective Naive Bayes". A ``None`` value is
returned if there is no predictor named "Selective Naive Bayes".
"""
return self.get_predictor("Selective Naive Bayes")

Expand Down Expand Up @@ -1588,14 +1569,10 @@ def get_predictor_performance(self, predictor_name):
Returns
-------
`PredictorPerformance`
The performance metrics for the specified predictor.

Raises
------
`KeyError`
If no predictor with the specified name exists.
The performance metrics for the specified predictor. A ``None`` value
is returned if the predictor name is not found.
"""
return self._predictors_performance_by_name[predictor_name]
return self._predictors_performance_by_name.get(predictor_name)

def get_snb_performance(self):
"""Returns the performance metrics for the Selective Naive Bayes predictor
Expand Down Expand Up @@ -1625,21 +1602,20 @@ def get_regressor_rec_curve(self, regressor_name):
Returns
-------
`PredictorCurve`
The REC curve for the specified regressor.
The REC curve for the specified regressor. A ``None`` value is
returned if the regressor name is not found.

Raises
------
`ValueError`
If no regressor curves available. (
`KeyError`
If no regressor with the specified name exists.
If no regressor curves available.
"""
if self.learning_task != "Regression analysis":
raise ValueError("REC curves are available only for regression")
for curve in self.regression_rec_curves:
if curve.name == regressor_name:
return curve
raise KeyError(regressor_name)
return None

def get_snb_rec_curve(self):
"""Returns the REC curve for the Selective Naive Bayes regressor
Expand Down Expand Up @@ -1675,12 +1651,8 @@ def get_classifier_lift_curve(self, classifier_name, target_value):
-------
`PredictorCurve`
The lift curve for the specified classifier and target value.

Raises
------
`KeyError`
If no classifier with the specified exists or no target value with the
specified name exists.
A ``None`` value is returned if no classifier with the specified
exists or no target value with the specified name exists.
"""
if self.learning_task != "Classification analysis":
raise ValueError("Lift curves are available only for classification")
Expand All @@ -1701,8 +1673,7 @@ def get_classifier_lift_curve(self, classifier_name, target_value):
for lift_curve in self.classification_lift_curves[i]:
if lift_curve.name == classifier_name:
return lift_curve
raise KeyError(classifier_name)
raise KeyError(target_value)
return None

def get_snb_lift_curve(self, target_value):
"""Returns lift curve for the Selective Naive Bayes clf. given a target value
Expand All @@ -1716,14 +1687,8 @@ def get_snb_lift_curve(self, target_value):
-------
`PredictorCurve`
The lift curve of the Selective Naive Bayes classifier for the specified
target value.

Raises
------
`ValueError`
If the Selective Naive Bayes classifier information is not available.
`KeyError`
If no target value with the specified name exists.
target value. A ``None`` value is returned if no Selective Naive Bayes
classifier information is available.
"""
if self.learning_task != "Classification analysis":
raise ValueError("Lift curves are available only for classification")
Expand All @@ -1732,10 +1697,7 @@ def get_snb_lift_curve(self, target_value):
for lift_curve in self.classification_lift_curves[i]:
if lift_curve.name == "Selective Naive Bayes":
return lift_curve
raise ValueError(
"Selective Naive Bayes classifier information not available"
)
raise KeyError(target_value)
return None

def to_dict(self):
"""Transforms this instance to a dict with the Khiops JSON file structure"""
Expand Down
30 changes: 9 additions & 21 deletions khiops/core/coclustering_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,10 @@ def get_dimension(self, dimension_name):
Returns
-------
`CoclusteringDimension`
The specified dimension.

Raises
------
`KeyError`
If no dimension with the specified names exist.
The specified dimension. A ``None`` value is returned
if the dimension name is not found.
"""
return self._dimensions_by_name[dimension_name]
return self._dimensions_by_name.get(dimension_name)

def to_dict(self):
"""Transforms this instance to a dict with the Khiops JSON file structure"""
Expand Down Expand Up @@ -1062,14 +1058,10 @@ def get_part(self, part_name):
Returns
-------
`CoclusteringDimensionPart`
The part with the specified name.

Raises
------
`KeyError`
If there is no part with the specified name.
The part with the specified name. A ``None`` value is returned
if the part name is not found.
"""
return self._parts_by_name[part_name]
return self._parts_by_name.get(part_name)

def get_cluster(self, cluster_name):
"""Returns the specified cluster
Expand All @@ -1082,14 +1074,10 @@ def get_cluster(self, cluster_name):
Returns
-------
`CoclusteringCluster`
The specified cluster.

Raises
------
`KeyError`
If there is no cluster with the specified name.
The specified cluster. A ``None`` value is returned
if the cluster name is not found.
"""
return self._clusters_by_name[cluster_name]
return self._clusters_by_name.get(cluster_name)

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