def __init__(self, data_dir=None, **kwargs): if data_dir: self._data_dir = data_dir else: self._data_dir = FileIO.pupyl_temp_data_dir() self._index_config_path = os.path.join(self._data_dir, 'index.json') configurations = self._index_configuration('r') if configurations: self._import_images = configurations['import_images'] self._characteristic = Characteristics.by_name( configurations['characteristic']) if configurations.get('feature_size'): self._feature_size = configurations['feature_size'] else: import_images = kwargs.get('import_images') characteristic = kwargs.get('characteristic') if import_images: self._import_images = import_images else: self._import_images = True if characteristic: self._characteristic = characteristic else: self._characteristic = Characteristics.\ HEAVYWEIGHT_HUGE_PRECISION self.image_database = ImageDatabase(import_images=self._import_images, data_dir=self._data_dir)
def test___len__(): """Unit test for __len__ method.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) assert len(image_database) == 1
def __init__(self, size, data_dir=None, trees=.001, volatile=False): """ Indexing tensors operations and nearest neighbours search. Parameters ---------- size: int Shape of unidimensional vectors which will be indexed data_dir: str Location where to load or save the index trees (optional): float Defines the number of trees to create based on the dataset size. Should be a number between 0 and 1. volatile (optional): bool If the index will be temporary or not. """ self._position = -1 self._size = size self._data_dir = data_dir self._trees = trees self._volatile = volatile if self._data_dir and not self._volatile: if os.path.isfile(self._data_dir): raise OSError('data_dir parameter is not a directory') os.makedirs(self._data_dir, exist_ok=True) self._path = os.path.join(self._data_dir, self.index_name) elif not self._data_dir and not self._volatile: raise NoDataDirForPermanentIndex elif not self._data_dir and self._volatile: _temp_file = FileIO.safe_temp_file() self._data_dir = os.path.dirname(_temp_file) self._path = _temp_file else: raise DataDirDefinedForVolatileIndex if os.path.isfile(self._path): try: self.tree = AnnoyIndex(size, metric='angular') self.tree.load(self._path) self._is_new_index = False except OSError as os_error: raise FileIsNotAnIndex from os_error else: self.tree = AnnoyIndex(size, metric='angular') self._is_new_index = True self._image_database = ImageDatabase( import_images=True, data_dir=self._data_dir, )
def test___get_item___not_found(self): """Unit test for method __get_item__, index not found case.""" with self.assertRaises(IndexError): image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) _ = image_database[999]
def test_image_size_property_no_import_images(): """Unit test for property image_size, no import images case.""" image_database = ImageDatabase( data_dir=TEST_DIRECTORY, import_images=False ) test_image_size = (800, 600) assert image_database.image_size == test_image_size
def test_load_image_metadata_not_found(self): """Unit test for method load_image_metadata, index not found case.""" with self.assertRaises(IndexError): image_database = ImageDatabase( import_images=True, directory=TEST_DIRECTORY ) _ = image_database.load_image_metadata(999)
def test_bucket_size_property(): """Unit test for property bucket_size.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) test_bucket_size = 999 image_database.bucket_size = test_bucket_size assert image_database.bucket_size == test_bucket_size
def test_image_size_property(): """Unit test for property image_size.""" image_database = ImageDatabase( data_dir=TEST_DIRECTORY, import_images=True ) test_image_size = (320, 200) image_database.image_size = test_image_size assert image_database.image_size == test_image_size
def test_what_bucket(): """Unit test for method what_bucket.""" test_bucket_size = 10 ** 4 image_database = ImageDatabase( import_images=True, bucket_size=test_bucket_size ) assert image_database.what_bucket(TEST_INDEX) == \ TEST_INDEX // test_bucket_size
def test_insert_no_import_images(): """Unit test for method insert, no import images case.""" image_database = ImageDatabase( data_dir=TEST_TEMP_DIRECTORY, import_images=False ) image_database.insert(20, 'tests/test_image.jpg') assert not exists(f'{TEST_TEMP_DIRECTORY}/0/20.jpg') and \ exists(f'{TEST_TEMP_DIRECTORY}/0/20.json')
def test_insert_import_images(): """Unit test for method insert, import images case.""" image_database = ImageDatabase( data_dir=TEST_TEMP_DIRECTORY, import_images=True ) image_database.insert(10, 'tests/test_image.jpg') assert exists(f'{TEST_TEMP_DIRECTORY}/0/10.jpg') \ and exists(f'{TEST_TEMP_DIRECTORY}/0/10.json')
def test_list_images(): """Unit test for method list_images.""" expected_result = abspath('tests/test_database/0/0.jpg') image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) actual_result = [*image_database.list_images()][0] assert expected_result == actual_result
def test_list_images_less_than_count(): """Unit test for method list_images, less than counter case.""" expected_length = -1 image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) actual_result = [*image_database.list_images(top=expected_length)] assert len(actual_result) == 0
def test_load_image_metadata_filtered(): """Unit test for method load_image_metadata, filtered input case.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) test_filter = ('id', 'original_file_size') test_metadata = image_database.load_image_metadata(0, filtered=test_filter) for key in test_metadata: assert key in test_filter
def test_mount_file_name(): """Unit test for method what_bucket.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) expected_path = join( TEST_DIRECTORY, str(image_database.what_bucket(TEST_INDEX)), f'{TEST_INDEX}.json' ) assert image_database.mount_file_name(TEST_INDEX, 'json') == expected_path
def test_import_images_property(): """Unit test for property import_images.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) test_import_images = True image_database.import_images = test_import_images assert image_database.import_images test_import_images = not test_import_images image_database.import_images = test_import_images assert not image_database.import_images
def test_save_image_metadata(): """Unit test for method save_image_metadata.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) image_database.save_image_metadata(0, TEST_IMAGE) test_metadata = image_database.load_image_metadata(0) del test_metadata['original_access_time'] test_metadata['original_path'] = relpath(test_metadata['original_path']) assert test_metadata == TEST_METADATA
def test_load_image(): """Unit test for method load_image.""" test_index = 0 image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) def inst_load_image(index): """Closure for method load_image.""" return image_database.get_image( image_database.mount_file_name(index, 'jpg') ) test_image = image_database.load_image(test_index) assert test_image == inst_load_image(test_index)
def test_image_database_definition(): """Unit test for instance definition for class ImageDatabase.""" test_import_images = True test_bucket_size = 100 test_image_size = (640, 480) image_database = ImageDatabase( import_images=test_import_images, data_dir=TEST_TEMP_DIRECTORY, bucket_size=test_bucket_size, image_size=test_image_size ) assert isinstance(image_database, ImageDatabase) assert image_database.import_images == test_import_images assert image_database._data_dir == TEST_TEMP_DIRECTORY assert image_database.bucket_size == test_bucket_size assert image_database.image_size == test_image_size
def test_list_images_return_index(): """Unit test for method list_images, return index case.""" expected_path = abspath('tests/test_database/0/0.jpg') expected_index = 0 expected_result = (expected_index, expected_path) image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) actual_index_path = [ *image_database.list_images( return_index=True ) ][0] assert actual_index_path == expected_result
def test___get_item__(): """Unit test for __get_item__ method.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) test_metadata = image_database[0] del test_metadata['original_access_time'] test_metadata = resolve_original_path(test_metadata) assert test_metadata == TEST_METADATA test_metadata = image_database[1] del test_metadata['original_access_time'] test_metadata = resolve_original_path(test_metadata) assert test_metadata == TEST_METADATA_HTTP
def test_load_image_metadata(): """Unit test for method load_image_metadata.""" image_database = ImageDatabase( import_images=True, data_dir=TEST_DIRECTORY ) test_metadata = image_database.load_image_metadata(0) del test_metadata['original_access_time'] test_metadata = resolve_original_path(test_metadata) assert test_metadata == TEST_METADATA test_metadata = image_database.load_image_metadata(1) del test_metadata['original_access_time'] test_metadata = resolve_original_path(test_metadata) assert test_metadata == TEST_METADATA_HTTP