def test_configuration(self) -> None: """ Test standard configuration """ ex_tname = 'test' ex_uuid = 0 inst = MemoryClassificationElement(ex_tname, ex_uuid) configuration_test_helper(inst, {'type_name', 'uuid'}, (ex_tname, ex_uuid))
def test_configuration_no_data(self) -> None: """ Testing getting the configuration from an instance with no matrix data and creating a new instance from that configuration. """ inst = MatrixDataElement(readonly=False) for i in configuration_test_helper(inst): assert i.matrix is None assert i.is_read_only() is False inst = MatrixDataElement(readonly=True) for i in configuration_test_helper(inst): assert i.matrix is None assert i.is_read_only() is True
def test_standard_config(self) -> None: ex_w = (777, 776) ex_s = (444, 445) impl = SlidingWindow(window_size=ex_w, stride=ex_s) for inst in configuration_test_helper(impl): assert inst.window_size == ex_w assert inst.stride == ex_s
def test_configuration(self) -> None: """ Test configuration aspects. """ inst = RISEScoring(p1=0.747) for inst_i in configuration_test_helper(inst): assert inst_i.p1 == 0.747
def test_get_config(self) -> None: """ Test expected configuation behavior. """ impl = DRISEScoring('euclidean') for i in configuration_test_helper(impl): assert i.proximity_metric == 'euclidean'
def test_get_config(self) -> None: """ Test expected configuation behavior. """ impl = SimilarityScoring('hamming') for i in configuration_test_helper(impl): assert i.proximity_metric == 'hamming'
def test_configuration_with_data(self) -> None: """ Testing getting the configuration from an instance with no matrix data and creating a new instance from that configuration. """ inst = MatrixDataElement((1, 2, 3, 4), readonly=False) for i in configuration_test_helper(inst): assert i.matrix is not None np.testing.assert_allclose(i.matrix, (1, 2, 3, 4)) assert i.is_read_only() is False inst = MatrixDataElement((1, 2, 3, 4), readonly=True) for i in configuration_test_helper(inst): assert i.matrix is not None np.testing.assert_allclose(i.matrix, (1, 2, 3, 4)) assert i.is_read_only() is True
def test_configuration(self) -> None: """ Test configuration suite using known simple implementations. """ class StubPI (PerturbImage): perturb = None # type: ignore def __init__(self, stub_param: int): self.p = stub_param def get_config(self) -> Dict[str, Any]: return {'stub_param': self.p} class StubGen (GenerateClassifierConfidenceSaliency): generate = None # type: ignore def __init__(self, stub_param: int): self.p = stub_param def get_config(self) -> Dict[str, Any]: return {'stub_param': self.p} test_threads = 87 test_spi_p = 0 test_sgn_p = 1 inst = PerturbationOcclusion( StubPI(test_spi_p), StubGen(test_sgn_p), 87 ) for inst_i in configuration_test_helper(inst): assert inst_i._threads == test_threads assert isinstance(inst_i._perturber, StubPI) assert inst_i._perturber.p == test_spi_p assert isinstance(inst_i._generator, StubGen) assert inst_i._generator.p == test_sgn_p
def test_configuration(self) -> None: index_filepath = osp.abspath(osp.expanduser('index_filepath')) para_filepath = osp.abspath(osp.expanduser('param_fp')) i = MRPTNearestNeighborsIndex( descriptor_set=MemoryDescriptorSet(), index_filepath=index_filepath, parameters_filepath=para_filepath, read_only=True, num_trees=9, depth=2, random_seed=8, pickle_protocol=0, use_multiprocessing=True, ) for inst in configuration_test_helper( i): # type: MRPTNearestNeighborsIndex assert isinstance(inst._descriptor_set, MemoryDescriptorSet) assert inst._index_filepath == index_filepath assert inst._index_param_filepath == para_filepath assert inst._read_only is True assert inst._num_trees == 9 assert inst._depth == 2 assert inst._rand_seed == 8 assert inst._pickle_protocol == 0 assert inst._use_multiprocessing is True
def test_configuration(self) -> None: ex_descr_set = MemoryDescriptorSet() ex_i2u_kvs = MemoryKeyValueStore() ex_u2i_kvs = MemoryKeyValueStore() ex_index_elem = DataMemoryElement() ex_index_param_elem = DataMemoryElement() i = FaissNearestNeighborsIndex( descriptor_set=ex_descr_set, idx2uid_kvs=ex_i2u_kvs, uid2idx_kvs=ex_u2i_kvs, index_element=ex_index_elem, index_param_element=ex_index_param_elem, read_only=True, factory_string=u'some fact str', ivf_nprobe=88, use_gpu=False, gpu_id=99, random_seed=8, ) for inst in configuration_test_helper(i): assert isinstance(inst._descriptor_set, MemoryDescriptorSet) assert isinstance(inst._idx2uid_kvs, MemoryKeyValueStore) assert isinstance(inst._uid2idx_kvs, MemoryKeyValueStore) assert isinstance(inst._index_element, DataMemoryElement) assert isinstance(inst._index_param_element, DataMemoryElement) assert inst.read_only is True assert isinstance(inst.factory_string, str) assert inst.factory_string == 'some fact str' assert inst._ivf_nprobe == 88 assert inst._use_gpu is False assert inst._gpu_id == 99 assert inst.random_seed == 8
def test_configuration(self) -> None: """ Test instance standard configuration """ inst = DescriptorMemoryElement('test', 'abcd') for i in configuration_test_helper( inst, {'type_str', 'uuid'}, ('test', 'abcd')): # type: DescriptorMemoryElement assert i.type() == 'test' assert i.uuid() == 'abcd'
def test_configuration(self) -> None: inst = DataFileSet(root_directory='/some/dir', uuid_chunk=10, pickle_protocol=-1) for i in configuration_test_helper(inst): # type: DataFileSet assert i._root_dir == '/some/dir' assert i._uuid_chunk == 10 assert i.pickle_protocol == -1
def test_configuration(self) -> None: """ Test instance standard configuration """ inst = DescriptorFileElement('abcd', save_dir='/some/path/somewhere', subdir_split=4) for i in configuration_test_helper(inst, {'uuid'}, ('abcd', )): assert i._save_dir == '/some/path/somewhere' assert i._subdir_split == 4
def test_configuration(self): """ Test getting and constructing from configuration. """ expected_exmode = 'L' i = PilImageReader(explicit_mode=expected_exmode) for inst in configuration_test_helper(i): # type: PilImageReader assert inst._explicit_mode == expected_exmode
def test_standard_config(self) -> None: ex_N = 1000 ex_s = 8 ex_p1 = 0.5 impl = RISEPertubation(N=ex_N, s=ex_s, p1=ex_p1) for inst in configuration_test_helper(impl): assert inst.N == ex_N assert inst.s == ex_s assert inst.p1 == ex_p1
def test_standard_config(self) -> None: ex_n = 1000 ex_s = 8 ex_p1 = 0.5 impl = RISEGrid(n=ex_n, s=ex_s, p1=ex_p1) for inst in configuration_test_helper(impl): assert inst.n == ex_n assert inst.s == ex_s assert inst.p1 == ex_p1
def test_configuration(self) -> None: fp = os.path.join(TEST_DATA_DIR, "grace_hopper.png") inst = DataFileElement(filepath=fp, readonly=True, explicit_mimetype='foo/bar') for i in configuration_test_helper(inst): # type: DataFileElement assert i._filepath == fp assert i._readonly is True assert i._explicit_mimetype == 'foo/bar'
def test_configuration(self) -> None: inst = DataMemoryElement( bytes=b'Hello World.', content_type='text/plain', readonly=True, ) for i in configuration_test_helper(inst): assert i._bytes == b'Hello World.' assert i._content_type == 'text/plain' assert i._readonly is True
def test_configuration(self): """ Test configuration using helper """ expected_load_method = GdalImageReader.LOAD_METHOD_TEMPFILE expected_channel_order = [3, 5, 4] i = GdalImageReader(load_method=expected_load_method, channel_order=expected_channel_order) for inst in configuration_test_helper(i): # type: GdalImageReader assert inst._load_method == expected_load_method assert inst._channel_order == expected_channel_order
def test_configuration(self) -> None: inst = LibSvmHikRelevancyIndex(descr_cache_filepath='foobar.thing', autoneg_select_ratio=89, multiprocess_fetch=True, cores=1) for i in configuration_test_helper( inst): # type: LibSvmHikRelevancyIndex assert i.descr_cache_fp == 'foobar.thing' assert i.autoneg_select_ratio == 89 assert i.multiprocess_fetch is True assert i.cores == 1
def test_config(self) -> None: inst = HBaseDataElement(element_key='foobar', binary_column='binary_data', hbase_address="some_address", hbase_table='some_table', timeout=12345) for i in configuration_test_helper(inst): # type: HBaseDataElement assert i.element_key == 'foobar' assert i.binary_column == 'binary_data' assert i.hbase_address == 'some_address' assert i.hbase_table == 'some_table' assert i.timeout == 12345
def test_from_config_full_constructor( self, _mock_authenticate: mock.MagicMock) -> None: expected_file_id = '34uhki34gh2345ghjk' expected_api_root = 'https://some.other.server/api/v1' expected_api_key = '1234ghk135hlg23435' inst = GirderDataElement(expected_file_id, api_root=expected_api_root, api_key=expected_api_key) for i in configuration_test_helper(inst): # type: GirderDataElement assert i.file_id == expected_file_id assert i.api_root == expected_api_root assert i.api_key == expected_api_key
def test_configuration(self) -> None: i = LSHNearestNeighborIndex( lsh_functor=ItqFunctor(), descriptor_set=MemoryDescriptorSet(), hash2uuids_kvstore=MemoryKeyValueStore(), hash_index=LinearHashIndex(), distance_method='euclidean', read_only=True ) for inst in configuration_test_helper(i): # type: LSHNearestNeighborIndex assert isinstance(inst.lsh_functor, LshFunctor) assert isinstance(inst.descriptor_set, MemoryDescriptorSet) assert isinstance(inst.hash_index, LinearHashIndex) assert isinstance(inst.hash2uuids_kvstore, MemoryKeyValueStore) assert inst.distance_method == 'euclidean' assert inst.read_only is True
def test_configuration(self) -> None: index_filepath = '/index_filepath' para_filepath = '/param_fp' descr_cache_fp = '/descrcachefp' c = FlannNearestNeighborsIndex( index_uri=index_filepath, parameters_uri=para_filepath, descriptor_cache_uri=descr_cache_fp, distance_method='hik', random_seed=42, ) for inst in configuration_test_helper(c): # type: FlannNearestNeighborsIndex assert inst._index_uri == index_filepath assert inst._index_param_uri == para_filepath assert inst._descr_cache_uri == descr_cache_fp assert inst._distance_method == 'hik' assert inst._rand_seed == 42
def test_configuration(self) -> None: """ Test standard config things. """ inst = SlidingWindowStack( window_size=(8, 9), stride=(19, 14), threads=99, ) for inst_i in configuration_test_helper(inst): inst_p = inst_i._po._perturber inst_g = inst_i._po._generator assert isinstance(inst_p, SlidingWindow) assert isinstance(inst_g, OcclusionScoring) assert inst_p.window_size == (8, 9) assert inst_p.stride == (19, 14) assert inst_i._po._threads == 99
def test_configuration(self, _mock_solr: mock.MagicMock) -> None: inst = SolrDescriptorElement( 'a', solr_conn_addr=self.TEST_URL, uuid_field='uuid_s', vector_field='vector_fs', timestamp_field='timestamp_f', timeout=101, persistent_connection=True, commit_on_set=False, ) for i in configuration_test_helper(inst, {'uuid'}, ('abcd', )): assert i.solr_conn_addr == self.TEST_URL assert i.uuid_field == 'uuid_s' assert i.vector_field == 'vector_fs' assert i.timestamp_field == 'timestamp_f' assert i.solr_timeout == 101 assert i.solr_persistent_connection is True assert i.solr_commit_on_set is False
def test_configuration() -> None: """ Test file-based impl's configuration reflection """ expected_typename = 'test' expected_uuid = 235246 expected_save_dir = '/foo/bar/thing' expected_pp = 1 expected_subdir_split = 2 inst = FileClassificationElement(expected_typename, expected_uuid, expected_save_dir, expected_subdir_split, expected_pp) for i in configuration_test_helper( inst, {"type_name", "uuid"}, (expected_typename, expected_uuid)): # type: FileClassificationElement assert i.save_dir == expected_save_dir assert i.pickle_protocol == expected_pp assert i.subdir_split == expected_subdir_split
def test_configuration_not_debiased(self) -> None: """ Test configuration when debiased is set to False. """ inst = RISEStack( n=444, s=33, p1=.22, seed=42, threads=99, debiased=False, ) for inst_i in configuration_test_helper(inst): inst_p = inst_i._po._perturber inst_g = inst_i._po._generator assert isinstance(inst_p, RISEGrid) assert isinstance(inst_g, RISEScoring) assert inst_p.p1 == .22 assert inst_g.p1 == 0.0 assert inst_i.get_config()['debiased'] is False
def test_configuration(self) -> None: """ Standard configuration test. """ # If there is no scikit-learn installed, this object has no constructor # parameterization. We would not be in here at that point because the # impl would not report as usable, but mypy doesn't care about that. inst = SkLearnLogisticRegression( # type: ignore penalty='l1', dual=True, tol=1e-6, C=2.0, fit_intercept=False, intercept_scaling=3, class_weight={ 0: 2.0, 1: 3.0 }, random_state=456, solver='liblinear', max_iter=99, multi_class='multinomial', verbose=1, warm_start=True, n_jobs=2, ) for inst_i in configuration_test_helper( inst): # type: SkLearnLogisticRegression assert inst.penalty == inst_i.penalty == 'l1' assert inst.dual is inst_i.dual is True assert inst.tol == inst_i.tol == 1e-6 assert inst.C == inst_i.C == 2.0 assert inst.fit_intercept is inst_i.fit_intercept is False assert inst.intercept_scaling == inst_i.intercept_scaling == 3 assert inst.class_weight == inst_i.class_weight == {0: 2.0, 1: 3.0} assert inst.random_state == inst_i.random_state == 456 assert inst.solver == inst_i.solver == 'liblinear' assert inst.multi_class == inst_i.multi_class == 'multinomial' assert inst.verbose == inst_i.verbose == 1 assert inst.warm_start is inst_i.warm_start is True assert inst.n_jobs == inst_i.n_jobs == 2
def test_configuraton(self) -> None: """ Test instance standard configuration. """ inst = PostgresKeyValueStore( table_name="data_set0", key_col='key1', value_col='value2', db_name='postgres3', db_host='foobar', db_port=8997, db_user='******', db_pass='******', batch_size=1007, pickle_protocol=2, read_only=True, create_table=False ) for i in configuration_test_helper(inst): # type: PostgresKeyValueStore assert i._table_name == 'data_set0' assert i._key_col == 'key1' assert i._value_col == 'value2' assert i._psql_helper.db_name == 'postgres3' assert i._psql_helper.db_host == 'foobar' assert i._psql_helper.db_port == 8997 assert i._psql_helper.db_user == 'Bob' assert i._psql_helper.db_pass == 'flabberghast' assert i._batch_size == 1007 assert i._pickle_protocol == 2 assert i._read_only is True assert i._create_table is False