o  i'@sddlmZddlZddlmZddlmZmZddlm Z m Z ddl Z ddl Z ddlmZmZddlmZmZmZmZmZedejed \ZZe rQdd lmZn ed ejed \ZZgd Zd4ddZd5ddZ   d6d7d&d'Zd8d9d.d/Z d:d2d3Z!dS);) annotationsN) OrderedDict)CallableSequence) TYPE_CHECKINGAny)KeysCollectionPathLike) IgniteInfo ensure_tuplelook_up_option min_versionoptional_importignite distributed)Enginez ignite.enginer)stopping_fn_from_metricstopping_fn_from_losswrite_metrics_reports from_engine metric_namestrreturnCallable[[Engine], Any]csdfdd }|S) zd Returns a stopping function for ignite.handlers.EarlyStopping using the given metric name. enginerrrcs |jjSN)statemetricsrrV/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/utils.py stopping_fn's z,stopping_fn_from_metric..stopping_fnNrrrrr )rr"r rr!r"srcCsddd}|S) z] Returns a stopping function for ignite.handlers.EarlyStopping using the loss value. rrrrcSs |jj Sr)routputrr r r!r"2s z*stopping_fn_from_loss..stopping_fnNr#r )r"r r r!r-s r,csvsave_dirr imagesSequence[str] | Noner+dict[str, torch.Tensor | np.ndarray] | Nonemetric_details summary_opsstr | Sequence[str] | Nonedeli output_type class_labelslist[str] | NoneNonec s$|dkrtd|dtj|st||durTt|dkrTttj|dd}| D]\} } | | |t | dq2Wdn1sOwY|durt|dkr| D]*\} } t | t jru| } | jdkr| d } n | jd kr| d } |durd d t| jd D}ndd |D}|dg7}tj| tj| d ddgd d} ttj|| dd=}| d|||dt| D]"\} } | |dur|| nt | ||dd | DdqWdn 1swY|durttjtjtjtjddtjdddt|} d| vr2t } d&fd!d" ttj|| d#d<}| d$||| dtt!| D]\} | || ||fd%d | Ddq\Wdn 1swYqddSdSdS)'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 )r6r6)r6cSsg|]}dt|qS)classr.0ir r r! }sz)write_metrics_reports..cSsg|]}t|qSr r9r:r r r!r=meanT)axiskeepdims)r@z_raw.csvfilenamecSs*g|]}t|ttfr|dnt|qSz.4f) isinstanceintfloatr)r;cr r r!r=s*cSst|d|dS)Nrr6)np nanpercentilexr r r!r>z'write_metrics_reports..cSst|Sr)rHisnansumrJr r r!rLs)r?medianmaxmin 90percentilestdZnotnans*oprd np.ndarrayrrcs>|dst|}||St|dd}d||fS)N percentilerrR)endswithr rEsplit)rUrVZc_op threshold) supported_opsr r! _compute_ops  z*write_metrics_reports.._compute_opz _summary.csvr8csg|] }|dqSrCr r;k)r]rGr r!r=s)rUrrVrWrr)"lower ValueErrorospathexistsmakedirslenopenjoinitemswriterrDtorchTensorcpunumpyndimreshaperangeshaperH concatenatenanmean enumerater nanmediannanmaxnanminnanstdr tuplekeys transpose)r'r(rr+r,r.r/r0fr_vr<bopsr )r]rGr\r!r8sr .            6rFr{rfirstboolrcst|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. cspttrtfddDSttr4tdtr6fddD}t|dkr0t|S|dSdSdS)Nc3s|]}|VqdSrr r^datar r! sz0from_engine.._wrapper..rcs.g|]r dnfddDqS)rcsg|]}|qSr r r:r_r r!r=r>z._wrapper...r )r;)rrrr!r=s.z1from_engine.._wrapper..r6)rDdictrzlistrf)rret_keysrrr!_wrappers zfrom_engine.._wrapper)r )r{rrr rr!rs rrKrcCsdS)z Always return `None` for any input data. A typical usage is to avoid logging the engine output of every iteration during evaluation. Nr rJr r r! ignore_datasr)rrrr)rr)r%r&N)r'r r(r)rr*r+r*r,r-r.rr/rr0r1rr2)F)r{rrrrr)rKrrr2)" __future__rrb collectionsrcollections.abcrrtypingrrrnrHrk monai.configrr monai.utilsr r r r rOPT_IMPORT_VERSIONidist_ ignite.enginer__all__rrrrrr r r r!s,     r+