-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add assertions for 1:1 mapping of rows for extractors #538
Open
zerismo
wants to merge
3
commits into
main
Choose a base branch
from
rk/DEV-4527
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,6 +450,63 @@ def is_user_defined(obj): | |
return inspect.isclass(type(obj)) and not inspect.isbuiltin(obj) | ||
|
||
|
||
|
||
def _add_input_constraints(func, params): | ||
""" | ||
Wraps the user's extractor with a function that: | ||
1. Checks that all input series have the same length. | ||
2. Runs the extractor. | ||
3. Asserts that the number of rows in the output matches the input length. | ||
The output can be either a pandas Series or DataFrame. | ||
""" | ||
|
||
@functools.wraps(func) | ||
def inner(*args, **kwargs): | ||
# Ensure we have the correct number of arguments | ||
assert len(args) == len(params) + 2, ( | ||
f"Expected {len(params) + 2} arguments, got {len(args)}" | ||
) | ||
|
||
args = list(args) | ||
input_series = [arg for arg in args[2:]] # Skip the first two fixed args | ||
|
||
# Measure length of input series | ||
input_lengths = [len(series) for series in input_series] | ||
|
||
# Check if all input series have the same length | ||
first_length = input_lengths[0] | ||
|
||
for i, series in enumerate(input_series): | ||
assert len(series) == first_length, ( | ||
f"Input length mismatch: expected {first_length}, got {len(series)} for input {i+1}" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is provided by us and you can assume that is correct |
||
renamed_args = args[:2] + [ | ||
arg.rename(name.fqn()) for name, arg in zip(params, input_series) | ||
] | ||
|
||
# Run the extractor | ||
ret = func(*renamed_args, **kwargs) | ||
|
||
# Ensure the output matches the input length | ||
if isinstance(ret, pd.Series) or isinstance(ret, pd.DataFrame): | ||
output_length = len(ret) | ||
else: | ||
raise ValueError( | ||
f"Expected a pandas Series or DataFrame but got {type(ret)} in {func.__qualname__}." | ||
) | ||
|
||
# Check that the output length matches the input length | ||
assert output_length == first_length, ( | ||
f"Output length mismatch in {func.__qualname__}: expected {first_length}, got {output_length}" | ||
) | ||
|
||
return ret | ||
|
||
return inner | ||
|
||
|
||
|
||
def _add_featureset_name(func, params): | ||
"""Rewrites the output column names of the extractor to be fully qualified names. | ||
Also add feature names to the input parameters of the extractor. | ||
|
@@ -773,6 +830,7 @@ def _get_extractors(self) -> List[Extractor]: | |
extractor.featureset = self._name | ||
extractor.inputs = inputs | ||
func = _add_featureset_name(extractor.func, extractor.inputs) | ||
func = _add_input_constraints(func, extractor.inputs) | ||
# Setting func at both extractor.func and class attribute | ||
extractor.func = func | ||
setattr(self.__fennel_original_cls__, name, classmethod(func)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also list out the differences in length