def test_get_lsh_functor_impls_no_reload(self, m_get_plugins): get_lsh_functor_impls() m_get_plugins.assert_called_once() NT.assert_equal(m_get_plugins.call_args[0][0], 'smqtk.algorithms.nn_index.lsh.functors') NT.assert_equal(m_get_plugins.call_args[0][2], 'LSH_FUNCTOR_PATH') NT.assert_equal(m_get_plugins.call_args[0][3], 'LSH_FUNCTOR_CLASS') NT.assert_equal(m_get_plugins.call_args[0][4], LshFunctor) NT.assert_false(m_get_plugins.call_args[1]['reload_modules'])
def test_get_lsh_functor_impls_with_reload(self, m_get_plugins): get_lsh_functor_impls(True) m_get_plugins.assert_called_once() NT.assert_equal(m_get_plugins.call_args[0][0], 'smqtk.algorithms.nn_index.lsh.functors') # m_get_plugins.call_args[0][1] is a path depending on where the python # code is. NT.assert_equal(m_get_plugins.call_args[0][2], 'LSH_FUNCTOR_PATH') NT.assert_equal(m_get_plugins.call_args[0][3], 'LSH_FUNCTOR_CLASS') NT.assert_equal(m_get_plugins.call_args[0][4], LshFunctor) NT.assert_true(m_get_plugins.call_args[1]['reload_modules'])
def get_default_config(cls): """ Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it. By default, we observe what this class's constructor takes as arguments, turning those argument names into configuration dictionary keys. If any of those arguments have defaults, we will add those values into the configuration dictionary appropriately. The dictionary returned should only contain JSON compliant value types. 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 """ default = super(LSHNearestNeighborIndex, cls).get_default_config() lf_default = plugin.make_config(get_lsh_functor_impls()) default['lsh_functor'] = lf_default di_default = plugin.make_config(get_descriptor_index_impls()) default['descriptor_index'] = di_default hi_default = plugin.make_config(get_hash_index_impls()) default['hash_index'] = hi_default default['hash_index_comment'] = "'hash_index' may also be null to " \ "default to a linear index built at " \ "query time." return default
def get_default_config(cls): """ Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it. By default, we observe what this class's constructor takes as arguments, turning those argument names into configuration dictionary keys. If any of those arguments have defaults, we will add those values into the configuration dictionary appropriately. The dictionary returned should only contain JSON compliant value types. 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 """ default = super(LSHNearestNeighborIndex, cls).get_default_config() lf_default = plugin.make_config(get_lsh_functor_impls()) default['lsh_functor'] = lf_default di_default = plugin.make_config(get_descriptor_index_impls()) default['descriptor_index'] = di_default hi_default = plugin.make_config(get_hash_index_impls()) default['hash_index'] = hi_default default['hash_index_comment'] = "'hash_index' may also be null to " \ "default to a linear index built at " \ "query time." return default
def from_config(cls, config_dict, merge_default=True): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :param merge_default: Merge the given configuration on top of the default provided by ``get_default_config``. :type merge_default: bool :return: Constructed instance from the provided config. :rtype: LSHNearestNeighborIndex """ # Controlling merge here so we can control known comment stripping from # default config. if merge_default: merged = cls.get_default_config() merge_dict(merged, config_dict) else: merged = config_dict merged['lsh_functor'] = \ plugin.from_plugin_config(merged['lsh_functor'], get_lsh_functor_impls()) merged['descriptor_index'] = \ plugin.from_plugin_config(merged['descriptor_index'], get_descriptor_index_impls()) # Hash index may be None for a default at-query-time linear indexing if merged['hash_index'] and merged['hash_index']['type']: merged['hash_index'] = \ plugin.from_plugin_config(merged['hash_index'], get_hash_index_impls()) else: cls.get_logger().debug( "No HashIndex impl given. Passing ``None``.") merged['hash_index'] = None # remove possible comment added by default generator if 'hash_index_comment' in merged: del merged['hash_index_comment'] merged['hash2uuids_kvstore'] = \ plugin.from_plugin_config(merged['hash2uuids_kvstore'], get_key_value_store_impls()) return super(LSHNearestNeighborIndex, cls).from_config(merged, False)
def from_config(cls, config_dict, merge_default=True): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :param merge_default: Merge the given configuration on top of the default provided by ``get_default_config``. :type merge_default: bool :return: Constructed instance from the provided config. :rtype: LSHNearestNeighborIndex """ # Controlling merge here so we can control known comment stripping from # default config. if merge_default: merged = cls.get_default_config() merge_dict(merged, config_dict) else: merged = config_dict merged['lsh_functor'] = \ plugin.from_plugin_config(merged['lsh_functor'], get_lsh_functor_impls()) merged['descriptor_index'] = \ plugin.from_plugin_config(merged['descriptor_index'], get_descriptor_index_impls()) # Hash index may be None for a default at-query-time linear indexing if merged['hash_index'] and merged['hash_index']['type']: merged['hash_index'] = \ plugin.from_plugin_config(merged['hash_index'], get_hash_index_impls()) else: cls.get_logger().debug("No HashIndex impl given. Passing ``None``.") merged['hash_index'] = None # remove possible comment added by default generator if 'hash_index_comment' in merged: del merged['hash_index_comment'] merged['hash2uuids_kvstore'] = \ plugin.from_plugin_config(merged['hash2uuids_kvstore'], get_key_value_store_impls()) return super(LSHNearestNeighborIndex, cls).from_config(merged, False)