-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Generalize metrics and add nullability delta metric #43
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
Open
Moritz Potthoff (MoritzPotthoffQC)
wants to merge
19
commits into
main
Choose a base branch
from
generalize-metrics-add-nullability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e97b755
init
MoritzPotthoffQC 1e72823
better output
MoritzPotthoffQC 1d7bdeb
cleanup
MoritzPotthoffQC a74429f
refactor
MoritzPotthoffQC 71338c9
Potential fix for pull request finding
MoritzPotthoffQC cd33f6a
fix
MoritzPotthoffQC f66bfa0
fix
MoritzPotthoffQC db3b168
Potential fix for pull request finding
MoritzPotthoffQC eb1c427
Merge branch 'main' into generalize-metrics-add-nullability
MoritzPotthoffQC 2923f2e
Update docs/api/metrics.rst
MoritzPotthoffQC e1ea6bd
Update tests/summary/fixtures/metrics_null_fraction/test_metrics_null…
MoritzPotthoffQC fc0426a
chore: Update pixi lockfile (#42)
quant-ranger[bot] f5b9117
review
MoritzPotthoffQC e374f0d
fix
MoritzPotthoffQC 627721e
fix
MoritzPotthoffQC cf686b4
Merge remote-tracking branch 'origin/main' into generalize-metrics-ad…
MoritzPotthoffQC 529b917
fix
MoritzPotthoffQC 6f5671d
fix
MoritzPotthoffQC bfc8d8c
fix docs
MoritzPotthoffQC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright (c) QuantCo 2025-2026 | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| """Metrics computed per column when generating a summary. | ||
|
|
||
| Two families are provided: | ||
|
|
||
| - Metrics in :mod:`~diffly.metrics.change` describe the change between numeric | ||
| columns itself by aggregating over ``right - left``. | ||
| - Metrics in :mod:`~diffly.metrics.data` describe the left and right datasets | ||
| individually, explaining how a change affects the data. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from . import change, data | ||
| from ._common import Metric, MetricFn | ||
| from .change import ( | ||
| _make_numeric_metric, | ||
| max, | ||
| mean, | ||
| mean_absolute_deviation, | ||
| mean_relative_deviation, | ||
| median, | ||
| min, | ||
| quantile, | ||
| std, | ||
| ) | ||
|
|
||
| DEFAULT_METRICS: dict[str, MetricFn | Metric] = { | ||
| **change.DEFAULT_CHANGE_METRICS, | ||
| **data.DEFAULT_DATA_METRICS, | ||
| } | ||
|
|
||
| __all__ = [ | ||
| "DEFAULT_METRICS", | ||
| "Metric", | ||
| "MetricFn", | ||
| "change", | ||
| "data", | ||
| "max", | ||
| "mean", | ||
| "mean_absolute_deviation", | ||
| "mean_relative_deviation", | ||
| "median", | ||
| "min", | ||
| "quantile", | ||
| "std", | ||
| "_make_numeric_metric", | ||
| ] | ||
|
MoritzPotthoffQC marked this conversation as resolved.
Comment on lines
+35
to
+50
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All change metrics and |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Copyright (c) QuantCo 2025-2026 | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| import polars as pl | ||
| import polars.selectors as cs | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Metric: | ||
| """A metric function paired with a column-applicability selector.""" | ||
|
|
||
| fn: MetricFn | ||
| selector: cs.Selector | ||
|
|
||
|
|
||
| MetricFn = Callable[[pl.Expr, pl.Expr], pl.Expr] | ||
| """A metric function maps ``(left_expr, right_expr)`` to a scalar aggregation | ||
| expression. | ||
|
|
||
| The expressions refer to the left-side and right-side values of a single column across | ||
| all joined rows. | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Copyright (c) QuantCo 2025-2026 | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| """Metrics describing the left and right datasets individually. | ||
|
|
||
| These characterize each side of a change so you can understand how the change affects | ||
| the data, rather than describing the change itself. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| import polars as pl | ||
| import polars.selectors as cs | ||
|
|
||
| from ._common import Metric, MetricFn | ||
|
|
||
|
|
||
| def null_fraction_change(left: pl.Expr, right: pl.Expr) -> pl.Expr: | ||
| """Change in the fraction of null entries, rendered as ``<old> -> <new> (<delta>)``. | ||
|
|
||
| ``old`` and ``new`` are the null percentages of the left and right side, and | ||
| ``delta`` is their signed difference (``+`` when the right side has proportionally | ||
| more nulls, ``-`` when it has fewer). This metric function can be applied to columns | ||
| of any type. | ||
| """ | ||
| return _render_change( | ||
| left.is_null().mean(), | ||
| right.is_null().mean(), | ||
| lambda value, signed: _percentage_string( | ||
| value, signed=signed, percent_sign=not signed | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| DEFAULT_DATA_METRICS: dict[str, MetricFn | Metric] = { | ||
| "Null%": Metric(fn=null_fraction_change, selector=cs.all()), | ||
| } | ||
|
|
||
|
|
||
| # ------------------------------------------------------------------------------------ # | ||
| # UTILITY METHODS # | ||
| # ------------------------------------------------------------------------------------ # | ||
|
|
||
|
|
||
| def _percentage_string( | ||
| fraction: pl.Expr, *, signed: bool = False, percent_sign: bool = True | ||
| ) -> pl.Expr: | ||
| """Format a fraction as a percentage string, optionally with an explicit sign.""" | ||
| pct = (fraction * 100).round(2) | ||
| body = pl.format("{}%", pct) if percent_sign else pl.format("{}", pct) | ||
| if signed: | ||
| return pl.when(pct >= 0).then(pl.format("+{}", body)).otherwise(body) | ||
| return body | ||
|
|
||
|
|
||
| def _render_change( | ||
| old: pl.Expr, | ||
| new: pl.Expr, | ||
| format_value: Callable[[pl.Expr, bool], pl.Expr], | ||
| ) -> pl.Expr: | ||
| """Render a change as ``<old> -> <new> (<delta>)``. | ||
|
|
||
| ``format_value(value, signed)`` formats a value for display; ``old`` and ``new`` are | ||
| rendered unsigned and the delta ``new - old`` is rendered signed (with an explicit | ||
| ``+`` prefix for positive values). | ||
| """ | ||
| return pl.format( | ||
| "{} -> {} ({})", | ||
| format_value(old, False), | ||
| format_value(new, False), | ||
| format_value(new - old, True), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.