/usr/local/lib/python3.6/site-packages/datasets/__pycache__
NameSizeModeActions
arrow_dataset.cpython-36.pyc1898500644editdlrm
arrow_reader.cpython-36.pyc224830644editdlrm
arrow_writer.cpython-36.pyc221100644editdlrm
builder.cpython-36.pyc527790644editdlrm
combine.cpython-36.pyc55080644editdlrm
config.cpython-36.pyc51810644editdlrm
dataset_dict.cpython-36.pyc833190644editdlrm
data_files.cpython-36.pyc301530644editdlrm
fingerprint.cpython-36.pyc189790644editdlrm
info.cpython-36.pyc173590644editdlrm
inspect.cpython-36.pyc185830644editdlrm
iterable_dataset.cpython-36.pyc629320644editdlrm
keyhash.cpython-36.pyc33410644editdlrm
load.cpython-36.pyc629220644editdlrm
metric.cpython-36.pyc232170644editdlrm
naming.cpython-36.pyc25710644editdlrm
search.cpython-36.pyc313390644editdlrm
splits.cpython-36.pyc225220644editdlrm
streaming.cpython-36.pyc41190644editdlrm
table.cpython-36.pyc796120644editdlrm
__init__.cpython-36.pyc22820644editdlrm
Edit: /usr/local/lib/python3.6/site-packages/datasets/__pycache__/metric.cpython-36.pyc (23217B)
3 <%Egk@s.dZddlZddlZddlZddlmZmZmZmZm Z m Z ddl Z ddl ZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZmZdd l m!Z!ddl"m#Z#m$Z$m%Z%ddl&m'Z'ddl(m)Z)m*Z*e'e+Z,Gddde#Z-ddZ.GdddZ/Gddde/Z0dS)z Metrics base class.N)AnyDictListOptionalTupleUnion)config)Dataset) ArrowReader) ArrowWriter)DownloadConfig)DownloadManager)Features) DatasetInfo MetricInfo)camelcase_to_snakecase) BaseFileLockFileLockTimeout) get_logger)copyfunc temp_seedcs0eZdZdZfddZddZddZZS) FileFreeLockz-Thread lock until a file **cannot** be lockedcs"t||_tj|f||dS)N)rfilelocksuper__init__)self lock_fileargskwargs) __class__9/usr/local/lib/python3.6/site-packages/datasets/metric.pyr-s zFileFreeLock.__init__c CsHy|jjdddWntk r2|jj|_YnX|jjd|_dS)Ng{Gz?g{Gz?)timeoutZpoll_intervall)racquirerr _lock_file_fdrelease)rr"r"r#_acquire1s  zFileFreeLock._acquirecCs d|_dS)N)r&)rr"r"r#_release<szFileFreeLock._release)__name__ __module__ __qualname____doc__rr(r) __classcell__r"r")r!r#r*s  rcCsRt|tk st|dkr |Sdd}d||ddd||dddS) NcSsdjdd|DS)Nz, css|]}t|VqdS)N)repr).0xr"r"r# Gsz?summarize_if_long_list..format_chunk..)join)chunkr"r"r# format_chunkFsz,summarize_if_long_list..format_chunk[z, ..., ])typelistlen)objr6r"r"r#summarize_if_long_listBsr?c@seZdZdZedddZeddZeeddd Z ee edd d Z eedd d Z eedddZ eedddZeedddZee edddZeedddZee eedddZee eedddZeedddZee edddZd S)!MetricInfoMixinzoThis base class exposes some attributes of MetricInfo at the base level of the Metric for easy access. )infocCs ||_dS)N) _metric_info)rrAr"r"r#rQszMetricInfoMixin.__init__cCs|jS)zN:class:`datasets.MetricInfo` object containing all the metadata in the metric.)rB)rr"r"r#rATszMetricInfoMixin.info)returncCs|jjS)N)rB metric_name)rr"r"r#nameYszMetricInfoMixin.namecCs|jjS)N)rB experiment_id)rr"r"r#rF]szMetricInfoMixin.experiment_idcCs|jjS)N)rB description)rr"r"r#rGaszMetricInfoMixin.descriptioncCs|jjS)N)rBcitation)rr"r"r#rHeszMetricInfoMixin.citationcCs|jjS)N)rBfeatures)rr"r"r#rIiszMetricInfoMixin.featurescCs|jjS)N)rBinputs_description)rr"r"r#rJmsz"MetricInfoMixin.inputs_descriptioncCs|jjS)N)rBhomepage)rr"r"r#rKqszMetricInfoMixin.homepagecCs|jjS)N)rBlicense)rr"r"r#rLuszMetricInfoMixin.licensecCs|jjS)N)rB codebase_urls)rr"r"r#rMyszMetricInfoMixin.codebase_urlscCs|jjS)N)rBreference_urls)rr"r"r#rN}szMetricInfoMixin.reference_urlscCs|jjS)N)rB streamable)rr"r"r#rOszMetricInfoMixin.streamablecCs|jjS)N)rBformat)rr"r"r#rPszMetricInfoMixin.formatN)r*r+r,r-rrpropertyrAstrrErrFrGrHrrIrJrKrLrrMrNboolrOrPr"r"r"r#r@Ls6 r@c @sHeZdZdZd1eeeeeeeeeeeeeee fd d d Z d d Z d dZ ddZ d2eeefdddZeeeeefdddZddZddZddZdddeedddZddddd Zddd!d"d#Zd3d$d%Zedd&d'Zd4eeeed(d)d*Zd+d,Z ddde!ee"fdd-d.Z#d/d0Z$dS)5MetricarA Metric is the base class and common API for all metrics. Args: config_name (``str``): This is used to define a hash specific to a metrics computation script and prevents the metric's data to be overridden when the metric loading script is modified. keep_in_memory (:obj:`bool`): keep all predictions and references in memory. Not possible in distributed settings. cache_dir (``str``): Path to a directory in which temporary prediction/references data will be stored. The data directory should be located on a shared file-system in distributed setups. num_process (``int``): specify the total number of nodes in a distributed settings. This is useful to compute metrics in distributed setups (in particular non-additive metrics like F1). process_id (``int``): specify the id of the current process in a distributed setup (between 0 and num_process-1) This is useful to compute metrics in distributed setups (in particular non-additive metrics like F1). seed (:obj:`int`, optional): If specified, this will temporarily set numpy's random seed when :func:`datasets.Metric.compute` is run. experiment_id (``str``): A specific experiment id. This is used if several distributed evaluations share the same file system. This is useful to compute metrics in distributed setups (in particular non-additive metrics like F1). max_concurrent_cache_files (``int``): Max number of concurrent metrics cache files (default 10000). timeout (``Union[int, float]``): Timeout in second for distributed setting synchronization. NFrr'd) config_namekeep_in_memory cache_dir num_process process_idseedrFmax_concurrent_cache_filesr$c Ks|pd|_|j} t|jj| _|j| _|p.d| _tj|| t |t  sR|dkrZt dt |t  sn||krvt d|r|dkrt d||_ ||_ ||_||_tjj|ptj|_|j|_|dkrtjj^} }} } | dkr|| n|d|_n||_| |_tjt|j ||_ tjt|j!||_!tjt|j"||_"|j j#j$|j%j&7_$|j!j#j$|j%j&7_$|j"j#j$|j%j&7_$d|_'d|_(d|_)d|_*d|_+d|_,d|_-d|_.d|_/dS) NdefaultZdefault_experimentrz.'process_id' should be a number greater than 0z8'num_process' should be a number greater than process_idrzPUsing 'keep_in_memory' is not possible in distributed setting (num_process > 1).ip)0rW_inforr!r*rDrFr@r isinstanceint ValueErrorrZr[r]rXospath expanduserr ZHF_METRICS_CACHE_data_dir_root_build_data_dirdata_dirnprandomZ get_stater\r$types MethodTypercompute add_batchadd__func__r-rArJ buf_writerwriterwriter_batch_sizedatacache_file_namerrendez_vous_lock file_paths filelocks)rrWrXrYrZr[r\rFr]r$r rA_posr"r"r#rsL     zMetric.__init__cCs|jdkrdSt|jS)zReturn the number of examples (predictions or predictions/references pair) currently stored in the metric's cache. Nr)rrr=)rr"r"r#__len__szMetric.__len__c Cs(d|jd|jd|jdt|d S)NzMetric(name: "z ", features: z , usage: """z""", stored examples: ))rErIrJr=)rr"r"r#__repr__szMetric.__repr__cCs,|j}tjj||j|j}tj|dd|S)aPath of this metric in cache_dir: Will be: self._data_dir_root/self.name/self.config_name/self.hash (if not none)/ If any of these element is missing or if ``with_version=False`` the corresponding subfolders are dropped. T)exist_ok)rfrcrdr4rErWmakedirs)rZbuilder_data_dirr"r"r#rgszMetric._build_data_dir)rCcCstjj|j|jd|jd|jd}d}xt|jD]}t |d}y|j |dWnt k r|jdkrt d|d|jd d||jdkrt d |jd dt tj}tjj|j|jd|d|jd|jd}Yq8XPq8W||fS) zTCreate a new cache file. If the default cache file is used, we generated a new hash.-z.arrowNz.lock)r$rz^Error in _create_cache_file: another metric instance is already using the local cache file at z.. Please specify an experiment_id (currently: z:) to avoid collision between distributed metric instances.zCannot acquire lock, too many metric instance are operating concurrently on this file system.You should set a larger value of max_concurrent_cache_files when creating the metric (current value is z).)rcrdr4rhrFrZr[ranger]rr%rrbrRuuiduuid4)rr$ file_pathriZ file_uuidr"r"r#_create_cache_files((   .zMetric._create_cache_filec sjdkr&jdkrtdjg}nfddtjD}g}xt|D]t\}}|dkrj|jjqLt|d}y|jj dWn,t k rtd |d |d dYqLX|j|qLW||fS) zGet a lock on all the cache files in a distributed setup. We wait for timeout second to let all the distributed node finish their tasks (default is 100 seconds). rNz|Metric cache file doesn't exist. Please make sure that you call `add` or `add_batch` at least once before calling `compute`.c s2g|]*}tjjjjdjd|dqS)rz.arrow)rcrdr4rhrFrZ)r1r[)rr"r# (sz/Metric._get_all_cache_files..rz.lock)r$z#Cannot acquire lock on cached file z for process .) rZrurbr enumerateappendrrr%r$r)rrwrxr[rrr")rr#_get_all_cache_filess(      zMetric._get_all_cache_filesc s|fddtjD}x^|D]V}t|}y|jjdWn.tk rjtd|djddYqX|jqWdS)Nc s2g|]*}tjjjjdjd|dqS)rz .arrow.lock)rcrdr4rhrFrZ)r1r[)rr"r#r@sz5Metric._check_all_processes_locks..)r$zExpected to find locked file z from process z but it doesn't exist.) rrZrr%r$rrbr[r')rZexpected_lock_file_namesexpected_lock_file_name nofilelockr")rr#_check_all_processes_locks>s   z!Metric._check_all_processes_lockscCstjj|j|jd|jd}t|}y|j|jdWn.t k rht d|d|j ddYn X|j tjj|j|jd|jd}t |}y|j|jdWn.t k rt d|d|j d dYn X|j dS) Nrz -0.arrow.lock)r$zExpected to find locked file z from process z but it doesn't exist.z -rdv.lockzCouldn't acquire lock on r)rcrdr4rhrFrZrr%r$rrbr[r'r)rrrZlock_file_namervr"r"r#_check_rendez_vousNs     zMetric._check_rendez_vousc Cs|jdk r|jjd|_|jdk r8|jdkr8|jj|jrht|jt|j dd}t j |j j |_nv|jdkr|j\}}y4tdt|j dd}t f|jdd|D|_Wntk rtddYnX||_||_dS) zClose all the writing process and load/gather the data from all the nodes if main node or all_process is True. Nr)rI)rdrAcSsg|] }d|iqS)filenamer")r1fr"r"r#rysz$Metric._finalize..zError in finalize: another metric instance is already using the local cache file. Please specify an experiment_id to avoid collision between distributed metric instances.)rrfinalizerr[r'rXr rhrrIr from_bufferrqgetvaluertrZ read_filesFileNotFoundErrorrbrwrx)rreaderrwrxr"r"r# _finalizebs&       zMetric._finalize) predictions referencesc s||d|dkr>|dkr>fddjD}j|n2fddjD}|rptd|dtjfd djD}fd dD}td d |jDrjf|jd_d_ j d krj j j jdfddjD}tjjf||}WdQRXjdk rDd_` d_ n\xZtttjjD]@\} } tj d| ` d_ `d_tj| | jq\W|SdSdS)aiCompute the metrics. Usage of positional arguments is not allowed to prevent mistakes. Args: predictions (list/array/tensor, optional): Predictions. references (list/array/tensor, optional): References. **kwargs (optional): Keyword arguments that will be forwarded to the metrics :meth:`_compute` method (see details in the docstring). Return: dict or None - Dictionary with the metrics if this metric is run on the main process (``process_id == 0``). - None if the metric is not run on the main process (``process_id != 0``). Example: ```py >>> from datasets import load_metric >>> metric = load_metric("accuracy") >>> accuracy = metric.compute(predictions=model_prediction, references=labels) ``` )rrNcsi|]}|krd|qS)Nr")r1k) all_kwargsr"r# sz"Metric.compute..csg|]}|kr|qSr"r")r1r)rr"r#rsz"Metric.compute..zMetric inputs are missing: z. All required inputs are csi|]}||qSr"r")r1 input_name)rr"r#rscs i|]}|jkr||qSr")rI)r1r)r rr"r#rscss|]}|dk VqdS)Nr")r1vr"r"r#r3sz!Metric.compute..r)r;csi|]}j||qSr")rt)r1r)rr"r#rsz Removing )rIupdaterbr<anyvaluesrnrrurr[rtZ set_formatrArPrr\_computerqreversedziprxrwloggerrrrcremover') rrrr Zmissing_kwargsZmissing_inputsinputsZcompute_kwargsoutputrrr")rr rr#rmsD       zMetric.computec s|fdd|D}|r0td|dtj||d|fddjDjjjjdkrrjyjjWntj k rvt fd d Drt t fd dDd }d dt d|dt |d }nftjddgkrHdjd}djfdd jD}||7}n djdt|dt|}t|dYnXdS)aAdd a batch of predictions and references for the metric's stack. Args: predictions (list/array/tensor, optional): Predictions. references (list/array/tensor, optional): References. Example: ```py >>> from datasets import load_metric >>> metric = load_metric("accuracy") >>> metric.add_batch(predictions=model_prediction, references=labels) ``` csg|]}|jkr|qSr")rI)r1r)rr"r#rsz$Metric.add_batch..zBad inputs for metric: z. All required inputs are )rrcsi|]}||qSr"r")r1 intput_name)batchr"r#rsz$Metric.add_batch..Nc3s.|]&}t|tttjkVqdS)N)r=nextiterr)r1c)rr"r#r3sz#Metric.add_batch..cs(g|] }t|tkr|qSr")r=)r1r)rcol0r"r#rsrzMismatch in the number of z (z) and r|rrz@Metric inputs don't match the expected format. Expected format: z, c3s&|]}d|dt|VqdS)zInput z: N)r?)r1r)rr"r#r3szPPredictions and/or references don't match the expected format. Expected format: z, Input predictions: z, Input references: )rbr<rIrAZ encode_batchrr _init_writerZ write_batchpa ArrowInvalidrrrr=sortedr4r?)rrrr bad_inputsZbad_col error_msgerror_msg_inputsr")rrrr#rns,  0  zMetric.add_batch) prediction referencec sfdd|D}|r0td|dtj||d|fddjDjjjjdkrrjyjjWnPtj k rd jd }d j fd d jD}||7}t|dYnXdS) aAdd one prediction and reference for the metric's stack. Args: prediction (list/array/tensor, optional): Predictions. reference (list/array/tensor, optional): References. Example: ```py >>> from datasets import load_metric >>> metric = load_metric("accuracy") >>> metric.add(predictions=model_predictions, references=labels) ``` csg|]}|jkr|qSr")rI)r1r)rr"r#r szMetric.add..zBad inputs for metric: z. All required inputs are )rrcsi|]}||qSr"r")r1r)exampler"r#r szMetric.add..Nz@Metric inputs don't match the expected format. Expected format: z, c3s&|]}d|dt|VqdS)zInput z: N)r?)r1r)rr"r#r3szMetric.add..) rbr<rIrAZencode_examplerrrwriterrr4)rrrr rrrr")rrr#ros  z Metric.addc Cs,|jdkr|jdkrtjj|j|jd|jd}t||_y|jj |dWn.t k r~t d|d|jddYnX|j rt j|_t|jj|j|jd |_nJd|_|jdks|jdkr|j\}}||_||_t|jj|j|jd |_|jdkr(|jdkr |j|jjn|jdS) Nrrrz -rdv.lock)r$zXError in _init_writer: another metric instance is already using the local cache file at z.. Please specify an experiment_id (currently: z:) to avoid collision between distributed metric instances.)rIstreamrs)rIrdrs)rZr[rcrdr4rhrFrrvr% TimeoutErrorrbrXrZBufferOutputStreamrqr rArIrsrrrurrrr'r)rr$rrurr"r"r#rs4          zMetric._init_writercCstdS)aConstruct the MetricInfo object. See `MetricInfo` for details. Warning: This function is only called once and the result is cached for all following .info() calls. Returns: info: (MetricInfo) The metrics information N)NotImplementedError)rr"r"r#r_Bs z Metric._info)download_config dl_managercCsN|dkr@|dkr.t}tjj|jd|_d|_t|j||jd}|j |dS)aDownloads and prepares dataset for reading. Args: download_config (:class:`DownloadConfig`, optional): Specific download configuration parameters. dl_manager (:class:`DownloadManager`, optional): Specific download manager to use. NZ downloadsF)Z dataset_namerrh) r rcrdr4rhrYZforce_downloadrrE_download_and_prepare)rrrr"r"r#download_and_prepareMs zMetric.download_and_preparecCsdS)aYDownloads and prepares resources for the metric. This is the internal implementation to overwrite called when user calls `download_and_prepare`. It should download all required resources for the metric. Args: dl_manager (:class:`DownloadManager`): `DownloadManager` used to download and cache data. Nr")rrr"r"r#rds zMetric._download_and_preparecKstdS)zEThis method defines the common API for all the metrics in the libraryN)r)rrrr r"r"r#roszMetric._computecCs\t|dr|jdk r|jjt|dr<|jdk r<|jjt|drJ|`t|drX|`dS)Nrrvrrrt)hasattrrr'rvrrrt)rr"r"r#__del__ss    zMetric.__del__) NFNrrNNrUrV)r)r)NN)%r*r+r,r-rrRrSrarfloatrr{r}rgrrrrrrrrdictrmrnrorrr_r rrrrrrrr"r"r"r#rTs88: !#"G/! '  rT)1r-rcrkrtypingrrrrrrZnumpyriZpyarrowrrr Z arrow_datasetr Z arrow_readerr Z arrow_writerr Zdownload.download_configr Zdownload.download_managerrrIrrArrZnamingrZutils.filelockrrrZ utils.loggingrZutils.py_utilsrrr*rrr?r@rTr"r"r"r#s.           >