o  io!@sddlmZddlmZmZddlmZddlmZddl m Z ddl m Z ddl m Zddl mZmZmZmZed e jed \ZZed e jed \ZZerUdd lmZn ed e jed\ZZGdddZdS)) annotations)CallableSequence) TYPE_CHECKING)decollate_batch)write_metrics_reports) IgniteInfo) ImageMetaKey) ensure_tuple min_versionoptional_importstring_list_all_gatherz ignite.engineEventsignite distributed)Enginerc@sVeZdZdZddddddddfd$ddZd%ddZd&ddZd%d d!Zd%d"d#ZdS)' MetricsSavera ignite handler to save metrics values and details into expected files. Args: save_dir: directory to save the metrics and metric details. metrics: expected final metrics to save into files, can be: None, "*" or list of strings. None - don't save any metrics into files. "*" - save all the existing metrics in `engine.state.metrics` dict into separate files. list of strings - specify the expected metrics to save. default to "*" to save all the metrics into `metrics.csv`. metric_details: expected metric details to save into files, the data comes from `engine.state.metric_details`, which should be provided by different `Metrics`, typically, it's some intermediate values in metric computation. for example: mean dice of every channel of every image in the validation dataset. it must contain at least 2 dims: (batch, classes, ...), if not, will unsqueeze to 2 dims. this arg can be: None, "*" or list of strings. None - don't save any metric_details into files. "*" - save all the existing metric_details in `engine.state.metric_details` dict into separate files. list of strings - specify the metric_details of expected metrics to save. if not None, every metric_details array will save a separate `{metric name}_raw.csv` file. batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch` if saving metric details. the purpose is to get the input filenames from the `meta_data` and store with metric details together. `engine.state` and `batch_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. 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 save_rank: only the handler on specified rank will save to files in multi-gpus validation, default to 0. delimiter: 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". *NcCs|SN)xrr^/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/metrics_saver.pyWszMetricsSaver.r,csvsave_dirstrmetricsstr | Sequence[str] | Nonemetric_detailsbatch_transformr summary_ops save_rankint delimiter output_typereturnNonec Csj||_|dur t|nd|_|durt|nd|_||_|dur$t|nd|_||_||_||_g|_ dSr) rr rrr r!r"delir% _filenames) selfrrrr r!r"r$r%rrr__init__Rs  zMetricsSaver.__init__enginercCs2|tj|j|tj|j|tj|dS)g Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)add_event_handlerr EPOCH_STARTED_startedITERATION_COMPLETED_get_filenamesEPOCH_COMPLETED)r*r,rrrattachgszMetricsSaver.attach_enginecCs g|_dS)zs Initialize internal buffers. Args: _engine: Ignite Engine, unused argument. N)r))r*r5rrrr0ps zMetricsSaver._startedcCsR|jdur%||jj}t|trt|}|D]}|j| t j qdSdSr) rr statebatch isinstancedictrr)appendgetKeyFILENAME_OR_OBJ)r*r, meta_datamrrrr2zs  zMetricsSaver._get_filenamesc s t}j|kr td|dkrtjdnj}tjkri}jdur=t|j jdkr=fdd|j j D}i}t |j drk|j j }j durkt|dkrk| D]\}}|j vsfd j vrj|||<qXt jt|dkrvdn|||jjjd dSdS) r-zs*z)MetricsSaver.__call__..rr)rimagesrrr!r(r%)idistget_world_sizer" ValueErrorr r)get_rankrlenr6itemshasattrrrrr!r(r%) r*r,ws_images_metricsZ_metric_detailsdetailsrCrDrrEr__call__s4   zMetricsSaver.__call__)rrrrrrr rr!rr"r#r$rr%rr&r')r,rr&r')r5rr&r') __name__ __module__ __qualname____doc__r+r4r0r2rSrrrrrs5   rN) __future__rcollections.abcrrtypingr monai.datarZmonai.handlers.utilsr monai.utilsrr r<r r r r OPT_IMPORT_VERSIONr_rH ignite.enginerrrrrrs