o # iX@snddlmZddlZddlZddlmZddlmZddlZ ddl Z ddl m Z ddl mZGdddZdS) ) annotationsN) OrderedDict)Path)PathLike) ImageMetaKeyc@s\eZdZdZ     d$d%ddZd&ddZd'd(ddZd'd)ddZd*d d!Zd&d"d#Z 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, output_dirrfilenamestr overwriteboolflush delimiterreturnNonecCsrt||_t|_t|tr|dddkstd|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)rr r _cache_dict isinstancer warningswarn _filepathexistsosremoverr _data_index)selfr r rrrrV/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/data/csv_saver.py__init__%s     zCSVSaver.__init__cCs|js |jjdddt|jd-}|jD]\}}|||D] }||j t |q&|dqWdn1sCwY| dS)z2 Writes the cached dict to a csv T)parentsexist_oka N) r rmkdiropenrritemswriteflattenrr reset_cache)rfkvresultrrr finalizeEs     zCSVSaver.finalizeNdatatorch.Tensor | np.ndarray meta_data dict | NonecCsZ|r|tjnt|j}|jd7_t|tjr!| }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)rr1r3Zsave_keyrrr saveUs   z CSVSaver.save batch_datacsLt|D]\}||rfddDndq|jr$|dSdS)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-ir3rr nsz'CSVSaver.save_batch..N) enumeraterArr0)rrBr3r1rrDr 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)rclearrIrrr r+xszCSVSaver.reset_cache)rr TFr ) r rr r rrrrrr rr)rr)N)r1r2r3r4rr)rBr2r3r4rr)rr) __name__ __module__ __qualname____doc__r!r0rArHrJr+rrrr rs    r) __future__rrr collectionsrpathlibrr=r>r9monai.config.type_definitionsr monai.utilsrr7rrrrr s