2020#
2121# https://www.nipreps.org/community/licensing/
2222#
23- """Representing data in hard-disk and memory."""
23+ """Four-dimensional data representation in hard-disk and memory."""
2424
2525from __future__ import annotations
2626
@@ -106,8 +106,12 @@ def __getitem__(
106106
107107 Returns
108108 -------
109- :obj:`tuple`
110- The selected volume(s) and corresponding affine(s).
109+ volumes : :obj:`~numpy.ndarray`
110+ The selected data subset.
111+ If ``idx`` is a single integer, this will have shape ``(X, Y, Z)``,
112+ otherwise it may have shape ``(X, Y, Z, k)``.
113+ motion_affine : :obj:`~numpy.ndarray` or ``None``
114+ The corresponding per-volume motion affine(s) or ``None`` if identity transform(s).
111115
112116 """
113117 if self .dataobj is None :
@@ -116,6 +120,27 @@ def __getitem__(
116120 affine = self .motion_affines [idx ] if self .motion_affines is not None else None
117121 return self .dataobj [..., idx ], affine
118122
123+ @classmethod
124+ def from_filename (cls , filename : Path | str ) -> BaseDataset :
125+ """
126+ Read an HDF5 file from disk and create a BaseDataset.
127+
128+ Parameters
129+ ----------
130+ filename : :obj:`os.pathlike`
131+ The HDF5 file path to read.
132+
133+ Returns
134+ -------
135+ :obj:`~nifreeze.data.base.BaseDataset`
136+ The constructed dataset with data loaded from the file.
137+
138+ """
139+ with h5py .File (filename , "r" ) as in_file :
140+ root = in_file ["/0" ]
141+ data = {k : np .asanyarray (v ) for k , v in root .items () if not k .startswith ("_" )}
142+ return cls (** data )
143+
119144 def get_filename (self ) -> Path :
120145 """Get the filepath of the HDF5 file."""
121146 return self ._filepath
@@ -131,7 +156,7 @@ def set_transform(self, index: int, affine: np.ndarray, order: int = 3) -> None:
131156 affine : :obj:`numpy.ndarray`
132157 The 4x4 affine matrix to be applied.
133158 order : :obj:`int`, optional
134- The order of the spline interpolation. Default is 3.
159+ The order of the spline interpolation.
135160
136161 """
137162 reference = namedtuple ("ImageGrid" , ("shape" , "affine" ))(
@@ -158,7 +183,7 @@ def set_transform(self, index: int, affine: np.ndarray, order: int = 3) -> None:
158183
159184 # if head motion affines are to be used, initialized to identities
160185 if self .motion_affines is None :
161- self .motion_affines = np .repeat (np .eye ( 4 )[None , ...], len (self ), axis = 0 )
186+ self .motion_affines = np .repeat (np .zeros (( 4 , 4 ) )[None , ...], len (self ), axis = 0 )
162187
163188 self .motion_affines [index ] = xform .matrix
164189
@@ -173,9 +198,11 @@ def to_filename(
173198 filename : :obj:`os.pathlike`
174199 The HDF5 file path to write to.
175200 compression : :obj:`str`, optional
176- Compression filter ('gzip', etc.). Default is None (no compression).
201+ Compression strategy.
202+ See :obj:`~h5py.Group.create_dataset` documentation.
177203 compression_opts : :obj:`typing.Any`, optional
178- Compression level or other compression parameters.
204+ Parameters for compression
205+ `filters <https://docs.h5py.org/en/stable/high/dataset.html#dataset-compression>`__.
179206
180207 """
181208 filename = Path (filename )
@@ -202,11 +229,11 @@ def to_filename(
202229
203230 def to_nifti (self , filename : Path ) -> None :
204231 """
205- Write a NIfTI 1.0 file to disk.
232+ Write a NIfTI file to disk.
206233
207234 Parameters
208235 ----------
209- filename : Path or str
236+ filename : :obj:`os.pathlike`
210237 The output NIfTI file path.
211238
212239 """
@@ -215,27 +242,6 @@ def to_nifti(self, filename: Path) -> None:
215242 nii .header .set_xyzt_units ("mm" )
216243 nii .to_filename (filename )
217244
218- @classmethod
219- def from_filename (cls , filename : Path | str ) -> BaseDataset :
220- """
221- Read an HDF5 file from disk and create a BaseDataset.
222-
223- Parameters
224- ----------
225- filename : :obj:`os.pathlike`
226- The HDF5 file path to read.
227-
228- Returns
229- -------
230- BaseDataset
231- The constructed dataset with data loaded from the file.
232-
233- """
234- with h5py .File (filename , "r" ) as in_file :
235- root = in_file ["/0" ]
236- data = {k : np .asanyarray (v ) for k , v in root .items () if not k .startswith ("_" )}
237- return cls (** data )
238-
239245
240246def load (
241247 filename : Path | str ,
@@ -253,11 +259,11 @@ def load(
253259 A brainmask NIfTI file. If provided, will be loaded and
254260 stored in the returned dataset.
255261 motion_file : :obj:`os.pathlike`
256- A file containing head-motion affine matrices (linear)
262+ A file containing head-motion affine matrices (linear).
257263
258264 Returns
259265 -------
260- BaseDataset
266+ :obj:`~nifreeze.data.base. BaseDataset`
261267 The loaded dataset.
262268
263269 Raises
@@ -266,6 +272,9 @@ def load(
266272 If the file extension is not supported or the file cannot be loaded.
267273
268274 """
275+ if motion_file :
276+ raise NotImplementedError
277+
269278 filename = Path (filename )
270279 if filename .name .endswith (NFDH5_EXT ):
271280 return BaseDataset .from_filename (filename )
@@ -277,7 +286,4 @@ def load(
277286 mask = nb .load (brainmask_file )
278287 retval .brainmask = np .asanyarray (mask .dataobj )
279288
280- if motion_file :
281- raise NotImplementedError
282-
283289 return retval
0 commit comments