Network/Algorithm/Louvain.pm

filter: Statementstmt 100% 142 / 142 Branchbran 100% 26 / 26 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 100% 186 / 186 CC 27 SCAR 22.2
CC
27
Cov
100%
identify_communities_with_louvain
17.0
_create_next_level_network
7.0
_consolidate_community_structure
4.0
CRAP 9.2
« Network/Algorithm/Betweenness.pm ↑ Summary Network/Algorithm/Cores.pm »
1 package SNA::Network::Algorithm::Louvain;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 require Exporter;
7 15 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 our @EXPORT = qw(identify_communities_with_louvain);
9  
10 15 ▶use SNA::Network::Community;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use SNA::Network::CommunityStructure;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use List::MoreUtils qw(uniq notall);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 =head1 NAME
18  
19 SNA::Network::Algorithm::Louvain - identifies communities with the Louvain-method developed by Blondel and Guillaume and Lamboitte and Lefebvre
20  
21  
22 =head1 SYNOPSIS
23  
24 use SNA::Network;
25  
26 my $net = SNA::Network->new();
27 $net->load_from_pajek_net($filename);
28 ...
29 my $num_communities = $net->identify_communities_with_louvain;
30  
31  
32 =head1 METHODS
33  
34 The following methods are added to L<SNA::Network>.
35  
36 =head2 identify_communities_with_louvain
37  
38 Performs the community identification algorithm and returns the number of communities it identified. The corresponding L<SNA::Network::Community> objects can be accessed with the C<communities> method of L<SNA::Network>.
39  
40 =cut
41  
42 3 ▶sub identify_communities_with_louvain {
Subroutine identify_communities_with_louvain: called CC=17 SCAR=28.3
Pod documented
43 3 my ($self, $level) = @_;
44 3 ▶ $level ||= 0;
Condition100%2/2
$level ||= 0
l!l
21
MC/DC100%1/1
$level ||= 0
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
45 3 $self->{louvain_levels} = $level;
46
47 3 $self->{total_weight} = $self->total_weight;
48
49 # initialize communities
50 3 foreach ($self->nodes) {
51 190 $_->{community} = $_->index;
52 190 $_->{_k_i} = $_->weighted_summed_degree;
53 }
54 my @communities = map {
55 3 SNA::Network::Community->new(
56 network => $self,
57 level => $level,
58 index => $_->index,
59 members_ref => [$_],
60 w_in => $level > 0 ? $_->loop->weight : 0,
61 190 ▶ w_tot => $_->{_k_i} - ($level > 0 ? $_->loop->weight : 0),
Branch: 2 branches
BranchTrueFalseTotal
$level > 0 ? : 10 180 190
$level > 0 ? : 10 180 190
62 )
63 } $self->nodes;
64  
65 3 $self->{communities_ref} = \@communities;
66
67 3 my $has_changed = 0;
68 3 my $has_improved;
69
70 PHASE_ONE_ITERATIONS:
71 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$has_improved and do { do { $has_improved = 0; foreach my $node_i ($self->nodes) { my $max_gain = 0; my $best_community_id; $node_i->{"_k_i_in"} = &sum($level > 0 ? -$node_i->loop->weight : 0, map({$_->weight;} grep({$_->source->community == $_->target->community;} $node_i->edges))); my $current_community = @communities[$node_i->community]; my $current_community_module = _module_value($current_community->w_in, $current_community->w_tot, $self->{"total_weight"}); my $new_current_community_module = _module_value($current_community->w_in - $node_i->{"_k_i_in"}, $current_community->w_tot - $node_i->{"_k_i"} + $node_i->{"_k_i_in"}, $self->{"total_weight"}); my $gain_on_removal = $new_current_community_module - $current_community_module; foreach $_ ($node_i->outgoing_edges) { $node_i->{"_k_i_new"}{$_->target->community} += $_->weight; } ; foreach $_ ($node_i->incoming_edges) { $node_i->{"_k_i_new"}{$_->source->community} += $_->weight; } ; if ($level > 0) { my $loop_weight = $node_i->loop->weight; foreach $_ (values %{$$node_i{"_k_i_new"};}) { $_ += $loop_weight; } ; } ; my(@neighbour_community_ids) = &uniq(grep({$_ != $node_i->community;} map({$_->community;} $node_i->related_nodes))); foreach my $neighbour_community_id (@neighbour_community_ids) { my $gain_on_addition = _modularity_gain($self, $node_i, $neighbour_community_id); my $gain = $gain_on_removal + $gain_on_addition; if ($gain > $max_gain) { $max_gain = $gain; $best_community_id = $neighbour_community_id; } ; } ; if ($max_gain > "1e-07") { _switch_community($self, $node_i, $best_community_id); $has_improved = 1; $has_changed = 1; } ; undef $node_i->{"_k_i_in"}; undef $node_i->{"_k_i_new"}; } } } 6 3 9
72 9 $has_improved = 0;
73
74 9 foreach my $node_i ($self->nodes) {
75 1096 my $max_gain = 0;
76 1096 my $best_community_id;
77  
78 $node_i->{_k_i_in} = sum(
79 $level > 0 ? - $node_i->loop->weight : 0,
80 map {
81 7738 $_->weight
82 } grep {
83 1096 ▶ $_->source->community == $_->target->community
Branch: 1 branch
BranchTrueFalseTotal
$level > 0 ? : 16 1080 1096
84 } $node_i->edges
85 );
86
87 1096 my $current_community = @communities[ $node_i->community ];
88
89 # gain in current community on removal
90 my $current_community_module = _module_value(
91 $current_community->w_in,
92 $current_community->w_tot,
93 $self->{total_weight},
94 1096 );
95
96 my $new_current_community_module = _module_value(
97 $current_community->w_in - $node_i->{_k_i_in},
98 $current_community->w_tot - $node_i->{_k_i} + $node_i->{_k_i_in},
99 $self->{total_weight},
100 1096 );
101
102 1096 my $gain_on_removal = $new_current_community_module - $current_community_module;
103
104 # pre-calculate k_i_new values for all neighbour communities
105 1096 foreach ($node_i->outgoing_edges) {
106 8764 $node_i->{_k_i_new}->{ $_->target->community } += $_->weight
107 }
108  
109 1096 foreach ($node_i->incoming_edges) {
110 8764 $node_i->{_k_i_new}->{ $_->source->community } += $_->weight
111 }
112  
113 1096 ▶ if ($level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level > 0) 16 1080 1096
114 16 my $loop_weight = $node_i->loop->weight;
115  
116 16 foreach ( values %{ $node_i->{_k_i_new} } ) {
117 65 $_ += $loop_weight;
118 }
119 }
120  
121 my @neighbour_community_ids = uniq grep {
122 17528 $_ != $node_i->community
123 } map {
124 1096 $_->community
125 } $node_i->related_nodes;
126
127 1096 foreach my $neighbour_community_id (@neighbour_community_ids) {
128 # calculate modularity changes
129 4966 my $gain_on_addition = _modularity_gain($self, $node_i, $neighbour_community_id);
130 4966 my $gain = $gain_on_removal + $gain_on_addition;
131
132 4966 ▶ if ( $gain > $max_gain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gain > $max_gain) 500 4466 4966
133 500 $max_gain = $gain;
134 500 $best_community_id = $neighbour_community_id;
135 }
136 }
137
138 1096 ▶ if ( $max_gain > 0.0000001 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_gain > "1e-07") 275 821 1096
139 # merge node to best community
140
141 275 _switch_community($self, $node_i, $best_community_id);
142 275 $has_improved = 1;
143 275 $has_changed = 1;
144 }
145
146 1096 undef $node_i->{_k_i_in};
147 1096 undef $node_i->{_k_i_new};
148 }
149 } while ($has_improved);
150  
151 3 _consolidate_community_structure($self);
152  
153 3 ▶ return map { $_->subcommunities } @communities unless $has_changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_changed 1 2 3
154  
155 2 $self->{louvain_levels} += 1;
156
157
158 2 PHASE_TWO:
159  
160 # merge communities to new nodes in a new network
161 my $next_level_network = _create_next_level_network($self);
162
163 # apply the algorithm recursively to the new network
164 2 my @new_community_structure = $next_level_network->identify_communities_with_louvain($level + 1);
165
166 2 $self->{louvain_levels} = $next_level_network->{louvain_levels};
167 2 ▶ return @new_community_structure if $level > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 0 1 1 2
168  
169 # build the hierarchical community structure
170 1 my @community_levels = _build_hierarchy(@new_community_structure);
171  
172 1 $self->{community_levels} = \@community_levels;
173 1 $self->{communities_ref} = \@new_community_structure;
174 1 return int $self->communities;
175 }
176  
177  
178 4966 ▶sub _modularity_gain {
Subroutine _modularity_gain: called CC=1 SCAR=0.0
179 4966 my ($net, $node_i, $new_community_id) = @_;
180 4966 my $new_community = $net->{communities_ref}->[$new_community_id];
181  
182 4966 my $k_i_new = $node_i->{_k_i_new}->{ $new_community_id };
183  
184 my $neighbour_community_module = _module_value(
185 $new_community->w_in,
186 $new_community->w_tot,
187 $net->{total_weight},
188 4966 );
189
190 my $new_neighbour_community_module = _module_value(
191 $new_community->w_in + $k_i_new,
192 $new_community->w_tot + $node_i->{_k_i} - $k_i_new,
193 $net->{total_weight},
194 4966 );
195  
196 4966 return $new_neighbour_community_module - $neighbour_community_module;
197 }
198  
199  
200 12124 ▶sub _module_value {
Subroutine _module_value: called CC=1 SCAR=0.0
201 12124 my ($w_in, $w_tot, $w_net) = @_;
202 12124 return $w_in / $w_net - ( ( $w_in + $w_tot ) / ( 2 * $w_net ) ) ** 2;
203 }
204  
205  
206 275 ▶sub _switch_community {
Subroutine _switch_community: called CC=1 SCAR=0.0
207 275 my ($net, $node, $new_community_id) = @_;
208 275 my $current_community = $net->{communities_ref}->[ $node->community ];
209 275 my $new_community = $net->{communities_ref}->[$new_community_id];
210
211 275 @{ $current_community->members_ref } = grep {
212 275 $_ != $node
213 } $current_community->members;
214  
215 275 push @{ $new_community->{members_ref} }, $node;
216  
217 275 $node->{community} = $new_community_id;
218
219 275 $current_community->{w_in} -= $node->{_k_i_in};
220 275 $current_community->{w_tot} -= $node->{_k_i} - $node->{_k_i_in};
221
222 275 $new_community->{w_in} += $node->{_k_i_new}->{ $new_community_id };
223 275 $new_community->{w_tot} += $node->{_k_i} - $node->{_k_i_new}->{ $new_community_id };
224 }
225  
226  
227 3 ▶sub _consolidate_community_structure {
Subroutine _consolidate_community_structure: called CC=4 SCAR=13.9
228 3 my ($self) = @_;
229  
230 3 @{$self->{communities_ref}} = grep {
231 3 int $_->members > 0
232 } $self->communities;
233  
234  
235 3 my $index = 0;
236 3 foreach my $community ($self->communities) {
237 14 $community->{index} = $index;
238  
239 14 foreach my $member ($self->{communities_ref}->[$index]->members) {
240 190 $member->{community} = $index;
241 }
242  
243 14 $index += 1;
244  
245 14 ▶ next if $self->{louvain_levels} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"louvain_levels"} == 0 6 8 14
246  
247 my @subcommunities = map {
248 8 $_->{subcommunity}
249 10 } $community->members;
250
251 8 $community->{subcommunities} = \@subcommunities;
252 8 undef $community->{members_ref};
253 }
254
255 # for my $index (0 .. int($self->communities) - 1) {
256 # $self->{communities_ref}->[$index]->{index} = $index;
257 # foreach my $member ($self->{communities_ref}->[$index]->members) {
258 # $member->{community} = $index;
259 # }
260 # }
261 }
262  
263  
264 2 ▶sub _create_next_level_network {
Subroutine _create_next_level_network: called CC=7 SCAR=19.5
265 2 my ($net) = @_;
266
267 2 my $next_level_network = SNA::Network->new;
268 2 foreach my $community ($net->communities) {
269 10 my $new_node = $next_level_network->create_node(
270 community => $community->index,
271 subcommunity => $community,
272 );
273 10 $next_level_network->create_edge(
274 source_index => $new_node->index,
275 target_index => $new_node->index,
276 weight => $community->w_in,
277 );
278 }
279
280 2 my $nc = int $net->communities;
281 2 my @edge_weights = map { {} } 1 .. $nc;
282 2 foreach my $edge ($net->edges) {
283 1478 $edge_weights[ $edge->source->community ]->{ $edge->target->community } += $edge->weight;
284 }
285
286 2 foreach my $meta_node ($next_level_network->nodes) {
287 PEERS:
288 10 foreach my $peer_node ($next_level_network->nodes) {
289 52 ▶ next PEERS if $meta_node == $peer_node;
Branch: 1 branch
BranchTrueFalseTotal
if $meta_node == $peer_node 10 42 52
290 42 my $weight = $edge_weights[ $meta_node->index ]->{ $peer_node->index };
291
292 42 ▶ if ($weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight) 36 6 42
293 36 $next_level_network->create_edge(
294 source_index => $meta_node->index,
295 target_index => $peer_node->index,
296 weight => $weight,
297 );
298 }
299 }
300 }
301
302 2 return $next_level_network;
303 }
304  
305  
306 2 ▶sub _build_hierarchy {
Subroutine _build_hierarchy: called CC=2 SCAR=6.9
307 2 my (@communities) = @_;
308  
309 2 ▶ return SNA::Network::CommunityStructure->new(@communities) if $communities[0]->level == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $communities[0]->level == 0 1 1 2
310  
311 my @subcommunities = map {
312 1 $_->subcommunities
313 } @communities;
314  
315 1 return _build_hierarchy(@subcommunities), SNA::Network::CommunityStructure->new(@communities);
316 }
317  
318  
319 =head1 AUTHOR
320  
321 Darko Obradovic, C<< <dobradovic at gmx.de> >>
322  
323 =head1 BUGS
324  
325 Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through
326 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll
327 automatically be notified of progress on your bug as I make changes.
328  
329  
330  
331  
332 =head1 SUPPORT
333  
334 You can find documentation for this module with the perldoc command.
335  
336 perldoc SNA::Network
337  
338  
339 You can also look for information at:
340  
341 =over 4
342  
343 =item * RT: CPAN's request tracker
344  
345 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network>
346  
347 =item * AnnoCPAN: Annotated CPAN documentation
348  
349 L<http://annocpan.org/dist/SNA-Network>
350  
351 =item * CPAN Ratings
352  
353 L<http://cpanratings.perl.org/d/SNA-Network>
354  
355 =item * Search CPAN
356  
357 L<http://search.cpan.org/dist/SNA-Network>
358  
359 =back
360  
361  
362 =head1 ACKNOWLEDGEMENTS
363  
364  
365 =head1 COPYRIGHT & LICENSE
366  
367 Copyright 2012 Darko Obradovic, all rights reserved.
368  
369 This program is free software; you can redistribute it and/or modify it
370 under the same terms as Perl itself.
371  
372  
373 =cut
374  
375 15 1;
376  
« Network/Algorithm/Betweenness.pm ↑ Summary Network/Algorithm/Cores.pm »