Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
Changed distribution keyword, casted some attributes to float, remove…
Browse files Browse the repository at this point in the history
…d setting list (#3784)

Changed capnproto schema, added distribution key on read method
  • Loading branch information
Kyle Sorensen authored and rhyolight committed Jan 3, 2018
1 parent 41e5a6a commit 5ecae91
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
72 changes: 32 additions & 40 deletions src/nupic/algorithms/anomaly_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,26 +271,23 @@ def read(cls, proto):
anomalyLikelihood._historicalScores.append((i, score.value,
score.anomalyScore))
if proto.distribution.name: # is "" when there is no distribution.
anomalyLikelihood._distribution = {}
anomalyLikelihood._distribution["name"] = proto.distribution.name
anomalyLikelihood._distribution["mean"] = proto.distribution.mean
anomalyLikelihood._distribution["variance"] = proto.distribution.variance
anomalyLikelihood._distribution["stdev"] = proto.distribution.stdev
anomalyLikelihood._distribution = dict()
anomalyLikelihood._distribution['distribution'] = dict()
anomalyLikelihood._distribution['distribution']["name"] = proto.distribution.name
anomalyLikelihood._distribution['distribution']["mean"] = proto.distribution.mean
anomalyLikelihood._distribution['distribution']["variance"] = proto.distribution.variance
anomalyLikelihood._distribution['distribution']["stdev"] = proto.distribution.stdev

anomalyLikelihood._distribution["movingAverage"] = {}
anomalyLikelihood._distribution["movingAverage"]["windowSize"] =\
proto.distribution.movingAverage.windowSize
anomalyLikelihood._distribution["movingAverage"]["windowSize"] = proto.distribution.movingAverage.windowSize
anomalyLikelihood._distribution["movingAverage"]["historicalValues"] = []
for value in proto.distribution.movingAverage.historicalValues:
anomalyLikelihood._distribution["movingAverage"]["historicalValues"]\
.append(value)
anomalyLikelihood._distribution["movingAverage"]["total"] =\
proto.distribution.movingAverage.total
anomalyLikelihood._distribution["movingAverage"]["historicalValues"].append(value)
anomalyLikelihood._distribution["movingAverage"]["total"] = proto.distribution.movingAverage.total

anomalyLikelihood._distribution["historicalLikelihoods"] = []
for likelihood in proto.distribution.historicalLikelihoods:
anomalyLikelihood._distribution["historicalLikelihoods"].append(
likelihood)
anomalyLikelihood._distribution["historicalLikelihoods"].append(likelihood)
else:
anomalyLikelihood._distribution = None

Expand All @@ -308,6 +305,7 @@ def write(self, proto):
:param proto: (Object) capnp proto object specified in
nupic.regions.AnomalyLikelihoodRegion.capnp
"""

proto.iteration = self._iteration

pHistScores = proto.init('historicalScores', len(self._historicalScores))
Expand All @@ -317,33 +315,27 @@ def write(self, proto):
record.value = float(value)
record.anomalyScore = float(anomalyScore)

if self._distribution:
proto.distribution.name = self._distribution["distributionParams"]["name"]
proto.distribution.mean = self._distribution["distributionParams"]["mean"]
proto.distribution.variance = self._distribution["distributionParams"]\
["variance"]
proto.distribution.stdev = self._distribution["distributionParams"]\
["stdev"]

proto.distribution.movingAverage.windowSize = self._distribution\
["movingAverage"]["windowSize"]

historicalValues = self._distribution["movingAverage"]["historicalValues"]
pHistValues = proto.distribution.movingAverage.init(
"historicalValues", len(historicalValues))
for i, value in enumerate(historicalValues):
pHistValues[i] = float(value)

proto.distribution.movingAverage.historicalValues = self._distribution\
["movingAverage"]["historicalValues"]
proto.distribution.movingAverage.total = self._distribution\
["movingAverage"]["total"]

historicalLikelihoods = self._distribution["historicalLikelihoods"]
pHistLikelihoods = proto.distribution.init("historicalLikelihoods",
len(historicalLikelihoods))
for i, likelihood in enumerate(historicalLikelihoods):
pHistLikelihoods[i] = float(likelihood)
proto.distribution.name = self._distribution["distribution"]["name"]
proto.distribution.mean = float(self._distribution["distribution"]["mean"])
proto.distribution.variance = float(self._distribution["distribution"]["variance"])
proto.distribution.stdev = float(self._distribution["distribution"]["stdev"])

proto.distribution.movingAverage.windowSize = float(self._distribution["movingAverage"]["windowSize"])

historicalValues = self._distribution["movingAverage"]["historicalValues"]
pHistValues = proto.distribution.movingAverage.init(
"historicalValues", len(historicalValues))
for i, value in enumerate(historicalValues):
pHistValues[i] = float(value)

#proto.distribution.movingAverage.historicalValues = self._distribution["movingAverage"]["historicalValues"]
proto.distribution.movingAverage.total = float(self._distribution["movingAverage"]["total"])

historicalLikelihoods = self._distribution["historicalLikelihoods"]
pHistLikelihoods = proto.distribution.init("historicalLikelihoods",
len(historicalLikelihoods))
for i, likelihood in enumerate(historicalLikelihoods):
pHistLikelihoods[i] = float(likelihood)

proto.probationaryPeriod = self._probationaryPeriod
proto.learningPeriod = self._learningPeriod
Expand Down
12 changes: 6 additions & 6 deletions src/nupic/regions/AnomalyLikelihoodRegion.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ struct AnomalyLikelihoodRegionProto {

struct Distribution {
name @0 :Text;
mean @1 :Float32;
variance @2 :Float32;
stdev @3 :Float32;
mean @1 :Float64;
variance @2 :Float64;
stdev @3 :Float64;
movingAverage @4 :MovingAverage;
historicalLikelihoods @5 :List(Float32);
historicalLikelihoods @5 :List(Float64);

struct MovingAverage {
windowSize @0 :UInt64;
historicalValues @1 :List(Float32);
total @2 :Float32;
historicalValues @1 :List(Float64);
total @2 :Float64;
}
}
}

0 comments on commit 5ecae91

Please sign in to comment.