o % i` @sddlmZddlmZddlZddlZgdZd-d.d d Zd/ddZ d0ddZ d1d2d d!Z d3d"d#Z d4d%d&Z d4d'd(Zd5d+d,ZdS)6) annotations)SequenceN) same_paddingstride_minus_kernel_paddingcalculate_out_shape gaussian_1dpolyval kernel_sizeSequence[int] | intdilationreturntuple[int, ...] | intcCs~t|}t|}t|d|ddkr"td|d|d|dd|}tdd|D}t|dkr;|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=.cs|]}t|VqdSNint.0pra/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/layers/convutils.py )zsame_padding..r)np atleast_1danyNotImplementedErrortuplelen)r r kernel_size_npZ dilation_np padding_nppaddingrrrrs rstridecCsFt|}t|}||}tdd|D}t|dkr|S|dS)Ncsrrrrrrrr3rz.stride_minus_kernel_padding..r r)rrr r!)r r%r" stride_npZout_padding_npZ out_paddingrrrr.s  rin_shape Sequence[int] | int | np.ndarrayr$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 csrrr)rsrrrrIrz&calculate_out_shape..)rrr ) r'r r%r$Z in_shape_npr"r&r#Z out_shape_np out_shaperrrr8s   r@erfFsigma torch.Tensor truncatedfloatapproxstr normalizeboolc Cstj|tjt|tjr|jndd}|j}|dkr!td|dttt||dd}| dkratj | |dtj|d}d t |}d||d ||d }|j d d }nx| d krtj | |dtj|jd}td |||d}|s|d|}nN| dkr||} dg|d} t| | d <t| | d<tdt| D] } t| | | | <q| 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)minZsampledgrg @Z scalespacezUnsupported option: approx='z'.)torch as_tensorr0 isinstanceTensorr7 ValueErrorrmaxlowerarangeabsr,clampexp_modified_bessel_0_modified_bessel_1ranger!_modified_bessel_iextendstackrsum) r-r/r1r3r7tailxtoutsigma2Zout_poskrrrrNs8$ $      rcCst|tjr |jnd}tj|tj|d}|jdkst|dkr%t|j Stj|tj|d}|d}|ddD]}|||}q8|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 Nr5rr ) r>r<r?r7r=r0ndimr!zerosshape)coefrOr7anscrrrrs rrOcCstj|tjt|tjr|jndd}t|dkr&||d}tgd|St|}d|}gd}t||t|t |S)Nr5@ ,@)gtHZr?gIx?g2t?g,?N?g03@g$ @?) g;^p?gUL+ߐgZ?g'gTPÂ?gJNYgՒ+Hub?g-5?e3E? r<r=r0r>r?r7rDrrFsqrt)rOyax_coefrrrrGs$   rGcCstj|tjt|tjr|jndd}t|dkr-||d}gd}t|t||St|}d|}gd}t||t|t |}|dkrP| S|S)Nr5rZr[)gӰ٩=5?g.h?gZ9?g*O?g(z?gY?r9) g;PJ4qgqJ:N?gP⥝g'8`?g<Q gtZOZ?g?Vmg.kr]r8r^)rOr`rbrarXrrrrHs$   rHnrc Cs6|dkr td|dtj|tjt|tjr|jndd}|dkr$|S|j}dt|}tjd|dtjd|dtjd|d}}}t d|t t d |}t |d d D](}|t|||} |}| }t|d kr}|d }|d }|d }||kr|}q[|t||}|dkr|ddkr| S|S)Nrz n must be greater than 1, got n=rr5r8g@)r7r\gD@rr;g _Bg|=r )r@r<r=r0r>r?r7rDtensorrrfloorr_rIrG) rcrOr7ZtoxrXZbipbimjZbimrrrrJs,$.  rJ)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,F) r-r.r/r0r1r2r3r4r r.)r r.)rOr.r r.)rcrrOr.r r.) __future__rcollections.abcrnumpyrr<__all__rrrrrrGrHrJrrrrs     8