





import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
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 DeformableDetrImageProcessor



class 					A__   (       unittest.TestCase			):





      def __init__( self       ,							A_       ,							A_=7       ,							A_=3       ,							A_=30       ,							A_=400       ,							A_=True       ,							A_=None       ,							A_=True       ,							A_=[0.5, 0.5, 0.5]       ,							A_=[0.5, 0.5, 0.5]       ,							A_=True       ,							A_=1 / 255       ,							A_=True       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   size if size is not None else {"shortest_edge": 18, "longest_edge": 1333}
       UpperCamelCase	:					List[str]       	=   parent
       UpperCamelCase	:					Dict       	=   batch_size
       UpperCamelCase	:					str       	=   num_channels
       UpperCamelCase	:					Optional[Any]       	=   min_resolution
       UpperCamelCase	:					Dict       	=   max_resolution
       UpperCamelCase	:					int       	=   do_resize
       UpperCamelCase	:					Any       	=   size
       UpperCamelCase	:					Tuple       	=   do_normalize
       UpperCamelCase	:					Optional[int]       	=   image_mean
       UpperCamelCase	:					Union[str, Any]       	=   image_std
       UpperCamelCase	:					Optional[int]       	=   do_rescale
       UpperCamelCase	:					str       	=   rescale_factor
       UpperCamelCase	:					int       	=   do_pad





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_rescale": self.do_rescale,
           "rescale_factor": self.rescale_factor,
           "do_pad": self.do_pad,
       }





      def        __UpperCamelCase( self       ,							A_       ,							A_=False   ):
       '''simple docstring'''




       if not batched:
        UpperCamelCase	:					Optional[int]       	=   image_inputs[0]
        if isinstance(A_       ,							Image.Image   ):
         UpperCamelCase	,    UpperCamelCase	:					List[str]       	=   image.size
        else:
         UpperCamelCase	,    UpperCamelCase	:					Tuple       	=   image.shape[1], image.shape[2]
        if w < h:
         UpperCamelCase	:					Dict       	=   int(self.size["shortest_edge"] * h / w   )
         UpperCamelCase	:					Dict       	=   self.size["shortest_edge"]
        elif w > h:
         UpperCamelCase	:					Optional[int]       	=   self.size["shortest_edge"]
         UpperCamelCase	:					Union[str, Any]       	=   int(self.size["shortest_edge"] * w / h   )
        else:
         UpperCamelCase	:					List[str]       	=   self.size["shortest_edge"]
         UpperCamelCase	:					Optional[int]       	=   self.size["shortest_edge"]

       else:
        UpperCamelCase	:					List[Any]       	=   []
        for image in image_inputs:
         UpperCamelCase	,    UpperCamelCase	:					int       	=   self.get_expected_values([image]   )
         expected_values.append((expected_height, expected_width)   )
        UpperCamelCase	:					List[Any]       	=   max(A_       ,							key=lambda A_   : item[0]   )[0]
        UpperCamelCase	:					int       	=   max(A_       ,							key=lambda A_   : item[1]   )[1]

       return expected_height, expected_width



@require_torch
@require_vision
class 					A__   (       __snake_case       ,   unittest.TestCase			):
      _UpperCAmelCase	:int						    =   DeformableDetrImageProcessor if is_vision_available() else None





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   DeformableDetrImageProcessingTester(self   )





      @property
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       return self.image_processor_tester.prepare_image_processor_dict()





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   self.image_processing_class(**self.image_processor_dict   )
       self.assertTrue(hasattr(A_       ,							"image_mean"   )   )
       self.assertTrue(hasattr(A_       ,							"image_std"   )   )
       self.assertTrue(hasattr(A_       ,							"do_normalize"   )   )
       self.assertTrue(hasattr(A_       ,							"do_resize"   )   )
       self.assertTrue(hasattr(A_       ,							"do_rescale"   )   )
       self.assertTrue(hasattr(A_       ,							"do_pad"   )   )
       self.assertTrue(hasattr(A_       ,							"size"   )   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					str       	=   self.image_processing_class.from_dict(self.image_processor_dict   )
       self.assertEqual(image_processor.size       ,							{"shortest_edge": 18, "longest_edge": 1333}   )
       self.assertEqual(image_processor.do_pad       ,							A_   )

       UpperCamelCase	:					str       	=   self.image_processing_class.from_dict(
           self.image_processor_dict       ,							size=42       ,							max_size=84       ,							pad_and_return_pixel_mask=A_   )
       self.assertEqual(image_processor.size       ,							{"shortest_edge": 42, "longest_edge": 84}   )
       self.assertEqual(image_processor.do_pad       ,							A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       pass





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   self.image_processing_class(**self.image_processor_dict   )
       # create random PIL images
       UpperCamelCase	:					Any       	=   prepare_image_inputs(self.image_processor_tester       ,							equal_resolution=A_   )
       for image in image_inputs:
        self.assertIsInstance(A_       ,							Image.Image   )

       # Test not batched input
       UpperCamelCase	:					Dict       	=   image_processing(image_inputs[0]       ,							return_tensors="pt"   ).pixel_values

       UpperCamelCase	,    UpperCamelCase	:					Optional[Any]       	=   self.image_processor_tester.get_expected_values(A_   )

       self.assertEqual(
           encoded_images.shape       ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,							)

       # Test batched
       UpperCamelCase	,    UpperCamelCase	:					int       	=   self.image_processor_tester.get_expected_values(A_       ,							batched=A_   )

       UpperCamelCase	:					Any       	=   image_processing(A_       ,							return_tensors="pt"   ).pixel_values
       self.assertEqual(
           encoded_images.shape       ,							(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )       ,							)





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   self.image_processing_class(**self.image_processor_dict   )
       # create random numpy tensors
       UpperCamelCase	:					Optional[Any]       	=   prepare_image_inputs(self.image_processor_tester       ,							equal_resolution=A_       ,							numpify=A_   )
       for image in image_inputs:
        self.assertIsInstance(A_       ,							np.ndarray   )

       # Test not batched input
       UpperCamelCase	:					Dict       	=   image_processing(image_inputs[0]       ,							return_tensors="pt"   ).pixel_values

       UpperCamelCase	,    UpperCamelCase	:					int       	=   self.image_processor_tester.get_expected_values(A_   )

       self.assertEqual(
           encoded_images.shape       ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,							)

       # Test batched
       UpperCamelCase	:					Optional[int]       	=   image_processing(A_       ,							return_tensors="pt"   ).pixel_values

       UpperCamelCase	,    UpperCamelCase	:					Optional[Any]       	=   self.image_processor_tester.get_expected_values(A_       ,							batched=A_   )

       self.assertEqual(
           encoded_images.shape       ,							(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )       ,							)





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[Any]       	=   self.image_processing_class(**self.image_processor_dict   )
       # create random PyTorch tensors
       UpperCamelCase	:					List[Any]       	=   prepare_image_inputs(self.image_processor_tester       ,							equal_resolution=A_       ,							torchify=A_   )
       for image in image_inputs:
        self.assertIsInstance(A_       ,							torch.Tensor   )

       # Test not batched input
       UpperCamelCase	:					Optional[int]       	=   image_processing(image_inputs[0]       ,							return_tensors="pt"   ).pixel_values

       UpperCamelCase	,    UpperCamelCase	:					Optional[int]       	=   self.image_processor_tester.get_expected_values(A_   )

       self.assertEqual(
           encoded_images.shape       ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,							)

       # Test batched
       UpperCamelCase	:					Union[str, Any]       	=   image_processing(A_       ,							return_tensors="pt"   ).pixel_values

       UpperCamelCase	,    UpperCamelCase	:					Dict       	=   self.image_processor_tester.get_expected_values(A_       ,							batched=A_   )

       self.assertEqual(
           encoded_images.shape       ,							(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )       ,							)





      @slow
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[Any]       	=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
       with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt"       ,							"r"   ) as f:
        UpperCamelCase	:					str       	=   json.loads(f.read()   )

       UpperCamelCase	:					Dict       	=   {"image_id": 3_9769, "annotations": target}

       # encode them
       UpperCamelCase	:					str       	=   DeformableDetrImageProcessor()
       UpperCamelCase	:					Optional[Any]       	=   image_processing(images=A_       ,							annotations=A_       ,							return_tensors="pt"   )

       # verify pixel values
       UpperCamelCase	:					Optional[int]       	=   torch.Size([1, 3, 800, 1066]   )
       self.assertEqual(encoding["pixel_values"].shape       ,							A_   )

       UpperCamelCase	:					Any       	=   torch.tensor([0.27_96, 0.31_38, 0.34_81]   )
       self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]       ,							A_       ,							atol=1e-4   )   )

       # verify area
       UpperCamelCase	:					Union[str, Any]       	=   torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["area"]       ,							A_   )   )
       # verify boxes
       UpperCamelCase	:					List[str]       	=   torch.Size([6, 4]   )
       self.assertEqual(encoding["labels"][0]["boxes"].shape       ,							A_   )
       UpperCamelCase	:					List[Any]       	=   torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]       ,							A_       ,							atol=1e-3   )   )
       # verify image_id
       UpperCamelCase	:					Optional[Any]       	=   torch.tensor([3_9769]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]       ,							A_   )   )
       # verify is_crowd
       UpperCamelCase	:					str       	=   torch.tensor([0, 0, 0, 0, 0, 0]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]       ,							A_   )   )
       # verify class_labels
       UpperCamelCase	:					str       	=   torch.tensor([75, 75, 63, 65, 17, 17]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]       ,							A_   )   )
       # verify orig_size
       UpperCamelCase	:					Dict       	=   torch.tensor([480, 640]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]       ,							A_   )   )
       # verify size
       UpperCamelCase	:					Any       	=   torch.tensor([800, 1066]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["size"]       ,							A_   )   )





      @slow
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
       with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"       ,							"r"   ) as f:
        UpperCamelCase	:					Optional[int]       	=   json.loads(f.read()   )

       UpperCamelCase	:					Any       	=   {"file_name": "000000039769.png", "image_id": 3_9769, "segments_info": target}

       UpperCamelCase	:					Tuple       	=   pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"   )

       # encode them
       UpperCamelCase	:					List[Any]       	=   DeformableDetrImageProcessor(format="coco_panoptic"   )
       UpperCamelCase	:					Dict       	=   image_processing(images=A_       ,							annotations=A_       ,							masks_path=A_       ,							return_tensors="pt"   )

       # verify pixel values
       UpperCamelCase	:					Any       	=   torch.Size([1, 3, 800, 1066]   )
       self.assertEqual(encoding["pixel_values"].shape       ,							A_   )

       UpperCamelCase	:					List[Any]       	=   torch.tensor([0.27_96, 0.31_38, 0.34_81]   )
       self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]       ,							A_       ,							atol=1e-4   )   )

       # verify area
       UpperCamelCase	:					Union[str, Any]       	=   torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["area"]       ,							A_   )   )
       # verify boxes
       UpperCamelCase	:					Dict       	=   torch.Size([6, 4]   )
       self.assertEqual(encoding["labels"][0]["boxes"].shape       ,							A_   )
       UpperCamelCase	:					Any       	=   torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]       ,							A_       ,							atol=1e-3   )   )
       # verify image_id
       UpperCamelCase	:					Union[str, Any]       	=   torch.tensor([3_9769]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]       ,							A_   )   )
       # verify is_crowd
       UpperCamelCase	:					Dict       	=   torch.tensor([0, 0, 0, 0, 0, 0]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]       ,							A_   )   )
       # verify class_labels
       UpperCamelCase	:					Any       	=   torch.tensor([17, 17, 63, 75, 75, 93]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]       ,							A_   )   )
       # verify masks
       UpperCamelCase	:					Dict       	=   82_2873
       self.assertEqual(encoding["labels"][0]["masks"].sum().item()       ,							A_   )
       # verify orig_size
       UpperCamelCase	:					List[Any]       	=   torch.tensor([480, 640]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]       ,							A_   )   )
       # verify size
       UpperCamelCase	:					List[Any]       	=   torch.tensor([800, 1066]   )
       self.assertTrue(torch.allclose(encoding["labels"][0]["size"]       ,							A_   )   )







import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


__lowerCamelCase       :     Union[str, Any]   =     pytest.mark.integration



@require_faiss
class 					A__   (       __snake_case			):





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Union[str, Any]       	=   Dataset.from_dict({"filename": ["my_name-train" + "_" + str(A_   ) for x in np.arange(30   ).tolist()]}   )
       return dset





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					Dataset       	=   self._create_dummy_dataset()
       UpperCamelCase	:					List[Any]       	=   dset.map(
           lambda A_       ,							A_   : {"vecs": i * np.ones(5       ,							dtype=np.floataa   )}       ,							with_indices=A_       ,							keep_in_memory=A_   )
       UpperCamelCase	:					List[str]       	=   dset.add_faiss_index("vecs"       ,							batch_size=100       ,							metric_type=faiss.METRIC_INNER_PRODUCT   )
       UpperCamelCase	,    UpperCamelCase	:					Tuple       	=   dset.get_nearest_examples("vecs"       ,							np.ones(5       ,							dtype=np.floataa   )   )
       self.assertEqual(examples["filename"][0]       ,							"my_name-train_29"   )
       dset.drop_index("vecs"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					Dataset       	=   self._create_dummy_dataset()
       dset.add_faiss_index_from_external_arrays(
           external_arrays=np.ones((30, 5)   ) * np.arange(30   ).reshape(-1       ,							1   )       ,							index_name="vecs"       ,							batch_size=100       ,							metric_type=faiss.METRIC_INNER_PRODUCT       ,							)
       UpperCamelCase	,    UpperCamelCase	:					int       	=   dset.get_nearest_examples("vecs"       ,							np.ones(5       ,							dtype=np.floataa   )   )
       self.assertEqual(examples["filename"][0]       ,							"my_name-train_29"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					Dataset       	=   self._create_dummy_dataset()
       dset.add_faiss_index_from_external_arrays(
           external_arrays=np.ones((30, 5)   ) * np.arange(30   ).reshape(-1       ,							1   )       ,							index_name="vecs"       ,							metric_type=faiss.METRIC_INNER_PRODUCT       ,							)

       # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
       # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
       # see https://bugs.python.org/issue14243 and
       # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
       with tempfile.NamedTemporaryFile(delete=A_   ) as tmp_file:
        dset.save_faiss_index("vecs"       ,							tmp_file.name   )
        dset.load_faiss_index("vecs2"       ,							tmp_file.name   )
       os.unlink(tmp_file.name   )

       UpperCamelCase	,    UpperCamelCase	:					List[str]       	=   dset.get_nearest_examples("vecs2"       ,							np.ones(5       ,							dtype=np.floataa   )   )
       self.assertEqual(examples["filename"][0]       ,							"my_name-train_29"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Dataset       	=   self._create_dummy_dataset()
       dset.add_faiss_index_from_external_arrays(
           external_arrays=np.ones((30, 5)   ) * np.arange(30   ).reshape(-1       ,							1   )       ,							index_name="vecs"   )
       dset.drop_index("vecs"   )
       self.assertRaises(A_       ,							partial(dset.get_nearest_examples       ,							"vecs2"       ,							np.ones(5       ,							dtype=np.floataa   )   )   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       from elasticsearch import Elasticsearch

       UpperCamelCase	:					Dataset       	=   self._create_dummy_dataset()
       with patch("elasticsearch.Elasticsearch.search"   ) as mocked_search, patch(
           "elasticsearch.client.IndicesClient.create"   ) as mocked_index_create, patch("elasticsearch.helpers.streaming_bulk"   ) as mocked_bulk:
        UpperCamelCase	:					List[str]       	=   {"acknowledged": True}
        mocked_bulk.return_value([(True, None)] * 30   )
        UpperCamelCase	:					List[Any]       	=   {"hits": {"hits": [{"_score": 1, "_id": 29}]}}
        UpperCamelCase	:					Optional[Any]       	=   Elasticsearch()

        dset.add_elasticsearch_index("filename"       ,							es_client=A_   )
        UpperCamelCase	,    UpperCamelCase	:					List[str]       	=   dset.get_nearest_examples("filename"       ,							"my_name-train_29"   )
        self.assertEqual(examples["filename"][0]       ,							"my_name-train_29"   )



@require_faiss
class 					A__   (       __snake_case			):





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					Optional[int]       	=   FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT   )

       # add vectors
       index.add_vectors(np.eye(5       ,							dtype=np.floataa   )   )
       self.assertIsNotNone(index.faiss_index   )
       self.assertEqual(index.faiss_index.ntotal       ,							5   )
       index.add_vectors(np.zeros((5, 5)       ,							dtype=np.floataa   )   )
       self.assertEqual(index.faiss_index.ntotal       ,							10   )

       # single query
       UpperCamelCase	:					Any       	=   np.zeros(5       ,							dtype=np.floataa   )
       UpperCamelCase	:					Optional[Any]       	=   1
       UpperCamelCase	,    UpperCamelCase	:					Optional[Any]       	=   index.search(A_   )
       self.assertRaises(A_       ,							index.search       ,							query.reshape(-1       ,							1   )   )
       self.assertGreater(scores[0]       ,							0   )
       self.assertEqual(indices[0]       ,							1   )

       # batched queries
       UpperCamelCase	:					Optional[int]       	=   np.eye(5       ,							dtype=np.floataa   )[::-1]
       UpperCamelCase	,    UpperCamelCase	:					Tuple       	=   index.search_batch(A_   )
       self.assertRaises(A_       ,							index.search_batch       ,							queries[0]   )
       UpperCamelCase	:					Optional[int]       	=   [scores[0] for scores in total_scores]
       UpperCamelCase	:					Tuple       	=   [indices[0] for indices in total_indices]
       self.assertGreater(np.min(A_   )       ,							0   )
       self.assertListEqual([4, 3, 2, 1, 0]       ,							A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					List[str]       	=   FaissIndex(string_factory="Flat"   )
       index.add_vectors(np.eye(5       ,							dtype=np.floataa   )   )
       self.assertIsInstance(index.faiss_index       ,							faiss.IndexFlat   )
       UpperCamelCase	:					List[str]       	=   FaissIndex(string_factory="LSH"   )
       index.add_vectors(np.eye(5       ,							dtype=np.floataa   )   )
       self.assertIsInstance(index.faiss_index       ,							faiss.IndexLSH   )
       with self.assertRaises(A_   ):
        UpperCamelCase	:					List[str]       	=   FaissIndex(string_factory="Flat"       ,							custom_index=faiss.IndexFlat(5   )   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					Dict       	=   faiss.IndexFlat(5   )
       UpperCamelCase	:					Union[str, Any]       	=   FaissIndex(custom_index=A_   )
       index.add_vectors(np.eye(5       ,							dtype=np.floataa   )   )
       self.assertIsInstance(index.faiss_index       ,							faiss.IndexFlat   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       import faiss

       UpperCamelCase	:					str       	=   FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT   )
       index.add_vectors(np.eye(5       ,							dtype=np.floataa   )   )

       # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
       # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
       # see https://bugs.python.org/issue14243 and
       # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
       with tempfile.NamedTemporaryFile(delete=A_   ) as tmp_file:
        index.save(tmp_file.name   )
        UpperCamelCase	:					int       	=   FaissIndex.load(tmp_file.name   )
       os.unlink(tmp_file.name   )

       UpperCamelCase	:					str       	=   np.zeros(5       ,							dtype=np.floataa   )
       UpperCamelCase	:					int       	=   1
       UpperCamelCase	,    UpperCamelCase	:					Dict       	=   index.search(A_   )
       self.assertGreater(scores[0]       ,							0   )
       self.assertEqual(indices[0]       ,							1   )



@require_faiss
def 		A_				(     _lowerCAmelCase     )						-> Optional[int]:
 import faiss

 UpperCamelCase	:					Union[str, Any]       	=   FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT     )
 index.add_vectors(np.eye(5							,   dtype=np.floataa     )     )

 UpperCamelCase	:					List[Any]       	=   "index.faiss"
 UpperCamelCase	:					List[str]       	=   F"""mock://{index_name}"""
 index.save(_lowerCAmelCase							,   storage_options=mockfs.storage_options     )
 UpperCamelCase	:					List[str]       	=   FaissIndex.load(_lowerCAmelCase							,   storage_options=mockfs.storage_options     )

 UpperCamelCase	:					List[str]       	=   np.zeros(5							,   dtype=np.floataa     )
 UpperCamelCase	:					Optional[int]       	=   1
 UpperCamelCase	,    UpperCamelCase	:					List[str]       	=   index.search(_lowerCAmelCase     )
 assert scores[0] > 0
 assert indices[0] == 1



@require_elasticsearch
class 					A__   (       __snake_case			):





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       from elasticsearch import Elasticsearch

       with patch("elasticsearch.Elasticsearch.search"   ) as mocked_search, patch(
           "elasticsearch.client.IndicesClient.create"   ) as mocked_index_create, patch("elasticsearch.helpers.streaming_bulk"   ) as mocked_bulk:
        UpperCamelCase	:					List[str]       	=   Elasticsearch()
        UpperCamelCase	:					Union[str, Any]       	=   {"acknowledged": True}
        UpperCamelCase	:					Union[str, Any]       	=   ElasticSearchIndex(es_client=A_   )
        mocked_bulk.return_value([(True, None)] * 3   )
        index.add_documents(["foo", "bar", "foobar"]   )

        # single query
        UpperCamelCase	:					str       	=   "foo"
        UpperCamelCase	:					Dict       	=   {"hits": {"hits": [{"_score": 1, "_id": 0}]}}
        UpperCamelCase	,    UpperCamelCase	:					Tuple       	=   index.search(A_   )
        self.assertEqual(scores[0]       ,							1   )
        self.assertEqual(indices[0]       ,							0   )

        # single query with timeout
        UpperCamelCase	:					Dict       	=   "foo"
        UpperCamelCase	:					Optional[Any]       	=   {"hits": {"hits": [{"_score": 1, "_id": 0}]}}
        UpperCamelCase	,    UpperCamelCase	:					str       	=   index.search(A_       ,							request_timeout=30   )
        self.assertEqual(scores[0]       ,							1   )
        self.assertEqual(indices[0]       ,							0   )

        # batched queries
        UpperCamelCase	:					Dict       	=   ["foo", "bar", "foobar"]
        UpperCamelCase	:					List[Any]       	=   {"hits": {"hits": [{"_score": 1, "_id": 1}]}}
        UpperCamelCase	,    UpperCamelCase	:					Optional[int]       	=   index.search_batch(A_   )
        UpperCamelCase	:					str       	=   [scores[0] for scores in total_scores]
        UpperCamelCase	:					Optional[Any]       	=   [indices[0] for indices in total_indices]
        self.assertGreater(np.min(A_   )       ,							0   )
        self.assertListEqual([1, 1, 1]       ,							A_   )

        # batched queries with timeout
        UpperCamelCase	:					int       	=   ["foo", "bar", "foobar"]
        UpperCamelCase	:					List[Any]       	=   {"hits": {"hits": [{"_score": 1, "_id": 1}]}}
        UpperCamelCase	,    UpperCamelCase	:					Union[str, Any]       	=   index.search_batch(A_       ,							request_timeout=30   )
        UpperCamelCase	:					Union[str, Any]       	=   [scores[0] for scores in total_scores]
        UpperCamelCase	:					Dict       	=   [indices[0] for indices in total_indices]
        self.assertGreater(np.min(A_   )       ,							0   )
        self.assertListEqual([1, 1, 1]       ,							A_   )
