class Cell: """ A class only to group properties related to cells. """ #region Constructor def __init__(self): """ Initializes a new instance of this class. """ #region Instance fields self.index = -1 """Index of this cell in the temporal pooler.""" self.z = -1 """Position on Z axis""" self.segments = [] """List of distal segments of this cell.""" # States of this element self.isLearning = MachineState(False, maxPreviousSteps) self.isActive = MachineState(False, maxPreviousSteps) self.isPredicted = MachineState(False, maxPreviousSteps) self.isFalselyPredicted = MachineState(False, maxPreviousSteps) #region Statistics properties self.statsActivationCount = 0 self.statsActivationRate = 0. self.statsPreditionCount = 0 self.statsPrecisionRate = 0. #endregion #region 3d-tree properties (simulation form) self.tree3d_initialized = False self.tree3d_x = 0 self.tree3d_y = 0 self.tree3d_z = 0 self.tree3d_item = None self.tree3d_selected = False #endregion #endregion #endregion #region Methods def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.isLearning.rotate() self.isActive.rotate() self.isPredicted.rotate() self.isFalselyPredicted.rotate() # Remove segments (and their synapses) that are marked to be removed for segment in self.segments: if segment.isRemoved.atFirstStep(): for synapse in segment.synapses: segment.synapses.remove(synapse) del synapse self.segments.remove(segment) del segment for segment in self.segments: segment.nextStep() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.isActive.atCurrStep(): self.statsActivationCount += 1 if self.isPredicted.atCurrStep(): self.statsPreditionCount += 1 if Global.currStep > 0: self.statsActivationRate = self.statsActivationCount / float(Global.currStep) if self.statsActivationCount > 0: self.statsPrecisionRate = self.statsPreditionCount / float(self.statsActivationCount) for segment in self.segments: segment.calculateStatistics()
class Bit: """ A class only to group properties related to input bits of sensors. """ def __init__(self): """ Initializes a new instance of this class. """ self.initialize() def initialize(self): """ Initialize this bit. """ # Position on X axis self.x = -1 # Position on Y axis self.y = -1 # States of this element self.is_active = MachineState(False, MAX_PREVIOUS_STEPS) self.is_predicted = MachineState(False, MAX_PREVIOUS_STEPS) self.is_falsely_predicted = MachineState(False, MAX_PREVIOUS_STEPS) # Statistics self.stats_activation_count = 0 self.stats_activation_rate = 0.0 self.stats_predition_count = 0 self.stats_precision_rate = 0.0 # 3D object reference self.tree3d_initialized = False self.tree3d_pos = (0, 0, 0) self.tree3d_item_np = None self.tree3d_selected = False def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.is_active.rotate() self.is_predicted.rotate() self.is_falsely_predicted.rotate() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.is_active.atCurrStep(): self.stats_activation_count += 1 if self.is_predicted.atCurrStep(): self.stats_predition_count += 1 if Global.curr_step > 0: self.stats_activation_rate = self.stats_activation_count / float( Global.curr_step) if self.stats_activation_count > 0: self.stats_precision_rate = self.stats_predition_count / float( self.stats_activation_count)
class Cell: """ A class only to group properties related to cells. """ def __init__(self): """ Initializes a new instance of this class. """ # Index of this cell in the temporal pooler. self.index = -1 # Position on Z axis self.z = -1 # List of distal segments of this cell. self.segments = [] # States of this element self.is_learning = MachineState(False, MAX_PREVIOUS_STEPS) self.is_active = MachineState(False, MAX_PREVIOUS_STEPS) self.is_predicted = MachineState(False, MAX_PREVIOUS_STEPS) self.is_falsely_predicted = MachineState(False, MAX_PREVIOUS_STEPS) # Statistics self.stats_activation_count = 0 self.stats_activation_rate = 0.0 self.stats_predition_count = 0 self.stats_precision_rate = 0.0 # 3D object reference self.tree3d_initialized = False self.tree3d_pos = (0, 0, 0) self.tree3d_item_np = None self.tree3d_selected = False def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.is_learning.rotate() self.is_active.rotate() self.is_predicted.rotate() self.is_falsely_predicted.rotate() # Remove segments (and their synapses) that are marked to be removed for segment in self.segments: if segment.is_removed.atFirstStep(): for synapse in segment.synapses: segment.synapses.remove(synapse) del synapse self.segments.remove(segment) del segment for segment in self.segments: segment.nextStep() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.is_active.atCurrStep(): self.stats_activation_count += 1 if self.is_predicted.atCurrStep(): self.stats_predition_count += 1 if Global.curr_step > 0: self.stats_activation_rate = self.stats_activation_count / float( Global.curr_step) if self.stats_activation_count > 0: self.stats_precision_rate = self.stats_predition_count / float( self.stats_activation_count) for segment in self.segments: segment.calculateStatistics()
class Synapse: """ A class only to group properties related to synapses. """ #region Constructor def __init__(self): """ Initializes a new instance of this class. """ #region Instance fields self.indexSP = -1 """Index of this cell in the spatial pooler.""" self.indexTP = -1 """Index of this synapse in the temporal pooler.""" self.inputElem = None """An input element is a cell in case of the source be a column or then a bit in case of the source be a sensor""" self.permanence = MachineState(0., maxPreviousSteps) """Permanence of this synapse.""" # States of this element self.isConnected = MachineState(False, maxPreviousSteps) self.isPredicted = MachineState(False, maxPreviousSteps) self.isFalselyPredicted = MachineState(False, maxPreviousSteps) self.isRemoved = MachineState(False, maxPreviousSteps) #region Statistics properties self.statsConnectionCount = 0 self.statsConnectionRate = 0. self.statsPreditionCount = 0 self.statsPrecisionRate = 0. #endregion #region 3d-tree properties (simulation form) self.tree3d_initialized = False self.tree3d_item = None self.tree3d_selected = False #endregion #endregion #endregion #region Methods def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.permanence.rotate() self.isConnected.rotate() self.isPredicted.rotate() self.isFalselyPredicted.rotate() self.isRemoved.rotate() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.isConnected.atCurrStep(): self.statsConnectionCount += 1 if self.isPredicted.atCurrStep(): self.statsPreditionCount += 1 if Global.currStep > 0: self.statsConnectionRate = self.statsConnectionCount / float( Global.currStep) if self.statsConnectionCount > 0: self.statsPrecisionRate = self.statsPreditionCount / float( self.statsConnectionCount)
class Synapse: """ A class only to group properties related to synapses. """ def __init__(self): """ Initializes a new instance of this class. """ # Index of this cell in the spatial pooler. self.index_sp = -1 # Index of this synapse in the temporal pooler. self.index_tp = -1 # An input element is a cell in case of the source be a column or then a bit in case of the source be a sensor. self.input_elem = None # Permanence of this synapse. self.permanence = MachineState(0.0, MAX_PREVIOUS_STEPS) # States of this element self.is_connected = MachineState(False, MAX_PREVIOUS_STEPS) self.is_predicted = MachineState(False, MAX_PREVIOUS_STEPS) self.is_falsely_predicted = MachineState(False, MAX_PREVIOUS_STEPS) self.is_removed = MachineState(False, MAX_PREVIOUS_STEPS) # Statistics self.stats_connection_count = 0 self.stats_connection_rate = 0.0 self.stats_predition_count = 0 self.stats_precision_rate = 0.0 # 3D object reference self.tree3d_initialized = False self.tree3d_item_np = None self.tree3d_selected = False def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.permanence.rotate() self.is_connected.rotate() self.is_predicted.rotate() self.is_falsely_predicted.rotate() self.is_removed.rotate() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.is_connected.atCurrStep(): self.stats_connection_count += 1 if self.is_predicted.atCurrStep(): self.stats_predition_count += 1 if Global.curr_step > 0: self.stats_connection_rate = self.stats_connection_count / float( Global.curr_step) if self.stats_connection_count > 0: self.stats_precision_rate = self.stats_predition_count / float( self.stats_connection_count)
class Segment: """ A class only to group properties related to segments. """ #region Constructor def __init__(self, type): """ Initializes a new instance of this class. """ #region Instance fields self.type = type """Determine if this segment is proximal or distal.""" self.indexTP = -1 """Index of this segment in the temporal pooler.""" self.synapses = [] """List of distal synapses of this segment.""" # States of this element self.isActive = MachineState(False, maxPreviousSteps) self.isPredicted = MachineState(False, maxPreviousSteps) self.isFalselyPredicted = MachineState(False, maxPreviousSteps) self.isRemoved = MachineState(False, maxPreviousSteps) #region Statistics properties self.statsActivationCount = 0 self.statsActivationRate = 0. self.statsPreditionCount = 0 self.statsPrecisionRate = 0. #endregion #region 3d-tree properties (simulation form) self.tree3d_initialized = False self.tree3d_x1 = 0 self.tree3d_y1 = 0 self.tree3d_z1 = 0 self.tree3d_x2 = 0 self.tree3d_y2 = 0 self.tree3d_z2 = 0 self.tree3d_item = None self.tree3d_selected = False #endregion #endregion #endregion #region Methods def getSynapse(self, indexSP): """ Return the synapse connected to a given cell or sensor bit """ synapse = None for synapse in self.synapses: if synapse.indexSP == indexSP: return synapse def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.isActive.rotate() self.isPredicted.rotate() self.isFalselyPredicted.rotate() self.isRemoved.rotate() # Remove synapses that are marked to be removed for synapse in self.synapses: if synapse.isRemoved.atFirstStep(): self.synapses.remove(synapse) del synapse for synapse in self.synapses: synapse.nextStep() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.isActive.atCurrStep(): self.statsActivationCount += 1 if self.isPredicted.atCurrStep(): self.statsPreditionCount += 1 if Global.currStep > 0: self.statsActivationRate = self.statsActivationCount / float(Global.currStep) if self.statsActivationCount > 0: self.statsPrecisionRate = self.statsPreditionCount / float(self.statsActivationCount) for synapse in self.synapses: synapse.calculateStatistics()
class Bit: """ A class only to group properties related to input bits of sensors. """ #region Constructor def __init__(self): """ Initializes a new instance of this class. """ self.initialize() #endregion #region Methods def initialize(self): """ Initialize this bit. """ self.x = -1 """Position on X axis""" self.y = -1 """Position on Y axis""" # States of this element self.isActive = MachineState(False, maxPreviousSteps) self.isPredicted = MachineState(False, maxPreviousSteps) self.isFalselyPredicted = MachineState(False, maxPreviousSteps) #region Statistics properties self.statsActivationCount = 0 self.statsActivationRate = 0. self.statsPreditionCount = 0 self.statsPrecisionRate = 0. #endregion #region 3d-tree properties (simulation form) self.tree3d_initialized = False self.tree3d_x = 0 self.tree3d_y = 0 self.tree3d_z = 0 self.tree3d_item = None self.tree3d_selected = False #endregion def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.isActive.rotate() self.isPredicted.rotate() self.isFalselyPredicted.rotate() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.isActive.atCurrStep(): self.statsActivationCount += 1 if self.isPredicted.atCurrStep(): self.statsPreditionCount += 1 if Global.currStep > 0: self.statsActivationRate = self.statsActivationCount / float(Global.currStep) if self.statsActivationCount > 0: self.statsPrecisionRate = self.statsPreditionCount / float(self.statsActivationCount)
class Synapse: """ A class only to group properties related to synapses. """ #region Constructor def __init__(self): """ Initializes a new instance of this class. """ #region Instance fields self.indexSP = -1 """Index of this cell in the spatial pooler.""" self.indexTP = -1 """Index of this synapse in the temporal pooler.""" self.inputElem = None """An input element is a cell in case of the source be a column or then a bit in case of the source be a sensor""" self.permanence = MachineState(0., maxPreviousSteps) """Permanence of this synapse.""" # States of this element self.isConnected = MachineState(False, maxPreviousSteps) self.isPredicted = MachineState(False, maxPreviousSteps) self.isFalselyPredicted = MachineState(False, maxPreviousSteps) self.isRemoved = MachineState(False, maxPreviousSteps) #region Statistics properties self.statsConnectionCount = 0 self.statsConnectionRate = 0. self.statsPreditionCount = 0 self.statsPrecisionRate = 0. #endregion #region 3d-tree properties (simulation form) self.tree3d_initialized = False self.tree3d_item = None self.tree3d_selected = False #endregion #endregion #endregion #region Methods def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.permanence.rotate() self.isConnected.rotate() self.isPredicted.rotate() self.isFalselyPredicted.rotate() self.isRemoved.rotate() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.isConnected.atCurrStep(): self.statsConnectionCount += 1 if self.isPredicted.atCurrStep(): self.statsPreditionCount += 1 if Global.currStep > 0: self.statsConnectionRate = self.statsConnectionCount / float(Global.currStep) if self.statsConnectionCount > 0: self.statsPrecisionRate = self.statsPreditionCount / float(self.statsConnectionCount)
class Cell: """ A class only to group properties related to cells. """ #region Constructor def __init__(self): """ Initializes a new instance of this class. """ #region Instance fields self.index = -1 """Index of this cell in the temporal pooler.""" self.z = -1 """Position on Z axis""" self.segments = [] """List of distal segments of this cell.""" # States of this element self.isLearning = MachineState(False, maxPreviousSteps) self.isActive = MachineState(False, maxPreviousSteps) self.isPredicted = MachineState(False, maxPreviousSteps) self.isFalselyPredicted = MachineState(False, maxPreviousSteps) #region Statistics properties self.statsActivationCount = 0 self.statsActivationRate = 0. self.statsPreditionCount = 0 self.statsPrecisionRate = 0. #endregion #region 3d-tree properties (simulation form) self.tree3d_initialized = False self.tree3d_x = 0 self.tree3d_y = 0 self.tree3d_z = 0 self.tree3d_item = None self.tree3d_selected = False #endregion #endregion #endregion #region Methods def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.isLearning.rotate() self.isActive.rotate() self.isPredicted.rotate() self.isFalselyPredicted.rotate() # Remove segments (and their synapses) that are marked to be removed for segment in self.segments: if segment.isRemoved.atFirstStep(): for synapse in segment.synapses: segment.synapses.remove(synapse) del synapse self.segments.remove(segment) del segment for segment in self.segments: segment.nextStep() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.isActive.atCurrStep(): self.statsActivationCount += 1 if self.isPredicted.atCurrStep(): self.statsPreditionCount += 1 if Global.currStep > 0: self.statsActivationRate = self.statsActivationCount / float( Global.currStep) if self.statsActivationCount > 0: self.statsPrecisionRate = self.statsPreditionCount / float( self.statsActivationCount) for segment in self.segments: segment.calculateStatistics()
class Segment: """ A class only to group properties related to segments. """ def __init__(self, type): """ Initializes a new instance of this class. """ # Determine if this segment is proximal or distal. self.type = type # Index of this segment in the temporal pooler. self.index_tp = -1 # List of distal synapses of this segment. self.synapses = [] # States of this element self.is_active = MachineState(False, MAX_PREVIOUS_STEPS) self.is_predicted = MachineState(False, MAX_PREVIOUS_STEPS) self.is_falsely_predicted = MachineState(False, MAX_PREVIOUS_STEPS) self.is_removed = MachineState(False, MAX_PREVIOUS_STEPS) # Statistics self.stats_activation_count = 0 self.stats_activation_rate = 0.0 self.stats_predition_count = 0 self.stats_precision_rate = 0.0 # 3D object reference self.tree3d_initialized = False self.tree3d_start_pos = (0, 0, 0) self.tree3d_end_pos = (0, 0, 0) self.tree3d_item_np = None self.tree3d_selected = False def getSynapse(self, index_sp): """ Return the synapse connected to a given cell or sensor bit """ for synapse in self.synapses: if synapse.index_sp == index_sp: return synapse return None def nextStep(self): """ Perfoms actions related to time step progression. """ # Update states machine by remove the first element and add a new element in the end self.is_active.rotate() self.is_predicted.rotate() self.is_falsely_predicted.rotate() self.is_removed.rotate() # Remove synapses that are marked to be removed for synapse in self.synapses: if synapse.is_removed.atFirstStep(): self.synapses.remove(synapse) del synapse for synapse in self.synapses: synapse.nextStep() def calculateStatistics(self): """ Calculate statistics after an iteration. """ # Calculate statistics if self.is_active.atCurrStep(): self.stats_activation_count += 1 if self.is_predicted.atCurrStep(): self.stats_predition_count += 1 if Global.curr_step > 0: self.stats_activation_rate = self.stats_activation_count / float( Global.curr_step) if self.stats_activation_count > 0: self.stats_precision_rate = self.stats_predition_count / float( self.stats_activation_count) for synapse in self.synapses: synapse.calculateStatistics()