forked from numenta/nupic-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNNAnomalyClassifierRegion.py
944 lines (775 loc) · 30.7 KB
/
KNNAnomalyClassifierRegion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc. Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""
## @file
This file defines the k Nearest Neighbor classifier region.
"""
import numpy
import copy
import numpy.random
from PyRegion import PyRegion
from KNNClassifierRegion import KNNClassifierRegion
from nupic.algorithms.anomaly import Anomaly as AnomalyImpl
from nupic.frameworks.opf.exceptions import (CLAModelInvalidRangeError,
CLAModelInvalidArgument)
class KNNAnomalyClassifierRegion(PyRegion):
"""
KNNAnomalyClassifierRegion wraps the KNNClassifierRegion to classify clamodel
state. It allows for individual records to be classified as anomalies and
supports anomaly detection even after the model has learned the anomalous
sequence.
Methods:
compute() - called by clamodel during record processing
getLabels() - return points with classification records
addLabel() - add a set label to a given set of points
removeLabels() - remove labels from a given set of points
Parameters:
trainRecords - number of records to skip before classification
anomalyThreshold - threshold on anomaly score to automatically classify
record as an anomaly
cacheSize - number of records to keep in cache. Can only recalculate
records kept in cache when setting the trainRecords.
"""
@classmethod
def getSpec(cls):
ns = dict(
description=KNNAnomalyClassifierRegion.__doc__,
singleNodeOnly=True,
inputs=dict(
spBottomUpOut=dict(
description="""The output signal generated from the bottom-up inputs
from lower levels.""",
dataType='Real32',
count=0,
required=True,
regionLevel=False,
isDefaultInput=True,
requireSplitterMap=False),
tpTopDownOut=dict(
description="""The top-down inputsignal, generated from
feedback from upper levels""",
dataType='Real32',
count=0,
required=True,
regionLevel=False,
isDefaultInput=True,
requireSplitterMap=False),
tpLrnActiveStateT=dict(
description="""Active cells in the learn state at time T from TP.
This is used to classify on.""",
dataType='Real32',
count=0,
required=True,
regionLevel=False,
isDefaultInput=True,
requireSplitterMap=False)
),
outputs=dict(
),
parameters=dict(
trainRecords=dict(
description='Number of records to wait for training',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
anomalyThreshold=dict(
description='Threshold used to classify anomalies.',
dataType='Real32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
cacheSize=dict(
description='Number of records to store in cache.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
classificationVectorType=dict(
description="""Vector type to use when classifying.
1 - Vector Column with Difference (TP and SP)
""",
dataType='UInt32',
count=1,
constraints='',
defaultValue=1,
accessMode='ReadWrite'),
activeColumnCount=dict(
description="""Number of active columns in a given step. Typically
equivalent to SP.numActivePerInhArea""",
dataType='UInt32',
count=1,
constraints='',
defaultValue=40,
accessMode='ReadWrite'),
classificationMaxDist=dict(
description="""Maximum distance a sample can be from an anomaly
in the classifier to be labeled as an anomaly.
Ex: With rawOverlap distance, a value of 0.65 means that the points
must be at most a distance 0.65 apart from each other. This
translates to they must be at least 35% similar.""",
dataType='Real32',
count=1,
constraints='',
defaultValue=0.65,
accessMode='Create'
)
),
commands=dict(
getLabels=dict(description=
"Returns a list of label dicts with properties ROWID and labels."
"ROWID corresponds to the records id and labels is a list of "
"strings representing the records labels. Takes additional "
"integer properties start and end representing the range that "
"will be returned."),
addLabel=dict(description=
"Takes parameters start, end and labelName. Adds the label "
"labelName to the records from start to end. This will recalculate "
"labels from end to the most recent record."),
removeLabels=dict(description=
"Takes additional parameters start, end, labelFilter. Start and "
"end correspond to range to remove the label. Remove labels from "
"each record with record ROWID in range from start to end, "
"noninclusive of end. Removes all records if labelFilter is None, "
"otherwise only removes the labels eqaul to labelFilter.")
)
)
ns['parameters'].update(KNNClassifierRegion.getSpec()['parameters'])
return ns
__VERSION__ = 1
AUTO_THRESHOLD_CLASSIFIED_LABEL = "Auto Threshold Classification"
AUTO_TAG = " (auto)"
def __init__(self,
trainRecords,
anomalyThreshold,
cacheSize,
classificationVectorType=1,
activeColumnCount=40,
classificationMaxDist=0.30,
**classifierArgs):
# Internal Region Values
self._maxLabelOutputs = 16
self._activeColumnCount = activeColumnCount
self._prevPredictedColumns = numpy.array([])
self._anomalyVectorLength = None
self._classificationMaxDist = classificationMaxDist
self._iteration = 0
# Set to create deterministic classifier
classifierArgs['SVDDimCount'] = None
# Parameters
self.trainRecords = trainRecords
self.anomalyThreshold = anomalyThreshold
self.cacheSize = cacheSize
self.classificationVectorType = classificationVectorType
self._knnclassifierArgs = classifierArgs
self._knnclassifier = KNNClassifierRegion(**self._knnclassifierArgs)
self.labelResults = []
self.saved_categories = []
self._recordsCache = []
self._version = KNNAnomalyClassifierRegion.__VERSION__
# anomaly
self._anomaly = AnomalyImpl()
def initialize(self, dims, splitterMaps):
assert tuple(dims) == (1,) * len(dims)
def getParameter(self, name, index=-1):
"""
Get the value of the parameter.
@param name -- the name of the parameter to retrieve, as defined
by the Node Spec.
"""
if name == "trainRecords":
return self.trainRecords
elif name == "anomalyThreshold":
return self.anomalyThreshold
elif name == "activeColumnCount":
return self._activeColumnCount
elif name == "classificationMaxDist":
return self._classificationMaxDist
else:
# If any spec parameter name is the same as an attribute, this call
# will get it automatically, e.g. self.learningMode
return PyRegion.getParameter(self, name, index)
def setParameter(self, name, index, value):
"""
Set the value of the parameter.
@param name -- the name of the parameter to update, as defined
by the Node Spec.
@param value -- the value to which the parameter is to be set.
"""
if name == "trainRecords":
# Ensure that the trainRecords can only be set to minimum of the ROWID in
# the saved states
if not (isinstance(value, float) or isinstance(value, int)):
raise CLAModelInvalidArgument("Invalid argument type \'%s\'. threshold "
"must be a number." % (type(value)))
if len(self._recordsCache) > 0 and value < self._recordsCache[0].ROWID:
raise CLAModelInvalidArgument("Invalid value. autoDetectWaitRecord "
"value must be valid record within output stream. Current minimum "
" ROWID in output stream is %d." % (self._recordsCache[0].ROWID))
self.trainRecords = value
# Remove any labels before the first cached record (wont be used anymore)
self._deleteRangeFromKNN(0, self._recordsCache[0].ROWID)
# Reclassify all states
self.classifyStates()
elif name == "anomalyThreshold":
if not (isinstance(value, float) or isinstance(value, int)):
raise CLAModelInvalidArgument("Invalid argument type \'%s\'. threshold "
"must be a number." % (type(value)))
self.anomalyThreshold = value
self.classifyStates()
elif name == "classificationMaxDist":
if not (isinstance(value, float) or isinstance(value, int)):
raise CLAModelInvalidArgument("Invalid argument type \'%s\'. "
"classificationMaxDist must be a number." % (type(value)))
self._classificationMaxDist = value
self.classifyStates()
elif name == "activeColumnCount":
self._activeColumnCount = value
else:
return PyRegion.setParameter(self, name, index, value)
def compute(self, inputs, outputs):
"""
Process one input sample.
This method is called by the runtime engine.
"""
record = self.constructClassificationRecord(inputs)
#Classify this point after waiting the classification delay
if record.ROWID >= self.getParameter('trainRecords'):
self.classifyState(record)
#Save new classification record and keep history as moving window
self._recordsCache.append(record)
while len(self._recordsCache) > self.cacheSize:
self._recordsCache.pop(0)
self.labelResults = record.anomalyLabel
self._iteration += 1
def getLabelResults(self):
"""
Get the labels of the previously computed record.
----------------
retval - array of strings representing the classification labels
"""
return self.labelResults
def classifyStates(self):
"""
Reclassifies all internal state
"""
for state in self._recordsCache:
self.classifyState(state)
def classifyState(self, state):
"""
Reclassifies given state.
"""
# Record is before wait period do not classifiy
if state.ROWID < self.getParameter('trainRecords'):
if not state.setByUser:
state.anomalyLabel = []
self._deleteRecordsFromKNN([state])
return
label = KNNAnomalyClassifierRegion.AUTO_THRESHOLD_CLASSIFIED_LABEL
autoLabel = label + KNNAnomalyClassifierRegion.AUTO_TAG
# Update the label based on classifications
newCategory = self._recomputeRecordFromKNN(state)
labelList = self._categoryToLabelList(newCategory)
if state.setByUser:
if label in state.anomalyLabel:
state.anomalyLabel.remove(label)
if autoLabel in state.anomalyLabel:
state.anomalyLabel.remove(autoLabel)
labelList.extend(state.anomalyLabel)
# Add threshold classification label if above threshold, else if
# classified to add the auto threshold classification.
if state.anomalyScore >= self.getParameter('anomalyThreshold'):
labelList.append(label)
elif label in labelList:
ind = labelList.index(label)
labelList[ind] = autoLabel
# Make all entries unique
labelList = list(set(labelList))
# If both above threshold and auto classified above - remove auto label
if label in labelList and autoLabel in labelList:
labelList.remove(autoLabel)
if state.anomalyLabel == labelList:
return
# Update state's labeling
state.anomalyLabel = labelList
# Update KNN Classifier with new labeling
if state.anomalyLabel == []:
self._deleteRecordsFromKNN([state])
else:
self._addRecordToKNN(state)
def constructClassificationRecord(self, inputs):
"""
Construct a _CLAClassificationRecord based on the state of the model
passed in through the inputs.
Types for self.classificationVectorType:
1 - TP active cells in learn state
2 - SP columns concatenated with error from TP column predictions and SP
"""
# Count the number of unpredicted columns
allSPColumns = inputs["spBottomUpOut"]
activeSPColumns = allSPColumns.nonzero()[0]
score = self._anomaly.computeAnomalyScore(activeSPColumns, self._prevPredictedColumns)
spSize = len(allSPColumns)
allTPCells = inputs['tpTopDownOut']
tpSize = len(inputs['tpLrnActiveStateT'])
classificationVector = numpy.array([])
if self.classificationVectorType == 1:
# Classification Vector: [---TP Cells---]
classificationVector = numpy.zeros(tpSize)
activeCellMatrix = inputs["tpLrnActiveStateT"].reshape(tpSize, 1)
activeCellIdx = numpy.where(activeCellMatrix > 0)[0]
if activeCellIdx.shape[0] > 0:
classificationVector[numpy.array(activeCellIdx, dtype=numpy.uint16)] = 1
elif self.classificationVectorType == 2:
# Classification Vecotr: [---SP---|---(TP-SP)----]
classificationVector = numpy.zeros(spSize+spSize)
if activeSPColumns.shape[0] > 0:
classificationVector[activeSPColumns] = 1.0
errorColumns = numpy.setdiff1d(self._prevPredictedColumns,
activeSPColumns)
if errorColumns.shape[0] > 0:
errorColumnIndexes = ( numpy.array(errorColumns, dtype=numpy.uint16) +
spSize )
classificationVector[errorColumnIndexes] = 1.0
else:
raise TypeError("Classification vector type must be either 'tpc' or"
" 'sp_tpe', current value is %s" % (self.classificationVectorType))
# Store the state for next time step
numPredictedCols = len(self._prevPredictedColumns)
predictedColumns = allTPCells.nonzero()[0]
self._prevPredictedColumns = copy.deepcopy(predictedColumns)
if self._anomalyVectorLength is None:
self._anomalyVectorLength = len(classificationVector)
result = _CLAClassificationRecord(
ROWID=self._iteration, #__numRunCalls called
#at beginning of model.run
anomalyScore=score,
anomalyVector=classificationVector.nonzero()[0].tolist(),
anomalyLabel=[]
)
return result
def _addRecordToKNN(self, record):
"""
Adds the record to the KNN classifier.
"""
knn = self._knnclassifier._knn
prototype_idx = self._knnclassifier.getParameter('categoryRecencyList')
category = self._labelListToCategoryNumber(record.anomalyLabel)
# If record is already in the classifier, overwrite its labeling
if record.ROWID in prototype_idx:
knn.prototypeSetCategory(record.ROWID, category)
return
# Learn this pattern in the knn
pattern = self._getStateAnomalyVector(record)
rowID = record.ROWID
knn.learn(pattern, category, rowID=rowID)
def _deleteRecordsFromKNN(self, recordsToDelete):
"""
Removes the given records from the classifier.
parameters
------------
recordsToDelete - list of records to delete from the classififier
"""
prototype_idx = self._knnclassifier.getParameter('categoryRecencyList')
idsToDelete = ([r.ROWID for r in recordsToDelete if
not r.setByUser and r.ROWID in prototype_idx])
nProtos = self._knnclassifier._knn._numPatterns
self._knnclassifier._knn.removeIds(idsToDelete)
assert self._knnclassifier._knn._numPatterns == nProtos - len(idsToDelete)
def _deleteRangeFromKNN(self, start=0, end=None):
"""
Removes any stored records within the range from start to
end. Noninclusive of end.
parameters
------------
start - integer representing the ROWID of the start of the deletion range,
end - integer representing the ROWID of the end of the deletion range,
if None, it will default to end.
"""
prototype_idx = numpy.array(
self._knnclassifier.getParameter('categoryRecencyList'))
if end is None:
end = prototype_idx.max() + 1
idsIdxToDelete = numpy.logical_and(prototype_idx >= start,
prototype_idx < end)
idsToDelete = prototype_idx[idsIdxToDelete]
nProtos = self._knnclassifier._knn._numPatterns
self._knnclassifier._knn.removeIds(idsToDelete.tolist())
assert self._knnclassifier._knn._numPatterns == nProtos - len(idsToDelete)
def _recomputeRecordFromKNN(self, record):
"""
returns the classified labeling of record
"""
inputs = {
"categoryIn": [None],
"bottomUpIn": self._getStateAnomalyVector(record),
}
outputs = {"categoriesOut": numpy.zeros((1,)),
"bestPrototypeIndices":numpy.zeros((1,)),
"categoryProbabilitiesOut":numpy.zeros((1,))}
# Only use points before record to classify and after the wait period.
classifier_indexes = numpy.array(
self._knnclassifier.getParameter('categoryRecencyList'))
valid_idx = numpy.where(
(classifier_indexes >= self.getParameter('trainRecords')) &
(classifier_indexes < record.ROWID)
)[0].tolist()
if len(valid_idx) == 0:
return None
self._knnclassifier.setParameter('inferenceMode', None, True)
self._knnclassifier.setParameter('learningMode', None, False)
self._knnclassifier.compute(inputs, outputs)
self._knnclassifier.setParameter('learningMode', None, True)
classifier_distances = self._knnclassifier.getLatestDistances()
valid_distances = classifier_distances[valid_idx]
if valid_distances.min() <= self._classificationMaxDist:
classifier_indexes_prev = classifier_indexes[valid_idx]
rowID = classifier_indexes_prev[valid_distances.argmin()]
indexID = numpy.where(classifier_indexes == rowID)[0][0]
category = self._knnclassifier.getCategoryList()[indexID]
return category
return None
def _labelToCategoryNumber(self, label):
"""
Since the KNN Classifier stores categories as numbers, we must store each
label as a number. This method converts from a label to a unique number.
Each label is assigned a unique bit so multiple labels may be assigned to
a single record.
"""
if label not in self.saved_categories:
self.saved_categories.append(label)
return pow(2, self.saved_categories.index(label))
def _labelListToCategoryNumber(self, labelList):
"""
This method takes a list of labels and returns a unique category number.
This enables this class to store a list of categories for each point since
the KNN classifier only stores a single number category for each record.
"""
categoryNumber = 0
for label in labelList:
categoryNumber += self._labelToCategoryNumber(label)
return categoryNumber
def _categoryToLabelList(self, category):
"""
Converts a category number into a list of labels
"""
if category is None:
return []
labelList = []
labelNum = 0
while category > 0:
if category % 2 == 1:
labelList.append(self.saved_categories[labelNum])
labelNum += 1
category = category >> 1
return labelList
def _getStateAnomalyVector(self, state):
"""
Returns a state's anomaly vertor converting it from spare to dense
"""
vector = numpy.zeros(self._anomalyVectorLength)
vector[state.anomalyVector] = 1
return vector
def getLabels(self, start=None, end=None):
"""
Get the labels on classified points within range start to end. Not inclusive
of end.
reval - dict of format:
{
'isProcessing': boolean,
'recordLabels': list of results
}
isProcessing - currently always false as recalculation blocks; used if
reprocessing of records is still being performed;
Each item in recordLabels is of format:
{
'ROWID': id of the row,
'labels': list of strings
}
"""
if len(self._recordsCache) == 0:
return {
'isProcessing': False,
'recordLabels': []
}
try:
start = int(start)
except Exception:
start = 0
try:
end = int(end)
except Exception:
end = self._recordsCache[-1].ROWID
if end <= start:
raise CLAModelInvalidRangeError("Invalid supplied range for 'getLabels'.",
debugInfo={
'requestRange': {
'startRecordID': start,
'endRecordID': end
},
'numRecordsStored': len(self._recordsCache)
})
results = {
'isProcessing': False,
'recordLabels': []
}
ROWIDX = numpy.array(
self._knnclassifier.getParameter('categoryRecencyList'))
validIdx = numpy.where((ROWIDX >= start) & (ROWIDX < end))[0].tolist()
categories = self._knnclassifier.getCategoryList()
for idx in validIdx:
row = dict(
ROWID=int(ROWIDX[idx]),
labels=self._categoryToLabelList(categories[idx]))
results['recordLabels'].append(row)
return results
def addLabel(self, start, end, labelName):
"""
Add the label labelName to each record with record ROWID in range from
start to end, noninclusive of end.
This will recalculate all points from end to the last record stored in the
internal cache of this classifier.
"""
if len(self._recordsCache) == 0:
raise CLAModelInvalidRangeError("Invalid supplied range for 'addLabel'. "
"Model has no saved records.")
try:
start = int(start)
except Exception:
start = 0
try:
end = int(end)
except Exception:
end = int(self._recordsCache[-1].ROWID)
startID = self._recordsCache[0].ROWID
clippedStart = max(0, start - startID)
clippedEnd = max(0, min( len( self._recordsCache) , end - startID))
if clippedEnd <= clippedStart:
raise CLAModelInvalidRangeError("Invalid supplied range for 'addLabel'.",
debugInfo={
'requestRange': {
'startRecordID': start,
'endRecordID': end
},
'clippedRequestRange': {
'startRecordID': clippedStart,
'endRecordID': clippedEnd
},
'validRange': {
'startRecordID': startID,
'endRecordID': self._recordsCache[len(self._recordsCache)-1].ROWID
},
'numRecordsStored': len(self._recordsCache)
})
# Add label to range [clippedStart, clippedEnd)
for state in self._recordsCache[clippedStart:clippedEnd]:
if labelName not in state.anomalyLabel:
state.anomalyLabel.append(labelName)
state.setByUser = True
self._addRecordToKNN(state)
assert len(self.saved_categories) > 0
# Recompute [end, ...)
for state in self._recordsCache[clippedEnd:]:
self.classifyState(state)
def removeLabels(self, start=None, end=None, labelFilter=None):
"""
Remove labels from each record with record ROWID in range from
start to end, noninclusive of end. Removes all records if labelFilter is
None, otherwise only removes the labels eqaul to labelFilter.
This will recalculate all points from end to the last record stored in the
internal cache of this classifier.
"""
if len(self._recordsCache) == 0:
raise CLAModelInvalidRangeError("Invalid supplied range for "
"'removeLabels'. Model has no saved records.")
try:
start = int(start)
except Exception:
start = 0
try:
end = int(end)
except Exception:
end = self._recordsCache[-1].ROWID
startID = self._recordsCache[0].ROWID
clippedStart = 0 if start is None else max(0, start - startID)
clippedEnd = len(self._recordsCache) if end is None else \
max(0, min( len( self._recordsCache) , end - startID))
if clippedEnd <= clippedStart:
raise CLAModelInvalidRangeError("Invalid supplied range for "
"'removeLabels'.", debugInfo={
'requestRange': {
'startRecordID': start,
'endRecordID': end
},
'clippedRequestRange': {
'startRecordID': clippedStart,
'endRecordID': clippedEnd
},
'validRange': {
'startRecordID': startID,
'endRecordID': self._recordsCache[len(self._recordsCache)-1].ROWID
},
'numRecordsStored': len(self._recordsCache)
})
# Remove records within the cache
recordsToDelete = []
for state in self._recordsCache[clippedStart:clippedEnd]:
if labelFilter is not None:
if labelFilter in state.anomalyLabel:
state.anomalyLabel.remove(labelFilter)
else:
state.anomalyLabel = []
state.setByUser = False
recordsToDelete.append(state)
self._deleteRecordsFromKNN(recordsToDelete)
# Remove records not in cache
self._deleteRangeFromKNN(start, end)
# Recompute [clippedEnd, ...)
for state in self._recordsCache[clippedEnd:]:
self.classifyState(state)
#############################################################################
#
# Methods to support serialization
#
#############################################################################
#############################################################################
def __getstate__(self):
"""
Return serializable state. This function will return a version of the
__dict__ with all "ephemeral" members stripped out. "Ephemeral" members
are defined as those that do not need to be (nor should be) stored
in any kind of persistent file (e.g., NuPIC network XML file.)
"""
state = self.__dict__.copy()
# Save knnclassifier properties
state['_knnclassifierProps'] = state['_knnclassifier'].__getstate__()
state.pop('_knnclassifier')
return state
#############################################################################
def __setstate__(self, state):
"""
Set the state of ourself from a serialized state.
"""
if '_version' not in state or state['_version'] == 1:
knnclassifierProps = state.pop('_knnclassifierProps')
self.__dict__.update(state)
self._knnclassifier = KNNClassifierRegion(**self._knnclassifierArgs)
self._knnclassifier.__setstate__(knnclassifierProps)
self._version = KNNAnomalyClassifierRegion.__VERSION__
else:
raise Exception("Invalid KNNAnomalyClassifierRegion version. Current "
"version: %s" % (KNNAnomalyClassifierRegion.__VERSION__))
def diff(self, knnRegion2):
diff = []
toCheck = [((), self.__getstate__(), knnRegion2.__getstate__())]
while toCheck:
keys, a, b = toCheck.pop()
if type(a) != type(b):
diff.append((keys, a, b))
elif 'saved_categories' in keys:
cats1 = set(a)
cats2 = set(b)
if cats1 != cats2:
for k in cats1 - cats2:
diff.append((keys + (k,), a[k], None))
for k in cats1 - cats2:
diff.append((keys + (k,), None, b[k]))
elif '_recordsCache' in keys:
if len(a) != len(b):
diff.append((keys + ('len', ), len(a), len(b)))
for i, v in enumerate(a):
if not (a[i] == b[i]):
diff.append((keys + ('_' + str(i), ), a[i].__getstate__(),
b[i].__getstate__()))
elif isinstance(a, dict):
keys1 = set(a.keys())
keys2 = set(b.keys())
# If there are missing keys, add them to the diff.
if keys1 != keys2:
for k in keys1 - keys2:
diff.append((keys + (k,), [k], None))
for k in keys2 - keys1:
diff.append((keys + (k,), None, b[k]))
# For matching keys, add the values to the list of things to check.
for k in keys1.union(keys2):
toCheck.append((keys + (k,), a[k], b[k]))
elif (isinstance(a, numpy.ndarray) or isinstance(a, list) or
isinstance(a, tuple)):
if len(a) != len(b):
diff.append((keys + ('len', ), len(a), len(b)))
elif not numpy.array_equal(a, b):
diff.append((keys, a, b))
#for i in xrange(len(a)):
# toCheck.append((keys + (k, i), a[i], b[i]))
elif isinstance(a, numpy.random.RandomState):
for i, v in enumerate(a.get_state()):
toCheck.append((keys + (i,), v, b.get_state()[i]))
else:
try:
_ = a != b
except ValueError:
raise ValueError(type(a))
if a != b:
diff.append((keys, a, b))
return diff
#############################################################################
#
# NuPIC 2 Support
# These methods are required by NuPIC 2
#
#############################################################################
def getOutputElementCount(self, name):
if name == 'labels':
return self._maxLabelOutputs
else:
raise Exception("Invalid output name specified")
class _CLAClassificationRecord(object):
"""
A single record to store data associated with a single prediction for the
anomaly classifier.
ROWID - prediction stream ROWID record number
setByUser - if true, a delete must be called explicitly on this point to
remove its label
"""
__slots__ = ["ROWID", "anomalyScore", "anomalyVector", "anomalyLabel",
"setByUser"]
def __init__(self, ROWID, anomalyScore, anomalyVector, anomalyLabel,
setByUser=False):
self.ROWID = ROWID
self.anomalyScore = anomalyScore
self.anomalyVector = anomalyVector
self.anomalyLabel = anomalyLabel
self.setByUser = setByUser
def __getstate__(self):
obj_slot_values = dict((k, getattr(self, k)) for k in self.__slots__)
return obj_slot_values
def __setstate__(self, data_dict):
for (name, value) in data_dict.iteritems():
setattr(self, name, value)
def __eq__(self, other):
return (self.ROWID == other.ROWID and
self.anomalyScore == other.anomalyScore and
self.anomalyLabel == other.anomalyLabel and
self.setByUser == other.setByUser and
numpy.array_equal(self.anomalyVector, other.anomalyVector))