U {Ph%@s^ddlmZddlZddlmZmZddlmZmZm Z ddl Z ddl m Z ddl mZddlmZmZddlmZmZmZmZed ejed \ZZerz4ed \ZZdd lmZdd lmZdd lm Z Wne!k rdZYnXnDedejed\ZZedejeddd\ZZedejeddd\Z ZGdddeZ"eddddGddde"Z#dS) ) annotationsN)CallableSequence) TYPE_CHECKINGAnycast)_Loss) IgniteInfo)CumulativeIterationMetric LossMetric)MetricReduction deprecated min_versionoptional_importignite distributed)Engine)Metric)reinit__is_reducedFz ignite.enginerzignite.metricsrbase)as_typezignite.metrics.metricr decoratorc seZdZdZdddddejdfddd d d d d d fdd Zed dddZedd dddZ ddddZ ddd dfdd Z Z S)IgniteMetricHandlera Base Metric class based on ignite event handler mechanism. The input `prediction` or `label` data can be a PyTorch Tensor or numpy array with batch dim and channel dim, or a list of PyTorch Tensor or numpy array without batch dim. Args: metric_fn: callable function or class to compute raw metric results after every iteration. expect to return a Tensor with shape (batch, channel, ...) or tuple (Tensor, not_nans). loss_fn: A torch _Loss function which is used to generate the LossMetric output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. `engine.state` and `output_transform` inherit from the ignite concept: https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean_dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. reduction: Argument for the LossMetric, look there for details get_not_nans: Argument for the LossMetric, look there for details NcCs|SNxrrQ/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/handlers/ignite_metric.pyGzIgniteMetricHandler.TF CumulativeIterationMetric | None _Loss | NonerboolMetricReduction | strNone metric_fnloss_fnoutput_transform save_details reduction get_not_nansreturncsd|_tt||_||_||_g|_d|_d|_|jdkrL|jdkrLt d|jdk rh|jdk rht d|jrt |j||d|_t |dS)NFz.Either metric_fn or loss_fn have to be passed.zmetric handler can only record the first value of result list.rTNzCplease call the attach() function to connect expected engine first.)r& aggregate isinstancetuplelistr;warningswarnr-r)r.r/ RuntimeError get_bufferstatemetric_detailstorchTensorsqueezendimitem)r4resultrrrcomputers     zIgniteMetricHandler.computerstr)enginenamer,cs:tj||d||_||_|jr6t|jds6i|j_dS)aF Attaches current metric to provided engine. On the end of engine's run, `engine.state.metrics` dictionary will contain computed metric's value under provided name. Args: engine: the engine to which the metric must be attached. name: the name of the metric to attach. )rRrSrIN)r1attachr.r/r)hasattrrHrI)r4rRrSr5rrrTs zIgniteMetricHandler.attach) __name__ __module__ __qualname____doc__r MEANr2rr7r>rPrT __classcell__rrr5rr,s rz1.2z1.4z0Use IgniteMetricHandler instead of IgniteMetric.)sinceremoved msg_suffixc sBeZdZdddddejdfdddd d d d d fd d ZZS) IgniteMetricNcCs|SrrrrrrrrzIgniteMetric.TFr r!rr"r#r$r%cstj||||||ddS)N)r&r'r(r)r*r+)r1r2r3r5rrr2s zIgniteMetric.__init__)rVrWrXr rZr2r[rrr5rr_sr_)$ __future__rrDcollections.abcrrtypingrrrrJtorch.nn.modules.lossr monai.configr monai.metricsr r monai.utilsr r rrOPT_IMPORT_VERSIONidist_ has_ignite ignite.enginerignite.metricsrZignite.metrics.metricr ImportErrorrr_rrrr s:        r