Skip to content

REFACTOR: Reorganize type hierarchy of sparse.py. Move array-level fu…#739

Open
genedan wants to merge 5 commits intomainfrom
#737-sparse-hierarchy
Open

REFACTOR: Reorganize type hierarchy of sparse.py. Move array-level fu…#739
genedan wants to merge 5 commits intomainfrom
#737-sparse-hierarchy

Conversation

@genedan
Copy link
Copy Markdown
Collaborator

@genedan genedan commented May 6, 2026

Summary of Changes

  • Reorganize type hierarchy of sparse.py. Move array-level functions to sparse.COO, and keep module-level functions to sp.
  • This isn't always possible, so I kept certain overrides like COO.cumprod, and added sp.sum = COO.sum.
  • Replaces xp() with xp.COO() to create sparse arrays when xp used to be callable and was used as a constructor. This keeps the hierarchy consistent across the 4 backends.
  • Removes unused args and kwargs from sparse.py

Related GitHub Issue(s)

#737
#736

Paves the way towards creating a consistent abstract, maintainable backend which may be used to swap specific backends as fashions change. Inches closer towards accomplishing our ambitious backend dreams.

Additional Context for Reviewers

I'm anticipating some hesitation on calling xp.COO where xp means the result of Triangle.get_array_module. However, this replaces instances when xp was called directly, which could only be done if the backend was sparse anyway. Therefore, the risk level is the same with this replacement, and there are already existing mechanisms in place that mention that the backend is sparse prior to the call. This reorganization of sparse.py is needed before we can tackle replacing xp.COO with something more generalized like xp.BackendArray.


Note

Medium Risk
Touches core sparse-backend construction paths (Triangle init, slicing/assignment, arithmetic, and regression) and changes how sparse.COO is instantiated/default fill values are handled, which could affect numerical results or edge cases. Scope is contained to the sparse backend and is backed by new unit tests for key wrappers.

Overview
Standardizes sparse-backend array construction to always use sp.COO/xp.COO (instead of treating sp/xp as a callable constructor), and updates several call sites (Triangle initialization, .loc/.at sparse assignment, arithmetic validation, and WeightedRegression) accordingly.

Refactors utils/sparse.py to clarify the type hierarchy by exposing COO explicitly, moving/aligning defaults like COO.nan, and trimming unused wrapper signatures; also adds a focused test_sparse.py suite covering array fill-value behavior plus where and in-place floor semantics.

Reviewed by Cursor Bugbot for commit 0715793. Bugbot is set up for automated code reviews on this repo. Configure here.

…nctions to sparse.COO, and keep module-level functions to sp.

Replaces xp() with xp.COO() to create sparse arrays when xp used to be callable and was used as a constructor.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.07%. Comparing base (9358edd) to head (0715793).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
chainladder/core/dunders.py 0.00% 0 Missing and 1 partial ⚠️
chainladder/utils/utility_functions.py 50.00% 1 Missing ⚠️
chainladder/utils/weighted_regression.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #739      +/-   ##
==========================================
+ Coverage   85.94%   86.07%   +0.12%     
==========================================
  Files          85       86       +1     
  Lines        4924     4918       -6     
  Branches      637      637              
==========================================
+ Hits         4232     4233       +1     
+ Misses        491      486       -5     
+ Partials      201      199       -2     
Flag Coverage Δ
unittests 86.07% <90.00%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

Comment thread chainladder/utils/sparse.py
Comment thread chainladder/utils/sparse.py
Comment thread chainladder/utils/sparse.py
Comment thread chainladder/utils/sparse.py
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a33f181. Configure here.

Comment thread chainladder/utils/weighted_regression.py
Comment thread chainladder/core/triangle.py Outdated
@genedan
Copy link
Copy Markdown
Collaborator Author

genedan commented May 7, 2026

@jbogaardt, we decided that we need your review on this one, for your Sparse knowledge.

I wrote a bunch of text, but the quick 1-sentence summary is that I'm trying to get get_array_module to return a single return type (ModuleType) rather than two possible return types (ModuleType | COO) that would be hard to predict when get_array_module is called dynamically.

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.

1 participant