o
    iN                      @  s6   d dl mZ d dlZd dlmZmZ G dd dZdS )    )annotationsN)OrderingTransformationsOrderingTypec                      s   e Zd ZdZdddejjejjejjffd6 fddZ	d7ddZ
d8ddZd8ddZd8ddZd8dd Zd8d!d"Zd9d$d%Zd9d&d'Zd9d(d)Zd9d*d+Zed:d;d0d1Zed:d;d2d3Zed:d;d4d5Z  ZS )<Orderinga  
    Ordering class that projects a 2D or 3D image into a 1D sequence. It also allows the image to be transformed with
    one of the following transformations:
    Reflection (see np.flip for more details).
    Transposition (see np.transpose for more details).
    90-degree rotation (see np.rot90 for more details).

    The transformations are applied in the order specified by the transformation_order parameter.

    Args:
        ordering_type: The ordering type. One of the following:
            - 'raster_scan': The image is projected into a 1D sequence by scanning the image from left to right and from
            top to bottom. Also called a row major ordering.
            - 's_curve': The image is projected into a 1D sequence by scanning the image in a circular snake like
            pattern from top left towards right gowing in a spiral towards the center.
            - random': The image is projected into a 1D sequence by randomly shuffling the image.
        spatial_dims: The number of spatial dimensions of the image.
        dimensions: The dimensions of the image.
        reflected_spatial_dims: A tuple of booleans indicating whether to reflect the image along each spatial dimension.
        transpositions_axes: A tuple of tuples indicating the axes to transpose the image along.
        rot90_axes: A tuple of tuples indicating the axes to rotate the image along.
        transformation_order: The order in which to apply the transformations.
    Nordering_typestrspatial_dimsint
dimensions0tuple[int, int, int] | tuple[int, int, int, int]reflected_spatial_dimstuple[bool, bool] | Nonetranspositions_axesEtuple[tuple[int, int], ...] | tuple[tuple[int, int, int], ...] | None
rot90_axes"tuple[tuple[int, int], ...] | Nonetransformation_ordertuple[str, ...]returnNonec           	        s
  t    || _| jttvrtdtt d| j d|| _|| _t|| jd kr<td| jd  dt| d|| _	|| _
|| _tt|t|krWtd| d|D ]}|ttvrntdtt d| dqY|| _|  | _|  | _t| j| _d S )	Nz+ordering_type must be one of the following z
, but got .   zdimensions must be of length z$No duplicates are allowed. Received zValid transformations are z but received )super__init__r   listr   
ValueErrorr   r
   lenr   r   r   setr   r   _create_templatetemplate_create_ordering_sequence_orderingnpargsort_revert_sequence_ordering)	selfr   r   r
   r   r   r   r   transformation	__class__ V/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/utils/ordering.pyr   ,   s2   
 

zOrdering.__init__x
np.ndarrayc                 C  s   || j  }|S Nr!   )r%   r+   r)   r)   r*   __call__Y   s   
zOrdering.__call__c                 C     | j S r-   r.   r%   r)   r)   r*   get_sequence_ordering^      zOrdering.get_sequence_orderingc                 C  r0   r-   )r$   r1   r)   r)   r*   get_revert_sequence_orderinga   r3   z%Ordering.get_revert_sequence_orderingc                 C  s   |   | _| j| jd}|S Nr   )_transform_templater   _order_template)r%   orderr)   r)   r*   r    d   s   
zOrdering._create_orderingc                 C  s(   | j dd  }tt|j| }|S )Nr   )r
   r"   arangeprodreshape)r%   spatial_dimensionsr   r)   r)   r*   r   j   s   zOrdering._create_templatec                 C  sj   | j D ].}|tjjkr| j| jd| _q|tjjkr#| j| jd| _q|tjjkr1| j	| jd| _q| jS r5   )
r   r   	TRANSPOSEvalue_transpose_templater   	ROTATE_90_rot90_templateREFLECT_flip_template)r%   r&   r)   r)   r*   r7   p   s   
zOrdering._transform_templater   c                 C  (   | j d ur| j D ]	}tj||d}q|S N)axes)r   r"   	transposer%   r   rG   r)   r)   r*   r@   {      

zOrdering._transpose_templatec                 C  s8   | j d urt| j D ]\}}|rtj||dn|}q
|S )N)axis)r   	enumerater"   flip)r%   r   rK   Z
to_reflectr)   r)   r*   rD      s   
zOrdering._flip_templatec                 C  rE   rF   )r   r"   rot90rI   r)   r)   r*   rB      rJ   zOrdering._rot90_templatec                   s~   d }| j dkr jd  jd }}n jd  jd  jd }}}td| j d|||}t fdd|D }|S )N   r   r   zself._idxc                   s   g | ]} t | qS r)   )tuple).0er6   r)   r*   
<listcomp>   s    z,Ordering._order_template.<locals>.<listcomp>)r   shapeevalr   r"   array)r%   r   depthsrowscolumnssequenceorderingr)   r6   r*   r8      s   
"zOrdering._order_templaterY   colsrX   
int | Nonec                 C  sd   g }t | D ]$}t |D ]}|d ur"t |D ]
}||||f qq|||f qqt|}|S r-   rangeappendr"   rW   rY   r]   rX   idxrcdidx_npr)   r)   r*   raster_scan_idx   s   
zOrdering.raster_scan_idxc           
      C  s   g }t | D ]B}|d dkrt |nt |d dd}|D ]+}|r@|d dkr*t |nt |d dd}|D ]
}||||f q4q|||f qqt|}	|	S )NrO   r   r   r_   )
rY   r]   rX   rc   rd   col_idxre   Z	depth_idxrf   rg   r)   r)   r*   s_curve_idx   s   $$
	zOrdering.s_curve_idxc                 C  sl   g }t | D ]"}t |D ]}|r t |D ]
}||||f qq|||f qqt|}tj| |S r-   )r`   ra   r"   rW   randomshufflerb   r)   r)   r*   
random_idx   s   
zOrdering.random_idx)r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   )r+   r,   r   r,   )r   r,   )r   r,   r   r,   r-   )rY   r	   r]   r	   rX   r^   r   r,   )__name__
__module____qualname____doc__r   r>   r?   rA   rC   r   r/   r2   r4   r    r   r7   r@   rD   rB   r8   staticmethodrh   rk   rn   __classcell__r)   r)   r'   r*   r      s4    
-








r   )
__future__r   numpyr"   monai.utils.enumsr   r   r   r)   r)   r)   r*   <module>   s   