o # iH@sddlmZddlZddlZddlmZmZddlZddl Z ddl m Z ddl m Z ddlmZddlmZmZddlmZmZmZdd lmZmZmZdd lmZmZmZgd ZGd d d e Z Gdddee Z!Gddde Z"dS)) annotationsN)CallableSequence)Dataset) MetaTensor)iter_patch_position) BaseWSIReader WSIReader)ForegroundMask Randomizableapply_transform)convert_to_dst_type ensure_tupleensure_tuple_rep) CommonKeys ProbMapKeys WSIPatchKeys)PatchWSIDatasetSlidingPatchWSIDatasetMaskedPatchWSIDatasetcsxeZdZdZ       d&d'fdd Zd(ddZd(ddZd(ddZd(ddZd(ddZ d(d d!Z d)d$d%Z Z S)*ra' This dataset extracts patches from whole slide images (without loading the whole image) It also reads labels for each patch and provides each patch with its associated class labels. Args: data: the list of input samples including image, location, and label (see the note below for more details). patch_size: the size of patch to be extracted from the whole slide image. patch_level: the level at which the patches to be extracted (default to 0). transform: transforms to be executed on input data. include_label: whether to load and include labels in the output center_location: whether the input location information is the position of the center of the patch additional_meta_keys: the list of keys for items to be copied to the output metadata from the input data reader: the module to be used for loading whole slide imaging. If `reader` is - a string, it defines the backend of `monai.data.WSIReader`. Defaults to cuCIM. - a class (inherited from `BaseWSIReader`), it is initialized and set as wsi_reader. - an instance of a class inherited from `BaseWSIReader`, it is set as the wsi_reader. kwargs: additional arguments to pass to `WSIReader` or provided whole slide reader class Returns: dict: a dictionary of loaded image (in MetaTensor format) along with the labels (if requested). {"image": MetaTensor, "label": torch.Tensor} Note: The input data has the following form as an example: .. code-block:: python [ {"image": "path/to/image1.tiff", "location": [200, 500], "label": 0}, {"image": "path/to/image2.tiff", "location": [100, 700], "patch_size": [20, 20], "patch_level": 2, "label": 1} ] NTcuCIMdatar patch_sizeint | tuple[int, int] | None patch_level int | None transformCallable | None include_labelboolcenter_locationadditional_meta_keysSequence[str] | Nonec  st|||durd|_nt|d|_||_|durd}|t|tr1td||d| |_n&t |rFt |t rF|dd|i| |_nt|t rO||_nt d|d|jj|_||_||_|peg|_i|_dS)Nr)backendlevelr%zUnsupported reader type: .)super__init__rrr isinstancestrr wsi_readerinspectisclass issubclassr ValueErrorr$rr r!wsi_object_dict) selfrrrrrr r!readerkwargs __class__r'Y/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/data/wsi_datasets.pyr)Es(       zPatchWSIDataset.__init__sampledictcCs0|tj}||jvr|j||j|<|j|SN)rIMAGEr1r,read)r2r8 image_pathr'r'r7_get_wsi_objectrs   zPatchWSIDataset._get_wsi_objectcCstj|tjtjdS)N)dtype)torchtensorrLABELfloat32r2r8r'r'r7 _get_labelxszPatchWSIDataset._get_labelcs>|jr|tfddttDSttjS)Nc3s*|]}tj||dVqdS)r#N)rLOCATION).0ir8sizer'r7 ~s(z0PatchWSIDataset._get_location..)r _get_sizerrangelenrrFrDr'rIr7 _get_location{s  zPatchWSIDataset._get_locationcCs|jdur |tjdS|jS)Nr)rgetrLEVELrDr'r'r7 _get_levels zPatchWSIDataset._get_levelcCs"|jdurt|tjdS|jS)Nr#)rrrPrSIZErDr'r'r7rLs zPatchWSIDataset._get_sizecCsL|jdkri|_||}||}||}||}|jj||||dS)NZ openslide)wsilocationrJr%)r$r1r>rOrRrLr,get_data)r2r8wsi_objrUr%rJr'r'r7 _get_datas     zPatchWSIDataset._get_dataindexintcCsn|j|}||\}}|jD]}||||<qtjt||di}|jr,|||tj<|j r5t |j |S|S)N)meta) rrXr!rr;rrrErBrr )r2rYr8imagemetadatakeyoutputr'r'r7 _transforms  zPatchWSIDataset._transform)NNNTTNr)rrrrrrrrrrr rr!r")r8r9)rYrZ) __name__ __module__ __qualname____doc__r)r>rErOrRrLrXr` __classcell__r'r'r5r7r s"' -      rc sXeZdZdZdddddddddejejejfddf d$fdd Zd d!Z d"d#Z Z S)%rav This dataset extracts patches in sliding-window manner from whole slide images (without loading the whole image). It also reads labels for each patch and provides each patch with its associated class labels. Args: data: the list of input samples including image, location, and label (see the note below for more details). patch_size: the size of patch to be extracted from the whole slide image. patch_level: the level at which the patches to be extracted (default to 0). mask_level: the resolution level at which the mask/map is created (for `ProbMapProducer` for instance). overlap: the amount of overlap of neighboring patches in each dimension (a value between 0.0 and 1.0). If only one float number is given, it will be applied to all dimensions. Defaults to 0.0. offset: the offset of image to extract patches (the starting position of the upper left patch). offset_limits: if offset is set to "random", a tuple of integers defining the lower and upper limit of the random offset for all dimensions, or a tuple of tuples that defines the limits for each dimension. transform: transforms to be executed on input data. include_label: whether to load and include labels in the output center_location: whether the input location information is the position of the center of the patch additional_meta_keys: the list of keys for items to be copied to the output metadata from the input data reader: the module to be used for loading whole slide imaging. Defaults to cuCIM. If `reader` is - a string, it defines the backend of `monai.data.WSIReader`. - a class (inherited from `BaseWSIReader`), it is initialized and set as wsi_reader, - an instance of a class inherited from `BaseWSIReader`, it is set as the wsi_reader. seed: random seed to randomly generate offsets. Defaults to 0. kwargs: additional arguments to pass to `WSIReader` or provided whole slide reader class Note: The input data has the following form as an example: .. code-block:: python [ {"image": "path/to/image1.tiff"}, {"image": "path/to/image2.tiff", "patch_size": [20, 20], "patch_level": 2} ] Unlike `MaskedPatchWSIDataset`, this dataset does not filter any patches. Nrg)rrFrrrrrrr mask_levelrZoverlaptuple[float, float] | floatoffsettuple[int, int] | int | str offset_limits@tuple[tuple[int, int], tuple[int, int]] | tuple[int, int] | Nonerrrrr r! Sequence[str]seedc stjd g|||| | | | d|||_|| d|_t|tr`|dkrXd|_||dur3d|_n3t|trTt|dt rE||f|_n!t|dtrP||_nt dt dt d|d t |d |_ ||_ |t||_|jD] }||}|j|qsdS) Nrrrrrr r!r3FrandomTrzUThe offset limits should be either a tuple of integers or tuple of tuple of integers.z$The offset limits should be a tuple.zInvalid string for offset "zc". It should be either "random" as a string,an integer, or a tuple of integers defining the offset.r#r')r(r)rgset_random_state random_offsetr*r+rktuplerZr0rrirflist image_data_evaluate_patch_locationsrextend)r2rrrrfrgrirkrrr r!r3rnr4r8 patch_samplesr5r'r7r)sR          zSlidingPatchWSIDataset.__init__csLjr#jdurtdd|D}nj}tfdd|DSjS)Ncss|]}| |fVqdSr:r')rGsr'r'r7rKsz5SlidingPatchWSIDataset._get_offset..c3s"|] \}}j||VqdSr:)Rrandint)rGlowhighr2r'r7rKs )rrrkrsrLri)r2r8rkr'r~r7 _get_offsets  z"SlidingPatchWSIDataset._get_offsetc s|}|}|}|j|d}|j||j}|j||tfdd|D}| }tt t ||||j dd} t | |dt|} |tjj<|tjj<tjtjtjj<t| tjj<t|j||jtjj<fddt| | DS)z@Calculate the location for each patch in a sliding-window mannerrcg|]}|qSr'r'rGp patch_ratior'r7 )zDSlidingPatchWSIDataset._evaluate_patch_locations..F) image_sizer start_posrgpaddedr#c2g|]\}}itjjt|tjj|iqSr'rrFvaluenparrayrrGlocZmask_locr8r'r7r; )rLrRr>r,get_sizeget_downsample_ratiorfrrrrtrrgroundfloatrrSrrQospathbasenamerr;rNAMErNCOUNTzip) r2r8rrrWZwsi_size mask_ratio patch_size_0ripatch_locationsmask_locationsr'rr8r7rvs0        z0SlidingPatchWSIDataset._evaluate_patch_locations)rrrrrrrfrZrgrhrirjrkrlrrrrr rr!rmrnrZ) rarbrcrdrrFrSrr)rrvrer'r'r5r7rs"+A rcsDeZdZdZddddddejejfdfdfdd ZddZZ S)ra4 This dataset extracts patches from whole slide images at the locations where foreground mask at a given level is non-zero. Args: data: the list of input samples including image, location, and label (see the note below for more details). patch_size: the size of patch to be extracted from the whole slide image. patch_level: the level at which the patches to be extracted (default to 0). mask_level: the resolution level at which the mask is created. transform: transforms to be executed on input data. include_label: whether to load and include labels in the output center_location: whether the input location information is the position of the center of the patch additional_meta_keys: the list of keys for items to be copied to the output metadata from the input data reader: the module to be used for loading whole slide imaging. Defaults to cuCIM. If `reader` is - a string, it defines the backend of `monai.data.WSIReader`. - a class (inherited from `BaseWSIReader`), it is initialized and set as wsi_reader, - an instance of a class inherited from `BaseWSIReader`, it is set as the wsi_reader. kwargs: additional arguments to pass to `WSIReader` or provided whole slide reader class Note: The input data has the following form as an example: .. code-block:: python [ {"image": "path/to/image1.tiff"}, {"image": "path/to/image2.tiff", "size": [20, 20], "level": 2} ] NFrrrrrrrrfrZrrrrr r!rmc s^tjdg||||||| d| ||_|t||_|jD] } || } |j| qdS)Nror')r(r)rfrtrurvrrw) r2rrrrfrrr r!r3r4r8rxr5r'r7r)cs&      zMaskedPatchWSIDataset.__init__c s*|}|}|}|jj||jd\}}ttt ddid||dd}t | j }|j ||j} |j ||tfdd|D} t|d t| | d t} |tjj<|tjj<tjtjtjj<t| tjj<|j tjj<fd dt!| |DS) zUCalculate the location for each patch based on the mask at different resolution level)r%Sotsu) hsv_threshold)dstrcrr'r'rrr'r7rrzCMaskedPatchWSIDataset._evaluate_patch_locations..g?r#crr'rrrr'r7rr)"rLrRr>r,rVrfrsqueezer r vstacknonzeroTrrrrastyperZrrSrrQrrrrr;rrrNrshaper) r2r8rrrWrT_maskrrrrr'rr7rvs$   $$   z/MaskedPatchWSIDataset._evaluate_patch_locations)rrrrrrrfrZrrrrr rr!rm) rarbrcrdrrFrr)rvrer'r'r5r7rAs$ !r)# __future__rr-rcollections.abcrrnumpyrr@ monai.datarmonai.data.meta_tensorrmonai.data.utilsrZmonai.data.wsi_readerrr monai.transformsr r r monai.utilsr rrmonai.utils.enumsrrr__all__rrrr'r'r'r7s&