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:
PointBasedSpaceUse 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.
- class cognitive_nodes.space.ActivatedDummySpace(size=30000, **kwargs)[source]
Bases:
PointBasedSpaceA 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
- class cognitive_nodes.space.CentroidPointBasedSpace(size=30000, **kwargs)[source]
Bases:
PointBasedSpaceCalculate 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.
- class cognitive_nodes.space.ClosestPointBasedSpace(size=30000, **kwargs)[source]
Bases:
PointBasedSpaceCalculate 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.
- class cognitive_nodes.space.NormalCentroidPointBasedSpace(size=30000, **kwargs)[source]
Bases:
PointBasedSpaceCalculate 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.
- class cognitive_nodes.space.PointBasedSpace(size=30000, **kwargs)[source]
Bases:
SpaceA 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
- 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.
- class cognitive_nodes.space.SVMSpace(**kwargs)[source]
Bases:
PointBasedSpaceUse 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
Utils
Python script which implements auxiliary functions used into the scripts of the cognitive nodes.
- class cognitive_nodes.utils.EpisodeSubscription[source]
Bases:
objectEpisodeSubscription is a mixin class that provides a method to configure a subscription to the episodes.
- class cognitive_nodes.utils.LTMSubscription[source]
Bases:
objectLTMSubscription 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.
- class cognitive_nodes.utils.PNodeSuccess[source]
Bases:
LTMSubscriptionPNodeSuccess 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.