Esempio n. 1
0
    def from_config(cls, config_dict, type_name, uuid, 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 type_name: Name of the type of classifier this classification was
            generated by.
        :type type_name: str

        :param uuid: Unique ID reference of the classification
        :type uuid: collections.Hashable

        :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: ClassificationElement

        """
        c = {}
        merge_configs(c, config_dict)
        c['type_name'] = type_name
        c['uuid'] = uuid
        return super(ClassificationElement, cls).from_config(c, merge_default)
Esempio n. 2
0
 def test_nested(self):
     a = {
         'a': 1,
         'b': {
             'c': 2,
             'd': {
                 'e': 3
             },
         },
         'f': {
             'g': 4,
             'h': {
                 'i': 5
             }
         },
     }
     b = {'b': {'c': 6}, 'f': {'h': {'i': 7}}, 'j': 8}
     expected = {
         'a': 1,
         'b': {
             'c': 6,
             'd': {
                 'e': 3
             },
         },
         'f': {
             'g': 4,
             'h': {
                 'i': 7
             }
         },
         'j': 8,
     }
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
    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: DescriptorElementFactory

        """
        if merge_default:
            merged_config = cls.get_default_config()
            merge_configs(merged_config, config_dict)
            config_dict = merged_config

        return DescriptorElementFactory(
            get_descriptor_element_impls()[config_dict['type']],
            config_dict[config_dict['type']]
        )
Esempio n. 4
0
    def from_config(cls, config_dict, type_str, uuid, merge_default=True):
        """
        Instantiate a new instance of this class given the desired type, uuid,
        and JSON-compliant configuration dictionary.

        :param type_str: Type of descriptor. This is usually the name of the
            content descriptor that generated this vector.
        :type type_str: str

        :param uuid: Unique ID reference of the descriptor.
        :type uuid: collections.Hashable

        :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: DescriptorElement

        """
        c = {}
        merge_configs(c, config_dict)
        c['type_str'] = type_str
        c['uuid'] = uuid
        return super(DescriptorElement, cls).from_config(c, merge_default)
Esempio n. 5
0
    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: Configurable

        """
        # The simple case is that the class doesn't require any special
        # parameters other than those that can be provided via the JSON
        # specification, which we cover here. If an implementation needs
        # something more special, they can override this function.
        if merge_default:
            merged_config = cls.get_default_config()
            merge_configs(merged_config, config_dict)
            config_dict = merged_config

        return cls(**config_dict)
Esempio n. 6
0
 def test_subset_merge(self):
     a = {
         'a': 1,
         'b': 2,
     }
     b = {'a': 3}
     expected = {
         'a': 3,
         'b': 2,
     }
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 7
0
 def test_disjoint_update(self):
     a = {
         'a': 1,
         'b': 2,
     }
     b = {'c': 3}
     expected = {
         'a': 1,
         'b': 2,
         'c': 3,
     }
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 8
0
 def get_default_config(cls):
     c = super(IqrSearchApp, cls).get_default_config()
     merge_configs(c, {
         "mongo": {
             "server": "127.0.0.1:27017",
             "database": "smqtk",
         },
         # Each entry in this mapping generates a new tab in the GUI
         "iqr_tabs": [
             IqrSearch.get_default_config(),
         ]
     })
     return c
Esempio n. 9
0
 def get_default_config(cls):
     c = super(IqrSearchApp, cls).get_default_config()
     merge_configs(
         c,
         {
             "mongo": {
                 "server": "127.0.0.1:27017",
                 "database": "smqtk",
             },
             # Each entry in this mapping generates a new tab in the GUI
             "iqr_tabs": [
                 IqrSearch.get_default_config(),
             ]
         })
     return c
Esempio n. 10
0
 def test_partial_update(self):
     a = {
         'a': 1,
         'b': 2,
     }
     b = {
         'a': 3,
         'c': 4,
     }
     expected = {
         'a': 3,
         'b': 2,
         'c': 4,
     }
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 11
0
    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.
        if merge_default:
            merged = cls.get_default_config()
            merge_configs(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'] is not None:
            merged['hash_index'] = \
                plugin.from_plugin_config(merged['hash_index'],
                                          get_hash_index_impls)

        # remove possible comment added by default generator
        if 'hash_index_comment' in merged:
            del merged['hash_index_comment']

        return super(LSHNearestNeighborIndex, cls).from_config(merged, False)
Esempio n. 12
0
    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.

        :return: Default configuration dictionary for the class.
        :rtype: dict

        """
        c = super(DescriptorServiceServer, cls).get_default_config()
        merge_configs(c, {
            "descriptor_factory": DescriptorElementFactory.get_default_config(),
            "descriptor_generators": {
                "example": plugin.make_config(get_descriptor_generator_impls)
            }
        })
        return c
Esempio n. 13
0
 def test_overrides(self):
     a = {
         'a': 1,
         'b': 2,
     }
     b = {
         'b': {
             'c': 3
         },
     }
     expected = {
         'a': 1,
         'b': {
             'c': 3,
         }
     }
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 14
0
 def test_nested(self):
     a = {"a": 1, "b": {"c": 2, "d": {"e": 3}}, "f": {"g": 4, "h": {"i": 5}}}
     b = {"b": {"c": 6}, "f": {"h": {"i": 7}}, "j": 8}
     expected = {"a": 1, "b": {"c": 6, "d": {"e": 3}}, "f": {"g": 4, "h": {"i": 7}}, "j": 8}
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 15
0
 def from_config(cls, config_dict, merge_default=True):
     if merge_default:
         merged = cls.get_default_config()
         merge_configs(merged, config_dict)
         config_dict = merged
     return cls(config_dict)
Esempio n. 16
0
 def test_disjoint_update(self):
     a = {"a": 1, "b": 2}
     b = {"c": 3}
     expected = {"a": 1, "b": 2, "c": 3}
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 17
0
 def test_subset_merge(self):
     a = {"a": 1, "b": 2}
     b = {"a": 3}
     expected = {"a": 3, "b": 2}
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 18
0
 def test_overrides(self):
     a = {"a": 1, "b": 2}
     b = {"b": {"c": 3}}
     expected = {"a": 1, "b": {"c": 3}}
     merge_configs(a, b)
     ntools.assert_equal(a, expected)
Esempio n. 19
0
 def test_partial_update(self):
     a = {"a": 1, "b": 2}
     b = {"a": 3, "c": 4}
     expected = {"a": 3, "b": 2, "c": 4}
     merge_configs(a, b)
     ntools.assert_equal(a, expected)