o  iE@sdZddlmZddlmZddlmZddlZddl Z ddl m Z m Z ddl mZddlmZdd lmZdd lmZmZdd lmZdd lmZmZmZdd lmZddlmZddl m!Z!m"Z"gdZ#GdddeZ$GdddeZ%GdddeZ&GdddeZ'dS)zbTransforms using a smooth spatial field generated by interpolating from smaller randomized fields.) annotations)Sequence)AnyN) grid_sample interpolate)NdarrayOrTensor)get_track_meta) meshgrid_ij) RandomizableRandomizableTransform)moveaxis)GridSampleModeGridSamplePadModeInterpolateMode)TransformBackends)look_up_option)convert_to_dst_typeconvert_to_tensor) SmoothFieldRandSmoothFieldAdjustContrastRandSmoothFieldAdjustIntensityRandSmoothDeformc @sdeZdZdZejgZddddddejddf d(ddZ d)d*ddZ d+d d!Z d,d"d#Z d-d.d&d'Z dS)/ra Generate a smooth field array by defining a smaller randomized field and then reinterpolating to the desired size. This exploits interpolation to create a smoothly varying field used for other applications. An initial randomized field is defined with `rand_size` dimensions with `pad` number of values padding it along each dimension using `pad_val` as the value. If `spatial_size` is given this is interpolated to that size, otherwise if None the random array is produced uninterpolated. The output is always a Pytorch tensor allocated on the specified device. Args: rand_size: size of the randomized field to start from pad: number of pixels/voxels along the edges of the field to pad with `pad_val` pad_val: value with which to pad field edges low: low value for randomized field high: high value for randomized field channels: number of channels of final output spatial_size: final output size of the array, None to produce original uninterpolated field mode: interpolation mode for resizing the field align_corners: if True align the corners when upsampling field device: Pytorch device to define field on rg?N rand_size Sequence[int]padintpad_valfloatlowhighchannels spatial_sizeSequence[int] | Nonemodestr align_corners bool | Nonedevicetorch.device | Nonec st|_|_|_|_|_|_| _| _d_ d_ ||kr(t dtfddjD_ t jdjfj jd|_jfj_jdkrWtdntjj } dtdf| ftj_|dS)NzFValue for `low` must be less than `high` otherwise field will be zerosc3s|] }|jdVqdS)N)r).0rsselfe/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/smooth_field/array.py Wsz'SmoothField.__init__..r)r)r)tuplerrr r!r"r%r'r)r# spatial_zoom ValueErrortotal_rand_sizetorchonesfield crand_sizeslicelen rand_slicesset_spatial_size) r/rrrr r!r"r#r%r'r)Z pad_slicer0r.r1__init__;s$ ""zSmoothField.__init__data Any | NonereturnNonecCs(t|j|j|j|j|j|j<dSN) r7 from_numpyRuniformr r!r:r9r=r/r@r0r0r1 randomizebs(zSmoothField.randomizecCsD|dur d|_d|_dSt||_tddt|j|jD|_dS)a  Set the `spatial_size` and `spatial_zoom` attributes used for interpolating the field to the given dimension, or not interpolate at all if None. Args: spatial_size: new size to interpolate to, or None to not interpolate Ncss|] \}}||VqdSrDr0)r,sfr0r0r1r2rsz/SmoothField.set_spatial_size..)r#r4r3zipr6)r/r#r0r0r1r>es   "zSmoothField.set_spatial_sizecC ||_dSrD)r%r/r%r0r0r1set_modet zSmoothField.set_modeF torch.Tensorc Cs|r||j}|jdurGt||jt|jt|jdd}| }| }|j }|j }| d| ||}| |||}|S)NF)input scale_factorr%r'recompute_scale_factorr)rIr9cloner4rrr%rr'minmaxsqueezediv_mul_add_) r/rIr9Z resized_fieldminamaxaminvmaxvZ norm_fieldr0r0r1__call__ws$     zSmoothField.__call__)rrrrrrr rr!rr"rr#r$r%r&r'r(r)r*rDr@rArBrC)r#r$rBrCr%r&rBrC)F)rBrQ)__name__ __module__ __qualname____doc__rTORCHbackendrAREAr?rIr>rOr`r0r0r0r1r#s" '  rcreZdZdZejgZdejddddfd,fdd Z d-d.fdd Z d/d0fd!d" Z d1d#d$Z d2d3d*d+Z ZS)4ra Randomly adjust the contrast of input images by calculating a randomized smooth field for each invocation. This uses SmoothField internally to define the adjustment over the image. If `pad` is greater than 0 the edges of the input volume of that width will be mostly unchanged. Contrast is changed by raising input values by the power of the smooth field so the range of values given by `gamma` should be chosen with this in mind. For example, a minimum value of 0 in `gamma` will produce white areas so this should be avoided. After the contrast is adjusted the values of the result are rescaled to the range of the original input. Args: spatial_size: size of input array's spatial dimensions rand_size: size of the randomized field to start from pad: number of pixels/voxels along the edges of the field to pad with 1 mode: interpolation mode to use when upsampling align_corners: if True align the corners when upsampling field prob: probability transform is applied gamma: (min, max) range for exponential field device: Pytorch device to define field on rN皙?)?g@r#rrrrr%r&r'r(probrgammaSequence[float] | floatr)r*c zt|t|ttfrd|f|_nt|dkrtdt|t |f|_t ||d|jd|jdd||||d |_ dSNrlr+z7Argument `gamma` should be a number or pair of numbers.rr) rrrr r!r"r#r%r'r) superr? isinstancerrrnr<r5rVrWrsfield r/r#rrr%r'rmrnr) __class__r0r1r?$    z&RandSmoothFieldAdjustContrast.__init__seed int | Nonestatenp.random.RandomState | NonerBc t|||j|||SrDrsset_random_staterur/rzr|rwr0r1rz.RandSmoothFieldAdjustContrast.set_random_stater@rArCc$td|jr|jdSdSrDrsrI _do_transformrurHrwr0r1rI z'RandSmoothFieldAdjustContrast.randomizecC|j|dSrDrurOrNr0r0r1rOz&RandSmoothFieldAdjustContrast.set_modeTimgrrIboolc Cszt|td}|r ||js|S|}|}||}|}t||^}}|||d}||}|||} | S){ Apply the transform to `img`, if `randomize` randomizing the smooth field otherwise reusing the previous.  track_metag|=)rrrIrrVrWrur) r/rrIimg_minimg_maxZimg_rngr9rfield_outr0r0r1r`s z&RandSmoothFieldAdjustContrast.__call__r#rrrrrr%r&r'r(rmrrnror)r*NN)rzr{r|r}rBrrDrarbTrrrIrrBrrcrdrerfrrgrhrrir?rrIrOr` __classcell__r0r0rwr1r# rcrj)4ra+ Randomly adjust the intensity of input images by calculating a randomized smooth field for each invocation. This uses SmoothField internally to define the adjustment over the image. If `pad` is greater than 0 the edges of the input volume of that width will be mostly unchanged. Intensity is changed by multiplying the inputs by the smooth field, so the values of `gamma` should be chosen with this in mind. The default values of `(0.1, 1.0)` are sensible in that values will not be zeroed out by the field nor multiplied greater than the original value range. Args: spatial_size: size of input array rand_size: size of the randomized field to start from pad: number of pixels/voxels along the edges of the field to pad with 1 mode: interpolation mode to use when upsampling align_corners: if True align the corners when upsampling field prob: probability transform is applied gamma: (min, max) range of intensity multipliers device: Pytorch device to define field on rNrk)rkrr#rrrrr%r&r'r(rmrrnror)r*c rprqrrrvrwr0r1r?ryz'RandSmoothFieldAdjustIntensity.__init__rzr{r|r}rBcr~rDrrrwr0r1r0rz/RandSmoothFieldAdjustIntensity.set_random_stater@rArCcrrDrrHrwr0r1rI7rz(RandSmoothFieldAdjustIntensity.randomizecCrrDrrNr0r0r1rO=rz'RandSmoothFieldAdjustIntensity.set_modeTrrrIrcCsFt|td}|r ||js|S|}t||^}}||}|S)rr)rrrIrrur)r/rrIr9rrrr0r0r1r`@sz'RandSmoothFieldAdjustIntensity.__call__rr)rzr{r|r}rBrrDrarbrrrr0r0rwr1rrrc seZdZdZejgZdejddde j e j e jddf d4fdd Zd5d6fd!d" Zd7d8fd&d' Zd9d)d*Zd9d+d,Z d:d;d2d3ZZS)sz-RandSmoothDeform.__init__..)rsr? grid_dtyperrr)rrrtrrr<r5rVrWrrur3r9shaper r7stack unsqueezetogrid)r/r#rrrr'rmrrrrrr)Z grid_spaceZ grid_rangesrrwr0r1r?os6  " "zRandSmoothDeform.__init__rzr{r|r}rBr cr~rDrrrwr0r1rsz!RandSmoothDeform.set_random_stater@rArCcrrDrrHrwr0r1rIrzRandSmoothDeform.randomizer%cCrrDrrNr0r0r1set_field_moderzRandSmoothDeform.set_field_modecCrMrD)rrNr0r0r1 set_grid_moderPzRandSmoothDeform.set_grid_modeTrrrIrc Cst|td}|r ||js|S|dur|n|j}|}|j||j}t |dd}|dt t |j ddddf}t|dt j|}t||t|jt|jt|jtd}t|d|^}} |S)Nrrr.)rRrr%r' padding_moder)rrrIrr)rurrrr listrangerr7float32rrrr rrrrrX) r/rrIr)r9Zdgridimg_trout_trr0r0r1r`s( "  zRandSmoothDeform.__call__)r#rrrrrrr&r'r(rmrrrorr&rr&rr(r)r*r)rzr{r|r}rBr rDrarb)TN)rrrIrr)r*rBr)rcrdrerfrrgrhrrir7rr NEARESTrBORDERr?rrIrrr`rr0r0rwr1rVs(3  r)(rf __future__rcollections.abcrtypingrnumpynpr7torch.nn.functionalrrmonai.config.type_definitionsrmonai.data.meta_objrmonai.networks.utilsr monai.transforms.transformr r 0monai.transforms.utils_pytorch_numpy_unificationr monai.utilsr rrmonai.utils.enumsrmonai.utils.modulermonai.utils.type_conversionrr__all__rrrrr0r0r0r1s*         oe_