rgraphum 0.0.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- 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,40 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rgraphum'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
class DegreeDistributionTest < MiniTest::Test
|
7
|
+
|
8
|
+
|
9
|
+
def setup
|
10
|
+
# a - b
|
11
|
+
# \ /
|
12
|
+
# c
|
13
|
+
# / \
|
14
|
+
# d - e
|
15
|
+
@graph = Rgraphum::Graph.new
|
16
|
+
|
17
|
+
@a = @graph.vertices.build(label: "a")
|
18
|
+
@b = @graph.vertices.build(label: "b")
|
19
|
+
@c = @graph.vertices.build(label: "c")
|
20
|
+
@d = @graph.vertices.build(label: "d")
|
21
|
+
@e = @graph.vertices.build(label: "e")
|
22
|
+
|
23
|
+
@a_b_e = @graph.edges.build(source: @a, target: @b)
|
24
|
+
@a_c_e = @graph.edges.build(source: @a, target: @c)
|
25
|
+
@b_c_e = @graph.edges.build(source: @b, target: @c)
|
26
|
+
@c_b_e = @graph.edges.build(source: @c, target: @d)
|
27
|
+
@c_e_e = @graph.edges.build(source: @c, target: @e)
|
28
|
+
@d_e_e = @graph.edges.build(source: @d, target: @e)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_degree_distribution
|
33
|
+
assert_equal ( {2=>0.8, 4=>0.2} ) , @graph.degree_distribution
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_degree_distribution_exponent
|
37
|
+
assert_equal -2.0, @graph.degree_distribution_exponent
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rgraphum'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
class RgraphumMathDijkstraTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@graph = Rgraphum::Graph.new
|
9
|
+
# @graph.vertices = [
|
10
|
+
# { label: "A" },
|
11
|
+
# { label: "B" },
|
12
|
+
# { label: "C" },
|
13
|
+
# { label: "D" },
|
14
|
+
# { label: "G" },
|
15
|
+
# ]
|
16
|
+
@a = @graph.vertices.build(label: "A")
|
17
|
+
@b = @graph.vertices.build(label: "B")
|
18
|
+
@c = @graph.vertices.build(label: "C")
|
19
|
+
@d = @graph.vertices.build(label: "D")
|
20
|
+
@g = @graph.vertices.build(label: "G")
|
21
|
+
end
|
22
|
+
|
23
|
+
# A --(5)-- B --(1)-- G
|
24
|
+
# | \ | /
|
25
|
+
#(1) (4) (2) (4)
|
26
|
+
# | \ | /
|
27
|
+
# C --(5)-- D
|
28
|
+
#
|
29
|
+
def test_adjacency_matrix_1
|
30
|
+
@graph.edges.build(source: @a, target: @b, weight: 5) # A->B
|
31
|
+
@graph.edges.build(source: @a, target: @c, weight: 1) # A->C
|
32
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
33
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
34
|
+
@graph.edges.build(source: @c, target: @d, weight: 5) # C->D
|
35
|
+
@graph.edges.build(source: @b, target: @g, weight: 1) # B->G
|
36
|
+
@graph.edges.build(source: @d, target: @g, weight: 4) # D->G
|
37
|
+
|
38
|
+
a_matrix = @graph.adjacency_matrix
|
39
|
+
|
40
|
+
expected = [
|
41
|
+
# A B C D G
|
42
|
+
[nil, 5, 1, 4, nil], # A
|
43
|
+
[ 5, nil, nil, 2, 1], # B
|
44
|
+
[ 1, nil, nil, 5, nil], # C
|
45
|
+
[ 4, 2, 5, nil, 4], # D
|
46
|
+
[nil, 1, nil, 4, nil], # G
|
47
|
+
]
|
48
|
+
|
49
|
+
assert_equal expected, a_matrix
|
50
|
+
end
|
51
|
+
|
52
|
+
# A --(1)-- B --(4)-- G
|
53
|
+
# | \ | /
|
54
|
+
#(2) (4) (2) (1)
|
55
|
+
# | \ | /
|
56
|
+
# C --(3)-- D
|
57
|
+
#
|
58
|
+
# A -> G : A -> B -> D -> G
|
59
|
+
def test_dijkstra_1
|
60
|
+
@graph.edges.build(source: @a, target: @b, weight: 1) # A->B
|
61
|
+
@graph.edges.build(source: @a, target: @c, weight: 2) # A->C
|
62
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
63
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
64
|
+
@graph.edges.build(source: @c, target: @d, weight: 3) # C->D
|
65
|
+
@graph.edges.build(source: @b, target: @g, weight: 4) # B->G
|
66
|
+
@graph.edges.build(source: @d, target: @g, weight: 1) # D->G
|
67
|
+
|
68
|
+
vertices = @graph.dijkstra(@a, @g)
|
69
|
+
# p vertices.map(&:label)
|
70
|
+
|
71
|
+
assert_equal 4, vertices.size
|
72
|
+
assert_equal vertices[0], @a
|
73
|
+
assert_equal vertices[1], @b
|
74
|
+
assert_equal vertices[2], @d
|
75
|
+
assert_equal vertices[3], @g
|
76
|
+
end
|
77
|
+
|
78
|
+
# A --(5)-- B --(1)-- G
|
79
|
+
# | \ | /
|
80
|
+
#(1) (4) (2) (4)
|
81
|
+
# | \ | /
|
82
|
+
# C --(5)-- D
|
83
|
+
#
|
84
|
+
# A -> G : A -> B -> G
|
85
|
+
def test_dijkstra_2
|
86
|
+
@graph.edges.build(source: @a, target: @b, weight: 5) # A->B
|
87
|
+
@graph.edges.build(source: @a, target: @c, weight: 1) # A->C
|
88
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
89
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
90
|
+
@graph.edges.build(source: @c, target: @d, weight: 5) # C->D
|
91
|
+
@graph.edges.build(source: @b, target: @g, weight: 1) # B->G
|
92
|
+
@graph.edges.build(source: @d, target: @g, weight: 4) # D->G
|
93
|
+
|
94
|
+
vertices = @graph.dijkstra(@a, @g)
|
95
|
+
# p vertices.map(&:label)
|
96
|
+
|
97
|
+
assert_equal 3, vertices.size
|
98
|
+
assert_equal vertices[0], @a
|
99
|
+
assert_equal vertices[1], @b
|
100
|
+
assert_equal vertices[2], @g
|
101
|
+
end
|
102
|
+
|
103
|
+
# A --(5)-- B --(1)-- G
|
104
|
+
# | \ | /
|
105
|
+
#(1) (4) (2) (4)
|
106
|
+
# | \ | /
|
107
|
+
# C --(5)-- D
|
108
|
+
#
|
109
|
+
# A -> B : A -> B
|
110
|
+
def test_dijkstra_3
|
111
|
+
@graph.edges.build(source: @a, target: @b, weight: 5) # A->B
|
112
|
+
@graph.edges.build(source: @a, target: @c, weight: 1) # A->C
|
113
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
114
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
115
|
+
@graph.edges.build(source: @c, target: @d, weight: 5) # C->D
|
116
|
+
@graph.edges.build(source: @b, target: @g, weight: 1) # B->G
|
117
|
+
@graph.edges.build(source: @d, target: @g, weight: 4) # D->G
|
118
|
+
|
119
|
+
vertices = @graph.dijkstra(@a, @b)
|
120
|
+
assert_equal 2, vertices.size
|
121
|
+
assert_equal vertices[0], @a
|
122
|
+
assert_equal vertices[1], @b
|
123
|
+
end
|
124
|
+
|
125
|
+
# A --(5)-- B --(1)-- G
|
126
|
+
# | \ | /
|
127
|
+
#(1) (4) (2) (4)
|
128
|
+
# | \ | /
|
129
|
+
# C --(5)-- D
|
130
|
+
#
|
131
|
+
# A -> C : A -> C
|
132
|
+
def test_dijkstra_4
|
133
|
+
@graph.edges.build(source: @a, target: @b, weight: 5) # A->B
|
134
|
+
@graph.edges.build(source: @a, target: @c, weight: 1) # A->C
|
135
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
136
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
137
|
+
@graph.edges.build(source: @c, target: @d, weight: 5) # C->D
|
138
|
+
@graph.edges.build(source: @b, target: @g, weight: 1) # B->G
|
139
|
+
@graph.edges.build(source: @d, target: @g, weight: 4) # D->G
|
140
|
+
|
141
|
+
vertices = @graph.dijkstra(@a, @c)
|
142
|
+
assert_equal 2, vertices.size
|
143
|
+
assert_equal vertices[0], @a
|
144
|
+
assert_equal vertices[1], @c
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'rgraphum'
|
5
|
+
|
6
|
+
class RgraphumMathModularityTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
# 1 - 2
|
9
|
+
# \ /
|
10
|
+
# 3
|
11
|
+
# / \
|
12
|
+
# 4 - 5
|
13
|
+
|
14
|
+
@graph = Rgraphum::Graph.new
|
15
|
+
@graph.vertices = [
|
16
|
+
{ id: 1, label: "A", community_id: 1 },
|
17
|
+
{ id: 2, label: "B", community_id: 1 },
|
18
|
+
{ id: 3, label: "C" },
|
19
|
+
{ id: 4, label: "D" },
|
20
|
+
{ id: 5, label: "E" },
|
21
|
+
]
|
22
|
+
@graph.edges = [
|
23
|
+
{ id: 0, source: 1, target: 2, weight: 1 },
|
24
|
+
{ id: 1, source: 1, target: 3, weight: 1 },
|
25
|
+
{ id: 2, source: 2, target: 3, weight: 1 },
|
26
|
+
{ id: 3, source: 3, target: 4, weight: 1 },
|
27
|
+
{ id: 4, source: 3, target: 5, weight: 1 },
|
28
|
+
{ id: 5, source: 4, target: 5, weight: 1 },
|
29
|
+
]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_modularity
|
33
|
+
assert @graph.modularity
|
34
|
+
assert_equal 1.0, @graph.modularity * 9.0
|
35
|
+
|
36
|
+
community_id_a = @graph.vertices[0].community_id
|
37
|
+
assert_equal @graph.vertices[0].community_id, community_id_a
|
38
|
+
assert_equal @graph.vertices[1].community_id, community_id_a
|
39
|
+
assert_equal @graph.vertices[2].community_id, community_id_a
|
40
|
+
community_id_a = @graph.vertices[3].community_id
|
41
|
+
assert_equal @graph.vertices[3].community_id, community_id_a
|
42
|
+
assert_equal @graph.vertices[4].community_id, community_id_a
|
43
|
+
|
44
|
+
@graph = Rgraphum::Graph.new
|
45
|
+
@graph.vertices = [
|
46
|
+
{ id: 0, label: "0" },
|
47
|
+
{ id: 1, label: "1" },
|
48
|
+
{ id: 2, label: "2" },
|
49
|
+
{ id: 3, label: "3" },
|
50
|
+
{ id: 4, label: "4" },
|
51
|
+
{ id: 5, label: "5" },
|
52
|
+
{ id: 6, label: "6" },
|
53
|
+
{ id: 7, label: "7" },
|
54
|
+
{ id: 8, label: "8" },
|
55
|
+
{ id: 9, label: "9" },
|
56
|
+
{ id: 10, label: "10" },
|
57
|
+
{ id: 11, label: "11" },
|
58
|
+
{ id: 12, label: "12" },
|
59
|
+
{ id: 13, label: "13" },
|
60
|
+
{ id: 14, label: "14" },
|
61
|
+
{ id: 15, label: "15" },
|
62
|
+
]
|
63
|
+
@graph.edges = [
|
64
|
+
{ id: 0, source: 0, target: 2, weight: 1 },
|
65
|
+
{ id: 1, source: 0, target: 3, weight: 1 },
|
66
|
+
{ id: 2, source: 0, target: 4, weight: 1 },
|
67
|
+
{ id: 3, source: 0, target: 5, weight: 1 },
|
68
|
+
{ id: 4, source: 1, target: 2, weight: 1 },
|
69
|
+
{ id: 5, source: 1, target: 4, weight: 1 },
|
70
|
+
{ id: 6, source: 1, target: 7, weight: 1 },
|
71
|
+
{ id: 7, source: 2, target: 4, weight: 1 },
|
72
|
+
{ id: 8, source: 2, target: 5, weight: 1 },
|
73
|
+
{ id: 9, source: 2, target: 6, weight: 1 },
|
74
|
+
{ id: 10, source: 3, target: 7, weight: 1 },
|
75
|
+
{ id: 11, source: 4, target: 10, weight: 1 },
|
76
|
+
{ id: 12, source: 5, target: 7, weight: 1 },
|
77
|
+
{ id: 13, source: 5, target: 11, weight: 1 },
|
78
|
+
{ id: 14, source: 6, target: 7, weight: 1 },
|
79
|
+
{ id: 15, source: 6, target: 11, weight: 1 },
|
80
|
+
{ id: 16, source: 8, target: 9, weight: 1 },
|
81
|
+
{ id: 17, source: 8, target: 10, weight: 1 },
|
82
|
+
{ id: 18, source: 8, target: 11, weight: 1 },
|
83
|
+
{ id: 19, source: 8, target: 14, weight: 1 },
|
84
|
+
{ id: 20, source: 8, target: 15, weight: 1 },
|
85
|
+
{ id: 21, source: 9, target: 12, weight: 1 },
|
86
|
+
{ id: 22, source: 9, target: 14, weight: 1 },
|
87
|
+
{ id: 23, source: 10, target: 11, weight: 1 },
|
88
|
+
{ id: 24, source: 10, target: 12, weight: 1 },
|
89
|
+
{ id: 25, source: 10, target: 13, weight: 1 },
|
90
|
+
{ id: 26, source: 10, target: 14, weight: 1 },
|
91
|
+
{ id: 27, source: 11, target: 13, weight: 1 },
|
92
|
+
]
|
93
|
+
vertices = @graph.vertices
|
94
|
+
edges = @graph.edges
|
95
|
+
|
96
|
+
assert_equal 0.392, @graph.modularity.round(3)
|
97
|
+
# @graph.modularity.round(3)
|
98
|
+
|
99
|
+
community_id_a = @graph.vertices[0].community_id
|
100
|
+
assert_equal 0, community_id_a
|
101
|
+
assert_equal @graph.vertices[ 0].community_id, community_id_a
|
102
|
+
assert_equal @graph.vertices[ 1].community_id, community_id_a
|
103
|
+
assert_equal @graph.vertices[ 2].community_id, community_id_a
|
104
|
+
assert_equal @graph.vertices[ 3].community_id, community_id_a
|
105
|
+
assert_equal @graph.vertices[ 4].community_id, community_id_a
|
106
|
+
assert_equal @graph.vertices[ 5].community_id, community_id_a
|
107
|
+
assert_equal @graph.vertices[ 6].community_id, community_id_a
|
108
|
+
assert_equal @graph.vertices[ 7].community_id, community_id_a
|
109
|
+
|
110
|
+
community_id_b = @graph.vertices[8].community_id
|
111
|
+
assert_equal 8, community_id_b
|
112
|
+
assert_equal @graph.vertices[ 8].community_id, community_id_b
|
113
|
+
assert_equal @graph.vertices[ 9].community_id, community_id_b
|
114
|
+
assert_equal @graph.vertices[10].community_id, community_id_b
|
115
|
+
assert_equal @graph.vertices[11].community_id, community_id_b
|
116
|
+
assert_equal @graph.vertices[12].community_id, community_id_b
|
117
|
+
assert_equal @graph.vertices[13].community_id, community_id_b
|
118
|
+
assert_equal @graph.vertices[14].community_id, community_id_b
|
119
|
+
|
120
|
+
# same graph befor after
|
121
|
+
@graph.vertices.each_with_index do |vertex,i|
|
122
|
+
assert_same vertex, vertices[i]
|
123
|
+
end
|
124
|
+
@graph.edges.each_with_index do |edge,i|
|
125
|
+
assert_same edge, edges[i]
|
126
|
+
assert_same edge.source, edges[i].source
|
127
|
+
assert_same edge.target, edges[i].target
|
128
|
+
end
|
129
|
+
|
130
|
+
gexf = @graph.to_gephi
|
131
|
+
file = File.open("tmp/modularity.gexf", "w")
|
132
|
+
file.write(gexf)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_none_edges_modularity
|
136
|
+
# nosw 3 is only vertex
|
137
|
+
# vertex 1 - 2, 4 - 5 is one edge cluster
|
138
|
+
|
139
|
+
@graph = Rgraphum::Graph.new
|
140
|
+
@graph.vertices = [
|
141
|
+
{ id: 1, label: "A", community_id: 1 },
|
142
|
+
{ id: 2, label: "B", community_id: 1 },
|
143
|
+
{ id: 3, label: "C" },
|
144
|
+
{ id: 4, label: "D" },
|
145
|
+
{ id: 5, label: "E" },
|
146
|
+
]
|
147
|
+
@graph.edges = [
|
148
|
+
{ id: 0, source: 1, target: 2, weight: 1 },
|
149
|
+
{ id: 5, source: 4, target: 5, weight: 1 },
|
150
|
+
]
|
151
|
+
|
152
|
+
assert_equal 0.5,@graph.modularity
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rgraphum'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
class RgraphumMathQuickAverageDistanceMatrixTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@graph = Rgraphum::Graph.new
|
9
|
+
# @graph.vertices = [
|
10
|
+
# { label: "A" },
|
11
|
+
# { label: "B" },
|
12
|
+
# { label: "C" },
|
13
|
+
# { label: "D" },
|
14
|
+
# { label: "G" },
|
15
|
+
# ]
|
16
|
+
@a = @graph.vertices.build(label: "A")
|
17
|
+
@b = @graph.vertices.build(label: "B")
|
18
|
+
@c = @graph.vertices.build(label: "C")
|
19
|
+
@d = @graph.vertices.build(label: "D")
|
20
|
+
@g = @graph.vertices.build(label: "G")
|
21
|
+
end
|
22
|
+
|
23
|
+
# A --(5)-- B --(1)-- G
|
24
|
+
# | \ | /
|
25
|
+
#(1) (4) (2) (4)
|
26
|
+
# | \ | /
|
27
|
+
# C --(5)-- D
|
28
|
+
#
|
29
|
+
def test_average_distance_1
|
30
|
+
@graph.edges.build(source: @a, target: @b, weight: 5) # A->B
|
31
|
+
@graph.edges.build(source: @a, target: @c, weight: 1) # A->C
|
32
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
33
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
34
|
+
@graph.edges.build(source: @c, target: @d, weight: 5) # C->D
|
35
|
+
@graph.edges.build(source: @b, target: @g, weight: 1) # B->G
|
36
|
+
@graph.edges.build(source: @d, target: @g, weight: 4) # D->G
|
37
|
+
|
38
|
+
average_distance = @graph.quick_average_distance
|
39
|
+
|
40
|
+
n = @graph.vertices.size
|
41
|
+
minimum_distance_matrix = [
|
42
|
+
# A B C D G
|
43
|
+
[ 0, 5, 1, 4, 6 ], # A
|
44
|
+
[ 5, 0, 6, 2, 1 ], # B
|
45
|
+
[ 1, 6, 0, 5, 7 ], # C
|
46
|
+
[ 4, 2, 5, 0, 3 ], # D
|
47
|
+
[ 6, 1, 7, 3, 0 ], # G
|
48
|
+
]
|
49
|
+
expected = Rational(minimum_distance_matrix.flatten.inject(&:+), n * (n - 1))
|
50
|
+
|
51
|
+
assert_equal expected, average_distance
|
52
|
+
end
|
53
|
+
|
54
|
+
# A --(1)-- B --(4)-- G
|
55
|
+
# | \ | /
|
56
|
+
#(2) (4) (2) (1)
|
57
|
+
# | \ | /
|
58
|
+
# C --(3)-- D
|
59
|
+
#
|
60
|
+
def test_average_distance_2
|
61
|
+
@graph.edges.build(source: @a, target: @b, weight: 1) # A->B
|
62
|
+
@graph.edges.build(source: @a, target: @c, weight: 2) # A->C
|
63
|
+
@graph.edges.build(source: @a, target: @d, weight: 4) # A->D
|
64
|
+
@graph.edges.build(source: @b, target: @d, weight: 2) # B->D
|
65
|
+
@graph.edges.build(source: @c, target: @d, weight: 3) # C->D
|
66
|
+
@graph.edges.build(source: @b, target: @g, weight: 4) # B->G
|
67
|
+
@graph.edges.build(source: @d, target: @g, weight: 1) # D->G
|
68
|
+
|
69
|
+
average_distance = @graph.quick_average_distance
|
70
|
+
|
71
|
+
n = @graph.vertices.size
|
72
|
+
minimum_distance_matrix = [
|
73
|
+
# A B C D G
|
74
|
+
[ 0, 1, 2, 3, 4 ], # A
|
75
|
+
[ 1, 0, 3, 2, 3 ], # B
|
76
|
+
[ 2, 3, 0, 3, 4 ], # C
|
77
|
+
[ 3, 2, 3, 0, 1 ], # D
|
78
|
+
[ 4, 3, 4, 1, 0 ], # G
|
79
|
+
]
|
80
|
+
expected = Rational(minimum_distance_matrix.flatten.inject(&:+), n * (n - 1))
|
81
|
+
|
82
|
+
assert_equal expected, average_distance
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rgraphum'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
class RgraphumPathTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@graph = Rgraphum::Graph.new
|
9
|
+
@v1 = @graph.vertices.build(label: "v1")
|
10
|
+
@v2 = @graph.vertices.build(label: "v2")
|
11
|
+
@v3 = @graph.vertices.build(label: "v3")
|
12
|
+
@v4 = @graph.vertices.build(label: "v4")
|
13
|
+
@v5 = @graph.vertices.build(label: "v5")
|
14
|
+
@v6 = @graph.vertices.build(label: "v6")
|
15
|
+
|
16
|
+
@e1 = @graph.edges.build(source: @v1, target: @v2, weight: 1)
|
17
|
+
@e2 = @graph.edges.build(source: @v2, target: @v3, weight: 2)
|
18
|
+
@e3 = @graph.edges.build(source: @v3, target: @v4, weight: 3)
|
19
|
+
@e4 = @graph.edges.build(source: @v4, target: @v5, weight: 4)
|
20
|
+
@e5 = @graph.edges.build(source: @v5, target: @v6, weight: 5)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_path_vertices
|
24
|
+
path = Rgraphum::Path.new(@v3, [@v1, @v2, @v3])
|
25
|
+
assert_equal [@v1, @v2, @v3], path.vertices
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_path_total_weight
|
29
|
+
path = Rgraphum::Path.new(@v2, [@v1, @v2])
|
30
|
+
assert_equal 1, path.total_weight
|
31
|
+
|
32
|
+
path = Rgraphum::Path.new(@v3, [@v1, @v2, @v3])
|
33
|
+
assert_equal 3, path.total_weight
|
34
|
+
|
35
|
+
path = Rgraphum::Path.new(@v4, [@v1, @v2, @v3, @v4])
|
36
|
+
assert_equal 6, path.total_weight
|
37
|
+
|
38
|
+
path = Rgraphum::Path.new(@v4, [@v1, @v2, @v3, @v4, @v5])
|
39
|
+
assert_equal 10, path.total_weight
|
40
|
+
|
41
|
+
path = Rgraphum::Path.new(@v6, [@v1, @v2, @v3, @v4, @v5, @v6])
|
42
|
+
assert_equal 15, path.total_weight
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'rgraphum'
|
5
|
+
|
6
|
+
class RgraphumQueryEnumerableTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@array = [
|
9
|
+
OpenStruct.new(id: 1, label: "aaa"),
|
10
|
+
OpenStruct.new(id: 2, label: "bbb"),
|
11
|
+
OpenStruct.new(id: 3, label: "ccc"),
|
12
|
+
OpenStruct.new(id: 4, label: "ddd"),
|
13
|
+
OpenStruct.new(id: 5, label: "aaa"),
|
14
|
+
OpenStruct.new(id: 6, label: "bbb"),
|
15
|
+
OpenStruct.new(id: 7, label: "ccc"),
|
16
|
+
OpenStruct.new(id: 8, label: "ddd"),
|
17
|
+
OpenStruct.new(id: 9, label: "aaa"),
|
18
|
+
OpenStruct.new(id: 10, label: "bbb"),
|
19
|
+
OpenStruct.new(id: 11, label: "ccc"),
|
20
|
+
OpenStruct.new(id: 12, label: "ddd"),
|
21
|
+
]
|
22
|
+
@query = Rgraphum::Query.new(@array)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_where_and_all
|
26
|
+
items = @query.where(label: "aaa") # .all
|
27
|
+
assert_equal [1, 5, 9], items.map(&:id)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_where_and_all_without_matches
|
31
|
+
items = @query.where(label: "xyz") # .all
|
32
|
+
assert_empty items
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_each
|
36
|
+
items = []
|
37
|
+
@query.where(label: "ccc").each do |item|
|
38
|
+
items << item
|
39
|
+
end
|
40
|
+
assert_equal [3, 7, 11], items.map(&:id)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rgraphum'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
class RgraphumQueryWhereOperatorsTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@array = [
|
9
|
+
OpenStruct.new(id: 1, label: "aaa"),
|
10
|
+
OpenStruct.new(id: 2, label: "bbb"),
|
11
|
+
OpenStruct.new(id: 3, label: "ccc"),
|
12
|
+
OpenStruct.new(id: 4, label: "ddd"),
|
13
|
+
OpenStruct.new(id: 5, label: "aaa"),
|
14
|
+
OpenStruct.new(id: 6, label: "bbb"),
|
15
|
+
OpenStruct.new(id: 7, label: "ccc"),
|
16
|
+
OpenStruct.new(id: 8, label: "ddd"),
|
17
|
+
OpenStruct.new(id: 9, label: "aaa"),
|
18
|
+
OpenStruct.new(id: 10, label: "bbb"),
|
19
|
+
OpenStruct.new(id: 11, label: "ccc"),
|
20
|
+
OpenStruct.new(id: 12, label: "ddd"),
|
21
|
+
]
|
22
|
+
@query = Rgraphum::Query.new(@array)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_where_eq_1
|
26
|
+
items = @query.where(:label, :eq, "aaa").all
|
27
|
+
assert_equal [1, 5, 9], items.map(&:id)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_where_eq_2
|
31
|
+
items = @query.where(:id, :eq, 3).all
|
32
|
+
assert_equal [3], items.map(&:id)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_where_ne
|
36
|
+
items = @query.where(:id, :ne, 3).all
|
37
|
+
assert_equal [1,2,4,5,6,7,8,9,10,11,12], items.map(&:id)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_where_gt
|
41
|
+
items = @query.where(:id, :gt, 6).all
|
42
|
+
assert_equal [7,8,9,10,11,12], items.map(&:id)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_where_gte
|
46
|
+
items = @query.where(:id, :gte, 6).all
|
47
|
+
assert_equal [6,7,8,9,10,11,12], items.map(&:id)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_where_lt
|
51
|
+
items = @query.where(:id, :lt, 6).all
|
52
|
+
assert_equal [1,2,3,4,5], items.map(&:id)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_where_lte
|
56
|
+
items = @query.where(:id, :lte, 6).all
|
57
|
+
assert_equal [1,2,3,4,5,6], items.map(&:id)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_where_match
|
61
|
+
items = @query.where(:label, :match, /^a+$/).all
|
62
|
+
assert_equal [1,5,9], items.map(&:id)
|
63
|
+
|
64
|
+
items = @query.where(:label, :=~, /^a+$/).all
|
65
|
+
assert_equal [1,5,9], items.map(&:id)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_where_not_match
|
69
|
+
items = @query.where(:label, :not_match, /^a+$/).all
|
70
|
+
assert_equal [2,3,4,6,7,8,10,11,12], items.map(&:id)
|
71
|
+
|
72
|
+
items = @query.where(:label, :!~, /^a+$/).all
|
73
|
+
assert_equal [2,3,4,6,7,8,10,11,12], items.map(&:id)
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rgraphum'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
class RgraphumQueryWhereTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@array = [
|
9
|
+
OpenStruct.new(id: 1, label: "aaa"),
|
10
|
+
OpenStruct.new(id: 2, label: "bbb"),
|
11
|
+
OpenStruct.new(id: 3, label: "ccc"),
|
12
|
+
OpenStruct.new(id: 4, label: "ddd"),
|
13
|
+
OpenStruct.new(id: 5, label: "aaa"),
|
14
|
+
OpenStruct.new(id: 6, label: "bbb"),
|
15
|
+
OpenStruct.new(id: 7, label: "ccc"),
|
16
|
+
OpenStruct.new(id: 8, label: "ddd"),
|
17
|
+
OpenStruct.new(id: 9, label: "aaa"),
|
18
|
+
OpenStruct.new(id: 10, label: "bbb"),
|
19
|
+
OpenStruct.new(id: 11, label: "ccc"),
|
20
|
+
OpenStruct.new(id: 12, label: "ddd"),
|
21
|
+
]
|
22
|
+
@query = Rgraphum::Query.new(@array)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_where_and_all
|
26
|
+
items = @query.where(label: "aaa").all
|
27
|
+
assert_equal [1, 5, 9], items.map(&:id)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_where_and_all_without_matches
|
31
|
+
items = @query.where(label: "xyz").all
|
32
|
+
assert_empty items
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_where_and_first_1
|
36
|
+
item = @query.where(id: 3).first
|
37
|
+
assert_equal 3, item.id
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_where_and_first_2
|
41
|
+
item = @query.where(id: 2).where(label: "bbb").first
|
42
|
+
assert_equal 2, item.id
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_where_and_first_without_matches
|
46
|
+
item = @query.where(id: 2).where(label: "xyz").first
|
47
|
+
assert_nil item
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_where_and_last
|
51
|
+
item = @query.where(label: "bbb").last
|
52
|
+
assert_equal 10, item.id
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_where_and_last_without_matches
|
56
|
+
item = @query.where(label: "xyz").last
|
57
|
+
assert_nil item
|
58
|
+
end
|
59
|
+
end
|