blib/lib/SNA/Network.pm

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