midas-edge 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,105 @@
1
+ // -----------------------------------------------------------------------------
2
+ // Copyright 2020 Rui Liu (liurui39660) and Siddharth Bhatia (bhatiasiddharth)
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ // -----------------------------------------------------------------------------
16
+
17
+ #pragma once
18
+
19
+ #include <algorithm>
20
+
21
+ namespace MIDAS {
22
+ struct CountMinSketch {
23
+ // Fields
24
+ // --------------------------------------------------------------------------------
25
+
26
+ const int r, c, m = 104729; // Yes, a magic number, I just pick a random prime
27
+ const int lenData;
28
+ int* const param1;
29
+ int* const param2;
30
+ float* const data;
31
+ constexpr static float infinity = std::numeric_limits<float>::infinity();
32
+
33
+ // Methods
34
+ // --------------------------------------------------------------------------------
35
+
36
+ CountMinSketch() = delete;
37
+ CountMinSketch& operator=(const CountMinSketch& b) = delete;
38
+
39
+ CountMinSketch(int numRow, int numColumn):
40
+ r(numRow),
41
+ c(numColumn),
42
+ lenData(r * c),
43
+ param1(new int[r]),
44
+ param2(new int[r]),
45
+ data(new float[lenData]) {
46
+ for (int i = 0; i < r; i++) {
47
+ param1[i] = rand() + 1; // ×0 is not a good idea, see Hash()
48
+ param2[i] = rand();
49
+ }
50
+ std::fill(data, data + lenData, 0);
51
+ }
52
+
53
+ CountMinSketch(const CountMinSketch& b):
54
+ r(b.r),
55
+ c(b.c),
56
+ lenData(b.lenData),
57
+ param1(new int[r]),
58
+ param2(new int[r]),
59
+ data(new float[lenData]) {
60
+ std::copy(b.param1, b.param1 + r, param1);
61
+ std::copy(b.param2, b.param2 + r, param2);
62
+ std::copy(b.data, b.data + lenData, data);
63
+ }
64
+
65
+ ~CountMinSketch() {
66
+ delete[] param1;
67
+ delete[] param2;
68
+ delete[] data;
69
+ }
70
+
71
+ void ClearAll(float with = 0) const {
72
+ std::fill(data, data + lenData, with);
73
+ }
74
+
75
+ void MultiplyAll(float by) const {
76
+ for (int i = 0, I = lenData; i < I; i++) // Vectorization
77
+ data[i] *= by;
78
+ }
79
+
80
+ void Hash(int* indexOut, int a, int b = 0) const {
81
+ for (int i = 0; i < r; i++) {
82
+ indexOut[i] = ((a + m * b) * param1[i] + param2[i]) % c;
83
+ indexOut[i] += i * c + (indexOut[i] < 0 ? c : 0);
84
+ }
85
+ }
86
+
87
+ float operator()(const int* index) const {
88
+ float least = infinity;
89
+ for (int i = 0; i < r; i++)
90
+ least = std::min(least, data[index[i]]);
91
+ return least;
92
+ }
93
+
94
+ float Assign(const int* index, float with) const {
95
+ for (int i = 0; i < r; i++)
96
+ data[index[i]] = with;
97
+ return with;
98
+ }
99
+
100
+ void Add(const int* index, float by = 1) const {
101
+ for (int i = 0; i < r; i++)
102
+ data[index[i]] += by;
103
+ }
104
+ };
105
+ }
@@ -0,0 +1,98 @@
1
+ // -----------------------------------------------------------------------------
2
+ // Copyright 2020 Rui Liu (liurui39660) and Siddharth Bhatia (bhatiasiddharth)
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ // -----------------------------------------------------------------------------
16
+
17
+ #pragma once
18
+
19
+ #include <cmath>
20
+
21
+ #include "CountMinSketch.hpp"
22
+
23
+ namespace MIDAS {
24
+ struct FilteringCore {
25
+ const float threshold;
26
+ int timestamp = 1;
27
+ const float factor;
28
+ const int lenData;
29
+ int* const indexEdge; // Pre-compute the index to-be-modified, thanks to the Same-Layout Assumption
30
+ int* const indexSource;
31
+ int* const indexDestination;
32
+ CountMinSketch numCurrentEdge, numTotalEdge, scoreEdge;
33
+ CountMinSketch numCurrentSource, numTotalSource, scoreSource;
34
+ CountMinSketch numCurrentDestination, numTotalDestination, scoreDestination;
35
+ float timestampReciprocal = 0;
36
+ bool* const shouldMerge;
37
+
38
+ FilteringCore(int numRow, int numColumn, float threshold, float factor = 0.5):
39
+ threshold(threshold),
40
+ factor(factor),
41
+ lenData(numRow * numColumn), // I assume all CMSs have same size, but Same-Layout Assumption is not that strict
42
+ indexEdge(new int[numRow]),
43
+ indexSource(new int[numRow]),
44
+ indexDestination(new int[numRow]),
45
+ numCurrentEdge(numRow, numColumn),
46
+ numTotalEdge(numCurrentEdge),
47
+ scoreEdge(numCurrentEdge),
48
+ numCurrentSource(numRow, numColumn),
49
+ numTotalSource(numCurrentSource),
50
+ scoreSource(numCurrentSource),
51
+ numCurrentDestination(numRow, numColumn),
52
+ numTotalDestination(numCurrentDestination),
53
+ scoreDestination(numCurrentDestination),
54
+ shouldMerge(new bool[numRow * numColumn]) { }
55
+
56
+ virtual ~FilteringCore() {
57
+ delete[] indexEdge;
58
+ delete[] indexSource;
59
+ delete[] indexDestination;
60
+ delete[] shouldMerge;
61
+ }
62
+
63
+ static float ComputeScore(float a, float s, float t) {
64
+ return s == 0 ? 0 : pow(a + s - a * t, 2) / (s * (t - 1)); // If t == 1, then s == 0, so no need to check twice
65
+ }
66
+
67
+ void ConditionalMerge(const float* current, float* total, const float* score) const {
68
+ for (int i = 0; i < lenData; i++)
69
+ shouldMerge[i] = score[i] < threshold;
70
+ for (int i = 0, I = lenData; i < I; i++) // Vectorization
71
+ total[i] += shouldMerge[i] * current[i] + (true - shouldMerge[i]) * total[i] * timestampReciprocal;
72
+ }
73
+
74
+ float operator()(int source, int destination, int timestamp) {
75
+ if (this->timestamp < timestamp) {
76
+ ConditionalMerge(numCurrentEdge.data, numTotalEdge.data, scoreEdge.data);
77
+ ConditionalMerge(numCurrentSource.data, numTotalSource.data, scoreSource.data);
78
+ ConditionalMerge(numCurrentDestination.data, numTotalDestination.data, scoreDestination.data);
79
+ numCurrentEdge.MultiplyAll(factor);
80
+ numCurrentSource.MultiplyAll(factor);
81
+ numCurrentDestination.MultiplyAll(factor);
82
+ timestampReciprocal = 1.f / (timestamp - 1); // So I can skip an if-statement
83
+ this->timestamp = timestamp;
84
+ }
85
+ numCurrentEdge.Hash(indexEdge, source, destination);
86
+ numCurrentEdge.Add(indexEdge);
87
+ numCurrentSource.Hash(indexSource, source);
88
+ numCurrentSource.Add(indexSource);
89
+ numCurrentDestination.Hash(indexDestination, destination);
90
+ numCurrentDestination.Add(indexDestination);
91
+ return std::max({
92
+ scoreEdge.Assign(indexEdge, ComputeScore(numCurrentEdge(indexEdge), numTotalEdge(indexEdge), timestamp)),
93
+ scoreSource.Assign(indexSource, ComputeScore(numCurrentSource(indexSource), numTotalSource(indexSource), timestamp)),
94
+ scoreDestination.Assign(indexDestination, ComputeScore(numCurrentDestination(indexDestination), numTotalDestination(indexDestination), timestamp)),
95
+ });
96
+ }
97
+ };
98
+ }
@@ -0,0 +1,53 @@
1
+ // -----------------------------------------------------------------------------
2
+ // Copyright 2020 Rui Liu (liurui39660) and Siddharth Bhatia (bhatiasiddharth)
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ // -----------------------------------------------------------------------------
16
+
17
+ #pragma once
18
+
19
+ #include <cmath>
20
+
21
+ #include "CountMinSketch.hpp"
22
+
23
+ namespace MIDAS {
24
+ struct NormalCore {
25
+ int timestamp = 1;
26
+ int* const index; // Pre-compute the index to-be-modified, thanks to the same structure of CMSs
27
+ CountMinSketch numCurrent, numTotal;
28
+
29
+ NormalCore(int numRow, int numColumn):
30
+ index(new int[numRow]),
31
+ numCurrent(numRow, numColumn),
32
+ numTotal(numCurrent) { }
33
+
34
+ virtual ~NormalCore() {
35
+ delete[] index;
36
+ }
37
+
38
+ static float ComputeScore(float a, float s, float t) {
39
+ return s == 0 || t - 1 == 0 ? 0 : pow((a - s / t) * t, 2) / (s * (t - 1));
40
+ }
41
+
42
+ float operator()(int source, int destination, int timestamp) {
43
+ if (this->timestamp < timestamp) {
44
+ numCurrent.ClearAll();
45
+ this->timestamp = timestamp;
46
+ }
47
+ numCurrent.Hash(index, source, destination);
48
+ numCurrent.Add(index);
49
+ numTotal.Add(index);
50
+ return ComputeScore(numCurrent(index), numTotal(index), timestamp);
51
+ }
52
+ };
53
+ }
@@ -0,0 +1,79 @@
1
+ // -----------------------------------------------------------------------------
2
+ // Copyright 2020 Rui Liu (liurui39660) and Siddharth Bhatia (bhatiasiddharth)
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ // -----------------------------------------------------------------------------
16
+
17
+ #pragma once
18
+
19
+ #include <cmath>
20
+
21
+ #include "CountMinSketch.hpp"
22
+
23
+ namespace MIDAS {
24
+ struct RelationalCore {
25
+ int timestamp = 1;
26
+ const float factor;
27
+ int* const indexEdge; // Pre-compute the index to-be-modified, thanks to the same structure of CMSs
28
+ int* const indexSource;
29
+ int* const indexDestination;
30
+ CountMinSketch numCurrentEdge, numTotalEdge;
31
+ CountMinSketch numCurrentSource, numTotalSource;
32
+ CountMinSketch numCurrentDestination, numTotalDestination;
33
+
34
+ RelationalCore(int numRow, int numColumn, float factor = 0.5):
35
+ factor(factor),
36
+ indexEdge(new int[numRow]),
37
+ indexSource(new int[numRow]),
38
+ indexDestination(new int[numRow]),
39
+ numCurrentEdge(numRow, numColumn),
40
+ numTotalEdge(numCurrentEdge),
41
+ numCurrentSource(numRow, numColumn),
42
+ numTotalSource(numCurrentSource),
43
+ numCurrentDestination(numRow, numColumn),
44
+ numTotalDestination(numCurrentDestination) { }
45
+
46
+ virtual ~RelationalCore() {
47
+ delete[] indexEdge;
48
+ delete[] indexSource;
49
+ delete[] indexDestination;
50
+ }
51
+
52
+ static float ComputeScore(float a, float s, float t) {
53
+ return s == 0 || t - 1 == 0 ? 0 : pow((a - s / t) * t, 2) / (s * (t - 1));
54
+ }
55
+
56
+ float operator()(int source, int destination, int timestamp) {
57
+ if (this->timestamp < timestamp) {
58
+ numCurrentEdge.MultiplyAll(factor);
59
+ numCurrentSource.MultiplyAll(factor);
60
+ numCurrentDestination.MultiplyAll(factor);
61
+ this->timestamp = timestamp;
62
+ }
63
+ numCurrentEdge.Hash(indexEdge, source, destination);
64
+ numCurrentEdge.Add(indexEdge);
65
+ numTotalEdge.Add(indexEdge);
66
+ numCurrentSource.Hash(indexSource, source);
67
+ numCurrentSource.Add(indexSource);
68
+ numTotalSource.Add(indexSource);
69
+ numCurrentDestination.Hash(indexDestination, destination);
70
+ numCurrentDestination.Add(indexDestination);
71
+ numTotalDestination.Add(indexDestination);
72
+ return std::max({
73
+ ComputeScore(numCurrentEdge(indexEdge), numTotalEdge(indexEdge), timestamp),
74
+ ComputeScore(numCurrentSource(indexSource), numTotalSource(indexSource), timestamp),
75
+ ComputeScore(numCurrentDestination(indexDestination), numTotalDestination(indexDestination), timestamp),
76
+ });
77
+ }
78
+ };
79
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midas-edge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-19 00:00:00.000000000 Z
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rice
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.2'
19
+ version: 4.0.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2.2'
26
+ version: 4.0.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: numo-narray
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,64 +38,8 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rake-compiler
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: minitest
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '5'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '5'
97
- description:
98
- email: andrew@chartkick.com
41
+ description:
42
+ email: andrew@ankane.org
99
43
  executables: []
100
44
  extensions:
101
45
  - ext/midas/extconf.rb
@@ -112,19 +56,15 @@ files:
112
56
  - lib/midas/version.rb
113
57
  - vendor/MIDAS/LICENSE
114
58
  - vendor/MIDAS/README.md
115
- - vendor/MIDAS/anom.cpp
116
- - vendor/MIDAS/anom.hpp
117
- - vendor/MIDAS/argparse.hpp
118
- - vendor/MIDAS/edgehash.cpp
119
- - vendor/MIDAS/edgehash.hpp
120
- - vendor/MIDAS/main.cpp
121
- - vendor/MIDAS/nodehash.cpp
122
- - vendor/MIDAS/nodehash.hpp
59
+ - vendor/MIDAS/src/CountMinSketch.hpp
60
+ - vendor/MIDAS/src/FilteringCore.hpp
61
+ - vendor/MIDAS/src/NormalCore.hpp
62
+ - vendor/MIDAS/src/RelationalCore.hpp
123
63
  homepage: https://github.com/ankane/midas
124
64
  licenses:
125
65
  - MIT
126
66
  metadata: {}
127
- post_install_message:
67
+ post_install_message:
128
68
  rdoc_options: []
129
69
  require_paths:
130
70
  - lib
@@ -132,15 +72,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
72
  requirements:
133
73
  - - ">="
134
74
  - !ruby/object:Gem::Version
135
- version: '2.4'
75
+ version: '2.6'
136
76
  required_rubygems_version: !ruby/object:Gem::Requirement
137
77
  requirements:
138
78
  - - ">="
139
79
  - !ruby/object:Gem::Version
140
80
  version: '0'
141
81
  requirements: []
142
- rubygems_version: 3.1.2
143
- signing_key:
82
+ rubygems_version: 3.2.3
83
+ signing_key:
144
84
  specification_version: 4
145
85
  summary: Edge stream anomaly detection for Ruby
146
86
  test_files: []