def test_config_cycle_imagemean_nonetyped( self, m_cdg_setup_network: mock.MagicMock) -> None: """ Test being able to get an instances config and use that config to construct an equivalently parameterized instance where the second instance is configured with a None-typed 'image_mean' parameter. """ # Mocking ``_setup_network`` so no caffe functionality is hit during # this test # Only required parameters, image_mean is empty SMQTK configuration # dict g1 = CaffeDescriptorGenerator(self.dummy_net_topo_elem, self.dummy_caffe_model_elem) g1_config = g1.get_config() # Modify config for g2 to pass None for image_mean for_g2 = dict(g1_config) for_g2['image_mean'] = {'type': None} g2 = CaffeDescriptorGenerator.from_config(for_g2) expected_config = { 'network_prototxt': to_config_dict(self.dummy_net_topo_elem), 'network_model': to_config_dict(self.dummy_caffe_model_elem), 'image_mean': None, 'return_layer': 'fc7', 'batch_size': 1, 'use_gpu': False, 'gpu_device_id': 0, 'network_is_bgr': True, 'data_layer': 'data', 'load_truncated_images': False, 'pixel_rescale': None, 'input_scale': None, 'threads': None, } assert g1_config == g2.get_config() == expected_config
def get_config(self) -> Dict[str, Any]: """ Return a JSON-compliant dictionary that could be passed to this class's ``from_config`` method to produce an instance with identical configuration. In the common case, this involves naming the keys of the dictionary based on the initialization argument names as if it were to be passed to the constructor via dictionary expansion. :return: JSON type compliant configuration dictionary. """ image_mean_config: Optional[Dict] if self.image_mean is not None: image_mean_config = to_config_dict(self.image_mean) else: image_mean_config = None return { "network_prototxt": to_config_dict(self.network_prototxt), "network_model": to_config_dict(self.network_model), "image_mean": image_mean_config, "return_layer": self.return_layer, "batch_size": self.batch_size, "use_gpu": self.use_gpu, "gpu_device_id": self.gpu_device_id, "network_is_bgr": self.network_is_bgr, "data_layer": self.data_layer, "load_truncated_images": self.load_truncated_images, "pixel_rescale": self.pixel_rescale, "input_scale": self.input_scale, "threads": self.threads, }
def test_to_config_dict() -> None: """ Test that the second-level helper function is called appropriately and directly returns. """ expected_ret_val = 'expected return value' with mock.patch('smqtk_core.configuration.cls_conf_to_config_dict') \ as m_cctcd: m_cctcd.return_value = expected_ret_val i1 = T1() i1_expected_conf = { 'foo': 1, 'bar': 'baz', } r1 = to_config_dict(i1) m_cctcd.assert_called_once_with(T1, i1_expected_conf) assert r1 == expected_ret_val with mock.patch('smqtk_core.configuration.cls_conf_to_config_dict') \ as m_cctcd: m_cctcd.return_value = expected_ret_val i2 = T1(foo=8) i2_expected_conf = { 'foo': 8, 'bar': 'baz', } r2 = to_config_dict(i2) m_cctcd.assert_called_once_with(T1, i2_expected_conf) assert r2 == expected_ret_val
def test_to_config_dict() -> None: """ Test that the second-level helper function is called appropriately and directly returns. """ # We will mock out the return of `cls_conf_to_config_dict` operation as # that is tested elsewhere. We more want to make sure the input to this # internal function is as expected. expected_ret_val = 'expected return value' with mock.patch('smqtk_core.configuration.cls_conf_to_config_dict') \ as m_cctcd: m_cctcd.return_value = expected_ret_val i1 = T1() i1_expected_conf = { 'foo': 1, 'bar': 'baz', } r1 = to_config_dict(i1) m_cctcd.assert_called_once_with(T1, i1_expected_conf) assert r1 == expected_ret_val with mock.patch('smqtk_core.configuration.cls_conf_to_config_dict') \ as m_cctcd: m_cctcd.return_value = expected_ret_val i2 = T1(foo=8) i2_expected_conf = { 'foo': 8, 'bar': 'baz', } r2 = to_config_dict(i2) m_cctcd.assert_called_once_with(T1, i2_expected_conf) assert r2 == expected_ret_val with mock.patch('smqtk_core.configuration.cls_conf_to_config_dict') \ as m_cctcd: m_cctcd.return_value = expected_ret_val i3 = T2(T1(foo=12, bar='OK'), 0.3, 'not default') i3_expected_conf = { 'child': { "foo": 12, "bar": "OK", }, "alpha": 0.3, "beta": "not default", } r3 = to_config_dict(i3) m_cctcd.assert_called_once_with(T2, i3_expected_conf) assert r3 == expected_ret_val
def get_config(self) -> Dict[str, Any]: hi_conf = None if self.hash_index is not None: hi_conf = to_config_dict(self.hash_index) return { "lsh_functor": to_config_dict(self.lsh_functor), "descriptor_set": to_config_dict(self.descriptor_set), "hash_index": hi_conf, "hash2uuids_kvstore": to_config_dict(self.hash2uuids_kvstore), "distance_method": self.distance_method, "read_only": self.read_only, }
def test_to_config_dict_given_non_configurable() -> None: """ Test that ``to_config_dict`` errors when passed an instance that does not descend from configurable. """ class SomeOtherClassType(object): pass inst = SomeOtherClassType() with pytest.raises(ValueError, match="c_inst must be an instance and its type must " r"subclass from Configurable\."): # noinspection PyTypeChecker to_config_dict(inst) # type: ignore
def test_get_config(self, _m_cdg_setupNetwork: mock.MagicMock) -> None: # Mocking set_network so we don't have to worry about actually # initializing any caffe things for this test. expected_params: Dict[str, Any] = { 'network_prototxt': DataMemoryElement(), 'network_model': DataMemoryElement(), 'image_mean': DataMemoryElement(), 'return_layer': 'layer name', 'batch_size': 777, 'use_gpu': False, 'gpu_device_id': 8, 'network_is_bgr': False, 'data_layer': 'data-other', 'load_truncated_images': True, 'pixel_rescale': (.2, .8), 'input_scale': 1.5, 'threads': 14, } # make sure that we're considering all constructor parameter # options default_params = CaffeDescriptorGenerator.get_default_config() assert set(default_params) == set(expected_params) g = CaffeDescriptorGenerator(**expected_params) # Shift to expecting sub-configs for DataElement params for key in ('network_prototxt', 'network_model', 'image_mean'): expected_params[key] = to_config_dict( cast(DataMemoryElement, expected_params[key])) assert g.get_config() == expected_params
def get_config(self) -> Dict[str, Any]: # If no cache elements (set to None), return default plugin configs. c = merge_dict( self.get_default_config(), { "bit_length": self.bit_length, "itq_iterations": self.itq_iterations, "normalize": self.normalize, "random_seed": self.random_seed, }) if self.mean_vec_cache_elem: c['mean_vec_cache'] = \ to_config_dict(self.mean_vec_cache_elem) if self.rotation_cache_elem: c['rotation_cache'] = \ to_config_dict(self.rotation_cache_elem) return c
def get_config(self) -> Dict[str, Any]: c = merge_dict(self.get_default_config(), { "pickle_protocol": self.pickle_protocol, }) if self.cache_element: merge_dict(c['cache_element'], to_config_dict(self.cache_element)) return c
def get_config(self) -> Dict[str, Any]: return merge_dict( self.get_default_config(), dict( rank_relevancy=to_config_dict(self._rank_relevancy), n=self._n, seed=self._seed, ))
def get_config(self) -> Dict[str, Any]: # Recursively get config from data element if we have one. if self._cache_element is not None: elem_config = to_config_dict(self._cache_element) else: # No cache element, output default config with no type. elem_config = make_default_config(DataElement.get_impls()) return {'cache_element': elem_config}
def get_config(self) -> Dict[str, Any]: c = merge_dict(self.get_default_config(), { 'leaf_size': self.leaf_size, 'random_seed': self.random_seed, }) if self.cache_element: c['cache_element'] = merge_dict(c['cache_element'], to_config_dict(self.cache_element)) return c
def get_config(self) -> Dict[str, Any]: config = { "descriptor_set": to_config_dict(self._descriptor_set), "uid2idx_kvs": to_config_dict(self._uid2idx_kvs), "idx2uid_kvs": to_config_dict(self._idx2uid_kvs), "factory_string": self.factory_string, "metric_type": self._metric_type, "ivf_nprobe": self._ivf_nprobe, "read_only": self.read_only, "random_seed": self.random_seed, "use_gpu": self._use_gpu, "gpu_id": self._gpu_id, } if self._index_element: config['index_element'] = to_config_dict(self._index_element) if self._index_param_element: config['index_param_element'] = to_config_dict( self._index_param_element) return config
def get_config(self) -> Dict[str, Any]: return { "descriptor_set": to_config_dict(self._descriptor_set), "index_filepath": self._index_filepath, "parameters_filepath": self._index_param_filepath, "read_only": self._read_only, "random_seed": self._rand_seed, "pickle_protocol": self._pickle_protocol, "use_multiprocessing": self._use_multiprocessing, "depth": self._depth, "num_trees": self._num_trees, }
def test_to_config_dict_given_type() -> None: """ Test that ``to_config_dict`` errors when passed a type. """ # Just with `object`. with pytest.raises(ValueError, match="c_inst must be an instance and its type must " r"subclass from Configurable\."): # noinspection PyTypeChecker to_config_dict(object) # type: ignore # Literally the Configurable interface (abstract class) with pytest.raises(ValueError, match="c_inst must be an instance and its type must " r"subclass from Configurable\."): # noinspection PyTypeChecker to_config_dict(Configurable) # type: ignore # New sub-class implementing Configurable, but passed as the type not an # instance. class SomeConfigurableType(Configurable): def get_config(self) -> Dict[str, Any]: ... with pytest.raises(ValueError): # noinspection PyTypeChecker to_config_dict(SomeConfigurableType) # type: ignore
def get_config(self) -> Dict[str, Any]: """ This implementation has no configuration properties. :return: JSON type compliant configuration dictionary. :rtype: dict """ c = merge_dict(self.get_default_config(), { "pickle_protocol": self.pickle_protocol, }) if self.cache_element: c['cache_element'] = merge_dict(c['cache_element'], to_config_dict(self.cache_element)) return c
def get_default_config(cls) -> Dict: """ Generate and return a default configuration dictionary for this class. It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class. :return: Default configuration dictionary for the class. :rtype: dict """ c = super(KVSDataSet, cls).get_default_config() c['kvstore'] = merge_dict( make_default_config(KeyValueStore.get_impls()), to_config_dict(c['kvstore'])) return c
def get_config(self) -> Dict[str, Any]: return {'kvstore': to_config_dict(self._kvstore)}
def get_config(self) -> Dict[str, Any]: c = self.get_default_config() if self.cache_element: c['cache_element'] = merge_dict(c['cache_element'], to_config_dict(self.cache_element)) return c
def get_config(self): with self._label_to_classifier_lock: c = dict( (label, to_config_dict(classifier)) for label, classifier in self._label_to_classifier.items()) return c
def get_config(self) -> Dict[str, Any]: return { "perturber": to_config_dict(self._perturber), "generator": to_config_dict(self._generator), "threads": self._threads, }