U PhX@snddlmZddlZddlZddlmZddlmZddlZ ddl Z ddl m Z ddl mZGdddZdS) ) annotationsN) OrderedDict)Path)PathLike) ImageMetaKeyc@s|eZdZdZdddd d dd d d d Zd dddZd ddd dddZd!ddd dddZddddZd dddZ dS)"CSVSavera  Save the data in a dictionary format cache, and write to a CSV file finally. Typically, the data can be classification predictions, call `save` for single data or call `save_batch` to save a batch of data together, and call `finalize` to write the cached data into CSV file. If no metadata provided, use index from 0 to save data. Note that this saver can't support multi-processing because it reads / writes single CSV file and can't guarantee the data order in multi-processing situation. ./predictions.csvTF,rstrboolNone) output_dirfilename overwriteflush delimiterreturncCsrt||_t|_t|tr,|dddks6td|j||_|j r\|r\t |j||_ ||_ d|_dS)a Args: output_dir: output CSV file directory. filename: name of the saved CSV file name. overwrite: whether to overwriting existing CSV file content, if True, will clear the file before saving. otherwise, will append new content to the CSV file. flush: whether to write the cache data to CSV file immediately when `save_batch` and clear the cache. default to False. 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. Nz.csvz.CSV filename is not a string ends with '.csv'.r)rrr _cache_dict isinstancer warningswarn _filepathexistsosremoverr _data_index)selfrrrrrrI/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/csv_saver.py__init__%s    zCSVSaver.__init__)rc Cs|js|jjdddt|jdP}|jD]>\}}|||D]}||j t |qL|dq2W5QRX| dS)z2 Writes the cached dict to a csv T)parentsexist_oka N) rrmkdiropenrritemswriteflattenrr reset_cache)rfkvresultrrr finalizeEs   zCSVSaver.finalizeNztorch.Tensor | np.ndarrayz dict | None)data meta_datarcCsZ|r|tjnt|j}|jd7_t|tjrB| }t j |t d|j |<dS)aSave data into the cache dictionary. The metadata should have the following key: - ``'filename_or_obj'`` -- save the data corresponding to file name or object. If meta_data is None, use the default index from 0 to save data instead. Args: data: target data content that save into cache. meta_data: the metadata information corresponding to the data. )dtypeN)KeyFILENAME_OR_OBJr rrtorchTensordetachcpunumpynpasarrayfloatr)rr1r2Zsave_keyrrr saveUs  z CSVSaver.save) batch_datar2rcsHt|D],\}||r.fddDndq|jrD|dS)zSave a batch of data into the cache dictionary. Args: batch_data: target batch data content that save into cache. meta_data: every key-value in the meta_data is corresponding to 1 batch of data. csi|]}||qSrr).0r-ir2rr nsz'CSVSaver.save_batch..N) enumerater?rr0)rr@r2r1rrBr save_batches&zCSVSaver.save_batchrcCs|jS)zMGet the cache dictionary, key is filename and value is the corresponding data)rrrrr get_cachesszCSVSaver.get_cachecCs|jdS)z"Clear the cache dictionary contentN)rclearrGrrr r+xszCSVSaver.reset_cache)rr TFr )N)N) __name__ __module__ __qualname____doc__r!r0r?rFrHr+rrrr rs  r) __future__rrr collectionsrpathlibrr;r<r7monai.config.type_definitionsr monai.utilsrr5rrrrr  s