o  i @sJddlmZddlmZddlZddlmZGdddeZdd dZdS)) annotations)CallableN)Metriccs.eZdZdZddfdd Zdd d ZZS) MMDMetrica Unbiased Maximum Mean Discrepancy (MMD) is a kernel-based method for measuring the similarity between two distributions. It is a non-negative metric where a smaller value indicates a closer match between the two distributions. Gretton, A., et al,, 2012. A kernel two-sample test. The Journal of Machine Learning Research, 13(1), pp.723-773. Args: y_mapping: Callable to transform the y tensors before computing the metric. It is usually a Gaussian or Laplace filter, but it can be any function that takes a tensor as input and returns a tensor as output such as a feature extractor or an Identity function., e.g. `y_mapping = lambda x: x.square()`. N y_mappingCallable | NonereturnNonecst||_dSN)super__init__r)selfr __class__S/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/metrics/mmd.pyr #s  zMMDMetric.__init__y torch.Tensory_predcCst|||jSr ) compute_mmdr)r rrrrr__call__'szMMDMetric.__call__r )rrrr )rrrrrr)__name__ __module__ __qualname____doc__r r __classcell__rrrrrs rrrrrrrcCs|jddks|jddkrtd|dur||}||}|j|jkr0td|jd|jtt|jdddD]}|j|d}|j|d}q;||jdd}||jdd}t||}t||}t||}|jd}|jd}d||d} t |t t |} d||d} t |t t |} d ||} t |}| | | | | |}|S) a- Args: y: first sample (e.g., the reference image). Its shape is (B,C,W,H) for 2D data and (B,C,W,H,D) for 3D. y_pred: second sample (e.g., the reconstructed image). It has similar shape as y. y_mapping: Callable to transform the y tensors before computing the metric. rz9MMD metric requires at least two samples in y and y_pred.Nz[y_pred and y shapes dont match after being processed by their transforms, received y_pred: z and y: )dim) shape ValueErrorrangelensqueezeviewtorchmmtsumdiagdiagonal)rrrdZy_yZ y_pred_y_predZy_pred_ymnc1ac2bc3cmmdrrrr+s>      r)rrrrrrrr) __future__rcollections.abcrr&Zmonai.metrics.metricrrrrrrrs