





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__			:    List[Any]			      =		{
    '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_						(						_UpperCAmelCase       :int							)  ->							str:
     '''simple docstring'''


     A_					      =					task_cls()
     A_					      =					asdict(_SCREAMING_SNAKE_CASE							)
     A_					      =					task_template_from_dict(_SCREAMING_SNAKE_CASE							)
     assert task == reloaded





class UpperCAmelCase_							:

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					LanguageModeling(text_column='''input_text'''   )
       assert {"input_text": "text"} == task.column_mapping




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					Features({'''text''': Value('''string'''   )}   )
       A_					      =					{'text_column': 'input_text'}
       A_					      =					LanguageModeling.from_dict(__a   )
       assert "language-modeling" == task.task
       assert input_schema == task.input_schema





class UpperCAmelCase_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					sorted(['''pos''', '''neg''']   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					TextClassification(text_column='''input_text'''   ,label_column='''input_label'''   )
       self.assertDictEqual({'''input_text''': '''text''', '''input_label''': '''labels'''}   ,task.column_mapping   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





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




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					TextClassification(text_column='''input_text'''   ,label_column='''input_label'''   )
       self.assertEqual(task.label_schema['''labels''']   ,__a   )
       A_					      =					task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels   )}   )   )
       self.assertEqual(task.label_schema['''labels''']   ,ClassLabel(names=self.labels   )   )





class UpperCAmelCase_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					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 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					Features({'''question''': Value('''string'''   ), '''context''': Value('''string'''   )}   )
       A_					      =					Features(
           {
               '''answers''': Sequence(
                   {
                       '''text''': Value('''string'''   ),
                       '''answer_start''': Value('''int32'''   ),
                   }   )
           }   )
       A_					      =					{
           'context_column': 'input_input_context',
           'question_column': 'input_question',
           'answers_column': 'input_answers',
       }
       A_					      =					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_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					Summarization(text_column='''input_text'''   ,summary_column='''input_summary'''   )
       self.assertDictEqual({'''input_text''': '''text''', '''input_summary''': '''summary'''}   ,task.column_mapping   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					Features({'''text''': Value('''string'''   )}   )
       A_					      =					Features({'''summary''': Value('''string'''   )}   )
       A_					      =					{'text_column': 'input_text', 'summary_column': 'input_summary'}
       A_					      =					Summarization.from_dict(__a   )
       self.assertEqual('''summarization'''   ,task.task   )
       self.assertEqual(__a   ,task.input_schema   )
       self.assertEqual(__a   ,task.label_schema   )





class UpperCAmelCase_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					AutomaticSpeechRecognition(audio_column='''input_audio'''   ,transcription_column='''input_transcription'''   )
       self.assertDictEqual({'''input_audio''': '''audio''', '''input_transcription''': '''transcription'''}   ,task.column_mapping   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					Features({'''audio''': Audio()}   )
       A_					      =					Features({'''transcription''': Value('''string'''   )}   )
       A_					      =					{
           'audio_column': 'input_audio',
           'transcription_column': 'input_transcription',
       }
       A_					      =					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_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					sorted(['''pos''', '''neg''']   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					AudioClassification(audio_column='''input_audio'''   ,label_column='''input_label'''   )
       self.assertDictEqual({'''input_audio''': '''audio''', '''input_label''': '''labels'''}   ,task.column_mapping   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





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




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					AudioClassification(audio_column='''input_audio'''   ,label_column='''input_label'''   )
       self.assertEqual(task.label_schema['''labels''']   ,__a   )
       A_					      =					task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels   )}   )   )
       self.assertEqual(task.label_schema['''labels''']   ,ClassLabel(names=self.labels   )   )





class UpperCAmelCase_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					sorted(['''pos''', '''neg''']   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					ImageClassification(image_column='''input_image'''   ,label_column='''input_label'''   )
       self.assertDictEqual({'''input_image''': '''image''', '''input_label''': '''labels'''}   ,task.column_mapping   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





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




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					ImageClassification(image_column='''input_image'''   ,label_column='''input_label'''   )
       self.assertEqual(task.label_schema['''labels''']   ,__a   )
       A_					      =					task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels   )}   )   )
       self.assertEqual(task.label_schema['''labels''']   ,ClassLabel(names=self.labels   )   )





class UpperCAmelCase_							(   lowerCAmelCase_   ):

  '''simple docstring'''





  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					DatasetInfo(task_templates=QuestionAnsweringExtractive()   )
       A_					      =					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(__snake_case   ):
            return x

       def dont_keep_task(__snake_case   ):
            A_					      =					deepcopy(__a   )
            A_					      =					0
            return out

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

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




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





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

       def process(__snake_case   ):
            return example

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










from __future__ import annotations

def     UpperCAmelCase_						(						_UpperCAmelCase       :list							)  ->							int | float:
     '''simple docstring'''


     A_					      =					sorted(_UpperCAmelCase							)
     A_					      =					len(_UpperCAmelCase							)
     A_					      =					length >> 1
     return (
         (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
         if length % 2 == 0
         else sorted_list[mid_index]
     )

def     UpperCAmelCase_						(						)  ->							Optional[Any]:
     '''simple docstring'''


     import doctest

     doctest.testmod()


if __name__ == "__main__":
      main()



