Medical transcript classification [scikit-learn]ΒΆ
Giskard is an open-source framework for testing all ML models, from LLMs to tabular models. Donβt hesitate to give the project a star on GitHub βοΈ if you find it useful!
In this notebook, youβll learn how to create comprehensive test suites for your model in a few lines of code, thanks to Giskardβs open-source Python library.
Use-case:
Multinomial classification of a medical transcript.
Outline:
Detect vulnerabilities automatically with Giskardβs scan
Automatically generate & curate a comprehensive test suite to test your model beyond accuracy-related metrics
Install dependenciesΒΆ
Make sure to install the giskard
[ ]:
%pip install giskard --upgrade
We also install the project-specific dependencies for this tutorial.
[ ]:
%pip install nltk
Import librariesΒΆ
[1]:
import string
from pathlib import Path
from urllib.request import urlretrieve
import nltk
import pandas as pd
from nltk.corpus import stopwords
from nltk.stem.snowball import SnowballStemmer
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
from typing import Iterable
from giskard import Dataset, Model, scan, testing
Define constantsΒΆ
[2]:
# Constants.
LABELS_LIST = [
'Neurosurgery',
'ENT - Otolaryngology',
'Discharge Summary',
'General Medicine',
'Gastroenterology',
'Neurology',
'SOAP / Chart / Progress Notes',
'Obstetrics / Gynecology',
'Urology'
]
TEXT_COLUMN_NAME = "transcription"
TARGET_COLUMN_NAME = "medical_specialty"
RANDOM_SEED = 8888
# Data.
DATA_URL = "ftp://sys.giskard.ai/pub/unit_test_resources/medical_transcript_classification_dataset/mtsamples.csv"
DATA_PATH = Path.home() / ".giskard" / "medical_transcript_classification_dataset" / "mtsamples.csv"
Dataset preparationΒΆ
Download NLTK stopwords corpusΒΆ
[ ]:
# Download list of english stopwords.
nltk.download('stopwords')
Load dataΒΆ
[4]:
def fetch_from_ftp(url: str, file: Path) -> None:
"""Helper to fetch data from the FTP server."""
if not file.parent.exists():
file.parent.mkdir(parents=True, exist_ok=True)
if not file.exists():
print(f"Downloading data from {url}")
urlretrieve(url, file)
print(f"Data was loaded!")
def load_data() -> pd.DataFrame:
"""Load and initially preprocess data."""
fetch_from_ftp(DATA_URL, DATA_PATH)
df = pd.read_csv(DATA_PATH)
# Drop useless columns.
df = df.drop(columns=['Unnamed: 0', "description", "sample_name", "keywords"])
# Trim text.
df = df.apply(lambda x: x.str.strip())
# Filter samples by label.
df = df[df[TARGET_COLUMN_NAME].isin(LABELS_LIST)]
# Drop rows with no transcript.
df = df[df[TEXT_COLUMN_NAME].notna()]
return df
[ ]:
transcript_df = load_data()
Train-test splitΒΆ
[6]:
X_train, X_test, y_train, y_test = train_test_split(transcript_df[[TEXT_COLUMN_NAME]],
transcript_df[TARGET_COLUMN_NAME],
random_state=RANDOM_SEED)
Wrap dataset with GiskardΒΆ
To prepare for the vulnerability scan, make sure to wrap your dataset using Giskardβs Dataset class. More details here.
[ ]:
raw_data = pd.concat([X_test, y_test], axis=1)
giskard_dataset = Dataset(
df=raw_data,
# A pandas.DataFrame that contains the raw data (before all the pre-processing steps) and the actual ground truth variable (target).
name="medical_transcript_dataset", # Ground truth variable.
target=TARGET_COLUMN_NAME # Optional.
)
Model buildingΒΆ
Define preprocessing stepsΒΆ
[8]:
stemmer = SnowballStemmer("english")
stop_words = stopwords.words("english")
def preprocess_text(df: pd.DataFrame) -> pd.DataFrame:
"""Preprocess text."""
# Lower.
df[TEXT_COLUMN_NAME] = df[TEXT_COLUMN_NAME].apply(lambda x: x.lower())
# Remove punctuation.
df[TEXT_COLUMN_NAME] = df[TEXT_COLUMN_NAME].apply(lambda x: x.translate(str.maketrans('', '', string.punctuation)))
# Tokenize.
df[TEXT_COLUMN_NAME] = df[TEXT_COLUMN_NAME].apply(lambda x: x.split())
# Stem.
df[TEXT_COLUMN_NAME] = df[TEXT_COLUMN_NAME].apply(lambda x: [stemmer.stem(word) for word in x])
# Remove stop-words.
df[TEXT_COLUMN_NAME] = df[TEXT_COLUMN_NAME].apply(
lambda x: ' '.join([word for word in x if word not in stop_words]))
return df
def adapt_vectorizer_input(df: pd.DataFrame) -> Iterable:
"""Adapt input for the vectorizers.
The problem is that vectorizers accept iterable, not DataFrame, but Series. Thus, we need to ravel dataframe with text have input single dimension.
Issue reference: https://stackoverflow.com/questions/50665240/valueerror-found-input-variables-with-inconsistent-numbers-of-samples-1-3185"""
df = df.iloc[:, 0]
return df
text_preprocessor = FunctionTransformer(preprocess_text)
vectorizer_input_adapter = FunctionTransformer(adapt_vectorizer_input)
Build estimatorΒΆ
[ ]:
pipeline = Pipeline(steps=[
("text_preprocessor", text_preprocessor),
("vectorizer_input_adapter", vectorizer_input_adapter),
("vectorizer", CountVectorizer(ngram_range=(1, 1))),
("estimator", RandomForestClassifier(random_state=RANDOM_SEED))
])
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
print(classification_report(y_test, y_pred))
Wrap model with GiskardΒΆ
To prepare for the vulnerability scan, make sure to wrap your model using Giskardβs Model class. You can choose to either wrap the prediction function (preferred option) or the model object. More details here.
[ ]:
# Wrap the prediction function
def prediction_function(df):
return pipeline.predict_proba(df)
giskard_model = Model(
model=prediction_function,
# A prediction function that encapsulates all the data pre-processing steps and that could be executed with the dataset used by the scan.
model_type="classification", # Either regression, classification or text_generation.
name="medical_transcript_classification", # Optional.
classification_labels=pipeline.classes_, # Their order MUST be identical to the prediction_function's output order.
feature_names=[TEXT_COLUMN_NAME] # Default: all columns of your dataset.
)
# Validate wrapped model.
print(classification_report(y_test, pipeline.classes_[giskard_model.predict(giskard_dataset).raw_prediction]))
Detect vulnerabilities in your modelΒΆ
Scan your model for vulnerabilities with GiskardΒΆ
Giskardβs scan allows you to detect vulnerabilities in your model automatically. These include performance biases, unrobustness, data leakage, stochasticity, underconfidence, ethical issues, and more. For detailed information about the scan feature, please refer to our scan documentation.
[ ]:
results = scan(giskard_model, giskard_dataset)
[12]:
display(results)
Generate comprehensive test suites automatically for your modelΒΆ
Generate test suites from the scanΒΆ
The objects produced by the scan can be used as fixtures to generate a test suite that integrate all detected vulnerabilities. Test suites allow you to evaluate and validate your modelβs performance, ensuring that it behaves as expected on a set of predefined test cases, and to identify any regressions or issues that might arise during development or updates.
[14]:
test_suite = results.generate_test_suite("My first test suite")
test_suite.run()
2024-05-29 13:49:53,802 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:53,804 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (371, 2) executed in 0:00:00.009591
2024-05-29 13:49:54,057 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,048 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (371, 2) executed in 0:00:00.998800
2024-05-29 13:49:55,051 pid:66538 MainThread giskard.utils.logging_utils INFO Perturb and predict data executed in 0:00:01.259399
2024-05-29 13:49:55,051 pid:66538 MainThread giskard.utils.logging_utils INFO Compare and predict the data executed in 0:00:00.000281
Executed 'Invariance to βAdd typosβ' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'transformation_function': <giskard.scanner.robustness.text_transformations.TextTypoTransformation object at 0x16ac02200>, 'threshold': 0.95, 'output_sensitivity': 0.05}:
Test failed
Metric: 0.9
- [INFO] 371 rows were perturbed
2024-05-29 13:49:55,070 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,071 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (60, 2) executed in 0:00:00.005307
Executed 'Overconfidence on data slice β`transcription` contains "temperature"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176666d70>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.73
2024-05-29 13:49:55,090 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,091 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (97, 2) executed in 0:00:00.006231
Executed 'Overconfidence on data slice β`transcription` contains "dr"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176664670>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.73
2024-05-29 13:49:55,111 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,112 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (76, 2) executed in 0:00:00.007671
Executed 'Overconfidence on data slice β`transcription` contains "weight"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176ef35e0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.72
2024-05-29 13:49:55,132 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,132 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (65, 2) executed in 0:00:00.006201
Executed 'Overconfidence on data slice β`transcription` contains "having"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176ef3ee0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.72
2024-05-29 13:49:55,151 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,152 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (71, 2) executed in 0:00:00.005367
Executed 'Overconfidence on data slice β`transcription` contains "today"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176fb60e0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.72
2024-05-29 13:49:55,172 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,173 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (80, 2) executed in 0:00:00.005348
Executed 'Overconfidence on data slice β`transcription` contains "follow"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1765f3a30>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.7
2024-05-29 13:49:55,191 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,192 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (191, 2) executed in 0:00:00.008549
Executed 'Overconfidence on data slice β`transcription` contains "blood"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176691270>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.7
2024-05-29 13:49:55,213 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,214 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (70, 2) executed in 0:00:00.007286
Executed 'Overconfidence on data slice β`transcription` contains "distress"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17678f070>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.69
2024-05-29 13:49:55,236 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,237 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (126, 2) executed in 0:00:00.007065
Executed 'Overconfidence on data slice β`transcription` contains "stable"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1765ffee0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.68
2024-05-29 13:49:55,257 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,258 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (93, 2) executed in 0:00:00.006464
Executed 'Overconfidence on data slice β`transcription` contains "mg"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f7fbe0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.68
2024-05-29 13:49:55,270 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,271 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (214, 2) executed in 0:00:00.009823
Executed 'Overconfidence on data slice β`text_length(transcription)` >= 2145.000β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.text_slicer.MetadataSliceFunction object at 0x1767b1330>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.68
2024-05-29 13:49:55,291 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,292 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (85, 2) executed in 0:00:00.006814
Executed 'Overconfidence on data slice β`transcription` contains "discharge"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176718eb0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.67
2024-05-29 13:49:55,312 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,313 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (101, 2) executed in 0:00:00.007278
Executed 'Overconfidence on data slice β`transcription` contains "hospital"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176690c70>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.67
2024-05-29 13:49:55,334 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,335 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (52, 2) executed in 0:00:00.006081
Executed 'Overconfidence on data slice β`transcription` contains "continue"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1766a7bb0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.67
2024-05-29 13:49:55,354 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,355 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (76, 2) executed in 0:00:00.005812
Executed 'Overconfidence on data slice β`transcription` contains "vital"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1760639a0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}:
Test failed
Metric: 0.67
2024-05-29 13:49:55,374 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,375 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (22, 2) executed in 0:00:00.005195
Executed 'Precision on data slice β`transcription` contains "xyz"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17703d9c0>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.32
2024-05-29 13:49:55,397 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,398 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (30, 2) executed in 0:00:00.004141
Executed 'Precision on data slice β`transcription` contains "subjective"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17678d5a0>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.37
2024-05-29 13:49:55,417 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,418 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (64, 2) executed in 0:00:00.005701
Executed 'Precision on data slice β`transcription` contains "admission"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f8c670>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.38
2024-05-29 13:49:55,438 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,439 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (65, 2) executed in 0:00:00.005455
Executed 'Precision on data slice β`transcription` contains "daily"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17673be80>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.38
2024-05-29 13:49:55,460 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,462 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (23, 2) executed in 0:00:00.006355
Executed 'Precision on data slice β`transcription` contains "coronary"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f9ce80>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.39
2024-05-29 13:49:55,482 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,484 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (23, 2) executed in 0:00:00.005071
Executed 'Precision on data slice β`transcription` contains "aspirin"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f9d600>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.39
2024-05-29 13:49:55,505 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,506 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (48, 2) executed in 0:00:00.005088
Executed 'Precision on data slice β`transcription` contains "followup"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176666a10>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.4
2024-05-29 13:49:55,526 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,528 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (20, 2) executed in 0:00:00.005684
Executed 'Precision on data slice β`transcription` contains "lung"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1760120e0>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.4
2024-05-29 13:49:55,549 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,550 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (50, 2) executed in 0:00:00.006254
Executed 'Precision on data slice β`transcription` contains "count"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176646980>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.4
2024-05-29 13:49:55,581 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,593 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (29, 2) executed in 0:00:00.016641
Executed 'Precision on data slice β`transcription` contains "function"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f5c400>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.41
2024-05-29 13:49:55,648 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,649 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (24, 2) executed in 0:00:00.008552
Executed 'Precision on data slice β`transcription` contains "abc"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17703e5c0>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.42
2024-05-29 13:49:55,679 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,681 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (31, 2) executed in 0:00:00.007550
Executed 'Precision on data slice β`transcription` contains "improved"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17703fdc0>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.42
2024-05-29 13:49:55,703 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,704 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (52, 2) executed in 0:00:00.006033
Executed 'Precision on data slice β`transcription` contains "continue"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176739300>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.42
2024-05-29 13:49:55,725 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,726 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (85, 2) executed in 0:00:00.006033
Executed 'Precision on data slice β`transcription` contains "discharge"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1765f1d50>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.42
2024-05-29 13:49:55,862 pid:66538 MainThread giskard.datasets.base INFO Casting dataframe columns from {'transcription': 'object'} to {'transcription': 'object'}
2024-05-29 13:49:55,863 pid:66538 MainThread giskard.utils.logging_utils INFO Predicted dataset with shape (23, 2) executed in 0:00:00.005574
Executed 'Precision on data slice β`transcription` contains "greater"β' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176ef0940>, 'threshold': 0.5787061994609164}:
Test failed
Metric: 0.43
2024-05-29 13:49:55,866 pid:66538 MainThread giskard.core.suite INFO Executed test suite 'My first test suite'
2024-05-29 13:49:55,866 pid:66538 MainThread giskard.core.suite INFO result: failed
2024-05-29 13:49:55,866 pid:66538 MainThread giskard.core.suite INFO Invariance to βAdd typosβ ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'transformation_function': <giskard.scanner.robustness.text_transformations.TextTypoTransformation object at 0x16ac02200>, 'threshold': 0.95, 'output_sensitivity': 0.05}): {failed, metric=0.9029649595687331}
2024-05-29 13:49:55,867 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "temperature"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176666d70>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.7333333333333333}
2024-05-29 13:49:55,867 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "dr"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176664670>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.7254901960784313}
2024-05-29 13:49:55,867 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "weight"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176ef35e0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.71875}
2024-05-29 13:49:55,867 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "having"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176ef3ee0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.71875}
2024-05-29 13:49:55,868 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "today"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176fb60e0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.717948717948718}
2024-05-29 13:49:55,868 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "follow"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1765f3a30>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.7027027027027027}
2024-05-29 13:49:55,869 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "blood"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176691270>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6973684210526315}
2024-05-29 13:49:55,869 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "distress"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17678f070>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6944444444444444}
2024-05-29 13:49:55,870 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "stable"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1765ffee0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6808510638297872}
2024-05-29 13:49:55,870 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "mg"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f7fbe0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.68}
2024-05-29 13:49:55,870 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`text_length(transcription)` >= 2145.000β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.text_slicer.MetadataSliceFunction object at 0x1767b1330>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6756756756756757}
2024-05-29 13:49:55,870 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "discharge"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176718eb0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.673469387755102}
2024-05-29 13:49:55,871 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "hospital"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176690c70>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6666666666666666}
2024-05-29 13:49:55,871 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "continue"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1766a7bb0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6666666666666666}
2024-05-29 13:49:55,871 pid:66538 MainThread giskard.core.suite INFO Overconfidence on data slice β`transcription` contains "vital"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1760639a0>, 'threshold': 0.6524137931034483, 'p_threshold': 0.2468526289804987}): {failed, metric=0.6666666666666666}
2024-05-29 13:49:55,871 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "xyz"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17703d9c0>, 'threshold': 0.5787061994609164}): {failed, metric=0.3181818181818182}
2024-05-29 13:49:55,872 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "subjective"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17678d5a0>, 'threshold': 0.5787061994609164}): {failed, metric=0.36666666666666664}
2024-05-29 13:49:55,873 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "admission"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f8c670>, 'threshold': 0.5787061994609164}): {failed, metric=0.375}
2024-05-29 13:49:55,873 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "daily"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17673be80>, 'threshold': 0.5787061994609164}): {failed, metric=0.38461538461538464}
2024-05-29 13:49:55,873 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "coronary"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f9ce80>, 'threshold': 0.5787061994609164}): {failed, metric=0.391304347826087}
2024-05-29 13:49:55,873 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "aspirin"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f9d600>, 'threshold': 0.5787061994609164}): {failed, metric=0.391304347826087}
2024-05-29 13:49:55,873 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "followup"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176666a10>, 'threshold': 0.5787061994609164}): {failed, metric=0.3958333333333333}
2024-05-29 13:49:55,874 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "lung"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1760120e0>, 'threshold': 0.5787061994609164}): {failed, metric=0.4}
2024-05-29 13:49:55,874 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "count"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176646980>, 'threshold': 0.5787061994609164}): {failed, metric=0.4}
2024-05-29 13:49:55,875 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "function"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176f5c400>, 'threshold': 0.5787061994609164}): {failed, metric=0.41379310344827586}
2024-05-29 13:49:55,875 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "abc"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17703e5c0>, 'threshold': 0.5787061994609164}): {failed, metric=0.4166666666666667}
2024-05-29 13:49:55,876 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "improved"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x17703fdc0>, 'threshold': 0.5787061994609164}): {failed, metric=0.41935483870967744}
2024-05-29 13:49:55,876 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "continue"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176739300>, 'threshold': 0.5787061994609164}): {failed, metric=0.4230769230769231}
2024-05-29 13:49:55,877 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "discharge"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x1765f1d50>, 'threshold': 0.5787061994609164}): {failed, metric=0.4235294117647059}
2024-05-29 13:49:55,878 pid:66538 MainThread giskard.core.suite INFO Precision on data slice β`transcription` contains "greater"β ({'model': <giskard.models.function.PredictionFunctionModel object at 0x109889240>, 'dataset': <giskard.datasets.base.Dataset object at 0x16ac01570>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x176ef0940>, 'threshold': 0.5787061994609164}): {failed, metric=0.43478260869565216}
[14]:
Customize your suite by loading objects from the Giskard catalogΒΆ
The Giskard open source catalog will enable to load:
Tests such as metamorphic, performance, prediction & data drift, statistical tests, etc
Slicing functions such as detectors of toxicity, hate, emotion, etc
Transformation functions such as generators of typos, paraphrase, style tune, etc
To create custom tests, refer to this page.
For demo purposes, we will load a simple unit test (test_f1) that checks if the test F1 score is above the given threshold. For more examples of tests and functions, refer to the Giskard catalog.
[ ]:
test_suite.add_test(testing.test_f1(model=giskard_model, dataset=giskard_dataset, threshold=0.7)).run()