midas-edge 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e4c7b4cac2e7a9dac3a6085b40d79a48b9f14dad32834e0d267f7bb667d86b9
4
- data.tar.gz: abd0836d284e7a9c34c7733f2f195e38faca2fb1ac6ab01687c3790bc96d3cfa
3
+ metadata.gz: b0d27786d0b99888ae441ad574d70f3686d206395dacef5273e473e90f35ec78
4
+ data.tar.gz: 8383b227b506effe504636f8284e67f304732c463daa1e5251236c4ddf5b3d4c
5
5
  SHA512:
6
- metadata.gz: 48c92dbdbff039a514ca99207a88276fada4a1391497a1fd4595176f90bec0ac46f0fc89126d36c7ccfc3a1afe212aa11f2142fb7d3d836734f64ab912a88ffb
7
- data.tar.gz: 9a2a140ed5d5a120969ee5fc0a599cccc6d590755c9eddf949c2e0a5897aaf39b6f8fe8137c9dde3fd418c78f04cdd8801d1353ca61009d4fbae5a610ad2edc5
6
+ metadata.gz: daa76e1ec74019adedb1ac4d187a303e549735024ac5fa1f410805b93cf2f8d8a2902ad94dcb7057f35c7a968bb595a6760b72e245a45a1dfbd0e33914c81702
7
+ data.tar.gz: f49958b7c705a72e1c8af80ac982dc58c272db360efef9632971b00a2f38ca58890728a33b1fcf6ba53bb89fe9f13227d49a34499c492c0a0b7fcd11b4ce3412
@@ -1,3 +1,7 @@
1
+ ## 0.2.1 (2020-06-17)
2
+
3
+ - Fixed installation (missing header files)
4
+
1
5
  ## 0.2.0 (2020-06-17)
2
6
 
3
7
  - Updated MIDAS to 1.0.0
@@ -1,3 +1,3 @@
1
1
  module Midas
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -0,0 +1,104 @@
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 EdgeHash {
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
+ EdgeHash() = delete;
37
+ EdgeHash& operator=(const EdgeHash& b) = delete;
38
+
39
+ EdgeHash(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
+ EdgeHash(const EdgeHash& 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
+ ~EdgeHash() {
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
+ std::for_each(data, data + lenData, [&](float& a) { a *= by; }); // Magic of vectorization
77
+ }
78
+
79
+ void Hash(int a, int b, int* indexOut) const {
80
+ for (int i = 0; i < r; i++) {
81
+ indexOut[i] = ((a + m * b) * param1[i] + param2[i]) % c;
82
+ indexOut[i] += i * c + (indexOut[i] < 0 ? c : 0);
83
+ }
84
+ }
85
+
86
+ float operator()(const int* index) const {
87
+ float least = infinity;
88
+ for (int i = 0; i < r; i++)
89
+ least = std::min(least, data[index[i]]);
90
+ return least;
91
+ }
92
+
93
+ float Assign(const int* index, float to) const {
94
+ for (int i = 0; i < r; i++)
95
+ data[index[i]] = to;
96
+ return to;
97
+ }
98
+
99
+ void Add(const int* index, float by = 1) const {
100
+ for (int i = 0; i < r; i++)
101
+ data[index[i]] += by;
102
+ }
103
+ };
104
+ }
@@ -0,0 +1,95 @@
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
+ #include <algorithm>
21
+
22
+ #include "EdgeHash.hpp"
23
+ #include "NodeHash.hpp"
24
+
25
+ namespace MIDAS {
26
+ struct FilteringCore {
27
+ const float threshold;
28
+ int timestampCurrent = 1;
29
+ const float factor;
30
+ int* const indexEdge; // Pre-compute the index to-be-modified, thanks to the same structure of CMSs
31
+ int* const indexSource;
32
+ int* const indexDestination;
33
+ EdgeHash numCurrentEdge, numTotalEdge, scoreEdge;
34
+ NodeHash numCurrentSource, numTotalSource, scoreSource;
35
+ NodeHash numCurrentDestination, numTotalDestination, scoreDestination;
36
+
37
+ FilteringCore(int numRow, int numColumn, float threshold, float factor = 0.5):
38
+ threshold(threshold),
39
+ factor(factor),
40
+ indexEdge(new int[numRow]),
41
+ indexSource(new int[numRow]),
42
+ indexDestination(new int[numRow]),
43
+ numCurrentEdge(numRow, numColumn),
44
+ numTotalEdge(numCurrentEdge),
45
+ scoreEdge(numCurrentEdge),
46
+ numCurrentSource(numRow, numColumn),
47
+ numTotalSource(numCurrentSource),
48
+ scoreSource(numCurrentSource),
49
+ numCurrentDestination(numRow, numColumn),
50
+ numTotalDestination(numCurrentDestination),
51
+ scoreDestination(numCurrentDestination) { }
52
+
53
+ virtual ~FilteringCore() {
54
+ delete[] indexEdge;
55
+ delete[] indexSource;
56
+ delete[] indexDestination;
57
+ }
58
+
59
+ static float ComputeScore(float a, float s, float t) {
60
+ 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
61
+ }
62
+
63
+ float operator()(int source, int destination, int timestamp) {
64
+ if (timestamp > timestampCurrent) {
65
+ for (int i = 0; i < numCurrentEdge.lenData; i++)
66
+ numTotalEdge.data[i] += scoreEdge.data[i] < threshold ?
67
+ numCurrentEdge.data[i] : timestampCurrent - 1 ?
68
+ numTotalEdge.data[i] / (timestampCurrent - 1) : 0;
69
+ for (int i = 0; i < numCurrentSource.lenData; i++)
70
+ numTotalSource.data[i] += scoreSource.data[i] < threshold ?
71
+ numCurrentSource.data[i] : timestampCurrent - 1 ?
72
+ numTotalSource.data[i] / (timestampCurrent - 1) : 0;
73
+ for (int i = 0; i < numCurrentDestination.lenData; i++)
74
+ numTotalDestination.data[i] += scoreDestination.data[i] < threshold ?
75
+ numCurrentDestination.data[i] : timestampCurrent - 1 ?
76
+ numTotalDestination.data[i] / (timestampCurrent - 1) : 0;
77
+ numCurrentEdge.MultiplyAll(factor);
78
+ numCurrentSource.MultiplyAll(factor);
79
+ numCurrentDestination.MultiplyAll(factor);
80
+ timestampCurrent = timestamp;
81
+ }
82
+ numCurrentEdge.Hash(source, destination, indexEdge);
83
+ numCurrentEdge.Add(indexEdge);
84
+ numCurrentSource.Hash(source, indexSource);
85
+ numCurrentSource.Add(indexSource);
86
+ numCurrentDestination.Hash(destination, indexDestination);
87
+ numCurrentDestination.Add(indexDestination);
88
+ return std::max({
89
+ scoreEdge.Assign(indexEdge, ComputeScore(numCurrentEdge(indexEdge), numTotalEdge(indexEdge), timestamp)),
90
+ scoreSource.Assign(indexSource, ComputeScore(numCurrentSource(indexSource), numTotalSource(indexSource), timestamp)),
91
+ scoreDestination.Assign(indexDestination, ComputeScore(numCurrentDestination(indexDestination), numTotalDestination(indexDestination), timestamp)),
92
+ });
93
+ }
94
+ };
95
+ }
@@ -0,0 +1,104 @@
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 NodeHash {
23
+ // Fields
24
+ // --------------------------------------------------------------------------------
25
+
26
+ const int r, c;
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
+ NodeHash() = delete;
37
+ NodeHash& operator=(const NodeHash& b) = delete;
38
+
39
+ NodeHash(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
+ NodeHash(const NodeHash& 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
+ ~NodeHash() {
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
+ std::for_each(data, data + lenData, [&](float& a) { a *= by; }); // Magic of vectorization
77
+ }
78
+
79
+ void Hash(int a, int* indexOut) const {
80
+ for (int i = 0; i < r; i++) {
81
+ indexOut[i] = (a * param1[i] + param2[i]) % c;
82
+ indexOut[i] += i * c + (indexOut[i] < 0 ? c : 0);
83
+ }
84
+ }
85
+
86
+ float operator()(const int* index) const {
87
+ float least = infinity;
88
+ for (int i = 0; i < r; i++)
89
+ least = std::min(least, data[index[i]]);
90
+ return least;
91
+ }
92
+
93
+ float Assign(const int* index, float to) const {
94
+ for (int i = 0; i < r; i++)
95
+ data[index[i]] = to;
96
+ return to;
97
+ }
98
+
99
+ void Add(const int* index, float by = 1) const {
100
+ for (int i = 0; i < r; i++)
101
+ data[index[i]] += by;
102
+ }
103
+ };
104
+ }
@@ -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 "EdgeHash.hpp"
22
+
23
+ namespace MIDAS {
24
+ struct NormalCore {
25
+ int timestampCurrent = 1;
26
+ int* const index; // Pre-compute the index to-be-modified, thanks to the same structure of CMSs
27
+ EdgeHash 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 (timestamp > timestampCurrent) {
44
+ numCurrent.ClearAll();
45
+ timestampCurrent = timestamp;
46
+ }
47
+ numCurrent.Hash(source, destination, index);
48
+ numCurrent.Add(index);
49
+ numTotal.Add(index);
50
+ return ComputeScore(numCurrent(index), numTotal(index), timestamp);
51
+ }
52
+ };
53
+ }
@@ -0,0 +1,81 @@
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
+ #include <algorithm>
21
+
22
+ #include "EdgeHash.hpp"
23
+ #include "NodeHash.hpp"
24
+
25
+ namespace MIDAS {
26
+ struct RelationalCore {
27
+ int timestampCurrent = 1;
28
+ const float factor;
29
+ int* const indexEdge; // Pre-compute the index to-be-modified, thanks to the same structure of CMSs
30
+ int* const indexSource;
31
+ int* const indexDestination;
32
+ EdgeHash numCurrentEdge, numTotalEdge;
33
+ NodeHash numCurrentSource, numTotalSource;
34
+ NodeHash numCurrentDestination, numTotalDestination;
35
+
36
+ RelationalCore(int numRow, int numColumn, float factor = 0.5):
37
+ factor(factor),
38
+ indexEdge(new int[numRow]),
39
+ indexSource(new int[numRow]),
40
+ indexDestination(new int[numRow]),
41
+ numCurrentEdge(numRow, numColumn),
42
+ numTotalEdge(numCurrentEdge),
43
+ numCurrentSource(numRow, numColumn),
44
+ numTotalSource(numCurrentSource),
45
+ numCurrentDestination(numRow, numColumn),
46
+ numTotalDestination(numCurrentDestination) { }
47
+
48
+ virtual ~RelationalCore() {
49
+ delete[] indexEdge;
50
+ delete[] indexSource;
51
+ delete[] indexDestination;
52
+ }
53
+
54
+ static float ComputeScore(float a, float s, float t) {
55
+ return s == 0 || t - 1 == 0 ? 0 : pow((a - s / t) * t, 2) / (s * (t - 1));
56
+ }
57
+
58
+ float operator()(int source, int destination, int timestamp) {
59
+ if (timestamp > timestampCurrent) {
60
+ numCurrentEdge.MultiplyAll(factor);
61
+ numCurrentSource.MultiplyAll(factor);
62
+ numCurrentDestination.MultiplyAll(factor);
63
+ timestampCurrent = timestamp;
64
+ }
65
+ numCurrentEdge.Hash(source, destination, indexEdge);
66
+ numCurrentEdge.Add(indexEdge);
67
+ numTotalEdge.Add(indexEdge);
68
+ numCurrentSource.Hash(source, indexSource);
69
+ numCurrentSource.Add(indexSource);
70
+ numTotalSource.Add(indexSource);
71
+ numCurrentDestination.Hash(destination, indexDestination);
72
+ numCurrentDestination.Add(indexDestination);
73
+ numTotalDestination.Add(indexDestination);
74
+ return std::max({
75
+ ComputeScore(numCurrentEdge(indexEdge), numTotalEdge(indexEdge), timestamp),
76
+ ComputeScore(numCurrentSource(indexSource), numTotalSource(indexSource), timestamp),
77
+ ComputeScore(numCurrentDestination(indexDestination), numTotalDestination(indexDestination), timestamp),
78
+ });
79
+ }
80
+ };
81
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midas-edge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -112,6 +112,11 @@ files:
112
112
  - lib/midas/version.rb
113
113
  - vendor/MIDAS/LICENSE
114
114
  - vendor/MIDAS/README.md
115
+ - vendor/MIDAS/src/EdgeHash.hpp
116
+ - vendor/MIDAS/src/FilteringCore.hpp
117
+ - vendor/MIDAS/src/NodeHash.hpp
118
+ - vendor/MIDAS/src/NormalCore.hpp
119
+ - vendor/MIDAS/src/RelationalCore.hpp
115
120
  homepage: https://github.com/ankane/midas
116
121
  licenses:
117
122
  - MIT