feat: Introduce new summary section for data metrics#47
feat: Introduce new summary section for data metrics#47Moritz Potthoff (MoritzPotthoffQC) wants to merge 24 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #47 +/- ##
===========================================
- Coverage 100.00% 99.64% -0.36%
===========================================
Files 14 14
Lines 1052 1134 +82
===========================================
+ Hits 1052 1130 +78
- Misses 0 4 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR extends Diffly’s summary reporting to distinguish change metrics (computed over deltas) from data inspection metrics (describing left/right datasets), and renders the latter in a new dedicated “Data Inspection” section while also separating them in the JSON schema.
Changes:
- Add
Metric.kind("change"vs"data") and mark theNull%preset as a"data"metric. - Split per-column metric outputs into
metricsvsdata_metricsin the summary JSON, and render"data"metrics in a new “Data Inspection” section. - Update summary tests and regenerate pretty-output fixtures to reflect the new section/layout.
Reviewed changes
Copilot reviewed 36 out of 36 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| diffly/metrics/_common.py | Adds Metric.kind to classify metrics by rendering section. |
| diffly/metrics/data.py | Marks Null% preset as a "data" metric. |
| diffly/summary.py | Routes metric labels into change vs data lists; adds “Data Inspection” rendering and JSON schema changes. |
| tests/summary/test_summary.py | Adds coverage for metrics vs data_metrics JSON routing; updates expected JSON shape. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_True_slim_True_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_True_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_True_slim_False_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_True_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_False_slim_True_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_False_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_False_slim_False_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_True_top_False_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_True_slim_True_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_True_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_True_slim_False_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_True_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_False_slim_True_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_False_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_False_slim_False_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_True_perfect_False_top_False_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_True_slim_True_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_True_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_True_slim_False_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_True_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_False_slim_True_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_False_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_False_slim_False_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_True_top_False_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_True_slim_True_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_True_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_True_slim_False_sample_rows_True_sample_pk_True.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_True_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_False_slim_True_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_False_slim_True_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_False_slim_False_sample_rows_True_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
| tests/summary/fixtures/metrics_null_fraction/gen/pretty_False_perfect_False_top_False_slim_False_sample_rows_False_sample_pk_False.txt | Updates fixture output to move Null% into “Data Inspection”. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
EgeKaraismailogluQC
left a comment
There was a problem hiding this comment.
Thank you Moritz Potthoff (@MoritzPotthoffQC)! In general I am happy with the layout. I added a few comments. In addition to those, I think it would be nice to have at least one summary fixture where we render two or more data metrics rather than just one, just to see what that looks like (maybe I missed it?).
|
|
||
| fn: MetricFn | ||
| selector: cs.Selector | ||
| kind: Literal["change", "data"] = "change" |
There was a problem hiding this comment.
I think it could be good to not provide a default here to force the user to think about what kind of metric they implemented. Wdyt?
There was a problem hiding this comment.
Also, nothing prevents me from "registering" a change metric as a data metric and vice versa. Going a step further, I wish there was a way to infer kind from the type of fn automatically. Right now that does not work of course since both change metrics and data metrics are the same type. Something along these lines:
DataMetricFn = Callable[[pl.Expr], pl.Expr]and then rewrite:
def null_fraction_change(col: pl.Expr) -> pl.Expr:
return col.is_null().mean()
class Metric:
fn: ChangeMetricFn | DataMetricFn
#(property)
def kind(self) -> str:
return "data" if isinstance(DataMetricFn, self.fn) else "change"Metric would then also need to wrap .fn if it is a DataMetricFn to make it have the same interface. Could you follow my idea? Wdyt?
There was a problem hiding this comment.
Agreed, thanks for the good point!
I went even a bit further now and clearly split the metrics by introducing two classes ChangeMetric and Data Metric. That way, the diff between them is clear and DataMetric even has a bit more logic. Internally, we now always use these classes (rather then bare callables), because I find it intuitive that way. When running a comparison, the user can still pass in callables. These will be wrapped in the appropriate class, based on the arity of the callable (one expression -> data, two expressions -> change)
There was a problem hiding this comment.
One more change is that we now properly compute data metrics on both columns independently. Previously, we evaluated them on the joined rows (only). Now, we actually compute them on both columns independently. So now we also consider unjoined rows for the data metrics (which is what I expect here).
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
EgeKaraismailogluQC I made quite a few changes since your review. I'd appreciate another look. Let me know if you want to chat about this! |
EgeKaraismailogluQC
left a comment
There was a problem hiding this comment.
Thank you for working on this Moritz Potthoff (@MoritzPotthoffQC), looks very nice! Just a few cosmetic comments. Let's also add ! to the PR title to mark it as breaking.
| The expressions refer to the left-side and right-side values of a single column across | ||
| all joined rows. | ||
| """ | ||
| ALL_METRICS: dict[str, Metric] = { |
There was a problem hiding this comment.
?
| ALL_METRICS: dict[str, Metric] = { | |
| DEFAULT_METRICS: dict[str, Metric] = { |
| DEFAULT_DATA_METRICS: dict[str, DataMetric] = { | ||
| "Null%": DataMetric( | ||
| fn=null_fraction, | ||
| selector=cs.all(), |
There was a problem hiding this comment.
It is the default
| selector=cs.all(), |
|
|
||
|
|
||
| def _resolve_metric(v: ChangeMetricFn | DataMetricFn | Metric) -> Metric: | ||
| if isinstance(v, (ChangeMetric, DataMetric)): |
There was a problem hiding this comment.
?
| if isinstance(v, (ChangeMetric, DataMetric)): | |
| if isinstance(v, Metric): |
| return v | ||
| # Infer the metric family from the callable's arity: a single-argument | ||
| # callable describes one side (data), two arguments describe a change. | ||
| if len(inspect.signature(v).parameters) >= 2: |
There was a problem hiding this comment.
Why not:
| if len(inspect.signature(v).parameters) >= 2: | |
| if isinstance(v, ChangeMetricFn): |
Also, how about being a bit defensive here and do:
elif isinstance(v, DataMetricFn):
...
else:
raiseso that the signature issue is easier to understand for the user?
| n_total_changes=n_total_changes, | ||
| changes=changes, | ||
| metrics=metrics_by_column.get(col_name), | ||
| change_metrics=change_metrics_by_column.get(col_name) or None, |
There was a problem hiding this comment.
?
| change_metrics=change_metrics_by_column.get(col_name) or None, | |
| change_metrics=change_metrics_by_column.get(col_name), |
| @@ -963,20 +1041,50 @@ def select_columns(selector: cs.Selector) -> set[str]: | |||
| for label, metric in metrics.items() | |||
| for column in sorted(metric_to_columns[label]) | |||
| ] | |||
| row = joined.select(agg_exprs).collect().row(0, named=True) | |||
| for label, columns in metric_to_columns.items(): | |||
| for column in columns: | |||
|
|
|||
| row = comp.joined(lazy=True).select(agg_exprs).collect().row(0, named=True) | |||
| out: dict[str, dict[str, Any]] = { | |||
| c: {} for c in set().union(*metric_to_columns.values()) | |||
| } | |||
| for label in metrics: | |||
| for column in metric_to_columns[label]: | |||
| out[column][label] = row[f"{label}__{column}"] | |||
| return out | |||
There was a problem hiding this comment.
Am I missing something or is this part just a "shallow" refactor?
| formatter=lambda value: f"{round(value * 100, 2)}%", | ||
| delta_formatter=lambda value: f"{round(value * 100, 2)}", |
There was a problem hiding this comment.
Should we use these as the default formatter for DataMetric?
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| Metric = ChangeMetric | DataMetric |
There was a problem hiding this comment.
We use ChangeMetricFn | DataMetricFn in a few places. For comfort, how about also defining
MetricFn = ChangeMetricFn | DataMetricFn?
Motivation
See this discussion: We want to display
datametrics in their own section in the summary.Changes
Data Inspectionthat gets all the data metrics