Skip to content

Statistics

simnibs_reader.efield.stats.compute_stats(values)

Descriptive statistics for a 1-D array of e-field values.

NaN and non-finite values are silently ignored.

Parameters:

Name Type Description Default
values ndarray

1-D array (e.g. voxel values inside an ROI).

required

Returns:

Type Description
dict

Keys: mean, median, std, min, max, p5, p95, n_voxels.

Source code in simnibs_reader/efield/stats.py
def compute_stats(values: np.ndarray) -> dict[str, float | int]:
    """Descriptive statistics for a 1-D array of e-field values.

    NaN and non-finite values are silently ignored.

    Parameters
    ----------
    values : np.ndarray
        1-D array (e.g. voxel values inside an ROI).

    Returns
    -------
    dict
        Keys: ``mean``, ``median``, ``std``, ``min``, ``max``,
        ``p5``, ``p95``, ``n_voxels``.
    """
    v = np.asarray(values, dtype=np.float64).ravel()
    v = v[np.isfinite(v)]

    if v.size == 0:
        return {
            "mean": np.nan,
            "median": np.nan,
            "std": np.nan,
            "min": np.nan,
            "max": np.nan,
            "p5": np.nan,
            "p95": np.nan,
            "n_voxels": 0,
        }

    return {
        "mean": float(np.mean(v)),
        "median": float(np.median(v)),
        "std": float(np.std(v)),
        "min": float(np.min(v)),
        "max": float(np.max(v)),
        "p5": float(np.percentile(v, 5)),
        "p95": float(np.percentile(v, 95)),
        "n_voxels": int(v.size),
    }