Skip to content

Commit

Permalink
Add basic print statements as "log"
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Aug 16, 2023
1 parent 6b5cee0 commit f8a479b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions modules/application/src/community_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ impl<'a> CommunityAssignment<'a> {
num_communities
}

pub fn get_nodes_in_community(&self, community: Community) -> Vec<Node> {
let mut nodes = Vec::new();
for node in 0..self.graph.num_nodes() {
if self.node_to_community[node] == community {
nodes.push(node);
}
}
nodes
}

fn calc_weighted_degrees_for_communities(&mut self, node: Node) {
// Reset weights
self.weighted_degrees_to_communities.clear();
Expand Down
31 changes: 31 additions & 0 deletions modules/application/src/louvain_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ impl<'a> LouvainLevel<'a> {
let mut is_improvement_in_one_graph_traversal;
let mut count_graph_traversals = 0;
loop {
println!();
println!();
println!(
"Optimize one level. Loop. Count: {}",
count_graph_traversals
);
is_improvement_in_one_graph_traversal = false;
let old_quality = quality;
for &node in &indices_shuffled {
Expand Down Expand Up @@ -99,7 +105,11 @@ impl<'a> LouvainLevel<'a> {
/// whether the node was moved to another community or stayed in its
/// previous community.
fn optimize_one_node(&mut self, node: Node) -> bool {
println!();
println!("💠 Optimize one node. Node: {}", node);

let prev_community = self.modularity.assignment.get_community(node);
println!("Prev community: {}", prev_community);

self.modularity
.assignment
Expand Down Expand Up @@ -135,11 +145,29 @@ impl<'a> LouvainLevel<'a> {
.for_each(|&adj_node| {
let target_community = self.modularity.assignment.get_community(adj_node);
let gain = self.modularity.gain(node, target_community);
print!("Gain moving to community {}: {}", target_community, gain,);
println!(
" (target community has nodes: {:?})",
self.modularity
.assignment
.get_nodes_in_community(target_community)
);
if gain > best_gain {
best_community = target_community;
best_gain = gain;
}
});
if best_community == prev_community {
println!(
"🏅 No improvement possible, staying in community {}",
prev_community
);
} else {
println!(
"🏅 Best community {}, yielding gain {}",
best_community, best_gain
);
}

best_community
}
Expand All @@ -154,6 +182,9 @@ impl<'a> LouvainLevel<'a> {
/// - edge weights are the sum of weights of edges between communities
/// - an edge within a community becomes two self-loops
pub fn get_next_level_graph(&mut self) -> LouvainGraph {
println!();
println!("💨 Get next level graph");
println!();
let num_communities = self.modularity.assignment.renumber_communities();
let mut next_graph = LouvainGraph::new(num_communities);

Expand Down
6 changes: 4 additions & 2 deletions modules/application/src/modularity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ impl<'a> Modularity<'a> {
});
let weighted_node_degree = self.graph.weighted_degrees[node];

vertex_weighted_degree_of_edges_to_community
- (tot_community * weighted_node_degree) / self.graph.twice_total_weighted_degree
let res = vertex_weighted_degree_of_edges_to_community
- (tot_community * weighted_node_degree) / self.graph.twice_total_weighted_degree;

res / (0.5 * self.graph.twice_total_weighted_degree)
}

/// Calculates the modularity of the current graph.
Expand Down
1 change: 1 addition & 0 deletions modules/cli/src/cli_louvain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn run(args: &LouvainArgs) {
let louvain = Louvain::new(&mut g);
let (hierarchy, modularities) = louvain.run();

println!();
println!("Modularities: {:?}", modularities);

if args.assignment_output_path.is_some() {
Expand Down

0 comments on commit f8a479b

Please sign in to comment.