Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Add cluster statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 26, 2024
1 parent 9693afe commit 3338d24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
17 changes: 14 additions & 3 deletions lib/mosaik/graph/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def initialize(options, graph)
end

def call
# Compute statistics
options[:metrics].to_h do |metric|
# Compute metric statistics
metrics = options[:metrics].to_h do |metric|
values = graph.clusters.each_value.map { |cluster| cluster.attributes[metric] }

statistics = {
Expand All @@ -32,7 +32,18 @@ def call
debug "Statistics for #{metric}: #{statistics.map { |k, v| "#{k} = #{v&.round(2)}" }.join(', ')}"

[metric, statistics]
end.deep_stringify_keys
end

# Compute cluster statistics
metrics[:clusters] = {
count: graph.clusters.size,
min: graph.clusters.values.map { |cluster| cluster.vertices.size }.min,
max: graph.clusters.values.map { |cluster| cluster.vertices.size }.max,
size: graph.clusters.values.map { |cluster| cluster.vertices.size },
}

# Return statistics
metrics.deep_stringify_keys
end

private
Expand Down
16 changes: 11 additions & 5 deletions spec/mosaik/graph/statistics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
let(:graph) { build(:graph) }

before do
graph.add_cluster("cluster1", abc_size: 1.0, cohesion: 0.5, complexity: 0.3, coupling: 0.2, modularity: 0.1)
graph.add_cluster("cluster2", abc_size: 2.0, cohesion: 0.4, complexity: 0.2, coupling: 0.3, modularity: 0.2)
graph.add_cluster("cluster3", abc_size: 1.5, cohesion: 0.3, complexity: 0.2, coupling: 0.3, modularity: 0.1)
c1 = graph.add_cluster("cluster1", abc_size: 1.0, cohesion: 0.5, complexity: 0.3, coupling: 0.2, modularity: 0.1)
c2 = graph.add_cluster("cluster2", abc_size: 2.0, cohesion: 0.4, complexity: 0.2, coupling: 0.3, modularity: 0.2)
c3 = graph.add_cluster("cluster3", abc_size: 1.5, cohesion: 0.3, complexity: 0.2, coupling: 0.3, modularity: 0.1)

c1.add_vertex(graph.add_vertex("vertex1"))
c2.add_vertex(graph.add_vertex("vertex2"))
c2.add_vertex(graph.add_vertex("vertex3"))
c3.add_vertex(graph.add_vertex("vertex4"))
end

describe "#call" do
it "computes the statistics" do
expect(statistics.call.transform_values { |s| s.transform_values { |f| f.round(2) } })
expect(statistics.call.transform_values { |s| s.transform_values { |f| f.is_a?(Numeric) ? f.round(2) : f } })
.to eq "abc_size" => { "min" => 1.0, "max" => 2.0, "mean" => 1.5, "q1" => 1.0, "q2" => 1.5, "q3" => 2.0 },
"cohesion" => { "min" => 0.3, "max" => 0.5, "mean" => 0.4, "q1" => 0.3, "q2" => 0.4, "q3" => 0.5 },
"complexity" => { "min" => 0.2, "max" => 0.3, "mean" => 0.23, "q1" => 0.2, "q2" => 0.2, "q3" => 0.3 },
"coupling" => { "min" => 0.2, "max" => 0.3, "mean" => 0.27, "q1" => 0.2, "q2" => 0.3, "q3" => 0.3 },
"modularity" => { "min" => 0.1, "max" => 0.2, "mean" => 0.13, "q1" => 0.1, "q2" => 0.1, "q3" => 0.2 }
"modularity" => { "min" => 0.1, "max" => 0.2, "mean" => 0.13, "q1" => 0.1, "q2" => 0.1, "q3" => 0.2 },
"clusters" => { "count" => 3, "min" => 1, "max" => 2, "size" => [1, 2, 1] }
end
end
end

0 comments on commit 3338d24

Please sign in to comment.