Skip to content

voxel_data_adapters

oxytcmri.interface.mri.voxel_data_adapters

NIfTI adapter implementations.

Classes:

Name Description
InMemoryNumpyVoxelData

Voxel data stored in memory as a numpy array.

NiftiVoxelData

Implementation of VoxelData for NIfTI files.

Attributes:

Name Type Description
T

T = TypeVar('T') module-attribute

InMemoryNumpyVoxelData(data=None, voxel_volume=None)

Bases: VoxelData[T]

Voxel data stored in memory as a numpy array.

Initialize the InMemoryNumpyVoxelData object.

Parameters:

Name Type Description Default
data ndarray

Numpy array containing voxel data.

None

Methods:

Name Description
get_value_at

Get the value at the specified coordinates.

get_dimensions

Get the dimensions of the voxel data.

get_voxel_volume_in_ml

Get the volume of a voxel in milliliters.

filter_values

Create a boolean representation of voxel data based on a filtering condition.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
17
18
19
20
21
22
23
24
25
26
def __init__(self, data: np.ndarray = None, voxel_volume: float = None):
    """Initialize the InMemoryNumpyVoxelData object.

    Parameters
    ----------
    data : np.ndarray, optional
        Numpy array containing voxel data.
    """
    self._data = data
    self._voxel_volume = voxel_volume

get_value_at(x, y, z)

Get the value at the specified coordinates.

Parameters:

Name Type Description Default
x int

X coordinate.

required
y int

Y coordinate.

required
z int

Z coordinate.

required

Returns:

Type Description
T

Value at the specified coordinates.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def get_value_at(self, x: int, y: int, z: int) -> T:
    """Get the value at the specified coordinates.

    Parameters
    ----------
    x : int
        X coordinate.
    y : int
        Y coordinate.
    z : int
        Z coordinate.

    Returns
    -------
    T
        Value at the specified coordinates.
    """
    return self._data[x, y, z]

get_dimensions()

Get the dimensions of the voxel data.

Returns:

Type Description
Tuple[int, int, int]

Dimensions of the voxel data (x, y, z).

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
47
48
49
50
51
52
53
54
55
def get_dimensions(self) -> Tuple[int, int, int]:
    """Get the dimensions of the voxel data.

    Returns
    -------
    Tuple[int, int, int]
        Dimensions of the voxel data (x, y, z).
    """
    return self._data.shape

get_voxel_volume_in_ml()

Get the volume of a voxel in milliliters.

Returns:

Type Description
float

Volume of a voxel in milliliters.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
57
58
59
60
61
62
63
64
65
def get_voxel_volume_in_ml(self) -> float:
    """Get the volume of a voxel in milliliters.

    Returns
    -------
    float
        Volume of a voxel in milliliters.
    """
    return self._voxel_volume

filter_values(condition)

Create a boolean representation of voxel data based on a filtering condition.

Parameters:

Name Type Description Default
condition Callable[[T], bool]

Function that takes a voxel value and returns True if the voxel should be included in the filter

required

Returns:

Type Description
VoxelData[bool]

A boolean representation where voxels are True if they match the condition

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def filter_values(self, condition: Callable[[T], bool]) -> VoxelData[bool]:
    """Create a boolean representation of voxel data based on a filtering condition.

    Parameters
    ----------
    condition : Callable[[T], bool]
        Function that takes a voxel value and returns True if the voxel
        should be included in the filter

    Returns
    -------
    VoxelData[bool]
        A boolean representation where voxels are True if they match the condition
    """
    return InMemoryNumpyVoxelData(self._data[condition(self._data)], self._voxel_volume)

NiftiVoxelData(nifti_path)

Bases: VoxelData[T]

Implementation of VoxelData for NIfTI files.

Parameters:

Name Type Description Default
nifti_path Path

Path to the NIfTI file.

required

Initialize the NiftiVoxelData object.

Parameters:

Name Type Description Default
nifti_path Path

Path to the NIfTI file.

required

Methods:

Name Description
get_nifti_image

Get the NIfTI image object.

get_data

Get the voxel data as a numpy array.

get_nifti_path_string

Get the path to the NIfTI file as a string.

get_value_at

Get the value at the specified coordinates.

get_dimensions

Get the dimensions of the voxel data.

get_voxel_volume_in_ml

Get the volume of a single voxel in milliliters (mL).

filter_values

Filter the voxel data based on a condition.

Attributes:

Name Type Description
nifti_path
Source code in oxytcmri/interface/mri/voxel_data_adapters.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
def __init__(self, nifti_path: Path):
    """Initialize the NiftiVoxelData object.

    Parameters
    ----------
    nifti_path : Path
        Path to the NIfTI file.
    """
    self.nifti_path = nifti_path
    self._data = None

nifti_path = nifti_path instance-attribute

get_nifti_image()

Get the NIfTI image object.

Returns:

Type Description
Nifti1Image

NIfTI image object.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
104
105
106
107
108
109
110
111
112
def get_nifti_image(self) -> FileBasedImage:
    """Get the NIfTI image object.

    Returns
    -------
    nib.Nifti1Image
        NIfTI image object.
    """
    return nib.load(self.nifti_path)

get_data()

Get the voxel data as a numpy array.

Returns:

Type Description
ndarray

Voxel data as a numpy array.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
114
115
116
117
118
119
120
121
122
123
124
125
def get_data(self) -> np.ndarray:
    """Get the voxel data as a numpy array.

    Returns
    -------
    np.ndarray
        Voxel data as a numpy array.
    """
    if self._data is None:
        # Load the data from the NIfTI file
        self._data = self.get_nifti_image().get_fdata()
    return self._data

get_nifti_path_string()

Get the path to the NIfTI file as a string.

Returns:

Type Description
str

Path to the NIfTI file.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
127
128
129
130
131
132
133
134
135
def get_nifti_path_string(self) -> str:
    """Get the path to the NIfTI file as a string.

    Returns
    -------
    str
        Path to the NIfTI file.
    """
    return str(self.nifti_path)

get_value_at(x, y, z)

Get the value at the specified coordinates.

Parameters:

Name Type Description Default
x int

X coordinate.

required
y int

Y coordinate.

required
z int

Z coordinate.

required

Returns:

Type Description
T

Value at the specified coordinates.

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def get_value_at(self, x: int, y: int, z: int) -> T:
    """Get the value at the specified coordinates.

    Parameters
    ----------
    x : int
        X coordinate.
    y : int
        Y coordinate.
    z : int
        Z coordinate.

    Returns
    -------
    T
        Value at the specified coordinates.
    """
    # Check if the coordinates are within bounds
    dimensions = self.get_dimensions()
    if 0 <= x < dimensions[0] and 0 <= y < dimensions[1] and 0 <= z < dimensions[2]:
        return cast(T, self.get_data()[x, y, z])
    else:
        raise ValueError(
            f"Coordinates ({x}, {y}, {z}) are out of bounds. Shape is {dimensions}"
        )

get_dimensions()

Get the dimensions of the voxel data.

Returns:

Type Description
Tuple[int, int, int]

Dimensions of the voxel data (x, y, z).

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def get_dimensions(self) -> Tuple[int, int, int]:
    """Get the dimensions of the voxel data.

    Returns
    -------
    Tuple[int, int, int]
        Dimensions of the voxel data (x, y, z).
    """
    # Get the shape of the data array
    shape = self.get_data().shape

    # Return the first three dimensions (x, y, z)
    # Some NIfTI files might have a 4th dimension (time), which we ignore here
    return shape[0], shape[1], shape[2]

get_voxel_volume_in_ml()

Get the volume of a single voxel in milliliters (mL).

1 mL = 1000 mm³

Returns:

Type Description
float

Volume of a single voxel in milliliters (mL).

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def get_voxel_volume_in_ml(self) -> float:
    """Get the volume of a single voxel in milliliters (mL).

    1 mL = 1000 mm³

    Returns
    -------
    float
        Volume of a single voxel in milliliters (mL).
    """
    # Get the spatial dimensions of each voxel in mm
    # nibabel stores this information in the NIfTI file header
    # The header contains "zooms" which are the physical dimensions of voxels
    zooms = self.get_nifti_image().header.get_zooms()

    # Calculate the volume by multiplying the 3 dimensions
    # We only consider the first 3 dimensions (x, y, z)
    # because some files might have a 4th dimension (time)
    # Convert from mm³ to mL (division by 1000)
    volume = (zooms[0] * zooms[1] * zooms[2]) / 1000

    return float(volume)

filter_values(condition)

Filter the voxel data based on a condition.

Parameters:

Name Type Description Default
condition Callable[[T], bool]

A function that takes a voxel value and returns True if the voxel should be included in the filter.

required

Returns:

Type Description
VoxelData[bool]

A boolean representation where voxels are True if they match the condition

Source code in oxytcmri/interface/mri/voxel_data_adapters.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def filter_values(self, condition: Callable[[T], bool]) -> VoxelData[bool]:
    """
    Filter the voxel data based on a condition.

    Parameters
    ----------
    condition : Callable[[T], bool]
        A function that takes a voxel value and returns True if the voxel
        should be included in the filter.

    Returns
    -------
    VoxelData[bool]
        A boolean representation where voxels are True if they match the condition
    """
    # Create a boolean numpy array based on the condition applied to the data
    vectorized_condition = np.vectorize(condition)
    numpy_array_bool = vectorized_condition(self.get_data())

    # Return a new InMemoryNumpyVoxelData object with the filtered data
    return InMemoryNumpyVoxelData(numpy_array_bool, self.get_voxel_volume_in_ml())