Skip to content

cv

Wrap cloud-volume mesh and skeleton sources.

PARAMETER DESCRIPTION
func
        Function/method to wrap.

TYPE: callable

only_on_kwarg
        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)

TYPE: bool DEFAULT: False

Source code in navis/utils/cv.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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