Skip to content
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

array spool indexing #491

Merged
merged 2 commits into from
Feb 28, 2025
Merged

array spool indexing #491

merged 2 commits into from
Feb 28, 2025

Conversation

d-chambers
Copy link
Contributor

@d-chambers d-chambers commented Feb 27, 2025

Description

This PR enables indexing a spool with a numpy array, which is the same behavior as numpy arrays. For example, boolean arrays can be used to "deselect" patches like so:

import dascore as dc
import numpy as np

spool = dc.get_example_spool()

# Get bool array, true values indicate patch is kept, false is discarded.
bool_array = np.ones(len(spool), dtype=np.bool_)
bool_array[1] = False

# Remove patch at position 1 from spool.
new = spool[bool_array]

and integer arrays can be used to re-arrange patches:

import dascore as dc
import numpy as np

spool = dc.get_example_spool()

# Get an array of integers which indicate the new order or patches  
bool_array = np.array([2, 0])

# create a new spool with patch 2 and patch 0 in that order.
new = spool[bool_array]

Checklist

I have (if applicable):

  • referenced the GitHub issue this PR closes.
  • documented the new feature with docstrings or appropriate doc page.
  • included a test. See testing guidelines.
  • your name has been added to the contributors page (docs/contributors.md).
  • added the "ready_for_review" tag once the PR is ready to be reviewed.

Summary by CodeRabbit

  • New Features
    • Enhanced data selection now supports flexible indexing using slices, boolean, and integer arrays, allowing for more versatile retrieval and rearrangement of data.
  • Documentation
    • Updated tutorials now include clear examples demonstrating how to select and reorder data using these new indexing methods.
  • Tests
    • Introduced new test classes to validate the functionality of selecting patches using boolean and integer arrays, improving test coverage for these features.

@d-chambers d-chambers added the spool related to Spool class label Feb 27, 2025
Copy link
Contributor

coderabbitai bot commented Feb 27, 2025

Walkthrough

This update extends the data selection capabilities in the spool handling classes. The __getitem__ method in the BaseSpool now accepts int, slice, and np.ndarray inputs. A new private method, _select_from_array, in the DataFrameSpool class processes array-based indexing and selection, constructing new spool instances from the selected dataframe portions. Updated documentation and tests provide examples and verify correct behavior for boolean and integer array selections.

Changes

File Change Summary
dascore/.../spool.py Updated __getitem__ in BaseSpool to accept `int
docs/.../spool.qmd Added examples demonstrating how to use boolean and integer arrays for selecting and rearranging patches within a spool.
tests/.../test_spool.py Introduced TestSpoolBoolArraySelect and TestSpoolIntArraySelect classes with tests covering boolean and integer array selections, including edge cases and error conditions.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant B as BaseSpool
    participant D as DataFrameSpool

    U->>B: Call __getitem__(item)
    alt item is np.ndarray
        B->>B: Check is_array(item)
        B->>D: Call _select_from_array(item)
        D-->>B: Return new spool instance
    else item is int/slice
        B->>B: Process simple index/slice
    end
    B-->>U: Return selected spool
Loading

Poem

In a meadow of code so deep,
I hopped to the spool without a peep.
Arrays now dance in every line,
Boolean and integer selections align.
With tests and docs, our spool sings—
Hoppin' to new features on spring wings!
🐇💻🌼


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 30778d0 and 02ce11b.

📒 Files selected for processing (1)
  • docs/tutorial/spool.qmd (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/tutorial/spool.qmd
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: test_code_min_deps (windows-latest, 3.12)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.13)
  • GitHub Check: test_code_min_deps (macos-latest, 3.13)
  • GitHub Check: test_code_min_deps (windows-latest, 3.13)
  • GitHub Check: test_code_min_deps (macos-latest, 3.12)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.12)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
docs/tutorial/spool.qmd (1)

104-133: Great addition of numpy-style array indexing examples!

The examples effectively demonstrate how to use boolean and integer arrays with spools, aligning the spool behavior with numpy's interface. This will be helpful for users familiar with numpy.

However, there's a small inconsistency in the variable naming in the integer array example:

-# Get an array of integers which indicate the new order or patches  
-bool_array = np.array([2, 0])
+# Get an array of integers which indicate the new order or patches  
+int_array = np.array([2, 0])

-# create a new spool with patch 2 and patch 0 in that order.
-new = spool[bool_array]
+# create a new spool with patch 2 and patch 0 in that order.
+new = spool[int_array]
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1dbb10e and 30778d0.

📒 Files selected for processing (3)
  • dascore/core/spool.py (4 hunks)
  • docs/tutorial/spool.qmd (1 hunks)
  • tests/test_core/test_spool.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (15)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.13)
  • GitHub Check: test_code_min_deps (windows-latest, 3.12)
  • GitHub Check: test_code_min_deps (macos-latest, 3.13)
  • GitHub Check: test_code_min_deps (windows-latest, 3.13)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.12)
  • GitHub Check: test_code_min_deps (macos-latest, 3.12)
  • GitHub Check: test_code (windows-latest, 3.11)
  • GitHub Check: test_code (ubuntu-latest, 3.12)
  • GitHub Check: test_code (macos-latest, 3.11)
  • GitHub Check: test_code (windows-latest, 3.10)
  • GitHub Check: test_code (macos-latest, 3.10)
  • GitHub Check: test_code (macos-latest, 3.12)
  • GitHub Check: test_code (windows-latest, 3.12)
  • GitHub Check: test_code (ubuntu-latest, 3.11)
  • GitHub Check: test_code (ubuntu-latest, 3.10)
🔇 Additional comments (5)
dascore/core/spool.py (3)

60-60: Improved type hint for __getitem__ method.

The updated type hint properly documents that the method can handle slices and numpy arrays in addition to integers.


380-399: Well-implemented array selection logic.

The implementation handles both boolean and integer arrays appropriately:

  1. Boolean arrays filter patches directly with the boolean mask
  2. Integer arrays select specific patches by index position
  3. The method correctly raises an error for unsupported array types

The method also preserves the source dataframe and instructions, maintaining consistency with the rest of the codebase.


412-413: Clean integration of array selection.

The addition to __getitem__ effectively integrates the array selection functionality while maintaining compatibility with existing indexing behavior.

tests/test_core/test_spool.py (2)

182-206: Comprehensive testing for boolean array selection.

The test class thoroughly covers boolean array selection with tests for:

  • All true values (maintaining the original spool)
  • All false values (resulting in an empty spool)
  • Mixed boolean values (correctly filtering specific patches)

The tests verify both the length of the resulting spool and the content equality with the expected dataframe, providing robust validation.


208-236: Well-designed tests for integer array selection.

The test class efficiently covers various integer array scenarios:

  • Standard sequential ordering
  • Error handling for out-of-bounds indices
  • Error handling for incorrect array types
  • Reordering of patches based on custom indices

The tests cover both normal usage and edge cases, ensuring reliability of the implementation.

Copy link

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.85%. Comparing base (1dbb10e) to head (02ce11b).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #491   +/-   ##
=======================================
  Coverage   99.85%   99.85%           
=======================================
  Files         118      118           
  Lines        9696     9711   +15     
=======================================
+ Hits         9682     9697   +15     
  Misses         14       14           
Flag Coverage Δ
unittests 99.85% <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.

Copy link
Collaborator

@ahmadtourei ahmadtourei left a comment

Choose a reason for hiding this comment

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

Looks great!

@d-chambers d-chambers merged commit f9f8289 into master Feb 28, 2025
19 checks passed
@d-chambers d-chambers deleted the boolean_select branch February 28, 2025 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spool related to Spool class
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants