Repository navigation
fix(monitoring): exact Spark KS through SciPy's public ks_2samp - #207
flyingriverhorse wants to merge 1 commit into
Conversation
The 1k-10k row KS path imported SciPy's private _attempt_exact_2kssamp, which test_internal_api_boundary rejects. The exact probability depends only on n, m and the statistic, so two tied samples with the same statistic now go through public ks_2samp(method="exact"). It runs the same exact count, so p-values are unchanged (identical on 18 checked size/shift cases, including equal and coprime sizes). If SciPy cannot finish the exact count it warns; that warning now returns None and the existing DKW fallback applies, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: flyingriverhorse <buzzycarl@gmail.com>
Reviewer's GuideThe PR removes the forbidden SciPy private Sequence diagram for public exact KS probability with DKW fallbacksequenceDiagram
participant Evidence as _ks_evidence
participant Probability as _scipy_exact_probability
participant SciPy as scipy.stats.ks_2samp
participant DKW as DKW fallback
Evidence->>Probability: _scipy_exact_probability(statistic, n, m)
Probability->>Probability: math.gcd(n, m)
Probability->>Probability: np.repeat(reference, current)
Probability->>SciPy: ks_2samp(reference, current, method="exact")
alt exact count succeeds
SciPy-->>Probability: pvalue
Probability-->>Evidence: probability
else warning or invalid reconstruction
SciPy-->>Probability: RuntimeWarning or error
Probability-->>Evidence: None
Evidence->>DKW: use DKW bound
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="skyulf-core/skyulf/integrations/databricks/observability/monitoring/spark/spark_monitoring_drift.py" line_range="73" />
<code_context>
- except (ImportError, TypeError, ValueError, ArithmeticError):
+ with warnings.catch_warnings():
+ # SciPy warns and silently switches to the asymptotic method on failure.
+ warnings.simplefilter("error", RuntimeWarning)
+ probability = float(ks_2samp(reference, current, method="exact").pvalue)
+ except (RuntimeWarning, ValueError, ArithmeticError):
</code_context>
<issue_to_address>
**Concurrent drift evidence is wrong**
When two monitoring calls overlap while SciPy emits its exact-count RuntimeWarning, `warnings.catch_warnings()` and `warnings.simplefilter()` modify process-wide warning filters, so one call can restore the filter before another call handles SciPy’s warning. The second call then accepts an asymptotic p-value as exact and can report incorrect drift evidence, while an unrelated thread’s RuntimeWarning can also be raised as an exception.
Handle the exact-count warning without changing process-wide warning filters.
</issue_to_address>
### Comment 2
<location path="skyulf-core/skyulf/integrations/databricks/observability/monitoring/spark/spark_monitoring_drift.py" line_range="74" />
<code_context>
+ with warnings.catch_warnings():
+ # SciPy warns and silently switches to the asymptotic method on failure.
+ warnings.simplefilter("error", RuntimeWarning)
+ probability = float(ks_2samp(reference, current, method="exact").pvalue)
+ except (RuntimeWarning, ValueError, ArithmeticError):
return None
</code_context>
<issue_to_address>
**Databricks behavior unvalidated**
When this Spark drift behavior change is validated only in the local environment, the new `ks_2samp(..., method="exact")` path is compared only against local SciPy, leaving its execution in the Databricks runtime unverified and a runtime-specific drift regression undetected.
Run a focused validation of the changed KS path on Databricks.
</issue_to_address>
### Comment 3
<location path="skyulf-core/tests/spark/test_monitoring_drift.py" line_range="180" />
<code_context>
+@pytest.mark.parametrize(("n", "m"), [(2000, 2000), (1999, 2003), (1001, 9999)])
+def test_middle_range_matches_local_exact_ks_for_any_sizes(n, m):
+ """Equal, coprime and uneven sizes take other exact-count paths; all must match local."""
+ from skyulf.integrations.databricks.observability.monitoring.spark.spark_monitoring_drift import (
+ _ks_evidence,
</code_context>
<issue_to_address>
**Drift impact left unclear**
When the new parametrized test is added, the docstring says the results must match local but not what breaks if they do not, leaving the significance of divergent Spark drift evidence for these sample sizes unstated.
State in the docstring that mismatches would make Spark drift evidence differ from local KS results for these sample sizes.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: skyulf-core/skyulf/integrations/databricks/observability/monitoring/spark/spark_monitoring_drift.py:73, skyulf-core/skyulf/integrations/databricks/observability/monitoring/spark/spark_monitoring_drift.py:74
| except (ImportError, TypeError, ValueError, ArithmeticError): | ||
| with warnings.catch_warnings(): | ||
| # SciPy warns and silently switches to the asymptotic method on failure. | ||
| warnings.simplefilter("error", RuntimeWarning) |
There was a problem hiding this comment.
🟡 Medium · Concurrent drift evidence is wrong
When two monitoring calls overlap while SciPy emits its exact-count RuntimeWarning, warnings.catch_warnings() and warnings.simplefilter() modify process-wide warning filters, so one call can restore the filter before another call handles SciPy’s warning. The second call then accepts an asymptotic p-value as exact and can report incorrect drift evidence, while an unrelated thread’s RuntimeWarning can also be raised as an exception.
Handle the exact-count warning without changing process-wide warning filters.
Prompt for AI agents
In `skyulf-core/skyulf/integrations/databricks/observability/monitoring/spark/spark_monitoring_drift.py` at line 73:
**Concurrent drift evidence is wrong**
When two monitoring calls overlap while SciPy emits its exact-count RuntimeWarning, `warnings.catch_warnings()` and `warnings.simplefilter()` modify process-wide warning filters, so one call can restore the filter before another call handles SciPy’s warning. The second call then accepts an asymptotic p-value as exact and can report incorrect drift evidence, while an unrelated thread’s RuntimeWarning can also be raised as an exception.
Handle the exact-count warning without changing process-wide warning filters.| with warnings.catch_warnings(): | ||
| # SciPy warns and silently switches to the asymptotic method on failure. | ||
| warnings.simplefilter("error", RuntimeWarning) | ||
| probability = float(ks_2samp(reference, current, method="exact").pvalue) |
There was a problem hiding this comment.
🟡 Medium · Databricks behavior unvalidated
When this Spark drift behavior change is validated only in the local environment, the new ks_2samp(..., method="exact") path is compared only against local SciPy, leaving its execution in the Databricks runtime unverified and a runtime-specific drift regression undetected.
Run a focused validation of the changed KS path on Databricks.
Prompt for AI agents
In `skyulf-core/skyulf/integrations/databricks/observability/monitoring/spark/spark_monitoring_drift.py` at line 74:
**Databricks behavior unvalidated**
When this Spark drift behavior change is validated only in the local environment, the new `ks_2samp(..., method="exact")` path is compared only against local SciPy, leaving its execution in the Databricks runtime unverified and a runtime-specific drift regression undetected.
Run a focused validation of the changed KS path on Databricks.From AGENTS.md L122-124:
- Keep Ruff, full CI Ty scope, applicable complexity checks and pre-commit hooks.
For Databricks behavior changes, retain focused validation on Databricks itself.
Do not reduce CI coverage or skip relevant tests to shorten local feedback.|
|
||
| @pytest.mark.parametrize(("n", "m"), [(2000, 2000), (1999, 2003), (1001, 9999)]) | ||
| def test_middle_range_matches_local_exact_ks_for_any_sizes(n, m): | ||
| """Equal, coprime and uneven sizes take other exact-count paths; all must match local.""" |
There was a problem hiding this comment.
⚪ Low · Drift impact left unclear
When the new parametrized test is added, the docstring says the results must match local but not what breaks if they do not, leaving the significance of divergent Spark drift evidence for these sample sizes unstated.
State in the docstring that mismatches would make Spark drift evidence differ from local KS results for these sample sizes.
Prompt for AI agents
In `skyulf-core/tests/spark/test_monitoring_drift.py` at line 180:
**Drift impact left unclear**
When the new parametrized test is added, the docstring says the results must match local but not what breaks if they do not, leaving the significance of divergent Spark drift evidence for these sample sizes unstated.
State in the docstring that mismatches would make Spark drift evidence differ from local KS results for these sample sizes.From AGENTS.md L143-145:
- **New tests still get a one-line docstring** stating the behaviour being
pinned — what would break if the test failed. The test name carries the *what*,
the docstring carries the *why it matters*.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|



What and why
Fixes the CI failure in
test_internal_api_boundary.py::test_integration_imports_do_not_borrow_private_symbols. Spark drift for 1,000–10,000 rows (added in 1a2a5ed) imported SciPy's private_attempt_exact_2kssamp.The exact two-sample KS probability depends only on
n,mand the statistic._scipy_exact_probabilitynow rebuilds two tied samples with that statistic (izeros amongn,jzeros amongm,|i/n − j/m| = d) and calls publicscipy.stats.ks_2samp(method="exact"), which runs the same exact count internally. Results don't change: healthy data is still judged like localks_2sampand does not fall back to the DKW bound.When SciPy can't finish the exact count, it warns and silently switches to the asymptotic method. That warning now returns
None, so the existing DKW fallback applies, the same as the oldsuccess=Falsepath. The boundary test is unchanged; no exception was added to it.Type
How I checked it
ks_2samp.test_middle_range_matches_local_exact_ks_for_any_sizescovers equal, coprime and uneven sizes.pytest test_internal_api_boundary.pyplus the KS tests intests/spark/test_monitoring_drift.py: 24 passed.ruff check,ruff format --check, the full CIty checkscope, andlizard -C 10on the file: all clean.Checklist
changelog/and docs updated: not needed, no user-visible changegit commit -s), no secrets committed🤖 Generated with Claude Code
Summary by Sourcery
Use SciPy’s public two-sample KS API for exact Spark drift probabilities without changing monitoring outcomes.
Bug Fixes:
ks_2sampAPI while preserving exact drift probabilities and applying the existing DKW fallback when exact evaluation cannot complete.Enhancements:
Tests: