playnano.utils.param_utils module¶
Mark which parameters of a processing or analysis function apply when.
The command line uses these conditions to skip questions and drop settings that do not apply, for example a hole size when holes are not filled.
- playnano.utils.param_utils.param_conditions(**conds: Callable[[dict], bool])[source]¶
Attach conditions saying when a function’s parameters apply.
- Parameters:
**conds (callable) – One per parameter name: a function taking the dict of parameters given so far and returning True if this parameter applies.
- Returns:
A decorator that stores
condson the function as_param_conditionsand returns the function unchanged.- Return type:
callable
See also
prune_kwargsRemove parameters that do not apply.
Examples
>>> from playnano.utils.param_utils import param_conditions >>> @param_conditions(hole_area=lambda p: p.get("fill_holes", False)) ... def mask_fn(frame, fill_holes=False, hole_area=None): ... return frame >>> sorted(mask_fn._param_conditions) ['hole_area']
- playnano.utils.param_utils.prune_kwargs(fn, kwargs)[source]¶
Remove parameters that are None or whose condition is not met.
- Parameters:
fn (callable) – Function the parameters are for, possibly decorated with
param_conditions().kwargs (dict) – Parameters. Not changed.
- Returns:
The parameters that are not None and whose condition, if any, returns True for
kwargs. A condition that raises an error counts as False.- Return type:
Examples
>>> from playnano.utils.param_utils import param_conditions, prune_kwargs >>> @param_conditions(hole_area=lambda p: p.get("fill_holes", False)) ... def mask_fn(frame, fill_holes=False, hole_area=None, sigma=None): ... return frame >>> prune_kwargs(mask_fn, {"fill_holes": False, "hole_area": 10, "sigma": None}) {'fill_holes': False}