rgraphum 0.0.1.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +26 -0
- data/GLOSSARIES.md +108 -0
- data/GREMLIN.md +1398 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +136 -0
- data/Rakefile +16 -0
- data/bin/.irbrc +41 -0
- data/bin/rgraphum_console +61 -0
- data/bin/rgraphum_runner +57 -0
- data/examples/ba_model/make.rb +19 -0
- data/examples/ba_model/make_dummy_twitter_rt_data.rb +0 -0
- data/examples/basic/check_modularity.rb +27 -0
- data/examples/basic/make_graph.rb +12 -0
- data/examples/parser/dot.rb +28 -0
- data/examples/sis_model/lifegame.rb +161 -0
- data/graph_struct.jpg +0 -0
- data/lib/rgraphum/analyzer/linear_regression.rb +31 -0
- data/lib/rgraphum/analyzer/meme_tracker.rb +296 -0
- data/lib/rgraphum/analyzer/twitter/rt_at_mark.rb +45 -0
- data/lib/rgraphum/analyzer.rb +8 -0
- data/lib/rgraphum/cluster.rb +67 -0
- data/lib/rgraphum/communities.rb +65 -0
- data/lib/rgraphum/community.rb +86 -0
- data/lib/rgraphum/cosine_similarity_matrix.rb +40 -0
- data/lib/rgraphum/edge.rb +194 -0
- data/lib/rgraphum/edges.rb +161 -0
- data/lib/rgraphum/ext/cosine_similarity_matrix.rb +79 -0
- data/lib/rgraphum/ext/linear_regression.rb +22 -0
- data/lib/rgraphum/ext/tf_idf.rb +52 -0
- data/lib/rgraphum/graph/gremlin.rb +193 -0
- data/lib/rgraphum/graph/math/clustering_coefficient.rb +53 -0
- data/lib/rgraphum/graph/math/community_detection.rb +141 -0
- data/lib/rgraphum/graph/math/degree_distribution.rb +50 -0
- data/lib/rgraphum/graph/math/dijkstra.rb +331 -0
- data/lib/rgraphum/graph/math.rb +45 -0
- data/lib/rgraphum/graph.rb +267 -0
- data/lib/rgraphum/importer.rb +97 -0
- data/lib/rgraphum/marshal.rb +26 -0
- data/lib/rgraphum/motifs.rb +8 -0
- data/lib/rgraphum/parsers/flare.rb +42 -0
- data/lib/rgraphum/parsers/gephi.rb +193 -0
- data/lib/rgraphum/parsers/graphviz.rb +78 -0
- data/lib/rgraphum/parsers/miserables.rb +54 -0
- data/lib/rgraphum/parsers.rb +32 -0
- data/lib/rgraphum/path.rb +37 -0
- data/lib/rgraphum/query.rb +130 -0
- data/lib/rgraphum/rgraphum_array.rb +159 -0
- data/lib/rgraphum/rgraphum_array_dividers.rb +43 -0
- data/lib/rgraphum/rgraphum_random.rb +5 -0
- data/lib/rgraphum/simulator/ba_model.rb +140 -0
- data/lib/rgraphum/simulator/sir_model.rb +178 -0
- data/lib/rgraphum/simulator/sis_model.rb +158 -0
- data/lib/rgraphum/simulator.rb +29 -0
- data/lib/rgraphum/statistic/power_law.rb +9 -0
- data/lib/rgraphum/t.rb +12 -0
- data/lib/rgraphum/tf_idf.rb +27 -0
- data/lib/rgraphum/version.rb +3 -0
- data/lib/rgraphum/vertex.rb +354 -0
- data/lib/rgraphum/vertices.rb +97 -0
- data/lib/rgraphum.rb +38 -0
- data/performance/add-vertices-edges.rb +20 -0
- data/performance/add-vertices.rb +12 -0
- data/performance/build-graph.rb +19 -0
- data/performance/delete-graph.rb +24 -0
- data/performance/delete-vertices.rb +25 -0
- data/performance/refer-graph.rb +23 -0
- data/rgraphum.gemspec +30 -0
- data/test/lib/rgraphum/analyzer/linear_regression_test.rb +20 -0
- data/test/lib/rgraphum/analyzer/meme_tracker_test.rb +383 -0
- data/test/lib/rgraphum/analyzer/twitter/rt_at_mark_test.rb +120 -0
- data/test/lib/rgraphum/array_test.rb +95 -0
- data/test/lib/rgraphum/bubble_test.rb +7 -0
- data/test/lib/rgraphum/communities_test.rb +53 -0
- data/test/lib/rgraphum/cosine_similarity_test.rb +18 -0
- data/test/lib/rgraphum/edge_test.rb +89 -0
- data/test/lib/rgraphum/edges_test.rb +178 -0
- data/test/lib/rgraphum/graph_builder_test.rb +64 -0
- data/test/lib/rgraphum/graph_dup_test.rb +199 -0
- data/test/lib/rgraphum/graph_plus_test.rb +80 -0
- data/test/lib/rgraphum/graph_test.rb +512 -0
- data/test/lib/rgraphum/gremlin_test.rb +145 -0
- data/test/lib/rgraphum/importers/idg_json_edges.json +20 -0
- data/test/lib/rgraphum/importers/idg_json_test.rb +207 -0
- data/test/lib/rgraphum/importers/idg_json_vertices.json +46 -0
- data/test/lib/rgraphum/math/average_distance_matrix_test.rb +142 -0
- data/test/lib/rgraphum/math/clustering_coefficient_test.rb +219 -0
- data/test/lib/rgraphum/math/community_test.rb +78 -0
- data/test/lib/rgraphum/math/degree_distribution_test.rb +40 -0
- data/test/lib/rgraphum/math/dijkstra_test.rb +146 -0
- data/test/lib/rgraphum/math/modularity_test.rb +154 -0
- data/test/lib/rgraphum/math/quick_average_distance_matrix_test.rb +84 -0
- data/test/lib/rgraphum/path_test.rb +44 -0
- data/test/lib/rgraphum/query/enumerable_test.rb +42 -0
- data/test/lib/rgraphum/query/where_operators_test.rb +75 -0
- data/test/lib/rgraphum/query/where_test.rb +59 -0
- data/test/lib/rgraphum/simulator/ba_model_test.rb +75 -0
- data/test/lib/rgraphum/simulator/sir_model_test.rb +513 -0
- data/test/lib/rgraphum/simulator/sis_model_test.rb +478 -0
- data/test/lib/rgraphum/simulator_test.rb +22 -0
- data/test/lib/rgraphum/tf_idf_test.rb +30 -0
- data/test/lib/rgraphum/vertex_test.rb +50 -0
- data/test/lib/rgraphum/vertices_test.rb +180 -0
- data/test/test_helper.rb +98 -0
- data/tmp/.gitkeep +0 -0
- metadata +254 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
require 'rgraphum'
|
|
5
|
+
|
|
6
|
+
class SISModelTest < MiniTest::Test
|
|
7
|
+
def setup
|
|
8
|
+
# A -- B -- C
|
|
9
|
+
# | | |
|
|
10
|
+
# D -- E -- F
|
|
11
|
+
# | | |
|
|
12
|
+
# G -- H -- I
|
|
13
|
+
v = {}
|
|
14
|
+
@graph = Rgraphum::Graph.new
|
|
15
|
+
v[:a] = @graph.vertices.build(label: "A")
|
|
16
|
+
v[:b] = @graph.vertices.build(label: "B")
|
|
17
|
+
v[:c] = @graph.vertices.build(label: "C")
|
|
18
|
+
v[:d] = @graph.vertices.build(label: "D")
|
|
19
|
+
v[:e] = @graph.vertices.build(label: "E")
|
|
20
|
+
v[:f] = @graph.vertices.build(label: "F")
|
|
21
|
+
v[:g] = @graph.vertices.build(label: "G")
|
|
22
|
+
v[:h] = @graph.vertices.build(label: "H")
|
|
23
|
+
v[:i] = @graph.vertices.build(label: "I")
|
|
24
|
+
|
|
25
|
+
@graph.edges.build(source: v[:a], target: v[:b])
|
|
26
|
+
@graph.edges.build(source: v[:b], target: v[:c])
|
|
27
|
+
@graph.edges.build(source: v[:a], target: v[:d])
|
|
28
|
+
@graph.edges.build(source: v[:b], target: v[:e])
|
|
29
|
+
@graph.edges.build(source: v[:c], target: v[:f])
|
|
30
|
+
@graph.edges.build(source: v[:d], target: v[:e])
|
|
31
|
+
@graph.edges.build(source: v[:e], target: v[:f])
|
|
32
|
+
@graph.edges.build(source: v[:d], target: v[:g])
|
|
33
|
+
@graph.edges.build(source: v[:e], target: v[:h])
|
|
34
|
+
@graph.edges.build(source: v[:f], target: v[:i])
|
|
35
|
+
@graph.edges.build(source: v[:g], target: v[:h])
|
|
36
|
+
@graph.edges.build(source: v[:h], target: v[:i])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_initial_si_map
|
|
40
|
+
sis = Rgraphum::Simulator::SISModel.new(graph: @graph)
|
|
41
|
+
|
|
42
|
+
assert_equal %w(A B C D E F G H I), sis.vertices.map(&:label)
|
|
43
|
+
assert_equal %w(s s s s s s s s s).map(&:to_sym), sis.si_map
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_set_si_map
|
|
47
|
+
si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
48
|
+
sis = Rgraphum::Simulator::SISModel.new(graph: @graph, si_map: si_map)
|
|
49
|
+
|
|
50
|
+
assert_equal %w(A B C D E F G H I), sis.vertices.map(&:label)
|
|
51
|
+
assert_equal %w(s i s i s i s i s).map(&:to_sym), sis.si_map
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_infection_rate
|
|
55
|
+
sis = Rgraphum::Simulator::SISModel.new(graph: @graph, infection_rate: 0.5)
|
|
56
|
+
assert_equal 0.5, sis.infection_rate
|
|
57
|
+
|
|
58
|
+
assert_raises(ArgumentError) do
|
|
59
|
+
Rgraphum::Simulator::SISModel.new(graph: @graph, infection_rate: -0.1)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
assert_raises(ArgumentError) do
|
|
63
|
+
Rgraphum::Simulator::SISModel.new(graph: @graph, infection_rate: 1.1)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_recovery_rate
|
|
68
|
+
sis = Rgraphum::Simulator::SISModel.new(graph: @graph, recovery_rate: 0.5)
|
|
69
|
+
assert_equal 0.5, sis.recovery_rate
|
|
70
|
+
|
|
71
|
+
assert_raises(ArgumentError) do
|
|
72
|
+
Rgraphum::Simulator::SISModel.new(graph: @graph, recovery_rate: -0.1)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
assert_raises(ArgumentError) do
|
|
76
|
+
Rgraphum::Simulator::SISModel.new(graph: @graph, recovery_rate: 1.1)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_zero_periods_simulate
|
|
81
|
+
initial_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
82
|
+
sis = Rgraphum::Simulator::SISModel.new(graph: @graph, si_map: initial_si_map)
|
|
83
|
+
|
|
84
|
+
sis.simulate periods: 0
|
|
85
|
+
|
|
86
|
+
expected_si_map = initial_si_map
|
|
87
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_infected_1?
|
|
91
|
+
# A -- B -- C S -- I -- S
|
|
92
|
+
# | | | | | |
|
|
93
|
+
# D -- E -- F I -- S -- I
|
|
94
|
+
# | | | | | |
|
|
95
|
+
# G -- H -- I S -- I -- S
|
|
96
|
+
options = {
|
|
97
|
+
graph: @graph,
|
|
98
|
+
si_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
99
|
+
infection_rate: 1.0,
|
|
100
|
+
recovery_rate: 0.0,
|
|
101
|
+
}
|
|
102
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
103
|
+
|
|
104
|
+
v1 = sis.vertices[0]
|
|
105
|
+
assert_equal false, sis.infected?(v1)
|
|
106
|
+
assert_equal false, sis.infected?(v1, 0)
|
|
107
|
+
assert_equal true, sis.infected?(v1, 1)
|
|
108
|
+
|
|
109
|
+
v2 = sis.vertices[1]
|
|
110
|
+
assert_equal true, sis.infected?(v2)
|
|
111
|
+
assert_equal true, sis.infected?(v2, 0)
|
|
112
|
+
assert_equal true, sis.infected?(v2, 1)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def test_infected_2?
|
|
116
|
+
# A -- B -- C S -- I -- S
|
|
117
|
+
# | | | | | |
|
|
118
|
+
# D -- E -- F I -- S -- I
|
|
119
|
+
# | | | | | |
|
|
120
|
+
# G -- H -- I S -- I -- S
|
|
121
|
+
options = {
|
|
122
|
+
graph: @graph,
|
|
123
|
+
si_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
124
|
+
infection_rate: 0.1,
|
|
125
|
+
recovery_rate: 0.0,
|
|
126
|
+
t_per_period: 0.5,
|
|
127
|
+
}
|
|
128
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
129
|
+
|
|
130
|
+
v1 = sis.vertices[0]
|
|
131
|
+
assert_equal false, sis.infected?(v1, 0)
|
|
132
|
+
assert_equal false, sis.infected?(v1, 1)
|
|
133
|
+
assert_equal false, sis.infected?(v1, 2)
|
|
134
|
+
assert_equal false, sis.infected?(v1, 3)
|
|
135
|
+
assert_equal false, sis.infected?(v1, 9)
|
|
136
|
+
assert_equal true, sis.infected?(v1, 10)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def test_susceptible?
|
|
140
|
+
# A -- B -- C I -- I -- I
|
|
141
|
+
# | | | | | |
|
|
142
|
+
# D -- E -- F I -- I -- I
|
|
143
|
+
# | | | | | |
|
|
144
|
+
# G -- H -- I I -- I -- I
|
|
145
|
+
options = {
|
|
146
|
+
graph: @graph,
|
|
147
|
+
si_map: [:i, :i, :i, :i, :i, :i, :i, :i, :i],
|
|
148
|
+
infection_rate: 0.0,
|
|
149
|
+
recovery_rate: 1.0,
|
|
150
|
+
}
|
|
151
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
152
|
+
|
|
153
|
+
v1 = sis.vertices[0]
|
|
154
|
+
assert_equal false, sis.susceptible?(v1)
|
|
155
|
+
assert_equal false, sis.susceptible?(v1, 0)
|
|
156
|
+
assert_equal true, sis.susceptible?(v1, 1)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def test_next_period_only_with_infection_rate
|
|
160
|
+
# A -- B -- C S -- I -- S
|
|
161
|
+
# | | | | | |
|
|
162
|
+
# D -- E -- F I -- S -- I
|
|
163
|
+
# | | | | | |
|
|
164
|
+
# G -- H -- I S -- I -- S
|
|
165
|
+
options = {
|
|
166
|
+
graph: @graph,
|
|
167
|
+
si_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
168
|
+
infection_rate: 0.2,
|
|
169
|
+
recovery_rate: 0.0,
|
|
170
|
+
t_per_period: 1,
|
|
171
|
+
}
|
|
172
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
173
|
+
|
|
174
|
+
# initial state
|
|
175
|
+
# A -- B -- C S -- I -- S 0 I 0
|
|
176
|
+
# | | | | | |
|
|
177
|
+
# D -- E -- F I -- S -- I I 0 I
|
|
178
|
+
# | | | | | |
|
|
179
|
+
# G -- H -- I S -- I -- S 0 I 0
|
|
180
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
181
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
182
|
+
|
|
183
|
+
# 1st period
|
|
184
|
+
# A -- B -- C S -- I -- S 0.4 I 0.4
|
|
185
|
+
# | | | | | |
|
|
186
|
+
# D -- E -- F I -- S -- I I 0.8 I
|
|
187
|
+
# | | | | | |
|
|
188
|
+
# G -- H -- I S -- I -- S 0.4 I 0.4
|
|
189
|
+
sis.next_period
|
|
190
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
191
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
192
|
+
|
|
193
|
+
# 2nd period
|
|
194
|
+
# A -- B -- C S -- I -- S 0.8 I 0.8
|
|
195
|
+
# | | | | | |
|
|
196
|
+
# D -- E -- F I -- S -- I I I I
|
|
197
|
+
# | | | | | |
|
|
198
|
+
# G -- H -- I S -- I -- S 0.8 I 0.8
|
|
199
|
+
sis.next_period
|
|
200
|
+
expected_si_map = [:s, :i, :s, :i, :i, :i, :s, :i, :s]
|
|
201
|
+
assert_equal expected_si_map, sis.si_map, "si_map should change"
|
|
202
|
+
|
|
203
|
+
# 3rd period
|
|
204
|
+
# A -- B -- C S -- I -- S I I I
|
|
205
|
+
# | | | | | |
|
|
206
|
+
# D -- E -- F I -- S -- I I I I
|
|
207
|
+
# | | | | | |
|
|
208
|
+
# G -- H -- I S -- I -- S I I I
|
|
209
|
+
sis.next_period
|
|
210
|
+
expected_si_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
|
|
211
|
+
assert_equal expected_si_map, sis.si_map, "si_map should only contain I"
|
|
212
|
+
|
|
213
|
+
# 4th period
|
|
214
|
+
# A -- B -- C S -- I -- S I I I
|
|
215
|
+
# | | | | | |
|
|
216
|
+
# D -- E -- F I -- S -- I I I I
|
|
217
|
+
# | | | | | |
|
|
218
|
+
# G -- H -- I S -- I -- S I I I
|
|
219
|
+
sis.next_period
|
|
220
|
+
expected_si_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
|
|
221
|
+
assert_equal expected_si_map, sis.si_map, "si_map should only contain I"
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def test_next_period_only_with_recovery_rate
|
|
225
|
+
# A -- B -- C S -- I -- S
|
|
226
|
+
# | | | | | |
|
|
227
|
+
# D -- E -- F I -- S -- I
|
|
228
|
+
# | | | | | |
|
|
229
|
+
# G -- H -- I S -- I -- S
|
|
230
|
+
options = {
|
|
231
|
+
graph: @graph,
|
|
232
|
+
si_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
233
|
+
infection_rate: 0,
|
|
234
|
+
recovery_rate: 0.3,
|
|
235
|
+
t_per_period: 1,
|
|
236
|
+
}
|
|
237
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
238
|
+
|
|
239
|
+
# initial state
|
|
240
|
+
# A -- B -- C S -- I -- S S 0 S
|
|
241
|
+
# | | | | | |
|
|
242
|
+
# D -- E -- F I -- S -- I 0 S 0
|
|
243
|
+
# | | | | | |
|
|
244
|
+
# G -- H -- I S -- I -- S S 0 S
|
|
245
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
246
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
247
|
+
|
|
248
|
+
# 1st period
|
|
249
|
+
# A -- B -- C S -- I -- S S 0.3 S
|
|
250
|
+
# | | | | | |
|
|
251
|
+
# D -- E -- F I -- S -- I 0.3 S 0.3
|
|
252
|
+
# | | | | | |
|
|
253
|
+
# G -- H -- I S -- I -- S S 0.3 S
|
|
254
|
+
sis.next_period
|
|
255
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
256
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
257
|
+
|
|
258
|
+
# 2nd period
|
|
259
|
+
# A -- B -- C S -- I -- S S 0.6 S
|
|
260
|
+
# | | | | | |
|
|
261
|
+
# D -- E -- F I -- S -- I 0.6 S 0.6
|
|
262
|
+
# | | | | | |
|
|
263
|
+
# G -- H -- I S -- I -- S S 0.6 S
|
|
264
|
+
sis.next_period
|
|
265
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
266
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
267
|
+
|
|
268
|
+
# 3rd period
|
|
269
|
+
# A -- B -- C S -- I -- S S 0.9 S
|
|
270
|
+
# | | | | | |
|
|
271
|
+
# D -- E -- F I -- S -- I 0.9 S 0.9
|
|
272
|
+
# | | | | | |
|
|
273
|
+
# G -- H -- I S -- I -- S S 0.9 S
|
|
274
|
+
sis.next_period
|
|
275
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
276
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
277
|
+
|
|
278
|
+
# 4th period
|
|
279
|
+
# A -- B -- C S -- I -- S S S S
|
|
280
|
+
# | | | | | |
|
|
281
|
+
# D -- E -- F I -- S -- I S S S
|
|
282
|
+
# | | | | | |
|
|
283
|
+
# G -- H -- I S -- I -- S S S S
|
|
284
|
+
sis.next_period
|
|
285
|
+
expected_si_map = [:s, :s, :s, :s, :s, :s, :s, :s, :s]
|
|
286
|
+
assert_equal expected_si_map, sis.si_map, "si_map should only contain S"
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def test_next_period_1
|
|
290
|
+
# A -- B -- C I -- I -- S
|
|
291
|
+
# | | | | | |
|
|
292
|
+
# D -- E -- F I -- S -- I
|
|
293
|
+
# | | | | | |
|
|
294
|
+
# G -- H -- I S -- I -- S
|
|
295
|
+
options = {
|
|
296
|
+
graph: @graph,
|
|
297
|
+
si_map: [:i, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
298
|
+
infection_rate: 0.2, # S -> I
|
|
299
|
+
recovery_rate: 0.3, # I -> S
|
|
300
|
+
t_per_period: 1,
|
|
301
|
+
}
|
|
302
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
303
|
+
|
|
304
|
+
# initial state
|
|
305
|
+
# A -- B -- C I -- I -- S I0 I0 S0
|
|
306
|
+
# | | | | | |
|
|
307
|
+
# D -- E -- F I -- S -- I I0 S0 I0
|
|
308
|
+
# | | | | | |
|
|
309
|
+
# G -- H -- I S -- I -- S S0 I0 S0
|
|
310
|
+
expected_si_map = [:i, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
311
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
312
|
+
|
|
313
|
+
# 1st period
|
|
314
|
+
# A -- B -- C I -- I -- S I0.3 I0.3 S0.4
|
|
315
|
+
# | | | | | |
|
|
316
|
+
# D -- E -- F I -- S -- I I0.3 S0.8 I0.3
|
|
317
|
+
# | | | | | |
|
|
318
|
+
# G -- H -- I S -- I -- S S0.4 I0.3 S0.4
|
|
319
|
+
sis.next_period
|
|
320
|
+
expected_si_map = [:i, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
321
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
322
|
+
|
|
323
|
+
# 2nd period
|
|
324
|
+
# A -- B -- C I -- I -- S I0.6 I0.6 S0.8
|
|
325
|
+
# | | | | | |
|
|
326
|
+
# D -- E -- F I -- I -- I I0.6 I0 I0.6
|
|
327
|
+
# | | | | | |
|
|
328
|
+
# G -- H -- I S -- I -- S S0.8 I0.6 S0.8
|
|
329
|
+
sis.next_period
|
|
330
|
+
expected_si_map = [:i, :i, :s, :i, :i, :i, :s, :i, :s]
|
|
331
|
+
assert_equal expected_si_map, sis.si_map
|
|
332
|
+
|
|
333
|
+
# 3rd period
|
|
334
|
+
# A -- B -- C I -- I -- I I0.9 I0.9 I0
|
|
335
|
+
# | | | | | |
|
|
336
|
+
# D -- E -- F I -- I -- I I0.9 I0.3 I0.9
|
|
337
|
+
# | | | | | |
|
|
338
|
+
# G -- H -- I I -- I -- I I0 I0.9 I0
|
|
339
|
+
sis.next_period
|
|
340
|
+
expected_si_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
|
|
341
|
+
assert_equal expected_si_map, sis.si_map
|
|
342
|
+
|
|
343
|
+
# 4th period
|
|
344
|
+
# A -- B -- C S -- S -- I S0 S0 I0.3
|
|
345
|
+
# | | | | | |
|
|
346
|
+
# D -- E -- F S -- I -- S S0 I0.6 S0
|
|
347
|
+
# | | | | | |
|
|
348
|
+
# G -- H -- I I -- S -- I I0.3 S0 I0.3
|
|
349
|
+
sis.next_period
|
|
350
|
+
expected_si_map = [:s, :s, :i, :s, :i, :s, :i, :s, :i]
|
|
351
|
+
assert_equal expected_si_map, sis.si_map
|
|
352
|
+
|
|
353
|
+
# 5th period
|
|
354
|
+
# A -- B -- C S -- S -- S S0 S0.4 I0.6
|
|
355
|
+
# | | | | | |
|
|
356
|
+
# D -- E -- F S -- I -- S S0.4 I0.9 S0.6
|
|
357
|
+
# | | | | | |
|
|
358
|
+
# G -- H -- I I -- S -- I I0.6 S0.6 I0.6
|
|
359
|
+
sis.next_period
|
|
360
|
+
expected_si_map = [:s, :s, :i, :s, :i, :s, :i, :s, :i]
|
|
361
|
+
assert_equal expected_si_map, sis.si_map
|
|
362
|
+
|
|
363
|
+
# 6th period
|
|
364
|
+
# A -- B -- C S -- S -- I S0 S0.8 I0.9
|
|
365
|
+
# | | | | | |
|
|
366
|
+
# D -- E -- F S -- S -- I S0.8 S0 I0
|
|
367
|
+
# | | | | | |
|
|
368
|
+
# G -- H -- I I -- I -- I I0.9 I0 I0.9
|
|
369
|
+
sis.next_period
|
|
370
|
+
expected_si_map = [:s, :s, :i, :s, :s, :i, :i, :i, :i]
|
|
371
|
+
assert_equal expected_si_map, sis.si_map
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def test_simulate_only_with_infection_rate
|
|
375
|
+
# A -- B -- C S -- I -- S
|
|
376
|
+
# | | | | | |
|
|
377
|
+
# D -- E -- F I -- S -- I
|
|
378
|
+
# | | | | | |
|
|
379
|
+
# G -- H -- I S -- I -- S
|
|
380
|
+
options = {
|
|
381
|
+
graph: @graph,
|
|
382
|
+
si_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
383
|
+
infection_rate: 0.2,
|
|
384
|
+
recovery_rate: 0.0,
|
|
385
|
+
t_per_period: 1,
|
|
386
|
+
}
|
|
387
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
388
|
+
|
|
389
|
+
# initial state
|
|
390
|
+
# A -- B -- C S -- I -- S 0.4 I 0.4
|
|
391
|
+
# | | | | | |
|
|
392
|
+
# D -- E -- F I -- S -- I I 0.8 I
|
|
393
|
+
# | | | | | |
|
|
394
|
+
# G -- H -- I S -- I -- S 0.4 I 0.4
|
|
395
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
396
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
397
|
+
|
|
398
|
+
# 3rd period
|
|
399
|
+
# A -- B -- C S -- I -- S I I I
|
|
400
|
+
# | | | | | |
|
|
401
|
+
# D -- E -- F I -- S -- I I I I
|
|
402
|
+
# | | | | | |
|
|
403
|
+
# G -- H -- I S -- I -- S I I I
|
|
404
|
+
sis.simulate periods: 3
|
|
405
|
+
expected_si_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
|
|
406
|
+
assert_equal expected_si_map, sis.si_map, "si_map should only contain I"
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
def test_simulate_only_with_recovery_rate
|
|
410
|
+
# A -- B -- C S -- I -- S
|
|
411
|
+
# | | | | | |
|
|
412
|
+
# D -- E -- F I -- S -- I
|
|
413
|
+
# | | | | | |
|
|
414
|
+
# G -- H -- I S -- I -- S
|
|
415
|
+
options = {
|
|
416
|
+
graph: @graph,
|
|
417
|
+
si_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
418
|
+
infection_rate: 0,
|
|
419
|
+
recovery_rate: 0.3,
|
|
420
|
+
t_per_period: 1,
|
|
421
|
+
}
|
|
422
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
423
|
+
|
|
424
|
+
# initial state
|
|
425
|
+
# A -- B -- C S -- I -- S S 0 S
|
|
426
|
+
# | | | | | |
|
|
427
|
+
# D -- E -- F I -- S -- I 0 S 0
|
|
428
|
+
# | | | | | |
|
|
429
|
+
# G -- H -- I S -- I -- S S 0 S
|
|
430
|
+
expected_si_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
431
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
432
|
+
|
|
433
|
+
# 4th period
|
|
434
|
+
# A -- B -- C S -- I -- S S S S
|
|
435
|
+
# | | | | | |
|
|
436
|
+
# D -- E -- F I -- S -- I S S S
|
|
437
|
+
# | | | | | |
|
|
438
|
+
# G -- H -- I S -- I -- S S S S
|
|
439
|
+
sis.simulate periods: 4
|
|
440
|
+
expected_si_map = [:s, :s, :s, :s, :s, :s, :s, :s, :s]
|
|
441
|
+
assert_equal expected_si_map, sis.si_map, "si_map should only contain S"
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def test_simulate
|
|
445
|
+
# A -- B -- C I -- I -- S
|
|
446
|
+
# | | | | | |
|
|
447
|
+
# D -- E -- F I -- S -- I
|
|
448
|
+
# | | | | | |
|
|
449
|
+
# G -- H -- I S -- I -- S
|
|
450
|
+
options = {
|
|
451
|
+
graph: @graph,
|
|
452
|
+
si_map: [:i, :i, :s, :i, :s, :i, :s, :i, :s],
|
|
453
|
+
infection_rate: 0.2, # S -> I
|
|
454
|
+
recovery_rate: 0.3, # I -> S
|
|
455
|
+
t_per_period: 1,
|
|
456
|
+
}
|
|
457
|
+
sis = Rgraphum::Simulator::SISModel.new(options)
|
|
458
|
+
|
|
459
|
+
# initial state
|
|
460
|
+
# A -- B -- C I -- I -- S I0 I0 S0
|
|
461
|
+
# | | | | | |
|
|
462
|
+
# D -- E -- F I -- S -- I I0 S0 I0
|
|
463
|
+
# | | | | | |
|
|
464
|
+
# G -- H -- I S -- I -- S S0 I0 S0
|
|
465
|
+
expected_si_map = [:i, :i, :s, :i, :s, :i, :s, :i, :s]
|
|
466
|
+
assert_equal expected_si_map, sis.si_map, "si_map should not change"
|
|
467
|
+
|
|
468
|
+
# 6th period
|
|
469
|
+
# A -- B -- C S -- S -- I S0 S0.8 I0.9
|
|
470
|
+
# | | | | | |
|
|
471
|
+
# D -- E -- F S -- S -- I S0.8 S0 I0
|
|
472
|
+
# | | | | | |
|
|
473
|
+
# G -- H -- I I -- I -- I I0.9 I0 I0.9
|
|
474
|
+
sis.simulate periods: 6
|
|
475
|
+
expected_si_map = [:s, :s, :i, :s, :s, :i, :i, :i, :i]
|
|
476
|
+
assert_equal expected_si_map, sis.si_map
|
|
477
|
+
end
|
|
478
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
require 'rgraphum'
|
|
5
|
+
|
|
6
|
+
class RgraphumSimulatorTest < MiniTest::Test
|
|
7
|
+
include Rgraphum::Simulator
|
|
8
|
+
|
|
9
|
+
def setup
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_guess_model_class
|
|
13
|
+
assert_equal Rgraphum::Simulator::BAModel, guess_model_class("BAModel")
|
|
14
|
+
assert_equal Rgraphum::Simulator::BAModel, guess_model_class("ba_model")
|
|
15
|
+
assert_equal Rgraphum::Simulator::BAModel, guess_model_class("BAmodel")
|
|
16
|
+
|
|
17
|
+
assert_equal Rgraphum::Simulator::SISModel, guess_model_class("SISmodel")
|
|
18
|
+
assert_equal Rgraphum::Simulator::SISModel, guess_model_class("SIS Model")
|
|
19
|
+
assert_equal Rgraphum::Simulator::SISModel, guess_model_class("SISModel")
|
|
20
|
+
assert_equal Rgraphum::Simulator::SISModel, guess_model_class("sismodel")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
require 'rgraphum'
|
|
4
|
+
require 'test_helper'
|
|
5
|
+
|
|
6
|
+
class TfIdfTest < MiniTest::Test
|
|
7
|
+
|
|
8
|
+
def test_tf_idf_c
|
|
9
|
+
labels = [ "A", "B", "C", "D", "E" ]
|
|
10
|
+
matrix = [ [2.0, 1.0, 1.0, 0.0, 0.0],
|
|
11
|
+
[0.0, 2.0, 1.0, 1.0, 0.0],
|
|
12
|
+
[1.0, 1.0, 1.0, 0.0, 0.0],
|
|
13
|
+
[0.0, 0.0, 0.0, 1.0, 1.0]]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
tf_idf_object = TfIdf.new
|
|
17
|
+
tf_idf = tf_idf_object.tf_idf( matrix )
|
|
18
|
+
|
|
19
|
+
tf_idf_ans_lib = [ [0.34657359027997264, 0.07192051811294521, 0.07192051811294521, 0.0, 0.0],
|
|
20
|
+
[0.0, 0.14384103622589042, 0.07192051811294521, 0.17328679513998632, 0.0],
|
|
21
|
+
[0.23104906018664842, 0.09589402415059362, 0.09589402415059362, 0.0, 0.0],
|
|
22
|
+
[0.0, 0.0, 0.0, 0.34657359027997264, 0.6931471805599453] ]
|
|
23
|
+
|
|
24
|
+
tf_idf_ans_lib.flatten.each_with_index do |ans_tmp,i|
|
|
25
|
+
assert_equal ans_tmp.round(5), tf_idf.flatten[i].round(5)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
require 'rgraphum'
|
|
4
|
+
require 'test_helper'
|
|
5
|
+
|
|
6
|
+
class RgraphumVertexTest < MiniTest::Test
|
|
7
|
+
def setup
|
|
8
|
+
@graph = Rgraphum::Graph.new
|
|
9
|
+
@graph.vertices = [
|
|
10
|
+
{ id: 1, label: "hoge" },
|
|
11
|
+
{ id: 2, label: "huga" },
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
@vertex0 = @graph.vertices[0]
|
|
15
|
+
@vertex1 = @graph.vertices[1]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_vertex_within_term
|
|
19
|
+
t = Time.now
|
|
20
|
+
vertex_a = Rgraphum::Vertex.new
|
|
21
|
+
vertex_a.start = t
|
|
22
|
+
vertex_a.end = t + 2
|
|
23
|
+
|
|
24
|
+
vertex_b = Rgraphum::Vertex.new
|
|
25
|
+
vertex_b.start = t + 1
|
|
26
|
+
vertex_b.end = t + 3
|
|
27
|
+
|
|
28
|
+
assert_equal true, vertex_a.within_term(vertex_b)
|
|
29
|
+
assert_equal false, vertex_b.within_term(vertex_a)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_vertex_dump_and_load
|
|
33
|
+
vertex = Rgraphum::Vertex.new(id: 1, label: "vertex 1")
|
|
34
|
+
|
|
35
|
+
data = Marshal.dump(vertex)
|
|
36
|
+
vertex_dash = Marshal.load(data)
|
|
37
|
+
|
|
38
|
+
rg_assert_equal vertex, vertex_dash
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_invalid_field
|
|
42
|
+
assert_raises(ArgumentError) do
|
|
43
|
+
Rgraphum::Vertex.new(labeeeeeeeeeeeeeeeeeeeel: "label")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
assert_raises(ArgumentError) do
|
|
47
|
+
Rgraphum::Vertex.new(label: "label", labeeeeeeeeeeeeeeeeeeeel: "label")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|