


import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam







class 						_snake_case    ( datasets.BeamBasedBuilder				):




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return datasets.DatasetInfo(
           features=datasets.Features({'''content''': datasets.Value('''string'''       )}       )   ,    supervised_keys=_lowerCamelCase   ,    )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       return [datasets.SplitGenerator(name=datasets.Split.TRAIN   ,    gen_kwargs={'''examples''': get_test_dummy_examples()}       )]




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       import apache_beam as beam

       return pipeline | "Load Examples" >> beam.Create(_lowerCamelCase       )







class 						_snake_case    ( datasets.BeamBasedBuilder				):




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return datasets.DatasetInfo(
           features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string'''       )}       )}       )   ,    supervised_keys=_lowerCamelCase   ,    )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       return [
           datasets.SplitGenerator(name=datasets.Split.TRAIN   ,    gen_kwargs={'''examples''': get_test_nested_examples()}       )
       ]




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       import apache_beam as beam

       return pipeline | "Load Examples" >> beam.Create(_lowerCamelCase       )







def 							__lowerCamelCase		(					):




      """simple docstring"""





      return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar''']		)]
def 							__lowerCamelCase		(					):




      """simple docstring"""





      return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar''']		)]







class 						_snake_case    ( _snake_case				):




 @require_beam
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Dict									=			len(get_test_dummy_examples()       )
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
             a		:Optional[Any]									=			DummyBeamDataset(cache_dir=_lowerCamelCase   ,    beam_runner='''DirectRunner'''       )
             builder.download_and_prepare()
             self.assertTrue(
                 os.path.exists(
                     os.path.join(_lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    F'''{builder.name}-train.arrow'''       )       )       )
             self.assertDictEqual(builder.info.features   ,    datasets.Features({'''content''': datasets.Value('''string'''       )}       )       )
             a		:str									=			builder.as_dataset()
             self.assertEqual(dset['''train'''].num_rows   ,    _lowerCamelCase       )
             self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples   ,    _lowerCamelCase       )
             self.assertDictEqual(dset['''train'''][0]   ,    get_test_dummy_examples()[0][1]       )
             self.assertDictEqual(
                 dset['''train'''][expected_num_examples - 1]   ,    get_test_dummy_examples()[expected_num_examples - 1][1]       )
             self.assertTrue(
                 os.path.exists(os.path.join(_lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    '''dataset_info.json'''       )       )       )
             del dset




 @require_beam
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       import apache_beam as beam

       a		:Any									=			beam.io.parquetio.WriteToParquet

       a		:Any									=			len(get_test_dummy_examples()       )
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
             a		:Union[str, Any]									=			DummyBeamDataset(cache_dir=_lowerCamelCase   ,    beam_runner='''DirectRunner'''       )
             with patch('''apache_beam.io.parquetio.WriteToParquet'''       ) as write_parquet_mock:
                   a		:str									=			partial(_lowerCamelCase   ,    num_shards=2       )
                   builder.download_and_prepare()
             self.assertTrue(
                 os.path.exists(
                     os.path.join(
                         _lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    F'''{builder.name}-train-00000-of-00002.arrow'''       )       )       )
             self.assertTrue(
                 os.path.exists(
                     os.path.join(
                         _lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    F'''{builder.name}-train-00000-of-00002.arrow'''       )       )       )
             self.assertDictEqual(builder.info.features   ,    datasets.Features({'''content''': datasets.Value('''string'''       )}       )       )
             a		:List[str]									=			builder.as_dataset()
             self.assertEqual(dset['''train'''].num_rows   ,    _lowerCamelCase       )
             self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples   ,    _lowerCamelCase       )
             # Order is not preserved when sharding, so we just check that all the elements are there
             self.assertListEqual(sorted(dset['''train''']['''content''']       )   ,    sorted(['''foo''', '''bar''', '''foobar''']       )       )
             self.assertTrue(
                 os.path.exists(os.path.join(_lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    '''dataset_info.json'''       )       )       )
             del dset




 @require_beam
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
             a		:Dict									=			DummyBeamDataset(cache_dir=_lowerCamelCase       )
             self.assertRaises(datasets.builder.MissingBeamOptions   ,    builder.download_and_prepare       )




 @require_beam
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:str									=			len(get_test_nested_examples()       )
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
             a		:List[Any]									=			NestedBeamDataset(cache_dir=_lowerCamelCase   ,    beam_runner='''DirectRunner'''       )
             builder.download_and_prepare()
             self.assertTrue(
                 os.path.exists(
                     os.path.join(_lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    F'''{builder.name}-train.arrow'''       )       )       )
             self.assertDictEqual(
                 builder.info.features   ,    datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string'''       )}       )}       )       )
             a		:Any									=			builder.as_dataset()
             self.assertEqual(dset['''train'''].num_rows   ,    _lowerCamelCase       )
             self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples   ,    _lowerCamelCase       )
             self.assertDictEqual(dset['''train'''][0]   ,    get_test_nested_examples()[0][1]       )
             self.assertDictEqual(
                 dset['''train'''][expected_num_examples - 1]   ,    get_test_nested_examples()[expected_num_examples - 1][1]       )
             self.assertTrue(
                 os.path.exists(os.path.join(_lowerCamelCase   ,    builder.name   ,    '''default'''   ,    '''0.0.0'''   ,    '''dataset_info.json'''       )       )       )
             del dset









import warnings

from ...utils import is_sklearn_available, requires_backends


if is_sklearn_available():
     from scipy.stats import pearsonr, spearmanr
     from sklearn.metrics import fa_score, matthews_corrcoef


snake_case							:       List[str]										=	(
    '''This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate '''
    '''library. You can have a look at this example script for pointers: '''
    '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py'''
)
def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[str]					,    UpperCAmelCase_		:						Optional[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      return (preds == labels).mean()
def 							__lowerCamelCase		(					UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						int		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      a		:Dict									=			simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)
      a		:Any									=			fa_score(y_true=UpperCAmelCase_					,    y_pred=UpperCAmelCase_		)
      return {
          "acc": acc,
          "f1": fa,
          "acc_and_f1": (acc + fa) / 2,
      }
def 							__lowerCamelCase		(					UpperCAmelCase_		:						Dict					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      a		:str									=			pearsonr(UpperCAmelCase_					,    UpperCAmelCase_		)[0]
      a		:Union[str, Any]									=			spearmanr(UpperCAmelCase_					,    UpperCAmelCase_		)[0]
      return {
          "pearson": pearson_corr,
          "spearmanr": spearman_corr,
          "corr": (pearson_corr + spearman_corr) / 2,
      }
def 							__lowerCamelCase		(					UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						Dict		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      assert len(UpperCAmelCase_		) == len(UpperCAmelCase_		), F'''Predictions and labels have mismatched lengths {len(UpperCAmelCase_		)} and {len(UpperCAmelCase_		)}'''
      if task_name == "cola":
            return {"mcc": matthews_corrcoef(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "sst-2":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "mrpc":
            return acc_and_fa(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "sts-b":
            return pearson_and_spearman(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "qqp":
            return acc_and_fa(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "mnli":
            return {"mnli/acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "mnli-mm":
            return {"mnli-mm/acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "qnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "rte":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "wnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "hans":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      else:
            raise KeyError(UpperCAmelCase_		)






def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[str]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      if len(UpperCAmelCase_		) != len(UpperCAmelCase_		):
            raise ValueError(F'''Predictions and labels have mismatched lengths {len(UpperCAmelCase_		)} and {len(UpperCAmelCase_		)}'''		)
      if task_name == "xnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      else:
            raise KeyError(UpperCAmelCase_		)





