o  i@sddlmZddlZddlmZmZddlmZddlm Z ddl m Z ddl mZddlm Z ddlmZmZmZdd lmZdd lmZdd lmZmZdd lmZgd ZGddde ZGdddeZGdddeZ e Z!Z"dS)) annotationsN)CallableSequence)Any)Dataset) DataLoader)KeysCollection)decollate_batch no_collationpad_list_data_collate)PadListDataCollate)InvertibleTransform) MapTransform Transform)first)BatchInverseTransform Decollated DecollateD DecollateDictc@s*eZdZdd d Zdd dZdddZdS)_BatchInverseDatasetdata Sequence[Any] transformr pad_collation_usedboolreturnNonecCs||_||_||_dSN)rinvertible_transformr)selfrrrr j/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/inverse_batch_transform.py__init__"s z_BatchInverseDataset.__init__indexintcCsDt|j|}|jrt|}t|jtst d|S|j|S)NzGtransform is not invertible, can't invert transform for the input data.) dictrrr inverse isinstancerr warningswarn)rr#rr r r! __getitem__'s    z _BatchInverseDataset.__getitem__cCs t|jSr)lenr)rr r r!__len__2s z_BatchInverseDataset.__len__N)rrrr rrrr)r#r$)rr$)__name__ __module__ __qualname__r"r*r,r r r r!r s   rc@s0eZdZdZeddddfdddZdddZdS)rz Perform inverse on a batch of data. This is useful if you have inferred a batch of images and want to invert them all. rTNrr loaderTorchDataLoader collate_fnCallable | None num_workers int | Nonedetachr pad_batchrrcCsZ||_|j|_|dur|jn||_||_||_||_||_|jjtjkp)t |jt |_ dS)a Args: transform: a callable data transform on input data. loader: data loader used to run `transforms` and generate the batch of data. collate_fn: how to collate data after inverse transformations. default won't do any collation, so the output will be a list of size batch size. num_workers: number of workers when run data loader for inverse transforms, default to 0 as only run 1 iteration and multi-processing may be even slower. if the transforms are really slow, set num_workers for multi-processing. if set to `None`, use the `num_workers` of the transform data loader. detach: whether to detach the tensors. Scalars tensors will be detached into number types instead of torch tensors. pad_batch: when the items in a batch indicate different batch size, whether to pad all the sequences to the longest. If False, the batch size will be the length of the shortest sequence. fill_value: the value to fill the padded sequences when `pad_batch=True`. N) r batch_sizer4r2r6r7 fill_value__doc__r r'r r)rrr0r2r4r6r7r9r r r!r"<s zBatchInverseTransform.__init__rdict[str, Any]rc Cst||j|j|jd}t||j|j}t||j|j |j d}zt |WSt y@}zt |}d|vr7|d7}t ||d}~ww)Nr6padr9)r8r4r2z equal sizezP MONAI hint: try creating `BatchInverseTransform` with `collate_fn=lambda x: x`.)r r6r7r9rrrrr8r4r2r RuntimeErrorstr)rrZdecollated_dataZinv_dsZ inv_loaderrere_strr r r!__call__cs  zBatchInverseTransform.__call__)rr r0r1r2r3r4r5r6rr7rrr)rr;rr)r-r.r/r:r r"rBr r r r!r6s  'rcs8eZdZdZ     ddfd d ZdddZZS)ra? Decollate a batch of data. If input is a dictionary, it also supports to only decollate specified keys. Note that unlike most MapTransforms, it will delete the other keys that are not specified. if `keys=None`, it will decollate all the data in the input. It replicates the scalar values to every item of the decollated list. Args: keys: keys of the corresponding items to decollate, note that it will delete other keys not specified. if None, will decollate all the keys. see also: :py:class:`monai.transforms.compose.MapTransform`. detach: whether to detach the tensors. Scalars tensors will be detached into number types instead of torch tensors. pad_batch: when the items in a batch indicate different batch size, whether to pad all the sequences to the longest. If False, the batch size will be the length of the shortest sequence. fill_value: the value to fill the padded sequences when `pad_batch=True`. allow_missing_keys: don't raise exception if key is missing. NTFkeysKeysCollection | Noner6rr7allow_missing_keysrrcs$t||||_||_||_dSr)superr"r6r7r9)rrCr6r7r9rE __class__r r!r"s zDecollated.__init__r dict | listcCsjt|jdkr|jddur|}nt|tstdi}||D]}||||<q!t||j|j|j dS)Nrz@input data is not a dictionary, but specified keys to decollate.r<) r+rCr'r% TypeError key_iteratorr r6r7r9)rrdkeyr r r!rBs zDecollated.__call__)NTTNF) rCrDr6rr7rrErrr)rrI)r-r.r/r:r"rB __classcell__r r rGr!rrs r)# __future__rr(collections.abcrrtypingrtorch.utils.datartorch.utils.data.dataloaderrr1 monai.configrZmonai.data.dataloadermonai.data.utilsr r r monai.transforms.croppad.batchr monai.transforms.inverser monai.transforms.transformrr monai.utilsr__all__rrrrrr r r r!s$         < 0