Relevancy Implementations
RankRelevancyWithMarginSampledFeedback
- class smqtk_relevancy.impls.rank_relevancy.margin_sampling.RankRelevancyWithMarginSampledFeedback(*args: Any, **kwargs: Any)[source]
Wrap an instance of
RankRelevancyto provide feedback via margin sampling- Parameters
rank_relevancy –
RankRelevancyto use for computing relevancy scoresn – Maximum number of items to return for feedback
center – Value for which pool items whose relevancy score is closest to it will be returned for feedback (default: 0.5)
- Raises
ValueError – n is negative
- classmethod from_config(config_dict: Dict[str, Any], merge_default: bool = True) T[source]
Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments.
This base method is adequate without modification when a class’s constructor argument types are JSON-compliant. If one or more are not, however, this method then needs to be overridden in order to convert from a JSON-compliant stand-in into the more complex object the constructor requires. It is recommended that when complex types are used they also inherit from the
Configurablein order to hopefully make easier the conversion to and from JSON-compliant stand-ins.When this method does need to be overridden, this usually looks like the following pattern:
D = TypeVar("D", bound="MyClass") class MyClass (Configurable): @classmethod def from_config( cls: Type[D], config_dict: Dict, merge_default: bool = True ) -> D: # Perform a shallow copy of the input ``config_dict`` which # is important to maintain idempotency. config_dict = dict(config_dict) # Optionally guarantee default values are present in the # configuration dictionary. This is useful when the # configuration dictionary input is partial and the logic # contained here wants to use config parameters that may # have defaults defined in the constructor. if merge_default: config_dict = merge_dict(cls.get_default_config(), config_dict) # # Perform any overriding of `config_dict` values here. # # Create and return an instance using the super method. return super().from_config(config_dict, merge_default=merge_default)
Note on type annotations: When defining a sub-class of configurable and override this class method, we will need to defined a new TypeVar that is bound at the new class type. This is because super requires a type to be given that descends from the implementing type. If C is used as defined in this interface module, which is upper-bounded on the base
Configurableclass, the type analysis will see that we are attempting to invoke super with a type that may not strictly descend from the implementing type (MyClassin the example above), and cause an error during type analysis.- Parameters
config_dict (dict) – JSON compliant dictionary encapsulating a configuration.
merge_default (bool) – Merge the given configuration on top of the default provided by
get_default_config.
- Returns
Constructed instance from the provided config.
- get_config() Dict[str, Any][source]
Return a JSON-compliant dictionary that could be passed to this class’s
from_configmethod to produce an instance with identical configuration.In the most cases, 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. In some cases, where it doesn’t make sense to store some object constructor parameters are expected to be supplied at as configuration values (i.e. must be supplied at runtime), this method’s returned dictionary may leave those parameters out. In such cases, the object’s
from_configclass-method would also take additional positional arguments to fill in for the parameters that this returned configuration lacks.- Returns
JSON type compliant configuration dictionary.
- Return type
dict
- classmethod get_default_config() Dict[str, Any][source]
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.
- Returns
Default configuration dictionary for the class.
- Return type
dict
>>> # noinspection PyUnresolvedReferences >>> class SimpleConfig(Configurable): ... def __init__(self, a=1, b='foo'): ... self.a = a ... self.b = b ... def get_config(self): ... return {'a': self.a, 'b': self.b} >>> self = SimpleConfig() >>> config = self.get_default_config() >>> assert config == {'a': 1, 'b': 'foo'}
RankRelevancyWithSupervisedClassifier
- class smqtk_relevancy.impls.rank_relevancy.wrap_classifier.RankRelevancyWithSupervisedClassifier(*args: Any, **kwargs: Any)[source]
Relevancy ranking that utilizes a usable supervised classifier for on-the-fly training and inference.
While the name of this class merely states “supervised classifier,” we specifically utilize the interface for descriptor classification as opposed to the interfaces for other modalities (like images).
# Classifier “cloning” The input supervised classifier instance to the constructor is not directly used, but its type and configuration are recorded in order to create a new instance in
rankto train and classify the index.The caveat here is that any non-configuration reflected, runtime modifications to the input classifier will not be reflected by the classifier used in
rank.Using a copy of the input classifier allows the
rankmethod to be used in parallel without blocking other calls torank.- Parameters
classifier_inst – Supervised classifier instance to base the ephemeral ranking classifier on. The type and configuration of this classifier is used to create a clone at rank time. The input classifier instance is not modified.
- classmethod from_config(config_dict: Dict[str, Any], merge_default: bool = True) T[source]
Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments.
This base method is adequate without modification when a class’s constructor argument types are JSON-compliant. If one or more are not, however, this method then needs to be overridden in order to convert from a JSON-compliant stand-in into the more complex object the constructor requires. It is recommended that when complex types are used they also inherit from the
Configurablein order to hopefully make easier the conversion to and from JSON-compliant stand-ins.When this method does need to be overridden, this usually looks like the following pattern:
D = TypeVar("D", bound="MyClass") class MyClass (Configurable): @classmethod def from_config( cls: Type[D], config_dict: Dict, merge_default: bool = True ) -> D: # Perform a shallow copy of the input ``config_dict`` which # is important to maintain idempotency. config_dict = dict(config_dict) # Optionally guarantee default values are present in the # configuration dictionary. This is useful when the # configuration dictionary input is partial and the logic # contained here wants to use config parameters that may # have defaults defined in the constructor. if merge_default: config_dict = merge_dict(cls.get_default_config(), config_dict) # # Perform any overriding of `config_dict` values here. # # Create and return an instance using the super method. return super().from_config(config_dict, merge_default=merge_default)
Note on type annotations: When defining a sub-class of configurable and override this class method, we will need to defined a new TypeVar that is bound at the new class type. This is because super requires a type to be given that descends from the implementing type. If C is used as defined in this interface module, which is upper-bounded on the base
Configurableclass, the type analysis will see that we are attempting to invoke super with a type that may not strictly descend from the implementing type (MyClassin the example above), and cause an error during type analysis.- Parameters
config_dict (dict) – JSON compliant dictionary encapsulating a configuration.
merge_default (bool) – Merge the given configuration on top of the default provided by
get_default_config.
- Returns
Constructed instance from the provided config.
- get_config() Dict[str, Any][source]
Return a JSON-compliant dictionary that could be passed to this class’s
from_configmethod to produce an instance with identical configuration.In the most cases, 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. In some cases, where it doesn’t make sense to store some object constructor parameters are expected to be supplied at as configuration values (i.e. must be supplied at runtime), this method’s returned dictionary may leave those parameters out. In such cases, the object’s
from_configclass-method would also take additional positional arguments to fill in for the parameters that this returned configuration lacks.- Returns
JSON type compliant configuration dictionary.
- Return type
dict
- classmethod get_default_config() Dict[str, Any][source]
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.
- Returns
Default configuration dictionary for the class.
- Return type
dict
>>> # noinspection PyUnresolvedReferences >>> class SimpleConfig(Configurable): ... def __init__(self, a=1, b='foo'): ... self.a = a ... self.b = b ... def get_config(self): ... return {'a': self.a, 'b': self.b} >>> self = SimpleConfig() >>> config = self.get_default_config() >>> assert config == {'a': 1, 'b': 'foo'}
- rank(pos: Sequence[numpy.ndarray], neg: Sequence[numpy.ndarray], pool: Sequence[numpy.ndarray]) Sequence[float][source]
Assign a relevancy score to each input descriptor in pool based on the positively and negatively adjudicated descriptors in pos and neg respectively.
- Parameters
pos – Sequence of positively adjudicated descriptor vectors.
neg – Sequence of negatively adjudicated descriptor vectors.
pool – A sequence of descriptor vectors that we want to rank by topical relevancy relative to the given positive and negative examples.
- Returns
An ordered sequence of float values denoting the relevancy of pool elements
SupervisedClassifierRelevancyIndex
[Deprecated] Uses the deprecated RelevancyIndex interface
LibSvmHikRelevancyIndex
[Deprecated] Uses the deprecated RelevancyIndex interface