mrpro.data.CsmData
- class mrpro.data.CsmData
Bases:
IDataCoil sensitivity map class.
- classmethod from_idata_inati(idata: IData, smoothing_width: int | SpatialDimension[int] = 5, chunk_size_otherdim: int | None = None, downsampled_size: int | SpatialDimension[int] | None = None) Self
Create csm object from image data using Inati method.
See also
inati.- Parameters:
idata (
IData) – IData object containing the images for each coil element.smoothing_width (
Union[int,SpatialDimension[int]], default:5) – Size of the smoothing kernel.chunk_size_otherdim (
int|None, default:None) – How many elements of the other dimensions should be processed at once. Default isNone, which means that all elements are processed at once.downsampled_size (
Union[int,SpatialDimension[int],None], default:None) – IData will be downsampled to this size before calculating the csm to speed up the calculation and reduce memory requirements. The final csm will be upsampled to the original size. If set toNoneno downsampling will be performed.
- Returns:
Coil sensitivity maps
- classmethod from_idata_walsh(idata: IData, smoothing_width: int | SpatialDimension[int] = 5, chunk_size_otherdim: int | None = None, downsampled_size: int | SpatialDimension[int] | None = None) Self
Create csm object from image data using Walsh method.
See also
walsh.- Parameters:
idata (
IData) – IData object containing the images for each coil element.smoothing_width (
Union[int,SpatialDimension[int]], default:5) – width of smoothing filter.chunk_size_otherdim (
int|None, default:None) – How many elements of the other dimensions should be processed at once. Default isNone, which means that all elements are processed at once.downsampled_size (
Union[int,SpatialDimension[int],None], default:None) – IData will be downsampled to this size before calculating the csm to speed up the calculation and reduce memory requirements. The final csm will be upsampled to the original size. If set toNoneno downsampling will be performed.
- Returns:
Coil sensitivity maps
- classmethod from_kdata_inati(kdata: KData, noise: KNoise | None = None, smoothing_width: int | SpatialDimension[int] = 5, chunk_size_otherdim: int | None = None, downsampled_size: int | SpatialDimension[int] | None = None) Self
Create csm object from k-space data using Inati method.
See also
inati.- Parameters:
kdata (
KData) – k-space datanoise (
KNoise|None, default:None) – Noise measurement for prewhitening.optional – Noise measurement for prewhitening.
smoothing_width (
Union[int,SpatialDimension[int]], default:5) – width of smoothing filterchunk_size_otherdim (
int|None, default:None) – How many elements of the other dimensions should be processed at once. Default isNone, which means that all elements are processed at once.downsampled_size (
Union[int,SpatialDimension[int],None], default:None) – IData will be downsampled to this size before calculating the csm to speed up the calculation and reduce memory requirements. The final csm will be upsampled to the original size. If set toNoneno downsampling will be performed.
- Returns:
Coil sensitivity maps
- classmethod from_kdata_walsh(kdata: KData, noise: KNoise | None = None, smoothing_width: int | SpatialDimension[int] = 5, chunk_size_otherdim: int | None = None, downsampled_size: int | SpatialDimension[int] | None = None) Self
Create csm object from k-space data using Walsh method.
See also
walsh.- Parameters:
kdata (
KData) – k-space datanoise (
KNoise|None, default:None) – Noise measurement for prewhitening.optional – Noise measurement for prewhitening.
smoothing_width (
Union[int,SpatialDimension[int]], default:5) – width of smoothing filterchunk_size_otherdim (
int|None, default:None) – How many elements of the other dimensions should be processed at once. Default isNone, which means that all elements are processed at once.downsampled_size (
Union[int,SpatialDimension[int],None], default:None) – IData will be downsampled to this size before calculating the csm to speed up the calculation and reduce memory requirements. The final csm will be upsampled to the original size. If set toNoneno downsampling will be performed.
- Returns:
Coil sensitivity maps
- classmethod from_dicom_files(filenames: Iterable[str | Path] | str | Path) Self
Read multiple DICOM files and return IData object.
DICOM images can be saved as single-frame or multi-frame images [DCMMF].
If the DICOM files are single-frame, we treat each file separately and stack them along the
otherdimension. If the DICOM files are multi-frame and the MRAcquisitionType is3Dwe treat the frame dimension as thezdimension. Otherwise, we move the frame dimension to theotherdimension. Multiple multi-frame DICOM images are stacked along an additionalotherdimension before the frame dimension. Providing the list of files sorted by filename usually leads to a reasonable sorting of the data.- Parameters:
filenames (
Iterable[str|Path] |str|Path) – Filename or sequence of DICOM filenames.
References
- classmethod from_dicom_folder(foldername: str | Path, suffix: str | None = 'dcm') Self
Read all DICOM files from a folder and return IData object.
- classmethod from_tensor_and_kheader(data: Tensor, header: KHeader) Self
Create IData object from a tensor and a KHeader object.
- data: torch.Tensor
Tensor containing image data with dimensions
(*other, coils, z, y, x).
- header: IHeader
Header for image data.
- property device: device | None
Return the device of the tensors.
Looks at each field of a dataclass implementing a device attribute, such as
torch.TensororDataclassinstances. If the devices of the fields differ, anInconsistentDeviceErroris 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.
- Returns:
The device of the fields or
Noneif no field implements adeviceattribute.
- 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
Dataclass)Returns
Falseif not all tensors are on cpu or if the device is inconsistent, returnsTrueif the data class has no tensors as attributes.
- property is_cuda: bool
Return
Trueif all tensors are on a single CUDA device.Checks all tensor attributes of the dataclass for their device, (recursively if an attribute is a
Dataclass)Returns
Falseif not all tensors are on the same CUDA devices, or if the device is inconsistent, returnsTrueif the data class has no tensors as attributes.
- property ndim: int
Return the number of dimensions of the dataclass.
This is the number of dimensions of the broadcasted shape of all fields.
- property shape: Size
Return the broadcasted shape of all tensor/data fields.
Each field of this dataclass is broadcastable to this shape.
- Returns:
The broadcasted shape of all fields.
- Raises:
InconsistentShapeError – If the shapes cannot be broadcasted.
- as_operator() SensitivityOp
Create SensitivityOp using a copy of the CSMs.
- apply(function: Callable[[Any], Any] | None = None, *, recurse: bool = True) Self
Apply a function to all children. Returns a new object.
- apply_(function: Callable[[Any], Any] | None = None, *, memo: dict[int, Any] | None = None, recurse: bool = True) Self
Apply a function to all children in-place.
- Parameters:
function (
Callable[[Any],Any] |None, default:None) – The function to apply to all fields.Noneis interpreted as a no-op.memo (
dict[int,Any] |None, default:None) – A dictionary to keep track of objects that the function has already been applied to, to avoid multiple applications. This is useful if the object has a circular reference.recurse (
bool, default:True) – IfTrue, the function will be applied to all children that areDataclassinstances.
- concatenate(*others: Self, dim: int) Self
Concatenate other instances to the current instance.
Only tensor-like fields will be concatenated in the specified dimension. List fields will be concatenated as a list. Other fields will be ignored.
- Parameters:
others (
Self) – other instance to concatnate.dim (
int) – The dimension to concatenate along.
- Returns:
The concatenated dataclass.
- cpu(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self
Put in CPU memory.
- Parameters:
memory_format (
memory_format, default:torch.preserve_format) – The desired memory format of returned tensor.copy (
bool, default:False) – IfTrue, 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 (
device|str|int|None, default:None) – The destination GPU device. Defaults to the current CUDA device.non_blocking (
bool, default:False) – IfTrueand the source is in pinned memory, the copy will be asynchronous with respect to the host. Otherwise, the argument has no effect.memory_format (
memory_format, default:torch.preserve_format) – The desired memory format of returned tensor.copy (
bool, default:False) – IfTrue, 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.
- detach() Self
Detach the data from the autograd graph.
- Returns:
A new dataclass with the data detached from the autograd graph. The data is shared between the original and the new object. Use
detach().clone()to create an independent copy.
- double(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self
Convert all float tensors to double precision.
converts
floattofloat64andcomplextocomplex128- Parameters:
memory_format (
memory_format, default:torch.preserve_format) – The desired memory format of returned tensor.copy (
bool, default:False) – IfTrue, 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.
- half(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self
Convert all float tensors to half precision.
converts
floattofloat16andcomplextocomplex32- Parameters:
memory_format (
memory_format, default:torch.preserve_format) – The desired memory format of returned tensor.copy (
bool, default:False) – IfTrue, 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.
- rearrange(pattern: str, **axes_lengths: int) Self
Rearrange the data according to the specified pattern.
Similar to
einops.rearrange, allowing flexible rearrangement of data dimensions.Examples
>>> # Split the phase encode lines into 8 cardiac phases >>> data.rearrange('batch coils k2 (phase k1) k0 -> batch phase coils k2 k1 k0', phase=8) >>> # Split the k-space samples into 64 k1 and 64 k2 lines >>> data.rearrange('... 1 1 (k2 k1 k0) -> ... k2 k1 k0', k2=64, k1=64, k0=128)
- Parameters:
pattern (
str) – String describing the rearrangement pattern. Seeeinops.rearrangeand the examples above for more details.**axes_lengths (dict) – Optional dictionary mapping axis names to their lengths. Used when pattern contains unknown dimensions.
- Returns:
The rearranged data with the same type as the input.
- rss(keepdim: bool = False) Tensor
Root-sum-of-squares over coils image data.
- Parameters:
keepdim (
bool, default:False) – ifTrue, the output tensor has the same number of dimensions as the data tensor, and the coil dimension is kept as a singleton dimension. IfFalse, the coil dimension is removed.- Returns:
image data tensor with shape
(..., 1, z, y, x)ifkeepdimisTrueor(..., z, y, x)if keepdim isFalse.
- single(*, memory_format: memory_format = torch.preserve_format, copy: bool = False) Self
Convert all float tensors to single precision.
converts
floattofloat32andcomplextocomplex64- Parameters:
memory_format (
memory_format, default:torch.preserve_format) – The desired memory format of returned tensor.copy (
bool, default:False) – IfTrue, 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.
- split(dim: int, size: int = 1, overlap: int = 0, dilation: int = 1) tuple[Self, ...]
Split the dataclass along a dimension.
- Parameters:
Examples
If the dimension has 6 elements:
split with size 2, overlap 0, dilation 1 -> elements (0,1), (2,3), and (4,5)
split with size 2, overlap 1, dilation 1 -> elements (0,1), (1,2), (2,3), (3,4), (4,5), and (5,6)
split with size 2, overlap 0, dilation 2 -> elements (0,2), and (3,5)
split with size 2, overlap -1, dilation 1 -> elements (0,1), and (3,4)
- Returns:
A tuple of the splits.
- stack(*others: Self) Self
Stack other along new first dimension.
- Parameters:
others (
Self) – other instance to stack.
- to(device: str | device | int | None = None, dtype: dtype | None = None, non_blocking: bool = False, *, copy: bool = False, memory_format: memory_format | None = None) Self
- to(dtype: dtype, non_blocking: bool = False, *, copy: bool = False, memory_format: memory_format | None = None) Self
- to(tensor: Tensor, non_blocking: bool = False, *, copy: bool = False, memory_format: memory_format | None = None) Self
Perform dtype and/or device conversion of data.
A
torch.dtypeandtorch.deviceare inferred from the arguments args and kwargs. Please have a look at the documentation oftorch.Tensor.tofor 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
Dataclass.The dtype-type, i.e. float or complex will always be preserved, but the precision of floating point dtypes might be changed.
Examples
If called with
dtype=torch.float32ORdtype=torch.complex64:A
complex128tensor will be converted tocomplex64A
float64tensor will be converted tofloat32A
booltensor will remainboolAn
int64tensor will remainint64
If other conversions are desired, please use the
tomethod 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 toTrueor a conversion is necessary. If set toFalse, some Tensors might be shared between the original and the new object.
- to_dicom_folder(foldername: str | Path, *, series_description: str | None = None, reference_patient_table_position: SpatialDimension | None = None, rescale_slope: float = 1.0, rescale_intercept: float = 0.0, normalize_data: bool = True) None
Write the image data to DICOM files in a folder.
The data is always saved in a multi-frame DICOM files.
- Parameters:
series_description (
str|None, default:None) – String to be saved as the series description in the DICOM files.reference_patient_table_position (
SpatialDimension|None, default:None) – If provided, the image position is calculated relative to this table positiion. This ensures that the image position is consistent across different scans even if the patient table has moved.rescale_slope (
float, default:1.0) – Slope of linear scaling of data. Data is save as (data - intercept)/slope.rescale_intercept (
float, default:0.0) – Intercept of linear scaling of data. Data is save as (data - intercept)/slope.normalize_data (
bool, default:True) – Normalize data prior to applying scaling.
- __eq__(other: object) bool
Check deep equality of two dataclasses.
Tests equality up to broadcasting.
- __new__(**kwargs)