本文整理汇总了Python中nupic.algorithms.anomaly_likelihood.AnomalyLikelihood.anomalyProbability方法的典型用法代码示例。如果您正苦于以下问题:Python AnomalyLikelihood.anomalyProbability方法的具体用法?Python AnomalyLikelihood.anomalyProbability怎么用?Python AnomalyLikelihood.anomalyProbability使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nupic.algorithms.anomaly_likelihood.AnomalyLikelihood
的用法示例。
在下文中一共展示了AnomalyLikelihood.anomalyProbability方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: runAvogadroAnomaly
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
def runAvogadroAnomaly(metric, options):
"""
Create a new HTM Model, fetch the data from the local DB, process it in NuPIC,
and save the results to a new CSV output file.
:param metric: AvogadroAgent metric class
:param options: CLI Options
"""
model = createModel(metric)
model.enableInference({"predictedField": metric.name})
fetched = metric.fetch(prefix=options.prefix, start=None)
resultFile = open(os.path.join(options.prefix, metric.name + "-result.csv"),
"wb")
csvWriter = csv.writer(resultFile)
csvWriter.writerow(["timestamp", metric.name, "raw_anomaly_score",
"anomaly_likelihood", "color"])
headers = ("timestamp", metric.name)
anomalyLikelihood = AnomalyLikelihood()
for (ts, value) in fetched:
try:
value = float(value)
except (ValueError, TypeError):
continue
if not math.isnan(value):
modelInput = dict(zip(headers, (ts, value)))
modelInput[metric.name] = float(value)
modelInput["timestamp"] = datetime.datetime.fromtimestamp(
float(modelInput["timestamp"]))
result = model.run(modelInput)
anomalyScore = result.inferences["anomalyScore"]
likelihood = anomalyLikelihood.anomalyProbability(
modelInput[metric.name], anomalyScore, modelInput["timestamp"])
logLikelihood = anomalyLikelihood.computeLogLikelihood(likelihood)
if logLikelihood > .5:
color = "red"
elif logLikelihood > .4 and logLikelihood <= .5:
color = "yellow"
else:
color = "green"
csvWriter.writerow([modelInput["timestamp"], float(value),
anomalyScore, logLikelihood, color])
else:
resultFile.flush()
示例2: testLikelihoodValues
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
def testLikelihoodValues(self):
""" test to see if the region keeps track of state correctly and produces
the same likelihoods as the AnomalyLikelihood module """
anomalyLikelihoodRegion = AnomalyLikelihoodRegion()
anomalyLikelihood = AnomalyLikelihood()
inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
outputs = AnomalyLikelihoodRegion.getSpec()['outputs']
with open (_INPUT_DATA_FILE) as f:
reader = csv.reader(f)
reader.next()
for record in reader:
consumption = float(record[1])
anomalyScore = float(record[2])
likelihood1 = anomalyLikelihood.anomalyProbability(
consumption, anomalyScore)
inputs['rawAnomalyScore'] = numpy.array([anomalyScore])
inputs['metricValue'] = numpy.array([consumption])
anomalyLikelihoodRegion.compute(inputs, outputs)
likelihood2 = outputs['anomalyLikelihood'][0]
self.assertEqual(likelihood1, likelihood2)
示例3: int
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
try:
event = (numpy.array([x, y]), int(10*float(earthquake.mag)))
modelInput = {}
modelInput["event"] = event
modelInput["timestamp"] = (
datetime.datetime.strptime(earthquake.time, "%Y-%m-%dT%H:%M:%S.%fZ"))
result = model.run(modelInput)
anomalyScore = result.inferences["anomalyScore"]
scores.append(anomalyScore)
likelihoodScores.append([modelInput["timestamp"],
modelInput["event"],
anomalyScore])
likelihood = anomalyLikelihood.anomalyProbability(
event[0] + numpy.array([event[1]]),
anomalyScore,
modelInput["timestamp"])
data = {"lat": earthquake.latitude,
"lng": earthquake.longitude,
"score": anomalyScore,
"mag": earthquake.mag,
"mean": (numpy.mean(scores), WINDOWSIZE),
"timestamp": earthquake.time,
"likelihood": likelihood}
r.publish("nupic", json.dumps(data))
print data
except ValueError:
pass
示例4: Anomaly
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class Anomaly(object):
"""basic class that computes anomaly
Anomaly is used to detect strange patterns/behaviors (outliners)
by a trained CLA model.
"""
# anomaly modes supported
MODE_PURE = "pure"
MODE_LIKELIHOOD = "likelihood"
MODE_WEIGHTED = "weighted"
_supportedModes = [MODE_PURE, MODE_LIKELIHOOD, MODE_WEIGHTED]
def __init__(self, slidingWindowSize = None, anomalyMode=MODE_PURE,
shiftPredicted=False):
"""
@param (optional) slidingWindowSize -- enables moving average on final
anomaly score; how many elements are summed up,
sliding window size; int >= 0
@param (optional) anomalyMode -- (string) how to compute anomaly;
possible values are:
-- "pure" -- the default, how much anomal the value is;
float 0..1 where 1=totally unexpected
-- "likelihood" -- uses the anomaly_likelihood code;
models probability of receiving this
value and anomalyScore; used in Grok
-- "weighted" -- "pure" anomaly weighted by "likelihood" (anomaly * likelihood)
@param shiftPredicted (optional) -- boolean [default=False];
normally active vs predicted are compared
if shiftPredicted=True: predicted(T-1) vs active(T)
are compared (eg from TP, CLAModel)
"""
# using cumulative anomaly , sliding window
if slidingWindowSize > 0:
self._windowSize = slidingWindowSize
#sliding window buffer
self._buf = numpy.array([0] * self._windowSize, dtype=numpy.float)
self._i = 0 # index pointer to actual position
elif slidingWindowSize is not None:
raise Exception("Anomaly: if you define slidingWindowSize, \
it has to be an integer > 0; \
slidingWindowSize="+str(slidingWindowSize))
# mode
self._mode = anomalyMode
if self._mode == Anomaly.MODE_LIKELIHOOD:
self._likelihood = AnomalyLikelihood() # probabilistic anomaly
if not self._mode in Anomaly._supportedModes:
raise ValueError('Invalid anomaly mode; only supported modes are: \
"Anomaly.MODE_PURE", "Anomaly.MODE_LIKELIHOOD", \
"Anomaly.MODE_WEIGHTED"; you used:' +self._mode)
if shiftPredicted:
self._prevPredictedColumns = numpy.array([])
def computeAnomalyScore(self, activeColumns, predictedColumns, value=None,
timestamp=None):
"""Compute the anomaly score as the percent of active columns not predicted
@param activeColumns: array of active column indices
@param predictedColumns: array of columns indices predicted in this step
(used for anomaly in step T+1)
@param value: (optional) input value, that is what activeColumns represent
(used in anomaly-likelihood)
@param timestamp: (optional) date timestamp when the sample occured
(used in anomaly-likelihood)
@return the computed anomaly score; float 0..1
"""
if hasattr(self, "_prevPredictedColumns"): # shiftPredicted==True
prevPredictedColumns = self._prevPredictedColumns
self._prevPredictedColumns = predictedColumns # to be used in step T+1
else:
prevPredictedColumns = predictedColumns
# 1. here is the 'classic' anomaly score
anomalyScore = computeRawAnomalyScore(activeColumns, prevPredictedColumns)
# compute final anomaly based on selected mode
if self._mode == Anomaly.MODE_PURE:
score = anomalyScore
elif self._mode == Anomaly.MODE_LIKELIHOOD:
probability = self._likelihood.anomalyProbability(value, anomalyScore, timestamp)
score = probability
elif self._mode == Anomaly.MODE_WEIGHTED:
probability = self._likelihood.anomalyProbability(value, anomalyScore, timestamp)
score = anomalyScore * probability
# last, do moving-average if windowSize is set
if hasattr(self, "_windowSize"):
score = self._movingAverage(score)
return score
#.........这里部分代码省略.........
示例5: _ModelRunner
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class _ModelRunner(object):
""" Use OPF Model to process metric data samples from stdin and and emit
anomaly likelihood results to stdout
"""
def __init__(self, inputFileObj, inputSpec, aggSpec, modelSpec):
"""
:param inputFileObj: A file-like object that contains input metric data
:param dict inputSpec: Input data specification per input_opt_schema.json
:param dict aggSpec: Optional aggregation specification per
agg_opt_schema.json or None if no aggregation is requested
:param dict modelSpec: Model specification per model_opt_schema.json
"""
self._inputSpec = inputSpec
self._aggSpec = aggSpec
self._modelSpec = modelSpec
if "modelId" in modelSpec:
self._modelId = modelSpec["modelId"]
else:
self._modelId = "Unknown"
inputRecordSchema = (
fieldmeta.FieldMetaInfo(modelSpec["timestampFieldName"],
fieldmeta.FieldMetaType.datetime,
fieldmeta.FieldMetaSpecial.timestamp),
fieldmeta.FieldMetaInfo(modelSpec["valueFieldName"],
fieldmeta.FieldMetaType.float,
fieldmeta.FieldMetaSpecial.none),
)
self._aggregator = aggregator.Aggregator(
aggregationInfo=dict(
fields=([(modelSpec["valueFieldName"], aggSpec["func"])]
if aggSpec is not None else []),
seconds=aggSpec["windowSize"] if aggSpec is not None else 0
),
inputFields=inputRecordSchema)
self._modelRecordEncoder = record_stream.ModelRecordEncoder(
fields=inputRecordSchema)
self._model = self._createModel(modelSpec=modelSpec)
self._anomalyLikelihood = AnomalyLikelihood()
self._csvReader = self._createCsvReader(inputFileObj)
@staticmethod
def _createModel(modelSpec):
"""Instantiate and configure an OPF model
:param dict modelSpec: Model specification per model_opt_schema.json
:returns: OPF Model instance
"""
model = ModelFactory.create(modelConfig=modelSpec["modelConfig"])
model.enableLearning()
model.enableInference(modelSpec["inferenceArgs"])
return model
@staticmethod
def _createCsvReader(fileObj):
# We'll be operating on csvs with arbitrarily long fields
csv.field_size_limit(2**27)
# Make sure readline() works on windows too
os.linesep = "\n"
return csv.reader(fileObj, dialect="excel")
@classmethod
def _emitOutputMessage(cls, dataRow, anomalyProbability):
"""Emit output message to stdout
:param list dataRow: the two-tuple data row on which anomalyProbability was
computed, whose first element is datetime timestamp and second element is
the float scalar value
:param float anomalyProbability: computed anomaly probability value
"""
message = "%s\n" % (json.dumps([dataRow[0].isoformat(), dataRow[1], anomalyProbability]),)
sys.stdout.write(message)
sys.stdout.flush()
def _computeAnomalyProbability(self, fields):
""" Compute anomaly log likelihood score
:param tuple fields: Two-tuple input metric data row
#.........这里部分代码省略.........
示例6: _ModelRunner
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class _ModelRunner(object):
""" Use OPF Model to process metric data samples from stdin and and emit
anomaly likelihood results to stdout
"""
# Input column meta info compatible with parameters generated by
# getScalarMetricWithTimeOfDayAnomalyParams
_INPUT_RECORD_SCHEMA = (
fieldmeta.FieldMetaInfo("c0", fieldmeta.FieldMetaType.datetime,
fieldmeta.FieldMetaSpecial.timestamp),
fieldmeta.FieldMetaInfo("c1", fieldmeta.FieldMetaType.float,
fieldmeta.FieldMetaSpecial.none),
)
def __init__(self, modelId, stats):
"""
:param str modelId: model identifier
:param dict stats: Metric data stats per stats_schema.json in the
unicorn_backend package.
"""
self._modelId = modelId
# NOTE: ModelRecordEncoder is implemented in the pull request
# https://github.com/numenta/nupic/pull/2432 that is not yet in master.
self._modelRecordEncoder = record_stream.ModelRecordEncoder(
fields=self._INPUT_RECORD_SCHEMA)
self._model = self._createModel(stats=stats)
self._anomalyLikelihood = AnomalyLikelihood()
@classmethod
def _createModel(cls, stats):
"""Instantiate and configure an OPF model
:param dict stats: Metric data stats per stats_schema.json in the
unicorn_backend package.
:returns: OPF Model instance
"""
# Generate swarm params
swarmParams = getScalarMetricWithTimeOfDayAnomalyParams(
metricData=[0],
minVal=stats["min"],
maxVal=stats["max"],
minResolution=stats.get("minResolution"))
model = ModelFactory.create(modelConfig=swarmParams["modelConfig"])
model.enableLearning()
model.enableInference(swarmParams["inferenceArgs"])
return model
@classmethod
def _readInputMessages(cls):
"""Create a generator that waits for and yields input messages from
stdin
yields two-tuple (<timestamp>, <scalar-value>), where <timestamp> is the
`datetime.datetime` timestamp of the metric data sample and <scalar-value>
is the floating point value of the metric data sample.
"""
while True:
message = sys.stdin.readline()
if message:
timestamp, scalarValue = json.loads(message)
yield (datetime.utcfromtimestamp(timestamp), scalarValue)
else:
# Front End closed the pipe (or died)
break
@classmethod
def _emitOutputMessage(cls, rowIndex, anomalyProbability):
"""Emit output message to stdout
:param int rowIndex: 0-based index of corresponding input sample
:param float anomalyProbability: computed anomaly probability value
"""
message = "%s\n" % (json.dumps([rowIndex, anomalyProbability]),)
sys.stdout.write(message)
sys.stdout.flush()
def _computeAnomalyProbability(self, inputRow):
""" Compute anomaly log likelihood score
:param tuple inputRow: Two-tuple input metric data row
(<datetime-timestamp>, <float-scalar>)
:returns: Log-scaled anomaly probability
:rtype: float
"""
# Generate raw anomaly score
inputRecord = self._modelRecordEncoder.encode(inputRow)
rawAnomalyScore = self._model.run(inputRecord).inferences["anomalyScore"]
#.........这里部分代码省略.........
示例7: Monitor
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class Monitor(object):
""" A NuPIC model that saves results to Redis. """
def __init__(self, config):
# Instantiate NuPIC model
model_params = base_model_params.MODEL_PARAMS
# Set resolution
model_params['modelParams']['sensorParams']['encoders']['value']['resolution'] = config['resolution']
# Override other Nupic parameters:
model_params['modelParams'] = update_dict(model_params['modelParams'], config['nupic_model_params'])
# Create model and enable inference on it
self.model = ModelFactory.create(model_params)
self.model.enableInference({'predictedField': 'value'})
# The shifter is used to bring the predictions to the actual time frame
self.shifter = InferenceShifter()
# The anomaly likelihood object
self.anomalyLikelihood = AnomalyLikelihood()
# Set stream source
self.stream = config['stream']
# Setup class variables
self.db = redis.Redis('localhost')
self.seconds_per_request = config['seconds_per_request']
self.webhook = config['webhook']
self.anomaly_threshold = config['anomaly_threshold']
self.likelihood_threshold = config['likelihood_threshold']
self.domain = config['domain']
self.alert = False # Toogle when we get above threshold
# Setup logging
self.logger = logger or logging.getLogger(__name__)
handler = logging.handlers.RotatingFileHandler(os.environ['LOG_DIR']+"/monitor_%s.log" % self.stream.name,
maxBytes=1024*1024,
backupCount=4,
)
handler.setFormatter(logging.Formatter('[%(levelname)s/%(processName)s][%(asctime)s] %(name)s %(message)s'))
handler.setLevel(logging.INFO)
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO)
self.logger.info("=== Settings ===")
self.logger.info("Webhook: %s", self.webhook)
self.logger.info("Domain: %s", self.domain)
self.logger.info("Seconds per request: %d", self.seconds_per_request)
# Write metadata to Redis
try:
# Save in redis with key = 'results:monitor_id' and value = 'time, status, actual, prediction, anomaly'
self.db.set('name:%s' % self.stream.id, self.stream.name)
self.db.set('value_label:%s' % self.stream.id, self.stream.value_label)
self.db.set('value_unit:%s' % self.stream.id, self.stream.value_unit)
except Exception:
self.logger.warn("Could not write results to redis.", exc_info=True)
def train(self):
data = self.stream.historic_data()
for model_input in data:
self.update(model_input, False) # Don't post anomalies in training
def loop(self):
while True:
data = self.stream.new_data()
for model_input in data:
self.update(model_input, True) # Post anomalies when online
sleep(self.seconds_per_request)
def update(self, model_input, is_to_post):
# Pass the input to the model
result = self.model.run(model_input)
# Shift results
result = self.shifter.shift(result)
# Save multi step predictions
inference = result.inferences['multiStepPredictions']
# Take the anomaly_score
anomaly_score = result.inferences['anomalyScore']
# Compute the Anomaly Likelihood
likelihood = self.anomalyLikelihood.anomalyProbability(model_input['value'],
anomaly_score,
model_input['time'])
# Get the predicted value for reporting
predicted = result.inferences['multiStepBestPredictions'][1]
# Get timestamp from datetime
timestamp = calendar.timegm(model_input['time'].timetuple())
#.........这里部分代码省略.........
示例8: Anomaly
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class Anomaly(object):
"""Utility class for generating anomaly scores in different ways.
Supported modes:
MODE_PURE - the raw anomaly score as computed by computeRawAnomalyScore
MODE_LIKELIHOOD - uses the AnomalyLikelihood class on top of the raw
anomaly scores
MODE_WEIGHTED - multiplies the likelihood result with the raw anomaly score
that was used to generate the likelihood
"""
# anomaly modes supported
MODE_PURE = "pure"
MODE_LIKELIHOOD = "likelihood"
MODE_WEIGHTED = "weighted"
_supportedModes = (MODE_PURE, MODE_LIKELIHOOD, MODE_WEIGHTED)
def __init__(self, slidingWindowSize = None, mode=MODE_PURE):
"""
@param slidingWindowSize (optional) - how many elements are summed up;
enables moving average on final anomaly score; int >= 0
@param mode (optional) - (string) how to compute anomaly;
possible values are:
- "pure" - the default, how much anomal the value is;
float 0..1 where 1=totally unexpected
- "likelihood" - uses the anomaly_likelihood code;
models probability of receiving this value and anomalyScore
- "weighted" - "pure" anomaly weighted by "likelihood"
(anomaly * likelihood)
"""
self._mode = mode
if slidingWindowSize is not None:
self._movingAverage = MovingAverage(windowSize=slidingWindowSize)
else:
self._movingAverage = None
if self._mode == Anomaly.MODE_LIKELIHOOD:
self._likelihood = AnomalyLikelihood() # probabilistic anomaly
if not self._mode in Anomaly._supportedModes:
raise ValueError("Invalid anomaly mode; only supported modes are: "
"Anomaly.MODE_PURE, Anomaly.MODE_LIKELIHOOD, "
"Anomaly.MODE_WEIGHTED; you used: %r" % self._mode)
def compute(self, activeColumns, predictedColumns,
inputValue=None, timestamp=None):
"""Compute the anomaly score as the percent of active columns not predicted.
@param activeColumns: array of active column indices
@param predictedColumns: array of columns indices predicted in this step
(used for anomaly in step T+1)
@param inputValue: (optional) value of current input to encoders
(eg "cat" for category encoder)
(used in anomaly-likelihood)
@param timestamp: (optional) date timestamp when the sample occured
(used in anomaly-likelihood)
@return the computed anomaly score; float 0..1
"""
# Start by computing the raw anomaly score.
anomalyScore = computeRawAnomalyScore(activeColumns, predictedColumns)
# Compute final anomaly based on selected mode.
if self._mode == Anomaly.MODE_PURE:
score = anomalyScore
elif self._mode == Anomaly.MODE_LIKELIHOOD:
if inputValue is None:
raise ValueError("Selected anomaly mode 'Anomaly.MODE_LIKELIHOOD' "
"requires 'inputValue' as parameter to compute() method. ")
probability = self._likelihood.anomalyProbability(
inputValue, anomalyScore, timestamp)
# low likelihood -> hi anomaly
score = 1 - probability
elif self._mode == Anomaly.MODE_WEIGHTED:
probability = self._likelihood.anomalyProbability(
inputValue, anomalyScore, timestamp)
score = anomalyScore * (1 - probability)
# Last, do moving-average if windowSize was specified.
if self._movingAverage is not None:
score = self._movingAverage.next(score)
return score
示例9: runAnomaly
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
def runAnomaly(options):
"""
Create and run a CLA Model on the given dataset (based on the hotgym anomaly
client in NuPIC).
"""
# Load the model params JSON
with open("model_params.json") as fp:
modelParams = json.load(fp)
# Update the resolution value for the encoder
sensorParams = modelParams['modelParams']['sensorParams']
numBuckets = modelParams['modelParams']['sensorParams']['encoders']['value'].pop('numBuckets')
resolution = options.resolution
if resolution is None:
resolution = max(0.001,
(options.max - options.min) / numBuckets)
print "Using resolution value: {0}".format(resolution)
sensorParams['encoders']['value']['resolution'] = resolution
model = ModelFactory.create(modelParams)
model.enableInference({'predictedField': 'value'})
with open (options.inputFile) as fin:
# Open file and setup headers
# Here we write the log likelihood value as the 'anomaly score'
# The actual CLA outputs are labeled 'raw anomaly score'
reader = csv.reader(fin)
csvWriter = csv.writer(open(options.outputFile,"wb"))
csvWriter.writerow(["timestamp", "value",
"_raw_score", "likelihood_score", "log_likelihood_score"])
headers = reader.next()
# The anomaly likelihood object
anomalyLikelihood = AnomalyLikelihood()
# Iterate through each record in the CSV file
print "Starting processing at",datetime.datetime.now()
for i, record in enumerate(reader, start=1):
# Convert input data to a dict so we can pass it into the model
inputData = dict(zip(headers, record))
inputData["value"] = float(inputData["value"])
inputData["dttm"] = dateutil.parser.parse(inputData["dttm"])
#inputData["dttm"] = datetime.datetime.now()
# Send it to the CLA and get back the raw anomaly score
result = model.run(inputData)
anomalyScore = result.inferences['anomalyScore']
# Compute the Anomaly Likelihood
likelihood = anomalyLikelihood.anomalyProbability(
inputData["value"], anomalyScore, inputData["dttm"])
logLikelihood = anomalyLikelihood.computeLogLikelihood(likelihood)
if likelihood > 0.9999:
print "Anomaly detected:",inputData['dttm'],inputData['value'],likelihood
# Write results to the output CSV file
csvWriter.writerow([inputData["dttm"], inputData["value"],
anomalyScore, likelihood, logLikelihood])
# Progress report
if (i%1000) == 0: print i,"records processed"
print "Completed processing",i,"records at",datetime.datetime.now()
print "Anomaly scores for",options.inputFile,
print "have been written to",options.outputFile
示例10: Anomaly
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class Anomaly(object):
"""Utility class for generating anomaly scores in different ways.
:param slidingWindowSize: [optional] - how many elements are summed up;
enables moving average on final anomaly score; int >= 0
:param mode: (string) [optional] how to compute anomaly, one of:
- :const:`nupic.algorithms.anomaly.Anomaly.MODE_PURE`
- :const:`nupic.algorithms.anomaly.Anomaly.MODE_LIKELIHOOD`
- :const:`nupic.algorithms.anomaly.Anomaly.MODE_WEIGHTED`
:param binaryAnomalyThreshold: [optional] if set [0,1] anomaly score
will be discretized to 1/0 (1 if >= binaryAnomalyThreshold)
The transformation is applied after moving average is computed.
"""
# anomaly modes supported
MODE_PURE = "pure"
"""
Default mode. The raw anomaly score as computed by
:func:`~.anomaly_likelihood.computeRawAnomalyScore`
"""
MODE_LIKELIHOOD = "likelihood"
"""
Uses the :class:`~.anomaly_likelihood.AnomalyLikelihood` class, which models
probability of receiving this value and anomalyScore
"""
MODE_WEIGHTED = "weighted"
"""
Multiplies the likelihood result with the raw anomaly score that was used to
generate the likelihood (anomaly * likelihood)
"""
_supportedModes = (MODE_PURE, MODE_LIKELIHOOD, MODE_WEIGHTED)
def __init__(self,
slidingWindowSize=None,
mode=MODE_PURE,
binaryAnomalyThreshold=None):
self._mode = mode
if slidingWindowSize is not None:
self._movingAverage = MovingAverage(windowSize=slidingWindowSize)
else:
self._movingAverage = None
if (self._mode == Anomaly.MODE_LIKELIHOOD or
self._mode == Anomaly.MODE_WEIGHTED):
self._likelihood = AnomalyLikelihood() # probabilistic anomaly
else:
self._likelihood = None
if not self._mode in self._supportedModes:
raise ValueError("Invalid anomaly mode; only supported modes are: "
"Anomaly.MODE_PURE, Anomaly.MODE_LIKELIHOOD, "
"Anomaly.MODE_WEIGHTED; you used: %r" % self._mode)
self._binaryThreshold = binaryAnomalyThreshold
if binaryAnomalyThreshold is not None and (
not isinstance(binaryAnomalyThreshold, float) or
binaryAnomalyThreshold >= 1.0 or
binaryAnomalyThreshold <= 0.0 ):
raise ValueError("Anomaly: binaryAnomalyThreshold must be from (0,1) "
"or None if disabled.")
def compute(self, activeColumns, predictedColumns,
inputValue=None, timestamp=None):
"""Compute the anomaly score as the percent of active columns not predicted.
:param activeColumns: array of active column indices
:param predictedColumns: array of columns indices predicted in this step
(used for anomaly in step T+1)
:param inputValue: (optional) value of current input to encoders
(eg "cat" for category encoder)
(used in anomaly-likelihood)
:param timestamp: (optional) date timestamp when the sample occured
(used in anomaly-likelihood)
:returns: the computed anomaly score; float 0..1
"""
# Start by computing the raw anomaly score.
anomalyScore = computeRawAnomalyScore(activeColumns, predictedColumns)
# Compute final anomaly based on selected mode.
if self._mode == Anomaly.MODE_PURE:
score = anomalyScore
elif self._mode == Anomaly.MODE_LIKELIHOOD:
if inputValue is None:
raise ValueError("Selected anomaly mode 'Anomaly.MODE_LIKELIHOOD' "
"requires 'inputValue' as parameter to compute() method. ")
probability = self._likelihood.anomalyProbability(
inputValue, anomalyScore, timestamp)
# low likelihood -> hi anomaly
score = 1 - probability
elif self._mode == Anomaly.MODE_WEIGHTED:
probability = self._likelihood.anomalyProbability(
#.........这里部分代码省略.........
示例11:
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
input_event = (numpy.array([x, y, z]), radius)
timestamp = datetime.datetime.strptime(event.time, "%Y-%m-%dT%H:%M:%S.%fZ")
# input_event = (timestamp, input_event)
modelInput = {}
modelInput["event"] = input_event
modelInput["timestamp"] = (timestamp)
result = model.run(modelInput)
model.save(MODELSTATE)
# print result
if not PREDICT:
# Anomaly-Stats:
anomalyScore = result.inferences["anomalyScore"]
# By default 0.5 for the first 600 iterations!
likelihood = anomalyLikelihood.anomalyProbability(modelInput["event"], anomalyScore, modelInput["timestamp"])
logLikelihood = anomalyLikelihood.computeLogLikelihood(likelihood)
AnomalyScores.append(anomalyScore)
LikelihoodScores.append([modelInput["timestamp"], modelInput["event"], likelihood])
prediction = 'None'
if PREDICT:
# Handle Anomaly:
anomalyScore, likelihood, logLikelihood = 'None', 'None', 'None'
pred_result = shifter.shift(result)
if result.inferences["multiStepBestPredictions"][1]:
prediction = result.inferences["multiStepBestPredictions"][1]
print prediction
else:
prediction = 'None'
示例12: AnomalyLikelihoodRegion
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
#.........这里部分代码省略.........
"count": 1,
"required": True,
"isDefaultInput": False
},
},
"outputs": {
"anomalyLikelihood": {
"description": "The resultant anomaly likelihood",
"dataType": "Real32",
"count": 1,
"isDefaultOutput": True,
},
},
"parameters": {
"learningPeriod": {
"description": "The number of iterations required for the\
algorithm to learn the basic patterns in the dataset\
and for the anomaly score to 'settle down'.",
"dataType": "UInt32",
"count": 1,
"constraints": "",
"defaultValue": 288,
"accessMode": "ReadWrite"
},
"estimationSamples": {
"description": "The number of reasonable anomaly scores\
required for the initial estimate of the\
Gaussian.",
"dataType": "UInt32",
"count": 1,
"constraints": "",
"defaultValue": 100,
"accessMode": "ReadWrite"
},
"historicWindowSize": {
"description": "Size of sliding window of historical data\
points to maintain for periodic reestimation\
of the Gaussian.",
"dataType": "UInt32",
"count": 1,
"constraints": "",
"defaultValue": 8640,
"accessMode": "ReadWrite"
},
"reestimationPeriod": {
"description": "How often we re-estimate the Gaussian\
distribution.",
"dataType": "UInt32",
"count": 1,
"constraints": "",
"defaultValue": 100,
"accessMode": "ReadWrite"
},
},
"commands": {
},
}
def __init__(self,
learningPeriod = 288,
estimationSamples = 100,
historicWindowSize = 8640,
reestimationPeriod = 100):
self.anomalyLikelihood = AnomalyLikelihood(
learningPeriod = learningPeriod,
estimationSamples = estimationSamples,
historicWindowSize = historicWindowSize,
reestimationPeriod = reestimationPeriod)
def __eq__(self, other):
return self.anomalyLikelihood == other.anomalyLikelihood
def __ne__(self, other):
return not self == other
@classmethod
def read(cls, proto):
anomalyLikelihoodRegion = object.__new__(cls)
anomalyLikelihoodRegion.anomalyLikelihood = AnomalyLikelihood.read(proto)
return anomalyLikelihoodRegion
def write(self, proto):
self.anomalyLikelihood.write(proto)
def initialize(self):
pass
def compute(self, inputs, outputs):
anomalyScore = inputs["rawAnomalyScore"][0]
value = inputs["metricValue"][0]
anomalyProbability = self.anomalyLikelihood.anomalyProbability(
value, anomalyScore)
outputs["anomalyLikelihood"][0] = anomalyProbability
示例13: Anomaly
# 需要导入模块: from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood [as 别名]
# 或者: from nupic.algorithms.anomaly_likelihood.AnomalyLikelihood import anomalyProbability [as 别名]
class Anomaly(object):
"""Utility class for generating anomaly scores in different ways.
Supported modes:
MODE_PURE - the raw anomaly score as computed by computeRawAnomalyScore
MODE_LIKELIHOOD - uses the AnomalyLikelihood class on top of the raw
anomaly scores
MODE_WEIGHTED - multiplies the likelihood result with the raw anomaly score
that was used to generate the likelihood
"""
# anomaly modes supported
MODE_PURE = "pure"
MODE_LIKELIHOOD = "likelihood"
MODE_WEIGHTED = "weighted"
_supportedModes = (MODE_PURE, MODE_LIKELIHOOD, MODE_WEIGHTED)
def __init__(self, slidingWindowSize = None, mode=MODE_PURE):
"""
@param slidingWindowSize (optional) - how many elements are summed up;
enables moving average on final anomaly score; int >= 0
@param mode (optional) - (string) how to compute anomaly;
possible values are:
- "pure" - the default, how much anomal the value is;
float 0..1 where 1=totally unexpected
- "likelihood" - uses the anomaly_likelihood code;
models probability of receiving this value and anomalyScore
- "weighted" - "pure" anomaly weighted by "likelihood"
(anomaly * likelihood)
"""
self._mode = mode
self._useMovingAverage = slidingWindowSize > 0
self._buf = None
self._i = None
# Using cumulative anomaly, sliding window
if self._useMovingAverage:
self._windowSize = slidingWindowSize
# Sliding window buffer
self._buf = numpy.array([0] * self._windowSize, dtype=numpy.float)
self._i = 0 # index pointer to actual position
elif slidingWindowSize is not None:
raise TypeError(
"Anomaly: if you define slidingWindowSize, it has to be an "
"integer > 0; slidingWindowSize=%r" % slidingWindowSize)
if self._mode == Anomaly.MODE_LIKELIHOOD:
self._likelihood = AnomalyLikelihood() # probabilistic anomaly
if not self._mode in Anomaly._supportedModes:
raise ValueError("Invalid anomaly mode; only supported modes are: "
"Anomaly.MODE_PURE, Anomaly.MODE_LIKELIHOOD, "
"Anomaly.MODE_WEIGHTED; you used: %r" % self._mode)
def computeAnomalyScore(self, activeColumns, predictedColumns, value=None,
timestamp=None):
"""Compute the anomaly score as the percent of active columns not predicted.
@param activeColumns: array of active column indices
@param predictedColumns: array of columns indices predicted in this step
(used for anomaly in step T+1)
@param value: (optional) metric value of current input
(used in anomaly-likelihood)
@param timestamp: (optional) date timestamp when the sample occured
(used in anomaly-likelihood)
@return the computed anomaly score; float 0..1
"""
# Start by computing the raw anomaly score.
anomalyScore = computeRawAnomalyScore(activeColumns, predictedColumns)
# Compute final anomaly based on selected mode.
if self._mode == Anomaly.MODE_PURE:
score = anomalyScore
elif self._mode == Anomaly.MODE_LIKELIHOOD:
# TODO add tests for likelihood modes
probability = self._likelihood.anomalyProbability(
value, anomalyScore, timestamp)
score = probability
elif self._mode == Anomaly.MODE_WEIGHTED:
probability = self._likelihood.anomalyProbability(
value, anomalyScore, timestamp)
score = anomalyScore * probability
# Last, do moving-average if windowSize was specified.
if self._useMovingAverage:
score = self._movingAverage(score)
return score
def _movingAverage(self, newElement=None):
"""moving average
@param newValue (optional) add a new element before computing the avg
@return moving average of self._windowSize last elements
"""
if newElement is not None:
self._buf[self._i]= newElement
#.........这里部分代码省略.........