Skip to content

Commit

Permalink
Added Serializable to all classes with a capnp write function (#3710)
Browse files Browse the repository at this point in the history
* Added Serializable to all classes with a capnp write function

* Added getSchema to MovingAverage

* Fixed Serializable extensions

* getSchema returns prototype

* Safe import of capnp for moving average proto
  • Loading branch information
rhyolight authored Jun 15, 2017
1 parent 2bd2fb7 commit 9641434
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/nupic/algorithms/anomaly_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@
import numbers
import numpy

from nupic.serializable import Serializable
from nupic.utils import MovingAverage


class AnomalyLikelihood(object):
class AnomalyLikelihood(Serializable):
"""
Helper class for running anomaly likelihood computation. To use it simply
create an instance and then feed it successive anomaly scores:
Expand Down
4 changes: 3 additions & 1 deletion src/nupic/algorithms/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from bisect import bisect_left
from collections import defaultdict

from nupic.serializable import Serializable

EPSILON = 0.00001 # constant error threshold to check equality of permanences to
# other floats

Expand Down Expand Up @@ -122,7 +124,7 @@ def binSearch(arr, val):



class Connections(object):
class Connections(Serializable):
"""
Class to hold data representing the connectivity of a collection of cells.
Expand Down
4 changes: 2 additions & 2 deletions src/nupic/algorithms/knn_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from nupic.bindings.math import (NearestNeighbor, min_score_per_category)


from nupic.serializable import Serializable

g_debugPrefix = "KNN"
KNNCLASSIFIER_VERSION = 1
Expand Down Expand Up @@ -61,7 +61,7 @@ def _labeledInput(activeInputs, cellsPerCol=32):



class KNNClassifier(object):
class KNNClassifier(Serializable):
"""
This class implements NuPIC's k Nearest Neighbor Classifier. KNN is very
useful as a basic classifier for many situations. This implementation contains
Expand Down
3 changes: 2 additions & 1 deletion src/nupic/algorithms/sdr_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@

import numpy

from nupic.serializable import Serializable


class SDRClassifier(object):
class SDRClassifier(Serializable):
"""
The SDR Classifier accepts a binary input pattern from the
level below (the "activationPattern") and information from the sensor and
Expand Down
5 changes: 2 additions & 3 deletions src/nupic/algorithms/spatial_pooler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
GetNTAReal,
Random as NupicRandom)
from nupic.math import topology


from nupic.serializable import Serializable

realDType = GetNTAReal()
uintType = "uint32"
Expand Down Expand Up @@ -89,7 +88,7 @@ class BinaryCorticalColumns(_SparseMatrixCorticalColumnAdapter,



class SpatialPooler(object):
class SpatialPooler(Serializable):
"""
This class implements the spatial pooler. It is in charge of handling the
relationships between the columns of a region and the inputs bits. The
Expand Down
3 changes: 2 additions & 1 deletion src/nupic/algorithms/temporal_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
from operator import mul

from nupic.algorithms.connections import Connections, binSearch
from nupic.serializable import Serializable
from nupic.support.group_by import groupby2

EPSILON = 0.00001 # constant error threshold to check equality of permanences to
# other floats



class TemporalMemory(object):
class TemporalMemory(Serializable):
"""
Class implementing the Temporal Memory algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/nupic/encoders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import numpy

from nupic.encoders.utils import bitsToString
from nupic.serializable import Serializable

defaultDtype = numpy.uint8

Expand Down Expand Up @@ -64,7 +65,7 @@ def _isSequence(obj):



class Encoder(object):
class Encoder(Serializable):
"""
An encoder converts a value to a sparse distributed representation.
Expand Down
4 changes: 2 additions & 2 deletions src/nupic/frameworks/opf/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
from abc import ABCMeta, abstractmethod

import nupic.frameworks.opf.opf_utils as opf_utils
from nupic.serializable import Serializable



class Model(object):
class Model(Serializable):
""" This is the base class that all OPF Model implementations should
subclass.
Expand Down
4 changes: 3 additions & 1 deletion src/nupic/regions/anomaly_likelihood_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
from nupic.algorithms.anomaly_likelihood import AnomalyLikelihood
from nupic.bindings.regions.PyRegion import PyRegion

from nupic.serializable import Serializable

class AnomalyLikelihoodRegion(PyRegion):

class AnomalyLikelihoodRegion(PyRegion, Serializable):
"""Region for computing the anomaly likelihoods."""


Expand Down
3 changes: 2 additions & 1 deletion src/nupic/regions/anomaly_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
from nupic.algorithms import anomaly
from nupic.bindings.regions.PyRegion import PyRegion

from nupic.serializable import Serializable


class AnomalyRegion(PyRegion):
class AnomalyRegion(PyRegion, Serializable):
"""Region for computing the anomaly score."""


Expand Down
15 changes: 14 additions & 1 deletion src/nupic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@

import numbers

from nupic.serializable import Serializable
try:
import capnp
except ImportError:
capnp = None
if capnp:
from nupic.movingaverage_capnp import MovingAverageProto


class MovingAverage(object):
class MovingAverage(Serializable):
"""Helper class for computing moving average and sliding window"""


Expand Down Expand Up @@ -127,3 +134,9 @@ def write(self, proto):
proto.windowSize = self.windowSize
proto.slidingWindow = self.slidingWindow
proto.total = self.total


@classmethod
def getSchema(cls):
return MovingAverageProto

0 comments on commit 9641434

Please sign in to comment.