o  id@sNddlmZddlZddlmZddlmZddlmZddl m Z ddl Z ddl Z ddlmZmZmZddlmZmZdd lmZdd lmZmZmZmZmZmZmZdd lm Z ed d d\Z!Z"eddd\Z#Z"gdZ$GdddeZ%Gddde%Z&eddGddde%Z'ed dGddde%Z(eddGddde%Z)dS)) annotationsN)abstractmethod)Sequence)abspath)Any) DtypeLikeNdarrayOrTensorPathLike) ImageReader _stack_images)is_supported_format) WSIPatchKeysdtype_numpy_to_torchdtype_torch_to_numpy ensure_tupleensure_tuple_repoptional_import require_pkg) ConvertUnits openslide OpenSlidenametifffileTiffFile) BaseWSIReader WSIReaderCuCIMWSIReaderOpenSlideWSIReaderTiffFileWSIReaderc seZdZUdZgZded<dZddddddddejdd f dRfdd Z ddZ d d!Z e dSd$d%Z dTd-d.ZdUd/d0Ze dVd1d2Ze dWd3d4Ze dXd5d6Ze dYd8d9Ze dWd:d;Ze dZd@dAZd[dEdFZ G     d\d]dKdLZd^dPdQZZS)_ra An abstract class that defines APIs to load patches from whole slide image files. Args: level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. mpp_rtol: the acceptable relative tolerance for resolution in micro per pixel. mpp_atol: the acceptable absolute tolerance for resolution in micro per pixel. power: the objective power at which the patches are extracted. power_rtol: the acceptable relative tolerance for objective power. power_atol: the acceptable absolute tolerance for objective power. channel_dim: the desired dimension for color channel. dtype: the data type of output image. device: target device to put the extracted patch. Note that if device is "cuda"", the output will be converted to torch tenor and sent to the gpu even if the dtype is numpy. mode: the output image color mode, e.g., "RGB" or "RGBA". kwargs: additional args for the reader Notes: Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided. If such parameters are provided in `get_data` method, those will override the values provided here. If none of them are provided here or in `get_data`, `level=0` will be used. Typical usage of a concrete implementation of this class is: .. code-block:: python image_reader = MyWSIReader() wsi = image_reader.read(filepath, **kwargs) img_data, meta_data = image_reader.get_data(wsi) - The `read` call converts an image filename into whole slide image object, - The `get_data` call fetches the image data, as well as metadata. The following methods needs to be implemented for any concrete implementation of this class: - `read` reads a whole slide image object from a given file - `get_size` returns the size of the whole slide image of a given wsi object at a given level. - `get_level_count` returns the number of levels in the whole slide image - `_get_patch` extracts and returns a patch image form the whole slide image - `_get_metadata` extracts and returns metadata for a whole slide image and a specific patch. z list[str]supported_suffixesN皙?rRGBlevel int | Nonempp"float | tuple[float, float] | Nonempp_rtolfloatmpp_atolpower power_rtol power_atol channel_dimintdtypeDtypeLike | torch.dtypedevicetorch.device | str | Nonemodestrc  svt||_||_|| || | |_| |_|dur$t|dnd|_ ||_ ||_ ||_ ||_ ||_i|_dS)N)super__init__r%r/ set_dtype set_devicer5kwargsrr'r,r)r+r-r.metadata) selfr%r'r)r+r,r-r.r/r1r3r5r< __class__W/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/data/wsi_reader.pyr9\s    zBaseWSIReader.__init__cCs*|t|tjr ||_dSt||_dS)N) isinstancetorchr1np)r>r1rArArBr:zs  zBaseWSIReader.set_dtypecCs6|dus t|tjtfr||_dStdt|d)Nz5`device` must be `torch.device`, `str` or `None` but z is given.)rCrDr3r6 ValueErrortype)r>r3rArArBr;s zBaseWSIReader.set_devicereturntuple[int, int]cCtd|jjd) Returns the size (height, width) of the whole slide image at a given level. Args: wsi: a whole slide image object loaded from a file. level: the level number where the size is calculated. Subclass  must implement this method.NotImplementedErrorr@__name__r>wsir%rArArBget_size zBaseWSIReader.get_sizervaluetuplevalue_at_levelsSequence[tuple]atolrtolcs|vr |St|fddd}ttD]<}t|||||t|krUtd|dd|d|d|d |d |d |d |d |d|dq||S)aFind the level corresponding to the value of the quantity in the list of values at each level. Args: name: the name of the requested quantity value: the value of requested quantity value_at_levels: list of value of the quantity at each level atol: the tolerance for the value rtol: relative tolerance for the value cstddt|DS)NcSsg|] \}}t||qSrA)abs).0xyrArArB zGBaseWSIReader._find_closest_level....)sumzip)a_valuerUrArBz3BaseWSIReader._find_closest_level..keyzThe requested z < z2 > does not exist in this whole slide image (with z_rtol=z and z_atol=z!). Here is the list of available z: z!. The closest matching available z is z8.Please consider changing the tolerances or use another .)indexminrangelenr[rF)r>rrUrWrYrZZ closest_valueirArdrB_find_closest_levels8 ( z!BaseWSIReader._find_closest_levelc s(|dur|dur|durj}j}j}ddd|fd|fd|ffD}t|dkr3td|d }|durZt|d }fd dt|D}d||j j }|S|dur{t |} fd dt|D} d| | j j }|S|durd }||krtd|dd|d|S)a4 Returns the level associated to the resolution parameters in the whole slide image. Args: wsi: a whole slide image object loaded from a file. level: the level number. mpp: the micron-per-pixel resolution. power: the objective power. NcSs g|] }|ddur|dqS)NrrA)r\valrArArBr_s z1BaseWSIReader.get_valid_level..r%r'r,rpz;Only one of `level`, `mpp`, or `power` should be provided. z are provided.r7csg|]}|qSrA)get_mppr\r%r>rRrArBr_rfcsg|] }|fqSrA) get_powerrsrtrArBr_srz#The maximum level of this image is z while level=z is requested)!)r'r,r%rmrFget_level_countrrlror+r)rr.r-) r>rRr%r'r, resolutionn_levelsmpp_Zavailable_mppsZpower_Zavailable_powersrArtrBget_valid_levels.     zBaseWSIReader.get_valid_levelcCrJ) Returns the number of levels in the whole slide image. Args: wsi: a whole slide image object loaded from a file. rLrMrNrtrArArBrvs zBaseWSIReader.get_level_countcCrJ) Returns the down-sampling ratio of the whole slide image at a given level. Args: wsi: a whole slide image object loaded from a file. level: the level number where the downsample ratio is calculated. rLrMrNrQrArArBget_downsample_ratiorTz"BaseWSIReader.get_downsample_ratiocCrJ)'Return the file path for the WSI objectrLrMrNrtrArArB get_file_pathszBaseWSIReader.get_file_pathtuple[float, float]cCrJ) Returns the micro-per-pixel resolution of the whole slide image at a given level. Args: wsi: a whole slide image object loaded from a file. level: the level number where the mpp is calculated. rLrMrNrQrArArBrrrTzBaseWSIReader.get_mppcCrJ) Returns the objective power of the whole slide image at a given level. Args: wsi: a whole slide image object loaded from a file. level: the level number where the objective power is calculated. rLrMrNrQrArArBrurTzBaseWSIReader.get_powerlocationsizer np.ndarraycCrJ)i Extracts and returns a patch image form the whole slide image. Args: wsi: a whole slide image object loaded from a file or a lis of such objects location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0). size: (height, width) tuple giving the patch size at the given level (`level`). If None, it is set to the full image size at the given level. level: the level number. dtype: the data type of output image. mode: the output image mode, 'RGB' or 'RGBA'. rLrMrNr>rRrrr%r1r5rArArB _get_patch szBaseWSIReader._get_patchpatchrdictcCs|jt|jks|jt|j krtd|jd|j|j|jdkr)t|jnd}d|jd|dt|jd||j|ddtjj dtj j | |tj j t |tjj t |tjj |i}|S) a4 Returns metadata of the extracted patch from the whole slide image. Args: wsi: the whole slide image object, from which the patch is loaded. patch: extracted patch from whole slide image. location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0). size: (height, width) tuple giving the patch size at the given level (`level`). If None, it is set to the full image size at the given level. level: the level number. zThe desired channel_dim (z#) is out of bound for image shape: rbackendoriginal_channel_dim spatial_shapeNrp)r/rmshaperFrrEarrayr COUNTrUPATHrLOCATIONasarraySIZELEVEL)r>rRrrrr%r/r=rArArB _get_metadata s"& zBaseWSIReader._get_metadatarrtuple[int, int] | None str | Nonetuple[np.ndarray, dict]c s|dur|j}g}g} t|ttfs|f}t|D]} || |||}|dur*d}|| d} |d| dks@|d| dkrJtd|d| |dur]|dkrVtd|| |}n|ddksi|ddkrptd|t|jt jr|t |jn|j} |j | |||| |d } t|jt js|j durt |j j d krt|jt jst|jn|j}| jd rt j| ||j d } n t j| ||j d } | jd krtd| jd|dkr| j|jdkrtd|d| j|jdn|dvr | j|jd kr td|d| j|jd|j| | |||d}|| | |qt|dkrZtdd| Ddkr9tdtdd| DdkrItdtD]fdd| D|<qKt|||fS) a; Verifies inputs, extracts patches from WSI image and generates metadata. Args: wsi: a whole slide image object loaded from a file or a list of such objects. location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0). size: (height, width) tuple giving the patch size at the given level (`level`). If not provided or None, it is set to the full image size at the given level. level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. power: the objective power at which the patches are extracted. dtype: the data type of output image. mode: the output image mode, 'RGB' or 'RGBA'. Returns: a tuples, where the first element is an image patch [CxHxW] or stack of patches, and second element is a dictionary of metadata. Notes: Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided. If none of them are provided, it uses the defaults that are set during class instantiation. If none of them are set here or during class instantiation, `level=0` will be used. Nrrrpz+Location is outside of the image: location=z , image size=z0Patch size should be defined to extract patches.z?Patch size should be greater than zero, provided: patch size = )rrr%r1r5cuda WRITEABLE)r1r3z(The image dimension should be 3 but has zI. `WSIReader` is designed to work only with 2D images with color channel.RGBAz6The image is expected to have four color channels in '' mode but has rir$z7The image is expected to have three color channels in '. )rRrrrr%cSsh|]}|dqS)rrAr\mrArArB z)BaseWSIReader.get_data..z:original_channel_dim is not consistent across wsi objects.cSsh|]}t|dqS)r)rVrrArArBrrfz3spatial_shape is not consistent across wsi objects.csg|]}|qSrArArrgrArBr_rz*BaseWSIReader.get_data..)r5rClistrVrrzrSrFr1rDrrr3rGrflags as_tensortensorndimrr/rappendrmr r )r>rRrrr%r'r,r5 patch_list metadata_listZeach_wsiwsi_sizedtype_nprZ dtype_torchr=rArgrBget_data@sr!         zBaseWSIReader.get_datafilenameSequence[PathLike] | PathLikeboolcCs t||jS)z Verify whether the specified file or files format is supported by WSI reader. The list of supported suffixes are read from `self.supported_suffixes`. Args: filename: filename or a list of filenames to read. )r r )r>rrArArB verify_suffixs zBaseWSIReader.verify_suffixr%r&r'r(r)r*r+r*r,r&r-r*r.r*r/r0r1r2r3r4r5r6r%r0rHrI) rr6rUrVrWrXrYr*rZr*rHr0)r%r&r'r(r,r&rHr0rHr0r%r0rHr*rHr6r%r0rHr rrIrrIr%r0r1rr5r6rHr) rrrrIrrIr%r0rHr)rNNNNN)rrIrrr%r&r'r(r,r&r5rrHr)rrrHr)rP __module__ __qualname____doc__r __annotations__rrEuint8r9r:r;rrSrorzrvr}rrrrurrrr __classcell__rArAr?rBr+sV  -  ,       # rrc @seZdZdZgdZdddddddddejddf d6ddZd7ddZd8d!d"Z d9d#d$Z d:d%d&Z d;d(d)Z d9d*d+Z dra} Read whole slide images and extract patches using different backend libraries Args: backend: the name of backend whole slide image reader library, the default is cuCIM. level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. mpp_rtol: the acceptable relative tolerance for resolution in micro per pixel. mpp_atol: the acceptable absolute tolerance for resolution in micro per pixel. power: the objective power at which the patches are extracted. power_rtol: the acceptable relative tolerance for objective power. power_atol: the acceptable absolute tolerance for objective power. channel_dim: the desired dimension for color channel. Default to 0 (channel first). dtype: the data type of output image. Defaults to `np.uint8`. device: target device to put the extracted patch. Note that if device is "cuda"", the output will be converted to torch tenor and sent to the gpu even if the dtype is numpy. mode: the output image color mode, "RGB" or "RGBA". Defaults to "RGB". num_workers: number of workers for multi-thread image loading (cucim backend only). kwargs: additional arguments to be passed to the backend library Notes: Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided. If such parameters are provided in `get_data` method, those will override the values provided here. If none of them are provided here or in `get_data`, `level=0` will be used. )cucimrrrNr"r#rr$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6c KsL||_||jdkr!td|||||||| | | | d | |_n=|jdkr;td|||||||| | | | d | |_n#|jdkrUtd|||||||| | | | d | |_n td|jd|jj|_|jj|_|jj |_ |jj |_ |jj |_ |jj |_ |jj |_ |jj|_|jj|_|jj|_|jj|_|jj|_|jj|_|jj|_dS)Nr) r%r'r)r+r,r-r.r/r1r3r5rrz?The supported backends are cucim, openslide, and tifffile but 'z ' was given.rA)lowerrrreaderrrrFr r%r)r+r,r-r.r/r1r3r5r<r=r')r>rr%r'r)r+r,r-r.r/r1r3r5r<rArArBr9s                   zWSIReader.__init__rHcC |j|Sr{)rrvrtrArArBrv/s zWSIReader.get_level_countrIcC|j||SrK)rrSrQrArArBrS9 zWSIReader.get_sizecCrr|)rr}rQrArArBr}DrzWSIReader.get_downsample_ratiocCrr~)rrrtrArArBrOs zWSIReader.get_file_pathrcCr)r)rrrrQrArArBrrSrzWSIReader.get_mppcCr)a Returns the micro-per-pixel resolution of the whole slide image at a given level. Args: wsi: a whole slide image object loaded from a file. level: the level number where the objective power is calculated. )rrurQrArArBru^rzWSIReader.get_powerrrrrcCs|jj||||||dS)ai Extracts and returns a patch image form the whole slide image. Args: wsi: a whole slide image object loaded from a file or a lis of such objects. location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0). size: (height, width) tuple giving the patch size at the given level (`level`). If None, it is set to the full image size at the given level. level: the level number. dtype: the data type of output image mode: the output image mode, 'RGB' or 'RGBA'. )rRrrr%r1r5)rrrrArArBriszWSIReader._get_patchdata*Sequence[PathLike] | PathLike | np.ndarraycKs|jjdd|i|S)aV Read whole slide image objects from given file or list of files. Args: data: file name or a list of file names to read. kwargs: additional args for the reader module (overrides `self.kwargs` for existing keys). Returns: whole slide image object or list of such objects. rNrA)rread)r>rr<rArArBr{s zWSIReader.readrrrrrrrrr)rPrrrsupported_backendsrErr9rvrSr}rrrrurrrArArArBrs0 R  rr)pkg_namecseZdZdZgdZdZd'd(fdd Zed)d d Zd*ddZ d+ddZ ed,ddZ d-ddZ d+ddZ d.ddZd/d%d&ZZS)0raE Read whole slide images and extract patches using cuCIM library. Args: level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. mpp_rtol: the acceptable relative tolerance for resolution in micro per pixel. mpp_atol: the acceptable absolute tolerance for resolution in micro per pixel. power: the objective power at which the patches are extracted. power_rtol: the acceptable relative tolerance for objective power. power_atol: the acceptable absolute tolerance for objective power. channel_dim: the desired dimension for color channel. Default to 0 (channel first). dtype: the data type of output image. Defaults to `np.uint8`. device: target device to put the extracted patch. Note that if device is "cuda"", the output will be converted to torch tenor and sent to the gpu even if the dtype is numpy. mode: the output image color mode, "RGB" or "RGBA". Defaults to "RGB". num_workers: number of workers for multi-thread image loading. kwargs: additional args for `cucim.CuImage` module: https://github.com/rapidsai/cucim/blob/main/cpp/include/cucim/cuimage.h Notes: Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided. If such parameters are provided in `get_data` method, those will override the values provided here. If none of them are provided here or in `get_data`, `level=0` will be used. tiftiffZsvsrr num_workersr0c stjdi|||_dSNrA)r8r9r)r>rr<r?rArBr9s zCuCIMWSIReader.__init__rHcCs |jdS)r{ level_count resolutionsrRrArArBrv zCuCIMWSIReader.get_level_countr%rIcCs$|jd|d|jd|dfS)rKlevel_dimensionsrprrrQrArArBrSs$ zCuCIMWSIReader.get_sizer*cCst|jd|S)r|level_downsamples)r*rrQrArArBr}s z#CuCIMWSIReader.get_downsample_ratior6cCtt|jSr)r6rpathrrArArBrzCuCIMWSIReader.get_file_pathrcCs|||}d|jvr|jdd}|r|t|fdSd|jvrL|jdd}|rLt|trLt|dkrL|drL|drL||d||dfStd) raperioZMPPr7rspacingrrpC`mpp` cannot be obtained for this file. Please use `level` instead.)r}r=getr*rCrrmrF)r>rRr%downsample_ratioryrArArBrrs  zCuCIMWSIReader.get_mppcCs>d|jvr|jdd}|r|||}t||Std)rrZAppMagzCurrently, cuCIM backend can obtain the objective power only for Aperio images. Please use `level` (or `mpp`) instead, or try OpenSlide backend.)r=rr}r*rFr>rRr%Zobjective_powerrrArArBrus   zCuCIMWSIReader.get_powerrrc Ksltddd\}}g}t|}|j}|||D]}||fi|} || qt|dkr2|S|dS)a Read whole slide image objects from given file or list of files. Args: data: file name or a list of file names to read. kwargs: additional args that overrides `self.kwargs` for existing keys. For more details look at https://github.com/rapidsai/cucim/blob/main/cpp/include/cucim/cuimage.h Returns: whole slide image object or list of such objects. rZCuImagerrpr)rrr<copyupdaterrm) r>rr<Z cuimage_cls_wsi_list filenameskwargs_rrRrArArBrs    zCuCIMWSIReader.readrrr1rr5rcCs|j|ddd|ddd||jd}tj||d}t|d|j}|dvrH|j|jdvr>td|d|j|jd t|gd |j}|S) aj Extracts and returns a patch image form the whole slide image. Args: wsi: a whole slide image object loaded from a file or a lis of such objects. location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0). size: (height, width) tuple giving the patch size at the given level (`level`). If None, it is set to the full image size at the given level. level: the level number. dtype: the data type of output image. mode: the output image mode, 'RGB' or 'RGBA'. N)rrr%rr1r$rr?The image is expected to have three or four color channels in 'rrrrpr7) read_regionrrErmoveaxisr/rrFtake)r>rRrrr%r1r5rrArArBrs zCuCIMWSIReader._get_patch)r)rr0rrrrrrrrPrrrr rr9 staticmethodrvrSr}rrrrurrrrArAr?rBrs      rceZdZdZgdZdZfddZed%dd Zd&d d Z d'ddZ ed(ddZ d)ddZ d'ddZ d*ddZd+d#d$ZZS),ra Read whole slide images and extract patches using OpenSlide library. Args: level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. mpp_rtol: the acceptable relative tolerance for resolution in micro per pixel. mpp_atol: the acceptable absolute tolerance for resolution in micro per pixel. power: the objective power at which the patches are extracted. power_rtol: the acceptable relative tolerance for objective power. power_atol: the acceptable absolute tolerance for objective power. channel_dim: the desired dimension for color channel. Default to 0 (channel first). dtype: the data type of output image. Defaults to `np.uint8`. device: target device to put the extracted patch. Note that if device is "cuda"", the output will be converted to torch tenor and sent to the gpu even if the dtype is numpy. mode: the output image color mode, "RGB" or "RGBA". Defaults to "RGB". kwargs: additional args for `openslide.OpenSlide` module. Notes: Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided. If such parameters are provided in `get_data` method, those will override the values provided here. If none of them are provided here or in `get_data`, `level=0` will be used. rrc tjdi|dSrr8r9r>r<r?rArBr9^zOpenSlideWSIReader.__init__rHr0cCs|jSr)rrrArArBrvas z"OpenSlideWSIReader.get_level_countr%rIcCs|j|d|j|dfS)rKrpr)rrQrArArBrSls zOpenSlideWSIReader.get_sizer*cCs |j|Sr)rrQrArArBr}wrz'OpenSlideWSIReader.get_downsample_ratior6cCrr)r6r _filenamerrArArBrrz OpenSlideWSIReader.get_file_pathrcCs|||}d|jvr,d|jvr,|jdr,|jdr,|t|jd|t|jdfSd|jvrld|jvrl|jdrl|jdrl|jd}|durQtdd}t|d}||t|jd||t|jdfStd ) rzopenslide.mpp-xzopenslide.mpp-yztiff.XResolutionztiff.YResolutionztiff.ResolutionUnitNzEThe resolution unit is missing, `micrometer` will be used as default. micrometerr)r} propertiesr*rwarningswarnrrF)r>rRr%runitconvert_to_micronrArArBrrs2       zOpenSlideWSIReader.get_mppcCs0|jd}|r|||}t||Std)rzopenslide.objective-powerzZObjective `power` cannot be obtained for this file. Please use `level` (or `mpp`) instead.)rrr}r*rFrrArArBrus   zOpenSlideWSIReader.get_powerrrcK\g}t|}|j}|||D]}t|fi|}||qt|dkr*|S|dSaC Read whole slide image objects from given file or list of files. Args: data: file name or a list of file names to read. kwargs: additional args that overrides `self.kwargs` for existing keys. Returns: whole slide image object or list of such objects. rpr)rr<rrrrrmr>rr<rrrrrRrArArBr    zOpenSlideWSIReader.readrrr1rr5rc CsP|j|ddd|ddd|d}||}tj||d}t|d|j}|S)rNr)rrr%r)rconvertrErrr/) r>rRrrr%r1r5Z pil_patchrrArArBrs $ zOpenSlideWSIReader._get_patchrrrrrrrrrArAr?rBr@s      ( rcr),ra Read whole slide images and extract patches using TiffFile library. Args: level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. mpp_rtol: the acceptable relative tolerance for resolution in micro per pixel. mpp_atol: the acceptable absolute tolerance for resolution in micro per pixel. channel_dim: the desired dimension for color channel. Default to 0 (channel first). dtype: the data type of output image. Defaults to `np.uint8`. device: target device to put the extracted patch. Note that if device is "cuda"", the output will be converted to torch tenor and sent to the gpu even if the dtype is numpy. mode: the output image color mode, "RGB" or "RGBA". Defaults to "RGB". kwargs: additional args for `tifffile.TiffFile` module. Notes: - Objective power cannot be obtained via TiffFile backend. - Only one of resolution parameters, `level` or `mpp`, should be provided. If such parameters are provided in `get_data` method, those will override the values provided here. If none of them are provided here or in `get_data`, `level=0` will be used. rrc rrrrr?rArBr9rzTiffFileWSIReader.__init__rHr0cCs t|jSr)rmpagesrrArArBrvrz!TiffFileWSIReader.get_level_countr%rIcCs|j|j|j|jfSr)r imagelengthZ imagewidthrQrArArBrS s zTiffFileWSIReader.get_sizer*cCs t|jdjt|j|jS)r|r)r*rr rQrArArBr}+s z&TiffFileWSIReader.get_downsample_ratior6cCstt|jjSr)r6rZ filehandlerrrArArBr6szTiffFileWSIReader.get_file_pathrcCsd|j|jvrqd|j|jvrq|j|jdjrq|j|jdjrq|j|jd}|dur5t|jj}|dus?t|dkrFtdd}t |d}|j|jdj}|j|jdj}||d|d||d|dfSt d ) r XResolution YResolutionResolutionUnitNrzEThe resolution unit is missing. `micrometer` will be used as default.rrpzD`mpp` cannot be obtained for this file. Please use `level` instead.) rtagsrUrr6rrmrrrrF)r>rRr%rrZyresZxresrArArBrr;s"    (zTiffFileWSIReader.get_mppcCstd)rzCurrently, TiffFile does not provide a general API to obtain objective power.Please use `level` (or `mpp`) instead, or try other backends.)rFrQrArArBruYs zTiffFileWSIReader.get_powerrrcKrr)rr<rrrrrmrrArArBrgrzTiffFileWSIReader.readrrr1rr5rc s|j|d|}t|jdkr|d}|j||dfddttD}||d|d|d|d|d|dd d f} t| d |j} |d vrs| j|jd vrit d |d| j|jdt | gd|j} | S)r)r%r).N)rRr%csg|] }t|qSrA)round)r\rnZdownsampling_ratiorrArBr_r`z0TiffFileWSIReader._get_patch..rrpNrr$rrrrr) rastypermrr}rlrErr/rFr) r>rRrrr%r1r5Z wsi_imageZ location_rrArrBr~s : zTiffFileWSIReader._get_patchrrrrrrrrrArAr?rBrs       r)* __future__rrabcrcollections.abcros.pathrtypingrnumpyrErD monai.configrrr monai.data.image_readerr r monai.data.utilsr monai.utilsr rrrrrrmonai.utils.miscrrrr__all__rrrrrrArArArBs<      $ L66