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

Commit

Permalink
Reducer: skip zero-weight edges
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 27, 2024
1 parent 195b74c commit b2b02e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/mosaik/graph/reducer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(options, graph)

def call(directed: false)
# Iterate over all combinations of vertices
weights = graph.vertices.keys.combination(2).map do |v1, v2|
weights = graph.vertices.keys.combination(2).filter_map do |v1, v2|
# Find all edges between the two vertices
edges = Set.new(graph.find_edges(v1, v2) + graph.find_edges(v2, v1))

Expand All @@ -39,6 +39,9 @@ def call(directed: false)
.select { |e| e.attributes[:type] == "contributor" }
.sum { |e| e.attributes.fetch(:weight, 0.0) }

# Don't add zero weights
next if weight.zero?

# Return vertices and weights
[v1, v2, weight]
end
Expand Down
29 changes: 23 additions & 6 deletions spec/mosaik/graph/reducer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# typed: true

RSpec.describe MOSAIK::Graph::Reducer do
subject(:preprocessor) { described_class.new(options, graph) }
subject(:reducer) { described_class.new(options, graph) }

let(:options) { { structural: 0.5, logical: 0.3, contributor: 0.2 } }
let(:graph) { build(:graph) }
Expand Down Expand Up @@ -31,13 +31,13 @@
it "changes the graph to undirected" do
expect(graph).to be_directed

preprocessor.call
reducer.call

expect(graph).not_to be_directed
end

it "aggregates the weights of the edges between vertices" do
preprocessor.call
reducer.call

v1_v2 = ((0.5 * 2) + (0.5 * 3)) + ((0.3 * 2) + (0.3 * 3)) + ((0.2 * 2) + (0.2 * 3))
v2_v3 = ((0.5 * 3) + (0.5 * 1)) + ((0.3 * 3) + (0.3 * 1)) + ((0.2 * 3) + (0.2 * 1))
Expand All @@ -52,11 +52,28 @@
expect(graph.find_edge("v3", "v1")).to be_nil
end

describe "no coupling" do
let(:options) { { structural: 0.0, logical: 0.0, contributor: 0.0 } }

it "does not aggregate edges" do
reducer.call

expect(graph.find_edge("v1", "v2")).to be_nil
expect(graph.find_edge("v2", "v1")).to be_nil

expect(graph.find_edge("v2", "v3")).to be_nil
expect(graph.find_edge("v3", "v2")).to be_nil

expect(graph.find_edge("v1", "v3")).to be_nil
expect(graph.find_edge("v3", "v1")).to be_nil
end
end

describe "structural coupling" do
let(:options) { { structural: 0.5, logical: 0.0, contributor: 0.0 } }

it "aggregates the weights of the edges between vertices" do
preprocessor.call
reducer.call

v1_v2 = (0.5 * 2) + (0.5 * 3)
v2_v3 = (0.5 * 3) + (0.5 * 1)
Expand All @@ -76,7 +93,7 @@
let(:options) { { structural: 0.0, logical: 0.3, contributor: 0.0 } }

it "aggregates the weights of the edges between vertices" do
preprocessor.call
reducer.call

v1_v2 = (0.3 * 2) + (0.3 * 3)
v2_v3 = (0.3 * 3) + (0.3 * 1)
Expand All @@ -96,7 +113,7 @@
let(:options) { { structural: 0.0, logical: 0.0, contributor: 0.1 } }

it "aggregates the weights of the edges between vertices" do
preprocessor.call
reducer.call

v1_v2 = (0.1 * 2) + (0.1 * 3)
v2_v3 = (0.1 * 3) + (0.1 * 1)
Expand Down

0 comments on commit b2b02e6

Please sign in to comment.