Functions

Functions that form the elements of a perceptual control node (system).

Overview

Each function outputs the result of applying the function logic and may be a scalar or array, depending on the input. The inputs are supplied as links to another function, from where the values are read. Unless the function is a simple case, such as a ‘Constant’.

Parameters are supplied as arguments to the constructor of the class.

All functions include the methods defined by the BaseFunction class.


source

HPCTFUNCTION

 HPCTFUNCTION (value, names=None, module=None, qualname=None, type=None,
               start=1)

Types of control functions in a node.


source

BaseFunction

 BaseFunction (name=None, value=None, links=None, new_name=True,
               namespace=None)

Base class of a PCT function. This class is not used directly by developers, but defines the functionality common to all.


source

FunctionFactory

 FunctionFactory ()

Initialize self. See help(type(self)) for accurate signature.

#show_doc(BaseFunction.summary)
#show_doc(BaseFunction.get_config)

source

Subtract

 Subtract (value=0, name='subtract', links=None, new_name=True,
           namespace=None, **cargs)

A function that subtracts one value from another. Parameter: None. Links: Two links required to each the values to be subtracted.


source

Proportional

 Proportional (gain=1, value=0, name='proportional', links=None,
               new_name=True, namespace=None, **cargs)

A proportion of the input value as defined by the gain parameter. Parameters: The gain value. Links: One.


source

Variable

 Variable (value=0, name='variable', links=None, new_name=True,
           namespace=None, **cargs)

A function that returns a variable value. Parameter: The variable value. Links: None


source

PassOn

 PassOn (value=0, name='variable', links=None, new_name=True,
         namespace=None, **cargs)

A function that passes on a variable value from a linked function. Parameter: None. Links: One


source

GreaterThan

 GreaterThan (threshold=0, upper=1, lower=0, value=0, name='greaterthan',
              links=None, new_name=True, namespace=None, **cargs)

One of two supplied values is returned if the input is greater than supplied threshold.
Parameters: The threshold and upper and lower value. Links: One


source

Constant

 Constant (value=0, name='constant', new_name=True, namespace=None,
           **cargs)

A function that returns a constant value. Parameter: The constant value. Links: None


source

Step

 Step (upper=None, lower=None, delay=None, period=None, value=0,
       name='step', new_name=True, namespace=None, **cargs)

A function that returns an alternating signal. Parameter: The upper and lower values, and a delay value. Links: None


source

Integration

 Integration (gain=1, slow=2, value=0, name='integration', links=None,
              new_name=True, namespace=None, **cargs)

A leaky integrating function. Equivalent of a exponential smoothing function, of the amplified input. Parameters: The gain and slow values. Links: One.


source

IntegrationDual

 IntegrationDual (gain=1, slow=2, value=0, name='integration', links=None,
                  new_name=True, namespace=None, **cargs)

A leaky integrating function, applying one signal to another. Equivalent of a exponential smoothing function, of the amplified input. Parameters: The gain and slow values. Links: Two.


source

Sigmoid

 Sigmoid (range=2, slope=10, value=0, name='sigmoid', links=None,
          new_name=True, namespace=None, **cargs)

A sigmoid function. Similar to a proportional function, but kept within a limit (+/- half the range). Parameters: The range and slope values. Links: One.


source

WeightedSum

 WeightedSum (weights=[0], value=0, name='weighted_sum', links=None,
              new_name=True, usenumpy=False, namespace=None, **cargs)

A function that combines a set of inputs by multiplying each by a weight and then adding them up. Parameter: The weights array. Links: Links to all the input functions.


source

SmoothWeightedSum

 SmoothWeightedSum (weights=[0], smooth_factor=0.0, value=0,
                    name='smooth_weighted_sum', links=None, new_name=True,
                    usenumpy=False, namespace=None, **cargs)

A function that combines a set of inputs by multiplying each by a weight and then adding them up. And then smooths the result. Parameter: The weights array. Links: Links to all the input functions.


source

IndexedParameter

 IndexedParameter (index=None, value=0, name='indexed_parameter',
                   links=None, new_name=True, namespace=None, **cargs)

A function that returns a parameter from a linked function, indexed by number. Parameter: The index. Links: One.


source

SigmoidWeightedSum

 SigmoidWeightedSum (weights=[0], range=2.0, slope=10.0, value=0,
                     name='sigmoid_weighted_sum', links=None,
                     new_name=True, usenumpy=False, namespace=None,
                     **cargs)

A function that combines a set of inputs by multiplying each by a weight and then adding them up. And then limits the output by squashing with a sigmoid function. Parameter: The weights array. Links: Links to all the input functions.


source

SigmoidSmoothWeightedSum

 SigmoidSmoothWeightedSum (weights=[0], smooth_factor=0.0, range=2.0,
                           slope=10.0, value=0,
                           name='sigmoid_smooth_weighted_sum', links=None,
                           new_name=True, usenumpy=False, namespace=None,
                           **cargs)

A function that combines a set of inputs by multiplying each by a weight and then adding them up. It then smooths the result and then limits the output by squashing with a sigmoid function. Parameter: The weights array. Links: Links to all the input functions.


source

Derivative

 Derivative (history_length=1, value=0, name='derivative', links=None,
             new_name=True, usenumpy=False, namespace=None, **cargs)

A function that provides the difference to previous values of the input signal. Parameter: The weights array. Links: Links to all the input functions.


source

DerivativeWeightedSum

 DerivativeWeightedSum (weights=[0], history_length=1, value=0,
                        name='derivative_weighted_sum', links=None,
                        new_name=True, usenumpy=False, namespace=None,
                        **cargs)

A function that combines a set of inputs by multiplying each by a weight and then adding them up. And then takes the difference of with a past value. Parameter: The weights array. Links: Links to all the input functions.

Creating Functions

Standard class constructor. Different ways to create a function with the standard constructor.

prop = Proportional()
print(prop.get_config())
prop = Proportional("myprop", 10)
print(prop.get_config())
prop = Proportional(gain=10)
print(prop.get_config())
{'type': 'Proportional', 'name': 'proportional', 'value': 0, 'links': {}, 'gain': 1}
{'type': 'Proportional', 'name': 'proportional', 'value': 10, 'links': {}, 'gain': 'myprop'}
{'type': 'Proportional', 'name': 'proportional', 'value': 0, 'links': {}, 'gain': 10}

Configuration class constructor. Create the function by passing a configuration structure to the constructor.

prop = Proportional(**{'name': 'myprop', 'value': 5, 'gain': 20})
print(prop.get_config())
{'type': 'Proportional', 'name': 'myprop', 'value': 5, 'links': {}, 'gain': 20}

Configuration class method. Create the function by passing a configuration structure to a class method.

config = {'name': 'myprop', 'value': -0.5, 'gain': 21}
prop = Proportional.from_config(config)
print(prop.get_config())
{'type': 'Proportional', 'name': 'myprop', 'value': -0.5, 'links': {}, 'gain': 21}
prop = Proportional()
print(prop.get_config())
prop1 = Proportional.from_config(prop.get_config())
print(prop1.get_config())
assert prop.get_config() == prop1.get_config()
{'type': 'Proportional', 'name': 'proportional', 'value': 0, 'links': {}, 'gain': 1}
{'type': 'Proportional', 'name': 'proportional', 'value': 0, 'links': {}, 'gain': 1}

An example showing creating a WeightedSum function.

wts=[1,1,1]
ws = WeightedSum(weights=wts)
ns = ws.namespace
ws.add_link(Constant(10, namespace=ns))
ws.add_link(Constant(5, namespace=ns))
ws.add_link(Constant(20, namespace=ns))
assert ws() == 35
config = ws.get_config()
#ws1 = WeightedSum.from_config(config, namespace=ns)
ws1 = WeightedSum.from_config(config, new_name= 'weighted_sum1', namespace=ns)
ws1.get_config()
{'type': 'WeightedSum',
 'name': 'weighted_sum1',
 'value': 35,
 'links': {0: 'constant', 1: 'constant1', 2: 'constant2'},
 'weights': [1, 1, 1]}
scons = Constant(2, name='scons')
sig = Sigmoid()
sig.add_link(scons)
sig()
0.9999092042625952
cons = Constant([2,2], name='cons')
print(cons.output_string()+ "")
[2, 2]

Viewing Functions

View the details of the function with the “summary”, which prints the name, type, parameters, value and links (if any).

prop.summary()
proportional Proportional | gain 1 | 0 

As already seen the function details can be seen by retrieving the configuration.

print(prop.get_config())
{'type': 'Proportional', 'name': 'proportional', 'value': 0, 'links': {}, 'gain': 1}

Or you can print the function.

print(prop)
{'namespace': UUID('c2f442d0-fb5c-11ee-ae40-5c879c15de65'), 'value': 0, 'links': [], 'checklinks': True, 'name': 'proportional', 'decimal_places': 3, 'gain': 1}

Set the decimal places for output display.

print(prop.output_string())
prop.set_decimal_places(2)
print(prop.output_string())
0.000
0.00

You can also view a function graphically as a network of connected nodes.

sub = Subtract(links=[Constant(1, name='cons'), Proportional(10, name='prop')], name='sub')
print(sub.value)
g = sub.graph()
print(g)
sub.draw(node_size=2000)
0
DiGraph with 3 nodes and 2 edges

Save and Load

Save a function to file.

import json
print(ws.get_config())
ws.save("ws.json")
{'type': 'WeightedSum', 'name': 'weighted_sum', 'value': 35, 'links': {0: 'constant', 1: 'constant1', 2: 'constant2'}, 'weights': [1, 1, 1]}

Create a function from file.

wss = WeightedSum.load("ws.json", new_name='weighted_sum1', namespace=ns)
print(ws.get_config())
print(wss.get_config())
assert wss.get_config() == {'type': 'WeightedSum', 'name': 'weighted_sum2', 'value': 35, 'links': {0: 'constant', 1: 'constant1', 2: 'constant2'}, 'weights': [1, 1, 1]}
{'type': 'WeightedSum', 'name': 'weighted_sum', 'value': 35, 'links': {0: 'constant', 1: 'constant1', 2: 'constant2'}, 'weights': [1, 1, 1]}
{'type': 'WeightedSum', 'name': 'weighted_sum2', 'value': 35, 'links': {0: 'constant', 1: 'constant1', 2: 'constant2'}, 'weights': [1, 1, 1]}