U Ph @sdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z d d d d d d d dddZ d d dddZdddddZd d d dddZddddddZd d dddZdddddZdS)!zJ This script contains utility functions for complex-value PyTorch tensor. ) annotationsN)Tensor)NdarrayOrTensor)convert_to_numpyconvert_to_tensorTFz$NdarrayOrTensor | list | int | floatztorch.dtype | Noneztorch.device | Noneboolr)datadtypedevice wrap_sequence track_metareturncCs"t|tr,t|sLt|||||d}|Sn t|sLt|||||d}|St|tjrptj|j|j gdd}nt|tj rt d|j jdkr|jdkrt|}tj|j|j fdd}nPt|ttfr|j|j gg}n2t|tr t|dd }tj|j|j fdd}t|||||d}|S) a# 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 float. will convert Tensor, Numpy array, float, int, bool to Tensor, strings and objects keep the original. for list, convert every item to a Tensor if applicable. dtype: target data type to when converting to Tensor. device: target device to put the converted Tensor data. wrap_sequence: if `False`, then lists will recursively call this function. E.g., `[1, 2]` -> `[tensor(1), tensor(2)]`. If `True`, then `[1, 2]` -> `tensor([1, 2])`. track_meta: whether to track the meta information, if `True`, will convert to `MetaTensor`. default to `False`. Returns: PyTorch version of the data Example: .. code-block:: python import numpy as np data = np.array([ [1+1j, 1-1j], [2+2j, 2-2j] ]) # the following line prints (2,2) print(data.shape) # the following line prints torch.Size([2, 2, 2]) print(convert_to_tensor_complex(data).shape) )r r r r dimz[SaUO]NraxisT)r ) isinstancertorch is_complexrnp iscomplexobjstackrealimagndarrayresearchr strndimascontiguousarrayfloatintlistrtolist)rr r r r Zconverted_datar%\/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/reconstruction/complex_utils.pyconvert_to_tensor_complexsN%         r')xr cCs@|jddkr$td|jdd|dd|dddS)z Compute the absolute value of a complex tensor. Args: x: Input tensor with 2 channels in the last dimension representing real and imaginary parts. Returns: Absolute value along the last dimension rzx.shape[-1] is not 2 (z)..r.g?)shape ValueErrorr(r%r%r& complex_abs_tfs r0rcCst|S)a Compute the absolute value of a complex array. Args: x: Input array/tensor with 2 channels in the last dimension representing real and imaginary parts. Returns: Absolute value along the last dimension Example: .. code-block:: python import numpy as np x = np.array([3,4])[np.newaxis] # the following line prints 5 print(complex_abs(x)) )r0r/r%r%r& complex_absusr1)r(yr cCs|jddks|jddkr>td|jdd|jdd|d|d|d|d}|d|d|d|d}tj||fddS) a Compute complex-valued multiplication. Supports Ndim inputs with last dim equal to 2 (real/imaginary channels) Args: x: Input tensor with 2 channels in the last dimension representing real and imaginary parts. y: Input tensor with 2 channels in the last dimension representing real and imaginary parts. Returns: Complex multiplication of x and y rr)'last dim must be 2, but x.shape[-1] is  and y.shape[-1] is .r*r+rr-r.rr)r(r2 real_part imag_partr%r%r& complex_mul_ts "  r9cCs|jddks|jddkr>td|jdd|jddt|trRt||S|d|d|d|d}|d|d|d|d}tj||fdd}|Sd S) a Compute complex-valued multiplication. Supports Ndim inputs with last dim equal to 2 (real/imaginary channels) Args: x: Input array/tensor with 2 channels in the last dimension representing real and imaginary parts. y: Input array/tensor with 2 channels in the last dimension representing real and imaginary parts. Returns: Complex multiplication of x and y Example: .. code-block:: python import numpy as np x = np.array([[1,2],[3,4]]) y = np.array([[1,1],[1,1]]) # the following line prints array([[-1, 3], [-1, 7]]) print(complex_mul(x,y)) rr)r3r4r5r*r+rN)r-r.rrr9rr)r(r2r7r8multr%r%r& complex_muls"    r;cCs@|jddkr$td|jddtj|d|d fddS)a Compute complex conjugate of a tensor. Supports Ndim inputs with last dim equal to 2 (real/imaginary channels) Args: x: Input tensor with 2 channels in the last dimension representing real and imaginary parts. Returns: Complex conjugate of x rr)r3r5r*r+rr6r/r%r%r&complex_conj_ts r<cCsZ|jddkr$td|jddt|tr6t|Stj|d|d fdd}|SdS) a Compute complex conjugate of an/a array/tensor. Supports Ndim inputs with last dim equal to 2 (real/imaginary channels) Args: x: Input array/tensor with 2 channels in the last dimension representing real and imaginary parts. Returns: Complex conjugate of x Example: .. code-block:: python import numpy as np x = np.array([[1,2],[3,4]]) # the following line prints array([[ 1, -2], [ 3, -4]]) print(complex_conj(x)) rr)r3r5r*r+rN)r-r.rrr<rr)r(Znp_conjr%r%r& complex_conjs  r=)NNTF)__doc__ __future__rrnumpyrrrmonai.config.type_definitionsrmonai.utils.type_conversionrrr'r0r1r9r;r<r=r%r%r%r& s$   K"