





import time
from dataclasses import dataclass
from multiprocessing import Pool
from unittest import TestCase
from unittest.mock import patch

import multiprocess
import numpy as np
import pytest

from datasets.utils.py_utils import (
    NestedDataStructure,
    asdict,
    iflatmap_unordered,
    map_nested,
    temp_seed,
    temporary_assignment,
    zip_dict,
)

from .utils import require_tf, require_torch





def lowerCAmelCase__(__snake_case							)       ->   Optional[Any]:  # picklable for multiprocessing







	'''simple docstring'''







	return x.sum()





def lowerCAmelCase__(__snake_case							)       ->   List[Any]:  # picklable for multiprocessing







	'''simple docstring'''







	return i + 1






@dataclass
class       __A					:

			'''simple docstring'''







			lowerCAmelCase_							  =		42
			lowerCAmelCase_							  =		42






class       __A					(					_UpperCAmelCase				):

			'''simple docstring'''







			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	{}
				lowerCamelCase__              =	[]
				lowerCamelCase__              =	1
				lowerCamelCase__              =	[1, 2]
				lowerCamelCase__              =	{'''a''': 1, '''b''': 2}
				lowerCamelCase__              =	{'''a''': [1, 2], '''b''': [3, 4]}
				lowerCamelCase__              =	{'''a''': {'''1''': 1}, '''b''': 2}
				lowerCamelCase__              =	{'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4}
				lowerCamelCase__              =	{}
				lowerCamelCase__              =	[]
				lowerCamelCase__              =	2
				lowerCamelCase__              =	[2, 3]
				lowerCamelCase__              =	{'''a''': 2, '''b''': 3}
				lowerCamelCase__              =	{'''a''': [2, 3], '''b''': [4, 5]}
				lowerCamelCase__              =	{'''a''': {'''1''': 2}, '''b''': 3}
				lowerCamelCase__              =	{'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5}
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_							) ,						lowerCamelCase_							)

				lowerCamelCase__              =	2
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)

				lowerCamelCase__              =	{'''a''': np.eye(2							), '''b''': np.zeros(3							), '''c''': np.ones(2							)}
				lowerCamelCase__              =	{'''a''': 2, '''b''': 0, '''c''': 2}
				lowerCamelCase__              =	{
				    '''a''': np.eye(2							).astype(lowerCamelCase_							),
				    '''b''': np.zeros(3							).astype(lowerCamelCase_							),
				    '''c''': np.ones(2							).astype(lowerCamelCase_							),
				}
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						map_numpy=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(
				    {k: v.tolist() for k, v in map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						map_numpy=lowerCamelCase_							).items()} ,						{k: v.tolist() for k, v in expected_map_nested_sna_int.items()} ,						)
				self.assertEqual(map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						map_numpy=lowerCamelCase_ ,						num_proc=lowerCamelCase_							) ,						lowerCamelCase_							)
				self.assertEqual(
				    {k: v.tolist() for k, v in map_nested(lowerCamelCase_ ,						lowerCamelCase_ ,						map_numpy=lowerCamelCase_ ,						num_proc=lowerCamelCase_							).items()} ,						{k: v.tolist() for k, v in expected_map_nested_sna_int.items()} ,						)
				with self.assertRaises(lowerCamelCase_							):  # can't pickle a local lambda
					map_nested(lambda __lowerCAmelCase							: x + 1 ,						lowerCamelCase_ ,						num_proc=lowerCamelCase_							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	{'''a''': 1, '''b''': 2}
				lowerCamelCase__              =	{'''a''': 3, '''b''': 4}
				lowerCamelCase__              =	{'''a''': 5, '''b''': 6}
				lowerCamelCase__              =	sorted([('''a''', (1, 3, 5)), ('''b''', (2, 4, 6))]							)
				self.assertEqual(sorted(zip_dict(lowerCamelCase_ ,						lowerCamelCase_ ,						lowerCamelCase_							)							) ,						lowerCamelCase_							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				class       __A					:

							'''simple docstring'''







							lowerCAmelCase_							  =		"""bar"""



				lowerCamelCase__              =	Foo()
				self.assertEqual(foo.my_attr ,						'''bar'''							)
				with temporary_assignment(lowerCamelCase_ ,						'''my_attr''' ,						'''BAR'''							):
					self.assertEqual(foo.my_attr ,						'''BAR'''							)
				self.assertEqual(foo.my_attr ,						'''bar'''							)





@pytest.mark.parametrize(
    '''iterable_length, num_proc, expected_num_proc''' ,[
        (1, None, 1),
        (1, 1, 1),
        (2, None, 1),
        (2, 1, 1),
        (2, 2, 1),
        (2, 3, 1),
        (3, 2, 1),
        (16, 16, 16),
        (16, 17, 16),
        (17, 16, 16),
    ] ,)
def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case							)       ->   str:







	'''simple docstring'''







	with patch('''datasets.utils.py_utils._single_map_nested'''							) as mock_single_map_nested, patch(
	    '''datasets.parallel.parallel.Pool'''							) as mock_multiprocessing_pool:
		lowerCamelCase__              =	{F'{i}': i for i in range(a_							)}
		lowerCamelCase__              =	map_nested(lambda __snake_case							: x + 10 ,a_ ,num_proc=a_ ,parallel_min_length=16							)
		if expected_num_proc == 1:
			assert mock_single_map_nested.called
			assert not mock_multiprocessing_pool.called
		else:
			assert not mock_single_map_nested.called
			assert mock_multiprocessing_pool.called
			assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc






class       __A					(					_UpperCAmelCase				):

			'''simple docstring'''







			@require_tf
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				import tensorflow as tf
				from tensorflow.keras import layers

				lowerCamelCase__              =	layers.Dense(2							)

				def gen_random_output():
					lowerCamelCase__              =	tf.random.uniform((1, 3)							)
					return model(lowerCamelCase_							).numpy()

				with temp_seed(4_2 ,						set_tensorflow=lowerCamelCase_							):
					lowerCamelCase__              =	gen_random_output()
				with temp_seed(4_2 ,						set_tensorflow=lowerCamelCase_							):
					lowerCamelCase__              =	gen_random_output()
				lowerCamelCase__              =	gen_random_output()

				np.testing.assert_equal(lowerCamelCase_ ,						lowerCamelCase_							)
				self.assertGreater(np.abs(outa - outa							).sum() ,						0							)





			@require_torch
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				import torch

				def gen_random_output():
					lowerCamelCase__              =	torch.nn.Linear(3 ,						2							)
					lowerCamelCase__              =	torch.rand(1 ,						3							)
					return model(lowerCamelCase_							).detach().numpy()

				with temp_seed(4_2 ,						set_pytorch=lowerCamelCase_							):
					lowerCamelCase__              =	gen_random_output()
				with temp_seed(4_2 ,						set_pytorch=lowerCamelCase_							):
					lowerCamelCase__              =	gen_random_output()
				lowerCamelCase__              =	gen_random_output()

				np.testing.assert_equal(lowerCamelCase_ ,						lowerCamelCase_							)
				self.assertGreater(np.abs(outa - outa							).sum() ,						0							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				def gen_random_output():
					return np.random.rand(1 ,						3							)

				with temp_seed(4_2							):
					lowerCamelCase__              =	gen_random_output()
				with temp_seed(4_2							):
					lowerCamelCase__              =	gen_random_output()
				lowerCamelCase__              =	gen_random_output()

				np.testing.assert_equal(lowerCamelCase_ ,						lowerCamelCase_							)
				self.assertGreater(np.abs(outa - outa							).sum() ,						0							)





@pytest.mark.parametrize('''input_data''' ,[{}]							)
def lowerCAmelCase__(__snake_case							)       ->   Optional[int]:







	'''simple docstring'''







	lowerCamelCase__              =	NestedDataStructure(a_							).data
	assert output_data == input_data





@pytest.mark.parametrize(
    '''data, expected_output''' ,[
        ({}, []),
        ([], []),
        ('''foo''', ['''foo''']),
        (['''foo''', '''bar'''], ['''foo''', '''bar''']),
        ([['''foo''', '''bar''']], ['''foo''', '''bar''']),
        ([[['''foo'''], ['''bar''']]], ['''foo''', '''bar''']),
        ([[['''foo'''], '''bar''']], ['''foo''', '''bar''']),
        ({'''a''': 1, '''b''': 2}, [1, 2]),
        ({'''a''': [1, 2], '''b''': [3, 4]}, [1, 2, 3, 4]),
        ({'''a''': [[1, 2]], '''b''': [[3, 4]]}, [1, 2, 3, 4]),
        ({'''a''': [[1, 2]], '''b''': [3, 4]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [[[3], [4]]]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [[3, 4]]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [3, 4]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [3, [4]]}, [1, 2, 3, 4]),
        ({'''a''': {'''1''': 1}, '''b''': 2}, [1, 2]),
        ({'''a''': {'''1''': [1]}, '''b''': 2}, [1, 2]),
        ({'''a''': {'''1''': [1]}, '''b''': [2]}, [1, 2]),
    ] ,)
def lowerCAmelCase__(__snake_case ,__snake_case							)       ->   Optional[Any]:







	'''simple docstring'''







	lowerCamelCase__              =	NestedDataStructure(a_							).flatten()
	assert output == expected_output





def lowerCAmelCase__()       ->   Any:







	'''simple docstring'''







	lowerCamelCase__              =	A(x=1 ,y='''foobar'''							)
	lowerCamelCase__              =	{'''x''': 1, '''y''': '''foobar'''}
	assert asdict(a_							) == expected_output

	lowerCamelCase__              =	{'''a''': {'''b''': A(x=10 ,y='''foo'''							)}, '''c''': [A(x=20 ,y='''bar'''							)]}
	lowerCamelCase__              =	{'''a''': {'''b''': {'''x''': 10, '''y''': '''foo'''}}, '''c''': [{'''x''': 20, '''y''': '''bar'''}]}
	assert asdict(a_							) == expected_output

	with pytest.raises(a_							):
		asdict([1, A(x=10 ,y='''foo'''							)]							)





def lowerCAmelCase__(__snake_case							)       ->   List[str]:







	'''simple docstring'''







	return text.split()





def lowerCAmelCase__(__snake_case							)       ->   Union[str, Any]:







	'''simple docstring'''







	yield (time.time(), content)
	time.sleep(2							)
	yield (time.time(), content)





def lowerCAmelCase__()       ->   Dict:







	'''simple docstring'''







	with Pool(2							) as pool:
		lowerCamelCase__              =	list(iflatmap_unordered(a_ ,_split_text ,kwargs_iterable=[{'''text''': '''hello there'''}] * 10							)							)
		assert out.count('''hello'''							) == 10
		assert out.count('''there'''							) == 10
		assert len(a_							) == 20

	# check multiprocess from pathos (uses dill for pickling)
	with multiprocess.Pool(2							) as pool:
		lowerCamelCase__              =	list(iflatmap_unordered(a_ ,_split_text ,kwargs_iterable=[{'''text''': '''hello there'''}] * 10							)							)
		assert out.count('''hello'''							) == 10
		assert out.count('''there'''							) == 10
		assert len(a_							) == 20

	# check that we get items as fast as possible
	with Pool(2							) as pool:
		lowerCamelCase__              =	[]
		for yield_time, content in iflatmap_unordered(
		    a_ ,_aseconds_generator_of_aitems_with_timing ,kwargs_iterable=[{'''content''': '''a'''}, {'''content''': '''b'''}]							):
			assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded"
			out.append(a_							)
		assert out.count('''a'''							) == 2
		assert out.count('''b'''							) == 2
		assert len(a_							) == 4










import warnings

from ...utils import logging
from .image_processing_owlvit import OwlViTImageProcessor


_a						=   logging.get_logger(__name__)






class       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			def __init__(					self ,						*__lowerCAmelCase ,						**__lowerCAmelCase							):



				'''simple docstring'''







				warnings.warn(
				    '''The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'''
				    ''' use OwlViTImageProcessor instead.''' ,						__lowerCAmelCase ,						)
				super().__init__(*__lowerCAmelCase ,						**__lowerCAmelCase							)



