|
1 |
| -"""Data structures for the X5 transform format.""" |
| 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 | +""" |
| 10 | +Data structures for the X5 transform format. |
| 11 | +
|
| 12 | +Implements what's drafted in the `BIDS X5 specification draft |
| 13 | +<https://docs.google.com/document/d/1yk5O0QTAOXLdP9iSG3W8ta7IcMFypu2106c3Pnjfi-4/edit>`__. |
| 14 | +
|
| 15 | +""" |
2 | 16 |
|
3 | 17 | from __future__ import annotations
|
4 | 18 |
|
5 | 19 | from dataclasses import dataclass
|
| 20 | +from pathlib import Path |
6 | 21 | from typing import Any, Dict, Optional, Sequence, List
|
7 | 22 |
|
8 | 23 | import json
|
|
13 | 28 |
|
14 | 29 | @dataclass
|
15 | 30 | class X5Domain:
|
16 |
| - """Domain information of a transform.""" |
| 31 | + """Domain information of a transform representing reference/moving spaces.""" |
17 | 32 |
|
18 | 33 | grid: bool
|
| 34 | + """Whether sampling locations in the manifold are located regularly.""" |
19 | 35 | size: Sequence[int]
|
20 |
| - mapping: np.ndarray |
| 36 | + """The number of sampling locations per dimension (or total if not a grid).""" |
| 37 | + mapping: Optional[np.ndarray] |
| 38 | + """A mapping to go from samples (pixel/voxel coordinates, indices) to space coordinates.""" |
21 | 39 | coordinates: Optional[str] = None
|
| 40 | + """Indexing type of the Mapping field (for example, "cartesian", "spherical" or "index").""" |
22 | 41 |
|
23 | 42 |
|
24 | 43 | @dataclass
|
25 | 44 | class X5Transform:
|
26 | 45 | """Represent one transform entry of an X5 file."""
|
27 | 46 |
|
28 | 47 | type: str
|
| 48 | + """A REQUIRED unicode string with possible values: "linear", "nonlinear", "composite".""" |
29 | 49 | transform: np.ndarray
|
30 |
| - dimension_kinds: Sequence[str] |
31 |
| - array_length: int = 1 |
32 |
| - domain: Optional[X5Domain] = None |
| 50 | + """A REQUIRED array of parameters (e.g., affine matrix, or dense displacements field).""" |
33 | 51 | subtype: Optional[str] = None
|
| 52 | + """An OPTIONAL extension of type to drive the interpretation of AdditionalParameters.""" |
34 | 53 | representation: Optional[str] = None
|
| 54 | + """ |
| 55 | + A string specifiying the transform representation or model, REQUIRED only for nonlinear Type. |
| 56 | + """ |
35 | 57 | metadata: Optional[Dict[str, Any]] = None
|
| 58 | + """An OPTIONAL string (JSON) to embed metadata.""" |
| 59 | + dimension_kinds: Optional[Sequence[str]] = None |
| 60 | + """Identifies what "kind" of information is represented by the samples along each axis.""" |
| 61 | + domain: Optional[X5Domain] = None |
| 62 | + """ |
| 63 | + A dataset specifying the reference manifold for the transform (either |
| 64 | + a regularly gridded 3D space or a surface/sphere). |
| 65 | + REQUIRED for nonlinear Type, RECOMMENDED for linear Type. |
| 66 | + """ |
36 | 67 | inverse: Optional[np.ndarray] = None
|
| 68 | + """Placeholder to pre-calculated inverses.""" |
37 | 69 | jacobian: Optional[np.ndarray] = None
|
38 |
| - additional_parameters: Optional[np.ndarray] = None |
| 70 | + """ |
| 71 | + A RECOMMENDED data array to keep cached the determinant of Jacobian of the transform |
| 72 | + in case tools have calculated it. |
| 73 | + For parametric models it is generally possible to obtain it analytically, so this dataset |
| 74 | + could not be as useful in that case. |
| 75 | + """ |
| 76 | + # additional_parameters: Optional[np.ndarray] = None |
| 77 | + # AdditionalParameters is empty in the draft spec - ignore for now. |
| 78 | + # Only documentation ATM is for SubType: |
| 79 | + # The SubType setting enables setting the additional parameters on a dataset called |
| 80 | + # "AdditionalParameters" that hangs directly from this transform node. |
| 81 | + array_length: int = 1 |
| 82 | + """Undocumented field in the draft to enable a single transform group for 4D transforms.""" |
| 83 | + |
| 84 | + |
| 85 | +def to_filename(fname: str | Path, x5_list: List[X5Transform]): |
| 86 | + """ |
| 87 | + Write a list of :class:`X5Transform` objects to an X5 HDF5 file. |
| 88 | +
|
| 89 | + Parameters |
| 90 | + ---------- |
| 91 | + fname : :obj:`os.pathlike` |
| 92 | + The file name (preferably with the ".x5" extension) in which transforms will be stored. |
| 93 | + x5_list : :obj:`list` |
| 94 | + The list of transforms to be stored in the output dataset. |
39 | 95 |
|
| 96 | + Returns |
| 97 | + ------- |
| 98 | + fname : :obj:`os.pathlike` |
| 99 | + File containing the transform(s). |
40 | 100 |
|
41 |
| -def to_filename(fname: str, x5_list: List[X5Transform]): |
42 |
| - """Write a list of :class:`X5Transform` objects to an X5 HDF5 file.""" |
| 101 | + """ |
43 | 102 | with h5py.File(str(fname), "w") as out_file:
|
44 | 103 | out_file.attrs["Format"] = "X5"
|
45 | 104 | out_file.attrs["Version"] = np.uint16(1)
|
@@ -71,8 +130,9 @@ def to_filename(fname: str, x5_list: List[X5Transform]):
|
71 | 130 | g.create_dataset("Inverse", data=node.inverse)
|
72 | 131 | if node.jacobian is not None:
|
73 | 132 | g.create_dataset("Jacobian", data=node.jacobian)
|
74 |
| - if node.additional_parameters is not None: |
75 |
| - g.create_dataset( |
76 |
| - "AdditionalParameters", data=node.additional_parameters |
77 |
| - ) |
| 133 | + # Disabled until we need SubType and AdditionalParameters |
| 134 | + # if node.additional_parameters is not None: |
| 135 | + # g.create_dataset( |
| 136 | + # "AdditionalParameters", data=node.additional_parameters |
| 137 | + # ) |
78 | 138 | return fname
|
0 commit comments