Common MRI Utils#

atommic.collections.common.parts.utils.add_coil_dim_if_singlecoil(x: torch.tensor, dim: int = 0) torch.tensor[source]#

Add dummy coil dimension if single coil data.

Parameters
  • x (torch.tensor) – The input data.

  • dim (int) – The dimension to add coil dimension. Default is 0.

Returns

The input data with coil dimension added if single coil.

Return type

torch.tensor

Examples

>>> import torch
>>> from atommic.collections.common.parts.utils import add_coil_dim_if_singlecoil
>>> data = torch.rand(10, 10)
>>> data.shape
(10, 10)
>>> add_coil_dim_if_singlecoil(data).shape
(1, 10, 10)
>>> add_coil_dim_if_singlecoil(data, dim=-1).shape
(10, 10, 1)
atommic.collections.common.parts.utils.apply_mask(x: torch.Tensor, mask_func: Callable, seed: Optional[Union[int, Tuple[int, ...]]] = None, padding: Optional[Sequence[int]] = None, shift: bool = False, partial_fourier_percentage: Optional[float] = 0.0, center_scale: Optional[float] = 0.02, existing_mask: Optional[torch.Tensor] = None) Tuple[Any, Any, int][source]#

Retrospectively accelerate/subsample k-space data by applying a mask to the input data.

Parameters
  • x (torch.Tensor) – The input k-space data. This should have at least 3 dimensions, where dimensions -3 and -2 are the spatial dimensions, and the final dimension has size 2 (for complex values).

  • mask_func (Callable) – A function that takes a shape (tuple of ints) and a random number seed and returns a mask.

  • seed (Optional[Union[int, Tuple[int, ...]]], optional) – Seed for the random number generator. Default is None.

  • padding (Optional[Sequence[int]], optional) – Padding value to apply for mask. Default is None.

  • shift (bool, optional) – Toggle to shift mask when subsampling. Applicable on 2D data. Default is False.

  • partial_fourier_percentage (Optional[float], optional) – Percentage of kspace to be dropped. Default is 0.0.

  • center_scale (Optional[float], optional) – Scale of the center of the mask. Applicable on Gaussian masks. Default is 0.02.

  • existing_mask (Optional[torch.Tensor], optional) – When given, use this mask instead of generating a new one. Default is None.

Returns

Tuple containing the masked k-space data, the mask, and the acceleration factor.

Return type

Tuple[Any, Any, int]

Examples

>>> from atommic.collections.common.parts.utils import apply_mask
>>> import torch
>>> data = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> data.shape
torch.Size([2, 2, 3, 2])
>>> mask = torch.tensor([[[1., 1., 1.], [1., 1., 1.]], [[1., 1., 1.], [1., 1., 1.]]])
>>> mask.shape
torch.Size([2, 2, 3])
>>> apply_mask(data, mask)
(tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],
    [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]]),
tensor([[[1., 1., 1.], [1., 1., 1.]], [[1., 1., 1.], [1., 1., 1.]]]),
6)
>>> masked_data, subsampling_mask, acceleration_factor = apply_mask(data, mask)
>>> masked_data.shape
torch.Size([2, 2, 3, 2])
>>> subsampling_mask.shape
torch.Size([2, 2, 3])
>>> acceleration_factor
6
>>> apply_mask(data, mask, padding=[1, 2], shift=True)
(tensor([[[[0., 0.], [0., 0.], [0., 0.]], [[1., 1.], [2., 2.], [3., 3.]]],
    [[[0., 0.], [0., 0.], [0., 0.]], [[1., 1.], [2., 2.], [3., 3.]]]]),
tensor([[[0., 0., 0.], [1., 1., 1.]], [[0., 0., 0.], [1., 1., 1.]]]),
3)
>>> masked_data, subsampling_mask, acceleration_factor = apply_mask(data, mask, padding=[1, 2], shift=True)
>>> masked_data.shape
torch.Size([2, 2, 3, 2])
>>> subsampling_mask.shape
torch.Size([2, 2, 3])
>>> acceleration_factor
3
atommic.collections.common.parts.utils.batched_mask_center(x: torch.Tensor, mask_from: torch.Tensor, mask_to: torch.Tensor, mask_type: str = '2D') torch.Tensor[source]#

Initializes a mask with the center filled in. Can operate with different masks for each batch element.

Parameters
  • x (torch.Tensor) – The input image or batch of images. This should have at least 3 dimensions, where dimensions -3 and -2 are the spatial dimensions, and the final dimension has size 1 (for real values).

  • mask_from (torch.Tensor) – Part of center to start filling.

  • mask_to (torch.Tensor) – Part of center to end filling.

  • mask_type (str, optional) – Type of mask to apply. Can be either 1D or 2D. Default is 2D.

Returns

The masked image or batch of images with filled center.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import batched_mask_center
>>> import torch
>>> data = torch.randn(1, 32, 320, 320)
>>> batched_mask_center(data, torch.tensor([140]), torch.tensor([180]))
atommic.collections.common.parts.utils.center_crop(x: torch.Tensor, shape: Tuple[int, int]) torch.Tensor[source]#

Apply a center crop to the input complex image or batch of complex images or real image or batch of real images without a complex dimension.

Parameters
  • x (torch.Tensor) – The input tensor to be center cropped. It should have at least 2 dimensions and the cropping is applied along the last two dimensions.

  • shape (Tuple[int, int]) – The output shape. The shape should be smaller than the corresponding dimensions of data.

Returns

The center cropped image or batch of images.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import center_crop
>>> import torch
>>> data = torch.tensor([[[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]], [[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]]])
>>> data.shape
torch.Size([2, 2, 3])
>>> center_crop(data, (1, 2))
tensor([[[2.+2.j, 3.+3.j]], [[2.+2.j, 3.+3.j]]])
>>> center_crop(data, (1, 2)).shape
torch.Size([2, 1, 2])
atommic.collections.common.parts.utils.center_crop_to_smallest(x: Union[torch.Tensor, numpy.ndarray], y: Union[torch.Tensor, numpy.ndarray]) Tuple[Union[torch.Tensor, numpy.ndarray], Union[torch.Tensor, numpy.ndarray]][source]#

Apply a center crop on the larger image to the size of the smaller.

The minimum is taken over dim=-1 and dim=-2. If x is smaller than y at dim=-1 and y is smaller than x at dim=-2,

then the returned dimension will be a mixture of the two.

Parameters
Returns

Tuple of x and y, cropped to the minimum size.

Return type

Tuple[torch.Tensor or np.ndarray, torch.Tensor or np.ndarray]

Examples

>>> from atommic.collections.common.parts.utils import center_crop_to_smallest
>>> import torch
>>> data1 = torch.tensor([[[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]], [[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]]])
>>> data2 = torch.tensor([[[1+1j, 2+2j, 3+3j, 4+4j, 5+5j], [1+1j, 2+2j, 3+3j, 4+4j, 5+5j]],     [[1+1j, 2+2j, 3+3j, 4+4j, 5+5j], [1+1j, 2+2j, 3+3j, 4+4j, 5+5j], [1+1j, 2+2j, 3+3j, 4+4j, 5+5j]]])
>>> data1.shape
torch.Size([2, 2, 3])
>>> data2.shape
torch.Size([2, 3, 5])
>>> center_crop_to_smallest(data1, data2)
(tensor([[[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]], [[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]]]),     tensor([[[1.+1.j, 2.+2.j, 3.+3.j], [1.+1.j, 2.+2.j, 3.+3.j]],     [[1.+1.j, 2.+2.j, 3.+3.j], [1.+1.j, 2.+2.j, 3.+3.j]]]))
>>> center_crop_to_smallest(data1, data2)[0].shape
torch.Size([2, 2, 3])
>>> center_crop_to_smallest(data1, data2)[1].shape
torch.Size([2, 2, 3])
>>> center_crop_to_smallest(data2, data1)
(tensor([[[1.+1.j, 2.+2.j, 3.+3.j], [1.+1.j, 2.+2.j, 3.+3.j]],     [[1.+1.j, 2.+2.j, 3.+3.j], [1.+1.j, 2.+2.j, 3.+3.j]]]),     tensor([[[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]], [[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]]]))
>>> center_crop_to_smallest(data2, data1)[0].shape
torch.Size([2, 2, 3])
>>> center_crop_to_smallest(data2, data1)[1].shape
torch.Size([2, 2, 3])
atommic.collections.common.parts.utils.check_stacked_complex(x: torch.Tensor) torch.Tensor[source]#

Check if tensor is stacked complex (real & imaginary parts stacked along last dim) and convert it to a combined complex tensor.

Parameters

x (torch.Tensor) – Tensor to check.

Returns

Tensor with stacked complex converted to combined complex.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import check_stacked_complex
>>> import torch
>>> data = torch.tensor([1+1j, 2+2j, 3+3j])
>>> data.shape
torch.Size([3])
>>> data = torch.view_as_real(data)
>>> data.shape
>>> check_stacked_complex(data)
tensor([1.+1.j, 2.+2.j, 3.+3.j])
>>> check_stacked_complex(data).shape
torch.Size([3])
>>> data = torch.tensor([1+1j, 2+2j, 3+3j])
>>> data.shape
torch.Size([3])
>>> check_stacked_complex(data)
tensor([1.+1.j, 2.+2.j, 3.+3.j])
>>> check_stacked_complex(data).shape
torch.Size([3])
atommic.collections.common.parts.utils.coil_combination_method(x: torch.Tensor, sensitivity_maps: torch.Tensor, method: str = 'SENSE', dim: int = 0) torch.Tensor[source]#

Selects the coil combination method.

Parameters
  • x (torch.Tensor) – The tensor to coil-combine.

  • sensitivity_maps (torch.Tensor) – The coil sensitivity maps.

  • method (str, optional) – The coil combination method to use. Options are "SENSE", "RSS", "RSS_COMPLEX". Default is "SENSE".

  • dim (int, optional) – The dimension to coil-combine along. Default is 0.

Returns

Coil-combined tensor with the selected method applied.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import coil_combination_method
>>> import torch
>>> data = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> data.shape
torch.Size([2, 2, 3, 2])
>>> coil_sensitivity_maps = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> coil_sensitivity_maps.shape
torch.Size([2, 2, 3, 2])
>>> coil_combination_method(data, coil_sensitivity_maps, method="SENSE")
tensor([[[2.8284, 2.8284],
    [5.6569, 5.6569],
    [8.4853, 8.4853]],
    [[2.8284, 2.8284],
    [5.6569, 5.6569],
    [8.4853, 8.4853]]])
>>> coil_combination_method(data, coil_sensitivity_maps, method="SENSE").shape
torch.Size([2, 3, 2])
>>> coil_combination_method(data, coil_sensitivity_maps, method="RSS")
tensor([[[1.4142, 1.4142],
    [2.8284, 2.8284],
    [4.2426, 4.2426]],
    [[1.4142, 1.4142],
    [2.8284, 2.8284],
    [4.2426, 4.2426]]])
>>> coil_combination_method(data, coil_sensitivity_maps, method="RSS").shape
torch.Size([2, 3, 2])
>>> coil_combination_method(data, coil_sensitivity_maps, method="RSS_COMPLEX")
tensor([[[1.4142, 1.4142],
    [2.8284, 2.8284],
    [4.2426, 4.2426]],
    [[1.4142, 1.4142],
    [2.8284, 2.8284],
    [4.2426, 4.2426]]])
>>> coil_combination_method(data, coil_sensitivity_maps, method="RSS_COMPLEX").shape
torch.Size([2, 3, 2])
atommic.collections.common.parts.utils.complex_abs(x: torch.Tensor) torch.Tensor[source]#

Compute the absolute value of a complex valued input tensor.

Parameters

x (torch.Tensor) – Complex tensor. The last dimension must be of size 2.

Returns

Absolute value of complex tensor.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import complex_abs
>>> import torch
>>> data = torch.tensor([1+1j, 2+2j, 3+3j])
>>> complex_abs(data)
tensor([1.4142, 2.8284, 4.2426])
atommic.collections.common.parts.utils.complex_abs_sq(x: torch.Tensor) torch.Tensor[source]#

Compute the squared absolute value of a complex tensor.

Parameters

x (torch.Tensor) – Complex tensor. The last dimension must be of size 2.

Returns

Squared absolute value of complex tensor.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import complex_abs_sq
>>> import torch
>>> data = torch.tensor([1+1j, 2+2j, 3+3j])
>>> complex_abs_sq(data)
tensor([2., 8., 18.])
atommic.collections.common.parts.utils.complex_center_crop(x: torch.Tensor, shape: Tuple[int, int]) torch.Tensor[source]#

Apply a center crop to the input image or batch of complex images.

Parameters
  • x (torch.Tensor) – The input tensor to be center cropped. It should have at least 3 dimensions and the cropping is applied along the last two dimensions.

  • shape (Tuple[int, int]) – The output shape. The shape should be smaller than the corresponding dimensions of data.

Returns

The complex center cropped image or batch of images.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import complex_center_crop
>>> import torch
>>> data = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> data.shape
torch.Size([2, 2, 3, 2])
>>> complex_center_crop(data, (1, 2))
tensor([[[[2., 2.]]],
    [[[2., 2.]]]])
>>> complex_center_crop(data, (1, 2)).shape
torch.Size([2, 1, 1, 2])
atommic.collections.common.parts.utils.complex_conj(x: torch.Tensor) torch.Tensor[source]#

Complex conjugate.

This applies the complex conjugate assuming that the input array has the last dimension as the complex dimension.

Parameters

x (torch.Tensor) – Complex tensor to apply the complex conjugate to. The last dimension must be of size 2.

Returns

Result of complex conjugate.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import complex_conj
>>> import torch
>>> data = torch.tensor([1+1j, 2+2j, 3+3j])
>>> complex_conj(data)
tensor([1.-1.j, 2.-2.j, 3.-3.j])
atommic.collections.common.parts.utils.complex_mul(x: torch.Tensor, y: torch.Tensor) torch.Tensor[source]#

Complex multiplication.

This multiplies two complex tensors assuming that they are both stored as real arrays with the last dimension being the complex dimension.

Parameters
  • x (torch.Tensor) – First complex tensor to multiply. The last dimension must be of size 2.

  • y (torch.Tensor) – Second complex tensor to multiply. The last dimension must be of size 2.

Returns

Result of complex multiplication.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import complex_mul
>>> import torch
>>> datax = torch.tensor([1+1j, 2+2j, 3+3j])
>>> datay = torch.tensor([4+4j, 5+5j, 6+6j])
>>> complex_mul(datax, datay)
tensor([[-7.+20.j],
        [-4.+16.j],
        [-1.+12.j]])
atommic.collections.common.parts.utils.crop_to_acs(acs_mask: torch.Tensor, kspace: torch.Tensor) torch.Tensor[source]#

Crops k-space to autocalibration region given the acs_mask.

Parameters
  • acs_mask (torch.Tensor) – Autocalibration mask of shape (height, width).

  • kspace (torch.Tensor) – K-space of shape (coil, height, width, *).

Returns

Cropped k-space of shape (coil, height, width, *), where height and width are the new dimensions derived from the acs_mask.

Return type

torch.Tensor

atommic.collections.common.parts.utils.expand_op(x: torch.Tensor, sensitivity_maps: torch.Tensor, dim: int = 1) torch.Tensor[source]#

Expand a coil-combined image to a multi-coil image.

Parameters
  • x (torch.Tensor) – The coil-combined image.

  • sensitivity_maps (torch.Tensor) – The sensitivity maps.

  • dim (int) – The coil dimension to expand. Default is 1.

Returns

The multi-coil image.

Return type

torch.Tensor

Examples

>>> import torch
>>> from atommic.collections.common.parts.utils import expand_op
>>> data = torch.rand(1, 200, 200, 2)
>>> sens = torch.rand(1, 30, 200, 200, 2)
>>> expand_op(data, sens).shape
(1, 30, 200, 200, 2)
atommic.collections.common.parts.utils.is_none(x: Optional[Any]) bool[source]#

Check if input is None or “None”.

Parameters

x (Union[Any, None]) – Input to check.

Returns

True if x is None or “None”, False otherwise.

Return type

bool

Examples

>>> from atommic.collections.common.parts.utils import is_none
>>> is_none(None)
True
>>> is_none("None")
True
atommic.collections.common.parts.utils.mask_center(x: torch.Tensor, mask_from: Optional[int], mask_to: Optional[int], mask_type: str = '2D') torch.Tensor[source]#

Apply a center crop to the input real image or batch of real images.

Parameters
  • x (torch.Tensor) – The input image or batch of images. This should have at least 3 dimensions, where dimensions -3 and -2 are the spatial dimensions, and the final dimension has size 1 (for real values).

  • mask_from (Optional[int]) – Part of center to start filling.

  • mask_to (Optional[int]) – Part of center to end filling.

  • mask_type (str, optional) – Type of mask to apply. Can be either 1D or 2D. Default is 2D.

Returns

The masked image or batch of images with filled center.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import mask_center
>>> import torch
>>> data = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> data.shape
torch.Size([2, 2, 3, 2])
>>> mask_center(data, 1, 2)
tensor([[[[0., 0.], [1., 1.], [0., 0.]], [[0., 0.], [1., 1.], [0., 0.]]],
    [[[0., 0.], [1., 1.], [0., 0.]], [[0., 0.], [1., 1.], [0., 0.]]]])
>>> mask_center(data, 1, 2).shape
torch.Size([2, 2, 3, 2])
atommic.collections.common.parts.utils.normalize_inplace(x: torch.Tensor, normalization_type: str = 'max') torch.Tensor[source]#

Normalize the input data inplace. This is different from the Normalizer transformation that normalizes the data in a non batch-wise manner.

Parameters
  • x (torch.Tensor) – The input data.

  • normalization_type (str) – The normalization type. Default is "max".

Returns

The unnormalized data.

Return type

torch.Tensor

Examples

>>> import torch
>>> from atommic.collections.common.parts.utils import normalize_inplace
>>> data = torch.rand(1, 200, 200, 2)
>>> attrs = {"max": 164.4672133, "min": 0.000279681}
>>> normalize_inplace(data, attrs).shape
(1, 200, 200, 2)
atommic.collections.common.parts.utils.parse_list_and_keep_last(x: Union[Any, List[Any]]) List[Any][source]#

Parse a list of values and keep the last one, until the last value is not a list.

atommic.collections.common.parts.utils.reshape_fortran(x, shape) torch.Tensor[source]#

Reshapes a tensor in Fortran order. Taken from https://stackoverflow.com/a/63964246

Parameters
  • x (torch.Tensor) – Input tensor to be reshaped.

  • shape (Sequence[int]) – Shape to reshape the tensor to.

Returns

Reshaped tensor.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import reshape_fortran
>>> import torch
>>> data = torch.tensor([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
>>> data.shape
torch.Size([2, 2, 3])
>>> reshape_fortran(data, (3, 2, 2))
tensor([[[ 1,  7],
        [ 4, 10]],
        [[ 2,  8],
        [ 5, 11]],
        [[ 3,  9],
        [ 6, 12]]])
>>> reshape_fortran(data, (3, 2, 2)).shape
torch.Size([3, 2, 2])
atommic.collections.common.parts.utils.rnn_weights_init(module: torch.nn.Module, std_init_range: float = 0.02, xavier: bool = True)[source]#

Initialize weights in Recurrent Neural Network.

Parameters
  • module (torch.nn.Module) – Module to initialize.

  • std_init_range (float) – Standard deviation of normal initializer. Default is 0.02.

  • xavier (bool) – If True, xavier initializer will be used in Linear layers as in [Vaswani2017]. Otherwise, normal initializer will be used. Default is True.

References

Vaswani2017

Vaswani A, Shazeer N, Parmar N, Uszkoreit J, Jones L, Gomez AN, Kaiser Ł, Polosukhin I. Attention is all you need. Advances in neural information processing systems. 2017;30.

Examples

>>> import torch
>>> from atommic.collections.common.parts.utils import rnn_weights_init
>>> rnn = torch.nn.GRU(10, 20, 2)
>>> rnn.apply(rnn_weights_init)
GRU(10, 20, num_layers=2)
atommic.collections.common.parts.utils.rss(x: torch.Tensor, dim: int = 0) torch.Tensor[source]#

Compute the Root Sum of Squares (RSS).

RSS is computed assuming that dim is the coil dimension.

Parameters
  • x (torch.Tensor) – Tensor to apply the RSS transform to.

  • dim (int, optional) – Dimension to apply the RSS transform to. Default is 0.

Returns

Coil-combined tensor with RSS applied.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import rss
>>> import torch
>>> data = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> data.shape
torch.Size([2, 2, 3, 2])
>>> rss(data)
tensor([[[2.8284, 2.8284],
    [5.6569, 5.6569],
    [8.4853, 8.4853]],
    [[2.8284, 2.8284],
    [5.6569, 5.6569],
    [8.4853, 8.4853]]])
>>> rss(data).shape
torch.Size([2, 3, 2])
atommic.collections.common.parts.utils.rss_complex(x: torch.Tensor, dim: int = 0) torch.Tensor[source]#

Compute the Root Sum of Squares (RSS) for complex inputs.

RSS is computed assuming that dim is the coil dimension.

Parameters
  • x (torch.Tensor) – Tensor to apply the RSS transform to.

  • dim (int, optional) – Dimension to apply the RSS transform to. Default is 0.

Returns

Coil-combined tensor with RSS applied.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import rss_complex
>>> import torch
>>> data = torch.tensor([[[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]], [[1+1j, 2+2j, 3+3j], [1+1j, 2+2j, 3+3j]]])
>>> data.shape
torch.Size([2, 2, 3])
>>> rss_complex(data, dim=0)
tensor([[1.4142, 2.8284, 4.2426],
        [1.4142, 2.8284, 4.2426]])
>>> rss_complex(data, dim=0).shape
torch.Size([2, 3])
atommic.collections.common.parts.utils.save_predictions(predictions: Dict[str, numpy.ndarray], out_dir: Path, key: str = 'reconstructions', file_format: str = 'h5') None[source]#

Save predictions to selected format.

Parameters
  • predictions (Dict[str, np.ndarray]) – A dictionary mapping input filenames to corresponding predictions.

  • out_dir (Path) – The output directory to save the predictions to.

  • key (str, optional) – The key to save the predictions under. Default is reconstructions.

  • file_format (str, optional) – The format to save the predictions in. Default is h5.

Examples

>>> from atommic.collections.common.parts.utils import save_predictions
>>> import numpy as np
>>> from pathlib import Path
>>> data = {"test.h5": np.array([[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]])}
>>> data["test.h5"].shape
(2, 3, 2)
>>> output_directory = Path("predictions")
>>> save_predictions(data, output_directory, key="reconstructions", file_format="h5")
>>> save_predictions(data, output_directory, key="segmentations", file_format="h5")
atommic.collections.common.parts.utils.sense(x: torch.Tensor, sensitivity_maps: torch.Tensor, dim: int = 0) torch.Tensor[source]#

Coil-combination according to the SENSitivity Encoding (SENSE) method [Pruessmann1999].

References

Pruessmann1999

Pruessmann KP, Weiger M, Scheidegger MB, Boesiger P. SENSE: Sensitivity encoding for fast MRI. Magn Reson Med 1999; 42:952-962.

Parameters
  • x (torch.Tensor) – The tensor to coil-combine.

  • sensitivity_maps (torch.Tensor) – The coil sensitivity maps.

  • dim (int, optional) – The dimension to coil-combine along. Default is 0.

Returns

Coil-combined tensor with SENSE applied.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import sense
>>> import torch
>>> data = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> data.shape
torch.Size([2, 2, 3, 2])
>>> coil_sensitivity_maps = torch.tensor([[[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]],     [[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]]])
>>> coil_sensitivity_maps.shape
torch.Size([2, 2, 3, 2])
>>> sense(data, coil_sensitivity_maps)
tensor([[[2.8284, 2.8284],
    [5.6569, 5.6569],
    [8.4853, 8.4853]],
    [[2.8284, 2.8284],
    [5.6569, 5.6569],
    [8.4853, 8.4853]]])
>>> sense(data, coil_sensitivity_maps).shape
torch.Size([2, 3, 2])
atommic.collections.common.parts.utils.to_tensor(x: numpy.ndarray) torch.Tensor[source]#

Converts a numpy array to a torch tensor. For complex arrays, the real and imaginary parts are stacked along the last dimension.

Parameters

x (np.ndarray) – Input numpy array to be converted to torch.

Returns

Torch tensor version of input.

Return type

torch.Tensor

Examples

>>> from atommic.collections.common.parts.utils import to_tensor
>>> import numpy as np
>>> data = np.array([[1+1j, 2+2j, 3+3j], [4+4j, 5+5j, 6+6j]])
>>> data.shape
(2, 3)
>>> to_tensor(data)
tensor([[[1., 1.],
        [2., 2.],
        [3., 3.]],
        [[4., 4.],
        [5., 5.],
        [6., 6.]]], dtype=torch.float64)
>>> to_tensor(data).shape
torch.Size([2, 3, 2])
atommic.collections.common.parts.utils.unnormalize(x: torch.Tensor, attrs: Dict, normalization_type: str = 'max') torch.Tensor[source]#

Unnormalize the input data.

Parameters
  • x (torch.Tensor) – The input data.

  • attrs (Dict) – The attributes of the input data.

  • normalization_type (str) – The normalization type. Default is "max".

Returns

The unnormalized data.

Return type

torch.Tensor

Examples

>>> import torch
>>> from atommic.collections.common.parts.utils import unnormalize
>>> data = torch.rand(1, 200, 200, 2)
>>> attrs = {"max": 1.0, "min": 0.0}
>>> unnormalize(data, attrs).shape
(1, 200, 200, 2)
atommic.collections.common.parts.utils.zero_nan_inf(x)[source]#

If x is nan or inf, return 0.