Skip to content

Commit 27340f7

Browse files
committed
Merge branch 'task/fix-scan-metrics' of github.com:Giskard-AI/giskard into task/fix-scan-metrics
2 parents 09fa043 + ce913b2 commit 27340f7

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

python-client/giskard/core/model_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _do_validate_model(model: BaseModel, validate_ds: Optional[Dataset] = None):
7272
else: # Classification with target = None
7373
validate_model_execution(model, validate_ds)
7474

75-
if model.meta.model_type == SupportedModelTypes.CLASSIFICATION:
75+
if model.meta.model_type == SupportedModelTypes.CLASSIFICATION and validate_ds.target is not None:
7676
validate_order_classifcation_labels(model, validate_ds)
7777

7878

python-client/giskard/scanner/calibration/underconfidence_detector.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
@detector(name="underconfidence", tags=["underconfidence", "classification"])
1616
class UnderconfidenceDetector(LossBasedDetector):
17+
_needs_target = False
18+
1719
def __init__(self, threshold=0.1, p_threshold=0.95, method="tree"):
1820
self.threshold = threshold
1921
self.p_threshold = p_threshold

python-client/giskard/scanner/common/examples.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def get_examples_dataframe(self, n=3, with_prediction: Union[int, bool] = 1):
2222
examples = dataset.df.copy()
2323

2424
# Keep only interesting columns
25-
cols_to_show = issue.features + [issue.dataset.target]
25+
cols_to_show = issue.features
26+
if issue.dataset.target is not None:
27+
cols_to_show += [issue.dataset.target]
2628
examples = examples.loc[:, cols_to_show]
2729

2830
# If metadata slice, add the metadata column
@@ -54,7 +56,10 @@ def get_examples_dataframe(self, n=3, with_prediction: Union[int, bool] = 1):
5456
else:
5557
pred_examples = model_pred.prediction
5658

57-
examples[f"Predicted `{issue.dataset.target}`"] = pred_examples
59+
predicted_label = "Predicted"
60+
if issue.dataset.target is not None:
61+
predicted_label += f" `{issue.dataset.target}`"
62+
examples[predicted_label] = pred_examples
5863

5964
n = min(len(examples), n)
6065
if n > 0:

python-client/giskard/scanner/common/loss_based_detector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ class LossBasedDetector(Detector):
2020
MAX_DATASET_SIZE = 10_000_000
2121
LOSS_COLUMN_NAME = "__gsk__loss"
2222

23+
_needs_target = True
24+
2325
def run(self, model: BaseModel, dataset: Dataset):
26+
if self._needs_target and dataset.target is None:
27+
logger.info(f"{self.__class__.__name__}: Skipping detection because the dataset has no target column.")
28+
return []
29+
2430
logger.info(f"{self.__class__.__name__}: Running")
2531

2632
# Check if we have enough data to run the scan

python-client/giskard/scanner/correlation/spurious_correlation_detector.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ def run(self, model: BaseModel, dataset: Dataset):
3636

3737
# Prepare dataset for slicing
3838
df = dataset.df.copy()
39-
df[dataset.target] = pd.Categorical(df[dataset.target])
40-
wdata = Dataset(df, column_types=dataset.column_types)
39+
if dataset.target is not None:
40+
df.drop(columns=dataset.target, inplace=True)
41+
df["__gsk__target"] = pd.Categorical(ds_predictions)
42+
wdata = Dataset(df, target="__gsk__target", column_types=dataset.column_types)
4143
wdata.load_metadata_from_instance(dataset.column_meta)
4244

4345
# Find slices
44-
sliced_cols = SliceFinder("tree").run(wdata, features, target=dataset.target)
46+
sliced_cols = SliceFinder("tree").run(wdata, features, target=wdata.target)
4547

4648
measure_fn, measure_name = self._get_measure_fn()
4749
issues = []

python-client/tests/scan/test_scanner.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ def test_scanner_raises_exception_if_no_detectors_available(german_credit_data,
7878
scanner.analyze(german_credit_model, german_credit_data)
7979

8080

81+
def test_scanner_works_if_dataset_has_no_target(titanic_model, titanic_dataset):
82+
scanner = Scanner()
83+
no_target_dataset = Dataset(titanic_dataset.df, target=None)
84+
result = scanner.analyze(titanic_model, no_target_dataset, raise_exceptions=True)
85+
86+
assert isinstance(result, ScanResult)
87+
assert result.has_issues()
88+
assert result.to_html()
89+
90+
8191
def test_scan_raises_exception_if_no_dataset_provided(german_credit_model):
8292
scanner = Scanner()
8393
with pytest.raises(ValueError) as info:

0 commit comments

Comments
 (0)