U Ph@sddlmZddlZddlmZmZddlmZddlm Z ddl m Z ddl mZddlm Z ddlmZmZmZdd lmZdd lmZdd lmZmZdd lmZd dddgZGddde ZGdd d eZGdddeZ e Z!Z"dS)) annotationsN)CallableSequence)Any)Dataset) DataLoader)KeysCollection)decollate_batch no_collationpad_list_data_collate)PadListDataCollate)InvertibleTransform) MapTransform Transform)firstBatchInverseTransform Decollated DecollateD DecollateDictc@s<eZdZdddddddZdd d d Zdd d dZdS)_BatchInverseDatasetz Sequence[Any]r boolNone)data transformpad_collation_usedreturncCs||_||_||_dSN)rinvertible_transformr)selfrrrr]/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/inverse_batch_transform.py__init__"sz_BatchInverseDataset.__init__int)indexcCsDt|j|}|jrt|}t|jts8t d|S|j|S)NzGtransform is not invertible, can't invert transform for the input data.) dictrrr inverse isinstancerr warningswarn)rr#rrrr __getitem__'s   z _BatchInverseDataset.__getitem__)rcCs t|jSr)lenr)rrrr __len__2sz_BatchInverseDataset.__len__N)__name__ __module__ __qualname__r!r)r+rrrr r s rc @sFeZdZdZeddddfddddd d d d d d ZdddddZdS)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. rTNr TorchDataLoaderzCallable | Nonez int | Nonerr)rloader collate_fn num_workersdetach pad_batchrcCsZ||_|j|_|dkr|jn||_||_||_||_||_|jjtjkpRt |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_sizer2r1r3r4 fill_value__doc__r r&r r)rrr0r1r2r3r4r6rrr r!<szBatchInverseTransform.__init__zdict[str, Any]r)rrc Cst||j|j|jd}t||j|j}t||j|j |j d}z t |WSt k r}z&t |}d|krp|d7}t ||W5d}~XYnXdS)Nr3padr6)r5r2r1z equal sizezP MONAI hint: try creating `BatchInverseTransform` with `collate_fn=lambda x: x`.)r r3r4r6rrrrr5r2r1r RuntimeErrorstr)rrZdecollated_dataZinv_dsZ inv_loaderrere_strrrr __call__cs zBatchInverseTransform.__call__)r,r-r.r7r r!r>rrrr r6s 'cs>eZdZdZdddddddfd d Zd d d dZZS)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. NTFzKeysCollection | Nonerr)keysr3r4allow_missing_keysrcs$t||||_||_||_dSr)superr!r3r4r6)rr?r3r4r6r@ __class__rr r!szDecollated.__init__z dict | list)rcCsjt|jdkr"|jddkr"|}n2t|ts4tdi}||D]}||||<qBt||j|j|j dS)Nrz@input data is not a dictionary, but specified keys to decollate.r8) r*r?r&r$ TypeError key_iteratorr r3r4r6)rrdkeyrrr r>s zDecollated.__call__)NTTNF)r,r-r.r7r!r> __classcell__rrrBr rrs )# __future__rr'collections.abcrrtypingrtorch.utils.datartorch.utils.data.dataloaderrr/ monai.configrZmonai.data.dataloadermonai.data.utilsr r r monai.transforms.croppad.batchr monai.transforms.inverser monai.transforms.transformrr monai.utilsr__all__rrrrrrrrr  s"          <0