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