






"""simple docstring"""



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


_UpperCamelCase		:  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': [5_1_5]},
}
@pytest.mark.parametrize(
    'task_cls'		, [
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]		, )
def 						_SCREAMING_SNAKE_CASE			(   __snake_case      :  Optional[int]   ):





  '''simple docstring'''





  lowercase							   =	task_cls()
  lowercase							   =	asdict(__snake_case   )
  lowercase							   =	task_template_from_dict(__snake_case   )
  assert task == reloaded

class 	a					:







    def       UpperCamelCase_				(      self	):
      lowercase							   =	LanguageModeling(text_column='input_text'	)
      assert {"input_text": "text"} == task.column_mapping







    def       UpperCamelCase_				(      self	):
      lowercase							   =	Features({'text': Value('string'	)}	)
      lowercase							   =	{'text_column': 'input_text'}
      lowercase							   =	LanguageModeling.from_dict(_lowerCamelCase	)
      assert "language-modeling" == task.task
      assert input_schema == task.input_schema


class 	a					(       a_     ):







    def       UpperCamelCase_				(      self	):
      lowercase							   =	sorted(['pos', 'neg']	)







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







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







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


class 	a					(       a_     ):







    def       UpperCamelCase_				(      self	):
      lowercase							   =	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	):
      lowercase							   =	Features({'question': Value('string'	), 'context': Value('string'	)}	)
      lowercase							   =	Features(
          {
              'answers': Sequence(
                  {
                      'text': Value('string'	),
                      'answer_start': Value('int32'	),
                  }	)
          }	)
      lowercase							   =	{
          'context_column': 'input_input_context',
          'question_column': 'input_question',
          'answers_column': 'input_answers',
      }
      lowercase							   =	QuestionAnsweringExtractive.from_dict(_lowerCamelCase	)
      self.assertEqual('question-answering-extractive'							,		task.task	)
      self.assertEqual(_lowerCamelCase							,		task.input_schema	)
      self.assertEqual(_lowerCamelCase							,		task.label_schema	)


class 	a					(       a_     ):







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







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


class 	a					(       a_     ):







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







    def       UpperCamelCase_				(      self	):
      lowercase							   =	Features({'audio': Audio()}	)
      lowercase							   =	Features({'transcription': Value('string'	)}	)
      lowercase							   =	{
          'audio_column': 'input_audio',
          'transcription_column': 'input_transcription',
      }
      lowercase							   =	AutomaticSpeechRecognition.from_dict(_lowerCamelCase	)
      self.assertEqual('automatic-speech-recognition'							,		task.task	)
      self.assertEqual(_lowerCamelCase							,		task.input_schema	)
      self.assertEqual(_lowerCamelCase							,		task.label_schema	)


class 	a					(       a_     ):







    def       UpperCamelCase_				(      self	):
      lowercase							   =	sorted(['pos', 'neg']	)







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







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







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


class 	a					(       a_     ):







    def       UpperCamelCase_				(      self	):
      lowercase							   =	sorted(['pos', 'neg']	)







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







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







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


class 	a					(       a_     ):







    def       UpperCamelCase_				(      self	):
      lowercase							   =	DatasetInfo(task_templates=QuestionAnsweringExtractive()	)
      lowercase							   =	Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}							,		info=_lowerCamelCase	)
      assert isinstance(dataset.info.task_templates							,		_lowerCamelCase	)
      assert len(dataset.info.task_templates	) == 1

      def keep_task(_lowerCamelCase	):
        return x

      def dont_keep_task(_lowerCamelCase	):
        lowercase							   =	deepcopy(_lowerCamelCase	)
        lowercase							   =	0
        return out

      lowercase							   =	dataset.map(_lowerCamelCase	)
      assert mapped_dataset.info.task_templates == dataset.info.task_templates
      # reload from cache
      lowercase							   =	dataset.map(_lowerCamelCase	)
      assert mapped_dataset.info.task_templates == dataset.info.task_templates

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







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

      def process(_lowerCamelCase	):
        return example

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










"""simple docstring"""



import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor

class 	a					(       unittest.TestCase     ):







    def __init__(      self							,		_lowerCamelCase							,		_lowerCamelCase=7							,		_lowerCamelCase=3							,		_lowerCamelCase=1_8							,		_lowerCamelCase=3_0							,		_lowerCamelCase=4_0_0							,		_lowerCamelCase=True							,		_lowerCamelCase=None							,		_lowerCamelCase=True							,		_lowerCamelCase=[0.5, 0.5, 0.5]							,		_lowerCamelCase=[0.5, 0.5, 0.5]							,		):
      lowercase							   =	size if size is not None else {'height': 1_8, 'width': 1_8}
      lowercase							   =	parent
      lowercase							   =	batch_size
      lowercase							   =	num_channels
      lowercase							   =	image_size
      lowercase							   =	min_resolution
      lowercase							   =	max_resolution
      lowercase							   =	do_resize
      lowercase							   =	size
      lowercase							   =	do_normalize
      lowercase							   =	image_mean
      lowercase							   =	image_std







    def       UpperCamelCase_				(      self	):
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
      }


@require_torch
@require_vision
class 	a					(       a_,    unittest.TestCase     ):
    UpperCAmelCase_			:					int     					=ViTImageProcessor if is_vision_available() else None







    def       UpperCamelCase_				(      self	):
      lowercase							   =	ViTImageProcessingTester(self	)







    @property
    def       UpperCamelCase_				(      self	):
      return self.image_processor_tester.prepare_image_processor_dict()







    def       UpperCamelCase_				(      self	):
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'image_mean'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'image_std'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'do_normalize'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'do_resize'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'size'	)	)







    def       UpperCamelCase_				(      self	):
      lowercase							   =	self.image_processing_class.from_dict(self.image_processor_dict	)
      self.assertEqual(image_processor.size							,		{'height': 1_8, 'width': 1_8}	)

      lowercase							   =	self.image_processing_class.from_dict(self.image_processor_dict							,		size=4_2	)
      self.assertEqual(image_processor.size							,		{'height': 4_2, 'width': 4_2}	)







    def       UpperCamelCase_				(      self	):
      pass







    def       UpperCamelCase_				(      self	):
      # Initialize image_processing
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      # create random PIL images
      lowercase							   =	prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase	)
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		Image.Image	)

      # Test not batched input
      lowercase							   =	image_processing(image_inputs[0]							,		return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_images.shape							,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							   =	image_processing(_lowerCamelCase							,		return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_images.shape							,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)







    def       UpperCamelCase_				(      self	):
      # Initialize image_processing
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      lowercase							   =	prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase							,		numpify=_lowerCamelCase	)
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		np.ndarray	)

      # Test not batched input
      lowercase							   =	image_processing(image_inputs[0]							,		return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_images.shape							,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							   =	image_processing(_lowerCamelCase							,		return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_images.shape							,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)







    def       UpperCamelCase_				(      self	):
      # Initialize image_processing
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      lowercase							   =	prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase							,		torchify=_lowerCamelCase	)
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		torch.Tensor	)

      # Test not batched input
      lowercase							   =	image_processing(image_inputs[0]							,		return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_images.shape							,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							   =	image_processing(_lowerCamelCase							,		return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_images.shape							,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)


