Advertisement
sheiser1

run file

Feb 22nd, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. import importlib
  2. import sys
  3. import csv
  4. import datetime
  5.  
  6. from nupic.data.inference_shifter import InferenceShifter
  7. from nupic.frameworks.opf.modelfactory import ModelFactory
  8.  
  9. import nupic_anomaly_output
  10.  
  11.  
  12. DESCRIPTION = (
  13.   "Starts a NuPIC model from the model params returned by the swarm\n"
  14.   "and pushes each line of input from the gym into the model. Results\n"
  15.   "are written to an output file (default) or plotted dynamically if\n"
  16.   "the --plot option is specified.\n"
  17. )
  18. GYM_NAME = "mn_traffic_data_occupancy"
  19. DATA_DIR = "."
  20. MODEL_PARAMS_DIR = "./model_params"
  21. # '7/2/10 0:00'
  22. DATE_FORMAT = "%m/%d/%y %H:%M"
  23.  
  24.  
  25. def createModel(modelParams):
  26.   """
  27.  Given a model params dictionary, create a CLA Model. Automatically enables
  28.  inference for kw_energy_consumption.
  29.  :param modelParams: Model params dict
  30.  :return: OPF Model object
  31.  """
  32.   model = ModelFactory.create(modelParams)
  33.   model.enableInference({"predictedField": "occupancy"})
  34.   return model
  35.  
  36.  
  37.  
  38. def getModelParamsFromName(gymName):
  39.   """
  40.  Given a gym name, assumes a matching model params python module exists within
  41.  the model_params directory and attempts to import it.
  42.  :param gymName: Gym name, used to guess the model params module name.
  43.  :return: OPF Model params dictionary
  44.  """
  45.   importName = "model_params.%s_model_params" % (
  46.     gymName.replace(" ", "_").replace("-", "_")
  47.   )
  48.   print "Importing model params from %s" % importName
  49.   try:
  50.     importedModelParams = importlib.import_module(importName).MODEL_PARAMS
  51.   except ImportError:
  52.     raise Exception("No model params exist for '%s'. Run swarm first!"
  53.                     % gymName)
  54.   return importedModelParams
  55.  
  56.  
  57.  
  58. def runIoThroughNupic(inputData, model, gymName, plot):
  59.   """
  60.  Handles looping over the input data and passing each row into the given model
  61.  object, as well as extracting the result object and passing it into an output
  62.  handler.
  63.  :param inputData: file path to input data CSV
  64.  :param model: OPF Model object
  65.  :param gymName: Gym name, used for output handler naming
  66.  :param plot: Whether to use matplotlib or not. If false, uses file output.
  67.  """
  68.   inputFile = open(inputData, "rb")
  69.   csvReader = csv.reader(inputFile)
  70.   # skip header rows
  71.   csvReader.next()
  72.   csvReader.next()
  73.   csvReader.next()
  74.  
  75.   shifter = InferenceShifter()
  76.   if plot:
  77.     output = nupic_anomaly_output.NuPICPlotOutput(gymName)
  78.   else:
  79.     output = nupic_anomaly_output.NuPICFileOutput(gymName)
  80.  
  81.   counter = 0
  82.   for row in csvReader:
  83.     counter += 1
  84.     if (counter % 100 == 0):
  85.       print "Read %i lines..." % counter
  86.     timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT) #timestamp
  87.     consumption = float(row[1]) #row[0]
  88.     #speed = float(row[2]) #row[1]
  89.     #occupancy = float(row[3]) #row[2]
  90.     result = model.run({
  91.       "timestamp": timestamp, # timestamp
  92.       #"speed": speed,
  93.       #"occupancy": occupancy,
  94.       "occupancy": consumption
  95.     })
  96.  
  97.     if plot:
  98.       result = shifter.shift(result)
  99.  
  100.     prediction = result.inferences["multiStepBestPredictions"][1]
  101.     anomalyScore = result.inferences["anomalyScore"]
  102.     output.write(timestamp, consumption, prediction, anomalyScore) # timestamp --> '___'
  103.  
  104.   inputFile.close()
  105.   output.close()
  106.  
  107.  
  108.  
  109. def runModel(gymName, plot=False):
  110.   """
  111.  Assumes the gynName corresponds to both a like-named model_params file in the
  112.  model_params directory, and that the data exists in a like-named CSV file in
  113.  the current directory.
  114.  :param gymName: Important for finding model params and input CSV file
  115.  :param plot: Plot in matplotlib? Don't use this unless matplotlib is
  116.  installed.
  117.  """
  118.   print "Creating model from %s..." % gymName
  119.   model = createModel(getModelParamsFromName(gymName))
  120.   inputData = "%s/%s.csv" % (DATA_DIR, gymName.replace(" ", "_"))
  121.   runIoThroughNupic(inputData, model, gymName, plot)
  122.  
  123.  
  124.  
  125. if __name__ == "__main__":
  126.   print DESCRIPTION
  127.   plot = False
  128.   args = sys.argv[1:]
  129.   if "--plot" in args:
  130.     plot = True
  131.   runModel(GYM_NAME, plot=plot)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement