[WIP] feat: Add combined_logical_filter operator with AND/OR support#914
[WIP] feat: Add combined_logical_filter operator with AND/OR support#914
Conversation
- Add CombinedLogicalFilter operator for explicit filter composition - Support AND/OR logical operations between multiple filters - Enable OR operations that were impossible with sequential filters - Optimize performance: single batch stats computation, no intermediate data reduction - Extend FusedFilter to support logical_op parameter (AND/OR) - Add comprehensive tests covering AND, OR, edge cases, and error handling - Add detailed documentation with performance comparisons and use cases Key advantages: 1. OR operation support (previously impossible with sequential filters) 2. Performance optimization (reduced data reorganization, optimized stats computation) 3. Explicit logical relationships (self-documenting configuration) 4. Complementary to FusedFilter (automatic optimization + explicit control)
Summary of ChangesHello @yxdyc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a powerful new Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new CombinedLogicalFilter operator, which allows combining multiple filters using explicit AND/OR logic. This is a great feature, as it enables more complex filtering logic (like OR operations) that was not possible with simple sequential filters. The implementation is well-structured, with good documentation and comprehensive unit tests.
I've made a few suggestions:
- A potential bug fix in
compute_stats_singlefor handling batched-only sub-operators. - A refactoring suggestion for
FusedFilter.process_batchedto improve readability and fix a potential bug. - A couple of minor corrections in the new documentation file for clarity and consistency.
Overall, this is a high-quality contribution that significantly enhances the filtering capabilities of the framework.
| else: | ||
| # For batched-only operators, we cannot compute stats for | ||
| # a single sample. This is a limitation - batched-only | ||
| # operators should implement compute_stats_single or we need | ||
| # to create a minimal batch. For now, we skip stats | ||
| # computation for batched-only operators in single mode. | ||
| # This is acceptable because process_single will handle the | ||
| # fallback. | ||
| pass |
There was a problem hiding this comment.
In compute_stats_single, for batched-only sub-operators, you're currently skipping statistics computation (pass). This will likely cause process_single to fail later, as it calls op.process_batched which expects pre-computed statistics.
To fix this, you should compute stats for these operators by creating a minimal batch and calling their compute_stats_batched method, similar to the fallback logic in process_single.
| res = None | ||
| for op in self.fused_filters: | ||
| this_res = np.array(list(op.process_batched(samples))) | ||
| if res is not None: | ||
| res = np.logical_and(res, this_res) | ||
| if self.logical_op == "and": | ||
| res = np.logical_and(res, this_res) | ||
| else: # or | ||
| res = np.logical_or(res, this_res) | ||
| else: | ||
| res = this_res | ||
| return res |
There was a problem hiding this comment.
This implementation has two issues:
- It returns
Noneifself.fused_filtersis empty, which will cause aTypeErrorfor the caller expecting an iterable of booleans. - The
if res is not None:check inside the loop can be avoided by initializingreswith the result of the first filter before the loop.
Here is a suggested refactoring that addresses both points for improved correctness and readability.
| res = None | |
| for op in self.fused_filters: | |
| this_res = np.array(list(op.process_batched(samples))) | |
| if res is not None: | |
| res = np.logical_and(res, this_res) | |
| if self.logical_op == "and": | |
| res = np.logical_and(res, this_res) | |
| else: # or | |
| res = np.logical_or(res, this_res) | |
| else: | |
| res = this_res | |
| return res | |
| if not self.fused_filters: | |
| num_samples = len(samples[list(samples.keys())[0]]) | |
| return [True] * num_samples | |
| res = np.array(list(self.fused_filters[0].process_batched(samples))) | |
| op_func = np.logical_and if self.logical_op == "and" else np.logical_or | |
| for op in self.fused_filters[1:]: | |
| this_res = np.array(list(op.process_batched(samples))) | |
| res = op_func(res, this_res) | |
| return res |
| logical_op: 'or' # ✅ Now possible! | ||
| ``` | ||
| - ✅ **Explicit logic**: AND/OR relationship is clearly specified. | ||
| - ✅ **OR support**: Can now implement OR operations that sequential version impossible. |
There was a problem hiding this comment.
|
|
||
| **Why this was impossible before**: Sequential filters can only implement AND logic. The first filter would remove all samples outside its range, leaving nothing for the second filter to process. | ||
|
|
||
| **为什么Default Sequential Version 无法实现**:串联过滤器只能实现 AND 逻辑。第一个过滤器会移除其范围外的所有样本,第二个过滤器无法处理。 |
Summary:
TODO
[] Check and improve unit-test