U Ph` @sddlmZddlmZddlZddlZdddddgZd'd d d d d dZd d d dddZ dd d d d dddZ d(ddddddddZ ddddZ ddddd Z dddd!d"Zd#ddd$d%d&ZdS))) annotations)SequenceN same_paddingstride_minus_kernel_paddingcalculate_out_shape gaussian_1dpolyvalzSequence[int] | intztuple[int, ...] | int) kernel_sizedilationreturncCs~t|}t|}t|d|ddkrDtd|d|d|dd|}tdd|D}t|dkrv|S|dS) 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=.css|]}t|VqdSNint.0prT/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/layers/convutils.py )szsame_padding..r)np atleast_1danyNotImplementedErrortuplelen)r r kernel_size_npZ dilation_np padding_nppaddingrrrrs  )r strider cCsFt|}t|}||}tdd|D}t|dkr>|S|dS)Ncss|]}t|VqdSrrrrrrr3sz.stride_minus_kernel_padding..r r)rrrr)r r!r stride_npZout_padding_npZ out_paddingrrrr.s   z Sequence[int] | int | np.ndarray)in_shaper r!r r c CsVt|}t|}t|}t|}|||||d}tdd|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 css|]}t|VqdSrr)rsrrrrIsz&calculate_out_shape..)rrr) r#r r!r Z in_shape_nprr"rZ out_shape_np out_shaperrrr8s    @erfFz torch.Tensorfloatstrbool)sigma truncatedapprox normalizer c Cstj|tjt|tjr|jndd}|j}|dkrBtd|dttt||dd}| dkrtj | |dtj|d}d t |}d||d ||d }|j d d }n| d krtj | |dtj|jd}td |||d}|s|d|}n| dkr||} dg|d} t| | d <t| | d<tdt| D]} t| | | | <qd| dd d}|| t|t| }ntd|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 Ndtypedevicez truncated must be positive, got r?r'r g'e?r)minZsampledgr g @Z scalespacezUnsupported option: approx='z'.)torch as_tensorr( isinstanceTensorr1 ValueErrorrmaxlowerarangeabsr'clampexp_modified_bessel_0_modified_bessel_1ranger_modified_bessel_iextendstackrsum) r+r,r-r.r1tailxtoutsigma2Zout_poskrrrrNs6$ $   )r cCst|tjr|jnd}tj|tj|d}|jdks>t|dkrJt|j Stj|tj|d}|d}|ddD]}|||}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/rr ) r8r6r9r1r7r(ndimrzerosshape)coefrIr1anscrrrrs )rIr c Cstj|tjt|tjr|jndd}t|dkrV||d}tdddddd d g|St|}d|}d d d ddddddg }t||t|t |S)Nr/@ ,@gtHZr?gIx?g2t?g,?N?g03@g$ @?g;^p?gUL+ߐgZ?g'gTPÂ?gJNYgՒ+Hub?g-5?e3E? r6r7r(r8r9r1r>rr@sqrt)rIyax_coefrrrrAs"$   rAc Cstj|tjt|tjr|jndd}t|dkrd||d}dddddd d g}t|t||St|}d|}d d d ddddddg }t||t|t |}|dkr| S|S)Nr/rTrUgӰ٩=5?g.h?gZ9?g*O?g(z?gY?r3g;PJ4qgqJ:N?gP⥝g'8`?g<Q gtZOZ?g?Vmg.krWr2rX)rIrZr\r[rRrrrrBs&$   rBr)nrIr c Cs:|dkrtd|dtj|tjt|tjr4|jndd}|dkrH|S|j}dt|}tjd|dtjd|dtjd|d}}}t d|t t d |}t |d d D]P}|t|||} |}| }t|d kr|d }|d }|d }||kr|}q|t||}|dkr6|ddkr6| S|S)Nr z n must be greater than 1, got n=rr/r2g@)r1rVgD@rr5g _Bg|=r )r:r6r7r(r8r9r1r>tensorrrfloorrYrCrA) r]rIr1ZtoxrRZbipbimjbimrrrrDs*$.  rD)r )r&r'F) __future__rcollections.abcrnumpyrr6__all__rrrrrrArBrDrrrr s   8