Skip to content

Rolling apply args#689

Merged
d-chambers merged 2 commits into
masterfrom
rolling-apply-args
May 22, 2026
Merged

Rolling apply args#689
d-chambers merged 2 commits into
masterfrom
rolling-apply-args

Conversation

@d-chambers
Copy link
Copy Markdown
Contributor

@d-chambers d-chambers commented May 22, 2026

Description

This PR updates rolling apply so custom functions can receive additional positional and keyword arguments directly from the apply call.

For example:

patch.rolling(time=100, samples=True).apply(np.percentile, 80)

The change supports both rolling engines:

  • numpy engine: forwards *args and **kwargs to the provided function
  • pandas engine: forwards *args and **kwargs through pandas rolling args/kwargs

This also removes the unused internal func_kwargs field from _PatchRollerInfo, since function arguments are now supplied per apply call instead of stored on the roller object.

The shared apply docstring now documents the arguments and includes an example. Tests were added for numpy and pandas rolling apply with extra positional and keyword arguments.

Superceeds #682.

Checklist

I have (if applicable):

  • referenced the GitHub issue this PR closes.
  • documented the new feature with docstrings and/or appropriate doc page.
  • included tests. See testing guidelines.
  • added the "ready_for_review" tag once the PR is ready to be reviewed.

Summary by CodeRabbit

  • New Features

    • Rolling apply method now accepts extra positional and keyword arguments to pass to user-provided functions.
  • Documentation

    • Enhanced rolling documentation with additional examples demonstrating parameter passing in rolling operations.
  • Tests

    • Added test coverage for positional and keyword argument support across rolling engines.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 22, 2026

📝 Walkthrough

Walkthrough

The rolling .apply() method signature is expanded to accept positional and keyword arguments forwarded to user-provided functions. The func_kwargs field is removed from the base model. Both numpy and pandas implementations are updated, documentation is refactored into a shared template, and tests validate both engines pass arguments correctly.

Changes

Rolling apply with args and kwargs support

Layer / File(s) Summary
API contract, docstring template, and base model cleanup
dascore/proc/rolling.py
The Field import is removed; a new reusable rolling_apply_description docstring template is added describing apply operation parameters; and the func_kwargs field is deleted from _PatchRollerInfo.
Numpy roller apply implementation
dascore/proc/rolling.py
_NumpyPatchRoller.apply() is updated to accept *args and **kwargs, decorated with @compose_docstring(...) to use the shared template, and implementation forwards arguments as function(trimmed_slide_view, *args, axis=-1, **kwargs).
Pandas roller apply implementation
dascore/proc/rolling.py
_PandasPatchRoller.apply() is updated to accept *args and **kwargs, forwarding them into pandas rolling .apply(...) via args=args and kwargs=kwargs parameters.
Documentation example with percentile
dascore/proc/rolling.py
Module-level rolling documentation is extended with a np.percentile example demonstrating passing an additional argument to .apply().
Test coverage for numpy and pandas apply with args and kwargs
tests/test_proc/test_rolling.py
Two new test cases verify that .apply() correctly forwards positional and keyword arguments on both engine="numpy" and engine="pandas" rolling implementations by computing a shifted percentile and comparing against lambda-based rolling computation.

Suggested labels

ready_for_review, proc

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Rolling apply args' directly describes the main change: expanding the rolling apply API to accept additional arguments for user-provided functions.
Description check ✅ Passed The description adequately explains the feature, provides usage examples, documents both engine implementations, and indicates that tests and docstrings were added.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch rolling-apply-args

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot added ready_for_review PR is ready for review proc Related to processing module labels May 22, 2026
@d-chambers
Copy link
Copy Markdown
Contributor Author

Hey @andreas-wuestefeld,

I had a few minutes before going home so I took a stab at the changes mentioned in #682. Mind taking a look to see if this will do what you need to simplify your other PRs?

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
dascore/proc/rolling.py (1)

107-131: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Guard against axis kwarg collisions in _NumpyPatchRoller.apply.

rolling.apply currently calls function(..., axis=-1, **kwargs). If the call site passes axis in **kwargs, Python raises a duplicate-keyword TypeError (got multiple values for keyword argument 'axis'). Reject axis explicitly with a ParameterError before invoking function.

Suggested patch
     def apply(self, function, *args, **kwargs):
         """
         {apply_description}

         Notes
         -----
         The provided function must accept an ``axis`` argument.
         """
+        if "axis" in kwargs:
+            msg = "`axis` is managed internally for rolling.apply; do not pass `axis`."
+            raise ParameterError(msg)
         # TODO look at replacing this with a call to `as_strided` that
         # accounts for strides.
         slide_view = np.lib.stride_tricks.sliding_window_view(
             self.patch.data,
             self.window,
             self.axis,
         )
@@
-        raw = function(trimmed_slide_view, *args, axis=-1, **kwargs).astype(np.float64)
+        raw = function(trimmed_slide_view, *args, axis=-1, **kwargs).astype(np.float64)
         out = self._pad_roll_array(raw)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dascore/proc/rolling.py` around lines 107 - 131, In _NumpyPatchRoller.apply,
guard against duplicate 'axis' by checking if 'axis' is present in the passed
**kwargs before calling function(..., axis=-1, **kwargs); if 'axis' in kwargs
raise ParameterError (use the project ParameterError type) with a clear message
(e.g. "axis kwarg is not allowed; rolling.apply sets axis internally"), then
proceed to call function with axis=-1; add this check just before the line that
calls function in apply.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@dascore/proc/rolling.py`:
- Around line 107-131: In _NumpyPatchRoller.apply, guard against duplicate
'axis' by checking if 'axis' is present in the passed **kwargs before calling
function(..., axis=-1, **kwargs); if 'axis' in kwargs raise ParameterError (use
the project ParameterError type) with a clear message (e.g. "axis kwarg is not
allowed; rolling.apply sets axis internally"), then proceed to call function
with axis=-1; add this check just before the line that calls function in apply.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4be00770-4dff-485c-adb9-ee435dc7e71f

📥 Commits

Reviewing files that changed from the base of the PR and between fc4e8be and 86bd847.

📒 Files selected for processing (2)
  • dascore/proc/rolling.py
  • tests/test_proc/test_rolling.py

@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.93%. Comparing base (2a54731) to head (86bd847).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #689   +/-   ##
=======================================
  Coverage   99.93%   99.93%           
=======================================
  Files         135      135           
  Lines       11704    11737   +33     
=======================================
+ Hits        11696    11729   +33     
  Misses          8        8           
Flag Coverage Δ
unittests 99.93% <100.00%> (+<0.01%) ⬆️

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.

Copy link
Copy Markdown
Contributor

@andreas-wuestefeld andreas-wuestefeld left a comment

Choose a reason for hiding this comment

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

This looks like something I would have needed a while ago :-)
Not tested yet, will try over the weekend

@d-chambers
Copy link
Copy Markdown
Contributor Author

OK, let's merge for now. If you decide the other api is better we can always do a follow up PR before the next release.

@d-chambers d-chambers merged commit 64cf5ca into master May 22, 2026
29 checks passed
@d-chambers d-chambers deleted the rolling-apply-args branch May 22, 2026 18:13
@coderabbitai coderabbitai Bot mentioned this pull request May 22, 2026
4 tasks
andreas-wuestefeld added a commit that referenced this pull request May 23, 2026
Added STALTA, FBE, and Kurtosis transforms

This works now also the with latest upgrade of the "rolling" function to allow for arguments being passed 
#689

numba implementation to speed up significantly


---------

Co-authored-by: Derrick Chambers <d-chambers@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

proc Related to processing module ready_for_review PR is ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants