Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 19 additions & 51 deletions pyclesperanto_prototype/_tier0/_plugin_function.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import inspect
from typing import Any, Callable, Dict, Optional, Sequence, Set, Type, Union
from typing import Callable
from functools import wraps
from toolz import curry

from ._pycl import OCLArray

from ._create import create_like
from ._types import Image, is_image
Expand All @@ -14,8 +13,8 @@
def plugin_function(
function: Callable,
output_creator: Callable = create_like,
categories : list = None,
priority : int = 0
categories: list = None,
priority: int = 0,
) -> Callable:
"""Function decorator to ensure correct types and values of all parameters.

Expand Down Expand Up @@ -51,54 +50,23 @@ def plugin_function(

@wraps(function)
def worker_function(*args, **kwargs):
# determine argument spec and default values, values are given as args
argument_specification = inspect.getfullargspec(function)

any_ocl_input = None

for arg_counter, argument in enumerate(argument_specification.args):
#print("---\nparsing argument " + argument)
if arg_counter < len(args):
value = args[arg_counter]
else:
value = None

sig = inspect.signature(function)
# create mapping from position and keyword arguments to parameters
# will raise a TypeError if the provided arguments do not match the signature
# https://docs.python.org/3/library/inspect.html#inspect.Signature.bind
bound = sig.bind(*args, **kwargs)
# set default values for missing arguments
# https://docs.python.org/3/library/inspect.html#inspect.BoundArguments.apply_defaults
bound.apply_defaults()

# copy images to GPU, and create output array if necessary
for key, value in bound.arguments.items():
if is_image(value):
value = push(value)
# value is now for sure OpenCL, we keep it in case we have to
# create another one of the same size
any_ocl_input = value

# default: keep value
if value is not None:
kwargs[argument] = value

# accumulate args in a list in case only kwargs were passed
args_list = []
for argument in enumerate(argument_specification.args):
try:
value = kwargs[argument[1]]
args_list = args_list + [value]
except KeyError:
break

# go through all arguments again and check if an image wasn't set,
# in which case we create one.
for argument in argument_specification.args:
# was the argument annotated?
type_annotation = argument_specification.annotations.get(argument);
if argument not in kwargs:
if type_annotation is Image:
# if not set and should be an image, create an image
# create a new output image with specified/default creator
kwargs[argument] = output_creator(*args_list)

#print("Got arguments")
#print(args)
#print("Will pass arguments")
#print(kwargs)
bound.arguments[key] = push(value)
if sig.parameters[key].annotation is Image and value is None:
bound.arguments[key] = output_creator(*bound.args[:len(args)])

# execute function with determined arguments
return function(**kwargs)
# call the decorated function
return function(*bound.args, **bound.kwargs)

return worker_function
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .._tier0 import Image

@plugin_function
def maximum_of_touching_neighbors(values : Image, touch_matrix : Image, maximum_values_destination : Image):
def maximum_of_touching_neighbors(values : Image, touch_matrix : Image, maximum_values_destination : Image = None):
"""Takes a touch matrix and a vector of values to determine the maximum
value among touching neighbors for every object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .._tier0 import Image

@plugin_function
def mean_of_touching_neighbors(values : Image, touch_matrix : Image, mean_values_destination : Image):
def mean_of_touching_neighbors(values : Image, touch_matrix : Image, mean_values_destination : Image = None):
"""Takes a touch matrix and a vector of values to determine the mean value
among touching neighbors for every object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .._tier0 import Image

@plugin_function
def median_of_touching_neighbors(values : Image, touch_matrix : Image, median_values_destination : Image):
def median_of_touching_neighbors(values : Image, touch_matrix : Image, median_values_destination : Image = None):
"""Takes a touch matrix and a vector of values to determine the median
value among touching neighbors for every object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .._tier0 import Image

@plugin_function
def minimum_of_touching_neighbors(values : Image, touch_matrix : Image, minimum_values_destination : Image):
def minimum_of_touching_neighbors(values : Image, touch_matrix : Image, minimum_values_destination : Image = None):
"""Takes a touch matrix and a vector of values to determine the minimum
value among touching neighbors for every object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .._tier0 import Image

@plugin_function
def mode_of_touching_neighbors(src_values : Image, touch_matrix : Image, dst_values : Image):
def mode_of_touching_neighbors(src_values : Image, touch_matrix : Image, dst_values : Image = None):
"""

Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .._tier0 import Image

@plugin_function
def standard_deviation_of_touching_neighbors(values : Image, touch_matrix : Image, standard_deviation_values_destination : Image):
def standard_deviation_of_touching_neighbors(values : Image, touch_matrix : Image, standard_deviation_values_destination : Image = None):
"""Takes a touch matrix and a vector of values to determine the standard
deviation value among touching neighbors for every object.

Expand Down