


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  						=    {
    '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     UpperCamelCase_(			_A				:int					)->						Tuple:
			UpperCamelCase__   =    task_cls()
			UpperCamelCase__   =    asdict(_A					)
			UpperCamelCase__   =    task_template_from_dict(_A					)
			assert task == reloaded







class     lowerCamelCase__		:






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    sorted(["pos", "neg"]       )




			def 			snake_case__			(							self       ):

						'''simple docstring'''


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




			def 			snake_case__			(							self       ):

						'''simple docstring'''


						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(snake_case       )
						self.assertEqual("text-classification" ,  task.task       )
						self.assertEqual(snake_case ,  task.input_schema       )
						self.assertEqual(snake_case ,  task.label_schema       )







			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						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 			snake_case__			(							self       ):

						'''simple docstring'''


						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(snake_case       )
						self.assertEqual("question-answering-extractive" ,  task.task       )
						self.assertEqual(snake_case ,  task.input_schema       )
						self.assertEqual(snake_case ,  task.label_schema       )







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    Features({"audio": Audio()}       )
						UpperCamelCase__   =    Features({"transcription": Value("string"       )}       )
						UpperCamelCase__   =    {
						    "audio_column": "input_audio",
						    "transcription_column": "input_transcription",
						}
						UpperCamelCase__   =    AutomaticSpeechRecognition.from_dict(snake_case       )
						self.assertEqual("automatic-speech-recognition" ,  task.task       )
						self.assertEqual(snake_case ,  task.input_schema       )
						self.assertEqual(snake_case ,  task.label_schema       )







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    sorted(["pos", "neg"]       )




			def 			snake_case__			(							self       ):

						'''simple docstring'''


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




			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    sorted(["pos", "neg"]       )




			def 			snake_case__			(							self       ):

						'''simple docstring'''


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




			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    DatasetInfo(task_templates=QuestionAnsweringExtractive()       )
						UpperCamelCase__   =    Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()} ,  info=snake_case       )
						assert isinstance(dataset.info.task_templates ,  snake_case       )
						assert len(dataset.info.task_templates       ) == 1

						def keep_task(snake_case       ):
									return x

						def dont_keep_task(snake_case       ):
									UpperCamelCase__   =    deepcopy(snake_case       )
									UpperCamelCase__   =    0
									return out

						UpperCamelCase__   =    dataset.map(snake_case       )
						assert mapped_dataset.info.task_templates == dataset.info.task_templates
						# reload from cache
						UpperCamelCase__   =    dataset.map(snake_case       )
						assert mapped_dataset.info.task_templates == dataset.info.task_templates

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







			def 			snake_case__			(							self       ):

						'''simple docstring'''


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

						def process(snake_case       ):
									return example

						UpperCamelCase__   =    dataset.remove_columns("label"       )
						UpperCamelCase__   =    modified_dataset.map(snake_case       )
						assert mapped_dataset.info.task_templates == []






import numpy as np
def     UpperCamelCase_(			_A				:Optional[int]			,       _A				:Dict="utf-8"			,       _A				:Optional[Any]="surrogatepass"					)->						int:
			UpperCamelCase__   =    bin(int.from_bytes(text.encode(_A			,       _A					)			,       "big"					)					)[2:]
			return bits.zfill(8 * ((len(_A					) + 7) // 8)					)
def     UpperCamelCase_(			_A				:Optional[int]			,       _A				:int="utf-8"			,       _A				:List[str]="surrogatepass"					)->						Optional[Any]:
			UpperCamelCase__   =    int(_A			,       2					)
			return n.to_bytes((n.bit_length() + 7) // 8			,       "big"					).decode(_A			,       _A					) or "\0"
def     UpperCamelCase_(			_A				:Union[str, Any]			,       _A				:Tuple					)->						Optional[int]:
			if size_par + len(_A					) <= 2**size_par - (len(_A					) - 1):
						raise ValueError("size of parity don't match with size of data"					)

			UpperCamelCase__   =    []
			UpperCamelCase__   =    []
			UpperCamelCase__   =    [bin(_A					)[2:] for x in range(1			,       size_par + len(_A					) + 1					)]

			# sorted information data for the size of the output data
			UpperCamelCase__   =    []
			# data position template + parity
			UpperCamelCase__   =    []
			# parity bit counter
			UpperCamelCase__   =    0
			# counter position of data bits
			UpperCamelCase__   =    0

			for x in range(1			,       size_par + len(_A					) + 1					):
						# Performs a template of bit positions - who should be given,
						# and who should be parity
						if qtd_bp < size_par:
									if (np.log(_A					) / np.log(2					)).is_integer():
												data_out_gab.append("P"					)
												UpperCamelCase__   =    qtd_bp + 1
									else:
												data_out_gab.append("D"					)
						else:
									data_out_gab.append("D"					)

						# Sorts the data to the new output size
						if data_out_gab[-1] == "D":
									data_ord.append(data[cont_data]					)
									cont_data += 1
						else:
									data_ord.append(_A					)

    # Calculates parity
			UpperCamelCase__   =    0  # parity bit counter
			for bp in range(1			,       size_par + 1					):
						# Bit counter one for a given parity
						UpperCamelCase__   =    0
						# counter to control the loop reading
						UpperCamelCase__   =    0
						for x in data_ord:
									if x is not None:
												try:
															UpperCamelCase__   =    (bin_pos[cont_loop])[-1 * (bp)]
												except IndexError:
															UpperCamelCase__   =    "0"
												if aux == "1" and x == "1":
															cont_bo += 1
									cont_loop += 1
						parity.append(cont_bo % 2					)

						qtd_bp += 1

			# Mount the message
			UpperCamelCase__   =    0  # parity bit counter
			for x in range(0			,       size_par + len(_A					)					):
						if data_ord[x] is None:
									data_out.append(str(parity[cont_bp]					)					)
									cont_bp += 1
						else:
									data_out.append(data_ord[x]					)

			return data_out
def     UpperCamelCase_(			_A				:int			,       _A				:str					)->						Dict:
			UpperCamelCase__   =    []
			# Parity bit counter
			UpperCamelCase__   =    0
			# Counter p data bit reading
			UpperCamelCase__   =    0
			# list of parity received
			UpperCamelCase__   =    []
			UpperCamelCase__   =    []

			for x in range(1			,       len(_A					) + 1					):
						# Performs a template of bit positions - who should be given,
						#  and who should be parity
						if qtd_bp < size_par and (np.log(_A					) / np.log(2					)).is_integer():
									data_out_gab.append("P"					)
									UpperCamelCase__   =    qtd_bp + 1
						else:
									data_out_gab.append("D"					)

						# Sorts the data to the new output size
						if data_out_gab[-1] == "D":
									data_output.append(data[cont_data]					)
						else:
									parity_received.append(data[cont_data]					)
						cont_data += 1

			# -----------calculates the parity with the data
			UpperCamelCase__   =    []
			UpperCamelCase__   =    []
			UpperCamelCase__   =    [bin(_A					)[2:] for x in range(1			,       size_par + len(_A					) + 1					)]

			#  sorted information data for the size of the output data
			UpperCamelCase__   =    []
			# Data position feedback + parity
			UpperCamelCase__   =    []
			# Parity bit counter
			UpperCamelCase__   =    0
			# Counter p data bit reading
			UpperCamelCase__   =    0

			for x in range(1			,       size_par + len(_A					) + 1					):
						# Performs a template position of bits - who should be given,
						# and who should be parity
						if qtd_bp < size_par and (np.log(_A					) / np.log(2					)).is_integer():
									data_out_gab.append("P"					)
									UpperCamelCase__   =    qtd_bp + 1
						else:
									data_out_gab.append("D"					)

						# Sorts the data to the new output size
						if data_out_gab[-1] == "D":
									data_ord.append(data_output[cont_data]					)
									cont_data += 1
						else:
									data_ord.append(_A					)

    # Calculates parity
			UpperCamelCase__   =    0  # parity bit counter
			for bp in range(1			,       size_par + 1					):
						# Bit counter one for a certain parity
						UpperCamelCase__   =    0
						# Counter to control loop reading
						UpperCamelCase__   =    0
						for x in data_ord:
									if x is not None:
												try:
															UpperCamelCase__   =    (bin_pos[cont_loop])[-1 * (bp)]
												except IndexError:
															UpperCamelCase__   =    "0"
												if aux == "1" and x == "1":
															cont_bo += 1
									cont_loop += 1
						parity.append(str(cont_bo % 2					)					)

						qtd_bp += 1

			# Mount the message
			UpperCamelCase__   =    0  # Parity bit counter
			for x in range(0			,       size_par + len(_A					)					):
						if data_ord[x] is None:
									data_out.append(str(parity[cont_bp]					)					)
									cont_bp += 1
						else:
									data_out.append(data_ord[x]					)

			UpperCamelCase__   =    parity_received == parity
			return data_output, ack


