


from copy import deepcopy
from unittest.case import TestCase

import pytest

from datasets.arrow_dataset import Dataset
from datasets.features import Audio, ClassLabel, Features, Image, Sequence, Value
from datasets.info import DatasetInfo
from datasets.tasks import (
    AudioClassification,
    AutomaticSpeechRecognition,
    ImageClassification,
    LanguageModeling,
    QuestionAnsweringExtractive,
    Summarization,
    TextClassification,
    task_template_from_dict,
)
from datasets.utils.py_utils import asdict


_A									=		{
    'id': '5733be284776f41900661182',
    'title': 'University_of_Notre_Dame',
    'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.',
    'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?',
    'answers': {'text': ['Saint Bernadette Soubirous'], 'answer_start': [515]},
}

@pytest.mark.parametrize(
    'task_cls'     ,	[
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]     ,	)
def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   Union[str, Any]							):
     __UpperCamelCase       				=task_cls()
     __UpperCamelCase       				=asdict(SCREAMING_SNAKE_CASE__							)
     __UpperCamelCase       				=task_template_from_dict(SCREAMING_SNAKE_CASE__							)
     assert task == reloaded






class        UpperCAmelCase__			:



    """simple docstring"""






    def _a   (     self       )    ->			Tuple:
         __UpperCamelCase       				=LanguageModeling(text_column='input_text'       )
         assert {"input_text": "text"} == task.column_mapping






    def _a   (     self       )    ->			int:
         __UpperCamelCase       				=Features({'text': Value('string'       )}       )
         __UpperCamelCase       				={'text_column': 'input_text'}
         __UpperCamelCase       				=LanguageModeling.from_dict(A_       )
         assert "language-modeling" == task.task
         assert input_schema == task.input_schema






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			str:
         __UpperCamelCase       				=sorted(['pos', 'neg']       )






    def _a   (     self       )    ->			Tuple:
         __UpperCamelCase       				=TextClassification(text_column='input_text'   ,  label_column='input_label'       )
         self.assertDictEqual({'input_text': 'text', 'input_label': 'labels'}   ,  task.column_mapping       )






    def _a   (     self       )    ->			Any:
         __UpperCamelCase       				=Features({'text': Value('string'       )}       )
         # Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
         __UpperCamelCase       				=Features({'labels': ClassLabel}       )
         __UpperCamelCase       				={'text_column': 'input_text', 'label_column': 'input_labels'}
         __UpperCamelCase       				=TextClassification.from_dict(A_       )
         self.assertEqual('text-classification'   ,  task.task       )
         self.assertEqual(A_   ,  task.input_schema       )
         self.assertEqual(A_   ,  task.label_schema       )






    def _a   (     self       )    ->			Optional[int]:
         __UpperCamelCase       				=TextClassification(text_column='input_text'   ,  label_column='input_label'       )
         self.assertEqual(task.label_schema['labels']   ,  A_       )
         __UpperCamelCase       				=task.align_with_features(Features({'input_label': ClassLabel(names=self.labels       )}       )       )
         self.assertEqual(task.label_schema['labels']   ,  ClassLabel(names=self.labels       )       )






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			str:
         __UpperCamelCase       				=QuestionAnsweringExtractive(
             context_column='input_context'   ,  question_column='input_question'   ,  answers_column='input_answers'       )
         self.assertDictEqual(
             {'input_context': 'context', 'input_question': 'question', 'input_answers': 'answers'}   ,  task.column_mapping       )






    def _a   (     self       )    ->			Any:
         __UpperCamelCase       				=Features({'question': Value('string'       ), 'context': Value('string'       )}       )
         __UpperCamelCase       				=Features(
             {
                 'answers': Sequence(
                     {
                         'text': Value('string'       ),
                         'answer_start': Value('int32'       ),
                     }       )
             }       )
         __UpperCamelCase       				={
             'context_column': 'input_input_context',
             'question_column': 'input_question',
             'answers_column': 'input_answers',
         }
         __UpperCamelCase       				=QuestionAnsweringExtractive.from_dict(A_       )
         self.assertEqual('question-answering-extractive'   ,  task.task       )
         self.assertEqual(A_   ,  task.input_schema       )
         self.assertEqual(A_   ,  task.label_schema       )






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			Union[str, Any]:
         __UpperCamelCase       				=Summarization(text_column='input_text'   ,  summary_column='input_summary'       )
         self.assertDictEqual({'input_text': 'text', 'input_summary': 'summary'}   ,  task.column_mapping       )






    def _a   (     self       )    ->			Union[str, Any]:
         __UpperCamelCase       				=Features({'text': Value('string'       )}       )
         __UpperCamelCase       				=Features({'summary': Value('string'       )}       )
         __UpperCamelCase       				={'text_column': 'input_text', 'summary_column': 'input_summary'}
         __UpperCamelCase       				=Summarization.from_dict(A_       )
         self.assertEqual('summarization'   ,  task.task       )
         self.assertEqual(A_   ,  task.input_schema       )
         self.assertEqual(A_   ,  task.label_schema       )






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			Tuple:
         __UpperCamelCase       				=AutomaticSpeechRecognition(audio_column='input_audio'   ,  transcription_column='input_transcription'       )
         self.assertDictEqual({'input_audio': 'audio', 'input_transcription': 'transcription'}   ,  task.column_mapping       )






    def _a   (     self       )    ->			Union[str, Any]:
         __UpperCamelCase       				=Features({'audio': Audio()}       )
         __UpperCamelCase       				=Features({'transcription': Value('string'       )}       )
         __UpperCamelCase       				={
             'audio_column': 'input_audio',
             'transcription_column': 'input_transcription',
         }
         __UpperCamelCase       				=AutomaticSpeechRecognition.from_dict(A_       )
         self.assertEqual('automatic-speech-recognition'   ,  task.task       )
         self.assertEqual(A_   ,  task.input_schema       )
         self.assertEqual(A_   ,  task.label_schema       )






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			List[Any]:
         __UpperCamelCase       				=sorted(['pos', 'neg']       )






    def _a   (     self       )    ->			Optional[Any]:
         __UpperCamelCase       				=AudioClassification(audio_column='input_audio'   ,  label_column='input_label'       )
         self.assertDictEqual({'input_audio': 'audio', 'input_label': 'labels'}   ,  task.column_mapping       )






    def _a   (     self       )    ->			Tuple:
         __UpperCamelCase       				=Features({'audio': Audio()}       )
         __UpperCamelCase       				=Features({'labels': ClassLabel}       )
         __UpperCamelCase       				={
             'audio_column': 'input_image',
             'label_column': 'input_label',
         }
         __UpperCamelCase       				=AudioClassification.from_dict(A_       )
         self.assertEqual('audio-classification'   ,  task.task       )
         self.assertEqual(A_   ,  task.input_schema       )
         self.assertEqual(A_   ,  task.label_schema       )






    def _a   (     self       )    ->			str:
         __UpperCamelCase       				=AudioClassification(audio_column='input_audio'   ,  label_column='input_label'       )
         self.assertEqual(task.label_schema['labels']   ,  A_       )
         __UpperCamelCase       				=task.align_with_features(Features({'input_label': ClassLabel(names=self.labels       )}       )       )
         self.assertEqual(task.label_schema['labels']   ,  ClassLabel(names=self.labels       )       )






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			Tuple:
         __UpperCamelCase       				=sorted(['pos', 'neg']       )






    def _a   (     self       )    ->			Optional[Any]:
         __UpperCamelCase       				=ImageClassification(image_column='input_image'   ,  label_column='input_label'       )
         self.assertDictEqual({'input_image': 'image', 'input_label': 'labels'}   ,  task.column_mapping       )






    def _a   (     self       )    ->			str:
         __UpperCamelCase       				=Features({'image': Image()}       )
         __UpperCamelCase       				=Features({'labels': ClassLabel}       )
         __UpperCamelCase       				={
             'image_column': 'input_image',
             'label_column': 'input_label',
         }
         __UpperCamelCase       				=ImageClassification.from_dict(A_       )
         self.assertEqual('image-classification'   ,  task.task       )
         self.assertEqual(A_   ,  task.input_schema       )
         self.assertEqual(A_   ,  task.label_schema       )






    def _a   (     self       )    ->			int:
         __UpperCamelCase       				=ImageClassification(image_column='input_image'   ,  label_column='input_label'       )
         self.assertEqual(task.label_schema['labels']   ,  A_       )
         __UpperCamelCase       				=task.align_with_features(Features({'input_label': ClassLabel(names=self.labels       )}       )       )
         self.assertEqual(task.label_schema['labels']   ,  ClassLabel(names=self.labels       )       )






class        UpperCAmelCase__			(   A_						):



    """simple docstring"""






    def _a   (     self       )    ->			Any:
         __UpperCamelCase       				=DatasetInfo(task_templates=QuestionAnsweringExtractive()       )
         __UpperCamelCase       				=Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}   ,  info=A_       )
         assert isinstance(dataset.info.task_templates   ,  A_       )
         assert len(dataset.info.task_templates       ) == 1

         def keep_task(A_       ):
              return x

         def dont_keep_task(A_       ):
              __UpperCamelCase       				=deepcopy(A_       )
              __UpperCamelCase       				=0
              return out

         __UpperCamelCase       				=dataset.map(A_       )
         assert mapped_dataset.info.task_templates == dataset.info.task_templates
         # reload from cache
         __UpperCamelCase       				=dataset.map(A_       )
         assert mapped_dataset.info.task_templates == dataset.info.task_templates

         __UpperCamelCase       				=dataset.map(A_       )
         assert mapped_dataset.info.task_templates == []
         # reload from cache
         __UpperCamelCase       				=dataset.map(A_       )
         assert mapped_dataset.info.task_templates == []






    def _a   (     self       )    ->			str:
         __UpperCamelCase       				=Features({'text': Value('string'       ), 'label': ClassLabel(names=('pos', 'neg')       )}       )
         __UpperCamelCase       				=TextClassification(text_column='text'   ,  label_column='label'       )
         __UpperCamelCase       				=DatasetInfo(features=A_   ,  task_templates=A_       )
         __UpperCamelCase       				=Dataset.from_dict({'text': ['A sentence.'], 'label': ['pos']}   ,  info=A_       )

         def process(A_       ):
              return example

         __UpperCamelCase       				=dataset.remove_columns('label'       )
         __UpperCamelCase       				=modified_dataset.map(A_       )
         assert mapped_dataset.info.task_templates == []









import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


_A									=		50_0000

_A			,	_A									=		os.path.split(__file__)
_A									=		os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))

@get_duration
def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   datasets.Dataset							):
     __UpperCamelCase       				=dataset.select(range(0     ,	len(SCREAMING_SNAKE_CASE__							)     ,	2							)							)

@get_duration
def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   datasets.Dataset							):
     __UpperCamelCase       				=dataset.sort('numbers'							)

@get_duration
def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   datasets.Dataset							):
     __UpperCamelCase       				=dataset.shuffle()

@get_duration
def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   datasets.Dataset							):
     __UpperCamelCase       				=dataset.train_test_split(0.1							)

@get_duration
def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   datasets.Dataset     ,	SCREAMING_SNAKE_CASE__     :   int=10							):
     for shard_id in range(SCREAMING_SNAKE_CASE__							):
          __UpperCamelCase       				=dataset.shard(SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__							)

def 		_UpperCAmelCase							(     ):
     __UpperCamelCase       				={'num examples': SPEED_TEST_N_EXAMPLES}
     __UpperCamelCase       				=(select, sort, shuffle, train_test_split, shard)
     with tempfile.TemporaryDirectory() as tmp_dir:
          print('generating dataset'							)
          __UpperCamelCase       				=datasets.Features({'text': datasets.Value('string'							), 'numbers': datasets.Value('float32'							)}							)
          __UpperCamelCase       				=generate_example_dataset(
              os.path.join(SCREAMING_SNAKE_CASE__     ,	'dataset.arrow'							)     ,	SCREAMING_SNAKE_CASE__     ,	num_examples=SCREAMING_SNAKE_CASE__							)
          print('Functions'							)
          for func in functions:
               print(func.__name__							)
               __UpperCamelCase       				=func(SCREAMING_SNAKE_CASE__							)

     with open(SCREAMING_SNAKE_CASE__     ,	'wb'							) as f:
          f.write(json.dumps(SCREAMING_SNAKE_CASE__							).encode('utf-8'							)							)


if __name__ == "__main__":  # useful to run the profiler
   benchmark_indices_mapping()





