Skip to content

nrrd_io

Source code in navis/io/nrrd_io.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 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
class NrrdReader(base.ImageReader):
    def __init__(
        self,
        output: Literal["voxels", "dotprops", "raw"] = "voxels",
        threshold: Optional[Union[int, float]] = None,
        thin: bool = False,
        dotprop_kwargs: Dict[str, Any] = {},
        fmt: str = DEFAULT_FMT,
        attrs: Optional[Dict[str, Any]] = None,
        errors: str = "raise",
    ):
        if not fmt.endswith(".nrrd"):
            raise ValueError('`fmt` must end with ".nrrd"')

        super().__init__(
            fmt=fmt,
            attrs=attrs,
            file_ext=".nrrd",
            name_fallback="NRRD",
            read_binary=True,
            output=output,
            threshold=threshold,
            thin=thin,
            dotprop_kwargs=dotprop_kwargs,
            errors=errors,
        )

    def format_output(self, x):
        # This function replaces the BaseReader.format_output()
        # This is to avoid trying to convert multiple (image, header) to NeuronList
        if self.output == "raw":
            return [n for n in x if n]
        elif x:
            return core.NeuronList([n for n in x if n])
        else:
            return core.NeuronList([])

    @base.handle_errors
    def read_buffer(
        self, f, attrs: Optional[Dict[str, Any]] = None
    ) -> Union[np.ndarray, "core.Dotprops", "core.VoxelNeuron"]:
        """Read buffer into (image, header) or a neuron.

        Parameters
        ----------
        f :         IO
                    Readable buffer (must be bytes).
        attrs :     dict | None
                    Arbitrary attributes to include in the neuron.

        Returns
        -------
        core.Dotprops | core.VoxelNeuron | np.ndarray

        """
        if isinstance(f, HTTPResponse):
            f = io.StringIO(f.content)

        if isinstance(f, bytes):
            f = io.BytesIO(f)

        header = nrrd.read_header(f)
        data = nrrd.read_data(header, f)

        if self.output == "raw":
            return data, header

        # Try parsing units - this is modelled after the nrrd files you get from
        # Virtual Fly Brain (VFB)
        units = None
        space_units = None
        voxdim = np.array([1, 1, 1])
        if "space directions" in header:
            sd = np.asarray(header["space directions"])
            if sd.ndim == 2:
                voxdim = np.diag(sd)[:3]
        if "space units" in header:
            space_units = header["space units"]
            if len(space_units) == 3:
                units = [f"{m} {u}" for m, u in zip(voxdim, space_units)]
        else:
            units = voxdim

        return self.convert_image(data, attrs, header, voxdim, units, space_units)

Read buffer into (image, header) or a neuron.

PARAMETER DESCRIPTION
f
    Readable buffer (must be bytes).

TYPE: IO

attrs
    Arbitrary attributes to include in the neuron.

TYPE: dict | None DEFAULT: None

RETURNS DESCRIPTION
core.Dotprops | core.VoxelNeuron | np.ndarray
Source code in navis/io/nrrd_io.py
 70
 71
 72
 73
 74
 75
 76
 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
@base.handle_errors
def read_buffer(
    self, f, attrs: Optional[Dict[str, Any]] = None
) -> Union[np.ndarray, "core.Dotprops", "core.VoxelNeuron"]:
    """Read buffer into (image, header) or a neuron.

    Parameters
    ----------
    f :         IO
                Readable buffer (must be bytes).
    attrs :     dict | None
                Arbitrary attributes to include in the neuron.

    Returns
    -------
    core.Dotprops | core.VoxelNeuron | np.ndarray

    """
    if isinstance(f, HTTPResponse):
        f = io.StringIO(f.content)

    if isinstance(f, bytes):
        f = io.BytesIO(f)

    header = nrrd.read_header(f)
    data = nrrd.read_data(header, f)

    if self.output == "raw":
        return data, header

    # Try parsing units - this is modelled after the nrrd files you get from
    # Virtual Fly Brain (VFB)
    units = None
    space_units = None
    voxdim = np.array([1, 1, 1])
    if "space directions" in header:
        sd = np.asarray(header["space directions"])
        if sd.ndim == 2:
            voxdim = np.diag(sd)[:3]
    if "space units" in header:
        space_units = header["space units"]
        if len(space_units) == 3:
            units = [f"{m} {u}" for m, u in zip(voxdim, space_units)]
    else:
        units = voxdim

    return self.convert_image(data, attrs, header, voxdim, units, space_units)