


"""simple docstring"""


import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows


def _lowerCamelCase      (						_UpperCamelCase ):  # picklable for multiprocessing

			'''simple docstring'''




			return i + 1


@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def _lowerCamelCase      (						):

			'''simple docstring'''




			with parallel_backend("spark" ):
						assert ParallelBackendConfig.backend_name == "spark"

			__lowerCAmelCase							       =     [1, 2, 3]
			with pytest.raises(_UpperCamelCase ):
						with parallel_backend("unsupported backend" ):
									map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=2 )

			with pytest.raises(_UpperCamelCase ):
						with parallel_backend("unsupported backend" ):
									map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=-1 )


@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("num_proc"       ,					[2, -1] )
def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			__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							       =     [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}

			with parallel_backend("spark" ):
						assert map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=_UpperCamelCase ) == expected_map_nested_sa
						assert map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=_UpperCamelCase ) == expected_map_nested_sa
						assert map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=_UpperCamelCase ) == expected_map_nested_sa
						assert map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=_UpperCamelCase ) == expected_map_nested_sa
						assert map_nested(_UpperCamelCase       ,					_UpperCamelCase       ,					num_proc=_UpperCamelCase ) == expected_map_nested_sa








"""simple docstring"""


from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			return math.sqrt(sum(pow(a - b       ,					2 ) for a, b in zip(_UpperCamelCase       ,					_UpperCamelCase ) ) )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			if dataset.ndim != value_array.ndim:
						__lowerCAmelCase							       =     (
						    "Wrong input data's dimensions... "
						    f"dataset : {dataset.ndim}, value_array : {value_array.ndim}"
						)
						raise ValueError(_UpperCamelCase )

			try:
						if dataset.shape[1] != value_array.shape[1]:
									__lowerCAmelCase							       =     (
									    "Wrong input data's shape... "
									    f"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}"
									)
									raise ValueError(_UpperCamelCase )
			except IndexError:
						if dataset.ndim != value_array.ndim:
									raise TypeError("Wrong shape" )

			if dataset.dtype != value_array.dtype:
						__lowerCAmelCase							       =     (
						    "Input data have different datatype... "
						    f"dataset : {dataset.dtype}, value_array : {value_array.dtype}"
						)
						raise TypeError(_UpperCamelCase )

			__lowerCAmelCase							       =     []

			for value in value_array:
						__lowerCAmelCase							       =     euclidean(_UpperCamelCase       ,					dataset[0] )
						__lowerCAmelCase							       =     dataset[0].tolist()

						for dataset_value in dataset[1:]:
									__lowerCAmelCase							       =     euclidean(_UpperCamelCase       ,					_UpperCamelCase )

									if dist > temp_dist:
												__lowerCAmelCase							       =     temp_dist
												__lowerCAmelCase							       =     dataset_value.tolist()

						answer.append([vector, dist] )

			return answer


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			return np.dot(_UpperCamelCase       ,					_UpperCamelCase ) / (norm(_UpperCamelCase ) * norm(_UpperCamelCase ))


if __name__ == "__main__":
	import doctest

	doctest.testmod()




