U
    Ph`                      @  s   d dl mZ d dlmZ d dlZd dlZdddddgZd'd
d
ddddZd
d
ddddZ	dd
d
d
ddddZ
d(ddddddddZddddZddddd Zdddd!d"Zd#ddd$d%d&ZdS ))    )annotations)SequenceNsame_paddingstride_minus_kernel_paddingcalculate_out_shapegaussian_1dpolyval   zSequence[int] | intztuple[int, ...] | int)kernel_sizedilationreturnc                 C  s~   t | }t |}t |d | d dkrDtd| d| d|d d | }tdd |D }t|dkrv|S |d S )	aS  
    Return the padding value needed to ensure a convolution using the given kernel size produces an output of the same
    shape as the input for a stride of 1, otherwise ensure a shape of the input divided by the stride rounded down.

    Raises:
        NotImplementedError: When ``np.any((kernel_size - 1) * dilation % 2 == 1)``.

    r	      z+Same padding not available for kernel_size=z and dilation=.c                 s  s   | ]}t |V  qd S Nint.0p r   T/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/layers/convutils.py	<genexpr>)   s     zsame_padding.<locals>.<genexpr>r   )np
atleast_1danyNotImplementedErrortuplelen)r
   r   kernel_size_npZdilation_np
padding_nppaddingr   r   r   r      s    


)r
   strider   c                 C  sF   t | }t |}|| }tdd |D }t|dkr>|S |d S )Nc                 s  s   | ]}t |V  qd S r   r   r   r   r   r   r   3   s     z.stride_minus_kernel_padding.<locals>.<genexpr>r	   r   )r   r   r   r   )r
   r!   r   	stride_npZout_padding_npZout_paddingr   r   r   r   .   s
    

z Sequence[int] | int | np.ndarray)in_shaper
   r!   r    r   c           
      C  sV   t | }t |}t |}t |}|| | | | d }tdd |D }	|	S )a-  
    Calculate the output tensor shape when applying a convolution to a tensor of shape `inShape` with kernel size
    `kernel_size`, stride value `stride`, and input padding value `padding`. All arguments can be scalars or multiple
    values, return value is a scalar if all inputs are scalars.
    r	   c                 s  s   | ]}t |V  qd S r   r   )r   sr   r   r   r   I   s     z&calculate_out_shape.<locals>.<genexpr>)r   r   r   )
r#   r
   r!   r    Zin_shape_npr   r"   r   Zout_shape_np	out_shaper   r   r   r   8   s    



      @erfFztorch.Tensorfloatstrbool)sigma	truncatedapprox	normalizer   c                 C  s  t j| t jt| t jr| jndd} | j}|dkrBtd| dttt| | dd }|	 dkrt j
| |d t j|d}d	t |  }d||d   ||d     }|jd
d}n|	 dkrt j
| |d t j| jd}t d| |   |d  }|s|d|   }n|	 dkr| |  }	dg|d  }
t|	|
d
< t|	|
d< tdt|
D ]}t||	|
|< qd|
dd
d }||
 t |t |	  }ntd| d|r||  S |S )a  
    one dimensional Gaussian kernel.

    Args:
        sigma: std of the kernel
        truncated: tail length
        approx: discrete Gaussian kernel type, available options are "erf", "sampled", and "scalespace".

            - ``erf`` approximation interpolates the error function;
            - ``sampled`` uses a sampled Gaussian kernel;
            - ``scalespace`` corresponds to
              https://en.wikipedia.org/wiki/Scale_space_implementation#The_discrete_Gaussian_kernel
              based on the modified Bessel functions.

        normalize: whether to normalize the kernel with `kernel.sum()`.

    Raises:
        ValueError: When ``truncated`` is non-positive.

    Returns:
        1D torch tensor

    Ndtypedevice        z truncated must be positive, got r         ?r'   r	   g'e?r   )minZsampledg      r   g@Z
scalespacezUnsupported option: approx='z'.)torch	as_tensorr(   
isinstanceTensorr1   
ValueErrorr   maxlowerarangeabsr'   clampexp_modified_bessel_0_modified_bessel_1ranger   _modified_bessel_iextendstackr   sum)r+   r,   r-   r.   r1   tailxtoutsigma2Zout_poskr   r   r   r   N   s6    $$
)r   c                 C  s   t |tjr|jnd}tj| tj|d} | jdks>t| dk rJt|j	S tj|tj|d}| d }| dd D ]}|| | }qp|S )a  
    Evaluates the polynomial defined by `coef` at `x`.

    For a 1D sequence of coef (length n), evaluate::

        y = coef[n-1] + x * (coef[n-2] + ... + x * (coef[1] + x * coef[0]))

    Args:
        coef: a sequence of floats representing the coefficients of the polynomial
        x: float or a sequence of floats representing the variable of the polynomial

    Returns:
        1D torch tensor
    Nr/   r   r	   )
r8   r6   r9   r1   r7   r(   ndimr   zerosshape)coefrI   r1   anscr   r   r   r      s    )rI   r   c              	   C  s   t j| t jt| t jr| jnd d} t | dk rV| |  d }tdddddd	d
g|S t | }d| }dddddddddg	}t||t | t 	| S )Nr/         @      ,@gtHZr?gIx?g2t?g,?N?g03@g$@      ?g;^p?gUL+ߐgZ?g'gTPÂ?gJNYgՒ+Hub?g-5? e3E?
r6   r7   r(   r8   r9   r1   r>   r   r@   sqrt)rI   yax_coefr   r   r   rA      s"    $
rA   c              	   C  s   t j| t jt| t jr| jnd d} t | dk rd| |  d }dddddd	d
g}t | t|| S t | }d| }dddddddddg	}t||t | t 	| }| dk r| S |S )Nr/   rT   rU   gӰ٩=5?g.h?gZ9?g*O?g(z?gY?r3   g;PJ4qgqJ:N?gP⥝g'8`?g<Q gtZOZ?g?Vmg.krW   r2   rX   )rI   rZ   r\   r[   rR   r   r   r   rB      s&    $
rB   r   )nrI   r   c           
   	   C  s:  | dk rt d|  dtj|tjt|tjr4|jnd d}|dkrH|S |j}dt| }tjd|dtjd|dtjd|d  }}}t	d| t
t
d	|    }t|d
dD ]P}|t|| |  }	|}|	}t|dkr|d }|d }|d }|| kr|}q|t| | }|dk r6| d dkr6| S |S )Nr   z n must be greater than 1, got n=r   r/   r2   g       @)r1   rV   g      D@r   r5   g    _Bg|=r	   )r:   r6   r7   r(   r8   r9   r1   r>   tensorr   r   floorrY   rC   rA   )
r]   rI   r1   ZtoxrR   Zbipbimjbimr   r   r   rD      s*    $. rD   )r	   )r&   r'   F)
__future__r   collections.abcr   numpyr   r6   __all__r   r   r   r   r   rA   rB   rD   r   r   r   r   <module>   s   
     8