Headers

zarr_vectors_tools.headers: the source format’s metadata that the store’s arrays cannot hold, kept under headers/<format>/ so that an export can write it back. HeaderRegistry reads and writes it as one dataclass per format.

Registry

HeaderRegistry — typed format headers on top of core’s dict registry.

Headers live in the attributes of /headers/<format>/ (its zarr.json). Core owns the storage half (zarr_vectors.headers.HeaderRegistry), which round-trips opaque JSON dicts and knows nothing about formats; this subclass adds the half that belongs to a format package — turning those dicts into the Header dataclasses, and back.

class zarr_vectors_tools.headers.registry.HeaderRegistry(*args, **kwargs)[source]

Bases: HeaderRegistry

Manages format-specific headers within a zarr vectors store.

Same constructor as core’s — a path/URL, or an already-open Group root handle. add and get speak Header rather than dict; available_formats, has and remove are inherited unchanged.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

Any

add(format_name, header)[source]

Store a format header.

If a header for this format already exists, it is overwritten.

Parameters:
  • format_name (str) – Format identifier.

  • header (Header) – Header dataclass to store.

Return type:

None

get(format_name)[source]

Read and deserialise a format header.

Parameters:

format_name (str) – Format identifier (e.g. "trk", "swc").

Returns:

The deserialised Header subclass.

Raises:

KeyError – If no header exists for this format.

Return type:

Header

Format-specific headers

Format-specific header dataclasses.

Each header captures the metadata that would be lost when converting to the zarr vectors format. Stored as JSON-serialisable dicts in the attributes of /headers/<format>/ (its zarr.json).

class zarr_vectors_tools.headers.formats.CSVHeader(format_name='csv', column_names=<factory>, delimiter=', ', position_columns=<factory>, attribute_columns=<factory>, has_header_row=True, normalise_offset=None, normalise_scale=None)[source]

Bases: Header

CSV point cloud file header metadata.

normalise_offset and normalise_scale are populated by the normalise=True ingest path so export can invert the transform. They are None for stores that were not normalised.

Parameters:
__init__(format_name='csv', column_names=<factory>, delimiter=', ', position_columns=<factory>, attribute_columns=<factory>, has_header_row=True, normalise_offset=None, normalise_scale=None)
Parameters:
Return type:

None

attribute_columns: list[str]
column_names: list[str]
delimiter: str = ','
format_name: str = 'csv'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

CSVHeader

has_header_row: bool = True
normalise_offset: list[float] | None = None
normalise_scale: float | None = None
position_columns: list[str]
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.GraphHeader(format_name='graph', node_count=0, edge_count=0, is_directed=False, mean_degree=0.0, n_components=0, largest_component_size=0)[source]

Bases: Header

Graph-level summary metrics.

Stored as a workaround for the lack of per-object attributes on the graphs geometry type in the core write API. When that gap is closed in core, this header can migrate into graph_attributes.

Parameters:
  • format_name (str)

  • node_count (int)

  • edge_count (int)

  • is_directed (bool)

  • mean_degree (float)

  • n_components (int)

  • largest_component_size (int)

__init__(format_name='graph', node_count=0, edge_count=0, is_directed=False, mean_degree=0.0, n_components=0, largest_component_size=0)
Parameters:
  • format_name (str)

  • node_count (int)

  • edge_count (int)

  • is_directed (bool)

  • mean_degree (float)

  • n_components (int)

  • largest_component_size (int)

Return type:

None

edge_count: int = 0
format_name: str = 'graph'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

GraphHeader

is_directed: bool = False
largest_component_size: int = 0
mean_degree: float = 0.0
n_components: int = 0
node_count: int = 0
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.H5ADHeader(format_name='h5ad', spatial_key='spatial', spatial_ndim=3, spatial_columns=<factory>, n_obs=0, n_vars=0, obs_names=<factory>, obs_attrs=<factory>, gene_names=<factory>, gene_attrs=<factory>, layer=None, categories=<factory>, dtypes=<factory>, row_attr=None, obs_index=None, object_id_column=None, object_id_categories=None, position_names=None)[source]

Bases: Header

AnnData .h5ad header metadata.

Zarr Vectors stores numeric per-vertex arrays, so three kinds of AnnData information cannot survive in the arrays alone and live here instead:

  • Names. obs columns and genes become attribute arrays whose names are sanitised for Zarr paths, so the original labels are kept in the *_names/*_attrs parallel lists.

  • Categories. Categorical/string obs columns are stored as integer codes, dictionary-encoded on the array itself so a reader sees labels; categories maps a column to its level labels too, under its original name, so export can rebuild the pandas.Categorical.

  • Order and identity. Zarr Vectors orders vertices by spatial chunk, not by original row, so row_attr names the attribute holding each cell’s source row index and obs_index (when small enough to inline) holds the original barcodes.

Parameters:
__init__(format_name='h5ad', spatial_key='spatial', spatial_ndim=3, spatial_columns=<factory>, n_obs=0, n_vars=0, obs_names=<factory>, obs_attrs=<factory>, gene_names=<factory>, gene_attrs=<factory>, layer=None, categories=<factory>, dtypes=<factory>, row_attr=None, obs_index=None, object_id_column=None, object_id_categories=None, position_names=None)
Parameters:
Return type:

None

attr_to_gene()[source]

Map stored attribute name -> original var (gene) name.

Return type:

dict[str, str]

attr_to_obs()[source]

Map stored attribute name -> original obs column label.

Return type:

dict[str, str]

categories: dict[str, list[str]]
dtypes: dict[str, str]
format_name: str = 'h5ad'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

H5ADHeader

gene_attrs: list[str]
gene_names: list[str]
layer: str | None = None
n_obs: int = 0
n_vars: int = 0
object_id_categories: list[str] | None = None
object_id_column: str | None = None
obs_attrs: list[str]
obs_index: list[str] | None = None
obs_names: list[str]
position_names: list[str] | None = None
row_attr: str | None = None
spatial_columns: list[int]
spatial_key: str = 'spatial'
spatial_ndim: int = 3
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.Header(format_name)[source]

Bases: object

Base class for format-specific headers.

Parameters:

format_name (str)

__init__(format_name)
Parameters:

format_name (str)

Return type:

None

format_name: str
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

Header

to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.LASHeader(format_name='las', version='1.4', point_format=0, point_count=0, scale=(0.001, 0.001, 0.001), offset=(0.0, 0.0, 0.0), min_bound=(0.0, 0.0, 0.0), max_bound=(0.0, 0.0, 0.0), crs_wkt='')[source]

Bases: Header

LAS/LAZ point cloud file header.

Parameters:
__init__(format_name='las', version='1.4', point_format=0, point_count=0, scale=(0.001, 0.001, 0.001), offset=(0.0, 0.0, 0.0), min_bound=(0.0, 0.0, 0.0), max_bound=(0.0, 0.0, 0.0), crs_wkt='')
Parameters:
Return type:

None

crs_wkt: str = ''
format_name: str = 'las'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

LASHeader

max_bound: tuple[float, float, float] = (0.0, 0.0, 0.0)
min_bound: tuple[float, float, float] = (0.0, 0.0, 0.0)
offset: tuple[float, float, float] = (0.0, 0.0, 0.0)
point_count: int = 0
point_format: int = 0
scale: tuple[float, float, float] = (0.001, 0.001, 0.001)
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

version: str = '1.4'
class zarr_vectors_tools.headers.formats.NIfTIHeader(format_name='nifti', affine=None, dimensions=(1, 1, 1), voxel_sizes=(1.0, 1.0, 1.0), qform_code=0, sform_code=0, xyzt_units=0)[source]

Bases: Header

NIfTI spatial reference header.

Stores the affine transform, voxel sizes, and dimension info. Used by any geometry type with a known coordinate system.

Parameters:
__init__(format_name='nifti', affine=None, dimensions=(1, 1, 1), voxel_sizes=(1.0, 1.0, 1.0), qform_code=0, sform_code=0, xyzt_units=0)
Parameters:
Return type:

None

affine: list[float] | None = None
property affine_matrix: ndarray | None
dimensions: tuple[int, ...] = (1, 1, 1)
format_name: str = 'nifti'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

NIfTIHeader

qform_code: int = 0
sform_code: int = 0
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

voxel_sizes: tuple[float, ...] = (1.0, 1.0, 1.0)
xyzt_units: int = 0
class zarr_vectors_tools.headers.formats.OBJHeader(format_name='obj', mtllib='', object_names=<factory>, group_names=<factory>)[source]

Bases: Header

Wavefront OBJ file header metadata.

Parameters:
__init__(format_name='obj', mtllib='', object_names=<factory>, group_names=<factory>)
Parameters:
Return type:

None

format_name: str = 'obj'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

OBJHeader

group_names: list[str]
mtllib: str = ''
object_names: list[str]
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.SWCHeader(format_name='swc', comment_lines=<factory>, coordinate_space='', scaling=(1.0, 1.0, 1.0))[source]

Bases: Header

SWC file header (comment lines and metadata).

Parameters:
__init__(format_name='swc', comment_lines=<factory>, coordinate_space='', scaling=(1.0, 1.0, 1.0))
Parameters:
Return type:

None

comment_lines: list[str]
coordinate_space: str = ''
format_name: str = 'swc'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

SWCHeader

scaling: tuple[float, float, float] = (1.0, 1.0, 1.0)
to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.SurfaceHeader(format_name='surface', source='', space='unknown', geometry='', hemispheres=<factory>, alternates=<factory>, scalars=<factory>, label_tables=<factory>, key_attribute='zv_join_key', c_ras=None, extra=<factory>)[source]

Bases: Header

What a cortical surface store needs that its arrays cannot say.

A surface store holds one mesh object per hemisphere. Four things about it live here rather than in the arrays:

  • Which hemisphere is which object, and how many vertices the source mesh had. CIFTI data is indexed by surface vertex, and a 32k map is meaningless on a 164k surface, so the count is what lets an attach refuse a mismatch instead of writing garbage.

  • Which surface is the geometry and which ride along as coordinate attributes (coords_white, coords_inflated…). All share one topology; only one can be chunked.

  • The coordinate space the geometry is in. FreeSurfer surfaces are in surface RAS, offset from scanner RAS by c_ras; whether that shift was applied is the difference between a surface that overlays a tractogram and one that sits beside it.

  • Label tables. Parcellations are stored as integer codes; the names and colours that make them a parcellation are kept per attribute.

Parameters:
__init__(format_name='surface', source='', space='unknown', geometry='', hemispheres=<factory>, alternates=<factory>, scalars=<factory>, label_tables=<factory>, key_attribute='zv_join_key', c_ras=None, extra=<factory>)
Parameters:
Return type:

None

alternates: dict[str, str]

Other surfaces of the same topology, as {attribute: surface name}.

c_ras: list[float] | None = None

The FreeSurfer c_ras offset, when the geometry carried one.

extra: dict[str, Any]

Anything a producer wrote that this class does not name.

format_name: str = 'surface'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

SurfaceHeader

geometry: str = ''

The surface chunked as geometry, e.g. "midthickness".

hemispheres: list[dict[str, Any]]

{object_id, hemisphere, structure, n_vertices, n_faces}. object_id is the store’s; hemisphere is "left"/"right".

Type:

One entry per object

key_attribute: str = 'zv_join_key'

Per-vertex attribute holding hemisphere << 32 | source vertex.

label_tables: dict[str, dict[str, dict[str, Any]]]

{attribute: {code: {"name": str, "rgba": [r, g, b, a]}}}. Codes are strings because JSON object keys must be.

object_for(hemisphere)[source]

The store’s object id for "left" or "right", if present.

Parameters:

hemisphere (str)

Return type:

int | None

scalars: dict[str, str]

Continuous per-vertex maps, as {attribute: source description}.

source: str = ''

"gifti" or "freesurfer".

Type:

Where the geometry came from

space: str = 'unknown'

"scanner" (world RAS mm), "surface" (FreeSurfer tkr RAS), or the GIFTI dataspace name when the file declares one.

to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

class zarr_vectors_tools.headers.formats.TRKHeader(format_name='trk', voxel_size=(1.0, 1.0, 1.0), dimensions=(1, 1, 1), vox_to_ras=None, voxel_order='LAS', n_scalars=0, scalar_names=<factory>, n_properties=0, property_names=<factory>, n_count=0, origin=None, version=None, space=None, n_count_mismatch=None, extra=<factory>)[source]

Bases: Header

TrackVis .trk file header.

Captures the vox_to_ras affine, voxel sizes, dimensions, voxel order, and scalar/property names needed for round-trip.

Parameters:
__init__(format_name='trk', voxel_size=(1.0, 1.0, 1.0), dimensions=(1, 1, 1), vox_to_ras=None, voxel_order='LAS', n_scalars=0, scalar_names=<factory>, n_properties=0, property_names=<factory>, n_count=0, origin=None, version=None, space=None, n_count_mismatch=None, extra=<factory>)
Parameters:
Return type:

None

property affine: ndarray | None

Return the vox_to_ras affine as a 4×4 numpy array.

dimensions: tuple[int, int, int] = (1, 1, 1)
extra: dict[str, Any]

Any key a producer wrote that this class does not name. Carried so a newer writer’s field survives a round-trip through an older reader instead of being dropped on the floor.

format_name: str = 'trk'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

TRKHeader

n_count: int = 0
n_count_mismatch: list[int] | None = None

[declared, actual] when the header’s streamline count disagreed with what the file holds.

n_properties: int = 0
n_scalars: int = 0
origin: list[float] | None = None

TrackVis origin field, as written by the file.

property_names: list[str]
scalar_names: list[str]
space: str | None = None

Which space the STORED coordinates are in – "voxmm" (as the file had them) or "rasmm" (registered at read time). Without it a later merge or export cannot tell whether the affine still has to be applied, which is the difference between two tractograms overlaying and one of them sitting in the wrong place entirely.

to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

version: int | None = None

TrackVis format version.

vox_to_ras: list[float] | None = None
voxel_order: str = 'LAS'
voxel_size: tuple[float, float, float] = (1.0, 1.0, 1.0)
class zarr_vectors_tools.headers.formats.TRXHeader(format_name='trx', voxel_to_rasmm=None, dimensions=(1, 1, 1), dpv_names=<factory>, dps_names=<factory>, dpg_names=<factory>, extra=<factory>)[source]

Bases: Header

What a TRX file says about the image its streamlines belong to.

TRX positions are always RAS millimetres, so unlike TRK there is no stored space to undo. What an export needs back is the reference image – its voxel-to-RAS affine and grid dimensions – because tools that load a TRX against a volume check the two agree.

Parameters:
__init__(format_name='trx', voxel_to_rasmm=None, dimensions=(1, 1, 1), dpv_names=<factory>, dps_names=<factory>, dpg_names=<factory>, extra=<factory>)
Parameters:
Return type:

None

property affine: ndarray | None

The reference affine as a 4x4 numpy array.

dimensions: tuple[int, int, int] = (1, 1, 1)

The reference image’s grid dimensions.

dpg_names: list[str]
dps_names: list[str]
dpv_names: list[str]

Names of the per-vertex (dpv), per-streamline (dps) and per-group (dpg) arrays the file had, as ingested.

extra: dict[str, Any]
format_name: str = 'trx'
classmethod from_dict(d)[source]

Deserialise from a dict.

Parameters:

d (dict[str, Any])

Return type:

TRXHeader

to_dict()[source]

Serialise to a JSON-compatible dict.

Return type:

dict[str, Any]

voxel_to_rasmm: list[float] | None = None

The reference image’s voxel-to-RAS affine, flattened 4x4.

zarr_vectors_tools.headers.formats.header_from_dict(d)[source]

Deserialise a header dict, dispatching to the correct class.

Parameters:

d (dict[str, Any])

Return type:

Header