blib/lib/PDL/CCS/IO/LDAC.pm

filter: Statementstmt 100% 105 / 105 Branchbran 78.8% 41 / 52 Conditioncond 64.2% 18 / 28 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 84.5% 180 / 213 CC 37 SCAR 31.0
CC
37
Cov
89%
ccs_readldac
32.7
ccs_writeldac
9.0
BEGIN
1.0
CRAP 22.1
« blib/lib/PDL/CCS/Nd.pm ↑ Summary blib/lib/PDL/CCS/IO/PETSc.pm »
1 ## File: PDL::CCS::IO::LDAC.pm
2 ## Author: Bryan Jurish <moocow@cpan.org>
3 ## Description: LDA-C wrappers for PDL::CCS::Nd
4  
5 package PDL::CCS::IO::LDAC;
6 1 ▶use PDL::CCS::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PDL::CCS::Config qw(ccs_indx);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use PDL::CCS::Nd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PDL::CCS::IO::Common qw(:intern); ##-- for e.g. _ccsio_header_lines(), _ccsio_parse_header()
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use PDL::IO::Misc; ##-- for rcols(), wcols(), $PDL::IO::Misc::deftype
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fcntl qw(:seek); ##-- for rewinding
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '1.24.1';
17 1 our @ISA = ('PDL::Exporter');
18 1 our @EXPORT_OK =
19 (
20 qw(ccs_writeldac ccs_readldac),
21 );
22 1 our %EXPORT_TAGS =
23 (
24 Func => [@EXPORT_OK], ##-- respect PDL conventions (hopefully)
25 );
26  
27 ##======================================================================
28 ## pod: headers
29 =pod
30  
31 =head1 NAME
32  
33 PDL::CCS::IO::LDAC - LDA-C format text I/O for PDL::CCS::Nd
34  
35 =head1 SYNOPSIS
36  
37 use PDL;
38 use PDL::CCS::Nd;
39 use PDL::CCS::IO::LDAC;
40  
41 ##-- (Document x Term) matrix
42 $dtm = PDL::CCS::Nd->newFromWhich($which,$nzvals);
43
44 ccs_writeldac($dtm,"dtm.ldac"); # write a sparse LDA-C text file
45 $dtm2 = ccs_readldac("dtm.ldac"); # read a sparse LDA-C text file
46  
47 ###-- (Term x Document) matrix in document-primary format
48 $tdm = $dtm->xchg(0,1)->make_physically_indexed();
49
50 ccs_writeldac($tdm,"tdm.ldac", {transpose=>1});
51 $dtm2 = ccs_readldac("tdm.ldac", {transpose=>1});
52  
53 =cut
54  
55  
56 ##======================================================================
57 ## I/O utilities
58 =pod
59  
60 =head1 I/O Utilities
61  
62 =cut
63  
64 ##---------------------------------------------------------------
65 ## ccs_writeldac
66 =pod
67  
68 =head2 ccs_writeldac
69  
70 Write a 2d L<PDL::CCS::Nd|PDL::CCS::Nd> (Document x Term)
71 matrix as an LDA-C text file. If the C<transpose> option is specified and true,
72 the input matrix C<$ccs> is treated as as a (Term x Document) matrix,
73 and output lines correspond to logical dimension 1 of C<$ccs>. Otherwise,
74 output lines correspond to logical dimension 0 of C<$ccs>, which is expected
75 to be a (Document x Term) matrix.
76  
77 ccs_writeldac($ccs,$filename_or_fh)
78 ccs_writeldac($ccs,$filename_or_fh,\%opts)
79  
80 Options %opts:
81  
82 header => $bool, ##-- do/don't write a header to the output file (default=do)
83 transpose => $bool, ##-- treat input $ccs as (Term x Document) matrix (default=don't)
84  
85 =cut
86  
87 1 *PDL::ccs_writeldac = *PDL::CCS::Nd::writeldac = \&ccs_writeldac;
88 4 ▶sub ccs_writeldac {
Subroutine ccs_writeldac: called CC=9 SCAR=22.0
Pod documented
89 4 my ($ccs,$file,$opts) = @_;
90 4 ▶ my %opts = %{$opts||{}};
Condition100%2/2
$opts || {}
l!l
31
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
91 4 ▶ $opts{header} = 1 if (!defined($opts{header}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"header"} 2 2 4
92  
93 ##-- sanity check(s)
94 4 ▶ confess("ccs_writeldac(): input matrix must be physically indexed 2d!")
Branch: 1 branch
BranchTrueFalseTotal
if $ccs->pdims->nelem != 2 0 4 4
95 if ($ccs->pdims->nelem != 2);
96  
97 ##-- open output file
98 4 ▶ my $fh = _ccsio_open($file,'>')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _ccsio_open($file, ">") or confess("ccs_writeldac(): open failed for output file '${file}': $!") 4 0 4
99 or confess("ccs_writeldac(): open failed for output file '$file': $!");
100 #binmode($fh,':raw');
101 4 local $,='';
102  
103 ##-- maybe print header
104 4 ▶ if ($opts{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"header"}) 2 2 4
105 print $fh
106 ("%%LDA-C sparse matrix file; see http://www.cs.princeton.edu/~blei/lda-c/readme.txt\n",
107 2 (map {("%", __PACKAGE__, " $_")} @{_ccsio_header_lines($ccs)}),
108 );
109 }
110  
111 ##-- transpose?
112 4 ▶ my ($ddim,$tdim) = $opts{transpose} ? (1,0) : (0,1);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'transpose'} ? : 2 2 4
113  
114 ##-- convert to lda-c format: use ptr()
115 4 my ($ptr,$pi2nzi) = $ccs->ptr($ddim);
116 4 my $nd = $ptr->nelem-1;
117 4 my $ix = $ccs->_whichND;
118 4 my $nz = $ccs->_nzvals;
119 4 my ($di,$i,$j,$nzi);
120 4 ▶ for ($di=0; $di < $nd; ++$di) {
Branch: 1 branch
BranchTrueFalseTotal
if ($di < $nd) 22 4 26
121 22 ($i,$j) = ($ptr->at($di),$ptr->at($di+1));
122 22 $nzi = $pi2nzi->slice("$i:".($j-1));
123 22 print $fh join(' ', ($j-$i), map {$ix->at($tdim,$_).":".$nz->at($_)} $nzi->list), "\n";
124 }
125  
126 ##-- cleanup
127 4 ▶ _ccsio_close($file,$fh)
Branch: 1 branch
BranchTrueFalseTotal
_ccsio_close($file, $fh) or confess("ccs_writeldac(): close failed for output file '${file}': $!") 4 0 4
128 or confess("ccs_writeldac(): close failed for output file '$file': $!");
129  
130 4 return 1;
131 }
132  
133  
134 ##---------------------------------------------------------------
135 ## ccs_readldac
136 =pod
137  
138 =head2 ccs_readldac
139  
140 Read a 2d (Document x Term) matrix from an LDA-C text file as a
141 L<PDL::CCS::Nd|PDL::CCS::Nd> object.
142 If the C<transpose> option is specified and true,
143 the output matrix C<$ccs> will be a (Term x Document) matrix,
144 and input lines correspond to logical dimension 1 of C<$ccs>. Otherwise,
145 input lines correspond to logical dimension 0 of C<$ccs>, which will be
146 returned as a (Document x Term) matrix.
147  
148 $ccs = ccs_readldac($filename_or_fh)
149 $ccs = ccs_readldac($filename_or_fh,\%opts)
150  
151 Options %opts:
152  
153 header => $bool, ##-- do/don't try to read header data from the output file (default=do)
154 type => $type, ##-- value datatype (default: from header or $PDL::IO::Misc::deftype)
155 transpose => $bool, ##-- generate a (Term x Document) matrix (default=don't)
156 sorted => $bool, ##-- assume input is lexicographically sorted (only if not transposed; default=don't)
157  
158 =cut
159  
160 1 *PDL::ccs_readldac = *PDL::CCS::Nd::readldac = \&ccs_readldac;
161 4 ▶sub ccs_readldac {
Subroutine ccs_readldac: called CC=29 SCAR=34.9
Pod documented
162 4 ▶ shift if (UNIVERSAL::isa($_[0],'PDL') || UNIVERSAL::isa($_[0],'PDL::CCS::Nd'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd") 0 4 4
Condition33%1/3
UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
l!l&&r!l&&!r
004
MC/DC0%0/2
UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
UNIVERSAL::isa($_[0], "PDL") UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
Truth table33%1/3
A or B
A= UNIVERSAL::isa($_[0], "PDL")B= UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
ABresult
1X1
011
000
163 4 my ($file,$opts) = @_;
164 4 ▶ my %opts = %{$opts||{}};
Condition100%2/2
$opts || {}
l!l
31
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
165 4 ▶ $opts{header} = 1 if (!defined($opts{header}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"header"} 2 2 4
166  
167 ##-- open input file
168 4 ▶ my $fh = _ccsio_open($file,'<')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _ccsio_open($file, "<") or confess("ccs_readldac(): open failed for input file '${file}': $!") 4 0 4
169 or confess("ccs_readldac(): open failed for input file '$file': $!");
170  
171 ##-- maybe scan for ccs header
172 4 my $header;
173 4 ▶ if ($opts{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'header'}) { } 2 2 4
174 ##-- scan initial comments for CCS header
175 2 my @hlines = qw();
176 2 ▶ while (defined($_=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 14 0 14
177 14 chomp;
178 14 ▶ if (/^[%\#](\S+) (.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^[%\#](\S+) (.*)$/) { } 12 2 14
elsif (not /^[%\#]/) { } 2 0 2
179 12 ▶ push(@hlines,$2) if (substr($_,1,length(__PACKAGE__)) eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 1, 18) eq __PACKAGE__ 10 2 12
180 } elsif (!/^[%\#]/) {
181 2 last;
182 }
183 }
184 2 $header = _ccsio_parse_header(\@hlines);
185 } else {
186 2 $header = {};
187 }
188  
189 ##-- get value datatype
190 4 ▶ my $type = $opts{type} || $header->{iotype} || $PDL::IO::Misc::deftype;
Condition66%4/6
$opts{'type'} || $header->{'iotype'} || $PDL::IO::Misc::deftype
l!l&&r!l&&!r
220
$opts{"type"} or $header->{"iotype"}
l!l&&r!l&&!r
022
MC/DC0%0/3
$opts{'type'} || $header->{'iotype'} || $PDL::IO::Misc::deftype
$opts{"type"} $header->{"iotype"} $PDL::IO::Misc::deftype
Truth table50%2/4
A or B || C
A= $opts{"type"}B= $header->{"iotype"}C= $PDL::IO::Misc::deftype
ABCresult
1XX1
01X1
0011
0000
191 4 ▶ $type = PDL->can($type)->() if (defined($type) && !ref($type) && PDL->can($type));
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and not ref $type and "PDL"->can($type) 2 2 4
Condition66%4/6
defined $type and not ref $type and "PDL"->can($type)
!ll&&!rl&&r
202
defined $type and not ref $type
!ll&&!rl&&r
022
MC/DC33%1/3
defined $type and not ref $type and "PDL"->can($type)
defined $type not ref $type "PDL"->can($type)
Truth table50%2/4
A and not(B) and C
A= defined $typeB= not ref $typeC= "PDL"->can($type)
ABCresult
0XX0
10X0
1100
1111
192 4 ▶ $type = $PDL::IO::Misc::deftype if (!ref($type));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $type 0 4 4
193  
194 ##-- get nnz (per doc)
195 4 ▶ seek($fh,0,SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or confess("ccs_readldac(): seek() failed for input file '${file}': $!") 4 0 4
196 or confess("ccs_readldac(): seek() failed for input file '$file': $!");
197 4 my $nnz0 = PDL->rcols($fh, [0], { TYPES=>[ccs_indx()], IGNORE=>qr{^\s*[^0-9]} });
198 4 my $nnz = $nnz0->sum;
199 4 my $nlines = $nnz0->nelem;
200 4 undef($nnz0);
201  
202 ##-- allocate output pdls
203 4 my $ix = zeroes(ccs_indx(), 2,$nnz);
204 4 my $nz = zeroes($type, $nnz+1);
205  
206 ##-- process input
207 4 ▶ seek($fh,0,SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or confess("ccs_readldac(): seek() failed for input file '${file}': $!") 4 0 4
208 or confess("ccs_readldac(): seek() failed for input file '$file': $!");
209 4 ▶ my ($dim0,$dim1) = $opts{transpose} ? (1,0) : (0,1);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'transpose'} ? : 2 2 4
210 4 my ($nzi,$i0,$i1,$f);
211 4 ▶ for ($nzi=$i0=0; $i0 < $nlines && $nzi < $nnz && defined($_=<$fh>); ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i0 < $nlines and $nzi < $nnz and defined($_ = readline $fh)) 34 4 38
Condition66%4/6
$i0 < $nlines and $nzi < $nnz and defined($_ = readline $fh)
!ll&&!rl&&r
4034
$i0 < $nlines and $nzi < $nnz
!ll&&!rl&&r
4340
MC/DC0%0/3
$i0 < $nlines and $nzi < $nnz and defined($_ = readline $fh)
$i0 < $nlines $nzi < $nnz defined($_ = readline $fh)
Truth table25%1/4
A and B and C
A= $i0 < $nlinesB= $nzi < $nnzC= defined($_ = readline $fh)
ABCresult
0XX0
10X0
1100
1111
212 34 chomp;
213 34 ▶ next if (/^\s*(?:$|[^0-9])/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(?:$|[^0-9])/ 12 22 34
214 22 ▶ while (/\b([0-9]+)\s*:\s*(\S+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\b([0-9]+)\s*:\s*(\S+)/g) 64 22 86
215 64 ($i1,$f) = ($1,$2);
216 64 $ix->set($dim1,$nzi => $i1);
217 64 $ix->set($dim0,$nzi => $i0);
218 64 $nz->set($nzi => $f);
219 64 ++$nzi;
220 }
221 22 ++$i0;
222 }
223  
224 ##-- cleanup
225 4 ▶ _ccsio_close($file,$fh)
Branch: 1 branch
BranchTrueFalseTotal
_ccsio_close($file, $fh) or confess("ccs_readldac(): close failed for input file '${file}': $!") 4 0 4
226 or confess("ccs_readldac(): close failed for input file '$file': $!");
227  
228 ##-- guess header data
229 4 ▶ if (!defined($header->{pdims})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $header->{"pdims"}) 2 2 4
230 2 $header->{pdims} = [];
231 2 $header->{pdims}[$dim0] = $nlines;
232 2 $header->{pdims}[$dim1] = $ix->slice("($dim1),")->max+1;
233 }
234 4 ▶ $header->{flags} = $PDL::CCS::Nd::CCSND_FLAGS_DEFAULT if (!defined($header->{flags}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header->{"flags"} 2 2 4
235  
236 ##-- construct and return
237 return PDL::CCS::Nd->newFromWhich($ix,$nz,
238 pdims=>$header->{pdims},
239 vdims=>$header->{vdims},
240 flags=>$header->{flags},
241 4 ▶ sorted=>($opts{sorted} && !$opts{transpose}),
Condition33%1/3
$opts{'sorted'} && !$opts{'transpose'}
!ll&&!rl&&r
400
MC/DC0%0/2
$opts{'sorted'} && !$opts{'transpose'}
$opts{'sorted'} !$opts{'transpose'}
Truth table33%1/3
A && not(B)
A= $opts{'sorted'}B= !$opts{'transpose'}
ABresult
0X0
100
111
242 steal=>1,
243 );
244 }
245  
246  
247 1 1; ##-- be happy
248  
249 ##======================================================================
250 ## POD: footer
251 =pod
252  
253 =head1 ACKNOWLEDGEMENTS
254  
255 Perl by Larry Wall.
256  
257 PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.
258  
259 LDA-C package by by David M. Blei.
260  
261 =cut
262  
263  
264 ##---------------------------------------------------------------------
265 =pod
266  
267 =head1 AUTHOR
268  
269 Bryan Jurish E<lt>moocow@cpan.orgE<gt>
270  
271 =head2 Copyright Policy
272  
273 Copyright (C) 2015-2024, Bryan Jurish. All rights reserved.
274  
275 This package is free software, and entirely without warranty.
276 You may redistribute it and/or modify it under the same terms
277 as Perl itself.
278  
279 =head1 SEE ALSO
280  
281 L<perl>,
282 L<PDL>,
283 L<PDL::CCS::Nd>,
284 L<PDL::CCS::IO::FastRaw>,
285 L<PDL::CCS::IO::FITS>,
286 L<PDL::CCS::IO::MatrixMarket>,
287 the LDA-C package documentation at L<http://www.cs.princeton.edu/~blei/lda-c/>
288 ...
289  
290 =cut
291  
292  
293 1 1; ##-- make perl happy
« blib/lib/PDL/CCS/Nd.pm ↑ Summary blib/lib/PDL/CCS/IO/PETSc.pm »