Skip to content

Instantly share code, notes, and snippets.

@rhyolight
Created September 12, 2016 20:11
Show Gist options
  • Save rhyolight/2cb5935142a2cb8511967da17dc4ad28 to your computer and use it in GitHub Desktop.
Save rhyolight/2cb5935142a2cb8511967da17dc4ad28 to your computer and use it in GitHub Desktop.
import importlib
import sys
import csv
import datetime
import pprint
from nupic.data import fieldmeta
from nupic.data.inference_shifter import InferenceShifter
from nupic.frameworks.opf.modelfactory import ModelFactory
from nupic.frameworks.opf.common_models.cluster_params import (
getScalarMetricWithTimeOfDayAnomalyParams)
import nupic_anomaly_output
DESCRIPTION = (
"Starts a NuPIC model from the model params returned by the swarm\n"
"and pushes each line of input from the gym into the model. Results\n"
"are written to an output file (default) or plotted dynamically if\n"
"the --plot option is specified.\n"
)
APPLICATION = "ddos"
DATA_DIR = "."
MODEL_PARAMS_DIR = "./model_params"
# '7/2/10 0:00'
DATE_FORMAT = "%m/%d/%y %H:%M"
def createModel(params):
"""
Given a model params dictionary, create a CLA Model. Automatically enables
inference for kw_energy_consumption.
:param modelParams: Model params dict
:return: OPF Model object
"""
pprint.pprint (params)
model = ModelFactory.create(modelConfig=params["modelConfig"])
model.enableLearning()
model.enableInference(params["inferenceArgs"])
return model
def runIoThroughNupic(inputData, model, app, plot):
"""
Handles looping over the input data and passing each row into the given model
object, as well as extracting the result object and passing it into an output
handler.
:param inputData: file path to input data CSV
:param model: OPF Model object
:param gymName: Gym name, used for output handler naming
:param plot: Whether to use matplotlib or not. If false, uses file output.
"""
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_anomaly_output.NuPICPlotOutput(app)
else:
output = nupic_anomaly_output.NuPICFileOutput(app)
counter = 0
for row in csvReader:
counter += 1
if (counter % 100 == 0):
print "Read %i lines..." % counter
#timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT)
#consumption = float(row[1])
packets = int(row[8])
bytes = int(row[9])
duration = float(row[10])
result = model.run({
"packets": packets,
"bytes": bytes,
"duration": duration
})
if plot:
result = shifter.shift(result)
#print result
#prediction = result.inferences["multiStepBestPredictions"][1]
anomalyScore = result.inferences["anomalyScore"]
output.write(packets, bytes,duration, anomalyScore)
#output.write(duration,packets,0, anomalyScore)
inputFile.close()
output.close()
def getModelParamsFromName(app):
"""
Given a app name, assumes a matching model params python module exists within
the model_params directory and attempts to import it.
:param gymName: Gym name, used to guess the model params module name.
:return: OPF Model params dictionary
"""
importName = "model_params.%s_model_params" % (
app.replace(" ", "_").replace("-", "_")
)
print "Importing model params from %s" % importName
try:
importedModelParams = importlib.import_module(importName).MODEL_PARAMS
except ImportError:
raise Exception("No model params exist for '%s'. Run swarm first!"
% app)
return importedModelParams
def runModel(app, plot=False):
"""
Assumes the gynName corresponds to both a like-named model_params file in the
model_params directory, and that the data exists in a like-named CSV file in
the current directory.
:param gymName: Important for finding model params and input CSV file
:param plot: Plot in matplotlib? Don't use this unless matplotlib is
installed.
"""
print "Creating model from %s..." % app
model = createModel(getModelParamsFromName(app))
#model = createModel()
# inputData = "%s/%s.csv" % (DATA_DIR, app.replace(" ", "_"))
inputData="./3.csv"
runIoThroughNupic(inputData, model, app, plot)
if __name__ == "__main__":
print DESCRIPTION
plot = False
args = sys.argv[1:]
if "--plot" in args:
plot = True
runModel(APPLICATION, plot=plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment