Example #1
0
    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
Example #2
0
    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
Example #3
0
    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
Example #4
0
    def initialize(self):
        """
    Initialize this node.
    """

        # Create Classifier instance with appropriate parameters
        self.minProbabilityThreshold = 0.0001
        self.steps = []
        for step in range(maxFutureSteps):
            self.steps.append(step + 1)
        self.classifier = CLAClassifier(steps=self.steps)

        # Increase history according to inference flag
        if self.enableInference:
            maxLen = maxPreviousStepsWithInference
            self.bestPredictedValue = MachineState(0, maxLen)
        else:
            maxLen = maxPreviousSteps
        self.currentValue = MachineState(0, maxLen)
Example #5
0
    def __init__(self):
        """
    Initializes a new instance of this class.
    """

        #region Instance fields

        self.dataSourceFieldName = ''
        """Target field of the database table or file."""

        self.dataSourceFieldDataType = FieldDataType.string
        """Data type of the field returned by database table or file."""

        self.encoder = None
        """Optional encoder to convert raw data to htm input and vice-versa."""

        self.encoderModule = ""
        """Module name which encoder class is imported."""

        self.encoderClass = ""
        """Class name which encode or decode values."""

        self.encoderParams = ""
        """Parameters passed to the encoder class constructor."""

        self.encoderFieldName = ""
        """Field name returned by encoder when decode() function."""

        self.encoderFieldDataType = FieldDataType.string
        """Data type of the field returned by encoder."""

        self.enableInference = False
        """Enable inference for this field."""

        self.currentValue = MachineState(None, maxPreviousSteps)
        """Value read currently from database."""

        self.bestPredictedValue = MachineState(None, maxPreviousSteps)
        """Best value predicted by network. This is need to build predictions chart."""

        self.predictedValues = MachineState(None, maxPreviousSteps)
        """Values predicted by network."""
Example #6
0
  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
Example #7
0
    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
Example #8
0
  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
Example #9
0
    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
Example #10
0
  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
Example #11
0
    def buttonInit_click(self, event):
        """
        Initializes the HTM-Network by creating the htm-controller to connect to events database
        """

        # Initialize the network starting from top region.
        start_time = time.time()
        end_time = time.time()
        initialized = Global.project.network.initialize()

        if initialized:
            self.state = State.SIMULATING

            # Create a simulation
            #TODO: self.simulation = Simulation(self.project, self.getProjectPath())
            self.simulation = Simulation(None, None)

            # Initialize time steps parameters
            Global.curr_step = 0
            Global.sel_step = 0
            Global.time_steps_predictions_chart = MachineState(
                0, MAX_PREVIOUS_STEPS_WITH_INFERENCE)

            self.output_window.addText("Initialization: " +
                                       "{0:.3f}".format(end_time -
                                                        start_time) + " secs")
            self.output_window.addText("")
            self.output_window.addText("Step\tTime (secs)\tAccuracy (%)")

            # Perfoms actions related to time step progression.
            start_time = time.time()
            Global.project.network.nextStep()
            Global.project.network.calculateStatistics()
            end_time = time.time()
            self.output_window.addText(
                str(Global.curr_step + 1) +
                "\t{0:.3f}".format(end_time - start_time) +
                "\t{0:.3f}".format(Global.project.network.stats_precision_rate)
            )

            # Disable relevant buttons:
            self.enableSteeringButtons(True)
            self.enableSimulationButtons(True)

            # Update controls
            self.simulation_window.viewer_3d.initializeControls(
                Global.project.network.nodes[0])
            self.refreshControls()

            self.update_timer.setInterval(1)
            self.update_timer.start()
Example #12
0
  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
Example #13
0
    def __buttonInitHTM_Click(self, event):
        """
    Initializes the HTM-Network by creating the htm-controller to connect to events database
    """

        # Initialize the network starting from top region.
        startTime = time.time()
        endTime = time.time()
        initialized = Global.project.network.initialize()

        if initialized:

            # Initialize time steps parameters
            Global.currStep = 0
            Global.selStep = 0
            Global.timeStepsPredictionsChart = MachineState(
                0, maxPreviousStepsWithInference)

            Global.outputForm.addText("Initialization: " +
                                      "{0:.3f}".format(endTime - startTime) +
                                      " secs")
            Global.outputForm.addText("")
            Global.outputForm.addText("Step\tTime (secs)\tAccuracy (%)")

            # Perfoms actions related to time step progression.
            startTime = time.time()
            Global.project.network.nextStep()
            Global.project.network.calculateStatistics()
            endTime = time.time()
            Global.outputForm.addText(
                str(Global.currStep + 1) +
                "\t{0:.3f}".format(endTime - startTime) +
                "\t{0:.3f}".format(Global.project.network.statsPrecisionRate))

            # Disable relevant buttons:
            self.__enableSteeringButtons(True)
            self.__enableSimulationButtons(True)

            # Update controls
            Global.simulationForm.topRegion = Global.project.network.nodes[0]
            Global.simulationForm.initializeControls()
            self.refreshControls()
Example #14
0
    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
Example #15
0
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()
Example #16
0
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)
Example #17
0
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()
Example #18
0
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)
Example #19
0
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)
Example #20
0
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)
Example #21
0
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()
Example #22
0
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()
Example #23
0
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)
Example #24
0
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()