blib/lib/SNA/Network/Algorithm/Louvain.pm

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