| 1 | ## File: PDL::CCS::IO::MatrixMarket.pm | ||||||||||||||||||||||||||||||||||||||||||
| 2 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ## Description: MatrixMarket I/O wrappers for PDL::CCS::Nd | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package PDL::CCS::IO::MatrixMarket; | ||||||||||||||||||||||||||||||||||||||||||
| 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_writemm ccs_readmm writemm readmm), | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | our %EXPORT_TAGS = | |||||||||||||||||||||||||||||||||||||||||
| 23 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 24 | Func => [@EXPORT_OK], ##-- respect PDL conventions (hopefully) | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | ##-- matrix market magic header line, sparse | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | my $MMAGIC = '%%MatrixMarket matrix coordinate real general'; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | ##-- matrix market magic header line, dense | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | my $DMAGIC = '%%MatrixMarket matrix array real general'; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ## pod: headers | ||||||||||||||||||||||||||||||||||||||||||
| 35 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | PDL::CCS::IO::MatrixMarket - Matrix Market Exchange Format text I/O for PDL::CCS::Nd | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | use PDL; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | use PDL::CCS::Nd; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | use PDL::CCS::IO::MatrixMarket; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | $ccs = PDL::CCS::Nd->newFromWhich($which,$nzvals); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | ccs_writemm($ccs,"ccs.mm"); # write a sparse matrix market text file | ||||||||||||||||||||||||||||||||||||||||||
| 50 | $ccs2 = ccs_readmm("ccs.mm"); # read a sparse matrix market text file | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | $dense = random(10,10); # ... also supported for dense piddles | ||||||||||||||||||||||||||||||||||||||||||
| 53 | writemm($dense, "file.mm"); # write a dense matrix market text file | ||||||||||||||||||||||||||||||||||||||||||
| 54 | $dense2 = readmm("file.mm"); # read a dense matrix market text file | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ## I/O utilities | ||||||||||||||||||||||||||||||||||||||||||
| 61 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 I/O Utilities | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | ##--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 68 | ## ccs_writemm | ||||||||||||||||||||||||||||||||||||||||||
| 69 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head2 ccs_writemm | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | Write a L<PDL::CCS::Nd|PDL::CCS::Nd> object as a MatrixMarket sparse coordinate text file. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | ccs_writemm($ccs,$filename_or_fh) | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ccs_writemm($ccs,$filename_or_fh,\%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Options %opts: | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | start => $i, ##-- index of first element (like perl $[); default=1 for MatrixMarket compatibility | ||||||||||||||||||||||||||||||||||||||||||
| 81 | header => $bool, ##-- write embedded PDL::CCS::Nd header? (default=do) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | *PDL::ccs_writemm = *PDL::CCS::Nd::writemm = \&ccs_writemm; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | ▶ | sub ccs_writemm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ccs_writemm: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | my ($ccs,$file,$opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | ▶ | my %opts =%{$opts||{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts || {}
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | $opts{start} = 1 if (!defined($opts{start})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | $opts{header} = 1 if (!defined($opts{header})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | ##-- write MatrixMarket magic header | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | my $fh = _ccsio_open($file,'>') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | or confess("ccs_writemm(): open failed for output file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | #binmode($fh,':raw'); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | local $,=''; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | print $fh "$MMAGIC\n"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | ##-- write ccs header to output file | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | ▶ | if ($opts{header}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | print $fh map {("%", __PACKAGE__, " $_")} @{_ccsio_header_lines($ccs)}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | ##-- write mm dimensions to output file | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | print $fh join(' ', '',$ccs->pdims->list,$ccs->_nnz_p), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | ##-- write mm data to output file | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | my $ix = $ccs->_whichND; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | ▶ | $ix = ($ix+$opts{start}) if ($opts{start} != 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | wcols($ix->xchg(0,1), $ccs->_nzvals, $fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | or confess("ccs_writemm(): failed to write data to '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | ##-- cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | _ccsio_close($file,$fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | or confess("ccs_writemm(): close failed for output file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | ##--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 121 | ## writemm (dense) | ||||||||||||||||||||||||||||||||||||||||||
| 122 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =head2 writemm | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | Write a dense PDL as a MatrixMarket array text file. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | writemm($pdl,$filename_or_handle) | ||||||||||||||||||||||||||||||||||||||||||
| 129 | writemm($pdl,$filename_or_handle,\%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Options %opts: (none yet) | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | *PDL::writemm = \&writemm; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | sub writemm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
writemm: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | my ($pdl,$file,$opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | ##-- dispatch for PDL::CCS::Nd objects | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | ▶ | return ccs_writemm($pdl,$file,$opts) if (UNIVERSAL::isa($pdl,'PDL::CCS::Nd')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | ##-- write MatrixMarket magic header | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | my $fh = _ccsio_open($file,'>') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | or confess("writemm(): open failed for output file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 145 | #binmode($fh,':raw'); | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | local $,=''; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | print $fh "$DMAGIC\n"; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | ##-- print administrative data | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 2 | print $fh "%", __PACKAGE__, " type ", $pdl->type, "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | ##-- write mm dimensions to output file | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 2 | print $fh " ", join(' ', $pdl->dims), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | ##-- write flat data to output file | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | ▶ | wcols($pdl->flat, $fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | or confess("writemm(): failed to write data to '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | ##-- cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | ▶ | _ccsio_close($file,$fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | or confess("writemm(): close failed for output file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | ##--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 168 | ## ccs_readmm | ||||||||||||||||||||||||||||||||||||||||||
| 169 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =head2 ccs_readmm | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | Read a Matrix Market sparse coordinate text file | ||||||||||||||||||||||||||||||||||||||||||
| 174 | as a L<PDL::CCS::Nd|PDL::CCS::Nd> object | ||||||||||||||||||||||||||||||||||||||||||
| 175 | using L<PDL::IO::Misc::rcols()|PDL::IO::Misc/rcols()>. | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | $ccs = ccs_readmm($filename_or_fh) | ||||||||||||||||||||||||||||||||||||||||||
| 178 | $ccs = ccs_readmm($filename_or_fh,\%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | Options %opts: | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | start => $i, ##-- index of first element (like perl $[); default=1 for MatrixMarket compatibility | ||||||||||||||||||||||||||||||||||||||||||
| 183 | header => $bool, ##-- attempt to read embedded CCS header from file (default=do) | ||||||||||||||||||||||||||||||||||||||||||
| 184 | sorted => $bool, ##-- assume input data is sorted (default=0) | ||||||||||||||||||||||||||||||||||||||||||
| 185 | nomagic => $bool, ##-- don't check for matrix market magic header (default:do) | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | *PDL::ccs_readmm = *PDL::CCS::Nd::readmm = \&ccs_readmm; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | ▶ | sub ccs_readmm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ccs_readmm: called CC=27 SCAR=38.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 4 | ▶ | shift if (UNIVERSAL::isa($_[0],'PDL') || UNIVERSAL::isa($_[0],'PDL::CCS::Nd')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 4 | my ($file,$opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | ▶ | my %opts = %{$opts||{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts || {}
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 4 | ▶ | $opts{start} = 1 if (!defined($opts{start})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 4 | ▶ | $opts{header} = 1 if (!defined($opts{header})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | ##-- open input file | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | ▶ | my $fh = _ccsio_open($file,'<') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | or confess("ccs_readmm(): open failed for input file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | ##-- get matrix market magic header | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | ▶ | if (!$opts{nomagic}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 4 | my $mmagic = <$fh>; chomp($mmagic); | |||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | ▶ | if ($mmagic eq $DMAGIC) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | ##-- dense input file, read as dense PDL | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | _ccsio_close($file,$fh); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | return readmm($file,{%opts,nomagic=>1}); | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | elsif ($mmagic ne $MMAGIC) { | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | confess("ccs_readmm(): bad magic header line in input file, should be '$MMAGIC'"); | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | ##-- scan initial comments, extracting CCS header | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 4 | my @hlines = qw(); | |||||||||||||||||||||||||||||||||||||||||
| 216 | 4 | ▶ | while (defined($_=<$fh>)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 14 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 14 | ▶ | if (/^%(\S+) (.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 10 | ▶ | push(@hlines,$2) if ($opts{header} && substr($_,1,length(__PACKAGE__)) eq __PACKAGE__); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"header"} and substr($_, 1, 26) eq __PACKAGE__
MC/DC0%0/2
$opts{"header"} and substr($_, 1, 26) eq __PACKAGE__
$opts{"header"}
substr($_, 1, 26) eq __PACKAGE__
Truth table33%1/3
A and B
A= $opts{"header"}B= substr($_, 1, 26) eq __PACKAGE__
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | } elsif (!/^%/) { | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | ##-- parse embedded CCS header if requested | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 4 | ▶ | my $header = _ccsio_parse_header($opts{header} ? \@hlines : []); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | ##-- we now have 1st non-comment line in $_: scan for mm dimension list | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | ▶ | while ($_ =~ /^\s*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $_ = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 4 | my @dims = split(' ',$_); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 4 | my $nnz = pop(@dims); | |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | ##-- update ccs header if required | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 4 | my $mmdims = pdl(ccs_indx(),\@dims); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 4 | ▶ | if (defined($header->{pdims}) && ($header->{pdims}->nelem != $mmdims->nelem || !all($header->{pdims}==$mmdims))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $header->{"pdims"} and $header->{"pdims"}->nelem != $mmdims->nelem || !all($header->{"pdims"} == $mmdims)
$header->{"pdims"}->nelem != $mmdims->nelem or not all($header->{"pdims"} == $mmdims)
MC/DC0%0/3
defined $header->{"pdims"} and $header->{"pdims"}->nelem != $mmdims->nelem || !all($header->{"pdims"} == $mmdims)
defined $header->{"pdims"}
$header->{"pdims"}->nelem != $mmdims->nelem
not all($header->{"pdims"} == $mmdims)
Truth table25%1/4
A and B or not(C)
A= defined $header->{"pdims"}B= $header->{"pdims"}->nelem != $mmdims->nelemC= not all($header->{"pdims"} == $mmdims)
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | $header->{pdims} = $mmdims; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | $header->{vdims} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | ##-- read data: indices | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 4 | my $offset = tell($fh); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 4 | my $ix = PDL->rcols($fh, [0..$#dims], { IGNORE=>qr{^%}, TYPES=>[ccs_indx()] }); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 4 | ▶ | $ix -= $opts{start} if ($opts{start} != 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 4 | $ix = $ix->xchg(0,1); | |||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | ##-- read data: values | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | seek($fh,$offset,SEEK_SET) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | or confess("ccs_readmm(): seek() failed for input file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 4 | my $iotype = $header->{iotype}; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 4 | ▶ | $iotype = PDL->can($iotype)->() if (defined($iotype) && !ref($iotype) && PDL->can($iotype)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $iotype and not ref $iotype and "PDL"->can($iotype)
defined $iotype and not ref $iotype
MC/DC33%1/3
defined $iotype and not ref $iotype and "PDL"->can($iotype)
defined $iotype
not ref $iotype
"PDL"->can($iotype)
Truth table50%2/4
A and not(B) and C
A= defined $iotypeB= not ref $iotypeC= "PDL"->can($iotype)
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 4 | ▶ | $iotype = $PDL::IO::Misc::deftype if (!ref($iotype)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | my $nz = PDL->rcols($fh, [$#dims+1], { IGNORE=>qr{^%}, TYPES=>[$iotype] }); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 4 | $nz = $nz->append(0); ##-- missing value | |||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | ##-- cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 4 | ▶ | _ccsio_close($file,$fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | or confess("ccs_readmm(): close failed for input file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | ##-- construct and return | ||||||||||||||||||||||||||||||||||||||||||
| 262 | return PDL::CCS::Nd->newFromWhich($ix,$nz, | ||||||||||||||||||||||||||||||||||||||||||
| 263 | pdims=>$header->{pdims}, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | vdims=>$header->{vdims}, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | flags=>$header->{flags}, | ||||||||||||||||||||||||||||||||||||||||||
| 266 | sorted=>$opts{sorted}, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 4 | steal=>1); | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | ##--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 272 | ## readmm (dense) | ||||||||||||||||||||||||||||||||||||||||||
| 273 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | =head2 readmm | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | Read a Matrix Market dense array text file as a dense pdl using L<PDL::IO::Misc::rcols()|PDL::IO::Misc/rcols()>. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | $pdl = readmm($fname) | ||||||||||||||||||||||||||||||||||||||||||
| 280 | $pdl = readmm($fname,\%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | Options %opts: | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | nomagic => $bool, ##-- don't check for matrix market magic header (default:do) | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | *PDL::readmm = \&readmm; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 2 | ▶ | sub readmm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
readmm: called CC=15 SCAR=32.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | ▶ | shift if (UNIVERSAL::isa($_[0],'PDL') || UNIVERSAL::isa($_[0],'PDL::CCS::Nd')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 2 | my ($file,$opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | ▶ | my %opts = %{$opts||{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts || {}
MC/DC0%0/1
$opts || {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | ##-- open input file | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 2 | ▶ | my $fh = _ccsio_open($file,'<') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | or confess("readmm(): open failed for input file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | ##-- get matrix market magic header | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 2 | ▶ | if (!$opts{nomagic}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | my $dmagic = <$fh>; chomp($dmagic); | |||||||||||||||||||||||||||||||||||||||||
| 301 | 2 | ▶ | if ($dmagic eq $MMAGIC) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | ##-- sparse input file, read as PDL::CCS::Nd | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | _ccsio_close($file,$fh); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | return ccs_readmm($file,{%opts,nomagic=>1}); | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | elsif ($dmagic ne $DMAGIC) { | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | confess("readmm(): bad magic header line in input file, should be '$DMAGIC'") | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | ##-- scan for header | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | my $iotype = $PDL::IO::Misc::deftype; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 2 | ▶ | while (defined($_=<$fh>)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | ▶ | if (!/^%/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 2 | ▶ | if (/^%(\S+) type (\S+)/ && $1 eq __PACKAGE__) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
/^%(\S+) type (\S+)/ and $1 eq __PACKAGE__
MC/DC0%0/2
/^%(\S+) type (\S+)/ and $1 eq __PACKAGE__
/^%(\S+) type (\S+)/
$1 eq __PACKAGE__
Truth table33%1/3
A and B
A= /^%(\S+) type (\S+)/B= $1 eq __PACKAGE__
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $iotype = PDL->can($_)->() if (PDL->can($_)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | } elsif (!/^\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | ##-- parse dims | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | my @dims = split(' ',$_); | |||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | ##-- read data | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | my $pdl = rcols($fh, [], { IGNORE=>qr{^%}, TYPES=>[$iotype] }); | |||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | ##-- cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 2 | ▶ | _ccsio_close($file,$fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | or confess("ccs_readmm(): close failed for input file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | ##-- construct and return | ||||||||||||||||||||||||||||||||||||||||||
| 334 | #$pdl = $pdl->reshape(@dims); ##-- pdl v2.014 chokes on this | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 2 | my $out = zeroes($pdl->type, @dims); | |||||||||||||||||||||||||||||||||||||||||
| 336 | 2 | (my $tmp = $out->flat) .= $pdl->flat; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 2 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | 1; ##-- be happy | |||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 344 | ## POD: footer | ||||||||||||||||||||||||||||||||||||||||||
| 345 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | Perl by Larry Wall. | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | ##--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 357 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | Bryan Jurish E<lt>moocow@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =head2 Copyright Policy | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | Copyright (C) 2015-2024, Bryan Jurish. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | This package is free software, and entirely without warranty. | ||||||||||||||||||||||||||||||||||||||||||
| 368 | You may redistribute it and/or modify it under the same terms | ||||||||||||||||||||||||||||||||||||||||||
| 369 | as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | L<perl>, | ||||||||||||||||||||||||||||||||||||||||||
| 374 | L<PDL>, | ||||||||||||||||||||||||||||||||||||||||||
| 375 | L<PDL::CCS::Nd>, | ||||||||||||||||||||||||||||||||||||||||||
| 376 | L<PDL::CCS::IO::FastRaw>, | ||||||||||||||||||||||||||||||||||||||||||
| 377 | L<PDL::CCS::IO::FITS>, | ||||||||||||||||||||||||||||||||||||||||||
| 378 | L<PDL::CCS::IO::LDAC>, | ||||||||||||||||||||||||||||||||||||||||||
| 379 | the matrix market format documentation at L<http://math.nist.gov/MatrixMarket/formats.html> | ||||||||||||||||||||||||||||||||||||||||||
| 380 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | 1 | 1; ##-- make perl happy | |||||||||||||||||||||||||||||||||||||||||