Convert complex-valued data to a 2-channel PyTorch tensor. The real and imaginary parts are stacked along the last dimension. This function relies on 'monai.utils.type_conversion.convert_to_tensor' Args: data: input data can be PyTorch Tensor, numpy array, list, int, and fl
(
data: NdarrayOrTensor | list | int | float,
dtype: torch.dtype | None = None,
device: torch.device | None = None,
wrap_sequence: bool = True,
track_meta: bool = False,
)
| 25 | |
| 26 | |
| 27 | def convert_to_tensor_complex( |
| 28 | data: NdarrayOrTensor | list | int | float, |
| 29 | dtype: torch.dtype | None = None, |
| 30 | device: torch.device | None = None, |
| 31 | wrap_sequence: bool = True, |
| 32 | track_meta: bool = False, |
| 33 | ) -> Tensor: |
| 34 | """ |
| 35 | Convert complex-valued data to a 2-channel PyTorch tensor. |
| 36 | The real and imaginary parts are stacked along the last dimension. |
| 37 | This function relies on 'monai.utils.type_conversion.convert_to_tensor' |
| 38 | |
| 39 | Args: |
| 40 | data: input data can be PyTorch Tensor, numpy array, list, int, and float. |
| 41 | will convert Tensor, Numpy array, float, int, bool to Tensor, strings and objects keep the original. |
| 42 | for list, convert every item to a Tensor if applicable. |
| 43 | dtype: target data type to when converting to Tensor. |
| 44 | device: target device to put the converted Tensor data. |
| 45 | wrap_sequence: if `False`, then lists will recursively call this function. |
| 46 | E.g., `[1, 2]` -> `[tensor(1), tensor(2)]`. If `True`, then `[1, 2]` -> `tensor([1, 2])`. |
| 47 | track_meta: whether to track the meta information, if `True`, will convert to `MetaTensor`. |
| 48 | default to `False`. |
| 49 | |
| 50 | Returns: |
| 51 | PyTorch version of the data |
| 52 | |
| 53 | Example: |
| 54 | .. code-block:: python |
| 55 | |
| 56 | import numpy as np |
| 57 | data = np.array([ [1+1j, 1-1j], [2+2j, 2-2j] ]) |
| 58 | # the following line prints (2,2) |
| 59 | print(data.shape) |
| 60 | # the following line prints torch.Size([2, 2, 2]) |
| 61 | print(convert_to_tensor_complex(data).shape) |
| 62 | """ |
| 63 | # if data is not complex, just turn it into a tensor |
| 64 | if isinstance(data, Tensor): |
| 65 | if not torch.is_complex(data): |
| 66 | converted_data: Tensor = convert_to_tensor( |
| 67 | data, dtype=dtype, device=device, wrap_sequence=wrap_sequence, track_meta=track_meta |
| 68 | ) |
| 69 | return converted_data |
| 70 | else: |
| 71 | if not np.iscomplexobj(data): |
| 72 | converted_data = convert_to_tensor( |
| 73 | data, dtype=dtype, device=device, wrap_sequence=wrap_sequence, track_meta=track_meta |
| 74 | ) |
| 75 | return converted_data |
| 76 | |
| 77 | # if data is complex, turn its stacked version into a tensor |
| 78 | if isinstance(data, torch.Tensor): |
| 79 | data = torch.stack([data.real, data.imag], dim=-1) |
| 80 | |
| 81 | elif isinstance(data, np.ndarray): |
| 82 | if re.search(r"[SaUO]", data.dtype.str) is None: |
| 83 | # numpy array with 0 dims is also sequence iterable, |
| 84 | # `ascontiguousarray` will add 1 dim if img has no dim, so we only apply on data with dims |
searching dependent graphs…