|
5 | 5 | import pickle
|
6 | 6 | from abc import ABC, abstractmethod
|
7 | 7 | from collections.abc import Iterable
|
| 8 | +from typing import Any, List |
8 | 9 |
|
9 | 10 | import numpy as np
|
10 | 11 | import pandas as pd
|
@@ -47,13 +48,37 @@ def __init__(self, data_interface, model_interface=None):
|
47 | 48 | # self.cont_precisions = \
|
48 | 49 | # [self.data_interface.get_decimal_precisions()[ix] for ix in self.encoded_continuous_feature_indexes]
|
49 | 50 |
|
| 51 | + def _find_features_having_missing_values( |
| 52 | + self, data: Any) -> List[str]: |
| 53 | + """Return list of features which have missing values. |
| 54 | +
|
| 55 | + :param data: The dataset to check. |
| 56 | + :type data: Any |
| 57 | + :return: List of feature names which have missing values. |
| 58 | + :rtype: List[str] |
| 59 | + """ |
| 60 | + if not isinstance(data, pd.DataFrame): |
| 61 | + return [] |
| 62 | + |
| 63 | + list_of_feature_having_missing_values = [] |
| 64 | + for feature in data.columns.tolist(): |
| 65 | + if np.any(data[feature].isnull()): |
| 66 | + list_of_feature_having_missing_values.append(feature) |
| 67 | + return list_of_feature_having_missing_values |
| 68 | + |
50 | 69 | def _validate_counterfactual_configuration(
|
51 | 70 | self, query_instances, total_CFs,
|
52 | 71 | desired_class="opposite", desired_range=None,
|
53 | 72 | permitted_range=None, features_to_vary="all",
|
54 | 73 | stopping_threshold=0.5, posthoc_sparsity_param=0.1,
|
55 | 74 | posthoc_sparsity_algorithm="linear", verbose=False, **kwargs):
|
56 | 75 |
|
| 76 | + if len(self._find_features_having_missing_values(query_instances)) > 0: |
| 77 | + raise UserConfigValidationException( |
| 78 | + "The query instance(s) should not have any missing values. " |
| 79 | + "Please impute the missing values and try again." |
| 80 | + ) |
| 81 | + |
57 | 82 | if total_CFs <= 0:
|
58 | 83 | raise UserConfigValidationException(
|
59 | 84 | "The number of counterfactuals generated per query instance (total_CFs) should be a positive integer.")
|
|
0 commit comments