Newer
Older
from types import FunctionType
from inspect import getfullargspec, getmembers, isfunction, isbuiltin
from extraction.core.functions import cell, trap
from extraction.core.functions.custom import localisation
from extraction.core.functions.distributors import trap_apply
from extraction.core.functions.math_utils import div0
Note that inspect.getmembers returns a list of function names and
functions, and inspect.getfullargspec returns a function's arguments.
"""Load functions from the cell module and return as a dict."""
return {
f[0]: f[1]
for f in getmembers(cell)
if isfunction(f[1])
and f[1].__module__.startswith("extraction.core.functions")
def load_custom_args() -> t.Tuple[
(t.Dict[str, t.Callable], t.Dict[str, t.List[str]])
]:
Load custom functions from the localisation module.
Return the functions and any additional arguments other
than cell_mask and trap_image as dictionaries.
funs = {
f[0]: f[1]
for f in getmembers(localisation)
if isfunction(f[1])
and f[1].__module__.startswith("extraction.core.functions")
k: getfullargspec(v).args[2:]
if set(["cell_mask", "trap_image"]).intersection(
getfullargspec(v).args
)
return (
{k: funs[k] for k in args.keys()},
{k: v for k, v in args.items() if v},
)
Create a dict of core functions for use on cell_masks.
# create dict of the core functions from cell.py - these functions apply to a single mask
# create a dict of functions that apply the core functions to an array of cell_masks
args = getfullargspec(f).args
if len(args) == 1:
return lambda m, _: trap_apply(f, m)
else:
return lambda m, img: trap_apply(f, m, img)
"""Load functions that are applied to an entire tile."""
TRAPFUNS = {
f[0]: f[1]
for f in getmembers(trap)
if isfunction(f[1])
and f[1].__module__.startswith("extraction.core.functions")
"""Combine all automatically loaded functions."""
CELLFUNS = load_cellfuns()
TRAPFUNS = load_trapfuns()
return CELLFUNS, TRAPFUNS, {**TRAPFUNS, **CELLFUNS}
def load_redfuns(
additional_reducers: t.Optional[
t.Union[t.Dict[str, t.Callable], t.Callable]
] = None,
) -> t.Dict[str, t.Callable]:
Load functions to reduce a multidimensional image by one dimension.
Parameters
----------
additional_reducers: function or a dict of functions (optional)
Functions to perform the reduction.
"max": bn.nanmax,
"mean": bn.nanmean,
"median": bn.nanmedian,
"div0": div0,
"add": bn.nansum,
if additional_reducers is not None:
if isinstance(additional_reducers, FunctionType):
additional_reducers = [
(additional_reducers.__name__, additional_reducers)
]
RED_FUNS.update(additional_reducers)