Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import importlib
- import sys
- import csv
- import datetime
- from nupic.data.inference_shifter import InferenceShifter
- from nupic.frameworks.opf.modelfactory import ModelFactory
- 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"
- )
- GYM_NAME = "mn_traffic_data_occupancy"
- DATA_DIR = "."
- MODEL_PARAMS_DIR = "./model_params"
- # '7/2/10 0:00'
- DATE_FORMAT = "%m/%d/%y %H:%M"
- def createModel(modelParams):
- """
- 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
- """
- model = ModelFactory.create(modelParams)
- model.enableInference({"predictedField": "occupancy"})
- return model
- def getModelParamsFromName(gymName):
- """
- Given a gym 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" % (
- gymName.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!"
- % gymName)
- return importedModelParams
- def runIoThroughNupic(inputData, model, gymName, 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(gymName)
- else:
- output = nupic_anomaly_output.NuPICFileOutput(gymName)
- 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) #timestamp
- consumption = float(row[1]) #row[0]
- #speed = float(row[2]) #row[1]
- #occupancy = float(row[3]) #row[2]
- result = model.run({
- "timestamp": timestamp, # timestamp
- #"speed": speed,
- #"occupancy": occupancy,
- "occupancy": consumption
- })
- if plot:
- result = shifter.shift(result)
- prediction = result.inferences["multiStepBestPredictions"][1]
- anomalyScore = result.inferences["anomalyScore"]
- output.write(timestamp, consumption, prediction, anomalyScore) # timestamp --> '___'
- inputFile.close()
- output.close()
- def runModel(gymName, 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..." % gymName
- model = createModel(getModelParamsFromName(gymName))
- inputData = "%s/%s.csv" % (DATA_DIR, gymName.replace(" ", "_"))
- runIoThroughNupic(inputData, model, gymName, plot)
- if __name__ == "__main__":
- print DESCRIPTION
- plot = False
- args = sys.argv[1:]
- if "--plot" in args:
- plot = True
- runModel(GYM_NAME, plot=plot)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement