本文整理匯總了Python中nupic.frameworks.opf.predictionmetricsmanager.MetricsManager.update方法的典型用法代碼示例。如果您正苦於以下問題:Python MetricsManager.update方法的具體用法?Python MetricsManager.update怎麽用?Python MetricsManager.update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nupic.frameworks.opf.predictionmetricsmanager.MetricsManager
的用法示例。
在下文中一共展示了MetricsManager.update方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: HotGym
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
class HotGym(CLASanityModel):
def __init__(self, model, inputs):
super(HotGym, self).__init__(model)
self.model = model
self.inputs = inputs
self.metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(), model.getInferenceType())
self.counter = 0
self.timestampStr = ""
self.consumptionStr = ""
def step(self):
self.counter += 1
self.timestampStr, self.consumptionStr = self.inputs.next()
timestamp = datetime.datetime.strptime(self.timestampStr, "%m/%d/%y %H:%M")
consumption = float(self.consumptionStr)
result = self.model.run({"timestamp": timestamp, "kw_energy_consumption": consumption})
result.metrics = self.metricsManager.update(result)
if self.counter % 100 == 0:
print "Read %i lines..." % self.counter
print (
"After %i records, 1-step altMAPE=%f"
% (
self.counter,
result.metrics[
"multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=kw_energy_consumption"
],
)
)
def getInputDisplayText(self):
return (["time", self.timestampStr], ["power consumption (kW)", self.consumptionStr])
示例2: runHotgym
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runHotgym():
model = createModel()
model.enableInference({'predictedField': 'consumption'})
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
with open (_INPUT_FILE_PATH) as fin:
reader = csv.reader(fin)
headers = reader.next()
reader.next()
reader.next()
for i, record in enumerate(reader, start=1):
modelInput = dict(zip(headers, record))
modelInput["consumption"] = float(modelInput["consumption"])
modelInput["timestamp"] = datetime.datetime.strptime(
modelInput["timestamp"], "%m/%d/%y %H:%M")
result = model.run(modelInput)
result.metrics = metricsManager.update(result)
isLast = i == _NUM_RECORDS
if i % 100 == 0 or isLast:
_LOGGER.info("After %i records, 1-step altMAPE=%f", i,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=consumption"])
if isLast:
break
示例3: runIoThroughNupic
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runIoThroughNupic(inputData, model, name, plot):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if len(plot) == 0:
for field in SWARM_DESCRIPTION["includedFields"]:
plot.append(field["fieldName"])
output = nupic_output.NuPICFileOutput(name, plot)
else:
output = nupic_output.NuPICPlotOutput(name, plot)
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
data = {}
fldCounter = 0
for field in SWARM_DESCRIPTION["includedFields"]:
data[field["fieldName"]] = translate_data(field["fieldType"], row[fldCounter])
fldCounter += 1
result = model.run(data)
result.metrics = metricsManager.update(result)
if options.verbose is not None and counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f" % (counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field="+PREDICTED_FIELD]))
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
vals = []
for field in plot:
vals.append(data[field])
output.write(vals, prediction)
inputFile.close()
output.close()
示例4: runCPU
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runCPU():
"""Poll CPU usage, make predictions, and plot the results. Runs forever."""
# Create the model for predicting CPU usage.
model = ModelFactory.create(model_params.MODEL_PARAMS)
model.enableInference({'predictedField': 'cpu'})
# Create a metrics manager for computing an error metric.
metricsManager = MetricsManager(METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
# The shifter will align prediction and actual values.
shifter = InferenceShifter()
# Keep the last WINDOW predicted and actual values for plotting.
actHistory = deque([0.0] * WINDOW, maxlen=60)
predHistory = deque([0.0] * WINDOW, maxlen=60)
# Initialize the plot lines that we will update with each new record.
actline, = plot(range(WINDOW), actHistory)
predline, = plot(range(WINDOW), predHistory)
# Set the y-axis range.
actline.axes.set_ylim(0, 100)
predline.axes.set_ylim(0, 100)
while True:
s = time.time()
# Get the CPU usage.
cpu = psutil.cpu_percent()
# Run the input through the model and shift the resulting prediction.
modelInput = {'cpu': cpu}
result = shifter.shift(model.run(modelInput))
# Compute an error metric (not currently used).
result.metrics = metricsManager.update(result)
# Update the trailing predicted and actual value deques.
inference = result.inferences['multiStepBestPredictions'][5]
if inference is not None:
actHistory.append(result.rawInput['cpu'])
predHistory.append(inference)
# Redraw the chart with the new data.
actline.set_ydata(actHistory) # update the data
predline.set_ydata(predHistory) # update the data
draw()
# Make sure we wait a total of 2 seconds per iteration.
time.sleep(SECONDS_PER_STEP - (time.time() - s))
示例5: runIoThroughNupic
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runIoThroughNupic(inputData, model, gymName, plot):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_output.NuPICPlotOutput([gymName])
else:
output = nupic_output.NuPICFileOutput([gymName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT)
consumption = float(row[1])
result = model.run({
"timestamp": timestamp,
"kw_energy_consumption": consumption
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f" % (counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=kw_energy_consumption"]))
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
output.write([timestamp], [consumption], [prediction])
if plot and counter % 20 == 0:
output.refreshGUI()
inputFile.close()
output.close()
示例6: runHotgym
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runHotgym():
model = createModel()
model.enableInference({'predictedField': 'Volume'})
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
with open (findDataset(_DATA_PATH)) as fin:
reader = csv.reader(fin)
headers = reader.next()
for i, record in enumerate(reader, start=1):
modelInput = dict(zip(headers, record))
modelInput["Volume"] = float(modelInput["Volume"])
result = model.run(modelInput)
result.metrics = metricsManager.update(result)
isLast = i == _NUM_RECORDS
if i % 100 == 0 or isLast:
print result.metrics
if isLast:
break
示例7: runIoThroughNupic
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runIoThroughNupic(inputData, model, modelName, plot):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
headers = csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_anomaly_output.NuPICPlotOutput(modelName)
else:
output = nupic_anomaly_output.NuPICFileOutput(modelName)
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT)
consumption = float(row[1])
result = model.run({
"Time": timestamp,
PREDICTED_FIELD: consumption
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=%s" % PREDICTED_FIELD])
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
anomalyScore = result.inferences["anomalyScore"]
output.write(timestamp, consumption, prediction, anomalyScore)
inputFile.close()
output.close()
示例8: runIoThroughNupic
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runIoThroughNupic( inputData, model, textName, word_list ):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
output = nupic_output.NuPICFileOutput([textName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
model.enableLearning()
counter = 0
for row in csvReader:
counter += 1
reset_flag = bool( row[0] )
word_num = int( row[1] )
if reset_flag:
print( 'resetting model' )
model.resetSequenceStates()
result = model.run({
"word_num": word_num
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=word_num"])
model.finishLearning()
model.save( MODEL_DIR )
inputFile.close()
output.close()
示例9: runIoThroughNupic
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runIoThroughNupic(inputData, model, dataName, plot):
inputFile = open(inputData, "rU")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_output.NuPICPlotOutput([dataName])
else:
output = nupic_output.NuPICFileOutput([dataName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
y = float(row[0])
result = model.run({
"y": y
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step nrmse=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='nrmse':steps=1:window=1000000:"
"field=y"])
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
output.write([0], [y], [prediction])
inputFile.close()
output.close()
示例10: runHotgym
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runHotgym():
model = createModel()
model.enableInference({'predictedField': 'consumption'})
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
with open (findDataset(_DATA_PATH)) as fin:
reader = csv.reader(fin)
headers = reader.next()
print headers[0], headers[1]
record = reader.next()
print record
record = reader.next()
print record
record1 = reader.next()
timestamp1 = time.mktime(datetime.datetime.strptime(record1[0], "%m/%d/%y %H:%M").timetuple())
print timestamp1
record2 = reader.next()
timestamp2 = time.mktime(datetime.datetime.strptime(record2[0], "%m/%d/%y %H:%M").timetuple())
print record2[0], record2[1]
print timestamp2-timestamp1
for i, record in enumerate(reader, start=1):
modelInput = dict(zip(headers, record))
modelInput["consumption"] = float(modelInput["consumption"])
modelInput["timestamp"] = datetime.datetime.strptime(
modelInput["timestamp"], "%m/%d/%y %H:%M")
result = model.run(modelInput)
#print result
#import sys; sys.exit()
result.metrics = metricsManager.update(result)
isLast = i == _NUM_RECORDS
if i % 10 == 0 or isLast:
_LOGGER.info("After %i records, 1-step altMAPE=%f", i,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=consumption"])
if isLast:
break
示例11: xrange
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
for i in xrange(len(df)):
inputRecord = getInputRecord(df, predictedField, i)
result = model.run(inputRecord)
trueBucketIndex.append(model._getClassifierInputRecord(inputRecord).bucketIndex)
# inspect SP
sp = model._getSPRegion().getSelf()._sfdr
spOutput = model._getSPRegion().getOutputData('bottomUpOut')
spActiveCellsCount[spOutput.nonzero()[0]] += 1
tp = model._getTPRegion()
tm = tp.getSelf()._tfdr
activeColumn = tm.getActiveCells()
activeCellNum.append(len(activeColumn))
result.metrics = metricsManager.update(result)
negLL = result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='negativeLogLikelihood':steps=%d:window=1000:"
"field=%s"%(_options.stepsAhead, predictedField)]
if i % 100 == 0 and i>0:
negLL = result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='negativeLogLikelihood':steps=%d:window=1000:"
"field=%s"%(_options.stepsAhead, predictedField)]
nrmse = result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='nrmse':steps=%d:window=1000:"
"field=%s"%(_options.stepsAhead, predictedField)]
numActiveCell = np.mean(activeCellNum[-100:])
print "After %i records, %d-step negLL=%f nrmse=%f ActiveCell %f " % \
示例12: Client
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
class Client(object):
""" Simple OPF client. """
def __init__(self, modelConfig, inferenceArgs, metricSpecs, sourceSpec,
sinkSpec=None):
"""Initialization.
Args:
modelConfig: The model config dict.
metricSpecs: A sequence of MetricSpec instances.
sourceSpec: Path to the source CSV file.
sinkSpec: Path to the sink CSV file.
"""
self.model = ModelFactory.create(modelConfig)
self.model.enableInference(inferenceArgs)
self.metricsManager = MetricsManager(metricSpecs, self.model.getFieldInfo(),
self.model.getInferenceType())
self.sink = None
if sinkSpec is not None:
# TODO: make this work - sinkSpec not yet supported.
raise NotImplementedError('The sinkSpec is not yet implemented.')
#self.sink = BasicPredictionLogger(
# self.model.getFieldInfo(), sinkSpec, 'myOutput',
# self.model.getInferenceType())
#self.sink.setLoggedMetrics(
# self.metricsManager.getMetricLabels())
self.datasetReader = BasicDatasetReader(sourceSpec)
def __iter__(self):
return self
def _processRecord(self, inputRecord):
modelResult = self.model.run(inputRecord)
modelResult.metrics = self.metricsManager.update(modelResult)
if self.sink:
self.sink.writeRecord(modelResult)
return modelResult
def next(self):
record = self.datasetReader.next()
return self._processRecord(record)
def skipNRecords(self, n):
for i in range(n):
self.datasetReader.next()
def nextTruthPrediction(self, field):
record = self.datasetReader.next()
prediction=self._processRecord(record).inferences['prediction'][0]
truth=record[field]
return truth, prediction
def run(self):
result = None
while True:
try:
result = self.next()
#print result
except StopIteration:
break
return result
示例13: HotGym
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
class HotGym(CLASanityModel):
def __init__(self, model, inputs):
super(HotGym, self).__init__(model)
self.model = model
self.inputs = inputs
self.metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
self.counter = 0
self.timestampStr = ""
self.consumptionStr = ""
self.shifter = InferenceShifter()
self.dates = deque(maxlen=WINDOW)
self.convertedDates = deque(maxlen=WINDOW)
self.actualValues = deque([0.0] * WINDOW, maxlen=WINDOW)
self.predictedValues = deque([0.0] * WINDOW, maxlen=WINDOW)
self.linesInitialized = False
self.actualLines = None
self.predictedLines = None
self.shouldScheduleDraw = True
fig = plt.figure(figsize=(6.1, 11))
plotCount = 1
gs = gridspec.GridSpec(plotCount, 1)
self.graph = fig.add_subplot(gs[0, 0])
plt.xlabel('Date')
plt.ylabel('Consumption (kW)')
plt.tight_layout()
def draw(self):
self.shouldScheduleDraw = True
self.graph.relim()
self.graph.autoscale_view(True, True, True)
plt.draw()
plt.legend(('actual', 'predicted'), loc=3)
def step(self):
self.counter += 1
self.timestampStr, self.consumptionStr = self.inputs.next()
timestamp = datetime.datetime.strptime(self.timestampStr, "%m/%d/%y %H:%M")
consumption = float(self.consumptionStr)
result = self.model.run({
"timestamp": timestamp,
"kw_energy_consumption": consumption,
})
result.metrics = self.metricsManager.update(result)
if self.counter % 100 == 0:
print "Read %i lines..." % self.counter
print ("After %i records, 1-step altMAPE=%f" % (
self.counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=kw_energy_consumption"]))
result = self.shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
if not self.linesInitialized:
self.dates += deque([timestamp]*WINDOW)
self.convertedDates += [date2num(date) for date in self.dates]
addedLinesActual, = self.graph.plot(
self.dates, self.actualValues, 'r'
)
self.actualLines = addedLinesActual
predictedLinesActual, = self.graph.plot(
self.dates, self.predictedValues, 'b'
)
self.predictedLines = predictedLinesActual
self.graph.xaxis.set_major_formatter(DateFormatter("%H:%M"))
self.linesInitialized = True
self.dates.append(timestamp)
self.convertedDates.append(date2num(timestamp))
self.actualValues.append(consumption)
self.predictedValues.append(prediction)
self.actualLines.set_xdata(self.convertedDates)
self.actualLines.set_ydata(self.actualValues)
self.predictedLines.set_xdata(self.convertedDates)
self.predictedLines.set_ydata(self.predictedValues)
if self.shouldScheduleDraw:
# If we're stepping the model really quickly, coalesce the redraws.
self.shouldScheduleDraw = False
t = threading.Timer(0.2, self.draw)
t.start()
def getInputDisplayText(self):
return (["time", self.timestampStr],
["power consumption (kW)", self.consumptionStr])
示例14: runIoThroughNupic
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
def runIoThroughNupic(inputData, model, gymName, plot):
inputFile = open(inputData, "rb")
#csvReader = csv.reader(inputFile)
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_output.NuPICPlotOutput([gymName])
else:
output = nupic_output.NuPICFileOutput([gymName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
#not quite sure best way to handle missing data, but for now use
#last good value
for row in csvReader:
counter += 1
timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT)
try:
temp = float(row[1])
except:
temp = temp
try:
dewpt = float(row[2])
except:
dewpt=dewpt
try:
wind = float(row[3])
except:
wind=wind
is_daylight = int(row[4])
elapsed = int(row[5])
kwh = float(row[6])
result = model.run({
"timestamp": timestamp,
"temp":temp,
"dewpt":dewpt,
"wind": wind,
"is_daylight":is_daylight,
"elapsed":elapsed,
"kwh":kwh,
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=10:"
"field=kwh"])
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
output.write([timestamp], [kwh], [prediction])
inputFile.close()
output.close()
示例15: ModelController
# 需要導入模塊: from nupic.frameworks.opf.predictionmetricsmanager import MetricsManager [as 別名]
# 或者: from nupic.frameworks.opf.predictionmetricsmanager.MetricsManager import update [as 別名]
#.........這裏部分代碼省略.........
# Multiprocessing entry point
def run(self):
# spawn a worker thread to queue incoming data
worker = threading.Thread(target=self.queuer)
worker.daemon = True
worker.start()
# Compute items in queue
# spinner spinner hey we have a spinner
while True:
while not self.data_queue:
time.sleep(0.1)
# get oldest queue item and compute
datapoint = self.data_queue.popleft()
result = self.iterate(datapoint)
# send result to Pipe
self.conn.send(result)
# just in case
worker.join()
# Data queuer thread
def queuer(self):
while True:
# send all incoming data to the deque
# should block when there are no incoming messages
self.data_queue.append(self.conn.recv())
# initialize metrics manager
def init_metrics(self):
metric_list = []
# create a MetricSpec for each level of inference
for i in self.steps:
metric_list.append(
MetricSpec(field='value',
metric='multiStep',
inferenceElement='multiStepBestPredictions',
params={'errorMetric': 'altMAPE',
'window': 384,
'steps': i
}
)
)
# convert specs to tuple and initialize metrics manager
metric_specs = tuple(metric_list)
self.metrics_manager = MetricsManager(metric_specs,
self.model.getFieldInfo(),
self.model.getInferenceType())
# for save/load, use alg/models dir always
# load model from file
def load_model(self, name):
name = os.path.abspath(name)
self.model = ModelFactory.loadFromCheckpoint(name)
# save model to file
def save_model(self, name):
name = os.path.abspath(name)
self.model.save(name)
# create model from params
def create_model(self, params):
# create model
self.model = ModelFactory.create(params)
# set init params
self.model.enableLearning()
self.model.enableInference({'predictedField': 'value'})
# init metrics
self.init_metrics()
# import model params from file
def get_params(self, name):
try:
params = importlib.import_module(name).MODEL_PARAMS
except ImportError:
raise Exception("No model exists called %s, need to run swarming algorithm" % name)
return params
# iterate through a single datapoint
def iterate(self, datapoint):
# compute
result = self.model.run(datapoint)
# run metrics
metrics = self.metrics_manager.update(result)
# scrape metrics and construct clean version
result.metrics = {}
for key in metrics:
for i in self.steps:
string = ':steps=' + str(i) + ':'
if string in key:
result.metrics[i] = metrics[key]
return (self.market, result)