-
Notifications
You must be signed in to change notification settings - Fork 14
/
runner.py
232 lines (186 loc) · 7.13 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# The MIT License (MIT)
#
# Copyright (c) 2013 Numenta, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import numpy
import itertools
import json
import cPickle as pickle
import zlib
from time import (sleep)
import threading
import multiprocessing
import copy
import datetime
import pymongo
import gevent
import pprint
from collections import namedtuple
from Queue import Empty
import Queue
from experiment_db import ExperimentDB
import nupic.frameworks.opf.opfutils
from nupic.frameworks.opf.opfutils import (InferenceType,
InferenceElement)
import nupic.frameworks.opf.opfhelpers as opfhelpers
import nupic.frameworks.opf.opfbasicenvironment as opfbasicenv
from nupic.frameworks.opf.modelfactory import ModelFactory
from nupic.data.dictutils import DictObj
############################################################################
class Runner(object):
__FILE_SCHEME = "file://"
_BATCH_INTERVAL = 10
CEREBRO_DB_NAME = "CerebroDB"
def __init__(self,
name,
modelDescription,
control,
dataset,
params=None):
self._modelDescription = modelDescription
if modelDescription is not None:
self._model = ModelFactory.create(modelDescription)
self._model.enableInference(control['inferenceArgs'])
self._maxiterations = control['iterationCount']
self.name = name
self._dataset = dataset
self.predictedField = control['inferenceArgs']["predictedField"] #modelDescription['predictedField']
#Figure out prediction field
fieldInfo = self._model.getFieldInfo()
self.fieldNames = [info.name for info in fieldInfo]
# State variables
self.fields = None
self.predictionFieldIndex = self.fieldNames.index(self.predictedField)
self.fieldRanges = self.getFieldRanges()
# We store the prediction from the previous timestep
# so that we can compare it to the current timestep
self.prevPredictedCols = None
self.prevPredictedConfs = None
self.prevTPPredicted = None
self.prevSensorPredicted = None
self._iteration = 0
self._maxwait = 25 # *0.2 for seconds
self._nwait = 0
# Helper process
self.process = None
# -----------------------------------------------------------------------
# Communication objects
# -----------------------------------------------------------------------
self._stop = threading.Event()
self._isFinished = threading.Event()
self._batching = threading.Event()
self._dataQ = Queue.Queue()
############################################################################
def getModel(self):
return self._model
############################################################################
def getFieldRanges(self):
return self.fieldRanges
############################################################################
def getFieldNames(self):
return self.fieldNames
############################################################################
def run(self):
import gevent
from gevent import monkey; monkey.patch_all()
# p = multiprocessing.Process(target = self._runExperimentLoop,
# args=(self._dataQ,))
# p.daemon = True
# try:
# p.start()
# except:
# raise
import gevent.queue as gevent_queue
self._dataQ = Queue.Queue()
gevent.spawn(self._runExperimentLoop, self._dataQ)
def load(self):
import gevent
from gevent import monkey; monkey.patch_all()
import gevent.queue as gevent_queue
self._dataQ = Queue.Queue()
gevent.spawn(self._runExperimentLoadLoop, self._dataQ)
def _runExperimentLoadLoop(self, queue):
collection = ExperimentDB.getExperimentDB(self.name)
experimentData = collection.find()
for record in experimentData:
self._dataQ.put(record)
gevent.sleep(0)
############################################################################
def stop(self):
self._stop.set()
############################################################################
def isFinished(self):
return self._isFinished.isSet()
############################################################################
def __unsetBatchMode(self):
self._batching.clear()
def getNewData(self):
""" Gets the most recent predictions from the ExperimentRunner.
Note: Once this function is called, the data is consumed and it will no
longer be stored in the ExperimentRunner
"""
data = []
while True:
shouldBlock = True
timeout = 0.2
try:
newData = self._dataQ.get(shouldBlock, timeout)
data.append(newData)
if self._isFinished.isSet() or len(data) >= self._BATCH_INTERVAL:
break
except Empty:
if self._nwait >= self._maxwait:
self._isFinished.set()
return None
self._nwait += 1
return data
############################################################################
def getFieldRanges(self):
return self.fieldRanges
def getFieldRanges(self):
encoder = self._model._getSensorRegion().getSelf().encoder
assert isinstance(encoder, nupic.encoders.multi.MultiEncoder)
# Gather the encoded fields
self.fields = [x[0] for x in encoder.getDescription()]
fieldRanges = {}
encoderWidth = encoder.getWidth()
description = encoder.description
for i, (fieldname, offset) in enumerate(encoder.description):
nextOffset = encoderWidth
if i < len(description) - 1:
nextOffset = description[i+1][1]
fieldRanges[fieldname] = (offset, nextOffset)
return fieldRanges
def auxText(self, experimentData, timestep):
return ""
def getDataAtTime(self, dataInput):
timestep = int(dataInput['timestep'])
collection = ExperimentDB.getExperimentDB(self.name)
experimentData = collection.find_one({"_id":timestep})
return experimentData
def setName(self, name):
oldname = self.name
if not self._isFinished.is_set():
return {'oldname': oldname, 'name': self.name, 'error': \
'Experiment is currently running.'}
result = ExperimentDB.rename(oldname, name)
if 'error' not in result:
self.name = name
return result