1+ '''
2+ Created on 6 Nov 2019
3+
4+ @author: fred
5+ '''
6+ import unittest
7+
8+ import pickle
9+ import sys
10+ import numpy as np
11+
12+ from htm .advanced .support .register_regions import registerAllAdvancedRegions
13+ from htm .bindings .engine_internal import Network
14+ from htm .advanced .frameworks .location .location_network_creation import createL4L6aLocationColumn
15+
16+ cortical_params = {
17+ # Column parameters
18+
19+ # L2 Parameters
20+ # Adapted from htmresearch.frameworks.layers.l2_l4_inference.L4L2Experiment#getDefaultL2Params
21+ # L4 Parameters
22+ "l4_cellsPerColumn" : 24 ,
23+ "l4_columnCount" : 16 ,
24+ "l4_connectedPermanence" : 0.6 ,
25+ "l4_permanenceIncrement" : 0.1 ,
26+ "l4_permanenceDecrement" : 0.02 ,
27+ "l4_apicalPredictedSegmentDecrement" : 0.0 ,
28+ "l4_basalPredictedSegmentDecrement" : 0.0 ,
29+ "l4_initialPermanence" : 1.0 ,
30+ "l4_activationThreshold" : 8 ,
31+ "l4_minThreshold" : 8 ,
32+ "l4_reducedBasalThreshold" : 8 ,
33+ "l4_sampleSize" : 10 ,
34+ "l4_implementation" : "ApicalTiebreak" ,
35+
36+ # L6a Parameters
37+ "l6a_moduleCount" : 10 ,
38+ "l6a_dimensions" : 2 ,
39+ "l6a_connectedPermanence" : 0.5 ,
40+ "l6a_permanenceIncrement" : 0.1 ,
41+ "l6a_permanenceDecrement" : 0.0 ,
42+ "l6a_initialPermanence" : 1.0 ,
43+ "l6a_activationThreshold" : 8 ,
44+ "l6a_initialPermanence" : 1.0 ,
45+ "l6a_learningThreshold" : 8 ,
46+ "l6a_sampleSize" : 10 ,
47+ "l6a_cellsPerAxis" : 10 ,
48+ "l6a_scale" : 10 ,
49+ "l6a_orientation" : 60 ,
50+ "l6a_bumpOverlapMethod" : "probabilistic"
51+ }
52+
53+ class TestSimpleSPTMNetwork (unittest .TestCase ):
54+
55+ def _create_network (self , L4Params , L6aParams ):
56+ """
57+ Constructor.
58+ """
59+ network = Network ()
60+
61+ # Create network
62+ network = createL4L6aLocationColumn (network = network ,
63+ L4Params = L4Params ,
64+ L6aParams = L6aParams ,
65+ inverseReadoutResolution = None ,
66+ baselineCellsPerAxis = 6 ,
67+ suffix = "" )
68+
69+ network .initialize ()
70+ return network
71+
72+ def setUp (self ):
73+ registerAllAdvancedRegions ()
74+
75+ self ._params = cortical_params
76+
77+ L4Params = {param .split ("_" )[1 ]:value for param , value in self ._params .items () if param .startswith ("l4" )}
78+ L6aParams = {param .split ("_" )[1 ]:value for param , value in self ._params .items () if param .startswith ("l6a" )}
79+ # Configure L6a self._htm_parameters
80+ numModules = L6aParams ["moduleCount" ]
81+ L6aParams ["scale" ] = [L6aParams ["scale" ]] * numModules
82+ angle = L6aParams ["orientation" ] // numModules
83+ orientation = list (range (angle // 2 , angle * numModules , angle ))
84+ L6aParams ["orientation" ] = np .radians (orientation ).tolist ()
85+
86+ self .network = self ._create_network (L4Params , L6aParams )
87+
88+ def tearDown (self ):
89+ self .network = None
90+
91+ def _run_network (self , network ):
92+ """
93+ Run the network with fixed data.
94+ """
95+ motorInput = network .getRegion ("motorInput" )
96+ sensorInput = network .getRegion ("sensorInput" )
97+ motorInput .executeCommand ('addDataToQueue' , [0 ,0 ])
98+ sensorInput .executeCommand ('addDataToQueue' , [1 ,2 ,3 ], False , 0 )
99+
100+ network .run (1 )
101+
102+ L4Region = network .getRegion ("L4" )
103+ activeL4Cells = np .array (L4Region .getOutputArray ("activeCells" )).nonzero ()[0 ]
104+
105+ return activeL4Cells
106+
107+ def testAL246aCorticalColumnPickle (self ):
108+ """
109+ Test that L246aCorticalColumn can be pickled.
110+ """
111+ if sys .version_info [0 ] >= 3 :
112+ proto = 3
113+ else :
114+ proto = 2
115+ # Simple test: make sure that dumping / loading works...
116+ pickledColumn = pickle .dumps (self .network , proto )
117+ network2 = pickle .loads (pickledColumn )
118+ s1 = self ._run_network (self .network )
119+ s2 = self ._run_network (network2 )
120+ self .assertTrue (np .array_equal (s1 , s2 ))
121+
122+
123+ if __name__ == "__main__" :
124+ #import sys;sys.argv = ['', 'Test.testName']
125+ unittest .main ()
0 commit comments