Issue with getting anomaly parameters instead of swarming

#1

I tried to change One Hot Gym Prediction to use getScalarMetricWithTimeOfDayAnomalyParams instead of swarming but I’m getting the following error:

Exception has occurred: ValueError
Unknown field name 'c0' in input record. Known fields are 'timestamp, kw_energy_consumption'.
This could be because input headers are mislabeled, or because input data rows do not contain a value for 'c0'.
  File "C:\Python27\Lib\site-packages\nupic\encoders\base.py", line 229, in _getInputValue
    fieldName, knownFields, fieldName
  File "C:\Python27\Lib\site-packages\nupic\encoders\multi.py", line 106, in encodeIntoArray
    encoder.encodeIntoArray(self._getInputValue(obj, name), output[offset:])
  File "C:\Python27\Lib\site-packages\nupic\regions\record_sensor.py", line 377, in compute
    self.encoder.encodeIntoArray(data, outputs["dataOut"])
  File "C:\Python27\Lib\site-packages\nupic\bindings\regions\PyRegion.py", line 186, in guardedCompute
    return self.compute(inputs, DictReadOnlyWrapper(outputs))
  File "C:\Python27\Lib\site-packages\nupic\bindings\engine_internal.py", line 1559, in compute
    return _engine_internal.Region_compute(self)
  File "C:\Python27\Lib\site-packages\nupic\engine\__init__.py", line 435, in compute
    return self._region.compute()
  File "C:\Python27\Lib\site-packages\nupic\frameworks\opf\htm_prediction_model.py", line 521, in _sensorCompute
    sensor.compute()
  File "C:\Python27\Lib\site-packages\nupic\frameworks\opf\htm_prediction_model.py", line 436, in run
    self._sensorCompute(inputRecord)
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 99, in runIoThroughNupic
    "kw_energy_consumption": consumption
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 141, in runModel
    runIoThroughNupic(inputData, model, gymName, plot)
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 151, in <module>
    runModel(GYM_NAME, plot=plot)
  File "C:\Python27\Lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "C:\Python27\Lib\runpy.py", line 252, in run_path

The code with my changes can be found here: https://gist.github.com/razvan-panda/ce11d0e2064e6ba34123d95c5136e8a5

1 Like

#2

You will probably need to change something in the params you got from that function so it better matches a prediction model like this one. Can you copy/paste the params you get and compare them? Is the model type correct?

0 Likes

#3

This is the value of modelParams:

{  
   "inferenceArgs":{  
      "predictionSteps":[  
         1
      ],
      "predictedField":"c1",
      "inputPredictedField":"auto"
   },
   "modelConfig":{  
      "aggregationInfo":{  
         "seconds":0,
         "fields":[  

         ],
         "months":0,
         "days":0,
         "years":0,
         "hours":0,
         "microseconds":0,
         "weeks":0,
         "minutes":0,
         "milliseconds":0
      },
      "model":"HTMPrediction",
      "version":1,
      "predictAheadTime":null,
      "modelParams":{  
         "sensorParams":{  
            "sensorAutoReset":null,
            "encoders":{  
               "c0_dayOfWeek":null,
               "c0_timeOfDay":{  
                  "fieldname":"c0",
                  "timeOfDay":[  
                     21,
                     9.49
                  ],
                  "type":"DateEncoder",
                  "name":"c0"
               },
               "c1":{  
                  "name":"c1",
                  "resolution":0.967473319609543,
                  "seed":42,
                  "fieldname":"c1",
                  "type":"RandomDistributedScalarEncoder"
               },
               "c0_weekend":null
            },
            "verbosity":0
         },
         "anomalyParams":{  
            "anomalyCacheRecords":null,
            "autoDetectThreshold":null,
            "autoDetectWaitRecords":5030
         },
         "spParams":{  
            "columnCount":2048,
            "synPermInactiveDec":0.0005,
            "spatialImp":"cpp",
            "inputWidth":0,
            "spVerbosity":0,
            "synPermConnected":0.2,
            "synPermActiveInc":0.003,
            "potentialPct":0.8,
            "numActiveColumnsPerInhArea":40,
            "boostStrength":0.0,
            "globalInhibition":1,
            "seed":1956
         },
         "trainSPNetOnlyIfRequested":false,
         "clParams":{  
            "alpha":0.035828933612158,
            "verbosity":0,
            "steps":"1",
            "regionName":"SDRClassifierRegion"
         },
         "tmParams":{  
            "columnCount":2048,
            "activationThreshold":13,
            "pamLength":3,
            "cellsPerColumn":32,
            "permanenceDec":0.1,
            "minThreshold":10,
            "inputWidth":2048,
            "maxSynapsesPerSegment":32,
            "outputType":"normal",
            "initialPerm":0.21,
            "globalDecay":0.0,
            "maxAge":0,
            "newSynapseCount":20,
            "maxSegmentsPerCell":128,
            "permanenceInc":0.1,
            "temporalImp":"cpp",
            "seed":1960,
            "verbosity":0
         },
         "tmEnable":true,
         "clEnable":false,
         "spEnable":true,
         "inferenceType":"TemporalAnomaly"
      }
   }
}

So the model type is correct. I don’t know what else to do about it.

0 Likes

#4

I think you want to change the inferenceType from TemporalAnomaly to TemporalMultistep.

0 Likes

#5

After making that change I get following error:

Exception has occurred: ValueError
<class 'nupic.frameworks.opf.htm_prediction_model.HTMPredictionModel'> received incompatible inference type: TemporalMultistep
  File "C:\Python27\Lib\site-packages\nupic\frameworks\opf\htm_prediction_model.py", line 197, in __init__
    .format(self.__class__, inferenceType))
  File "C:\Python27\Lib\site-packages\nupic\frameworks\opf\model_factory.py", line 85, in create
    return modelClass(**modelConfig['modelParams'])
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 71, in createModel
    model = ModelFactory.create(modelConfig=modelParams["modelConfig"])
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 145, in runModel
    model = createModel(params)
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 156, in <module>
    runModel(GYM_NAME, plot=plot)
  File "C:\Python27\Lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "C:\Python27\Lib\runpy.py", line 252, in run_path
    return _run_module_code(code, init_globals, run_name, path_name)
0 Likes

#6

Try capital S for TemporalMultiStep (sorry)…

0 Likes

#7

I think that got the parameter set correctly but I’m still getting the original error:

Exception has occurred: ValueError
Unknown field name 'c0' in input record. Known fields are 'timestamp, kw_energy_consumption'.
This could be because input headers are mislabeled, or because input data rows do not contain a value for 'c0'.
  File "C:\Python27\Lib\site-packages\nupic\encoders\base.py", line 229, in _getInputValue
    fieldName, knownFields, fieldName
  File "C:\Python27\Lib\site-packages\nupic\encoders\multi.py", line 106, in encodeIntoArray
    encoder.encodeIntoArray(self._getInputValue(obj, name), output[offset:])
  File "C:\Python27\Lib\site-packages\nupic\regions\record_sensor.py", line 377, in compute
    self.encoder.encodeIntoArray(data, outputs["dataOut"])
  File "C:\Python27\Lib\site-packages\nupic\bindings\regions\PyRegion.py", line 186, in guardedCompute
    return self.compute(inputs, DictReadOnlyWrapper(outputs))
  File "C:\Python27\Lib\site-packages\nupic\bindings\engine_internal.py", line 1559, in compute
    return _engine_internal.Region_compute(self)
  File "C:\Python27\Lib\site-packages\nupic\engine\__init__.py", line 435, in compute
    return self._region.compute()
  File "C:\Python27\Lib\site-packages\nupic\frameworks\opf\htm_prediction_model.py", line 521, in _sensorCompute
    sensor.compute()
  File "C:\Python27\Lib\site-packages\nupic\frameworks\opf\htm_prediction_model.py", line 436, in run
    self._sensorCompute(inputRecord)
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 104, in runIoThroughNupic
    "kw_energy_consumption": consumption
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 146, in runModel
    runIoThroughNupic(inputData, model, gymName, plot)
  File "D:\Projects\nupic\examples\opf\clients\hotgym\prediction\one_gym\run.py", line 156, in <module>
    runModel(GYM_NAME, plot=plot)
  File "C:\Python27\Lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "C:\Python27\Lib\runpy.py", line 252, in run_path
0 Likes

#8

That is because you need to match the field names in the model params encoder section to the actual field names in the data input. This is an informative error:

Unknown field name 'c0' in input record. Known fields are 'timestamp, kw_energy_consumption'.

The c0 is a name of an input field that probably came from an automated process (the function you called). Either I think you just need to change it to an value in the input data. There is a probably also a c1 in there, and these should correspond to 'timestamp, kw_energy_consumption'

0 Likes

#9

Sorry, I don’t actually follow.

I see that inferenceArgs.predictedField is c1 but inferenceArgs is not used at all. I’m just passing modelParams[“modelConfig”] to ModelFactory.create
Also the error mentions c0 which is nowhere to be found.

What changes would I need to make to the code?

0 Likes

#10

I am not sure you’ll need to update the code, but you need to update the sensorParams in the model params so the data input field names match the data field names you are using when you run the model:

result = model.run({
  "timestamp": timestamp,
  "kw_energy_consumption": consumption
})
0 Likes

#11

I haven’t changed this part of the code:

result = model.run({
  "timestamp": timestamp,
  "kw_energy_consumption": consumption
})

because I’m running hot gym prediction with the same data. The only change I am trying to make is to use getScalarMetricWithTimeOfDayAnomalyParams instead of swarming.

0 Likes

#12

For anyone who might be able to help. An easy way to check if something would work is to copy paste the code from https://gist.github.com/razvan-panda/ce11d0e2064e6ba34123d95c5136e8a5 in the prediction folder and run it.

0 Likes

#13

Then you will need to change the model params you get back from the getScalarMetricWithTimeOfDayAnomalyParams so the field names match the data. If you see c0, c1, etc, they need to be replaced with timestamp and kw_energy_consumption.

0 Likes

#14

Do I also need to change it in places such as "c0_dayOfWeek":null, ?

0 Likes

#15

Is this correct? I replaced all the c0 and c1’s with their correct names in the values. Didn’t do that in the keys though. Is that required also?

{  
   "inferenceArgs":{  
      "predictionSteps":[  
         1
      ],
      "predictedField":"kw_energy_consumption",
      "inputPredictedField":"auto"
   },
   "modelConfig":{  
      "aggregationInfo":{  
         "seconds":0,
         "fields":[  

         ],
         "months":0,
         "days":0,
         "years":0,
         "hours":0,
         "microseconds":0,
         "weeks":0,
         "minutes":0,
         "milliseconds":0
      },
      "model":"HTMPrediction",
      "version":1,
      "predictAheadTime":null,
      "modelParams":{  
         "sensorParams":{  
            "sensorAutoReset":null,
            "encoders":{  
               "c0_dayOfWeek":null,
               "c0_timeOfDay":{  
                  "fieldname":"timestamp",
                  "timeOfDay":[  
                     21,
                     9.49
                  ],
                  "type":"DateEncoder",
                  "name":"timestamp"
               },
               "c1":{  
                  "name":"kw_energy_consumption",
                  "resolution":0.967473319609543,
                  "seed":42,
                  "fieldname":"kw_energy_consumption",
                  "type":"RandomDistributedScalarEncoder"
               },
               "c0_weekend":null
            },
            "verbosity":0
         },
         "anomalyParams":{  
            "anomalyCacheRecords":null,
            "autoDetectThreshold":null,
            "autoDetectWaitRecords":5030
         },
         "spParams":{  
            "columnCount":2048,
            "synPermInactiveDec":0.0005,
            "spatialImp":"cpp",
            "inputWidth":0,
            "spVerbosity":0,
            "synPermConnected":0.2,
            "synPermActiveInc":0.003,
            "potentialPct":0.8,
            "numActiveColumnsPerInhArea":40,
            "boostStrength":0.0,
            "globalInhibition":1,
            "seed":1956
         },
         "trainSPNetOnlyIfRequested":false,
         "clParams":{  
            "alpha":0.035828933612158,
            "verbosity":0,
            "steps":"1",
            "regionName":"SDRClassifierRegion"
         },
         "tmParams":{  
            "columnCount":2048,
            "activationThreshold":13,
            "pamLength":3,
            "cellsPerColumn":32,
            "permanenceDec":0.1,
            "minThreshold":10,
            "inputWidth":2048,
            "maxSynapsesPerSegment":32,
            "outputType":"normal",
            "initialPerm":0.21,
            "globalDecay":0.0,
            "maxAge":0,
            "newSynapseCount":20,
            "maxSegmentsPerCell":128,
            "permanenceInc":0.1,
            "temporalImp":"cpp",
            "seed":1960,
            "verbosity":0
         },
         "tmEnable":true,
         "clEnable":false,
         "spEnable":true,
         "inferenceType":"TemporalMultiStep"
      }
   }
}

After the change it is throwing the following error:

Creating model from rec-center-hourly...
Preparing to output rec-center-hourly data to rec-center-hourly_out.csv
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/Projects/nupic/examples/opf/clients/hotgym/prediction/one_gym/run.py", line 164, in <module>
    runModel(GYM_NAME, plot=plot)
  File "D:/Projects/nupic/examples/opf/clients/hotgym/prediction/one_gym/run.py", line 154, in runModel
    runIoThroughNupic(inputData, model, gymName, plot)
  File "D:/Projects/nupic/examples/opf/clients/hotgym/prediction/one_gym/run.py", line 126, in runIoThroughNupic
    prediction = result.inferences["multiStepBestPredictions"][1]
KeyError: 'multiStepBestPredictions'
0 Likes

#16

What does result.inferences contain if not multiStepBestPredictions?

0 Likes

#17

image

0 Likes

#18

If the model creating those results is an HTMPrediction, I would expect a lot more results. Can you inspect the model instance and ensure it is an HTMPrediction?

0 Likes

#19

Seems to be correct:

0 Likes