Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text metric #622

Merged
merged 5 commits into from
Jun 5, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wDEp8XLyTVMl"
},
"outputs": [],
"source": [
"try:\n",
" import evidently\n",
"except:\n",
" !pip install git+https://github.com/evidentlyai/evidently.git"
]
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"from sklearn import datasets\n",
"\n",
"from evidently import ColumnMapping\n",
"from evidently.report import Report\n",
"\n",
"from evidently.metrics import ColumnDriftMetric, ColumnValuePlot, Comment"
],
"metadata": {
"id": "wAK2ki_SULhx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"adult_data = datasets.fetch_openml(name='adult', version=2, as_frame='auto')\n",
"adult = adult_data.frame\n",
"\n",
"adult_ref = adult[~adult.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
"adult_cur = adult[adult.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]"
],
"metadata": {
"id": "gEFdC8pxUcwX"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"text_example = \"\"\"\n",
" # Header H1\n",
" Important paragraph!\n",
"\n",
" ## Header H2\n",
" - point 1\n",
" - point 2\n",
" - point 3\n",
"\"\"\""
],
"metadata": {
"id": "eq7gbQnvU0iq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"report = Report(metrics=[\n",
" ColumnDriftMetric('age'),\n",
" Comment(text_example),\n",
" ColumnValuePlot('age'), \n",
"])\n",
"\n",
"report.run(reference_data=adult_ref, current_data=adult_cur)\n",
"report"
],
"metadata": {
"id": "G5rYds0rUeCw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "uwSbgPnUU5vb"
},
"execution_count": null,
"outputs": []
}
]
}
2 changes: 2 additions & 0 deletions src/evidently/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .data_drift.embeddings_drift import EmbeddingsDriftMetric
from .data_drift.target_by_features_table import TargetByFeaturesTable
from .data_drift.text_descriptors_drift_metric import TextDescriptorsDriftMetric
from .data_drift.text_metric import Comment
from .data_integrity.column_missing_values_metric import ColumnMissingValuesMetric
from .data_integrity.column_regexp_metric import ColumnRegExpMetric
from .data_integrity.column_summary_metric import ColumnSummaryMetric
Expand Down Expand Up @@ -79,6 +80,7 @@
"ColumnQuantileMetric",
"ColumnValueListMetric",
"ColumnValueRangeMetric",
"Comment",
"ConflictPredictionMetric",
"ConflictTargetMetric",
"DatasetCorrelationsMetric",
Expand Down
33 changes: 33 additions & 0 deletions src/evidently/metrics/data_drift/text_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List

from evidently.base_metric import InputData
from evidently.base_metric import Metric
from evidently.base_metric import MetricResult
from evidently.core import IncludeTags
from evidently.model.widget import BaseWidgetInfo
from evidently.renderers.base_renderer import MetricRenderer
from evidently.renderers.base_renderer import default_renderer
from evidently.renderers.html_widgets import text_widget


class CommentResults(MetricResult):
class Config:
dict_include = False
tags = {IncludeTags.Render}

text: str


class Comment(Metric[CommentResults]):
def __init__(self, text: str):
self.text = text

def calculate(self, data: InputData) -> CommentResults:
return CommentResults(text=self.text)


@default_renderer(wrap_type=Comment)
class CommentRenderer(MetricRenderer):
def render_html(self, obj: Comment) -> List[BaseWidgetInfo]:
result = obj.get_result()
return [text_widget(text=result.text, title="")]
11 changes: 11 additions & 0 deletions src/evidently/renderers/html_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,3 +921,14 @@ def get_class_separation_plot_data_agg(

additional_plots.append((str(label), plotly_figure(title="", figure=fig)))
return additional_plots


def text_widget(*, text: str, title: str = "", size: WidgetSize = WidgetSize.FULL):
"""
generate widget with markdown text
Args:
text: markdown formatted text
title: widget title
size: widget size
"""
return BaseWidgetInfo(title=title, type="text", size=size.value, params={"text": text})