|
1 | | -# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- |
2 | | -# vi: set ft=python sts=4 ts=4 sw=4 et: |
3 | | -### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
4 | | -# |
5 | | -# See COPYING file distributed along with the NiBabel package for the |
6 | | -# copyright and license terms. |
7 | | -# |
8 | | -### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
9 | | -"""Read/write x5 transforms.""" |
| 1 | +"""Data structures for the X5 transform format.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import Any, Dict, Optional, Sequence, List |
| 7 | + |
| 8 | +import json |
| 9 | +import h5py |
10 | 10 |
|
11 | 11 | import numpy as np |
12 | | -from h5py import File as H5File |
13 | 12 |
|
14 | 13 |
|
15 | | -def to_filename(fname, xfm, moving=None): |
16 | | - """Store the transform in BIDS-Transforms HDF5 file format (.x5).""" |
17 | | - with H5File(fname, "w") as out_file: |
18 | | - out_file.attrs["Format"] = "X5" |
19 | | - out_file.attrs["Version"] = np.uint16(1) |
20 | | - x5_root = out_file.create_group("/0") |
| 14 | +@dataclass |
| 15 | +class X5Domain: |
| 16 | + """Domain information of a transform.""" |
21 | 17 |
|
22 | | - # Serialize this object into the x5 file format. |
23 | | - transform_group = x5_root.create_group("TransformGroup") |
| 18 | + grid: bool |
| 19 | + size: Sequence[int] |
| 20 | + mapping: np.ndarray |
| 21 | + coordinates: Optional[str] = None |
24 | 22 |
|
25 | | - # Group '0' containing Affine transform |
26 | | - transform_0 = transform_group.create_group("0") |
27 | 23 |
|
28 | | - transform_0.attrs["Type"] = "Affine" |
29 | | - transform_0.create_dataset("Transform", data=xfm.matrix) |
30 | | - transform_0.create_dataset("Inverse", data=~xfm) |
| 24 | +@dataclass |
| 25 | +class X5Transform: |
| 26 | + """Represent one transform entry of an X5 file.""" |
31 | 27 |
|
32 | | - metadata = {"key": "value"} |
33 | | - transform_0.attrs["Metadata"] = str(metadata) |
| 28 | + type: str |
| 29 | + transform: np.ndarray |
| 30 | + dimension_kinds: Sequence[str] |
| 31 | + array_length: int = 1 |
| 32 | + domain: Optional[X5Domain] = None |
| 33 | + subtype: Optional[str] = None |
| 34 | + representation: Optional[str] = None |
| 35 | + metadata: Optional[Dict[str, Any]] = None |
| 36 | + inverse: Optional[np.ndarray] = None |
| 37 | + jacobian: Optional[np.ndarray] = None |
| 38 | + additional_parameters: Optional[np.ndarray] = None |
| 39 | + |
| 40 | + |
| 41 | +def to_filename(fname: str, x5_list: List[X5Transform]): |
| 42 | + """Write a list of :class:`X5Transform` objects to an X5 HDF5 file.""" |
| 43 | + with h5py.File(str(fname), "w") as out_file: |
| 44 | + out_file.attrs["Format"] = "X5" |
| 45 | + out_file.attrs["Version"] = np.uint16(1) |
| 46 | + tg = out_file.create_group("TransformGroup") |
| 47 | + for i, node in enumerate(x5_list): |
| 48 | + g = tg.create_group(str(i)) |
| 49 | + g.attrs["Type"] = node.type |
| 50 | + g.attrs["ArrayLength"] = node.array_length |
| 51 | + if node.subtype is not None: |
| 52 | + g.attrs["SubType"] = node.subtype |
| 53 | + if node.representation is not None: |
| 54 | + g.attrs["Representation"] = node.representation |
| 55 | + if node.metadata is not None: |
| 56 | + g.attrs["Metadata"] = json.dumps(node.metadata) |
| 57 | + g.create_dataset("Transform", data=node.transform) |
| 58 | + g.create_dataset( |
| 59 | + "DimensionKinds", |
| 60 | + data=np.asarray(node.dimension_kinds, dtype="S"), |
| 61 | + ) |
| 62 | + if node.domain is not None: |
| 63 | + dgrp = g.create_group("Domain") |
| 64 | + dgrp.create_dataset( |
| 65 | + "Grid", data=np.uint8(1 if node.domain.grid else 0) |
| 66 | + ) |
| 67 | + dgrp.create_dataset("Size", data=np.asarray(node.domain.size)) |
| 68 | + dgrp.create_dataset("Mapping", data=node.domain.mapping) |
| 69 | + if node.domain.coordinates is not None: |
| 70 | + dgrp.attrs["Coordinates"] = node.domain.coordinates |
34 | 71 |
|
35 | | - # sub-group 'Domain' contained within group '0' |
36 | | - transform_0.create_group("Domain") |
37 | | - # domain_group.attrs["Grid"] = self._grid |
38 | | - # domain_group.create_dataset("Size", data=_as_homogeneous(self._reference.shape)) |
39 | | - # domain_group.create_dataset("Mapping", data=self.mapping) |
| 72 | + if node.inverse is not None: |
| 73 | + g.create_dataset("Inverse", data=node.inverse) |
| 74 | + if node.jacobian is not None: |
| 75 | + g.create_dataset("Jacobian", data=node.jacobian) |
| 76 | + if node.additional_parameters is not None: |
| 77 | + g.create_dataset( |
| 78 | + "AdditionalParameters", data=node.additional_parameters |
| 79 | + ) |
| 80 | + return str(fname) |
0 commit comments