Esempio n. 1
0
    def testQueue(self):
        """
        Test that RawValues executes correctly.
        """
        net = Network()

        # Create simple region to pass motor commands as displacement vectors (dx, dy)
        motor = net.addRegion("motor", "py.RawValues",
                              json.dumps({"outputWidth": 2}))
        motor.executeCommand('addDataToQueue', [0, 0], 1)
        motor.executeCommand('addDataToQueue', [1, 1])

        net.run(1)

        output = list(np.array(motor.getOutputArray("dataOut")))
        self.assertEqual([0, 0], output)
        output = list(np.array(motor.getOutputArray("resetOut")))
        self.assertEqual([True], output)

        net.run(1)

        output = list(np.array(motor.getOutputArray("dataOut")))
        self.assertEqual([1, 1], output)
        output = list(np.array(motor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
Esempio n. 2
0
    def testGetParametersCustomRegions(self):

        Network.cleanup()  # removes all previous registrations
        registerAllAdvancedRegions()
        json_list = Network.getRegistrations()
        #print(json_list)
        y = json.loads(json_list)
        self.assertTrue("py.ColumnPoolerRegion" in y)

        net = Network()
        #print(net.getSpecJSON("py.ColumnPoolerRegion"))
        cp = net.addRegion(
            "py.ColumnPoolerRegion", "py.ColumnPoolerRegion", """{ 
	"activationThresholdDistal": 20,
	"cellCount": 4096,
	"connectedPermanenceDistal": 0.5,
	"connectedPermanenceProximal": 0.5,
	"initialDistalPermanence": 0.51,
	"initialProximalPermanence": 0.6,
	"minThresholdProximal": 5,
	"sampleSizeDistal": 30,
	"sampleSizeProximal": 10,
	"sdrSize": 40,
	"synPermDistalDec": 0.001,
	"synPermDistalInc": 0.1,
	"synPermProximalDec": 0.001,
	"synPermProximalInc": 0.1
}""")

        # expected results from getParameters()  (in Spec order)
        expected = """{
  "learningMode": true,
  "onlineLearning": false,
  "cellCount": 4096,
  "inputWidth": 16384,
  "numOtherCorticalColumns": 0,
  "sdrSize": 40,
  "maxSdrSize": 0,
  "minSdrSize": 0,
  "synPermProximalInc": 0.100000,
  "synPermProximalDec": 0.001000,
  "initialProximalPermanence": 0.600000,
  "sampleSizeProximal": 10,
  "minThresholdProximal": 5,
  "connectedPermanenceProximal": 0.500000,
  "predictedInhibitionThreshold": 20.000000,
  "synPermDistalInc": 0.100000,
  "synPermDistalDec": 0.001000,
  "initialDistalPermanence": 0.510000,
  "sampleSizeDistal": 30,
  "activationThresholdDistal": 20,
  "connectedPermanenceDistal": 0.500000,
  "inertiaFactor": 1.000000,
  "seed": 42,
  "defaultOutputType": "active"
}"""

        json_list = cp.getParameters()
        #print(json_list)
        self.assertEqual(json_list, expected)
Esempio n. 3
0
    def testGetParameters(self):
        """
    A test of the getParameters( ) and getParameterJSON() functions.
    """
        expected = """{
  "activeBits": 200,
  "category": false,
  "noise": 0.010000,
  "radius": 0.030000,
  "resolution": 0.000150,
  "seed": 2019,
  "sensedValue": 0.000000,
  "size": 1000,
  "sparsity": 0.200000
}"""
        net = Network()
        encoder = net.addRegion(
            "encoder", "RDSEEncoderRegion",
            "{size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}"
        )
        json_str = encoder.getParameters()
        self.assertEqual(json_str, expected)

        json.loads(json_str)  # test if json package can load it

        json_str = encoder.getParameterJSON("activeBits", False)
        self.assertEqual(json_str, "200")
        json_str = encoder.getParameterJSON("activeBits", True)
        self.assertEqual(json_str, "{\"value\": 200, \"type\": \"UInt32\"}")
    def testOverlap(self):
        """Create a simple network to test the region."""

        rawParams = {"outputWidth": 8 * 2048}
        net = Network()
        rawSensor = net.addRegion("raw", "py.RawSensor", json.dumps(rawParams))
        l2c = net.addRegion("L2", "py.ColumnPoolerRegion", "")
        net.link("raw", "L2", "UniformLink", "")

        self.assertEqual(rawSensor.getParameterUInt32("outputWidth"), l2c.getParameterUInt32("inputWidth"), "Incorrect outputWidth parameter")

        rawSensor.executeCommand('addDataToQueue', [2, 4, 6], 0, 42)
        rawSensor.executeCommand('addDataToQueue', [2, 42, 1023], 1, 43)
        rawSensor.executeCommand('addDataToQueue', [18, 19, 20], 0, 44)

        # Run the network and check outputs are as expected
        net.run(3)
Esempio n. 5
0
    def testSPRegionParametersAreWritable(self):
        """
        Test that the SPRegion parameters can be set.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sp = net.addRegion("sp", "SPRegion", json.dumps(spParams))
Esempio n. 6
0
    def testSPRegionIsCreatable(self):
        """
        Test that the SPRegion can be created in Python.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sp = net.addRegion("sp", "SPRegion", json.dumps({}))
Esempio n. 7
0
    def testGetSpec(self):
        """
    Test of region.getSpec() function. Testing if pybind pointers are correctly handled
    """
        net = Network()
        dateRegion = net.addRegion(
            'dateEncoder', 'DateEncoderRegion',
            str(dict(timeOfDay_width=30, timeOfDay_radius=1,
                     weekend_width=21)))

        dateRegion.getSpec()  # twice times to check if no double free arises
        dateRegion.getSpec()
Esempio n. 8
0
    def testGetParametersGridCell(self):
        # a test of arrays in parameters
        Network.cleanup()  # removes all previous registrations
        registerAllAdvancedRegions()
        json_list = Network.getRegistrations()
        #print(json_list)
        y = json.loads(json_list)
        self.assertTrue("py.GridCellLocationRegion" in y)

        # only provide one orentation to shorten the test.  Full test in location_region_test.py
        net = Network()
        #print(net.getSpecJSON("py.GridCellLocationRegion"))
        cp = net.addRegion(
            "grid", "py.GridCellLocationRegion", """{ 
  "moduleCount": 1,
  "cellsPerAxis": 10,
  "scale": [1],
  "orientation": [0.5],
  "anchorInputSize": 1,
  "activeFiringRate": 10 
}""")

        # expected results from getParameters()  (in Spec order)
        expected = """{
  "moduleCount": 1,
  "cellsPerAxis": 10,
  "scale": [1],
  "orientation": [0.5],
  "anchorInputSize": 1,
  "activeFiringRate": 10.000000,
  "bumpSigma": 0.181720,
  "activationThreshold": 10,
  "initialPermanence": 0.210000,
  "connectedPermanence": 0.500000,
  "learningThreshold": 10,
  "sampleSize": 20,
  "permanenceIncrement": 0.100000,
  "permanenceDecrement": 0.000000,
  "maxSynapsesPerSegment": -1,
  "bumpOverlapMethod": "probabilistic",
  "learningMode": false,
  "dualPhase": true,
  "dimensions": 2,
  "seed": 42
}"""

        json_list = cp.getParameters()
        #print(json_list)
        self.assertEqual(json_list, expected)
Esempio n. 9
0
    def testExecuteCommandListWithNoReset(self):
        """
        Test that execute command executes the correct command with a list with reset 0.
        """
        net = Network()

        # Create simple region to pass motor commands as displacement vectors (dx, dy)
        motor = net.addRegion("motor", "py.RawValues",
                              json.dumps({"outputWidth": 2}))
        motor.executeCommand('addDataToQueue', [0, 0], 0)

        net.run(1)

        output = list(np.array(motor.getOutputArray("dataOut")))
        self.assertEqual([0, 0], output)
        output = list(np.array(motor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
Esempio n. 10
0
    def testEmptyQueue(self):
        """
        Test that RawValues detects empty queue.
        """
        net = Network()

        # Create simple region to pass motor commands as displacement vectors (dx, dy)
        motor = net.addRegion("motor", "py.RawValues",
                              json.dumps({"outputWidth": 2}))

        try:
            net.run(1)
            output = list(np.array(motor.getOutputArray("dataOut")))
            self.assertEqual([0, 0], output)
            self.fail("Empty queue should throw exception")
        except:
            pass
Esempio n. 11
0
    def testExecuteCommandListWithReset(self):
        """
        Test that execute command executes the correct command with a list and reset set.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sensor = net.addRegion("sensor", "py.RawSensor",
                               json.dumps({"outputWidth": 8}))
        sensor.executeCommand('addDataToQueue', [0, 1], 1, 0)

        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([1, 1, 0, 0, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([True], output)
Esempio n. 12
0
    def testSequence(self):
        """
        Test that RawSensor executes correctly.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sensor = net.addRegion("sensor", "py.RawSensor",
                               json.dumps({"outputWidth": 8}))
        sensor.executeCommand('addDataToQueue', [0, 1], 1, 0)
        sensor.executeCommand('addDataToQueue', [1, 2], 0, 1)
        sensor.executeCommand('addDataToQueue', [2, 3], 0, 2)

        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([1, 1, 0, 0, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([True], output)
        output = list(np.array(sensor.getOutputArray("sequenceIdOut")))
        self.assertEqual([0], output)

        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([0, 1, 1, 0, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
        output = list(np.array(sensor.getOutputArray("sequenceIdOut")))
        self.assertEqual([1], output)
        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([0, 0, 1, 1, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
        output = list(np.array(sensor.getOutputArray("sequenceIdOut")))
        self.assertEqual([2], output)
Esempio n. 13
0
def _createNetwork(inverseReadoutResolution, anchorInputSize, dualPhase=False):
    """
    Create a simple network connecting sensor and motor inputs to the location
    region. Use :meth:`RawSensor.addDataToQueue` to add sensor input and growth
    candidates. Use :meth:`RawValues.addDataToQueue` to add motor input.
    ::
                        +----------+
    [   sensor*   ] --> |          | --> [     activeCells        ]
    [ candidates* ] --> | location | --> [    learnableCells      ]
    [    motor    ] --> |          | --> [ sensoryAssociatedCells ]
                        +----------+

    :param inverseReadoutResolution:
        Specifies the diameter of the circle of phases in the rhombus encoded by a
        bump.
    :type inverseReadoutResolution: int

    :type anchorInputSize: int
    :param anchorInputSize:
        The number of input bits in the anchor input.

    .. note::
        (*) This function will only add the 'sensor' and 'candidates' regions when
        'anchorInputSize' is greater than zero. This is useful if you would like to
        compute locations ignoring sensor input

    .. seealso::
         - :py:func:`htmresearch.frameworks.location.path_integration_union_narrowing.createRatModuleFromReadoutResolution`

    """
    net = Network()

    # Create simple region to pass motor commands as displacement vectors (dx, dy)
    net.addRegion("motor", "py.RawValues", json.dumps({"outputWidth": 2}))

    if anchorInputSize > 0:
        # Create simple region to pass growth candidates
        net.addRegion("candidates", "py.RawSensor",
                      json.dumps({"outputWidth": anchorInputSize}))

        # Create simple region to pass sensor input
        net.addRegion("sensor", "py.RawSensor",
                      json.dumps({"outputWidth": anchorInputSize}))

    # Initialize region with 5 modules varying scale by sqrt(2) and 4 different
    # random orientations for each scale
    scale = []
    orientation = []
    for i in range(5):
        for _ in range(4):
            angle = np.radians(random.gauss(7.5, 7.5))
            orientation.append(random.choice([angle, -angle]))
            scale.append(10.0 * (math.sqrt(2)**i))

    # Create location region
    params = computeRatModuleParametersFromReadoutResolution(
        inverseReadoutResolution)
    params.update({
        "moduleCount": len(scale),
        "scale": scale,
        "orientation": orientation,
        "anchorInputSize": anchorInputSize,
        "activationThreshold": 8,
        "initialPermanence": 1.0,
        "connectedPermanence": 0.5,
        "learningThreshold": 8,
        "sampleSize": 10,
        "permanenceIncrement": 0.1,
        "permanenceDecrement": 0.0,
        "dualPhase": dualPhase,
        "bumpOverlapMethod": "probabilistic"
    })
    net.addRegion("location", "py.GridCellLocationRegion", json.dumps(params))

    if anchorInputSize > 0:
        # Link sensor
        net.link("sensor",
                 "location",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="anchorInput")
        net.link("sensor",
                 "location",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
        net.link("candidates",
                 "location",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="anchorGrowthCandidates")

    # Link motor input
    net.link("motor",
             "location",
             "UniformLink",
             "",
             srcOutput="dataOut",
             destInput="displacement")

    # Initialize network objects
    net.initialize()

    return net
Esempio n. 14
0
def main(parameters=default_parameters, argv=None, verbose=True):
    if verbose:
        import pprint
        print("Parameters:")
        pprint.pprint(parameters, indent=4)
        print("")

    # Read the input file.
    records = []
    with open(_INPUT_FILE_PATH, "r") as fin:
        reader = csv.reader(fin)
        headers = next(reader)
        next(reader)
        next(reader)
        for record in reader:
            records.append(record)

    net = Network()
    # Make the Encoders.  These will convert input data into binary representations.
    dateEncoderRegion = net.addRegion(
        'dateEncoder', 'DateEncoderRegion',
        str(
            dict(timeOfDay_width=parameters["enc"]["time"]["timeOfDay"][0],
                 timeOfDay_radius=parameters["enc"]["time"]["timeOfDay"][1],
                 weekend_width=parameters["enc"]["time"]["weekend"])))

    valueEncoderRegion = net.addRegion(
        'valueEncoder', 'RDSEEncoderRegion',
        str(
            dict(size=parameters["enc"]["value"]["size"],
                 sparsity=parameters["enc"]["value"]["sparsity"],
                 resolution=parameters["enc"]["value"]["resolution"])))

    # Make the HTM.  SpatialPooler & TemporalMemory & associated tools.
    spParams = parameters["sp"]
    spRegion = net.addRegion(
        'sp',
        'SPRegion',
        str(
            dict(
                columnCount=spParams['columnCount'],
                potentialPct=spParams["potentialPct"],
                potentialRadius=0,  # 0 is auto assign as inputWith
                globalInhibition=True,
                localAreaDensity=spParams["localAreaDensity"],
                synPermInactiveDec=spParams["synPermInactiveDec"],
                synPermActiveInc=spParams["synPermActiveInc"],
                synPermConnected=spParams["synPermConnected"],
                boostStrength=spParams["boostStrength"],
                wrapAround=True)))

    tmParams = parameters["tm"]
    tmRegion = net.addRegion(
        'tm', 'TMRegion',
        str(
            dict(columnCount=spParams['columnCount'],
                 cellsPerColumn=tmParams["cellsPerColumn"],
                 activationThreshold=tmParams["activationThreshold"],
                 initialPermanence=tmParams["initialPerm"],
                 connectedPermanence=spParams["synPermConnected"],
                 minThreshold=tmParams["minThreshold"],
                 maxNewSynapseCount=tmParams["newSynapseCount"],
                 permanenceIncrement=tmParams["permanenceInc"],
                 permanenceDecrement=tmParams["permanenceDec"],
                 predictedSegmentDecrement=0.0,
                 maxSegmentsPerCell=tmParams["maxSegmentsPerCell"],
                 maxSynapsesPerSegment=tmParams["maxSynapsesPerSegment"])))

    net.link('dateEncoder', 'sp', '', '', 'encoded', 'bottomUpIn')
    net.link('valueEncoder', 'sp', '', '', 'encoded', 'bottomUpIn')
    net.link('sp', 'tm', '', '', 'bottomUpOut', 'bottomUpIn')

    net.initialize()

    # Iterate through every datum in the dataset, record the inputs & outputs.
    inputs = []
    anomaly = []

    for count, record in enumerate(records):

        # Convert date string into Python date object.
        dateString = datetime.datetime.strptime(record[0], "%m/%d/%y %H:%M")
        # Convert data value string into float.
        consumption = float(record[1])
        inputs.append(consumption)

        # Call the encoders to create bit representations for each value.  These are SDR objects.
        dateEncoderRegion.setParameterInt64('sensedTime',
                                            int(dateString.timestamp()))
        valueEncoderRegion.setParameterReal64('sensedValue', consumption)

        net.run(1)
        anomaly.append(np.array(tmRegion.getOutputArray("anomaly"))[0])

    try:
        import matplotlib.pyplot as plt
    except:
        print("WARNING: failed to import matplotlib, plots cannot be shown.")
        return

    plt.title("Anomaly Score")
    plt.xlabel("Time")
    plt.ylabel("Power Consumption")
    inputs = np.array(inputs) / max(inputs)
    plt.plot(
        np.arange(len(inputs)),
        inputs,
        'red',
        np.arange(len(inputs)),
        anomaly,
        'blue',
    )
    plt.legend(labels=('Input', 'Anomaly Score'))
    plt.show()
    return