Auxiliary Scripts

Here you can find a description of all the scripts that provide supporting functionality for the cognitive nodes. These auxiliary modules implement common tools and abstractions which are used in this type of nodes.

Space

Python script to implement a space used by P-Nodes and Goals, which discretizes a continuous perceptual domain. Different types of spaces have been implemented, depending on the system used to determine whether a new perception belongs to an existing perceptual class:

  • PointBased Space: Uses rules that relate the position of the new perceptionto the closest old ones contained in the space.

  • SVM Space: Uses Support Vector Machines (SVMs), with Scikit-learn.

  • ANN Space: Uses Artificial Neural Networks (ANNs), with Tensorflow.

class cognitive_nodes.space.ANNSpace(**kwargs)[source]

Bases: PointBasedSpace

Use and train a Neural Network to calculate the activations.

add_point(perception, confidence)[source]

Add a new point to the P-Node.

Parameters:
  • perception (dict) – A given perception to add.

  • confidence (float) – The confidence of the added point that specifies if it is a point or an antipoint.

Returns:

The position of the added point.

Return type:

int

build_model(input_shape)[source]

Build the model with the given input shape.

Parameters:

input_shape (tuple) – The shape of the input data.

get_probability(perception)[source]

Calculate the new activation value.

Parameters:

perception (dict) – The given perception to calculate the activation.

Returns:

The activation value.

Return type:

float

save_model(path)[source]

Save the trained model to the specified path.

Parameters:

path (str) – The file path where the model should be saved.

class cognitive_nodes.space.ActivatedDummySpace(size=30000, **kwargs)[source]

Bases: PointBasedSpace

A dummy space that always returns an activation of 1.0 for any perception.

add_point(perception, confidence)[source]

Dummy method to add a point to the space. This method does not actually add any points.

Parameters:
  • perception (dict) – A given perception to add. It is not used.

  • confidence (float) – The confidence of the added point. Irrelevant in this case.

Returns:

-1

Return type:

int

get_probability(perception)[source]

Activation value is always 1.0.

Parameters:

perception (dict) – A given perception to add. It is not used.

Returns:

The activation value, which is always 1.0.

Return type:

float

class cognitive_nodes.space.CentroidPointBasedSpace(size=30000, **kwargs)[source]

Bases: PointBasedSpace

Calculate the new activation value.

This activation value is for a given perception and it is calculated as follows: - Calculate the closest point to the new point. - If the closest point has a positive membership, the membership of the new point is that divided by the distance between them. - Otherwise: * Calculate the centroid of points with a positive membership. * If the distance from the new point to the centroid is less than the distance from the closest point to the centroid, then the activation is calculated as before but using the closest point with positive membership. Otherwise the activation is -1.

get_probability(perception)[source]

Calculate the new activation value.

Parameters:

perception (dict) – The given perception to calculate the activation.

Returns:

The activation value.

Return type:

float

class cognitive_nodes.space.ClosestPointBasedSpace(size=30000, **kwargs)[source]

Bases: PointBasedSpace

Calculate the new activation value.

This activation value is for a given perception and it is calculated as follows: - Calculate the closest point to the new point. - If the closest point has a positive membership, the membership of the new point is that divided by the distance between them. Otherwise, the activation is -1.

get_probability(perception)[source]

Calculate the new activation value.

Parameters:

perception (dict) – The given perception to calculate the activation.

Returns:

The activation value.

Return type:

float

class cognitive_nodes.space.NormalCentroidPointBasedSpace(size=30000, **kwargs)[source]

Bases: PointBasedSpace

Calculate the new activation value.

This activation value is for a given perception and it is calculated as follows: - Calculate the closest point to the new point. - If the closest point has a positive membership, the membership of the new point is that divided by the distance between them. - Otherwise: * Calculate the centroid of points with a positive membership. * If the distance from the new point to the centroid is less than the distance from the closest point to the centroid, or the distance of the closest point to the line that goes from the new point to the centroid is high (see source code), then the activation is calculated as before but using the closest point with positive membership, otherwise the activation is -1.

get_probability(perception)[source]

Calculate the new activation value.

Parameters:

perception (dict) – The given perception to calculate the activation.

Returns:

The activation value.

Return type:

float

class cognitive_nodes.space.PointBasedSpace(size=30000, **kwargs)[source]

Bases: Space

A state space based on points.

add_point(perception, confidence)[source]

Add a new point to the P-Node.

Parameters:
  • perception (dict) – A given perception to add.

  • confidence (float) – The confidence of the added point that specifies if it is a point or an antipoint.

Raises:

RuntimeError – If LTM operation cannot continue.

Returns:

The position of the added point.

Return type:

int

aging()[source]

Move towards zero the activation for every point or anti-point.

contains(space, threshold=0.9)[source]

Check if other space is contained inside this one. That happens if this space has a given value of probability for every point belonging to the other space.

Parameters:
  • space (cognitive_nodes.Space) – Space that is checked if it is included.

  • threshold (float) – Minimum probability value.

Returns:

Indicates whether the space is contained or not.

Return type:

bool

static copy_perception(space, position, perception)[source]

Copy a perception to a structured array.

Parameters:
  • space (numpy.ndarray) – An structured array, filled with zeros.

  • position (int) – Position of the array in which the perception is added.

  • perception (dict) – The perception that is copied in the structured array.

static create_point_from_labels(labels)[source]

Generates a point from a list of labels.

Parameters:

labels (list) – List of labels of the space.

Returns:

Space point.

Return type:

dict

create_structured_array(perception, base_dtype, size)[source]

Create a structured array to store points.

The key is what fields to use. There are three cases: - If base_dtype is specified, use the fields in perception that are also in base_dtype. - Otherwise, if this space is a specialization, use the fields in perception that are NOT in parent_space. - Otherwise, use every field in perception.

Parameters:
  • perception (dict) – The perception that sizes the structured array.

  • base_dtype (numpy.dtype) – The dtype of the structured array.

  • size (int) – The size of the structured array.

Returns:

The structured array, filled with zeros.

Return type:

numpy.ndarray

static get_closest_point_and_antipoint_info(members, memberships, foreigner)[source]

Obtain info about the closest point and antipoint for a given foreigner.

Parameters:
  • members (numpy.ndarray) – Set of the points and antipoints.

  • memberships (numpy.ndarray) – The confidence of the points contained in members.

  • foreigner (numpy.ndarray) – The given foreigner point in order to obtain the info.

Returns:

The position of in the members array the closest point and antipoints and their distance with the foreigner point.

Return type:

int (position), float (distance)

get_probability(perception)[source]

Calculate the new activation value.

Parameters:

perception (dict) – The given perception to calculate the activation.

Raises:

NotImplementedError – The method has to be implemented in a child class.

learnable()[source]

Only antipoints are considered learnables.

Returns:

Return if the perception (point) is learnable or not.

Return type:

bool

populate_space(labels, members, memberships)[source]

Populate the structured array and memberships list based on the given parameters.

Parameters:
  • point (dict) – A perception dictionary describing the structure of the space.

  • members (list) – A flattened list of data with size n_dims * n_data.

  • memberships (list) – A list of membership data with size n_data.

Raises:

ValueError – If the size of memberships does not match the calculated size of the space.

prune(space)[source]

Prune sensors that are present only in this space or in the space given for comparison.

Parameters:

space (cognitive_nodes.Space) – The given space.

same_sensors(space)[source]

Check if other space has exactly the same sensors that this one.

Parameters:

space (cognitive_nodes.Space) – The space to check.

Returns:

Indicates whether the space has the same sensors or not.

Return type:

bool

specialize(space=None)[source]

Return a new space with those fields that are in r”space” and not in r”self”.

Parameters:

space (cognitive_nodes.Space) – Space used to specialize.

Returns:

The new space.

Return type:

cognitive_nodes.Space

class cognitive_nodes.space.SVMSpace(**kwargs)[source]

Bases: PointBasedSpace

Use a SVM to calculate activations.

add_point(perception, confidence)[source]

Add a new point to the P-Node.

Parameters:
  • perception (dict) – A given perception to add.

  • confidence (float) – The confidence of the added point that specifies if it is a point or an antipoint.

Returns:

The position of the added point.

Return type:

int

fit_and_score()[source]

Fit and score the SVM Model.

Returns:

The score of the model.

Return type:

float

get_probability(perception)[source]

Calculate the new activation value.

Parameters:

perception (dict) – The given perception to calculate the activation.

Returns:

The activation value.

Return type:

float

prune_points(score, memberships)[source]

Prune points depending on the model score obtained.

Parameters:
  • score (float) – Score that determines the pruning.

  • memberships (numpy.ndarray) – The confidence of the points.

remove_close_points()[source]

Remove points that are too close in space.

class cognitive_nodes.space.Space(ident=None, random_seed=0, **kwargs)[source]

Bases: object

A n-dimensional state space.

Utils

Python script which implements auxiliary functions used into the scripts of the cognitive nodes.

class cognitive_nodes.utils.EpisodeSubscription[source]

Bases: object

EpisodeSubscription is a mixin class that provides a method to configure a subscription to the episodes.

configure_episode_subscription(episode_topic, episode_msg, callback_group)[source]

Configure the subscription to the episodes.

Parameters:
  • episode_topic (str) – Name of the topic where the episodes are published.

  • episode_msg (str) – Message type of the episodes.

episode_callback(msg)[source]

Callback that processes the episodes.

Parameters:

msg (ROS2 message. Typically cognitive_process_interfaces.msg.Episode) – Episode message.

Raises:

NotImplementedError – Method must be implemented in the subclass.

class cognitive_nodes.utils.LTMSubscription[source]

Bases: object

LTMSubscription is a mixin class that provides a method to configure a subscription to the LTM.

configure_ltm_subscription(ltm, callback_group)[source]

Configure the subscription to the LTM.

Parameters:

ltm (str) – LTM ID.

ltm_change_callback(msg)[source]

Callback that processes the LTM message.

Parameters:

msg (std_msgs.msg.String) – Message from the LTM.

read_ltm(ltm_dump)[source]

Placeholder for LTM processing.

Parameters:

ltm_dump (dict) – Dictionary with the data from the LTM

Raises:

NotImplementedError – Method must be implemented in the subclass.

class cognitive_nodes.utils.PNodeSuccess[source]

Bases: LTMSubscription

PNodeSuccess is a mixin class that provides a method to configure a subscription to the success rate of the P-Nodes.

configure_pnode_success(ltm, callback_group)[source]

Configure the subscription to the success rate of the P-Nodes.

Parameters:

ltm (str) – LTM id.

pnode_success_callback(msg: cognitive_node_interfaces.msg.SuccessRate)[source]

Callback that processes the success rate of a P-Node.

Parameters:

msg (SuccessRate) – Success rate message.

read_ltm(ltm_dump)[source]

Method that processes the LTM data and subscribes to the success rate of the P-Nodes.

Parameters:

ltm_dump (str) – Dictionary with the data from the LTM.