Issue with getting anomaly parameters instead of swarming

The results object won’t contain ‘multiStepBestPredicitons’ in ‘TemporalAnomaly’ models. I think you mean to set your ‘prediction’ variable equal to the anomaly likelihood? This is the value from 0-1 used to make the final determination if the data point is an anomaly.

In the “encoders” dict, the keys must match the field names in the data, so instead of field ‘c1’ it should be field ‘consumption’. There should only be “encoders” items for fields that are found in the data.

I think this is the basic setup you want:

from nupic.algorithms import anomaly_likelihood
from nupic.frameworks.opf.model_factory import ModelFactory

model = ModelFactory.create(modelConfig=params["modelConfig"])
        model.enableLearning()
        model.enableInference(params["inferenceArgs"])
AnomLikl_obj = anomaly_likelihood.AnomalyLikelihood()

for index, row in DataDF.iterrows():
    AnomScore = resultObj.inferences["anomalyScore"]
    AnomLikl = AnomLikl_obj.anomalyProbability(row, AnomScore, timestamp=None)
    if AnomLikl > 0.999:
        print('ANOMALY FOUND -- Index = {}'.format(index))

I tried a lot of things and nothing works :frowning_face:
@rhyolight @sheiser1 Would any of you be willing to try to run it on your machine? This would probably be the most efficient way to figure out what is wrong. The code is here: https://gist.github.com/razvan-panda/ce11d0e2064e6ba34123d95c5136e8a5

I can’t find an example usage for getScalarMetricWithTimeOfDayAnomalyParams anywhere on the internet.

If no one else tries this, I will do it Friday on my live-stream.

1 Like

params = getScalarMetricWithTimeOfDayAnomalyParams(data)

Did you check the inference type that comes with getScalarMetricWithTimeOfDayAnomalyParams()?

The ‘result.inferences’ object will only contain “multiStepBestPredictions” if the model inference type is ‘TemporalMultistep’ (I think). But the ‘params’ file you’re generating with getScalarMetricWithTimeOfDayAnomalyParams() has inference type ‘TemporalAnomaly’ (I think).

I’d recommend inspecting the ‘params’ object for its model inference type and also the encoders --> params[“modelConfig”][‘modelParams’][‘sensorParams’][‘encoders’] to ensure they match your input fields.

1 Like

Also I think you’re getting the wrong model output for anomaly detection. The value from result.inferences[“multiStepBestPredictions”][1] is a forecast for a single ‘predictedField’, but for anomaly detection we use the Anomaly Score (result.inferences[“anomalyScore”]) to get the Anomaly Likelihood, which determines the anomalous state of the system at each timestep.

from nupic.algorithms import anomaly_likelihood
from nupic.frameworks.opf.model_factory import ModelFactory

model = ModelFactory.create(modelConfig=params["modelConfig"])
        model.enableLearning()
        model.enableInference(params["inferenceArgs"])
AnomLikl_obj = anomaly_likelihood.AnomalyLikelihood()

for index, row in DataDF.iterrows():
    AnomScore = resultObj.inferences["anomalyScore"]
    AnomLikl = AnomLikl_obj.anomalyProbability(row, AnomScore, timestamp=None)
    if AnomLikl > 0.999:
        print('ANOMALY FOUND -- Index = {}'.format(index))
1 Like

I checked the params and the inference type is TemporalMultistep so I don’t know what is wrong.

I’d definitely recommend changing it to ‘TemporalAnomaly’ and trying the setup outlined above, where an Anomaly Likelihood object is initiated

and then updated at each time step.

@Razvan_Flavius_Panda I just spent some time on this, and I cannot figure out what is wrong. I took your code and ended up having the same problems. I’m going to point you to this really simple hot gym example that does seem to work for me.

I know I did not do a good job answering your questions, but here is proof that I at least spent some time on it. :stuck_out_tongue:

1 Like

Thank you for taking your time to work on it.
Bear with me, I’ve got 1 more question :slight_smile:
In the simple hotgym example I see that there is a loop being done over the data. How can I get predicted value, anomaly score and anomaly likelihood at each iteration?

I think all you need to do is change one line to:

'inferenceType': 'TemporalAnomaly',

Please try that.

I made the change but where can I read those values from? Which variable stores them?

You should still find them in results.inferences. Per this example:

After this change, model results will contain an anomalyScore value in addition to the multi-step inferences.

Anomaly likelihood seems to be missing. Anomaly score is there.

How do I interpret these values for multiStepPredictions? I was expecting a single value to be predicted.

image

Not missing, it’s just that you have to add it yourself. See the next section of the tutorial above.

1 Like

The Anomaly Likelihood isn’t in the result.inferences object like the Anomaly Score is. To get the anomaly likelihood you need to first create an Anomaly Likelihood object of its own before looping over the rows in the data. Then at each row you need to feed the current tilmestep and Anomaly Score into the Anomaly Likelihood object, which will return the Anomaly Likelihood value (0.5 - 0.999999) and update the Anomaly Likelihood object.

2 Likes

Regarding predictions, I assumed there would be 1 value predicted but in the inferences object there isn’t any prediction that is just 1.

It’s either this:

image

or this:

And neither of them contain just 1 prediction as expected.
How can I interpret this to get the predicted value?

In the hotgym anomaly example I see that the single prediction is obtained via:

prediction = result.inferences["multiStepBestPredictions"][1]

Is that correct?

Just curious, what is the meaning of result.inferences["multiStepBestPredictions"][5] ?

1 Like

From what you said I understand that getting anomaly likelihood like specified here is wrong: Getting anomaly likelihood

Which can partly justify the bad anomaly detection in: Problems with anomaly detection

This is saying that the hotgym anomaly example uses anomaly_likelihood but actually it is not.