def return_navis(func, only_on_kwarg=False):
"""Wrap cloud-volume mesh and skeleton sources.
Parameters
----------
func : callable
Function/method to wrap.
only_on_kwarg : bool
If True, will look for a `as_navis=True` (default=False)
keyword argument to determine if results should be converted
to navis neurons. If 'process' is set to False, the neuron
will not be processed by TriMesh (remove nan, duplicate vertices,etc)
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
ret_navis = kwargs.pop("as_navis", False)
process = kwargs.pop("process", False)
res = func(*args, **kwargs)
if not only_on_kwarg or ret_navis:
neurons = []
if isinstance(res, (list, tuple)):
res = {getattr(n, "id", uuid.uuid4()): n for n in res}
if isinstance(res, (cv.Mesh, cv.Skeleton)):
res = {getattr(res, "id", uuid.uuid4()): res}
for k, v in res.items():
if isinstance(v, cv.Mesh):
n = core.MeshNeuron(v, id=k, units="nm", process=process)
neurons.append(n)
elif isinstance(v, cv.Skeleton):
swc_str = v.to_swc()
n = io.read_swc(swc_str)
n.id = k
n.units = "nm"
neurons.append(n)
else:
logger.warning(
f"Skipped {k}: Unable to convert {type(v)} to " "navis Neuron."
)
return core.NeuronList(neurons)
return res
return wrapper