o
    /iGT                     @  s   d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlZd dlZd dlmZmZmZmZmZmZ erKd dlZned	\ZZg d
ZG dd deZG dd deZed	dG dd deZdddZdS )    )annotationsN)ABCabstractmethod)Sequence)nullcontext)TemporaryDirectory)TYPE_CHECKINGAny)deprecated_argensure_tuple_sizeget_package_versionoptional_importrequire_pkgversion_geqzarr)Merger	AvgMergerZarrAvgMergerc                   @  s<   e Zd ZdZ		ddddZedddZedddZdS )r   ao  
    A base class for merging patches.
    Extend this class to support operations for `PatchInference`.
    There are two methods that must be implemented in the concrete classes:

        - aggregate: aggregate the values at their corresponding locations
        - finalize: perform any final process and return the merged output

    Args:
        merged_shape: the shape of the tensor required to merge the patches.
        cropped_shape: the shape of the final merged output tensor.
            If not provided, it will be the same as `merged_shape`.
        device: the device where Merger tensors should reside.
    Nmerged_shapeSequence[int]cropped_shapeSequence[int] | Nonedevicetorch.device | str | NonereturnNonec                 C  sB   |d u rt dt|| _t|d u r| jn|| _|| _d| _d S )Nz(Argument `merged_shape` must be providedF)
ValueErrortupler   r   r   is_finalized)selfr   r   r    r    W/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/inferers/merger.py__init__9   s   

zMerger.__init__valuestorch.Tensorlocationc                 C     t d| jj d)a  
        Aggregate values for merging.
        This method is being called in a loop and should add values to their corresponding location in the merged output results.

        Args:
            values: a tensor of shape BCHW[D], representing the values of inference output.
            location: a tuple/list giving the top left location of the patch in the output.

        Raises:
            NotImplementedError: When the subclass does not override this method.

        	Subclass  must implement this method.NotImplementedError	__class____name__)r   r#   r%   r    r    r!   	aggregateG   s   zMerger.aggregater	   c                 C  r&   )a  
        Perform final operations for merging patches and return the final merged output.

        Returns:
            The results of merged patches, which is commonly a torch.Tensor representing the merged result, or
                a string representing the filepath to the merged results on disk.

        Raises:
            NotImplementedError: When the subclass does not override this method.

        r'   r(   r)   r   r    r    r!   finalizeW   s   zMerger.finalize)NN)r   r   r   r   r   r   r   r   r#   r$   r%   r   r   r   )r   r	   )r,   
__module____qualname____doc__r"   r   r-   r/   r    r    r    r!   r   )   s    r   c                      sb   e Zd ZdZdejejdfd fddZdddZd ddZ	d ddZ
d ddZd ddZ  ZS )!r   a  Merge patches by taking average of the overlapping area

    Args:
        merged_shape: the shape of the tensor required to merge the patches.
        cropped_shape: the shape of the final merged output tensor.
            If not provided, it will be the same as `merged_shape`.
        device: the device for aggregator tensors and final results.
        value_dtype: the dtype for value aggregating tensor and the final result.
        count_dtype: the dtype for sample counting tensor.
    Ncpur   r   r   r   value_dtypetorch.dtypecount_dtyper   torch.device | strr   r   c                   sj   t  j|||d | jstd| j d|| _|| _tj| j| j| jd| _	tj| j| j| jd| _
d S )N)r   r   r   z1`merged_shape` must be provided for `AvgMerger`. 	 is give.)dtyper   )superr"   r   r   r5   r7   torchzerosr   r#   counts)r   r   r   r5   r7   r   r+   r    r!   r"   s   s   zAvgMerger.__init__r#   r$   r%   c                 C  sr   | j rtd|jdd }tdd t||D }t||jtddd}| j|  |7  < | j	|  d7  < dS )	ab  
        Aggregate values for merging.

        Args:
            values: a tensor of shape BCHW[D], representing the values of inference output.
            location: a tuple/list giving the top left location of the patch in the original image.

        Raises:
            NotImplementedError: When the subclass does not override this method.

        zO`AvgMerger` is already finalized. Please instantiate a new object to aggregate.   Nc                 s  "    | ]\}}t ||| V  qd S Nslice.0locsizer    r    r!   	<genexpr>        z&AvgMerger.aggregate.<locals>.<genexpr>Tpad_valpad_from_start   )
r   r   shaper   zipr   ndimrD   r#   r>   r   r#   r%   
patch_sizeZ	map_slicer    r    r!   r-      s   zAvgMerger.aggregatec                 C  s<   | j s| j| j | jtdd | jD  | _d| _ | jS )a  
        Finalize merging by dividing values by counts and return the merged tensor.

        Notes:
            To avoid creating a new tensor for the final results (to save memory space),
            after this method is called, `get_values()` method will return the "final" averaged values,
            and not the accumulating values. Also calling `finalize()` multiple times does not have any effect.

        Returns:
            torch.tensor: a tensor of merged patches
        c                 s  s    | ]}t d |V  qdS )r   NrC   )rF   endr    r    r!   rI      s    z%AvgMerger.finalize.<locals>.<genexpr>T)r   r#   div_r>   r   r   r.   r    r    r!   r/      s
   zAvgMerger.finalizec                 C  s   |   S )zi
        Get the final merged output.

        Returns:
            torch.Tensor: merged output.
        )r/   r.   r    r    r!   
get_output   s   zAvgMerger.get_outputc                 C     | j S )a  
        Get the accumulated values during aggregation or final averaged values after it is finalized.

        Returns:
            torch.tensor: aggregated values.

        Notes:
            - If called before calling `finalize()`, this method returns the accumulating values.
            - If called after calling `finalize()`, this method returns the final merged [and averaged] values.
        r#   r.   r    r    r!   
get_values   s   zAvgMerger.get_valuesc                 C  rW   )z
        Get the aggregator tensor for number of samples.

        Returns:
            torch.Tensor: number of accumulated samples at each location.
        r>   r.   r    r    r!   
get_counts      zAvgMerger.get_counts)r   r   r   r   r5   r6   r7   r6   r   r8   r   r   r0   )r   r$   )r,   r1   r2   r3   r<   float32uint8r"   r-   r/   rV   rY   r[   __classcell__r    r    r?   r!   r   g   s    



	r   )pkg_namec                	      s   e Zd ZdZeddddddedddd	d
dedddddd															d8d9 fd(d)Zd:d-d.Zd;d0d1Zd;d2d3Zd;d4d5Z	d;d6d7Z
  ZS )<r   ar  Merge patches by taking average of the overlapping area and store the results in zarr array.

    Zarr is a format for the storage of chunked, compressed, N-dimensional arrays.
    Zarr data can be stored in any storage system that can be represented as a key-value store,
    like POSIX file systems, cloud object storage, zip files, and relational and document databases.
    See https://zarr.readthedocs.io/en/stable/ for more details.
    It is particularly useful for storing N-dimensional arrays too large to fit into memory.
    One specific use case of this class is to merge patches extracted from whole slide images (WSI),
    where the merged results do not fit into memory and need to be stored on a file system.

    Args:
        merged_shape: the shape of the tensor required to merge the patches.
        cropped_shape: the shape of the final merged output tensor.
            If not provided, it will be the same as `merged_shape`.
        dtype: the dtype for the final merged result. Default is `float32`.
        value_dtype: the dtype for value aggregating tensor and the final result. Default is `float32`.
        count_dtype: the dtype for sample counting tensor. Default is `uint8`.
        store: the zarr store to save the final results. Default is "merged.zarr".
        value_store: the zarr store to save the value aggregating tensor. Default is a temporary store.
        count_store: the zarr store to save the sample counting tensor. Default is a temporary store.
        compressor: the compressor for final merged zarr array. Default is None.
            Deprecated since 1.5.0 and will be removed in 1.7.0. Use codecs instead.
        value_compressor: the compressor for value aggregating zarr array. Default is None.
            Deprecated since 1.5.0 and will be removed in 1.7.0. Use value_codecs instead.
        count_compressor: the compressor for sample counting zarr array. Default is None.
            Deprecated since 1.5.0 and will be removed in 1.7.0. Use count_codecs instead.
        codecs: the codecs for final merged zarr array. Default is None.
            For zarr v3, this is a list of codec configurations. See zarr documentation for details.
        value_codecs: the codecs for value aggregating zarr array. Default is None.
            For zarr v3, this is a list of codec configurations. See zarr documentation for details.
        count_codecs: the codecs for sample counting zarr array. Default is None.
            For zarr v3, this is a list of codec configurations. See zarr documentation for details.
        chunks : int or tuple of ints that defines the chunk shape, or boolean. Default is True.
            If True, chunk shape will be guessed from `shape` and `dtype`.
            If False, it will be set to `shape`, i.e., single chunk for the whole array.
            If an int, the chunk size in each dimension will be given by the value of `chunks`.
    
compressorz1.5.0z1.7.0codecszPlease use 'codecs' instead.)namesinceremovednew_name
msg_suffixvalue_compressorvalue_codecsz"Please use 'value_codecs' instead.count_compressorcount_codecsz"Please use 'count_codecs' instead.Nr]   r^   merged.zarrTr   r   r   r   r:   np.dtype | strr5   r7   storezarr.storage.Store | strvalue_storezarr.storage.Store | str | Nonecount_store
str | Nonelist | NonechunksSequence[int] | boolthread_lockingboolr   r   c                   sL  t  j||d | jstd| j d|| _|| _|| _|| _|  tt	dd}|rV|d u r=t
 | _tj| jj| _n|| _|d u rRt
 | _tj| jj| _n|| _nd | _|d u rbtj n|| _|d u rntj n|| _|| _tt	dd}d | _d | _d | _|r|d ur|| _n|	d urt|	ttfr|	| _n|	g| _nd | _|d ur|| _n|
d urt|
ttfr|
| _n|
g| _nd | _|d ur|| _n^|d urt|ttfr|| _nO|g| _nJd | _nF|d urt|ttfr|d n|| _n|	| _|d urt|ttfr|d n|| _n|
| _|d ur't|ttfr#|d n|| _n|| _|ratj| j| j| j| j| jdd| _tj| j| j| j| j| jdd| _tj| j| j| j| j| jdd| _n3tj| j| j| j| j| jdd	| _tj| j| j| j| j| jdd	| _tj| j| j| j| j| jdd	| _|  |rt  | _!d S t" | _!d S )
N)r   r   z5`merged_shape` must be provided for `ZarrAvgMerger`. r9   r   z3.0.0r   T)rO   ru   r:   rb   rn   	overwrite)rO   ru   r:   ra   rn   ry   )#r;   r"   r   r   output_dtyper5   r7   rn   r   r   r   tmpdirr   storageZ
LocalStorerc   rp   rr   Z	TempStoreru   rb   ri   rk   
isinstancelistr   emptyoutputr=   r#   r>   	threadingLocklockr   )r   r   r   r:   r5   r7   rn   rp   rr   ra   rh   rj   rb   ri   rk   ru   rw   Z
is_zarr_v3r?   r    r!   r"      s   $



 
 
		zZarrAvgMerger.__init__r#   r$   r%   c                 C  s   | j rtd|jdd }tdd t||D }t||jtddd}| j | j	|  |
 7  < | j|  d7  < W d   dS 1 sHw   Y  dS )	z
        Aggregate values for merging.

        Args:
            values: a tensor of shape BCHW[D], representing the values of inference output.
            location: a tuple/list giving the top left location of the patch in the original image.
        zS`ZarrAvgMerger` is already finalized. Please instantiate a new object to aggregate.r@   Nc                 s  rA   rB   rC   rE   r    r    r!   rI     rJ   z*ZarrAvgMerger.aggregate.<locals>.<genexpr>TrK   rN   )r   r   rO   r   rP   r   rQ   rD   r   r#   numpyr>   rR   r    r    r!   r-     s   "zZarrAvgMerger.aggregate
zarr.Arrayc                 C  sR   | j s&t| jj| jjD ]}| j| | j|  | j|< q| j| j d| _ | jS )a  
        Finalize merging by dividing values by counts and return the merged tensor.

        Notes:
            To avoid creating a new tensor for the final results (to save memory space),
            after this method is called, `get_values()` method will return the "final" averaged values,
            and not the accumulating values. Also calling `finalize()` multiple times does not have any effect.

        Returns:
            zarr.Array: a zarr array of of merged patches
        T)	r   iterate_over_chunksr#   ru   cdata_shaper>   r   resizer   )r   chunkr    r    r!   r/     s   zZarrAvgMerger.finalizec                 C  rW   )zy
        Get the final merged output.

        Returns:
            zarr.Array: Merged (averaged) output tensor.
        )r   r.   r    r    r!   rV     r\   zZarrAvgMerger.get_outputc                 C  rW   )z}
        Get the accumulated values during aggregation

        Returns:
            zarr.Array: aggregated values.

        rX   r.   r    r    r!   rY     s   zZarrAvgMerger.get_valuesc                 C  rW   )z
        Get the aggregator tensor for number of samples.

        Returns:
            zarr.Array: Number of accumulated samples at each location.
        rZ   r.   r    r    r!   r[     r\   zZarrAvgMerger.get_counts)Nr]   r]   r^   rl   NNNNNNNNTT)"r   r   r   r   r:   rm   r5   rm   r7   rm   rn   ro   rp   rq   rr   rq   ra   rs   rh   rs   rj   rs   rb   rt   ri   rt   rk   rt   ru   rv   rw   rx   r   r   r0   )r   r   )r,   r1   r2   r3   r
   r"   r-   r/   rV   rY   r[   r_   r    r    r?   r!   r      sR    &

 
(


	
r   r    c              
   c  s    t | t |krtdt | dkr1t|d D ]}|t|| d  |d | d  f V  qdS t|d D ]#}t| dd |dd |t|| d  |d | d  f E dH  q7dS )aQ  
    Iterate over chunks of a given shape.

    Args:
        chunks: the chunk shape
        cdata_shape: the shape of the data in chunks
        slice_tuple: the slice tuple to be used for indexing

    Raises:
        ValueError: When the length of chunks and cdata_shape are not the same.

    Yields:
        slices of the data
    z0chunks and cdata_shape must have the same lengthrN   r   N)lenr   rangerD   r   )ru   r   Zslice_tupleir    r    r!   r     s   (6r   )r    ) 
__future__r   r   abcr   r   collections.abcr   
contextlibr   tempfiler   typingr   r	   r   npr<   monai.utilsr
   r   r   r   r   r   r   ___all__r   r   r   r   r    r    r    r!   <module>   s*    	
>g  '