Skip to content

Add Cramer's V contingency metric - #459

Open
Kkkakania wants to merge 3 commits into
xarray-contrib:mainfrom
Kkkakania:feat/cramers-v
Open

Kkkakania wants to merge 3 commits into
xarray-contrib:mainfrom
Kkkakania:feat/cramers-v

Conversation

@Kkkakania

Copy link
Copy Markdown
Contributor

Description

Add Cramér's V as a multi-category metric on Contingency. The implementation uses xarray operations throughout, preserves Dataset dimensions and Dask laziness, and excludes empty rows or columns when determining the effective table size.

Closes #75

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • compared a 2x2 result with scipy.stats.contingency.association(method="cramer")
  • covered Dataset and Dask inputs, empty declared categories, and invalid one-category tables
  • uv run --extra test pytest -q (2798 passed, 3 skipped)
  • pre-commit on all four changed files, including Ruff, blackdoc, doc8, and mypy

Checklist (while developing)

  • I have added a docstring and API reference entry for the new method.
  • The implementation is expressed through named xarray dimensions and operations.
  • Tests added for pytest.

Pre-Merge Checklist (final steps)

  • Based on the current upstream main.
  • Contains one focused commit.

References

AI assistance

I used an AI coding assistant to help inspect the existing contingency API and draft tests. I reviewed the statistical implementation, compared it with SciPy, and ran the complete test and pre-commit suites locally.

@Kkkakania

Copy link
Copy Markdown
Contributor Author

I investigated the failed Read the Docs build. It reaches the existing tabular-data.ipynb gallery notebook and fails while fetch_california_housing() looks for a missing local cal_housing.pkz dataset file. That notebook and its data-loading path are unrelated to the new contingency metric. The focused contingency tests, full local suite, typing, and pre-commit checks for this PR pass.

@codecov

codecov Bot commented Sep 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.65%. Comparing base (e457985) to head (fa6dd72).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #459      +/-   ##
==========================================
+ Coverage   94.57%   94.65%   +0.07%     
==========================================
  Files          27       27              
  Lines        2841     2920      +79     
==========================================
+ Hits         2687     2764      +77     
- Misses        154      156       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

)
degrees = (
xr.apply_ufunc(
np.minimum,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn’t this work within apply_ufunc using xr min()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: cramer’s v

2 participants