blib/lib/SNA/Network/Filter/Guess.pm

filter: Statementstmt 100% 74 / 74 Branchbran 63.6% 14 / 22 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 89.7% 105 / 117 CC 18 SCAR 19.3
CC
18
Cov
90%
load_from_gdf
11.2
save_to_gdf
8.3
BEGIN
1.0
CRAP 6.9
« blib/lib/SNA/Network/Algorithm/Cores.pm ↑ Summary blib/lib/SNA/Network/Filter/Pajek.pm »
1 package SNA::Network::Filter::Guess;
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(load_from_gdf new_from_gdf save_to_gdf);
9  
10 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use List::MoreUtils qw(pairwise);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 =head1 NAME
16  
17 SNA::Network::Filter::Guess - load and save networks from/to Guess .gdf files
18  
19  
20 =head1 SYNOPSIS
21  
22 use SNA::Network;
23  
24 my $net = SNA::Network->new();
25 $net->load_from_gdf($filename);
26 ...
27 # shortcut
28 my $net = SNA::Network->new_from_gdf($filename);
29 ...
30 $net->save_to_gdf(filename => $filename, edge_fields => ['weight']);
31  
32  
33 =head1 DESCRIPTION
34  
35 This enables import/export to the GUESS data format.
36 See L<http://guess.wikispot.org/The_GUESS_.gdf_format> for details about the format.
37  
38  
39 =head1 METHODS
40  
41 The following methods are added to L<SNA::Network>.
42  
43 =head2 load_from_gdf
44  
45 load a network from a passed filename.
46 Nodes and edges are created with the fields specified in the file.
47 They are accessible as hash entries of the objects.
48  
49 =cut
50  
51 sub load_from_gdf {
52 3 ▶ my ($self, $filename) = @_;
Subroutine load_from_gdf: called CC=11 SCAR=24.2
Pod documented
53 3 ▶ open my $GDF_FILE, '<', $filename or croak "cannot open '$filename': $OS_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $GDF_FILE, "<", $filename or croak("cannot open '${filename}': $SNA::Network::Filter::Guess::OS_ERROR\n") 0 3 3
54
55 3 my $line;
56
57 # search start of node definitions
58 START:
59 3 ▶ while (defined($line = <$GDF_FILE>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $GDF_FILE)) 3 0 3
60 3 ▶ last START if $line =~ m/^nodedef>/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^nodedef>/ 3 0 3
61 }
62 3 $line =~ m/^nodedef> (.+)$/;
63 3 my ($name, $label, @fields) = split /,/, $1;
64 3 foreach (@fields) {
65 6 s/\s*(\w+).*/$1/
66 }
67
68 # read nodes and create graph nodes
69 NODES:
70 3 ▶ while (defined($line = <$GDF_FILE>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $GDF_FILE)) 15 0 15
71 15 ▶ last NODES if $line =~ m/^edgedef> (.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^edgedef> (.+)$/ 3 12 15
72  
73 #chomp $line;
74 12 $line =~ s/\s//g;
75 12 my ($node_number, $node_name, @field_values) = split ',', $line;
76
77 $self->create_node_at_index(
78 index => _extract_index($node_number),
79 name => $node_name,
80 12 ▶ (pairwise { ($a, $b) } @fields, @field_values)
Subroutine __ANON__: called CC=1 SCAR=0.0
81 );
82 }
83
84 # read arcs and create graph edges
85 3 $line =~ m/^edgedef> (.+)$/;
86 3 my ($node1, $node2);
87 3 ($node1, $node2, @fields) = split /,/, $1;
88 3 foreach (@fields) {
89 6 s/\s*(\w+).*/$1/
90 }
91
92 EDGES:
93 3 ▶ while (defined($line = <$GDF_FILE>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $GDF_FILE)) 20 1 21
94 20 ▶ last EDGES unless $line =~ m/\s*\w+,\s*\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s*\w+,\s*\w+/ 2 18 20
95
96 18 $line =~ s/\s//g;
97 18 my ($source, $target, @field_values) = split ',', $line;
98 $self->create_edge(
99 source_index => _extract_index($source),
100 target_index => _extract_index($target),
101 18 ▶ (pairwise { ($a, $b) } @fields, @field_values)
Subroutine __ANON__: called CC=1 SCAR=0.0
102 );
103 }
104  
105 3 ▶ close $GDF_FILE or croak "cannot close '$filename': $OS_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
close $GDF_FILE or croak("cannot close '${filename}': $SNA::Network::Filter::Guess::OS_ERROR\n") 0 3 3
106 }
107  
108  
109 =head2 new_from_gdf
110  
111 Returns a newly created network from a passed filename.
112 Nodes and edges are created with the fields specified in the file.
113 They are accessible as hash entries of the objects.
114  
115 =cut
116  
117 sub new_from_gdf {
118 1 ▶ my ($package, $filename) = @_;
Subroutine new_from_gdf: called CC=1 SCAR=0.0
Pod documented
119 1 my $net = $package->new;
120 1 $net->load_from_gdf($filename);
121 1 return $net;
122 }
123  
124  
125  
126 =head2 save_to_gdf
127  
128 Saves the current network to a GDF file.
129 The named parameters are B<filename>,
130 and optionally array references in B<node_fields> and B<edge_fields>,
131 that will write the corresponding hash entries of the objects into the file.
132  
133 =cut
134  
135 sub save_to_gdf {
136 1 ▶ my ($self, %params) = @_;
Subroutine save_to_gdf: called CC=8 SCAR=21.2
Pod documented
137 1 ▶ croak "no filename passed to save_to_gdf!" unless $params{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"filename"} 0 1 1
138 1 ▶ $params{node_fields} ||= [];
Condition50%1/2
$params{"node_fields"} ||= []
l!l
10
MC/DC0%0/1
$params{"node_fields"} ||= []
$params{"node_fields"}
Truth table50%1/2
A
A= $params{"node_fields"}
Aresult
11
00
139 1 ▶ $params{edge_fields} ||= [];
Condition50%1/2
$params{"edge_fields"} ||= []
l!l
10
MC/DC0%0/1
$params{"edge_fields"} ||= []
$params{"edge_fields"}
Truth table50%1/2
A
A= $params{"edge_fields"}
Aresult
11
00
140  
141 1 ▶ open my $GDF_FILE, '>', $params{filename} or croak "cannot open '$params{filename}': $OS_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $GDF_FILE, ">", $params{"filename"} or croak(qq[cannot open '$params{"filename"}': $SNA::Network::Filter::Guess::OS_ERROR\n]) 0 1 1
142  
143 # process nodes
144 1 printf $GDF_FILE "nodedef> %s\n", join ',', ('name', 'label', @{ $params{node_fields} });
145  
146 1 foreach my $node ($self->nodes()) {
147 printf $GDF_FILE "%s\n", join(',',
148 'n' . ($node->index() + 1),
149 $node->{name},
150 4 map { $node->{$_} } @{ $params{node_fields} }
151 );
152 }
153  
154 # process edges
155 1 printf $GDF_FILE "edgedef> %s\n", join ',', qw(node1 node2 directed), @{ $params{edge_fields} };
156  
157 1 foreach my $edge ($self->edges()) {
158 printf $GDF_FILE "%s\n", join(',',
159 12 ( map { 'n' . ($_->index() + 1) } ($edge->source(), $edge->target()) ),
160 'true',
161 6 ( map { $edge->{$_} } @{ $params{edge_fields} } )
162 );
163 }
164
165 1 ▶ close $GDF_FILE or croak "cannot close '$params{filename}': $OS_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
close $GDF_FILE or croak(qq[cannot close '$params{"filename"}': $SNA::Network::Filter::Guess::OS_ERROR\n]) 0 1 1
166 }
167  
168  
169 sub _extract_index {
170 48 ▶ my ($node_name) = @_;
Subroutine _extract_index: called CC=1 SCAR=0.0
171 48 $node_name =~ m/\D+(\d+)/;
172 48 return $1 - 1;
173 }
174  
175  
176 =head1 AUTHOR
177  
178 Darko Obradovic, C<< <dobradovic at gmx.de> >>
179  
180 =head1 BUGS
181  
182 Please report any bugs or feature requests to C<bug-sna-network-filter-pajek at rt.cpan.org>, or through
183 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll
184 automatically be notified of progress on your bug as I make changes.
185  
186  
187  
188  
189 =head1 SUPPORT
190  
191 You can find documentation for this module with the perldoc command.
192  
193 perldoc SNA::Network
194  
195  
196 You can also look for information at:
197  
198 =over 4
199  
200 =item * RT: CPAN's request tracker
201  
202 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network>
203  
204 =item * AnnoCPAN: Annotated CPAN documentation
205  
206 L<http://annocpan.org/dist/SNA-Network>
207  
208 =item * CPAN Ratings
209  
210 L<http://cpanratings.perl.org/d/SNA-Network>
211  
212 =item * Search CPAN
213  
214 L<http://search.cpan.org/dist/SNA-Network>
215  
216 =back
217  
218  
219 =head1 COPYRIGHT & LICENSE
220  
221 Copyright 2009 Darko Obradovic, all rights reserved.
222  
223 This program is free software; you can redistribute it and/or modify it
224 under the same terms as Perl itself.
225  
226  
227 =cut
228  
229 15 1; # End of SNA::Network::Filter::Guess
230  
« blib/lib/SNA/Network/Algorithm/Cores.pm ↑ Summary blib/lib/SNA/Network/Filter/Pajek.pm »