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

add customdata for distribution #1392

Merged
merged 4 commits into from
Dec 13, 2024
Merged
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
26 changes: 20 additions & 6 deletions src/evidently/ui/dashboards/reports.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from collections import defaultdict
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
Expand Down Expand Up @@ -29,6 +30,7 @@
from evidently.ui.type_aliases import DataPointsAsType
from evidently.ui.type_aliases import PointInfo
from evidently.ui.type_aliases import ProjectID
from evidently.ui.type_aliases import SnapshotID

if TYPE_CHECKING:
from evidently.ui.base import DataStorage
Expand Down Expand Up @@ -173,10 +175,13 @@ async def build(
raise ValueError(f"Cannot build hist from {self.value}")
if len(bins_for_hists) > 1:
raise ValueError(f"Ambiguious metrics for {self.value}")
bins_for_hist: List[Tuple[datetime.datetime, HistogramData]] = next(
metric = next(iter(bins_for_hists.keys()))
fingerprint = metric.get_fingerprint()
bins_for_hist: List[Tuple[datetime.datetime, SnapshotID, HistogramData]] = next(
[
(
d.timestamp,
d.snapshot_id,
d.value if isinstance(d.value, HistogramData) else HistogramData.from_distribution(d.value),
)
for d in v
Expand All @@ -187,26 +192,35 @@ async def build(
timestamps: List[datetime.datetime] = []
names: Set[str] = set()
values: List[Dict[str, Any]] = []
snapshot_ids = []

for timestamp, hist in bins_for_hist:
for timestamp, snapshot_id, hist in bins_for_hist:
timestamps.append(timestamp)
data = dict(zip(hist.x, hist.count))
names.update(data.keys())
values.append(data)
names.update(data.keys())
snapshot_ids.append(snapshot_id)

names_sorted = list(sorted(names))
name_to_date_value: Dict[str, List[Any]] = {name: [] for name in names_sorted}
for timestamp, data in zip(timestamps, values):
name_to_date_value: Dict[str, List[Any]] = defaultdict(list)
name_to_snapshot_id: Dict[str, List[SnapshotID]] = defaultdict(list)
for timestamp, snapshot_id, data in zip(timestamps, snapshot_ids, values):
for name in names_sorted:
name_to_date_value[name].append(data.get(name))
name_to_snapshot_id[name].append(snapshot_id)

hovertemplate = "<b>{name}: %{{y}}</b><br><b>Timestamp: %{{x}}</b>"
fig = go.Figure(
data=[
go.Bar(
name=name,
x=timestamps,
y=name_to_date_value.get(name),
y=name_to_date_value[name],
hovertemplate=hovertemplate.format(name=name),
customdata=[
{"metric_fingerprint": fingerprint, "snapshot_id": str(snapshot_id)}
for snapshot_id in name_to_snapshot_id[name]
],
)
for name in names_sorted
]
Expand Down
Loading