U |Ph' @s(ddlmZddlZddlmZddlmZmZddlm Z m Z ddl Z ddl Z ddlmZmZmZddlmZmZmZmZedejed \ZZe rdd lmZned ejed \ZZd dddgZddddd ZddddZd+dddddddddd d dZd,d"d#d$d%d&dZ d'dd(d)d*Z!dS)-) annotationsN) OrderedDict)CallableSequence) TYPE_CHECKINGAny) IgniteInfoKeysCollectionPathLike) ensure_tuplelook_up_option min_versionoptional_importignite distributed)Enginez ignite.enginerstopping_fn_from_metricstopping_fn_from_losswrite_metrics_reports from_enginestrzCallable[[Engine], Any]) metric_namereturncsdddfdd }|S)zd Returns a stopping function for ignite.handlers.EarlyStopping using the given metric name. rrenginercs |jjSN)statemetricsrrI/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/handlers/utils.py stopping_fn'sz,stopping_fn_from_metric..stopping_fnr )rr"r rr!r"s)rcCsddddd}|S)z] Returns a stopping function for ignite.handlers.EarlyStopping using the loss value. rrrcSs |jj Sr)routputrr r r!r"2sz*stopping_fn_from_loss..stopping_fnr )r"r r r!r-s,csvr zSequence[str] | Nonez+dict[str, torch.Tensor | np.ndarray] | Nonezstr | Sequence[str] | Nonezlist[str] | NoneNone) save_dirimagesrmetric_details summary_opsdeli output_type class_labelsrc s|dkrtd|dtj|s2t||dk rt|dkrttj|dd4}| D]$\} } | | |t | dqdW5QRX|dk rt|dkr| D].\} } t | t jr| } | jdkr| d } n| jd kr| d } |dkr&d d t| jd D}ndd |D}|dg7}tj| tj| d ddgd d} ttj|| ddt}| d|||dt| D]H\} } | |dk r|| nt | ||dd | DdqW5QRX|dk rttjtjtjtjddtjdddt|} d| kr@t } ddddfdd ttj|| d!dn}| d"||| dtt!| D]<\} | || ||fd#d | DdqW5QRXqdS)$a Utility function to write the metrics into files, contains 3 parts: 1. if `metrics` dict is not None, write overall metrics into file, every line is a metric name and value pair. 2. if `metric_details` dict is not None, write raw metric data of every image into file, every line for 1 image. 3. if `summary_ops` is not None, compute summary based on operations on `metric_details` and write to file. Args: save_dir: directory to save all the metrics reports. images: name or path of every input image corresponding to the metric_details data. if None, will use index number as the filename of every input image. metrics: a dictionary of (metric name, metric value) pairs. metric_details: a dictionary of (metric name, metric raw values) pairs, usually, it comes from metrics computation, for example, the raw value can be the mean_dice of every channel of every input image. summary_ops: expected computation operations to generate the summary report. it can be: None, "*" or list of strings, default to None. None - don't generate summary report for every expected metric_details. "*" - generate summary report for every metric_details with all the supported operations. list of strings - generate summary report for every metric_details with specified operations, they should be within list: ["mean", "median", "max", "min", "percentile", "std", "notnans"]. the number in "percentile" should be [0, 100], like: "15percentile". default: "90percentile". for more details, please check: https://numpy.org/doc/stable/reference/generated/numpy.nanpercentile.html. note that: for the overall summary, it computes `nanmean` of all classes for each image first, then compute summary. example of the generated summary report:: class mean median max 5percentile 95percentile notnans class0 6.0000 6.0000 7.0000 5.1000 6.9000 2.0000 class1 6.0000 6.0000 6.0000 6.0000 6.0000 1.0000 mean 6.2500 6.2500 7.0000 5.5750 6.9250 2.0000 deli: the delimiter character in the saved file, default to "," as the default output type is `csv`. to be consistent with: https://docs.python.org/3/library/csv.html#csv.Dialect.delimiter. output_type: expected output file type, supported types: ["csv"], default to "csv". class_labels: list of class names used to name the classes in the output report, if None, "class0", ..., "classn" are used, default to None. r%zunsupported output type: .Nrz metrics.csvw )r1r1)r1cSsg|]}dt|qS)classr.0ir r r! }sz)write_metrics_reports..cSsg|] }t|qSr r4r5r r r!r8smeanT)axiskeepdims)r:z_raw.csvfilenamecSs*g|]"}t|ttfr|dnt|qSz.4f) isinstanceintfloatr)r6cr r r!r8scSst|d|dS)Nrr1)np nanpercentilexr r r!z'write_metrics_reports..cSst|Sr)rBisnansumrDr r r!rFrG)r9medianmaxmin 90percentilestdZnotnans*rz np.ndarrayr)opdrcs>|dst|}||St|dd}d||fS)N percentilerrM)endswithr r?split)rPrQZc_op threshold) supported_opsr r! _compute_ops   z*write_metrics_reports.._compute_opz _summary.csvr3csg|]}|dqSr=r r6k)rWrAr r!r8s)"lower ValueErrorospathexistsmakedirslenopenjoinitemswriterr>torchTensorcpunumpyndimreshaperangeshaperB concatenatenanmean enumerater nanmediannanmaxnanminnanstdr tuplekeys transpose)r'r(rr)r*r+r,r-frYvr7bopsr )rWrArVr!r8s\.   (        6   Fr boolr)rufirstrcst|fdd}|S)a Utility function to simplify the `batch_transform` or `output_transform` args of ignite components when handling dictionary or list of dictionaries(for example: `engine.state.batch` or `engine.state.output`). Users only need to set the expected keys, then it will return a callable function to extract data from dictionary and construct a tuple respectively. If data is a list of dictionaries after decollating, extract expected keys and construct lists respectively, for example, if data is `[{"A": 1, "B": 2}, {"A": 3, "B": 4}]`, from_engine(["A", "B"]): `([1, 3], [2, 4])`. It can help avoid a complicated `lambda` function and make the arg of metrics more straight-forward. For example, set the first key as the prediction and the second key as label to get the expected data from `engine.state.output` for a metric:: from monai.handlers import MeanDice, from_engine metric = MeanDice( include_background=False, output_transform=from_engine(["pred", "label"]) ) Args: keys: specified keys to extract data from dictionary or decollated list of dictionaries. first: whether only extract specified keys from the first item if input data is a list of dictionaries, it's used to extract the scalar data which doesn't have batch dim and was replicated into every dictionary when decollating, like `loss`, etc. cslttr tfddDSttrhtdtrhfddD}t|dkr`t|S|dSdS)Nc3s|]}|VqdSrr rXdatar r! sz0from_engine.._wrapper..rcs.g|]&rdnfddDqS)rcsg|] }|qSr r r5rYr r!r8sz._wrapper...r )r6)r~r|rr!r8sz1from_engine.._wrapper..r1)r>dictrtlistr`)r~ret_keysr|r}r!_wrappers  zfrom_engine.._wrapper)r )rur|rr rr!rs r)rErcCsdS)z Always return `None` for any input data. A typical usage is to avoid logging the engine output of every iteration during evaluation. Nr rDr r r! ignore_datasr)r$r%N)F)" __future__rr\ collectionsrcollections.abcrrtypingrrrhrBre monai.configrr r monai.utilsr r r rOPT_IMPORT_VERSIONidist_ ignite.enginer__all__rrrrrr r r r! s*     r+