Connections

class nupic::algorithms::connections::Connections

Connections implementation in C++.

Description The Connections class is a data structure that represents the connections of a collection of cells. It is used in the HTM learning algorithms to store and access data related to the connectivity of cells.

Its main utility is to provide a common, optimized data structure that all HTM learning algorithms can use. It is flexible enough to support any learning algorithm that operates on a collection of cells.

Each type of connection (proximal, distal basal, apical) should be represented by a different instantiation of this class. This class will help compute the activity along those connections due to active input cells. The responsibility for what effect that activity has on the cells and connections lies in the user of this class.

This class is optimized to store connections between cells, and compute the activity of cells due to input over the connections.

This class assigns each segment a unique “flatIdx” so that it’s possible to use a simple vector to associate segments with values. Create a vector of length connections.segmentFlatListLength(), iterate over segments and update the vector at index segment.flatIdx, and then recover the segments using connections.segmentForFlatIdx(i).

Inherits from nupic::Serializable< ConnectionsProto >

Public Functions

Connections()

Connections empty constructor.

(Does not call initialize.)

Connections(CellIdx numCells, SegmentIdx maxSegmentsPerCell = 255, SynapseIdx maxSynapsesPerSegment = 255)

Connections constructor.

Parameters
  • numCells: Number of cells.
  • maxSegmentsPerCell: Maximum number of segments per cell.
  • maxSynapsesPerSegment: Maximum number of synapses per segment.

void initialize(CellIdx numCells, SegmentIdx maxSegmentsPerCell, SynapseIdx maxSynapsesPerSegment)

Initialize connections.

Parameters
  • numCells: Number of cells.
  • maxSegmentsPerCell: Maximum number of segments per cell.
  • maxSynapsesPerSegment: Maximum number of synapses per segment.

Segment createSegment(CellIdx cell)

Creates a segment on the specified cell.

Parameters
  • cell: Cell to create segment on.
Return Value
  • Created: segment.

Synapse createSynapse(Segment segment, CellIdx presynapticCell, Permanence permanence)

Creates a synapse on the specified segment.

Created synapse.

Parameters
  • segment: Segment to create synapse on.
  • presynapticCell: Cell to synapse on.
  • permanence: Initial permanence of new synapse.

void destroySegment(Segment segment)

Destroys segment.

Parameters
  • segment: Segment to destroy.

void destroySynapse(Synapse synapse)

Destroys synapse.

Parameters
  • synapse: Synapse to destroy.

void updateSynapsePermanence(Synapse synapse, Permanence permanence)

Updates a synapse’s permanence.

Parameters
  • synapse: Synapse to update.
  • permanence: New permanence.

const std::vector<Segment> &segmentsForCell(CellIdx cell) const

Gets the segments for a cell.

Parameters
  • cell: Cell to get segments for.
Return Value
  • Segments: on cell.

const std::vector<Synapse> &synapsesForSegment(Segment segment) const

Gets the synapses for a segment.

Parameters
  • segment: Segment to get synapses for.
Return Value
  • Synapses: on segment.

CellIdx cellForSegment(Segment segment) const

Gets the cell that this segment is on.

Parameters
  • segment: Segment to get the cell for.
Return Value
  • Cell: that this segment is on.

Segment segmentForSynapse(Synapse synapse) const

Gets the segment that this synapse is on.

Parameters
  • synapse: Synapse to get Segment for.
Return Value
  • Segment: that this synapse is on.

const SegmentData &dataForSegment(Segment segment) const

Gets the data for a segment.

Parameters
  • segment: Segment to get data for.
Return Value
  • Segment: data.

const SynapseData &dataForSynapse(Synapse synapse) const

Gets the data for a synapse.

Parameters
  • synapse: Synapse to get data for.
Return Value
  • Synapse: data.

Segment getSegment(CellIdx cell, SegmentIdx idx) const

Get the segment at the specified cell and offset.

Parameters
  • cell: The cell that the segment is on.
  • idx: The index of the segment on the cell.
Return Value
  • Segment:

Segment segmentForFlatIdx(UInt32 flatIdx) const

Do a reverse-lookup of a segment from its flatIdx.

Parameters
  • flatIdx: the flatIdx of the segment
Return Value
  • Segment:

UInt32 segmentFlatListLength() const

Get the vector length needed to use the segments’ flatIdx for indexing.

Return Value
  • A: vector length

bool compareSegments(Segment a, Segment b) const

Compare two segments.

Returns true if a < b.

Segments are ordered first by cell, then by their order on the cell.

Parameters
  • a: Left segment to compare
  • b: Right segment to compare
Return Value
  • true: if a < b, false otherwise.

std::vector<Synapse> synapsesForPresynapticCell(CellIdx presynapticCell) const

Returns the synapses for the source cell that they synapse on.

Return
Synapse indices
Parameters
  • presynapticCell(int): Source cell index

void computeActivity(std::vector<UInt32> &numActiveConnectedSynapsesForSegment, std::vector<UInt32> &numActivePotentialSynapsesForSegment, const std::vector<CellIdx> &activePresynapticCells, Permanence connectedPermanence) const

Compute the segment excitations for a vector of active presynaptic cells.

The output vectors aren’t grown or cleared. They must be preinitialized with the length returned by getSegmentFlatVectorLength().

Parameters
  • numActiveConnectedSynapsesForSegment: An output vector for active connected synapse counts per segment.
  • numActivePotentialSynapsesForSegment: An output vector for active potential synapse counts per segment.
  • activePresynapticCells: Active cells in the input.
  • connectedPermanence: Minimum permanence for a synapse to be “connected”.

void computeActivity(std::vector<UInt32> &numActiveConnectedSynapsesForSegment, std::vector<UInt32> &numActivePotentialSynapsesForSegment, CellIdx activePresynapticCell, Permanence connectedPermanence) const

Compute the segment excitations for a single active presynaptic cell.

The output vectors aren’t grown or cleared. They must be preinitialized with the length returned by getSegmentFlatVectorLength().

Parameters
  • numActiveConnectedSynapsesForSegment: An output vector for active connected synapse counts per segment.
  • numActivePotentialSynapsesForSegment: An output vector for active potential synapse counts per segment.
  • activePresynapticCells: Active cells in the input.
  • connectedPermanence: Minimum permanence for a synapse to be “connected”.

void recordSegmentActivity(Segment segment)

Record the fact that a segment had some activity.

This information is used during segment cleanup.

Parameters
  • segment: The segment that had some activity.

void startNewIteration()

Mark the passage of time.

This information is used during segment cleanup.

virtual void save(std::ostream &outStream) const

Saves serialized data to output stream.

virtual void write(ConnectionsProto::Builder &proto) const

Writes serialized data to proto object.

virtual void load(std::istream &inStream)

Loads serialized data from input stream.

virtual void read(ConnectionsProto::Reader &proto)

Reads serialized data from proto object.

CellIdx numCells() const

Gets the number of cells.

Return Value
  • Number: of cells.

UInt numSegments() const

Gets the number of segments.

Return Value
  • Number: of segments.

UInt numSegments(CellIdx cell) const

Gets the number of segments on a cell.

Return Value
  • Number: of segments.

UInt numSynapses() const

Gets the number of synapses.

Return Value
  • Number: of synapses.

UInt numSynapses(Segment segment) const

Gets the number of synapses on a segment.

Return Value
  • Number: of synapses.

bool operator==(const Connections &other) const

Comparison operator.

UInt32 subscribe(ConnectionsEventHandler *handler)

Add a connections events handler.

The Connections instance takes ownership of the eventHandlers object. Don’t delete it. When calling from Python, call eventHandlers.__disown__() to avoid garbage-collecting the object while this instance is still using it. It will be deleted on unsubscribe.

Parameters
  • handler: An object implementing the ConnectionsEventHandler interface
Return Value
  • Unsubscribe: token

void unsubscribe(UInt32 token)

Remove an event handler.

Parameters
  • token: The return value of subscribe.