confusius.validation¶
validation ¶
Data validation utilities for confusius.
Modules:
-
atlas–Atlas Dataset validation utilities.
-
coordinates–Coordinate validation utilities.
-
mask–Mask validation utilities.
-
registration–Validation helpers for registration transform DataArrays.
-
time_series–Time series validation utilities.
-
units–Unit validation utilities.
-
voxeldata–Validation helpers for the ConfUSIus VoxelData model and fUSI recordings.
Functions:
-
canonicalize_voxeldata–Restore a scalar-indexed VoxelData dimension and its geometry.
-
ensure_labels–Canonicalize
labelsanddata, then validate labels share data's grid. -
ensure_mask–Canonicalize
maskanddata, then validate thatmaskshares data's grid. -
ensure_time_aligned–Return
valueas a(time, ...)DataArray aligned withsignals. -
ensure_voxeldata–Return a canonical, validated VoxelData array.
-
validate_atlas–Validate that a Dataset is a well-formed atlas.
-
validate_bspline–Raise ValueError if
dais not a valid B-spline transform DataArray. -
validate_displacement_field–Raise ValueError if
dais not a valid displacement field DataArray. -
validate_labels–Validate that a label map shares data's VoxelData grid.
-
validate_mask–Validate that a mask shares data's VoxelData grid.
-
validate_matching_coordinates–Validate that selected coordinates match between two DataArrays.
-
validate_matching_spatial_units–Raise
ValueErrorif world-space units disagree across DataArrays. -
validate_time_series–Validate time series for time series processing operations.
-
validate_voxeldata–Validate a DataArray against the VoxelData model without modifying it.
canonicalize_voxeldata ¶
Restore a scalar-indexed VoxelData dimension and its geometry.
Scalar indexing such as data.isel(j=0) removes j from the array dimensions
but retains it as a scalar coordinate. This function restores every missing
native voxel dimension (k, j, i) as a length-one dimension, then orders all
dimensions as (...extra_dims, time, pose, k, j, i). It preserves coordinate
values and their order. When scalar indexing fixed a dimension in a
VoxelToWorldIndex,
its geometry is rebuilt from the untouched affine, including its units
(VoxelToWorldIndex always carries one, defaulting to "mm" since
[attach_voxel_to_world_index][confusius._utils.geometry.attach_voxel_to_world_index]
itself defaults it). Missing time acquisition metadata also defaults where
possible. This function does not otherwise validate the VoxelData model; use
ensure_voxeldata to canonicalize and
validate.
Parameters:
-
(data¶DataArray) –DataArray to canonicalize.
Returns:
-
DataArray–Canonicalized DataArray with all native voxel dimensions present.
Raises:
-
TypeError–If
datais not anxarray.DataArray. -
ValueError–If a native voxel dimension is absent and has no scalar coordinate from which to restore it, or its same-named coordinate is not scalar.
Warns:
-
UserWarning–If missing
timeorslice_timeacquisition metadata is defaulted.
ensure_labels ¶
Canonicalize labels and data, then validate labels share data's grid.
Both labels and data are canonicalized via
ensure_voxeldata (restoring any
scalar-reduced voxel dims) before
validate_labels checks them.
Parameters:
-
(labels¶DataArray) –Label map to validate. Must have integer dtype. Accepts two formats:
- Flat label map: Spatial dims only, e.g.
(k, j, i). Background voxels labeled0; each unique non-zero integer identifies a distinct, non-overlapping region. Theregionscoordinate of the output holds the integer label values. - Stacked mask format: Has a leading
maskdimension followed by spatial dims, e.g.(mask, k, j, i). Each layer has values in{0, region_id}and regions may overlap. Theregioncoordinate of the output holds themaskcoordinate values (e.g., region label).
- Flat label map: Spatial dims only, e.g.
-
(data¶DataArray) –VoxelData array to validate labels against.
-
(labels_name¶str, default:"labels") –Name of the labels parameter (used in error messages).
Returns:
-
DataArray–The canonicalized
labels.
Raises:
-
TypeError–If
labelsis not an integer dtype DataArray. -
ValueError–If
labelsordataisn't a VoxelData array, or iflabels's voxel grid doesn't matchdata's.
ensure_mask ¶
ensure_mask(
mask: DataArray,
data: DataArray,
mask_name: str = "mask",
require_exact_dims: bool = False,
coerce_bool: bool = True,
) -> DataArray
Canonicalize mask and data, then validate that mask shares data's grid.
Both mask and data are canonicalized via
ensure_voxeldata (restoring any
scalar-reduced voxel dims) before
validate_mask checks them.
Parameters:
-
(mask¶DataArray) –Mask to validate. Must have boolean dtype, or integer dtype with exactly one non-zero value (0 = background, one region id = foreground). The latter format is produced by
get_masks. -
(data¶DataArray) –VoxelData array to validate mask against.
-
(mask_name¶str, default:"mask") –Name of the mask parameter (used in error messages).
-
(require_exact_dims¶bool, default:False) –Whether
mask.dimsmust match all non-timedimensions ofdatain the same order. -
(coerce_bool¶bool, default:True) –Whether to coerce the returned
maskto boolean dtype. Single-label integer masks ({0, region_id}) become{False, True}so callers can index with the result without the integer label being misread as a positional index. When False,maskis returned with its original dtype unchanged.
Returns:
-
DataArray–The canonicalized
mask, coerced to boolean dtype whencoerce_boolisTrue(the default), otherwise returned with its original dtype.
Raises:
-
TypeError–If
maskis not a boolean or single-label integer DataArray. -
ValueError–If
maskordataisn't a VoxelData array, ifmask's voxel grid doesn't matchdata's, or ifrequire_exact_dimsis set andmask's dimensions don't matchdata's.
ensure_time_aligned ¶
ensure_time_aligned(
signals: DataArray,
value: DataArray | ndarray | DataFrame,
name: str,
*,
ndim: Literal[1, 2],
allow_dataframe: bool = True,
) -> DataArray
Return value as a (time, ...) DataArray aligned with signals.
value is validated against signals and returned as a (time,) DataArray
(ndim=1) or a (time, name) DataArray (ndim=2, a 1D value becoming one
column) with time as its first dimension:
- A DataArray must have a
timedimension. - A DataFrame must have a
timecolumn and at least one other, numeric column; the other columns become anamedimension named after them. - A NumPy array is wrapped with dims
(time, name).
value must have as many timepoints as signals. When both carry time
coordinates these must match within the default coordinate-comparison tolerance
(rtol=1e-5, atol=1e-8). When only signals does, value is assumed to be
ordered like signals along time and takes its time coordinates, with a warning
since alignment cannot be verified.
signals with a pose-dependent (time, pose) time coordinate are represented
by their consolidated time (see
consolidate_poses).
Parameters:
-
(signals¶(time, ...) xarray.DataArray) –Signals defining the
timegrid. -
(value¶(time, ...) xarray.DataArray, numpy.ndarray, or pandas.DataFrame) –Array to align.
-
(name¶str) –Name of
valueused in error and warning messages. Ifvalueis a DataFrame, its columns become thenamedimension. -
(ndim¶(1, 2), default:1) –Number of dimensions of the result:
1for a single series such as a sample mask,2for a set of regressors such as confounds. -
(allow_dataframe¶bool, default:True) –Whether to accept a DataFrame
value.
Returns:
-
(time,) or (time, confound) xarray.DataArray–valuewithtimeas its first dimension andndimdimensions.
Raises:
-
TypeError–If
valueis not one of the accepted types. -
ValueError–If
valuehas notimedimension or column, has more thanndimdimensions, or does not have as many timepoints assignals; if a DataFramevaluehas duplicate or non-numeric columns or no column besidestime; or iftimecoordinates do not match those ofsignals.
Warns:
-
UserWarning–If
valuehas notimecoordinates whilesignalsdoes, since alignment cannot be verified. -
UserWarning–If the per-pose timing metadata of pose-dependent
signalsare insufficient to infer their whole-volume time, in which case the first pose's timestamps are used.
ensure_voxeldata ¶
ensure_voxeldata(
data: DataArray, **validate_kwargs: Any
) -> DataArray
Return a canonical, validated VoxelData array.
This is the normal entry point for spatial inputs: it restores scalar-indexed
native voxel dimensions and geometry, and orders dimensions as
(...extra_dims, time, pose, k, j, i) with
canonicalize_voxeldata, then checks
the resulting DataArray against the VoxelData model. Use
validate_voxeldata when the input must
already follow that model.
Parameters:
-
(data¶DataArray) –DataArray to canonicalize and validate.
-
(**validate_kwargs¶Any, default:{}) –Keyword arguments forwarded to validate_voxeldata.
Returns:
-
DataArray–Canonicalized VoxelData array that satisfies the requested validation checks.
Raises:
-
TypeError–If
datais not anxarray.DataArray. -
ValueError–If canonicalization or validation fails.
validate_atlas ¶
validate_atlas(
ds: Dataset, *, require_mesh_use: bool = False
) -> None
Validate that a Dataset is a well-formed atlas.
Companion to validate_voxeldata. Checks that
ds matches the atlas schema produced by
fetch_brainglobe_atlas and consumed
by the .atlas accessor:
- Type:
dsis anxarray.Dataset. - Data variables:
reference,annotation, andhemispheresare all present as data variables (ahemispheresstored as a coordinate is reported as missing). - Grid: the three variables share identical dimensions, those dimensions are a
subset of
(k, j, i)(a resampled single slice has a singletonk), and each variable carries aVoxelToWorldIndexderiving its worldz/y/xcoordinates. - Data types:
referenceis floating-point;annotationandhemispheresare integer-valued. - Attributes:
attrs["structures"]is present and is a brainglobeStructuresDict. The descriptive metadata the builder adds (name,citation,species,orientation) is not required. - Affines: where two data variables both define an affine of the same name (in
attrs["affines"]), the matrices must be equal — a mismatch means the variables are not on a common world frame. - Mesh use (only when
require_mesh_useis set):attrs["world_to_base"]— the pull mesh transform get_mesh needs — is present, and at least one structure references a mesh file that exists on disk.
Parameters:
-
(ds¶Dataset) –Dataset to validate as an atlas.
-
(require_mesh_use¶bool, default:False) –Whether to also require the machinery
get_meshneeds: theworld_to_basetransform attribute and at least one existing region mesh file.
Raises:
-
TypeError–If
dsis not anxarray.Dataset, or ifreferenceis not floating-point orannotation/hemispheresare not integer-valued. -
ValueError–If any required data variable or attribute is missing, if the variables do not share dimensions that are a subset of
(k, j, i), if a variable lacks aVoxelToWorldIndex, ifattrs["structures"]is not a brainglobeStructuresDict, or ifrequire_mesh_useis set andworld_to_baseor usable region meshes are absent.
Examples:
validate_bspline ¶
Raise ValueError if da is not a valid B-spline transform DataArray.
Parameters:
-
(da¶DataArray) –DataArray to validate.
Raises:
-
ValueError–If
da.attrs["transform_type"] != "bspline_transform", required attrs are missing, ordais not a VoxelData array.
validate_displacement_field ¶
Raise ValueError if da is not a valid displacement field DataArray.
Parameters:
-
(da¶DataArray) –DataArray to validate.
Raises:
-
ValueError–If
da.attrs["type"] != "displacement_field_transform",dadoes not have"component"as its first dimension, ordais not a VoxelData array.
validate_labels ¶
Validate that a label map shares data's VoxelData grid.
labels and data must already be canonical VoxelData arrays (see
validate_voxeldata) -- this does not
canonicalize either. For a labels/data pair that may not already be canonical
(e.g. a scalar-reduced voxel dim), use
ensure_labels instead.
Parameters:
-
(labels¶DataArray) –Label map to validate. Must have integer dtype. Accepts two formats:
- Flat label map: Spatial dims only, e.g.
(k, j, i). Background voxels labeled0; each unique non-zero integer identifies a distinct, non-overlapping region. Theregionscoordinate of the output holds the integer label values. - Stacked mask format: Has a leading
maskdimension followed by spatial dims, e.g.(mask, k, j, i). Each layer has values in{0, region_id}and regions may overlap. Theregioncoordinate of the output holds themaskcoordinate values (e.g., region label).
- Flat label map: Spatial dims only, e.g.
-
(data¶DataArray) –VoxelData array to validate labels against.
-
(labels_name¶str, default:"labels") –Name of the labels parameter (used in error messages).
Raises:
-
TypeError–If
labelsis not an integer dtype DataArray. -
ValueError–If
labelsordataisn't a valid VoxelData array, or iflabels's voxel grid doesn't matchdata's.
validate_mask ¶
validate_mask(
mask: DataArray,
data: DataArray,
mask_name: str = "mask",
require_exact_dims: bool = False,
) -> None
Validate that a mask shares data's VoxelData grid.
mask and data must already be canonical VoxelData arrays (see
validate_voxeldata) -- this does not
canonicalize either. For a mask/data pair that may not already be canonical
(e.g. a scalar-reduced voxel dim), use
ensure_mask instead.
Parameters:
-
(mask¶DataArray) –Mask to validate. Must have boolean dtype, or integer dtype with exactly one non-zero value (0 = background, one region id = foreground). The latter format is produced by
get_masks. -
(data¶DataArray) –VoxelData array to validate mask against.
-
(mask_name¶str, default:"mask") –Name of the mask parameter (used in error messages).
-
(require_exact_dims¶bool, default:False) –Whether
mask.dimsmust match all non-timedimensions ofdatain the same order.
Raises:
-
TypeError–If
maskis not a boolean or single-label integer DataArray. -
ValueError–If
maskordataisn't a valid VoxelData array, ifmask's voxel grid doesn't matchdata's, or ifrequire_exact_dimsis set andmask's dimensions don't matchdata's.
validate_matching_coordinates ¶
validate_matching_coordinates(
left: DataArray,
right: DataArray,
coord_names: Hashable
| Iterable[Hashable]
| None = None,
*,
left_name: str = "left array",
right_name: str = "right array",
rtol: float = 1e-05,
atol: float = 1e-08,
) -> None
Validate that selected coordinates match between two DataArrays.
Comparison is performed on coordinate values rather than the full coordinate
DataArray, so unrelated attached coordinates do not cause false mismatches.
Numeric coordinates are compared with tolerance to accommodate harmless
floating-point drift (for example after serialization and reload). Non-numeric
coordinates are compared exactly.
Parameters:
-
(left¶DataArray) –First array to compare.
-
(right¶DataArray) –Second array to compare.
-
(coord_names¶Hashable or Iterable[Hashable], default:None) –Coordinate names to compare. If not provided, all shared dimension coordinates are checked.
-
(left_name¶str, default:"left array") –Label used for
leftin error messages. Override with a context-specific name (e.g."run 0","map 0") for more actionable errors. -
(right_name¶str, default:"right array") –Label used for
rightin error messages. -
(rtol¶float, default:1e-5) –Relative tolerance used for numeric coordinate comparison.
-
(atol¶float, default:1e-8) –Absolute tolerance used for numeric coordinate comparison.
Raises:
-
ValueError–If a requested coordinate is missing or if coordinates do not match.
validate_matching_spatial_units ¶
Raise ValueError if world-space units disagree across DataArrays.
Parameters:
-
(arrays¶sequence of tuple[str, xarray.DataArray]) –Named DataArrays to compare. Each must carry voxel-to-world geometry.
Raises:
-
ValueError–If any input lacks voxel-to-world geometry, or if any two inputs disagree on their
.fusi.affine.units.
validate_time_series ¶
validate_time_series(
time_series: DataArray,
operation_name: str,
require_unchunked_time: bool = True,
require_sorted_time: bool = False,
require_uniform_time: Literal[False] = False,
uniformity_tolerance: float = 0.01,
) -> tuple[int, None]
validate_time_series(
time_series: DataArray,
operation_name: str,
require_unchunked_time: bool = True,
require_sorted_time: bool = False,
require_uniform_time: Literal[True] = True,
uniformity_tolerance: float = 0.01,
) -> tuple[int, float]
validate_time_series(
time_series: DataArray,
operation_name: str,
require_unchunked_time: bool = True,
require_sorted_time: bool = False,
require_uniform_time: bool = False,
uniformity_tolerance: float = 0.01,
) -> tuple[int, float | None]
Validate time series for time series processing operations.
Performs common validation checks:
- Time series have a
timedimension. - Time dimension has more than 1 timepoint.
- Time dimension is not chunked for Dask arrays (optional).
- Time coordinate is strictly increasing (optional).
- Time coordinate is uniformly sampled (optional).
Parameters:
-
(time_series¶DataArray) –Input time series to validate. Must have a
timedimension. -
(operation_name¶str) –Name of the operation (used in error/warning messages).
-
(require_unchunked_time¶bool, default:True) –Whether to require the time dimension to occupy one Dask chunk. Set to
Falsefor operations that can process chunked time (e.g.,confusius.signal.standardize). -
(require_sorted_time¶bool, default:False) –Whether to require strictly increasing
timecoordinates. -
(require_uniform_time¶bool, default:False) –Whether to require uniformly sampled
timecoordinates and return their spacing. -
(uniformity_tolerance¶float, default:1e-2) –Maximum allowed relative range of consecutive time intervals, defined as
(max_interval - min_interval) / median_interval. Raise aValueErrorif the time coordinate exceeds this threshold.
Returns:
-
time_axis(int) –Axis number for the
timedimension. -
time_spacing(float or None) –Time spacing when
require_uniform_time=True, otherwiseNone.
Raises:
-
ValueError–If
time_serieshas notimedimension, if thetimedimension has only 1 timepoint, if thetimedimension is chunked in a Dask array (whenrequire_unchunked_time=True), ifrequire_sorted_time=Trueand thetimecoordinate is not strictly increasing, or ifrequire_uniform_time=Trueand thetimecoordinate is not uniformly sampled.
validate_voxeldata ¶
validate_voxeldata(
data: DataArray,
*,
require_time: bool = False,
require_unchunked_time: bool = False,
require_uniform_time: bool = False,
uniformity_tolerance: float = 0.01,
allow_pose: bool = True,
allow_extra_dims: bool = True,
require_regular_spacing: bool = False,
regular_spacing_tolerance: float = 0.01,
regular_spacing_dims: RegularSpacingDims = "space",
require_velocity_attrs: bool = False,
require_dtype: Any | None = None,
) -> None
Validate a DataArray against the VoxelData model without modifying it.
This requires non-empty native voxel dimensions (k, j, i), their
coordinates, and a matching VoxelToWorldIndex (which carries the world-space
units shared by z/y/x, exposed via .fusi.affine.units), and dimensions
ordered as (...extra_dims, time, pose, k, j, i). It also validates every
dimension coordinate. When time is present, its acquisition metadata and
units are required. The optional flags add requirements
needed by a particular consumer. Use
ensure_voxeldata when scalar indexing
may have removed a voxel dimension and the input should be canonicalized first.
Parameters:
-
(data¶DataArray) –DataArray to validate.
-
(require_time¶bool, default:False) –Whether to require
timewith more than one coordinate value. -
(require_unchunked_time¶bool, default:False) –Whether to require
timewith more than one coordinate value in a single Dask chunk. -
(require_uniform_time¶bool, default:False) –Whether to require
timewith more than one uniformly spaced coordinate value. -
(uniformity_tolerance¶float, default:1e-2) –Maximum relative variation allowed between consecutive time intervals.
-
(allow_pose¶bool, default:True) –Whether to allow a
posedimension. -
(allow_extra_dims¶bool, default:True) –Whether to allow dimensions outside
time,pose,k,j, andi. -
(require_regular_spacing¶bool, default:False) –Whether to require regular spacing for selected numeric dimension coordinates.
-
(regular_spacing_tolerance¶float, default:1e-2) –Relative tolerance used to assess coordinate regularity.
-
(regular_spacing_dims¶('space', 'core', 'all'), default:"space") –Dimensions to check for regular spacing.
"space"checksk,j, andi;"core"checks present core dimensions;"all"checks every dimension. -
(require_velocity_attrs¶bool, default:False) –Whether to require positive, finite
transmit_frequencyandbeamforming_sound_velocityDataArray attributes. -
(require_dtype¶Any, default:None) –Required data dtype or dtype class, passed to
numpy.issubdtype.
Raises:
-
TypeError–If
datais not anxarray.DataArrayor its dtype does not satisfyrequire_dtype. -
ValueError–If VoxelData geometry, dimensions, coordinates, timing, spacing, or metadata validation fails.