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

__call__ to direct on type #381

Merged
merged 3 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pyterrier/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ def set_parameter(self, name : str, value):
raise ValueError(('Invalid parameter name %s for transformer %s. '+
'Check the list of available parameters') %(name, str(self)))

def __call__(self, *args, **kwargs) -> pd.DataFrame:
def __call__(self, input : Union[pd.DataFrame, Iterable[dict]]) -> pd.DataFrame:
"""
Sets up a default method for every transformer, which is aliased to transform().
Sets up a default method for every transformer, which is aliased to transform() (for DataFrames)
or transform_iter() (for iterable dictionaries) depending on the type of input.
"""
return self.transform(*args, **kwargs)
if isinstance(input, pd.DataFrame):
return self.transform(input)
return self.transform_iter(input)

def __rshift__(self, right) -> 'Transformer':
from .ops import ComposedPipeline
Expand Down
8 changes: 8 additions & 0 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

class TestTransformer(BaseTestCase):

def test_call(self):
inputDocs = pt.new.ranked_documents([[2, 1], [2]], qid=["q100", "q10"])
t = pt.Transformer.from_df(inputDocs)
self.assertEqual(2, len(t(pt.new.queries(['a'], qid=['q100']))))
self.assertEqual(1, len(t(pt.new.queries(['a'], qid=['q10']))))
self.assertEqual(2, len(t([{'qid' : 'q100'}])))
self.assertEqual(1, len(t([{'qid' : 'q10'}])))

def test_is_transformer(self):
class MyTransformer1(pt.Transformer):
pass
Expand Down