o  i#@s0ddlmZddlZddlmZmZddlmZmZm Z ddl Z ddl m Z ddl mZmZddlmZmZmZmZedejed \ZZerlzed\ZZdd lmZdd lmZdd lmZWn-eykd ZYn#wedejed\ZZedejeddd\ZZedejeddd\ZZGdddeZ dS)) annotationsN)CallableSequence) TYPE_CHECKINGAnycast)_Loss)CumulativeIterationMetric LossMetric) IgniteInfoMetricReduction min_versionoptional_importignite distributed)Engine)Metric)reinit__is_reducedFz ignite.enginerzignite.metricsrbase)as_typezignite.metrics.metricr decoratorcsjeZdZdZdddddejdfd%fdd Zed&ddZed'ddZ d(ddZ d)fd#d$ 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|SN)xrr^/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/ignite_metric.pyFszIgniteMetricHandler.TF metric_fn CumulativeIterationMetric | Noneloss_fn _Loss | Noneoutput_transformr save_detailsbool reductionMetricReduction | str get_not_nansreturnNonecsd|_tt||_||_||_g|_d|_d|_|jdur&|jdur&t d|jdur4|jdur4t d|jr@t |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 isinstancetuplelistr7warningswarnr)r"r*r+ RuntimeError get_bufferstatemetric_detailstorchTensorsqueezendimitem)r/resultrrrcomputeqs     zIgniteMetricHandler.computeenginernamestrcsBtj||d||_||_|jrt|jdsi|j_dSdSdS)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. )rMrNrEN)r-attachr*r+r"hasattrrDrE)r/rMrNr0rrrPs   zIgniteMetricHandler.attach)rrrr r!rr"r#r$r%r&r#r'r()r'r()r3r4r'r()r'r)rMrrNrOr'r() __name__ __module__ __qualname____doc__r MEANr.rr2r:rLrP __classcell__rrr0rr+s   r)! __future__rr@collections.abcrrtypingrrrrFtorch.nn.modules.lossr monai.metricsr r monai.utilsr r r rOPT_IMPORT_VERSIONidist_ has_ignite ignite.enginerignite.metricsrZignite.metrics.metricr ImportErrorrrrrrs0