U Phx#@svddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZmZddlmZddlmZddlmZddlmZmZmZmZmZddlZddl Z dd l!m"Z"m#Z#m$Z$dd l%m&Z&m'Z'er dd l(m)Z)ne&d d dd\Z)Z*ddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2g#Z+da,e j-j.j/Z0e j-j.j1Z2e3ej4j5Z6e6d3Z7e8d4d5dZ9d6dZ:ed7Z;ed8d7d7d9d:dZdZdd?d@dEdFdGdZ?dd?dHd?d@dEdIdJdZ@d?dHdKdLdMdZAdNd?dOdPdQdZBdRdSfd?dTdUdKdVdWdZCd?d@dXdYdZDd?d@dXdZdZEddHdHd\dHd@d]d^d_dZFd`dadbdZGe6ddfd`dcddd]dedfdZHdgdZIdd?did@d@d?djdkd!ZJddld@d@d@dmdnd"ZKddod@dpdqdrd#ZLGdsd$d$ZMGdtd%d%ZNdUdud@dvdwd'ZOdxd&ZPddydHd@dHdydzd{d(ZQdd|d@d]d}d~d)ZRddd|d@d@dd?d]ddd*ZSddddd+ZTddd@dd?ddddZUd|dddd,ZVdd?dHdddd-ZWddddd.ZXGdd/d/e)ZYGdd0d0ZZdd1Z[dd?dddd2Z\dd@dddZ]dydHdydddZ^dydHdydddZ_dS)) annotationsN) literal_eval)CallableIterableSequence) strtobool)log10)Path) TYPE_CHECKINGAnyTypeVarcastoverload)NdarrayOrTensor NdarrayTensorPathLike)optional_import version_leq) SafeLoaderyamlrbase)nameas_typezip_with star_zip_withfirstissequenceiterable is_immutable ensure_tupleensure_tuple_sizeensure_tuple_repto_tuple_of_dictionariesfall_back_tupleis_scalar_tensor is_scalar progress_barget_seedset_determinism list_to_dictMAX_SEEDcopy_to_devicestr2boolstr2list MONAIEnvVars ImageMetaKeyis_module_ver_at_least has_option sample_slicescheck_parent_dirsave_obj label_union path_to_uri pprint_edgescheck_key_duplicatesCheckKeyDuplicatesYamlLoader ConvertUnits check_kwargs_exist_in_class_initrun_cmd)mapfunccGs||t|S)z` Map `op`, using `mapfunc`, to each tuple derived from zipping the iterables in `vals`. )zip)opr=valsrAE/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/utils/misc.pyrXscGst|f|dtjiS)z9 Use starmap as the mapping function in zipWith. r=)r itertoolsstarmap)r?r@rArArBr_sTz Iterable[T])iterabledefaultreturncCsdSNrA)rFrGrArArBriszT | None)rFrHcCsdSrIrA)rFrArArBrmscCs|D] }|S|S)zw Returns the first item in the given iterable or `default` if empty, meaningful mostly with 'for' expressions. rA)rFrGirArArBrqsr bool)objrHcCsPzt|dr|jdkrWdSWntk r4YdSXt|toNt|ttf S)zN Determine if the object is an iterable sequence and is not a string. ndimrF)hasattrrM Exception isinstancerstrbytesrLrArArBrzs  c Cs$t|tdttttttttt t f S)z Determine if the object is an immutable object. see also https://github.com/python/cpython/blob/3.11/Lib/copy.py#L109 N) rPtypeintfloatrKcomplexrQtuplerRrangeslicerSrArArBrsFrX)r@ wrap_arrayrHcCs2|rt|tjtjfr|fSt|r,t|S|fS)a# Returns a tuple of `vals`. Args: vals: input data to convert to a tuple. wrap_array: if `True`, treat the input numerical array (ndarray/tensor) as one item of the tuple. if `False`, try to convert the array with `tuple(vals)`, default to `False`. )rPnpndarraytorchTensorrrX)r@r[rArArBrs rU)r@dimpad_valpad_from_startrHcCsHt|}|t|}|dkr(|d|S|r:|f||S||f|S)zn Returns a copy of `tup` with `dim` values by either shortened or padded with `pad_val` as necessary. rN)rlen)r@r`rarbtupZpad_dimrArArBrs  ztuple[Any, ...])rdr`rHcCstt|tjr|}t|tjr0|}t |sB|f|St ||krVt |St d|dt |ddS)al Returns a copy of `tup` with `dim` values by either shortened or duplicated input. Raises: ValueError: When ``tup`` is a sequence and ``tup`` length is not ``dim``. Examples:: >>> ensure_tuple_rep(1, 3) (1, 1, 1) >>> ensure_tuple_rep(None, 3) (None, None, None) >>> ensure_tuple_rep('test', 3) ('test', 'test', 'test') >>> ensure_tuple_rep([1, 2, 3], 3) (1, 2, 3) >>> ensure_tuple_rep(range(3), 3) (0, 1, 2) >>> ensure_tuple_rep([1, 2], 3) ValueError: Sequence must have length 3, got length 2. zSequence must have length z, got .N) rPr^r_detachcpunumpyr\r]tolistrrcrX ValueError)rdr`rArArBr s    dictztuple[dict[Any, Any], ...])dictionary_of_tupleskeysrHcsPttdkrtiSfdd|DtfddttDS)a Given a dictionary whose values contain scalars or tuples (with the same length as ``keys``), Create a dictionary for each key containing the scalar values mapping to that key. Args: dictionary_of_tuples: a dictionary whose values are scalars or tuples whose length is the length of ``keys`` keys: a tuple of string values representing the keys in question Returns: a tuple of dictionaries that contain scalar values, one dictionary for each key Raises: ValueError: when values in the dictionary are tuples but not the same length as the length of ``keys`` Examples: >>> to_tuple_of_dictionaries({'a': 1 'b': (2, 3), 'c': (4, 4)}, ("x", "y")) ({'a':1, 'b':2, 'c':4}, {'a':1, 'b':3, 'c':4}) rcs i|]\}}|t|tqSrA)r rc.0kv)rmrArB sz,to_tuple_of_dictionaries..c3s$|]fddDVqdS)csi|]\}}||qSrArArnikrArBrrsz6to_tuple_of_dictionaries...N)items)ro)dict_overridesrsrB sz+to_tuple_of_dictionaries..)rrcrXrurY)rlrmrA)rvrmrBr!s  cCs |o |dkSNrrAxrArArBr{zSequence | NdarrayTensorr) user_providedrGfuncrHcs.t|}t||}tfddt||DS)a$ Refine `user_provided` according to the `default`, and returns as a validated tuple. The validation is done for each element in `user_provided` using `func`. If `func(user_provided[idx])` returns False, the corresponding `default[idx]` will be used as the fallback. Typically used when `user_provided` is a tuple of window size provided by the user, `default` is defined by data, this function returns an updated `user_provided` with its non-positive components replaced by the corresponding components from `default`. Args: user_provided: item to be validated. default: a sequence used to provided the fallbacks. func: a Callable to validate every components of `user_provided`. Examples:: >>> fall_back_tuple((1, 2), (32, 32)) (1, 2) >>> fall_back_tuple(None, (32, 32)) (32, 32) >>> fall_back_tuple((-1, 10), (32, 32)) (32, 10) >>> fall_back_tuple((-1, None), (32, 32)) (32, 32) >>> fall_back_tuple((1, None), (32, 32)) (1, 32) >>> fall_back_tuple(0, (32, 32)) (32, 32) >>> fall_back_tuple(range(3), (32, 64, 48)) (32, 1, 2) >>> fall_back_tuple([0], (32, 32)) ValueError: Sequence must have length 2, got length 1. c3s"|]\}}|r|n|VqdSrIrA)roZ default_cZuser_cr~rArBrwsz"fall_back_tuple..)rcr rXr>)r}rGr~rMuserrArrBr"s '  )valrHcCst|tjo|jdkSrx)rPr^r_rMrrArArBr#scCs(t|tjr|jdkrdStt|S)NrT)rPr^r_rMrKr\isscalarrrArArBr$s str | NoneNone)indexcountdescbar_lennewlinerHcCs|sdnd}t|||}|dk r.|dnd}|dd|d||d7}t|d |d||d ||kr~tddS) aprint a progress bar to track some time consuming task. Args: index: current status in progress. count: total steps of the progress. desc: description of the progress bar, if not None, show before the progress bar. bar_len: the total length of the bar on screen, default is 30 char. newline: whether to print in a new line for every index.  z N [=]/)end)rUprint)rrrrrrZ filled_lenbarrArArBr%%s  z int | NonerHcCstSrI)_seedrArArArBr&8sz bool | Nonez Set random seed for modules to enable or disable deterministic training. Args: seed: the random seed to use, default is np.iinfo(np.int32).max. It is recommended to set a large seed, i.e. a number that has a good balance of 0 and 1 bits. Avoid having many 0 bits in the seed. if set to None, will disable deterministic training. use_deterministic_algorithms: Set whether PyTorch operations must use "deterministic" algorithms. additional_settings: additional settings that need to set random seed. Note: This function will not affect the randomizable objects in :py:class:`monai.transforms.Randomizable`, which have independent random states. For those objects, the ``set_random_state()`` method should be used to ensure the deterministic behavior (alternatively, :py:class:`monai.data.DataLoader` by default sets the seeds according to the global random state, please see also: :py:class:`monai.data.utils.worker_init_fn` and :py:class:`monai.data.utils.set_rnd`). Nz[PyTorch global flag support of backends is disabled, enable it to set global `cudnn` flags.TFrset_deterministiczRuse_deterministic_algorithms=True, but PyTorch version is too old to set the mode.)r^default_generatorrr) manual_seedrUrrandomr\rbackends flags_frozenwarningswarn"__allow_nonbracketed_mutation_flagcudnn deterministic benchmark_flag_deterministic_flag_cudnn_benchmarkrNrr)rrrZseed_r~rArArBr'<s4                 c Csdd}i}|r|D]}||\}}z(||kr>td|dt|||<Wqtk rzttt|||<Wntk r|||<YnXYqXq|S)a9 To convert a list of "key=value" pairs into a dictionary. For examples: items: `["a=1", "b=2", "c=3"]`, return: {"a": "1", "b": "2", "c": "3"}. If no "=" in the pair, use None as the value, for example: ["a"], return: {"a": None}. Note that it will remove the blanks around keys and values. cSsB|jddd}|dd}t|dkr6|ddnd}||fS)Nrr<)maxsplitrz ')splitstriprc)srukeyvaluerArArB _parse_varsz list_to_dict.._parse_varzencounter duplicated key re)KeyErrorrrjrKrrQ)rurditemrrrArArBr(ys  Tzstr | torch.device | None)rLdevice non_blockingverboserHcst|dr|jdSt|tr:tfdd|DSt|trXfdd|DSt|trzfdd|DS|rttj t j j }t|d t|d |S) a Copy object or tuple/list/dictionary of objects to ``device``. Args: obj: object or tuple/list/dictionary of objects to move to ``device``. device: move ``obj`` to this device. Can be a string (e.g., ``cpu``, ``cuda``, ``cuda:0``, etc.) or of type ``torch.device``. non_blocking: when `True`, moves data to device asynchronously if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices. verbose: when `True`, will print a warning for any elements of incompatible type not copied to ``device``. Returns: Same as input, copied to ``device`` where possible. Original input will be unchanged. to)rc3s|]}t|VqdSrIr*roorrrArBrwsz!copy_to_device..csg|]}t|qSrArrrrArB sz"copy_to_device..csi|]\}}|t|qSrAr)rorprrrArBrrsz"copy_to_device..z called with incompatible type: z". Data will be returned unchanged.)rNrrPrXlistrkrur types FrameTypeinspect currentframef_codeco_namerrrT)rLrrrfn_namerArrBr*s    z str | bool)rrG raise_excrHcCsft|tr|Sd}d}t|tr@|}||kr4dS||kr@dS|rbtd|dd|||S)a Convert a string to a boolean. Case insensitive. True: yes, true, t, y, 1. False: no, false, f, n, 0. Args: value: string to be converted to a boolean. If value is a bool already, simply return it. raise_exc: if value not in tuples of expected true or false inputs, should we raise an exception? If not, return `default`. Raises ValueError: value not in tuples of expected true or false inputs and `raise_exc` is `True`. Useful with argparse, for example: parser.add_argument("--convert", default=False, type=str2bool) python mycode.py --convert=True )yestruety1)nofalsefn0TFzGot "z", expected a value from: , )rPrKrQlowerrjjoin)rrGrZtrue_setZ false_setrArArBr+s  zstr | list | Nonez list | None)rrrHc Cs|dkr dSt|tr|St|trv|d}tt|D]6}zt||}|||<Wq:tk rnYq:Xq:|S|rt d|ddS)a Convert a string to a list. Useful with argparse commandline arguments: parser.add_argument("--blocks", default=[1,2,3], type=str2list) python mycode.py --blocks=1,2,2,4 Args: value: string (comma separated) to be converted to a list raise_exc: if not possible to convert to a list, raise an exception Raises ValueError: value not a string or list or not possible to convert N,zUnable to convert "z-", expected a comma-separated str, e.g. 1,2,3) rPrrQrrYrcrrrOrj)rrrqrJarArArBr,s     c@seZdZdZeddddZeddddZeddd d Zeddd d Zeddd dZ eddddZ eddddZ eddddZ eddddZ dS)r-z. Environment variables used by MONAI. rrcCs tjdS)NZMONAI_DATA_DIRECTORYosenvirongetrArArArBdata_dirszMONAIEnvVars.data_dirrKcCs$tjdd}t|tr|St|S)NZ MONAI_DEBUGF)rrrrPrKr+rrArArBdebugszMONAIEnvVars.debugcCs tjdS)NZMONAI_DOC_IMAGESrrArArArB doc_images szMONAIEnvVars.doc_imagescCstjddS)NZMONAI_ALGO_HASHZe4cf5a1rrArArArB algo_hashszMONAIEnvVars.algo_hashcCstjddS)NZMONAI_TRACE_TRANSFORMrrrArArArBtrace_transformszMONAIEnvVars.trace_transformcCstjddS)NZMONAI_EVAL_EXPRrrrArArArB eval_exprszMONAIEnvVars.eval_exprcCstjddS)NZMONAI_ALLOW_MISSING_REFERENCErrrArArArBallow_missing_referencesz$MONAIEnvVars.allow_missing_referencecCstjddS)NZMONAI_EXTRA_TEST_DATArrrArArArBextra_test_data szMONAIEnvVars.extra_test_datacCstjddS)NZMONAI_TESTING_ALGO_TEMPLATErrArArArBtesting_algo_template$sz"MONAIEnvVars.testing_algo_templateN)__name__ __module__ __qualname____doc__ staticmethodrrrrrrrrrrArArArBr-s&c@seZdZdZdZdZdZdS)r.z; Common key names in the metadata header of images filename_or_objZ patch_index spatial_shapeN)rrrrZFILENAME_OR_OBJZ PATCH_INDEX SPATIAL_SHAPErArArArBr.)szstr | Sequence[str])rLkeywordsrHcs0t|s dSt|tfddt|DS)zk Return a boolean indicating whether the given callable `obj` has the `keywords` in its signature. Fc3s|]}|jkVqdSrI) parameters)rorsigrArBrw:szhas_option..)callabler signatureallr)rLrrArrBr03s cCs&dtt|}|j|ko$t||jS)a!Determine if a module's version is at least equal to the given value. Args: module: imported module's name, e.g., `np` or `torch`. version: required version, given as a tuple, e.g., `(1, 8, 0)`. Returns: `True` if module is the given version or newer. re)rmaprQ __version__r)moduleversionZtest_verrArArBr/=s r)datar` as_indices slicevalsrHcGs4tdgt|j}|r|nt|||<|t|S)asample several slices of input numpy array or Tensor on specified `dim`. Args: data: input data to sample slices, can be numpy array or PyTorch Tensor. dim: expected dimension index to sample slices, default to `1`. as_indices: if `True`, `slicevals` arg will be treated as the expected indices of slice, like: `1, 3, 5` means `data[..., [1, 3, 5], ...]`, if `False`, `slicevals` arg will be treated as args for `slice` func, like: `1, None` means `data[..., [1:], ...]`, `1, 5` means `data[..., [1: 5], ...]`. slicevals: indices of slices or start and end indices of expected slices, depends on `as_indices` flag. N)rZrcshaperX)rr`rrslicesrArArBr1Js r)path create_dirrHcCs<t|}|j}|s8|r(|jddntd|ddS)a Utility to check whether the parent directory of the `path` exists. Args: path: input path to check the parent directory. create_dir: if True, when the parent directory doesn't exist, create the directory, otherwise, raise exception. T)parentsz1the directory of specified path does not exist: `z`.N)r parentexistsmkdirrj)rrZpath_dirrArArBr2\s objectzCallable | None)rLrratomicr~kwargsrHc Kst|}t||d|r&t||dkr4tj}|sP|f||d|dSzRt@}t||j }|f||d|| rt t ||W5QRXWntk rYnXdS)a Save an object to file with specified path. Support to serialize to a temporary file first, then move to final destination, so that files are guaranteed to not be damaged if exception occurs. Args: obj: input object data to save. path: target file path to save the input object. create_dir: whether to create dictionary of the path if not existing, default to `True`. atomic: if `True`, state is serialized to a temporary file first, then move to final destination. so that files are guaranteed to not be damaged if exception occurs. default to `True`. func: the function to save file, if None, default to `torch.save`. kwargs: other args for the save `func` except for the checkpoint and filename. default `func` is `torch.save()`, details of other args: https://pytorch.org/docs/stable/generated/torch.save.html. )rrN)rLr)r r2rrremover^savetempfileTemporaryDirectoryris_fileshutilmoverQPermissionError)rLrrrr~rtempdir temp_pathrArArBr3os"   zlist | np.ndarrayr)rzrHcCstttt|S)z Compute the union of class IDs in label and generate a list to include all class IDs Args: x: a list of numbers (for example, class_IDs) Returns a list showing the union (the union the class IDs) )rsetunionr\arrayriryrArArBr4s ?z torch.TensorrV)rzsigmoid thresholdrrHcKs|stj|f|S||kS)z Compute the lab from the probability of predicted feature maps Args: sigmoid: If the sigmoid function should be used. threshold: threshold value to activate the sigmoid function. )r^argmaxrU)rzrrrrArArB prob2classsrrQ)rrHcCst|S)z Convert a file path to URI. if not absolute path, will convert to absolute path first. Args: path: input file path to convert, can be a string or `Path` object. )r absoluteas_uri)rrArArBr5s)rn_linesrHcCspt|d}t|d}t||ddkrft||d}|d|d|dg|| d}d|S) z Pretty print the head and tail ``n_lines`` of ``val``, and omit the middle part if the part has more than 3 lines. Returns: the formatted string. Tr<Nz ... omitted z line(s) r)pprintpformat splitlinesmaxrcr)rrval_strZhidden_nrArArBr6s  (zSequence[tuple[Any, Any]]zdict[Any, Any]) ordered_pairsrHcCsft}|D]R\}}||krRtjdddkr>td|dq\td|dq ||q t|S)a Checks if there is a duplicated key in the sequence of `ordered_pairs`. If there is - it will log a warning or raise ValueError (if configured by environmental var `MONAI_FAIL_ON_DUPLICATE_CONFIG==1`) Otherwise, it returns the dict made from this sequence. Satisfies a format for an `object_pairs_hook` in `json.load` Args: ordered_pairs: sequence of (key, value) MONAI_FAIL_ON_DUPLICATE_CONFIGrrDuplicate key: ``) r rrrrjrraddrk)rrmrp_rArArBr7s   cseZdZdfdd ZZS)r8Fcszt}|jD]^\}}|j||d}||kr`tjdddkrNtd|dntd|d| |q t ||S)N)deepr rrr!r") r rZconstruct_objectrrrrjrrr#superconstruct_mapping)selfnoder%mappingZkey_noder$r __class__rArBr's z.CheckKeyDuplicatesYamlLoader.construct_mapping)F)rrrr' __classcell__rArAr+rBr8sc@seZdZdZdddddZddd d d d d ddddddddZdddgZddddddZddZd d!Z d"d#Z d$d%d&d'd(Z d)S)*r9z Convert the values from input unit to the target unit Args: input_unit: the unit of the input quantity target_unit: the unit of the target quantity g F%u?gׁ?gB?gL7A`%@)inchfootZyardZmile rrr<iiii)ZpetaZteraZgigaZmegaZkiloZhectoZdecaZdeciZcentimillimicronanoZpicoZfemtometerbytebitrQr) input_unit target_unitrHcCsP||\|_}||\|_}||kr0||_ntd|d||dS)NzPBoth input and target units should be from the same quantity. Input quantity is z while target quantity is )_get_valid_unit_and_baser=r> unit_baserj_calculate_conversion_factor)r(r=r>Z input_baseZ target_baserArArB__init__szConvertUnits.__init__cCsTt|}||jkr|dfS|jD]}||r$||fSq$td|ddS)Nr:z3Currently, it only supports length conversion but `z ` is given.)rQrimperial_unit_of_length base_unitsendswithrj)r(unitZ base_unitrArArBr?!s    z%ConvertUnits._get_valid_unit_and_basecCs@||jkrt|j|S|dt|j}|dkr6dS|j|S)zDCalculate the power of the unit factor with respect to the base unitNr?)rCrrcr@ unit_prefix)r(rFprefixrArArB_get_unit_power*s  zConvertUnits._get_unit_powercCs:|j|jkrdS||j}||j}d|||_dS)z?Calculate unit conversion factor with respect to the input unitrG N)r=r>rJconversion_factor)r(Z input_powerZ target_powerrArArBrA4s    z)ConvertUnits._calculate_conversion_factorz int | floatr )rrHcCst||jSrI)rVrL)r(rrArArB__call__<szConvertUnits.__call__N) rrrrrCrHrDrBr?rJrArMrArArArBr9s.     cCs:t|j}t|jdh}t|}||}|tk|fS)a  Check if the all keys in kwargs exist in the __init__ method of the class. Args: cls: the class to check. kwargs: kwargs to examine. Returns: a boolean indicating if all keys exist. a set of extra keys that are not used in the __init__. r()rrrBr r)clsrZinit_signatureZ init_params input_kwargs extra_kwargsrArArBr:@s z list[str]zsubprocess.CompletedProcess)cmd_listrrHc Kst}|d||d<|ddrBddl}|jjd|zt j |f|WSt j k r}zN|slt |j jdd}t |jjdd}td |jd |d |d |W5d}~XYnXdS) a Run a command by using ``subprocess.run`` with capture_output=True and stderr=subprocess.STDOUT so that the raise exception will have that information. The argument `capture_output` can be set explicitly if desired, but will be overriden with the debug status from the variable. Args: cmd_list: a list of strings describing the command to run. kwargs: keyword arguments supported by the ``subprocess.run`` method. Returns: a CompletedProcess instance after the command completes. capture_outputZrun_cmd_verboseFrNr;replace)errorszsubprocess call error z: rre)r-rrpopmonaiappsutils get_loggerinfo subprocessrunCalledProcessErrorrQstdoutdecodestderr RuntimeError returncode)rQrrrVeoutputrTrArArBr;Ts  zSequence[int] | int)numrHcCs6t|}dd|D}ddt||D}t||kS)zS Determine if the input is a square number or a squence of square numbers. cSsg|]}tt|qSrA)rUmathsqrt)ro_numrArArBrwszis_sqrt..cSsg|]\}}||qSrArA)ro_iZ_jrArArBrxs)rr>)reZsqrt_numretrArArBis_sqrtrsrk)arrrMrHcCs|dd||jS)zMAppend 1-sized dimensions to `arr` to create a result with `ndim` dimensions.).rIrMrlrMrArArBunsqueeze_right|srocCs|d||jS)zNPrepend 1-sized dimensions to `arr` to create a result with `ndim` dimensions.rIrmrnrArArBunsqueeze_leftsrp)N)F)rF)NrF)TF)FT)T)r<T)T)TTN)Fr)r)` __future__rrrCrfrrrrr[rrrastrcollections.abcrrrdistutils.utilrrpathlibr typingr r r r rrhr\r^Zmonai.config.type_definitionsrrrmonai.utils.modulerrrrr$__all__rrrrrrriinfouint32rZNP_MAXr)rrrrErrrrrr r!r"r#r$r%r&r'r(r*r+r,r-r.r0r/r1r2r3r4rr5r6r7r8r9r:r;rkrorprArArArB s     &      # .=!"# +   0   I