def from_config(cls, config, parent_app): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. :param config: JSON compliant dictionary encapsulating a configuration. :type config: dict :param parent_app: Parent containing flask app instance :type parent_app: smqtk.web.search_app.app.search_app :return: Constructed instance from the provided config. :rtype: IqrSearch """ merged = cls.get_default_config() merged.update(config) # construct nested objects via configurations merged['data_set'] = \ plugin.from_plugin_config(merged['data_set'], get_data_set_impls()) merged['descr_generator'] = \ plugin.from_plugin_config(merged['descr_generator'], get_descriptor_generator_impls()) merged['nn_index'] = \ plugin.from_plugin_config(merged['nn_index'], get_nn_index_impls()) merged['descriptor_factory'] = \ DescriptorElementFactory.from_config(merged['descriptor_factory']) return cls(parent_app, **merged)
def from_config(cls, config, parent_app): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. :param config: JSON compliant dictionary encapsulating a configuration. :type config: dict :param parent_app: Parent containing flask app instance :type parent_app: smqtk.web.search_app.app.search_app :return: Constructed instance from the provided config. :rtype: IqrSearch """ merged = cls.get_default_config() merged.update(config) # construct nested objects via configurations merged['data_set'] = \ plugin.from_plugin_config(merged['data_set'], get_data_set_impls()) merged['descr_generator'] = \ plugin.from_plugin_config(merged['descr_generator'], get_descriptor_generator_impls()) merged['nn_index'] = \ plugin.from_plugin_config(merged['nn_index'], get_nn_index_impls()) merged['descriptor_factory'] = \ DescriptorElementFactory.from_config(merged['descriptor_factory']) return cls(parent_app, **merged)
def get_default_config(cls): d = super(IqrSearch, cls).get_default_config() # Remove parent_app slot for later explicit specification. del d['parent_app'] # fill in plugin configs d['data_set'] = plugin.make_config(get_data_set_impls()) d['descr_generator'] = \ plugin.make_config(get_descriptor_generator_impls()) d['nn_index'] = plugin.make_config(get_nn_index_impls()) ri_config = plugin.make_config(get_relevancy_index_impls()) if d['rel_index_config']: ri_config.update(d['rel_index_config']) d['rel_index_config'] = ri_config df_config = DescriptorElementFactory.get_default_config() if d['descriptor_factory']: df_config.update(d['descriptor_factory'].get_config()) d['descriptor_factory'] = df_config return d
def get_default_config(cls): d = super(IqrSearch, cls).get_default_config() # Remove parent_app slot for later explicit specification. del d['parent_app'] # fill in plugin configs d['data_set'] = plugin.make_config(get_data_set_impls()) d['descr_generator'] = \ plugin.make_config(get_descriptor_generator_impls()) d['nn_index'] = plugin.make_config(get_nn_index_impls()) ri_config = plugin.make_config(get_relevancy_index_impls()) if d['rel_index_config']: ri_config.update(d['rel_index_config']) d['rel_index_config'] = ri_config df_config = DescriptorElementFactory.get_default_config() if d['descriptor_factory']: df_config.update(d['descriptor_factory'].get_config()) d['descriptor_factory'] = df_config return d
def get_descriptor_inst(self, label): """ Get the cached content descriptor instance for a configuration label :type label: str :rtype: smqtk.descriptor_generator.DescriptorGenerator """ with self.descriptor_cache_lock: if label not in self.descriptor_cache: self._log.debug("Caching descriptor '%s'", label) self.descriptor_cache[label] = \ plugin.from_plugin_config( self.generator_label_configs[label], get_descriptor_generator_impls() ) return self.descriptor_cache[label]
def get_descriptor_inst(self, label): """ Get the cached content descriptor instance for a configuration label :type label: str :rtype: smqtk.descriptor_generator.DescriptorGenerator """ with self.descriptor_cache_lock: if label not in self.descriptor_cache: self._log.debug("Caching descriptor '%s'", label) self.descriptor_cache[label] = \ plugin.from_plugin_config( self.generator_label_configs[label], get_descriptor_generator_impls() ) return self.descriptor_cache[label]
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_dict(c, { "descriptor_factory": DescriptorElementFactory.get_default_config(), "descriptor_generators": { "example": plugin.make_config(get_descriptor_generator_impls()) } }) return c
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_dict(c, { "descriptor_factory": DescriptorElementFactory.get_default_config(), "descriptor_generators": { "example": plugin.make_config(get_descriptor_generator_impls()) } }) return c
def main(): parser = cli_parser() args = parser.parse_args() config = bin_utils.utility_main_helper(default_config, args) log = logging.getLogger(__name__) output_filepath = args.output_filepath overwrite = args.overwrite if not args.input_file: log.error("Failed to provide an input file path") exit(1) elif not os.path.isfile(args.input_file): log.error("Given path does not point to a file.") exit(1) input_filepath = args.input_file data_element = DataFileElement(input_filepath) factory = DescriptorElementFactory.from_config( config['descriptor_factory']) #: :type: smqtk.algorithms.descriptor_generator.DescriptorGenerator cd = plugin.from_plugin_config(config['content_descriptor'], get_descriptor_generator_impls()) descr_elem = cd.compute_descriptor(data_element, factory, overwrite) vec = descr_elem.vector() if vec is None: log.error("Failed to generate a descriptor vector for the input data!") if output_filepath: numpy.save(output_filepath, vec) else: # Construct string, because numpy s = [] # noinspection PyTypeChecker for f in vec: s.append('%15f' % f) print(' '.join(s))
def test_get_descriptors(self): m = get_descriptor_generator_impls() # Currently no types that are guaranteed available self.assertIsInstance(m, dict, "Should return a dictionary of " "class label-to-types")
def test_impl_findable(self): nose.tools.assert_in(CaffeDescriptorGenerator.__name__, get_descriptor_generator_impls())
def test_get_descriptors(): m = get_descriptor_generator_impls() # Currently no types that are guaranteed available ntools.assert_is_instance( m, dict, "Should return a dictionary of class " "label-to-types")
def default_config(): return { "descriptor_factory": DescriptorElementFactory.get_default_config(), "content_descriptor": plugin.make_config(get_descriptor_generator_impls()), }
def test_impl_findable(self): ntools.assert_in(ColorDescriptor_Image_csift.__name__, get_descriptor_generator_impls())
def test_impl_findable(self): self.assertIn(CaffeDescriptorGenerator.__name__, get_descriptor_generator_impls())
def default_config(): return { "descriptor_factory": DescriptorElementFactory.get_default_config(), "content_descriptor": plugin.make_config(get_descriptor_generator_impls()), }
def test_impl_findable(self): self.assertIn(CaffeDescriptorGenerator.__name__, get_descriptor_generator_impls())
def main(): parser = cli_parser() args = parser.parse_args() output_filepath = args.output_filepath overwrite = args.overwrite verbose = args.verbose llevel = logging.DEBUG if verbose else logging.INFO bin_utils.initialize_logging(logging.getLogger(), llevel) log = logging.getLogger("main") # Merge loaded config with default config_loaded = False config = default_config() if args.config: if os.path.isfile(args.config): with open(args.config, 'r') as f: config.update(json.load(f)) config_loaded = True elif not os.path.isfile(args.config): log.error("Configuration file path not valid.") exit(1) bin_utils.output_config(args.output_config, config, log, True) # Configuration must have been loaded at this point since we can't normally # trust the default. if not config_loaded: log.error("No configuration provided") exit(1) if not args.input_file: log.error("Failed to provide an input file path") exit(1) elif not os.path.isfile(args.input_file): log.error("Given path does not point to a file.") exit(1) input_filepath = args.input_file data_element = DataFileElement(input_filepath) factory = DescriptorElementFactory.from_config(config['descriptor_factory']) #: :type: smqtk.algorithms.descriptor_generator.DescriptorGenerator cd = plugin.from_plugin_config(config['content_descriptor'], get_descriptor_generator_impls()) descr_elem = cd.compute_descriptor(data_element, factory, overwrite) vec = descr_elem.vector() if vec is None: log.error("Failed to generate a descriptor vector for the input data!") if output_filepath: numpy.save(output_filepath, vec) else: # Construct string, because numpy s = [] # noinspection PyTypeChecker for f in vec: s.append('%15f' % f) print ' '.join(s)
def test_impl_findable(self): self.assertIn(ColorDescriptor_Image_csift.__name__, get_descriptor_generator_impls())
def test_get_descriptors(): m = get_descriptor_generator_impls() # Currently no types that are guaranteed available ntools.assert_is_instance(m, dict, "Should return a dictionary of class " "label-to-types")