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

improve(detector): remove &self from OutlierDetector::preprocess #251

Conversation

shenxiangzhuang
Copy link

@shenxiangzhuang shenxiangzhuang commented Feb 14, 2025

Fix #242

Summary by CodeRabbit

  • Refactor
    • Updated outlier detection processing to use static (class-level) methods, eliminating the need for instance-based calls.
    • Streamlined preprocessing and data analysis routines for improved clarity and usability.
  • Tests
    • Updated test cases to reflect the new static method invocation approach for DbscanDetector and MADDetector.

@shenxiangzhuang
Copy link
Author

Hi @sd2k , I'm trying to improve the api in #242 and I'm not sure the changes in mad.rs is your desired implementation of not. If it's not, I'd like to change according to your suggestions.

Copy link
Contributor

coderabbitai bot commented Feb 14, 2025

Warning

Rate limit exceeded

@shenxiangzhuang has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 2 minutes and 16 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between bd021d8 and d59ee4f.

📒 Files selected for processing (1)
  • js/augurs-outlier-js/src/lib.rs (2 hunks)

Walkthrough

The pull request updates the method signatures for the preprocess function and related processing functions across multiple detector implementations. The changes remove the &self parameter, converting instance methods into static methods. This adjustment applies to DbscanDetector and DummyDetector via the OutlierDetector trait, as well as to MADDetector (including its calculate_mad method). Corresponding test cases have been updated to invoke these methods statically, while the core functionality remains unaffected.

Changes

File(s) Change Summary
crates/.../dbscan.rs, crates/.../lib.rs Updated preprocess in the OutlierDetector trait and its implementations (DbscanDetector & DummyDetector): removed &self to enable static calls.
crates/.../mad.rs Refactored MADDetector: integrated preprocess_impl into a static preprocess, removed &self from calculate_mad, and streamlined error handling.
crates/.../tests/integration.rs Modified integration tests for DbscanDetector and MADDetector to call preprocess statically instead of on instances.
examples/.../dbscan_outlier_detection.rs Changed preprocess call from instance to static for DbscanDetector.
examples/.../mad_outlier_detection.rs Changed preprocess call from instance to static for MADDetector.

Sequence Diagram(s)

sequenceDiagram
    participant T as Test
    participant D as DbscanDetector
    T->>D: Call preprocess(data)
    D-->>T: Return processed result
Loading
sequenceDiagram
    participant T as Test
    participant M as MADDetector
    T->>M: Call preprocess(data)
    Note over M: Compute medians and MAD
    M-->>T: Return MAD result
Loading

Possibly related PRs

  • fix!: add serde derives for more types #112: The changes in the main PR regarding the preprocess method's signature in the DbscanDetector struct are related to similar modifications made to the preprocess method in the DummyDetector struct within the retrieved PR, as both involve changing instance methods to static methods.

Poem

I'm a rabbit on a coding spree,
Hoping through changes wild and free.
No more self—static calls lead the way,
In our detectors the methods now play.
Hop along with tests so spry and keen,
Where code dances in a joyful scene!


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. (Beta)
  • @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)
crates/augurs-outlier/src/lib.rs (1)

226-226: LGTM! Good architectural improvement.

Making preprocess a static method is a better design choice since preprocessing is a pure function that doesn't require instance state. This change improves API ergonomics by allowing preprocessing without constructing a detector instance.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6077c79 and 0723051.

📒 Files selected for processing (3)
  • crates/augurs-outlier/src/dbscan.rs (6 hunks)
  • crates/augurs-outlier/src/lib.rs (2 hunks)
  • crates/augurs-outlier/src/mad.rs (3 hunks)
🔇 Additional comments (3)
crates/augurs-outlier/src/mad.rs (1)

243-250: LGTM! Clean implementation of static preprocessing.

The static implementation correctly maintains all functionality while removing unnecessary instance dependency. The use of Self:: for static method calls follows Rust best practices.

crates/augurs-outlier/src/dbscan.rs (2)

55-57: LGTM! Consistent implementation of static preprocessing.

The implementation correctly follows the trait's new static design while maintaining its simple data transformation logic.


561-561: LGTM! Thorough test updates.

All test cases have been consistently updated to use the new static method call pattern.

Also applies to: 597-597, 633-633, 651-651, 660-660

Copy link
Collaborator

@sd2k sd2k left a comment

Choose a reason for hiding this comment

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

This is exactly what I had in mind, thanks @shenxiangzhuang! Just one comment on the MAD implementation.

Comment on lines +244 to +245
let medians = Self::calculate_double_medians(y)
.map_err(|x| PreprocessingError::from(Box::new(x) as Box<dyn std::error::Error>))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should use self.medians if it's not None (meaning it's been preset by the user using set_medians), which is what made preprocess_impl a bit more complicated 🙂

Copy link
Author

Choose a reason for hiding this comment

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

Hmm...I'm a little confused, how could we access self.medians without the &self in fn preprocess(y: &[&[f64]])?

Copy link
Collaborator

Choose a reason for hiding this comment

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

🤦‍♂️ good point, I missed that. That's probably why I made it a method rather than an associated function originally 😞

Setting medians is a pretty niche use case but I need to think about whether it's something we should continue to support. I'll have a think and reply soon.

Sorry about this, I missed it completely.

@sd2k
Copy link
Collaborator

sd2k commented Feb 17, 2025

I'd hoped this would be possible some other way but the more I look at it the more I think we need to keep the current API for flexibility 😞 for example, we have a use case in MAD which pre-calculates the medians for the last 24 hours and uses those when preprocessing even more recent data, to ensure we're detecting long-term outliers rather than just recent ones.

I do think it would be nice to add methods such as set_sensitivity, set_threshold, set_epsilon to the detectors so that the case in the original issue (#242) becomes a bit less unpleasant; we'd be able to reuse the same detector and just mutate it in each iteration of the loop.

Again, sorry I missed this @shenxiangzhuang, and thanks for the contribution! Happy to help/review if you would like to work on the set_XYZ methods mentioned above 🙏

@sd2k sd2k closed this Feb 17, 2025
@shenxiangzhuang
Copy link
Author

Never mind! I have learned so much from this project. And sure, I will continue to try to contribute to Augurs in the future!

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.

outlier: remove &self argument from OutlierDetector::preprocess
2 participants