-
Notifications
You must be signed in to change notification settings - Fork 216
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
Added conditional property to expose time scale predictions #327
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b54fb98
Added conditional property to pipeline to expose time scale predictio…
Finesim97 7aa910c
Removed unnecessary else
Finesim97 cccdfd4
Merge branch 'sebp:master' into pipeline_scale_fix
Finesim97 3ec982d
Sorted imports
Finesim97 b23af44
Merge branch 'sebp:master' into pipeline_scale_fix
Finesim97 ccd891d
Added tests
Finesim97 6270039
Tried to fix Codacy issues
Finesim97 1e12758
Improved test ccv
Finesim97 c15832e
Fixed test
Finesim97 dac2c18
Removed setter for check
Finesim97 7a8e2f4
WS not noticed by tox locally removed
Finesim97 445454c
Merge branch 'master' into pipeline_scale_fix
Finesim97 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 |
---|---|---|
|
@@ -260,3 +260,83 @@ def safe_concat(objs, *args, **kwargs): | |
concatenated[name] = pd.Categorical(concatenated[name], **params) | ||
|
||
return concatenated | ||
|
||
|
||
class _ConditionalAvailableProperty: | ||
"""Implements a conditional property using the descriptor protocol based on the property decorator. | ||
|
||
The corresponding class in scikit-learn (_AvailableIfDescriptor) only supports callables. This | ||
class adopts the property decorator as presented by the descriptor guide in the offical documentation. | ||
|
||
The check is defined on the getter function. Setter, Deleter also need to be defined on the exposing class | ||
if they are supposed to be available. | ||
|
||
See Also | ||
-------- | ||
sklearn.utils.available_if._AvailableIfDescriptor: | ||
The original class in scikit-learn. | ||
""" | ||
|
||
def __init__(self, check, fget=None, fset=None, fdel=None, doc=None): | ||
self._check = check | ||
self.fget = fget | ||
self.fset = fset | ||
self.fdel = fdel | ||
if doc is None and fget is not None: | ||
doc = fget.__doc__ | ||
self.__doc__ = doc | ||
self._name = '' | ||
|
||
def _run_check(self, obj): | ||
attr_err = AttributeError( | ||
f"This {repr(obj)} has no attribute {repr(self._name)}" | ||
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. Instead of |
||
) | ||
if not self.check(obj): | ||
raise attr_err | ||
|
||
def __set_name__(self, owner, name): | ||
self._name = name | ||
|
||
def __get__(self, obj, objtype=None): | ||
if obj is None: | ||
return self | ||
self._run_check(obj) | ||
if self.fget is None: | ||
raise AttributeError(f"property '{self._name}' has no getter") | ||
return self.fget(obj) | ||
|
||
def __set__(self, obj, value): | ||
self._run_check(obj) | ||
if self.fset is None: | ||
raise AttributeError(f"property '{self._name}' has no setter") | ||
self.fset(obj, value) | ||
|
||
def __delete__(self, obj): | ||
self._run_check(obj) | ||
if self.fdel is None: | ||
raise AttributeError(f"property '{self._name}' has no deleter") | ||
self.fdel(obj) | ||
|
||
@property | ||
def check(self): | ||
return self._check | ||
|
||
def getter(self, fget): | ||
prop = type(self)(self.check, fget, self.fset, self.fdel, self.__doc__) | ||
prop._name = self._name | ||
return prop | ||
|
||
def setter(self, fset): | ||
prop = type(self)(self.check, self.fget, fset, self.fdel, self.__doc__) | ||
prop._name = self._name | ||
return prop | ||
|
||
def deleter(self, fdel): | ||
prop = type(self)(self.check, self.fget, self.fset, fdel, self.__doc__) | ||
prop._name = self._name | ||
return prop | ||
|
||
|
||
def conditionalAvailableProperty(check): | ||
prop = _ConditionalAvailableProperty(check=check) | ||
return prop.getter |
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
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.
I wonder if you could inherit from the built-in
property
, because it is a class. This could allow removing the setter/getter/deleter code.