o  iJV@snddlmZddlZddlmZddlmZddlZddl Z ddl Z ddl m Z m Z ddlmZed\ZZeddd \ZZgd Zed ejed ejed ejiZdIddZdJddZdKddZdLddZddZdMd d!ZdNd"d#Z   $ $ $ %dOdPd-d.Z dQdRd0d1Z!dQdSd3d4Z"    $ $dTdUd8d9Z#  $  $dVdWd>d?Z$dXdBdCZ%dYdEdFZ&dZd[dGdHZ'dS)\) annotationsN)Sequence)Any) DtypeLike NdarrayTensor)optional_importcupyndarray)name) get_numpy_dtype_from_stringget_torch_dtype_from_stringdtype_torch_to_numpydtype_numpy_to_torchget_equivalent_dtypeconvert_data_type get_dtypeget_dtype_stringconvert_to_cupyconvert_to_numpyconvert_to_tensorconvert_to_dst_typeuint16uint32uint64dtypestrreturnnp.dtypecCstjgt|dddjS)zKGet a numpy dtype (e.g., `np.float32`) from its string (e.g., `"float32"`)..r)npemptyrsplitrr r$]/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/utils/type_conversion.pyr /sr torch.dtypecCs tt|S)zNGet a torch dtype (e.g., `torch.float32`) from its string (e.g., `"float32"`).)rr r r$r$r%r 4s r cCstjg|djS)z.Convert a torch dtype to its numpy equivalent.r )torchr"numpyrr r$r$r%r 9sr cCsttjg|djS)z.Convert a numpy dtype to its torch equivalent.r )r' from_numpyr!r"rr r$r$r%r>srcCsP|durdS|tjus|jdkrt|tjr|St|St|tjs$|St|S)aConvert to the `dtype` that corresponds to `data_type`. The input dtype can also be a string. e.g., `"float32"` becomes `torch.float32` or `np.float32` as necessary. Example:: im = torch.tensor(1) dtype = get_equivalent_dtype(np.float32, type(im)) N MetaTensor)r'Tensor__name__ isinstancerrr )r data_typer$r$r%rCs   rdatarDtypeLike | torch.dtypecCs.t|dr|jSt|trt|dSt|S)zGet the dtype of an image, or if there is a sequence, recursively call the method on the 0th element. This therefore assumes that in a `Sequence`, all types are the same. rr)hasattrrr-rrtyper/r$r$r%r\s   rcCs,t|tjrt|ddSt|ddS)z)Get a string representation of the dtype.N)r-r'rrr r$r$r%rjs rFTdeviceNone | str | torch.device wrap_sequencebool track_metasafeconvert_numericc s^dfdd }|rt|}ttjt|tjr%||jtjdSt|tjrHt d|j j d urF|j d kr?t|}||d S|StrOt|tsYr`t|tttfr`||d St|tr|fd d |D}|rz||d S|St|trtfdd|D} |r|| d S| St|trfdd|DS|S)aR Utility to convert the input data to a PyTorch Tensor, if `track_meta` is True, the output will be a `MetaTensor`, otherwise, the output will be a regular torch Tensor. If passing a dictionary, list or tuple, recursively check every item and convert it to PyTorch Tensor. Args: data: input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc. will convert Tensor, Numpy array, float, int, bool to Tensor, strings and objects keep the original. for dictionary, list or tuple, 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`. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[tensor(0), tensor(244)]`. If `True`, then `[256, -12]` -> `[tensor(255), tensor(0)]`. convert_numeric: if `True`, convert numeric Python values to tensors. tensorrkwargsrcst|tjs"t|tjr|jtvr|t|j}tj|fi|}r1t|t j j s1t j |Ss>t|t j j r>|S|SN) r-r'r+r!r rUNSUPPORTED_TYPESastype as_tensormonair/r*)r=r>)r:r$r%_convert_tensors  z*convert_to_tensor.._convert_tensor)rr6 memory_formatz[SaUO]Nr)rr6c sg|] }t|dqSrr6r:r<r.0ir<r6rr:r$r% sz%convert_to_tensor..c3s"|] }t|dVqdS)rGNrHrIrLr$r% s  z$convert_to_tensor..c s$i|]\}}|t|dqSrFrHrJkvrLr$r% sz%convert_to_tensor..)r=rr>rrr)safe_dtype_rangerr'r+r-tocontiguous_formatr!r researchrrndimascontiguousarrayhas_cp cp_ndarrayfloatintr9listtupledictitems) r/rr6r8r:r;r<rDlist_ret tuple_retr$rLr%rqs:      "   rrcsb|rt|}t|tjr!tj|jddt tj d}n~t r3t|t r3t |jdd}nlt|tj tttfr_t|tj rW|jdkrW|jjtjkrWt|}tj|d}n@t|trvfdd|D}|rtt|S|St|trtfd d |D}|rt|S|St|trfd d |DSt|tj r|jdkrt|}|S) a Utility to convert the input data to a numpy array. If passing a dictionary, list or tuple, recursively check every item and convert it to numpy array. Args: data: input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc. will convert Tensor, Numpy array, float, int, bool to numpy arrays, strings and objects keep the original. for dictionary, list or tuple, convert every item to a numpy array if applicable. dtype: target data type when converting to numpy array. wrap_sequence: if `False`, then lists will recursively call this function. E.g., `[1, 2]` -> `[array(1), array(2)]`. If `True`, then `[1, 2]` -> `array([1, 2])`. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. cpu)r6r F)copyrcg|]}t|dqSr rrIr r$r%rMz$convert_to_numpy..c3|] }t|dVqdSr NrhrIr r$r%rNz#convert_to_numpy..ci|] \}}|t|dqSrgrhrOr r$r%rRz$convert_to_numpy..)rSr-r'r+r!asarraydetachrTr(rr rZr[cpZasnumpyrAr\r]r9rXritemsizerYr^r_r`rar/rr8r;rbrcr$r r%rs*  (*     rnp.dtype | Nonecs<|rt|}t|tjr.|jjdkr.|jtjkr'| tj }dur'tt |}nTt|t tjtjtttfrBt |}n@t|trYfdd|D}|rWt |S|St|trrtfdd|D}|rpt |S|St|trfdd|DSt|t jstd t|d |jd krt |}|S) a7 Utility to convert the input data to a cupy array. If passing a dictionary, list or tuple, recursively check every item and convert it to cupy array. Args: data: input data can be PyTorch Tensor, numpy array, cupy array, list, dictionary, int, float, bool, str, etc. Tensor, numpy array, cupy array, float, int, bool are converted to cupy arrays, for dictionary, list or tuple, convert every item to a numpy array if applicable. dtype: target data type when converting to Cupy array, tt must be an argument of `numpy.dtype`, for more details: https://docs.cupy.dev/en/stable/reference/generated/cupy.array.html. wrap_sequence: if `False`, then lists will recursively call this function. E.g., `[1, 2]` -> `[array(1), array(2)]`. If `True`, then `[1, 2]` -> `array([1, 2])`. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. cudaNcsg|]}t|qSr$rrIr r$r%rMsz#convert_to_cupy..c3s|]}t|VqdSr?rvrIr r$r%rNsz"convert_to_cupy..csi|] \}}|t|qSr$rvrOr r$r%rRsz#convert_to_cupy..zThe input data type [z'] cannot be converted into cupy arrays!r)rSr-r'r+r6r2rr9rprTuint8rqror[r!r r\r]r^r_r`ra ValueErrorrXrYrsr$r r%rs.        r output_typetype[NdarrayTensor] | None/tuple[NdarrayTensor, type, torch.device | None]c Cst|tjjr tjj}n$t|tjrtj}nt|tjr tj}ntr,t|t jr,t j}nt |}t|tjr9|j nd}|p>|}t ||}t |tjr`t |tjj} t||||| |d} | ||fSt |tjrst||||d} | ||fStrt |t jrt||||d} | ||fStd|)ar Convert to `MetaTensor`, `torch.Tensor` or `np.ndarray` from `MetaTensor`, `torch.Tensor`, `np.ndarray`, `float`, `int`, etc. Args: data: data to be converted output_type: `monai.data.MetaTensor`, `torch.Tensor`, or `np.ndarray` (if `None`, unchanged) device: if output is `MetaTensor` or `torch.Tensor`, select device (if `None`, unchanged) dtype: dtype of output data. Converted to correct library type (e.g., `np.float32` is converted to `torch.float32` if output type is `torch.Tensor`). If left blank, it remains unchanged. wrap_sequence: if `False`, then lists will recursively call this function. E.g., `[1, 2]` -> `[array(1), array(2)]`. If `True`, then `[1, 2]` -> `array([1, 2])`. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. Returns: modified data, orig_type, orig_device Note: When both `output_type` and `dtype` are specified with different backend (e.g., `torch.Tensor` and `np.float32`), the `output_type` will be used as the primary type, for example:: >>> convert_data_type(1, torch.Tensor, dtype=np.float32) (1.0, , None) N)rr6r8r:r;)rr8r;zUnsupported output type: )r-rCr/r*r'r+r!r rZrqr2r6r issubclassrrrrx) r/ryr6rr8r; orig_type orig_devicedtype_r:Zdata_r$r$r%rs2%          rsrcdstrDtypeLike | torch.dtype | Nonec Cs|dur t|tjr |jn|}|durt|dd}d}t|tjjr0tjj}t|tjjs/d}nt|tjr:tj}nt|tj rDtj }nt |}t ||||||d\}} } |rct|tjjrc| ||| | fS)a Convert source data to the same data type and device as the destination data. If `dst` is an instance of `torch.Tensor` or its subclass, convert `src` to `torch.Tensor` with the same data type as `dst`, if `dst` is an instance of `numpy.ndarray` or its subclass, convert to `numpy.ndarray` with the same data type as `dst`, otherwise, convert to the type of `dst` directly. Args: src: source data to convert type. dst: destination data that convert to the same data type as it. dtype: an optional argument if the target `dtype` is different from the original `dst`'s data type. wrap_sequence: if `False`, then lists will recursively call this function. E.g., `[1, 2]` -> `[array(1), array(2)]`. If `True`, then `[1, 2]` -> `array([1, 2])`. device: target device to put the converted Tensor data. If unspecified, `dst.device` will be used if possible. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. See Also: :func:`convert_data_type` NrFT)r/ryr6rr8r;) r-r'r+r6getattrrCr/r*r!r r2rZcopy_meta_from) rrrr8r6r;Z copy_metaryoutput_type_devicer$r$r%rcs(       r$Sequence | torch.Tensor | np.ndarrayr^cCs"t|tjtjfr |St|S)z Convert to list from `torch.Tensor`/`np.ndarray`/`list`/`tuple` etc. Args: data: data to be converted Returns: a list )r-r'r+r!r tolistr^r3r$r$r%convert_to_lists" rtuple[float, float]cCs\|tvrd}nt|tjj}t|tj}|r"t|jt|j fSt |jt |j fS)z Get dtype bound value Args: dtype: dtype to get bound value Returns: (bound_min_value, bound_max_value) F) r@rr'r+is_floating_pointr!arrayfinfominmaxiinfo)rrr$r$r%get_dtype_bound_values rcs0dd}trt|trt||St|tjr!t||St|tjr,||St|t t t fr:dur:|St|t t t frhdurh}t |}||dkrV|dn|}||dkrd|d}|S|}|St|t rvfdd|DSt|trtfdd |DSt|trfd d |DS|S) ai Utility to safely convert the input data to target dtype. Args: data: input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc. will convert to target dtype and keep the original type. for dictionary, list or tuple, convert every item. dtype: target data type to convert. cSs|dur|n|j}t|}|jdkr||f}nt|tjr(t|t|f}n t|t|f}|d|dksB|d|dkrzt|tjrSt ||d|dSt|tj rdt ||d|dSt rvt|t rxt ||d|dSdSdS|S)Nr)rrrXr-r'r+rrr!clampr cliprZr[rq)r/r output_dtypedtype_bound_valueZ data_boundr$r$r%_safe_dtype_ranges       z+safe_dtype_range.._safe_dtype_rangeNrrcrfrgrSrIr r$r%rMriz$safe_dtype_range..c3rjrkrrIr r$r%rNrlz#safe_dtype_range..crmrgrrOr r$r%rRrnz$safe_dtype_range..)rZr-r[rqror!r r'r+r\r]r9rr^r_r`ra)r/rrrrr$r r%rSs0       rS)rrrr)rrrr&)rr&rr)rrrr&)r/rrr0)rr0rr)NNFFFT)r/rrr0r6r7r8r9r:r9r;r9r<r9rr)NFF) r/rrrr8r9r;r9rr) r/rrrtr8r9r;r9rr)NNNFF)r/rryrzr6r7rr0r8r9r;r9rr{)NFNF)rrrrrrr8r9r6r7r;r9rr{)r/rrr^)rr0rrr?)r/rrr0rr)( __future__rrVcollections.abcrtypingrr(r!r'rCmonai.config.type_definitionsrr monai.utilsrrqrZr[___all__rint32int64r@r r r rrrrrrrrrrrrSr$r$r$r%sV     (        S -/ H 5