mrpro.data.IData

class mrpro.data.IData(data: Tensor, header: IHeader)[source]

Bases: Data

MR image data (IData) class.

__init__(data: Tensor, header: IHeader) None

Methods

__init__(data, header)

clone()

Return a deep copy of the object.

cpu(*[, memory_format, copy])

Put in CPU memory.

cuda([device, non_blocking, memory_format, copy])

Put object in CUDA memory.

double(*[, memory_format, copy])

Convert all float tensors to double precision.

from_dicom_files(filenames)

Read multiple DICOM files and return IData object.

from_dicom_folder(foldername[, suffix])

Read all DICOM files from a folder and return IData object.

from_single_dicom(filename)

Read single DICOM file and return IData object.

from_tensor_and_kheader(data, kheader)

Create IData object from a tensor and a KHeader object.

half(*[, memory_format, copy])

Convert all float tensors to half precision.

rss([keepdim])

Root-sum-of-squares over coils image data.

single(*[, memory_format, copy])

Convert all float tensors to single precision.

to(*args, **kwargs)

Perform dtype and/or device conversion of data.

Attributes

data

Data.

device

Return the device of the tensors.

header

Header for image data.

is_cpu

Return True if all tensors are on the CPU.

is_cuda

Return True if all tensors are on a single CUDA device.

clone() Self

Return a deep copy of the object.

cpu(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self

Put in CPU memory.

Parameters:
  • memory_format – The desired memory format of returned tensor.

  • copy – If True, the returned tensor will always be a copy, even if the input was already on the correct device. This will also create new tensors for views

cuda(device: device | str | int | None = None, *, non_blocking: bool = False, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self

Put object in CUDA memory.

Parameters:
  • device – The destination GPU device. Defaults to the current CUDA device.

  • non_blocking – If True and the source is in pinned memory, the copy will be asynchronous with respect to the host. Otherwise, the argument has no effect.

  • memory_format – The desired memory format of returned tensor.

  • copy – If True, the returned tensor will always be a copy, even if the input was already on the correct device. This will also create new tensors for views

data: Tensor

Data. Shape (…other coils k2 k1 k0)

property device: device | None

Return the device of the tensors.

Looks at each field of a dataclass implementing a device attribute, such as torch.Tensors or MoveDataMixin instances. If the devices of the fields differ, an InconsistentDeviceError is raised, otherwise the device is returned. If no field implements a device attribute, None is returned.

Raises:

InconsistentDeviceError: – If the devices of different fields differ.

Return type:

The device of the fields or None if no field implements a device attribute.

double(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self

Convert all float tensors to double precision.

converts float to float64 and complex to complex128

Parameters:
  • memory_format – The desired memory format of returned tensor.

  • copy – If True, the returned tensor will always be a copy, even if the input was already on the correct device. This will also create new tensors for views

classmethod from_dicom_files(filenames: Sequence[str] | Sequence[Path] | Generator[Path, None, None]) Self[source]

Read multiple DICOM files and return IData object.

Parameters:

filenames – List of DICOM filenames.

classmethod from_dicom_folder(foldername: str | Path, suffix: str | None = 'dcm') Self[source]

Read all DICOM files from a folder and return IData object.

Parameters:
  • foldername – path to folder with DICOM files.

  • suffix – file extension (without period/full stop) to identify the DICOM files. If None, then all files in the folder are read in.

classmethod from_single_dicom(filename: str | Path) Self[source]

Read single DICOM file and return IData object.

Parameters:

filename – path to DICOM file.

classmethod from_tensor_and_kheader(data: Tensor, kheader: KHeader) Self[source]

Create IData object from a tensor and a KHeader object.

Parameters:
  • data – torch.Tensor containing image data with dimensions (broadcastable to) (other, coils, z, y, x).

  • kheader – MR raw data header (KHeader) containing required meta data for the image header (IHeader).

half(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self

Convert all float tensors to half precision.

converts float to float16 and complex to complex32

Parameters:
  • memory_format – The desired memory format of returned tensor.

  • copy – If True, the returned tensor will always be a copy, even if the input was already on the correct device. This will also create new tensors for views

header: IHeader

Header for image data.

property is_cpu: bool

Return True if all tensors are on the CPU.

Checks all tensor attributes of the dataclass for their device, (recursively if an attribute is a MoveDataMixin)

Returns False if not all tensors are on cpu or if the device is inconsistent, returns True if the data class has no tensors as attributes.

property is_cuda: bool

Return True if all tensors are on a single CUDA device.

Checks all tensor attributes of the dataclass for their device, (recursively if an attribute is a MoveDataMixin)

Returns False if not all tensors are on the same CUDA devices, or if the device is inconsistent, returns True if the data class has no tensors as attributes.

rss(keepdim: bool = False) Tensor[source]

Root-sum-of-squares over coils image data.

Parameters:

keepdim – if True, the output tensor has the same number of dimensions as the data tensor, and the coil dimension is kept as a singleton dimension. If False, the coil dimension is removed.

Return type:

image data tensor with shape (…, 1, z, y, x) if keepdim is True or (…, z, y, x) if keepdim is False.

single(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self

Convert all float tensors to single precision.

converts float to float32 and complex to complex64

Parameters:
  • memory_format – The desired memory format of returned tensor.

  • copy – If True, the returned tensor will always be a copy, even if the input was already on the correct device. This will also create new tensors for views

to(*args, **kwargs) Self

Perform dtype and/or device conversion of data.

A torch.dtype and torch.device are inferred from the arguments args and kwargs. Please have a look at the documentation of torch.Tensor.to() for more details.

A new instance of the dataclass will be returned.

The conversion will be applied to all Tensor- or Module fields of the dataclass, and to all fields that implement the MoveDataMixin.

The dtype-type, i.e. float or complex will always be preserved, but the precision of floating point dtypes might be changed.

Example: If called with dtype=torch.float32 OR dtype=torch.complex64:

  • A complex128 tensor will be converted to complex64

  • A float64 tensor will be converted to float32

  • A bool tensor will remain bool

  • An int64 tensor will remain int64

If other conversions are desired, please use the torch.Tensor.to() method of the fields directly.

If the copy argument is set to True (default), a deep copy will be returned even if no conversion is necessary. If two fields are views of the same data before, in the result they will be independent copies if copy is set to True or a conversion is necessary. If set to False, some Tensors might be shared between the original and the new object.