Skip to content

ROI

simnibs_reader.nifti.roi.ROI

E-field values extracted within a region of interest.

Attributes:

Name Type Description
values ndarray

1-D array of e-field values inside the ROI (raw or cleaned).

mask_img Nifti1Image

Binary mask used for extraction (on the e-field grid).

efield EField

Back-reference to the source e-field.

masked_img Nifti1Image or None

Volumetric image before outlier removal (set by postprocess()).

cleaned_img Nifti1Image or None

Volumetric image after outlier removal (set by postprocess()).

is_cleaned bool

True if this ROI went through postprocess().

Source code in simnibs_reader/nifti/roi.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
class ROI:
    """E-field values extracted within a region of interest.

    Attributes
    ----------
    values : np.ndarray
        1-D array of e-field values inside the ROI (raw or cleaned).
    mask_img : nib.Nifti1Image
        Binary mask used for extraction (on the e-field grid).
    efield : EField
        Back-reference to the source e-field.
    masked_img : nib.Nifti1Image or None
        Volumetric image before outlier removal (set by ``postprocess()``).
    cleaned_img : nib.Nifti1Image or None
        Volumetric image after outlier removal (set by ``postprocess()``).
    is_cleaned : bool
        ``True`` if this ROI went through ``postprocess()``.
    """

    def __init__(
        self,
        values: np.ndarray,
        mask_img: nib.Nifti1Image,
        efield: EField,
        *,
        masked_img: nib.Nifti1Image | None = None,
        cleaned_img: nib.Nifti1Image | None = None,
        is_cleaned: bool = False,
    ) -> None:
        self.values      = values
        self.mask_img    = mask_img
        self.efield      = efield
        self.masked_img  = masked_img
        self.cleaned_img = cleaned_img
        self.is_cleaned  = is_cleaned

    # -- dunders ----------------------------------------------------------

    def __repr__(self) -> str:
        status = "cleaned" if self.is_cleaned else "raw"
        return f"ROI(n_voxels={self.n_voxels}, status={status})"

    def __len__(self) -> int:
        return self.n_voxels

    def __array__(self, dtype=None) -> np.ndarray:
        return self.values.astype(dtype) if dtype else self.values

    def __bool__(self) -> bool:
        return self.n_voxels > 0

    def __eq__(self, other) -> bool:
        return isinstance(other, ROI) and np.array_equal(self.values, other.values)

    __hash__ = None  # ROI is mutable-ish / value-compared → not hashable

    def __add__(self, other: ROI) -> np.ndarray:
        return np.concatenate([self.values, other.values])

    # -- properties -------------------------------------------------------

    @property
    def n_voxels(self) -> int:
        return int(self.values.size)

    @property
    def volume_mm3(self) -> float:
        """Volume of the ROI mask in mm³."""
        voxel_vol = float(np.prod(self.mask_img.header.get_zooms()[:3]))
        return float(self.mask_img.get_fdata().sum() * voxel_vol)

    # -- statistics -------------------------------------------------------

    def stats(self) -> dict[str, float | int]:
        """Descriptive statistics on the extracted values (NaN-aware)."""
        return {
            **compute_stats(self.values),
            "volume_mm3": self.volume_mm3,
            "n_voxels":   self.n_voxels,
        }

    # ------------------------------------------------------------------
    # Post-processing
    # ------------------------------------------------------------------

    # -- private helpers --------------------------------------------------

    @staticmethod
    def _fdata_3d(img: nib.Nifti1Image) -> np.ndarray:
        """Return ``img`` data squeezed to 3D.

        SimNIBS NIfTI volumes (e-field, ``final_tissues``, tissue labels …)
        often carry a trailing singleton 4th axis, e.g. ``(X, Y, Z, 1)``.
        Squeezing keeps boolean mask operations broadcast-safe.
        """
        data = np.squeeze(np.asarray(img.get_fdata()))
        if data.ndim != 3:
            raise ValueError(
                f"Expected a 3D volume after squeeze, got shape {img.shape}."
            )
        return data

    def _smooth(self, fwhm: float) -> nib.Nifti1Image:
        """Return a smoothed copy of the source e-field image."""
        return image.smooth_img(self.efield.img, fwhm=fwhm)   # .img, pas EField

    def _extract(self, efield_img: nib.Nifti1Image) -> tuple[np.ndarray, nib.Nifti1Image]:
        """Mask an e-field image → (1-D values, volumetric masked image)."""
        # apply_mask on a (X,Y,Z,1) image returns (1, N) → squeeze to (N,)
        values     = np.squeeze(masking.apply_mask(efield_img, self.mask_img)).astype(np.float64)
        masked_img = masking.unmask(values, self.mask_img)
        return values, masked_img

    def _clean(
        self,
        values: np.ndarray,
        method: str,
        portion: float | None,
    ) -> tuple[np.ndarray, nib.Nifti1Image]:
        """Remove outliers on non-zero voxels → (filtered values, cleaned image)."""
        filtered = values.copy()
        nonzero  = values > 0
        if nonzero.any():
            cleaned_nonzero, _ = self.remove_outliers(
                values[nonzero], method=method, portion=portion,
            )
            filtered[nonzero] = cleaned_nonzero

        cleaned_img = masking.unmask(filtered, self.mask_img)
        return filtered, cleaned_img

    # -- public -----------------------------------------------------------

    def postprocess(
        self,
        smooth_fwhm: float | None = 2.0,
        outlier_method: str = "iqr",
        portion: float | None = None,
    ) -> ROI:
        """Smooth and/or remove outliers.

        Returns a **new** ``ROI`` — the original is never mutated.

        Parameters
        ----------
        smooth_fwhm : float or None
            FWHM (mm) for Gaussian smoothing applied *before* masking.
            ``None`` or ``0`` to skip.
        outlier_method : {"iqr", "z"}
            Outlier removal strategy.
        portion : float or None
            Central portion to keep (e.g. ``0.95``). ``None`` to skip.
        """
        efield_img = (
            self._smooth(smooth_fwhm)
            if smooth_fwhm and smooth_fwhm > 0
            else self.efield.img                              # .img, pas EField
        )
        values, masked_img    = self._extract(efield_img)
        filtered, cleaned_img = self._clean(values, outlier_method, portion)

        return ROI(
            values=filtered,
            mask_img=self.mask_img,
            efield=self.efield,
            masked_img=masked_img,
            cleaned_img=cleaned_img,
            is_cleaned=True,
        )

    @staticmethod
    def remove_outliers(
        values: np.ndarray,
        method: str = "iqr",
        z_thresh: float = 3.5,
        iqr_factor: float = 1.5,
        portion: float | None = None,
    ) -> tuple[np.ndarray, np.ndarray]:
        """Filter outliers on a 1-D vector.

        Parameters
        ----------
        values : np.ndarray
            Input values.
        method : {"iqr", "z"}
            ``"iqr"`` — keep values within ``[Q1 - factor·IQR, Q3 + factor·IQR]``.
            ``"z"``   — keep values with robust |z-score| ≤ *z_thresh*.
        z_thresh : float
            Threshold for the robust z-score method (default 3.5).
        iqr_factor : float
            Multiplier for IQR fences (default 1.5).
        portion : float or None
            If set, additionally trim to the central *portion*
            (e.g. ``0.95`` keeps the 2.5 %–97.5 % range).

        Returns
        -------
        filtered : np.ndarray
            Copy of *values* with outliers set to ``NaN``.
        keep : np.ndarray
            Boolean mask of retained values.
        """
        vals = np.asarray(values, dtype=np.float64)
        keep = np.ones(vals.shape, dtype=bool)

        # Central portion trim
        if portion is not None:
            if not (0.0 < portion <= 1.0):
                raise ValueError("portion must be in (0, 1]")
            lo_q = (1.0 - portion) / 2.0
            hi_q = 1.0 - lo_q
            lo, hi = np.quantile(vals, [lo_q, hi_q])
            keep &= (vals >= lo) & (vals <= hi)

        # IQR or robust z-score
        if method.lower() == "iqr":
            q1, q3 = np.quantile(vals, [0.25, 0.75])
            iqr = q3 - q1
            keep &= (vals >= q1 - iqr_factor * iqr) & (vals <= q3 + iqr_factor * iqr)
        elif method.lower() == "z":
            med = np.median(vals)
            mad = np.median(np.abs(vals - med))
            if mad == 0:
                keep &= np.isfinite(vals)
            else:
                robust_z = 0.6745 * (vals - med) / mad
                keep &= np.abs(robust_z) <= z_thresh
        else:
            raise ValueError(f"method must be 'iqr' or 'z', got '{method}'")

        filtered = vals.copy()
        filtered[~keep] = np.nan
        return filtered, keep

    # ------------------------------------------------------------------
    # Extra-ROI (complement)
    # ------------------------------------------------------------------

    def complement(self, brain_mask: str | Path | None = None) -> ROI:
        """E-field values *outside* this ROI but *inside* the brain.

        Returns a **new** ``ROI`` — the original is never mutated.

        Parameters
        ----------
        brain_mask : str, Path, or None
            Path to a binary brain mask NIfTI.  When ``None``, resolved
            automatically from the attached segmentation's ``final_tissues``
            (any tissue > 0 = brain).

        Raises
        ------
        ValueError
            If *brain_mask* is ``None`` and no segmentation is attached.
        """
        brain_img = self._resolve_brain_mask(brain_mask)
        brain_img = image.resample_to_img(
            brain_img, self.mask_img, interpolation="nearest"
        )
        # squeeze both → 3D, sinon (X,Y,Z,1) & (X,Y,Z) ne broadcast pas
        extra_data = (
            self._fdata_3d(brain_img).astype(bool)
            & ~self._fdata_3d(self.mask_img).astype(bool)
        ).astype(np.uint8)
        extra_mask = nib.Nifti1Image(extra_data, self.mask_img.affine)

        values = np.squeeze(masking.apply_mask(self.efield.img, extra_mask))   # .img !
        return ROI(values=values, mask_img=extra_mask, efield=self.efield)

    def _resolve_brain_mask(self, brain_mask: str | Path | None) -> nib.Nifti1Image:
        """Return a binary brain mask image, in the e-field's own space.

        Priority:
        1. Explicit *brain_mask* path.
        2. The attached segmentation's tissue map, binarised (value > 0),
           picking the MNI-space version when the e-field is in MNI space.

        Raises
        ------
        ValueError
            If no *brain_mask* is given and no segmentation is attached.
        """
        if brain_mask is not None:
            return nib.load(str(brain_mask))

        sim = getattr(self.efield, "simulation", None)
        seg = getattr(sim, "segmentation", None) if sim is not None else None
        if seg is not None:
            # An MNI e-field needs an MNI tissue map: the native one would be
            # resampled onto the wrong grid and mask out everything.
            if getattr(self.efield, "space", "native") == "mni":
                tissues_path = seg.final_tissues_mni
            else:
                tissues_path = seg.final_tissues
            tissues_nii = nib.load(str(tissues_path))
            data = (self._fdata_3d(tissues_nii) > 0).astype(np.uint8)   # squeeze → 3D
            return nib.Nifti1Image(data, tissues_nii.affine)

        raise ValueError(
            "brain_mask is required when no SegmentationResult is attached.\n"
            "Either pass brain_mask= explicitly, or call "
            "sim.set_segmentation(seg) beforehand."
        )

    # ------------------------------------------------------------------
    # Tissue filtering
    # ------------------------------------------------------------------

    def filter_tissue(
        self,
        tissue: str,
        label_img: str | Path | None = None,
        lut: str | Path | None = None,
    ) -> ROI:
        """Restrict the ROI to a specific tissue type.

        Returns a **new** ``ROI`` — the original is never mutated.

        ``label_img`` can be omitted when a ``SegmentationResult`` has been
        attached to the parent simulation via ``set_segmentation``; the path is
        then resolved automatically from ``segmentation.tissue_labeling_upsampled``.

        Parameters
        ----------
        tissue : str
            Tissue name (e.g. ``"Gray-Matter"``). Case-insensitive.
        label_img : str, Path, or None
            Path to the SimNIBS tissue labeling NIfTI. ``None`` → auto-resolve.
        lut : str, Path, or None
            Path to ``*_LUT.txt``. If omitted, the standard SimNIBS mapping is used.
        """
        resolved_label_img = self._resolve_label_img(label_img)
        tissue_mask = self._build_tissue_mask(resolved_label_img, tissue, lut)
        new_mask    = self._intersect_masks(self.mask_img, tissue_mask)

        values = np.squeeze(masking.apply_mask(self.efield.img, new_mask))     # .img !
        return ROI(values=values, mask_img=new_mask, efield=self.efield)

    def _resolve_label_img(self, label_img: str | Path | None) -> Path:
        """Return a concrete path to the tissue labeling NIfTI.

        Priority:
        1. Explicit *label_img* argument.
        2. ``self.efield.simulation.segmentation.tissue_labeling_upsampled``.
        """
        if label_img is not None:
            return Path(label_img)

        sim = getattr(self.efield, "simulation", None)
        seg = getattr(sim, "segmentation", None) if sim is not None else None
        if seg is not None:
            return seg.tissue_labeling_upsampled

        raise ValueError(
            "label_img is required when no SegmentationResult is attached.\n"
            "Either pass label_img= explicitly, or call "
            "sim.set_segmentation(seg) beforehand."
        )

    def _build_tissue_mask(
        self,
        label_img_path: str | Path,
        tissue: str,
        lut_path: str | Path | None,
    ) -> nib.Nifti1Image:
        """Build a binary mask for a single tissue type."""
        label_nii   = nib.load(str(label_img_path))
        label_array = np.round(self._fdata_3d(label_nii)).astype(int)   # squeeze → 3D

        effective_lut = parse_lut(lut_path) if lut_path else _SIMNIBS_LUT
        tissue_val    = resolve_tissue_value(tissue, effective_lut)

        data = (label_array == tissue_val).astype(np.uint8)
        return nib.Nifti1Image(data, label_nii.affine)

    @staticmethod
    def _intersect_masks(
        mask_a: nib.Nifti1Image,
        mask_b: nib.Nifti1Image,
    ) -> nib.Nifti1Image:
        """Binary intersection of two masks (mask_b resampled onto mask_a)."""
        mask_b = image.resample_to_img(mask_b, mask_a, interpolation="nearest")
        combined = (
            ROI._fdata_3d(mask_a).astype(bool)
            & ROI._fdata_3d(mask_b).astype(bool)
        ).astype(np.uint8)
        return nib.Nifti1Image(combined, mask_a.affine)

    # ------------------------------------------------------------------
    # Export  (delegates to io/)
    # ------------------------------------------------------------------

    def save(
        self,
        path: str | Path,
        metrics: list[str] | None = None,
        format: str = "tsv",
    ) -> Path:
        """Export statistics to disk (tsv or csv)."""
        from ..io.export import save_results
        return save_results(self.stats(), path, metrics=metrics, format=format)

    def save_nifti(self, path: str | Path) -> Path:
        """Write ``cleaned_img`` (or ``masked_img``) to a NIfTI file."""
        from ..io.nifti import save_nifti
        img = self.cleaned_img or self.masked_img
        if img is None:
            raise ValueError(
                "No volumetric image to save. Call .postprocess() first, "
                "or use .save() to export stats only."
            )
        return save_nifti(img, path)

volume_mm3 property

Volume of the ROI mask in mm³.

stats()

Descriptive statistics on the extracted values (NaN-aware).

Source code in simnibs_reader/nifti/roi.py
def stats(self) -> dict[str, float | int]:
    """Descriptive statistics on the extracted values (NaN-aware)."""
    return {
        **compute_stats(self.values),
        "volume_mm3": self.volume_mm3,
        "n_voxels":   self.n_voxels,
    }

postprocess(smooth_fwhm=2.0, outlier_method='iqr', portion=None)

Smooth and/or remove outliers.

Returns a new ROI — the original is never mutated.

Parameters:

Name Type Description Default
smooth_fwhm float or None

FWHM (mm) for Gaussian smoothing applied before masking. None or 0 to skip.

2.0
outlier_method ('iqr', 'z')

Outlier removal strategy.

"iqr"
portion float or None

Central portion to keep (e.g. 0.95). None to skip.

None
Source code in simnibs_reader/nifti/roi.py
def postprocess(
    self,
    smooth_fwhm: float | None = 2.0,
    outlier_method: str = "iqr",
    portion: float | None = None,
) -> ROI:
    """Smooth and/or remove outliers.

    Returns a **new** ``ROI`` — the original is never mutated.

    Parameters
    ----------
    smooth_fwhm : float or None
        FWHM (mm) for Gaussian smoothing applied *before* masking.
        ``None`` or ``0`` to skip.
    outlier_method : {"iqr", "z"}
        Outlier removal strategy.
    portion : float or None
        Central portion to keep (e.g. ``0.95``). ``None`` to skip.
    """
    efield_img = (
        self._smooth(smooth_fwhm)
        if smooth_fwhm and smooth_fwhm > 0
        else self.efield.img                              # .img, pas EField
    )
    values, masked_img    = self._extract(efield_img)
    filtered, cleaned_img = self._clean(values, outlier_method, portion)

    return ROI(
        values=filtered,
        mask_img=self.mask_img,
        efield=self.efield,
        masked_img=masked_img,
        cleaned_img=cleaned_img,
        is_cleaned=True,
    )

remove_outliers(values, method='iqr', z_thresh=3.5, iqr_factor=1.5, portion=None) staticmethod

Filter outliers on a 1-D vector.

Parameters:

Name Type Description Default
values ndarray

Input values.

required
method ('iqr', 'z')

"iqr" — keep values within [Q1 - factor·IQR, Q3 + factor·IQR]. "z" — keep values with robust |z-score| ≤ z_thresh.

"iqr"
z_thresh float

Threshold for the robust z-score method (default 3.5).

3.5
iqr_factor float

Multiplier for IQR fences (default 1.5).

1.5
portion float or None

If set, additionally trim to the central portion (e.g. 0.95 keeps the 2.5 %–97.5 % range).

None

Returns:

Name Type Description
filtered ndarray

Copy of values with outliers set to NaN.

keep ndarray

Boolean mask of retained values.

Source code in simnibs_reader/nifti/roi.py
@staticmethod
def remove_outliers(
    values: np.ndarray,
    method: str = "iqr",
    z_thresh: float = 3.5,
    iqr_factor: float = 1.5,
    portion: float | None = None,
) -> tuple[np.ndarray, np.ndarray]:
    """Filter outliers on a 1-D vector.

    Parameters
    ----------
    values : np.ndarray
        Input values.
    method : {"iqr", "z"}
        ``"iqr"`` — keep values within ``[Q1 - factor·IQR, Q3 + factor·IQR]``.
        ``"z"``   — keep values with robust |z-score| ≤ *z_thresh*.
    z_thresh : float
        Threshold for the robust z-score method (default 3.5).
    iqr_factor : float
        Multiplier for IQR fences (default 1.5).
    portion : float or None
        If set, additionally trim to the central *portion*
        (e.g. ``0.95`` keeps the 2.5 %–97.5 % range).

    Returns
    -------
    filtered : np.ndarray
        Copy of *values* with outliers set to ``NaN``.
    keep : np.ndarray
        Boolean mask of retained values.
    """
    vals = np.asarray(values, dtype=np.float64)
    keep = np.ones(vals.shape, dtype=bool)

    # Central portion trim
    if portion is not None:
        if not (0.0 < portion <= 1.0):
            raise ValueError("portion must be in (0, 1]")
        lo_q = (1.0 - portion) / 2.0
        hi_q = 1.0 - lo_q
        lo, hi = np.quantile(vals, [lo_q, hi_q])
        keep &= (vals >= lo) & (vals <= hi)

    # IQR or robust z-score
    if method.lower() == "iqr":
        q1, q3 = np.quantile(vals, [0.25, 0.75])
        iqr = q3 - q1
        keep &= (vals >= q1 - iqr_factor * iqr) & (vals <= q3 + iqr_factor * iqr)
    elif method.lower() == "z":
        med = np.median(vals)
        mad = np.median(np.abs(vals - med))
        if mad == 0:
            keep &= np.isfinite(vals)
        else:
            robust_z = 0.6745 * (vals - med) / mad
            keep &= np.abs(robust_z) <= z_thresh
    else:
        raise ValueError(f"method must be 'iqr' or 'z', got '{method}'")

    filtered = vals.copy()
    filtered[~keep] = np.nan
    return filtered, keep

complement(brain_mask=None)

E-field values outside this ROI but inside the brain.

Returns a new ROI — the original is never mutated.

Parameters:

Name Type Description Default
brain_mask str, Path, or None

Path to a binary brain mask NIfTI. When None, resolved automatically from the attached segmentation's final_tissues (any tissue > 0 = brain).

None

Raises:

Type Description
ValueError

If brain_mask is None and no segmentation is attached.

Source code in simnibs_reader/nifti/roi.py
def complement(self, brain_mask: str | Path | None = None) -> ROI:
    """E-field values *outside* this ROI but *inside* the brain.

    Returns a **new** ``ROI`` — the original is never mutated.

    Parameters
    ----------
    brain_mask : str, Path, or None
        Path to a binary brain mask NIfTI.  When ``None``, resolved
        automatically from the attached segmentation's ``final_tissues``
        (any tissue > 0 = brain).

    Raises
    ------
    ValueError
        If *brain_mask* is ``None`` and no segmentation is attached.
    """
    brain_img = self._resolve_brain_mask(brain_mask)
    brain_img = image.resample_to_img(
        brain_img, self.mask_img, interpolation="nearest"
    )
    # squeeze both → 3D, sinon (X,Y,Z,1) & (X,Y,Z) ne broadcast pas
    extra_data = (
        self._fdata_3d(brain_img).astype(bool)
        & ~self._fdata_3d(self.mask_img).astype(bool)
    ).astype(np.uint8)
    extra_mask = nib.Nifti1Image(extra_data, self.mask_img.affine)

    values = np.squeeze(masking.apply_mask(self.efield.img, extra_mask))   # .img !
    return ROI(values=values, mask_img=extra_mask, efield=self.efield)

filter_tissue(tissue, label_img=None, lut=None)

Restrict the ROI to a specific tissue type.

Returns a new ROI — the original is never mutated.

label_img can be omitted when a SegmentationResult has been attached to the parent simulation via set_segmentation; the path is then resolved automatically from segmentation.tissue_labeling_upsampled.

Parameters:

Name Type Description Default
tissue str

Tissue name (e.g. "Gray-Matter"). Case-insensitive.

required
label_img str, Path, or None

Path to the SimNIBS tissue labeling NIfTI. None → auto-resolve.

None
lut str, Path, or None

Path to *_LUT.txt. If omitted, the standard SimNIBS mapping is used.

None
Source code in simnibs_reader/nifti/roi.py
def filter_tissue(
    self,
    tissue: str,
    label_img: str | Path | None = None,
    lut: str | Path | None = None,
) -> ROI:
    """Restrict the ROI to a specific tissue type.

    Returns a **new** ``ROI`` — the original is never mutated.

    ``label_img`` can be omitted when a ``SegmentationResult`` has been
    attached to the parent simulation via ``set_segmentation``; the path is
    then resolved automatically from ``segmentation.tissue_labeling_upsampled``.

    Parameters
    ----------
    tissue : str
        Tissue name (e.g. ``"Gray-Matter"``). Case-insensitive.
    label_img : str, Path, or None
        Path to the SimNIBS tissue labeling NIfTI. ``None`` → auto-resolve.
    lut : str, Path, or None
        Path to ``*_LUT.txt``. If omitted, the standard SimNIBS mapping is used.
    """
    resolved_label_img = self._resolve_label_img(label_img)
    tissue_mask = self._build_tissue_mask(resolved_label_img, tissue, lut)
    new_mask    = self._intersect_masks(self.mask_img, tissue_mask)

    values = np.squeeze(masking.apply_mask(self.efield.img, new_mask))     # .img !
    return ROI(values=values, mask_img=new_mask, efield=self.efield)

save(path, metrics=None, format='tsv')

Export statistics to disk (tsv or csv).

Source code in simnibs_reader/nifti/roi.py
def save(
    self,
    path: str | Path,
    metrics: list[str] | None = None,
    format: str = "tsv",
) -> Path:
    """Export statistics to disk (tsv or csv)."""
    from ..io.export import save_results
    return save_results(self.stats(), path, metrics=metrics, format=format)

save_nifti(path)

Write cleaned_img (or masked_img) to a NIfTI file.

Source code in simnibs_reader/nifti/roi.py
def save_nifti(self, path: str | Path) -> Path:
    """Write ``cleaned_img`` (or ``masked_img``) to a NIfTI file."""
    from ..io.nifti import save_nifti
    img = self.cleaned_img or self.masked_img
    if img is None:
        raise ValueError(
            "No volumetric image to save. Call .postprocess() first, "
            "or use .save() to export stats only."
        )
    return save_nifti(img, path)