Skip to content

Commit

Permalink
Updated to use latest history server updates for connection and activ…
Browse files Browse the repository at this point in the history
…e column display.
  • Loading branch information
rhyolight committed Aug 11, 2016
1 parent 6a024c4 commit 8d22a78
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 53 deletions.
32 changes: 19 additions & 13 deletions nupic_history/sp_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,31 @@ def _conjureInput(self, iteration=None, **kwargs):
if self._retrieveFromSp(iteration):
return compressSdr(self._input)
else:
return self._redisClient.getLayerState(
return self._redisClient.getLayerStateByIteration(
self.getId(), SNAPS.INPUT, iteration
)


def _conjureActiveColumns(self, iteration=None, **kwargs):
if self._retrieveFromSp(iteration):
return compressSdr(self._activeColumns)
def _conjureActiveColumns(self, iteration=None, columnIndex=None):
if self._retrieveFromSp(iteration, columnIndex):
out = compressSdr(self._activeColumns)
else:
return self._redisClient.getLayerState(
self.getId(), SNAPS.ACT_COL, iteration
)
if columnIndex is None:
out = self._redisClient.getLayerState(
self.getId(), SNAPS.ACT_COL, iteration
)
else:
out = self._redisClient.getActiveColumnsByColumn(
self.getId(), columnIndex, self.getIteration() + 1
)
return out


def _conjureOverlaps(self, iteration=None, **kwargs):
if self._retrieveFromSp(iteration):
return self._sp.getOverlaps().tolist()
else:
return self._redisClient.getLayerState(
return self._redisClient.getLayerStateByIteration(
self.getId(), SNAPS.OVERLAPS, iteration
)

Expand Down Expand Up @@ -346,12 +352,12 @@ def _conjurePermanences(self, iteration=None, columnIndex=None):
out.append([round(perm, 2) for perm in perms.tolist()])
else:
if columnIndex is None:
out = self._redisClient.getPerColumnState(
out = self._redisClient.getStateByIteration(
self.getId(), SNAPS.PERMS, iteration, numColumns
)
else:
out = self._redisClient.getPerIterationState(
self.getId(), SNAPS.PERMS, columnIndex, self.getIteration()
out = self._redisClient.getStatebyColumn(
self.getId(), SNAPS.PERMS, columnIndex, self.getIteration() + 1
)
return out

Expand All @@ -363,7 +369,7 @@ def _conjureActiveDutyCycles(self, iteration=None, **kwargs):
sp.getActiveDutyCycles(dutyCycles)
return dutyCycles.tolist()
else:
return self._redisClient.getLayerState(
return self._redisClient.getLayerStateByIteration(
self.getId(), SNAPS.ACT_DC, iteration
)

Expand All @@ -375,6 +381,6 @@ def _conjureOverlapDutyCycles(self, iteration=None, **kwargs):
sp.getOverlapDutyCycles(dutyCycles)
return dutyCycles.tolist()
else:
return self._redisClient.getLayerState(
return self._redisClient.getLayerStateByIteration(
self.getId(), SNAPS.OVP_DC, iteration
)
39 changes: 33 additions & 6 deletions nupic_history/sp_redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,37 @@ def getMaxIteration(self, spid):
return maxIteration


def getLayerState(self, spid, stateType, iteration):
def getLayerStateByIteration(self, spid, stateType, iteration):
key = self.GLOBAL_VALS.format(spid, iteration, stateType)
return self._getSnapshot(stateType, key)


def getPerColumnState(self, spid, stateType, iteration, numColumns):
def getActiveColumnsByColumn(self, spid, columnIndex, maxIteration):
print columnIndex
print type(columnIndex)
out = []
searchKey = self.GLOBAL_VALS.format(spid, "*", SNAPS.ACT_COL)
print "searching for {}".format(searchKey)
keys = self._redis.keys(searchKey)
for iteration in xrange(0, maxIteration):
possibleKey = self.GLOBAL_VALS.format(spid, iteration, SNAPS.ACT_COL)
print possibleKey
found = None
if possibleKey in keys:
activeColumns = self._getSnapshot(SNAPS.ACT_COL, possibleKey)
print activeColumns
if columnIndex in activeColumns["indices"]:
found = 1
else:
found = 0
else:
print "** WARNING ** Missing {} data for column {} iteration {} (key: {})"\
.format(SNAPS.ACT_COL, columnIndex, iteration, possibleKey)
out.append(found)
return out


def getStateByIteration(self, spid, stateType, iteration, numColumns):
out = []
# Before making a DB call for every column, let's ensure that there are
# values stored for this type of snapshot.
Expand All @@ -105,18 +130,20 @@ def getPerColumnState(self, spid, stateType, iteration, numColumns):
return out


def getPerIterationState(self, spid, stateType, columnIndex, iterations):
def getStatebyColumn(self, spid, stateType, columnIndex, maxIteration):
out = []
searchString = self.COLUMN_VALS.format(spid, "*", columnIndex, stateType)
keys = sorted(self._redis.keys(searchString))
from pprint import pprint; pprint(keys)
for iteration in xrange(0, iterations):
keys = self._redis.keys(searchString)
for iteration in xrange(0, maxIteration):
possibleKey = self.COLUMN_VALS.format(
spid, iteration, columnIndex, stateType
)
found = None
if possibleKey in keys:
found = self._getSnapshot(stateType, possibleKey)
else:
print "** WARNING ** Missing {} data for column {} iteration {} (key: {})"\
.format(stateType, columnIndex, iteration, possibleKey)
out.append(found)
return out

Expand Down
43 changes: 29 additions & 14 deletions test1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@


spHistory = SpHistory()
iterations = 10


def createSpatialPooler(inputSize):
return SP(
inputDimensions=(inputSize,),
columnDimensions=(2048,),
columnDimensions=(200,),
potentialRadius=16,
potentialPct=0.85,
globalInhibition=True,
Expand All @@ -37,7 +38,7 @@ def createSpatialPooler(inputSize):
def runSaveTest():
spHistory.nuke()

inputSize = 600
inputSize = 3

rawSp = createSpatialPooler(inputSize)

Expand All @@ -58,7 +59,6 @@ def runSaveTest():

start = time.time()

iterations = 10

for _ in range(0, iterations):
encoding = np.zeros(shape=(inputSize,))
Expand All @@ -72,8 +72,7 @@ def runSaveTest():

print "\nSTORAGE: {} iterations took {} seconds.\n\n".format(iterations, (end - start))

# This SP's history can be retrieved with an id.
return sp.getId()
return sp


def retrieveByIteration(sp):
Expand All @@ -97,25 +96,41 @@ def retrieveByIteration(sp):
)


def retrievePermanencesByColumn(sp, column):
def retrieveSnapshotByColumn(snapshot, sp, column):
start = time.time()
# We can playback the life of one column.
data = sp.getState(SNAPS.PERMS, columnIndex=column)[SNAPS.PERMS]
data = sp.getState(snapshot, columnIndex=column)[snapshot]
end = time.time()
print "\nRETRIEVAL of column {} ({} iterations) took {} seconds.".format(
column, sp.getIteration(), (end - start)
print "\nRETRIEVAL of column {} {} ({} iterations) took {} seconds.".format(
column, snapshot, sp.getIteration(), (end - start)
)
return data


def runFetchTest(spid):
def runFetchTest(sp):
spid = sp.getId()
print "Fetching sp {}".format(spid)
sp = spHistory.get(spid)
history = sp.getState(
SNAPS.PERMS, SNAPS.CON_SYN, columnIndex=0
)
print history
# retrieveByIteration(sp)
# columnHistory = retrieveSnapshotByColumn(SNAPS.PERMS, sp, 0)
# assert len(columnHistory) == iterations

retrieveByIteration(sp)

retrievePermanencesByColumn(sp, 0)
def runHistoryTest():
sp = runSaveTest()
time.sleep(1)
history = sp.getState(
SNAPS.PERMS, SNAPS.ACT_COL, columnIndex=199
)
print history


if __name__ == "__main__":
spid = runSaveTest()
runFetchTest(spid)
runHistoryTest()
# spid = runSaveTest()
# time.sleep(5)
# runFetchTest(spid)
19 changes: 0 additions & 19 deletions test2.py

This file was deleted.

2 changes: 1 addition & 1 deletion webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class History:
def GET(self, spId, columnIndex):
sp = spFacades[spId]
history = sp.getState(
SNAPS.PERMS, columnIndex=int(columnIndex)
SNAPS.PERMS, SNAPS.ACT_COL, columnIndex=int(columnIndex)
)
return json.dumps(history)

Expand Down

0 comments on commit 8d22a78

Please sign in to comment.