@@ -519,7 +519,57 @@ def wrapper(f):
519519 return wrapper (func )
520520
521521
522- def _pipeline (func , retain_doc = False ):
522+ def _pipeline (func_or_class , ** kwargs ):
523+ try :
524+ is_class = issubclass (func_or_class , Pipeline )
525+ except TypeError :
526+ is_class = False
527+ if is_class :
528+ return _pipeline_fromclass (func_or_class , ** kwargs )
529+ else :
530+ return _pipeline_fromfunc (func_or_class , ** kwargs )
531+
532+
533+ def _pipeline_fromclass (cls , retain_doc = False ):
534+ """Actual `pipeline` implementation
535+
536+ Parameters
537+ ----------
538+ func : class
539+ Class for lazy evaluation
540+ retain_doc : bool
541+ If True, don't modify `func`'s doc string to say that it has been
542+ made lazy
543+
544+ Returns
545+ -------
546+ Pipeline
547+ Lazy function evaluation :py:class:`Pipeline` for `func`.
548+ """
549+ @wraps (cls )
550+ def process (obj , * args , ** kwargs ):
551+ if hasattr (obj , '_slicerator_flag' ) or isinstance (obj , Slicerator ) \
552+ or isinstance (obj , Pipeline ):
553+ return cls (obj , * args , ** kwargs )
554+ else :
555+ # Fall back on normal behavior of func, interpreting input
556+ # as a single image.
557+ return cls ([obj ], * args , ** kwargs )[0 ]
558+
559+ if not retain_doc :
560+ if process .__doc__ is None :
561+ process .__doc__ = ''
562+ process .__doc__ = ("This function has been made lazy. When passed\n "
563+ "a Slicerator, it will return a \n "
564+ "Pipeline of the results. When passed \n "
565+ "any other objects, its behavior is "
566+ "unchanged.\n \n " ) + process .__doc__
567+ process .__name__ = cls .__name__
568+ return process
569+
570+
571+
572+ def _pipeline_fromfunc (func , retain_doc = False ):
523573 """Actual `pipeline` implementation
524574
525575 Parameters
@@ -541,6 +591,7 @@ def process(obj, *args, **kwargs):
541591 or isinstance (obj , Pipeline ):
542592 def proc_func (x ):
543593 return func (x , * args , ** kwargs )
594+
544595 return Pipeline (obj , proc_func )
545596 else :
546597 # Fall back on normal behavior of func, interpreting input
0 commit comments