Network.pm

filter: Statementstmt 100% 138 / 138 Branchbran 75.0% 3 / 4 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 35 / 35 Podpod 100% 12 / 12 totaltotal 98.4% 194 / 197 CC 11 SCAR 6.1
CC
11
Cov
99%
delete_nodes
5.0
delete_edges
4.0
create_node
2.0
CRAP 1.8
« Network/Node.pm ↑ Summary Network/CommunityStructure.pm »
1 package SNA::Network;
2  
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use Object::Tiny::XS qw(community_levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 15 ▶use SNA::Network::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use SNA::Network::Edge;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use SNA::Network::Filter::Pajek;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use SNA::Network::Filter::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use SNA::Network::Algorithm::Betweenness;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use SNA::Network::Algorithm::Connectivity;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use SNA::Network::Algorithm::Cores;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use SNA::Network::Algorithm::HITS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use SNA::Network::Algorithm::Louvain;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 15 ▶use SNA::Network::Algorithm::PageRank;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 15 ▶use SNA::Network::Generator::ByDensity;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use SNA::Network::Generator::ConfigurationModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 ▶use SNA::Network::Generator::MCMC;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 15 ▶use Module::List::Pluggable qw(import_modules);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 15 import_modules('SNA::Network::Plugin');
30  
31  
32 =head1 NAME
33  
34 SNA::Network - A toolkit for Social Network Analysis
35  
36 =head1 VERSION
37  
38 Version 0.21
39  
40 =cut
41  
42 15 our $VERSION = '0.21';
43  
44  
45 =head1 SYNOPSIS
46  
47 Quick summary of what the module does.
48  
49 use SNA::Network;
50  
51 my $net = SNA::Network->new();
52 $net->create_node_at_index(index => 0, name => 'A');
53 $net->create_node_at_index(index => 1, name => 'B');
54 $net->create_edge(source_index => 0, target_index => 1, weight => 1);
55 ...
56
57 =head1 DESCRIPTION
58  
59 SNA::Network is a bundle of modules for network algorithms,
60 specifically designed for the needs of Social Network Analysis (SNA),
61 but can be used for any other graph algorithms of course.
62  
63 It represents a standard directed and weighted network,
64 which can also be used as an undirected and/or unweighted network of course.
65 It is freely extensible by using own hash entries.
66  
67 Data structures have been designed for SNA-typical sparse network operations,
68 and consist of Node and Edge objects, linked via references to each other.
69  
70 Functionality is implemented in sub-modules in the B<SNA::Network> namespace,
71 and all methods are imported into B<Network.pm>.
72 So you can read the documentation in the sub-modules and call the methods
73 from your B<SNA::Network> instance.
74  
75 Methods are called with named parameter style, e.g.
76  
77 $net->method( param1 => value1, param2 => value2, ...);
78
79 Only in cases, where methods have only one parameter,
80 this one is passed by value.
81  
82 This module was implemented mainly because I had massive problems
83 understanding the internal structures of Perl's L<Graph> module.
84 Despite it uses lots of arrays instead of hashes for attributes
85 and bit setting for properties, it was terribly slow for my purposes,
86 especially in network manipulation (consistent node removal).
87 It currently has much more features and plugins though,
88 and is suitable for different network types.
89 This package is focussing on directed networks only,
90 with the possibility to model undirected ones as well.
91  
92  
93 =head1 METHODS
94  
95 =head2 new
96  
97 Creates a new empty network.
98 There are no parameters.
99 After creation, use methods to add nodes and edges,
100 or load a network from a file.
101  
102 =cut
103  
104 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
105 25 my ($package) = @_;
106 25 return bless { nodes => [], edges => [], communities_ref => [] }, $package;
107 }
108  
109  
110 =head2 create_node_at_index
111  
112 Creates a node at the given index.
113 Pass node attributes as additional named parameters, index is mandatory.
114 Returns the created L<SNA::Network::Node> object.
115  
116 =cut
117  
118 660 ▶sub create_node_at_index {
Subroutine create_node_at_index: called CC=1 SCAR=0.0
Pod documented
119 660 my ($self, %node_attributes) = @_;
120 660 return $self->{nodes}->[$node_attributes{index}] = SNA::Network::Node->new(%node_attributes);
121 }
122  
123  
124 =head2 create_node
125  
126 Creates a node at the next index.
127 Pass node attributes as additional named parameters, index is forbidden.
128 Returns the created L<SNA::Network::Node> object with the right index field.
129  
130 =cut
131  
132 12 ▶sub create_node {
Subroutine create_node: called CC=2 SCAR=7.0
Pod documented
133 12 my ($self, %node_attributes) = @_;
134 12 ▶ croak "illegally passed index to create_node method" if defined $node_attributes{index};
Branch: 1 branch
BranchTrueFalseTotal
if defined $node_attributes{"index"} 0 12 12
135 12 my $index = int $self->nodes;
136 12 return $self->create_node_at_index( index => $index, %node_attributes );
137 }
138  
139  
140 =head2 create_edge
141  
142 Creates a new edge between nodes with the given B<source_index> and B<target_index>.
143 A B<weight> is optional, it defaults to 1.
144 Pass any additional attributes as key/value pairs.
145 Returns the created L<SNA::Network::Edge> object.
146  
147 =cut
148  
149 3956 ▶sub create_edge {
Subroutine create_edge: called CC=1 SCAR=0.0
Pod documented
150 3956 my ($self, %params) = @_;
151 3956 my $source_node = $self->node_at_index( $params{source_index} );
152 3956 my $target_node = $self->node_at_index( $params{target_index} );
153 3956 my $index = int @{ $self->{edges} };
154 3956 my $weight = $params{weight};
155 3956 delete $params{source_index};
156 3956 delete $params{target_index};
157 3956 delete $params{weight};
158 3956 my $edge = SNA::Network::Edge->new(
159 source => $source_node,
160 target => $target_node,
161 weight => $weight,
162 index => $index,
163 %params,
164 );
165 3956 push @{ $self->{edges} }, $edge;
166 3956 push @{ $source_node->{outgoing_edges} }, $edge;
167 3956 weaken $source_node->{outgoing_edges}->[-1];
168 3956 push @{ $target_node->{incoming_edges} }, $edge;
169 3956 weaken $target_node->{incoming_edges}->[-1];
170 3956 return $edge;
171 }
172  
173  
174 =head2 nodes
175  
176 Returns the array of L<SNA::Network::Node> objects belonging to this network.
177  
178 =cut
179  
180 177 ▶sub nodes {
Subroutine nodes: called CC=1 SCAR=0.0
Pod documented
181 177 my ($self) = @_;
182 177 return @{ $self->{nodes} };
183 }
184  
185  
186 =head2 node_at_index
187  
188 Returns the L<SNA::Network::Node> object at the given index.
189  
190 =cut
191  
192 8111 ▶sub node_at_index {
Subroutine node_at_index: called CC=1 SCAR=0.0
Pod documented
193 8111 my ($self, $index) = @_;
194 8111 return $self->{nodes}->[$index];
195 }
196  
197  
198 =head2 edges
199  
200 Returns the array of L<SNA::Network::Edge> objects belonging to this network.
201  
202 =cut
203  
204 21119 ▶sub edges {
Subroutine edges: called CC=1 SCAR=0.0
Pod documented
205 21119 my ($self) = @_;
206 21119 return @{ $self->{edges} };
207 }
208  
209  
210 =head2 total_weight
211  
212 Returns the sum of all weights of the L<SNA::Network::Edge> objects belonging to this network.
213  
214 =cut
215  
216 4 ▶sub total_weight {
Subroutine total_weight: called CC=1 SCAR=0.0
Pod documented
217 4 my ($self) = @_;
218 4 return sum map { $_->weight } $self->edges;
219 }
220  
221  
222 =head2 delete_nodes
223  
224 Delete the passed node objects.
225 These have to be sorted by index!
226 All related edges get deleted as well.
227 Indexes get restored after this operation.
228  
229 =cut
230  
231 1 ▶sub delete_nodes {
Subroutine delete_nodes: called CC=5 SCAR=16.1
Pod documented
232 1 my ($self, @nodes_to_delete) = @_;
233 # nodes have to be sorted by index!
234
235 1 foreach (@nodes_to_delete) {
236 2 $self->delete_edges( $_->edges() );
237 }
238
239 $self->{nodes} = [ grep {
240 1 ▶ ($nodes_to_delete[0] && $_ == $nodes_to_delete[0]) ? shift @nodes_to_delete && 0 : 1
Branch: 1 branch
BranchTrueFalseTotal
$nodes_to_delete[0] && $_ == $nodes_to_delete[0] ? : 2 5 7
Condition80%4/5
$nodes_to_delete[0] and $_ == $nodes_to_delete[0]
!ll&&!rl&&r
232
shift @nodes_to_delete and 0
!ll
02
MC/DC100%2/2
$nodes_to_delete[0] and $_ == $nodes_to_delete[0]
$nodes_to_delete[0] $_ == $nodes_to_delete[0]
MC/DC0%0/1
shift @nodes_to_delete and 0
shift @nodes_to_delete
Truth table100%3/3
A and B
A= $nodes_to_delete[0]B= $_ == $nodes_to_delete[0]
ABresult
0X0
100
111
Truth table50%1/2
A
A= shift @nodes_to_delete
Aresult
00
11
241 } $self->nodes() ];
242 1 $self->_restore_node_indexes();
243 }
244  
245  
246 1 ▶sub _restore_node_indexes {
Subroutine _restore_node_indexes: called CC=2 SCAR=6.9
247 1 my ($self) = @_;
248 1 my $i = 0;
249 1 foreach ($self->nodes()) {
250 5 $_->{index} = $i++;
251 5 undef $_->{weak_component_index};
252 }
253 }
254  
255  
256 =head2 delete_edges
257  
258 Delete the passed edge objects.
259  
260 =cut
261  
262 3 ▶sub delete_edges {
Subroutine delete_edges: called CC=4 SCAR=13.9
Pod documented
263 3 my ($self, @edges_to_delete) = @_;
264
265 3 foreach my $edge (@edges_to_delete) {
266
267 # delete references in endpoint nodes
268  
269 $edge->source->{outgoing_edges} = [ grep {
270 8 $_ != $edge
271 } $edge->source->outgoing_edges ];
272
273 8 for (0 .. int $edge->source->outgoing_edges - 1) {
274 5 weaken $edge->source->{outgoing_edges}->[$_];
275 }
276  
277 $edge->target->{incoming_edges} = [ grep {
278 8 $_ != $edge
279 } $edge->target->incoming_edges ];
280
281 8 for (0 .. int $edge->target->incoming_edges - 1) {
282 6 weaken $edge->target->{incoming_edges}->[$_];
283 }
284
285 # delete references in edge index
286 $self->{edges} = [ grep {
287 8 $_ != $edge
288 } $self->edges ];
289 }
290
291
292 3 $self->_restore_edge_indexes;
293 }
294  
295  
296 3 ▶sub _restore_edge_indexes {
Subroutine _restore_edge_indexes: called CC=2 SCAR=6.9
297 3 my ($self) = @_;
298 3 my $i = 0;
299 3 foreach ($self->edges) {
300 9 $_->{index} = $i++;
301 }
302 }
303  
304  
305 =head2 community_levels
306  
307 Returns an array reference containing L<SNA::Network::CommunityStructure> objects, which were identified by a previously executed community identification algorithm, usually the L<SNA::Network::Algorithm::Louvain> algorithm.
308 With a hierarchical identification algorithm, the array containts the structures of the different levels from the finest-granular structure at index 0 to the most coarsely-granular structure at the last index.
309 If no such algorithm had been executed, it returns C<undef>.
310  
311  
312 =head2 communities
313  
314 Return a list of L<SNA::Network::Community> objects, which were identified by a previously executed community identification algorithm, usually the L<SNA::Network::Algorithm::Louvain> algorithm.
315 If no such algorithm was executed, returns an empty list.
316  
317 =cut
318  
319 15 ▶sub communities {
Subroutine communities: called CC=1 SCAR=0.0
Pod documented
320 15 my ($self) = @_;
321 15 return @{ $self->{communities_ref} };
322 }
323  
324  
325 =head2 modularity
326  
327 Return the modularity value based on the current communities of the network,
328 which were identified by a previously executed community identification algorithm,
329 usually the L<SNA::Network::Algorithm::Louvain> algorithm.
330 If no such algorithm was executed, returns C<undef>.
331  
332 =cut
333  
334 1 ▶sub modularity {
Subroutine modularity: called CC=1 SCAR=0.0
Pod documented
335 1 my ($self) = @_;
336 1 return sum map { $_->module_value } $self->communities;
337 }
338  
339  
340  
341 =head1 PLUGIN SYSTEM
342  
343 This package can be extenden with plugins,
344 which gives you the possibility, to add your own algorithms, filters, and so on.
345 Each class found in the namespace B<SNA::Network::Plugin>
346 will be imported into the namespace of B<SNA::Network>,
347 and each class found in the namespace B<SNA::Network::Node::Plugin>
348 will be imported into the namespace of B<SNA::Network::Node>.
349 With this mechanism, you can add methods to these classes.
350  
351 For example:
352  
353 package SNA::Network::Plugin::Foo;
354  
355 use warnings;
356 use strict;
357  
358 require Exporter;
359 use base qw(Exporter);
360 our @EXPORT = qw(foo);
361  
362 sub foo {
363 my ($self) = @_;
364 # $self is a reference to our network object
365 # do something with it here
366 ...
367 }
368  
369 adds a new foo method to B<SNA::Network>.
370  
371  
372 =head1 SEE ALSO
373  
374 =over 4
375  
376 =item * L<SNA::Network::Node>
377  
378 =item * L<SNA::Network::Edge>
379  
380 =item * L<SNA::Network::Community>
381  
382 =item * L<SNA::Network::Filter::Pajek>
383  
384 =item * L<SNA::Network::Filter::Guess>
385  
386 =item * L<SNA::Network::Algorithm::Betweenness>
387  
388 =item * L<SNA::Network::Algorithm::Connectivity>
389  
390 =item * L<SNA::Network::Algorithm::Cores>
391  
392 =item * L<SNA::Network::Algorithm::HITS>
393  
394 =item * L<SNA::Network::Algorithm::Louvain>
395  
396 =item * L<SNA::Network::Algorithm::PageRank>
397  
398 =item * L<SNA::Network::Generator::ByDensity>
399  
400 =item * L<SNA::Network::Generator::ConfigurationModel>
401  
402 =item * L<SNA::Network::Generator::MCMC>
403  
404 =back
405  
406  
407 =head1 AUTHOR
408  
409 Darko Obradovic, C<< <dobradovic at gmx.de> >>
410  
411  
412 =head1 BUGS
413  
414 Please report any bugs or feature requests to C<bug-sna-network at rt.cpan.org>, or through
415 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll
416 automatically be notified of progress on your bug as I make changes.
417  
418  
419 =head1 SUPPORT
420  
421 You can find documentation for this module with the perldoc command.
422  
423 perldoc SNA::Network
424  
425  
426 You can also look for information at:
427  
428 =over 4
429  
430 =item * RT: CPAN's request tracker
431  
432 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network>
433  
434 =item * AnnoCPAN: Annotated CPAN documentation
435  
436 L<http://annocpan.org/dist/SNA-Network>
437  
438 =item * CPAN Ratings
439  
440 L<http://cpanratings.perl.org/d/SNA-Network>
441  
442 =item * Search CPAN
443  
444 L<http://search.cpan.org/dist/SNA-Network>
445  
446 =back
447  
448  
449 =head1 ACKNOWLEDGEMENTS
450  
451 This module has been developed as part of my work at the
452 German Research Center for Artificial Intelligence (DFKI) L<http://www.dfki.de/>.
453  
454  
455 =head1 COPYRIGHT & LICENSE
456  
457 Copyright 2009 Darko Obradovic, all rights reserved.
458  
459 This program is free software; you can redistribute it and/or modify it
460 under the same terms as Perl itself.
461  
462  
463 =cut
464  
465 15 1; # End of SNA::Network
« Network/Node.pm ↑ Summary Network/CommunityStructure.pm »