so_magic.data.features package

Submodules

so_magic.data.features.features module

class so_magic.data.features.features.AttributeReporter(label, reporter=<so_magic.data.features.features.BaseAttributeReporter object>)[source]

Bases: object

value_set(datapoints)[source]
values(datapoints)[source]

A default implementation of the values method

variable_type(datapoints)[source]

A default implementation of the values method

class so_magic.data.features.features.AttributeReporterInterface[source]

Bases: abc.ABC

A class implementing this interface has the ability to report information on an attribute/variable of some structured data (observations)

abstract value_set(datapoints, attribute, **kwargs)[source]
abstract values(datapoints, attribute, **kwargs)[source]

Get the values ([N x 1] vector) of all datapoints (N x D) corresponding to the input variable/attribute.

Parameters
  • datapoints (Datapoints) – [description]

  • attribute (str) – [description]

Returns

the values in a [N x 1] vector

Return type

(numpy.ndarray)

abstract variable_type(datapoints, attribute, **kwargs)[source]

Call to get the variable type of the datapoints, given the attribute.

Parameters
  • datapoints (Datapoints) – [description]

  • attribute (str) – [description]

Returns

[description]

Return type

(str)

class so_magic.data.features.features.BaseAttributeReporter[source]

Bases: so_magic.data.features.features.AttributeReporterInterface

value_set(datapoints, attribute, **kwargs)[source]
values(datapoints, attribute, **kwargs)[source]

Get the values ([N x 1] vector) of all datapoints (N x D) corresponding to the input variable/attribute.

Parameters
  • datapoints (Datapoints) – [description]

  • attribute (str) – [description]

Returns

the values in a [N x 1] vector

Return type

(numpy.ndarray)

variable_type(datapoints, attribute, **kwargs)[source]

Call to get the variable type of the datapoints, given the attribute.

Parameters
  • datapoints (Datapoints) – [description]

  • attribute (str) – [description]

Returns

[description]

Return type

(str)

class so_magic.data.features.features.FeatureFunction(function, label=None)[source]

Bases: object

Example: Assume we have a datapoint v = [v_1, v_2, .., v_n, and 2 feature functions f_1, f_2

Then we can produce an encoded vector (eg to feed for training a ML model) like: encoded_vector = [f_1(v), f_2(v)]

is_label(_attribute, value)[source]
property state
values(dataset)[source]
class so_magic.data.features.features.FeatureIndex(keys)[source]

Bases: object

class so_magic.data.features.features.FeatureState(key, reporter)[source]

Bases: object

class so_magic.data.features.features.PhiFeatureFunction[source]

Bases: object

class so_magic.data.features.features.StateMachine(states, init_state)[source]

Bases: object

property current
property state

Construct an object representing the current state

update(*args, **kwargs)[source]
class so_magic.data.features.features.TrackingFeature(feature, state_machine, variable_type=None)[source]

Bases: object

classmethod from_callable(a_callable, label=None, variable_type=None)[source]

Construct a feature that has one extract/report capability. Input id is correlated to the features position on the vector (see FeatureFunction above)

label()[source]
property state

Returns the current state

update(*args, **kwargs)[source]
values(dataset)[source]
so_magic.data.features.features.is_callable(_self, _attribute, value)[source]

so_magic.data.features.features_set module

class so_magic.data.features.features_set.BaseFeatureSet(features=[])[source]

Bases: object

classmethod from_raw_extractors(data)[source]

Create a Feature for each of the lists in the input data (list). Inner lists must satisfy 0 < len(l)

class so_magic.data.features.features_set.FeatureConfiguration(variables, feature_vectors=NOTHING)[source]

Bases: so_magic.utils.notification.Observer

update(subject: so_magic.utils.notification.Subject)None[source]

Receive an update (from a subject); handle an event notification.

valid_encoding(feature)[source]
property valid_variables
class so_magic.data.features.features_set.FeatureManager(feature_configuration, subject=<so_magic.utils.notification.Subject object>)[source]

Bases: object

property feature_configuration
class so_magic.data.features.features_set.FeatureSet(features=[])[source]

Bases: so_magic.data.features.features_set.BaseFeatureSet

property binnable
property encoded
property not_encoded

so_magic.data.features.phi module

This module is responsible to provide a formal way of registering phi functions at runtime. See the ‘PhiFunctionRegistrator’ class and its ‘register’ decorator method

class so_magic.data.features.phi.PhiFunctionMetaclass(*args, **kwargs)[source]

Bases: type

Class type with a single broadcasting (notifies listeners) facility.

Classes using this class as metaclass, obtain a single broadcasting facility as a class attribute. The class attribute is called ‘subject’ can be referenced as any class attribute.

Example

class MyExampleClass(metaclass=PhiFunctionMetaclass):

pass

instance_object_1 = MyExampleClass() instance_object_2 = MyExampleClass() assert id(MyExampleClass.subject) == id(instance_object_1.subject) == id(instance_object_2.subject)

exception so_magic.data.features.phi.PhiFunctionNameDeterminationError[source]

Bases: Exception

class so_magic.data.features.phi.PhiFunctionRegistrator[source]

Bases: object

Add phi functions to the registry and notify observers/listeners.

This class provides the ‘register’ decorator, that client can use to decorate either functions (defined with the def python special word), or classes (defined with the python class special word).

static get_name(a_callable: Callable)[source]

Get the ‘name’ of the input callable object

Parameters

a_callable (Callable) – a callable object to get its name

Returns

the name of the callable object

Return type

str

classmethod register(phi_name='')[source]

Add a new phi function to phi function registry and notify listeners/observers.

Use this decorator around either a callable function (defined with the ‘def’ python special word) or a class with a takes-no-arguments (or all-optional-arguments) constructor and a __call__ magic method.

All phi functions are expected to be registered with a __name__ and a __doc__ attribute.

You can select your custom phi_name under which to register the phi function or default to an automatic determination of the phi_name to use.

Automatic determination of phi_name is done by examining either the __name__ attribute of the function or the class name of the class.

Example

>>> from so_magic.data.features.phi import PhiFunctionRegistrator
>>> from so_magic.utils import Observer, ObjectRegistry
>>> class PhiFunctionRegistry(Observer):
...  def __init__(self):
...   self.registry = ObjectRegistry()
...  def update(self, subject, *args, **kwargs):
...   self.registry.add(subject.name, subject.state)
>>> phis = PhiFunctionRegistry()
>>> PhiFunctionRegistrator.subject.add(phis)
>>> @PhiFunctionRegistrator.register()
... def f1(x):
...  '''Multiply by 2.'''
...  return x * 2
Registering input function f1 as phi function, at key f1.
>>> phis.registry.get('f1').__doc__
'Multiply by 2.'
>>> input_value = 5
>>> print(f"{input_value} * 2 = {phis.registry.get('f1')(input_value)}")
5 * 2 = 10
>>> @PhiFunctionRegistrator.register()
... class f2:
...  def __call__(self, data, **kwargs):
...   return data + 5
Registering input class f2 instance as phi function, at key f2.
>>> input_value = 1
>>> print(f"{input_value} + 5 = {phis.registry.get('f2')(input_value)}")
1 + 5 = 6
>>> @PhiFunctionRegistrator.register('f3')
... class MyCustomClass:
...  def __call__(self, data, **kwargs):
...   return data + 1
Registering input class MyCustomClass instance as phi function, at key f3.
>>> input_value = 3
>>> print(f"{input_value} + 1 = {phis.registry.get('f3')(input_value)}")
3 + 1 = 4
Parameters

phi_name (str, optional) – custom name to register the phi function. Defaults to automatic computation.

subject = <so_magic.utils.notification.Subject object>

so_magic.data.features.phis module

class so_magic.data.features.phis.DatapointsAttributePhi(datapoints)[source]

Bases: object

class so_magic.data.features.phis.ListOfCategoricalPhi(datapoints_attribute_phi)[source]

Bases: object

property attribute

Module contents

class so_magic.data.features.FeatureManager(feature_configuration, subject=<so_magic.utils.notification.Subject object>)[source]

Bases: object

property feature_configuration