| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | svdpackout.pl - Reconstruct post-SVD form of matrix from singular values output by SVDPACKC | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | svdpackout.pl [OPTIONS] lav2 lao2 | ||
| 10 | |||
| 11 | Type C<svdpackout.pl --help> for a quick summary of options | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Reconstructs a matrix from its singular values and singular vectors created | ||
| 16 | by SVDPACKC. The result of this is essentially a "smoothed" matrix equal | ||
| 17 | in size to the original pre-SVDPACKC matrix, but where the | ||
| 18 | non-significant dimensions have been removed. | ||
| 19 | |||
| 20 | SVDPACKC decomposes the original input matrix into three matrices : | ||
| 21 | |||
| 22 | =over | ||
| 23 | |||
| 24 | =item * U : M x k (rows by k) | ||
| 25 | |||
| 26 | =item * S : k x k (k singular values of input matrix stored in a diagonal matrix) | ||
| 27 | |||
| 28 | =item * VT : k x N (columns by k, obtained by transposing V : N x k) | ||
| 29 | |||
| 30 | =back | ||
| 31 | |||
| 32 | where k is the dimension value to which we have reduced the matrix | ||
| 33 | (maxprs), M is the number of rows, and N is the number of columns. | ||
| 34 | |||
| 35 | We will normally keep the first k singular values (since those are | ||
| 36 | organized greatest to least in S, and represent the most significant | ||
| 37 | dimensions in the data). We do this by keeping the first k rows of S | ||
| 38 | and VT, and the first k columns of U. | ||
| 39 | |||
| 40 | When we use --rowonly we reconstruct an M x k matrix by taking the | ||
| 41 | product U * S. This matrix represents all of our original rows in a | ||
| 42 | reduce column/dimension space which is then clustered by Cluto. | ||
| 43 | |||
| 44 | If we don't use --rowonly we reconstruct the original M x N matrix, | ||
| 45 | but only using k dimensions (which gives us a kind of smoothing | ||
| 46 | effect). That is we take the product (M x k) * (k x k) * (k * N). | ||
| 47 | Note that L<discriminate.pl> defaults to using --rowonly reconstration, | ||
| 48 | at least in part in the interests of computational efficiently. | ||
| 49 | |||
| 50 | =head1 INPUT | ||
| 51 | |||
| 52 | =head2 Required Arguments: | ||
| 53 | |||
| 54 | =head3 lav2 | ||
| 55 | |||
| 56 | Binary output file created by SVDPACKC las2 | ||
| 57 | |||
| 58 | =head3 lao2 | ||
| 59 | |||
| 60 | ASCII output file created by SVDPACK las2 | ||
| 61 | |||
| 62 | =head2 Optional Arguments: | ||
| 63 | |||
| 64 | =head4 --rowonly | ||
| 65 | |||
| 66 | Only the row vectors are reconstructed. By default, svdpackout | ||
| 67 | reconstructs entire matrix. This may not be used with --output. | ||
| 68 | This is the default setting for L<discriminate.pl>. | ||
| 69 | |||
| 70 | =head4 --output OUTPUT | ||
| 71 | |||
| 72 | Specifies the form of the output to be written by this program: | ||
| 73 | |||
| 74 | reconstruct - re-constructs the full rank-k matrix, output to STDOUT | ||
| 75 | rowonly - same as --rowonly, output to STDOUT | ||
| 76 | components - output the U, S, and VT matrices to U.txt, S.txt, VT.txt | ||
| 77 | |||
| 78 | =head4 --sqrt | ||
| 79 | |||
| 80 | In --rowonly reconstruction, take sqrt of S (kxk). This was the | ||
| 81 | default method in SenseClusters 1.00 and previous. Has no effect | ||
| 82 | when used with full reconstruction. Provided mainly for backwards | ||
| 83 | compatability. | ||
| 84 | |||
| 85 | =head4 --negatives | ||
| 86 | |||
| 87 | Set negative values in reconstructed matrices that are between -1 and 0 | ||
| 88 | to 0 (except in component output). This option is provided mainly for | ||
| 89 | backwards compatibility as this was the default behavior in | ||
| 90 | SenseClusters 1.00 and previous. | ||
| 91 | |||
| 92 | =head4 --format FORM | ||
| 93 | |||
| 94 | Specifies numeric format for representing output matrix values. | ||
| 95 | Following formats are supported with --format : | ||
| 96 | |||
| 97 | iN - Output matrix will contain integer values each occupying N spaces | ||
| 98 | |||
| 99 | fM.N - Output matrix will contain real values each occupying total M spaces of which last N digits show fractional part. M spaces for each entry include the decimal point and +/- sign if any. | ||
| 100 | |||
| 101 | Default format value is f16.10. | ||
| 102 | |||
| 103 | =head3 Other Options : | ||
| 104 | |||
| 105 | =head4 --help | ||
| 106 | |||
| 107 | Displays this message. | ||
| 108 | |||
| 109 | =head4 --version | ||
| 110 | |||
| 111 | Displays the version information. | ||
| 112 | |||
| 113 | =head1 OUTPUT | ||
| 114 | |||
| 115 | svdpackout.pl displays a matrix reconstructed from the Singular Triplets | ||
| 116 | created by SVD. By default, entire matrix (product of left and right | ||
| 117 | singular vectors and singular values) is reconstructed. When --rowonly | ||
| 118 | is ON, or when --output rowonly is set, only the reduced row vectors | ||
| 119 | are built. When --output components is set, the three component | ||
| 120 | matrices, U, S, and V, are output separately. | ||
| 121 | |||
| 122 | =head1 SYSTEM REQUIREMENTS | ||
| 123 | |||
| 124 | =over | ||
| 125 | |||
| 126 | =item SVDPACKC - L<http://netlib.org/svdpack/> (also available in /External) | ||
| 127 | |||
| 128 | =item PDL - L<http://search.cpan.org/dist/PDL/> | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =head1 BUGS | ||
| 133 | |||
| 134 | In version 1.00 and before, we took the square root of the k x k matrix | ||
| 135 | before recombining in --rowonly made. We have changed that to make it | ||
| 136 | an option (via --sqrt), since the motives for that are at this point | ||
| 137 | unclear. The following discussion originally formulated by Mahesh Joshi | ||
| 138 | motivates our change to a default method of not taking --sqrt. | ||
| 139 | |||
| 140 | Deerwester et al. (S. Deerwester, S.T. Dumais, G.W. Furnas, T.K. | ||
| 141 | Landauer, and R. Harshman. Indexing by latent semantic analysis. Journal | ||
| 142 | of the American Society for Information Science, 41:391.407, 1990.), | ||
| 143 | give a nice explanation of the reason of combing M x k and k x k to | ||
| 144 | evaluate similarity between whatever was represented along the rows in | ||
| 145 | the original matrix (contexts in order 1 and features in order 2). | ||
| 146 | |||
| 147 | They also mention the use of the combination Mxk and SquareRoot(kxk), | ||
| 148 | but that is for evaluating correlation between a term and a document (as | ||
| 149 | against between a term-term or document-document pair) and in such a | ||
| 150 | case what is also needed is the combination of Nxk and SquareRoot(kxk) | ||
| 151 | since in a heterogeneous pair (one term and one document) correlation | ||
| 152 | analysis, one vector each is needed from these two different | ||
| 153 | combinations. All this is mentioned in the "Technical Details" section | ||
| 154 | of the Deerwester et al. paper. | ||
| 155 | |||
| 156 | But this does not seem to explain the use of the square root in the type | ||
| 157 | of analysis we are doing - which is homogeneous, i.e. we are only | ||
| 158 | analyzing term-term or context-context similarities. | ||
| 159 | |||
| 160 | In both --rowonly and full reconstruction, we smoothed negative values | ||
| 161 | between 0 and -1 to 0. The motive for that is unclear, and so a option | ||
| 162 | --negatives is provided to preserve negative values and override that | ||
| 163 | behavior. | ||
| 164 | |||
| 165 | We would now generally recommend that svdpackout.pl be run without | ||
| 166 | --sqrt and --negatives. That is do not take the square roots of | ||
| 167 | the S matrix when doing reconstruction and let negative values stand. | ||
| 168 | This is the default behavior as of 1.01 and beyond. | ||
| 169 | |||
| 170 | =head1 AUTHORS | ||
| 171 | |||
| 172 | Amruta Purandare, University of Pittsburgh | ||
| 173 | |||
| 174 | Richard Wicentowski, Swarthmore College | ||
| 175 | richardw at cs.swarthmore.edu | ||
| 176 | |||
| 177 | Ted Pedersen, University of Minnesota, Duluth | ||
| 178 | tpederse at d.umn.edu | ||
| 179 | |||
| 180 | =head1 COPYRIGHT | ||
| 181 | |||
| 182 | Copyright (c) 2002-2008, Amruta Purandare, Richard Wicentowski, Ted Pedersen | ||
| 183 | |||
| 184 | This program is free software; you can redistribute it and/or modify it under | ||
| 185 | the terms of the GNU General Public License as published by the Free Software | ||
| 186 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 187 | version. | ||
| 188 | |||
| 189 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 190 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 191 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 192 | |||
| 193 | You should have received a copy of the GNU General Public License along with | ||
| 194 | this program; if not, write to | ||
| 195 | |||
| 196 | The Free Software Foundation, Inc., | ||
| 197 | 59 Temple Place - Suite 330, | ||
| 198 | Boston, MA 02111-1307, USA. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | ############################################################################### | ||
| 203 | |||
| 204 | # THE CODE STARTS HERE | ||
| 205 | |||
| 206 | # $0 contains the program name along with the | ||
| 207 | # complete path. Extract just the program | ||
| 208 | # name and use in error messages | ||
| 209 | $0=~s/.*\/(.+)/$1/; | ||
| 210 | |||
| 211 | use PDL; | ||
| 212 | use PDL::NiceSlice; | ||
| 213 | use PDL::Primitive; | ||
| 214 | |||
| 215 | ############################################################################### | ||
| 216 | |||
| 217 | # ================================ | ||
| 218 | # COMMAND LINE OPTIONS AND USAGE | ||
| 219 | # ================================ | ||
| 220 | |||
| 221 | # command line options | ||
| 222 | use Getopt::Long; | ||
| 223 | GetOptions ("help","version","format=s","output=s","rowonly","negatives","sqrt"); | ||
| 224 | |||
| 225 | # show help option | ||
| 226 | |||
| 227 | if (defined $opt_help) { | ||
| 228 | $opt_help=1; | ||
| 229 | &showhelp(); | ||
| 230 | exit; | ||
| 231 | } | ||
| 232 | |||
| 233 | # show version information | ||
| 234 | if (defined $opt_version) { | ||
| 235 | $opt_version=1; | ||
| 236 | &showversion(); | ||
| 237 | exit; | ||
| 238 | } | ||
| 239 | |||
| 240 | # | ||
| 241 | # output options | ||
| 242 | # | ||
| 243 | # check to see if --output is set | ||
| 244 | # if it is, find out the setting and proceed | ||
| 245 | # and if it isn't, then check to see if --rowonly is set | ||
| 246 | # if it is, then set output variable to that | ||
| 247 | # if it isn't, the do a set output variable to full reconstruction | ||
| 248 | # | ||
| 249 | |||
| 250 | if (defined $opt_output) { | ||
| 251 | if ((defined $opt_rowonly) && ($opt_output !~ /rowonly/)) { | ||
| 252 | printf ("Incompatible options: --rowonly --output=%s\n", $opt_output); | ||
| 253 | &showhelp(); | ||
| 254 | exit; | ||
| 255 | } | ||
| 256 | if (($opt_output ne "reconstruct") && | ||
| 257 | ($opt_output ne "rowonly") && | ||
| 258 | ($opt_output ne "components")) { | ||
| 259 | printf ("Invalid output format: %s\n", $opt_output); | ||
| 260 | &showhelp(); | ||
| 261 | exit; | ||
| 262 | } | ||
| 263 | } elsif (defined $opt_rowonly) { | ||
| 264 | $opt_output="rowonly"; | ||
| 265 | undef $opt_rowonly; | ||
| 266 | } else { | ||
| 267 | $opt_output="reconstruct"; | ||
| 268 | } | ||
| 269 | |||
| 270 | # | ||
| 271 | # formatting matrix values | ||
| 272 | # | ||
| 273 | |||
| 274 | if (defined $opt_format) { | ||
| 275 | # integer format | ||
| 276 | if ($opt_format=~/^i(\d+)$/) { | ||
| 277 | $format="%$1d"; | ||
| 278 | $lower_format="-"; | ||
| 279 | while (length($lower_format)<($1-1)) { | ||
| 280 | $lower_format.="9"; | ||
| 281 | } | ||
| 282 | if ($lower_format eq "-") { | ||
| 283 | $lower_format="0"; | ||
| 284 | } | ||
| 285 | $upper_format=""; | ||
| 286 | while (length($upper_format)<($1-1)) { | ||
| 287 | $upper_format.="9"; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | # floating point format | ||
| 291 | elsif ($opt_format=~/^f(\d+)\.(\d+)$/) { | ||
| 292 | $format="%$1.$2f"; | ||
| 293 | $lower_format="-"; | ||
| 294 | while (length($lower_format)<($1-$2-2)) { | ||
| 295 | $lower_format.="9"; | ||
| 296 | } | ||
| 297 | $lower_format.="."; | ||
| 298 | while (length($lower_format)<($1-1)) { | ||
| 299 | $lower_format.="9"; | ||
| 300 | } | ||
| 301 | |||
| 302 | $upper_format=""; | ||
| 303 | while (length($upper_format)<($1-$2-2)) { | ||
| 304 | $upper_format.="9"; | ||
| 305 | } | ||
| 306 | $upper_format.="."; | ||
| 307 | while (length($upper_format)<($1-1)) { | ||
| 308 | $upper_format.="9"; | ||
| 309 | } | ||
| 310 | } else { | ||
| 311 | print STDERR "ERROR($0): | ||
| 312 | Wrong format value --format=$opt_format.\n"; | ||
| 313 | exit; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | # default | ||
| 317 | else { | ||
| 318 | # $format="%8.3f"; | ||
| 319 | $format="%16.10f"; | ||
| 320 | $lower_format="-999.9999999999"; | ||
| 321 | $upper_format="9999.9999999999"; | ||
| 322 | } | ||
| 323 | |||
| 324 | # show minimal usage message if no arguments | ||
| 325 | if ($#ARGV<1) { | ||
| 326 | &showminimal(); | ||
| 327 | exit; | ||
| 328 | } | ||
| 329 | |||
| 330 | ############################################################################# | ||
| 331 | |||
| 332 | # ================================ | ||
| 333 | # INITIALIZATION AND INPUT | ||
| 334 | # ================================ | ||
| 335 | |||
| 336 | # accept lav2 file name | ||
| 337 | $lav2=$ARGV[0]; | ||
| 338 | if (!-e $lav2) { | ||
| 339 | print STDERR "ERROR($0): | ||
| 340 | lav2 file <$lav2> doesn't exist...\n"; | ||
| 341 | exit; | ||
| 342 | } | ||
| 343 | open(LAV2,$lav2) || die "Error($0): | ||
| 344 | Error(code=$!) in opening lav2 file <$lav2>.\n"; | ||
| 345 | |||
| 346 | #accept lao2 file name | ||
| 347 | $lao2=$ARGV[1]; | ||
| 348 | if (!-e $lao2) { | ||
| 349 | print STDERR "ERROR($0): | ||
| 350 | lao2 file <$lao2> doesn't exist...\n"; | ||
| 351 | exit; | ||
| 352 | } | ||
| 353 | open(LAO2,$lao2) || die "Error($0): | ||
| 354 | Error(code=$!) in opening lao2 file <$lao2>.\n"; | ||
| 355 | |||
| 356 | ############################################################################## | ||
| 357 | |||
| 358 | # ======================== | ||
| 359 | # CODE SECTION | ||
| 360 | # ======================== | ||
| 361 | |||
| 362 | # ------------------- | ||
| 363 | # Reading file lao2 | ||
| 364 | # ------------------- | ||
| 365 | while (<LAO2>) { | ||
| 366 | # e-pairs = K, reduction factor specified by the user | ||
| 367 | if (/MAX. NO. OF EIGENPAIRS\s*=\s*(\d+)/) { | ||
| 368 | $k=$1; | ||
| 369 | } | ||
| 370 | # rows | ||
| 371 | if (/NO\. OF TERMS\s*\(ROWS\)\s*=\s*(\d+)/) { | ||
| 372 | $rows=$1; | ||
| 373 | next; | ||
| 374 | } | ||
| 375 | # cols | ||
| 376 | if (/NO\. OF DOCUMENTS\s*\(COLS\)\s*=\s*(\d+)/) { | ||
| 377 | $cols=$1; | ||
| 378 | next; | ||
| 379 | } | ||
| 380 | # nsig | ||
| 381 | if (/NSIG\s*=\s*(\d+)/) { | ||
| 382 | $nsig=$1; | ||
| 383 | next; | ||
| 384 | } | ||
| 385 | # after this the S-values follow | ||
| 386 | if (/COMPUTED S-VALUES/) { | ||
| 387 | last; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | # check: valid values of rows, cols, k, nsig | ||
| 392 | # are obtained from lao2 | ||
| 393 | if (!defined $rows) { | ||
| 394 | print STDERR "ERROR($0): | ||
| 395 | #ROWS not found in lao2 file <$lao2>.\n"; | ||
| 396 | exit; | ||
| 397 | } | ||
| 398 | if (!defined $cols) { | ||
| 399 | print STDERR "ERROR($0): | ||
| 400 | #COLS not found in lao2 file <$lao2>.\n"; | ||
| 401 | exit; | ||
| 402 | } | ||
| 403 | if (!defined $nsig) { | ||
| 404 | print STDERR "ERROR($0): | ||
| 405 | NSIG value not found in lao2 file <$lao2>.\n"; | ||
| 406 | exit; | ||
| 407 | } | ||
| 408 | if (!defined $k) { | ||
| 409 | print STDERR "ERROR($0): | ||
| 410 | NO. OF EIGENPAIRS not found in lao2 file <$lao2>.\n"; | ||
| 411 | exit; | ||
| 412 | } | ||
| 413 | |||
| 414 | # one line is blank in lao2 after "COMPUTED S-VALUES..." line | ||
| 415 | <LAO2>; | ||
| 416 | |||
| 417 | $d=zeroes($nsig); | ||
| 418 | # reading singular values | ||
| 419 | |||
| 420 | # singular values are reverse ordered in lao2 | ||
| 421 | # such that the most significant/highest value | ||
| 422 | # occurs last | ||
| 423 | for ($i=$nsig-1 ; $i>=0 ; $i--) { | ||
| 424 | $line=<LAO2>; | ||
| 425 | if (!defined $line) { | ||
| 426 | print STDERR "ERROR($0): | ||
| 427 | lao2 file <$lao2> doesn't have $nsig S-values.\n"; | ||
| 428 | exit; | ||
| 429 | } | ||
| 430 | $line=~s/^\s+//; | ||
| 431 | # line containing S-value contains | ||
| 432 | # ellipses, index of S-value, S-value, RES NORMS | ||
| 433 | ($ellipses,$ind,$sval,@rest)=split(/\s+/,$line); | ||
| 434 | # we only need the S-value | ||
| 435 | undef $ellipses; | ||
| 436 | undef $ind; | ||
| 437 | undef @rest; | ||
| 438 | set($d,$i,$sval); | ||
| 439 | } | ||
| 440 | |||
| 441 | # taking minimum of #nsig and k | ||
| 442 | $k=($k<$nsig) ? $k : $nsig; | ||
| 443 | |||
| 444 | # -------------------- | ||
| 445 | # Reading file lav2 | ||
| 446 | # -------------------- | ||
| 447 | |||
| 448 | # binary file needs to specify | ||
| 449 | # binmode | ||
| 450 | binmode LAV2; | ||
| 451 | |||
| 452 | # lav2 contains some header information | ||
| 453 | # before the actual S-vectors | ||
| 454 | |||
| 455 | # the header length is length(pack(2l+d)) | ||
| 456 | $longsize=length(pack("l",())); | ||
| 457 | $doubsize=length(pack("d",())); | ||
| 458 | |||
| 459 | # right S-vectors start after header | ||
| 460 | $vstart=(2*$longsize) + $doubsize; | ||
| 461 | |||
| 462 | # left S-vectors start after right S-vectors | ||
| 463 | $ustart=$vstart+($cols*$nsig*$doubsize); | ||
| 464 | |||
| 465 | # printing rows cols on first line | ||
| 466 | if ($opt_output eq "reconstruct") { | ||
| 467 | print "$rows $cols\n"; | ||
| 468 | } elsif ($opt_output eq "rowonly") { | ||
| 469 | print "$rows $k\n"; | ||
| 470 | } elsif ($opt_output eq "components") { | ||
| 471 | |||
| 472 | } | ||
| 473 | |||
| 474 | # | ||
| 475 | # output components U, S, VT to files | ||
| 476 | # --negatives has no effect here | ||
| 477 | # --sqrt does not either (sqrt was only ever taken on --rowonly) | ||
| 478 | # | ||
| 479 | |||
| 480 | if ($opt_output eq "components") { | ||
| 481 | print STDERR "Writing to U.txt, S.txt and VT.txt\n"; | ||
| 482 | |||
| 483 | open (U, ">U.txt") or die "$!: U.txt\n"; | ||
| 484 | print U "$rows $k\n"; | ||
| 485 | for ($i=0;$i<$rows;$i++) { | ||
| 486 | for ($m=$nsig-1;$m>=$nsig-$k;$m--) { | ||
| 487 | $index=$ustart+((($m*$rows)+$i)*$doubsize); | ||
| 488 | seek(LAV2,$index,0); | ||
| 489 | read(LAV2,$value,$doubsize); | ||
| 490 | if (!defined $value) { | ||
| 491 | print STDERR "ERROR($0): | ||
| 492 | lav2 file <$lav2> doesn't have sufficient S-vectors.\n"; | ||
| 493 | exit; | ||
| 494 | } | ||
| 495 | #unpacking | ||
| 496 | $value=unpack("d",$value); | ||
| 497 | printf U ($format, $value); | ||
| 498 | } | ||
| 499 | print U "\n"; | ||
| 500 | } | ||
| 501 | close(U); | ||
| 502 | |||
| 503 | open (S, ">S.txt") or die "$!: S.txt\n"; | ||
| 504 | print S "$k $k\n"; | ||
| 505 | for ($i=0 ; $i<$k ; $i++) { | ||
| 506 | for ($j=0; $j<$i ; $j++) { printf S ($format, 0); } | ||
| 507 | printf S ($format, $d->at($i)); | ||
| 508 | for ($j=$i+1; $j<$k ; $j++) { printf S ($format, 0); } | ||
| 509 | print S "\n"; | ||
| 510 | } | ||
| 511 | close(S); | ||
| 512 | |||
| 513 | open (VT, ">VT.txt") or die "$!: VT.txt\n"; | ||
| 514 | print VT "$k $cols\n"; | ||
| 515 | for ($j=0;$j<$cols;$j++) { | ||
| 516 | for ($m=$nsig-1;$m>=$nsig-$k;$m--) { | ||
| 517 | $index=$vstart+((($m*$cols)+$j)*$doubsize); | ||
| 518 | seek(LAV2,$index,0); | ||
| 519 | read(LAV2,$value,$doubsize); | ||
| 520 | if (!defined $value) { | ||
| 521 | print STDERR "ERROR($0): | ||
| 522 | lav2 file <$lav2> doesn't have sufficient S-vectors.\n"; | ||
| 523 | exit; | ||
| 524 | } | ||
| 525 | # unpacking | ||
| 526 | $value = unpack("d", $value); | ||
| 527 | printf VT ($format, $value); | ||
| 528 | } | ||
| 529 | print VT "\n"; | ||
| 530 | } | ||
| 531 | close(VT); | ||
| 532 | |||
| 533 | exit; | ||
| 534 | } | ||
| 535 | |||
| 536 | # | ||
| 537 | # reconstruct full matrix, U*S*VT, with only k dimensions, | ||
| 538 | # (M x k) * (k x k) * (k x N) => M x N (smoothed) | ||
| 539 | # | ||
| 540 | |||
| 541 | elsif ($opt_output eq "reconstruct") { | ||
| 542 | $u=zeroes($k); | ||
| 543 | $v=zeroes($k); | ||
| 544 | for ($i=0;$i<$rows;$i++) { | ||
| 545 | $u->inplace->zeroes; | ||
| 546 | for ($m=$nsig-1;$m>=$nsig-$k;$m--) { | ||
| 547 | $index=$ustart+((($m*$rows)+$i)*$doubsize); | ||
| 548 | seek(LAV2,$index,0); | ||
| 549 | read(LAV2,$value,$doubsize); | ||
| 550 | if (!defined $value) { | ||
| 551 | print STDERR "ERROR($0): | ||
| 552 | lav2 file <$lav2> doesn't have sufficient S-vectors.\n"; | ||
| 553 | exit; | ||
| 554 | } | ||
| 555 | #unpacking | ||
| 556 | $value=unpack("d",$value); | ||
| 557 | set($u,$nsig-1-$m,$value); | ||
| 558 | } | ||
| 559 | for ($j=0;$j<$cols;$j++) { | ||
| 560 | $v->inplace->zeroes; | ||
| 561 | for ($m=$nsig-1;$m>=$nsig-$k;$m--) { | ||
| 562 | $index=$vstart+((($m*$cols)+$j)*$doubsize); | ||
| 563 | seek(LAV2,$index,0); | ||
| 564 | read(LAV2,$value,$doubsize); | ||
| 565 | if (!defined $value) { | ||
| 566 | print STDERR "ERROR($0): | ||
| 567 | lav2 file <$lav2> doesn't have sufficient S-vectors.\n"; | ||
| 568 | exit; | ||
| 569 | } | ||
| 570 | # unpacking | ||
| 571 | $value = unpack("d", $value); | ||
| 572 | set($v,$nsig-1-$m,$value); | ||
| 573 | } | ||
| 574 | $recon=$u * $d(0:$k-1) x transpose $v; | ||
| 575 | $number=sprintf($format,$recon->sclr); | ||
| 576 | |||
| 577 | # if --negatives is on, then values between -1 and 0 are set to 0 | ||
| 578 | |||
| 579 | if ((defined $opt_negatives) && ($number=~/\-(0.)?0+/)) { $number=0; } | ||
| 580 | |||
| 581 | if ($number<$lower_format) { | ||
| 582 | print STDERR "ERROR($0): | ||
| 583 | Floating point underflow. | ||
| 584 | Value <$number> can't be represented with format $format.\n"; | ||
| 585 | exit 1; | ||
| 586 | } | ||
| 587 | if ($number>$upper_format) { | ||
| 588 | print STDERR "ERROR($0): | ||
| 589 | Floating point overflow. | ||
| 590 | Value <$number> can't be represented with format $format.\n"; | ||
| 591 | exit 1; | ||
| 592 | } | ||
| 593 | printf($format,$number); | ||
| 594 | } | ||
| 595 | print "\n"; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | |||
| 599 | # | ||
| 600 | # recontruct only row vectors | ||
| 601 | # | ||
| 602 | |||
| 603 | elsif ($opt_output eq "rowonly") { | ||
| 604 | |||
| 605 | for ($i=0;$i<$rows;$i++) { | ||
| 606 | for ($j=$nsig-1;$j>=$nsig-$k;$j--) { | ||
| 607 | |||
| 608 | # computing index of each value from beginning of file | ||
| 609 | |||
| 610 | $index = $ustart+((($j*$rows)+$i)*$doubsize); | ||
| 611 | seek(LAV2,$index,0); | ||
| 612 | read(LAV2,$value,$doubsize); | ||
| 613 | if (!defined $value) { | ||
| 614 | print STDERR "ERROR($0): | ||
| 615 | lav2 file <$lav2> doesn't have sufficient left S-vectors.\n"; | ||
| 616 | exit; | ||
| 617 | } | ||
| 618 | # unpacking | ||
| 619 | |||
| 620 | $value = unpack("d", $value); | ||
| 621 | |||
| 622 | # if the sqrt option is turned on, then take the | ||
| 623 | # square root of the kxk matrix when building the | ||
| 624 | # --row-only reconstruction | ||
| 625 | |||
| 626 | # see discussion in bugs about this issue | ||
| 627 | |||
| 628 | if (defined $opt_sqrt) { | ||
| 629 | |||
| 630 | $recon = $value * sqrt at($d,$nsig-1-$j); | ||
| 631 | } | ||
| 632 | else { | ||
| 633 | $recon = $value * at($d,$nsig-1-$j); | ||
| 634 | } | ||
| 635 | |||
| 636 | $number=sprintf($format,$recon); | ||
| 637 | |||
| 638 | # if --negatives is on, then values between -1 and 0 are set to 0 | ||
| 639 | |||
| 640 | if ((defined $opt_negatives) && ($number=~/\-(0.)?0+/)) { $number=0; } | ||
| 641 | |||
| 642 | if ($number<$lower_format) { | ||
| 643 | print STDERR "ERROR($0): | ||
| 644 | Floating point underflow. | ||
| 645 | Value <$number> can't be represented with format $format.\n"; | ||
| 646 | exit 1; | ||
| 647 | } | ||
| 648 | if ($number>$upper_format) { | ||
| 649 | print STDERR "ERROR($0): | ||
| 650 | Floating point overflow. | ||
| 651 | Value <$number> can't be represented with format $format.\n"; | ||
| 652 | exit 1; | ||
| 653 | } | ||
| 654 | printf($format,$number); | ||
| 655 | } | ||
| 656 | print "\n"; | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | ############################################################################## | ||
| 661 | |||
| 662 | # ========================== | ||
| 663 | # SUBROUTINE SECTION | ||
| 664 | # ========================== | ||
| 665 | |||
| 666 | #----------------------------------------------------------------------------- | ||
| 667 | #show minimal usage message | ||
| 668 | sub showminimal() | ||
| 669 | { | ||
| 670 | print "Usage: svdpackout.pl [OPTIONS] LAV2 LAO2"; | ||
| 671 | print "\nTYPE svdpackout.pl --help for help\n"; | ||
| 672 | } | ||
| 673 | |||
| 674 | #----------------------------------------------------------------------------- | ||
| 675 | #show help | ||
| 676 | sub showhelp() | ||
| 677 | { | ||
| 678 | print "Usage: svdpackout.pl [OPTIONS] LAV2 LAO2 | ||
| 679 | |||
| 680 | Reconstructs a rank-k matrix from output files LAV2 and LAO2 created by las2 | ||
| 681 | program of SVDPack. | ||
| 682 | |||
| 683 | LAV2 | ||
| 684 | Binary output file created by las2 | ||
| 685 | |||
| 686 | LAO2 | ||
| 687 | ASCII output file created by las2 | ||
| 688 | |||
| 689 | OPTIONS: | ||
| 690 | |||
| 691 | --rowonly | ||
| 692 | Reconstructs only row vectors, output to STDOUT. May not be | ||
| 693 | used with --output | ||
| 694 | |||
| 695 | --output OUTPUT | ||
| 696 | reconstruct - reconstructs the rank-k matrix, output to STDOUT | ||
| 697 | rowonly - same as --rowonly, output to STDOUT | ||
| 698 | components - output U, S, VT matrices to U.txt, S.txt, VT.txt | ||
| 699 | |||
| 700 | --sqrt | ||
| 701 | In --rowonly reconstruction, take square root of S (k x k). | ||
| 702 | This was the default in SenseClusters 1.02 and previous. | ||
| 703 | |||
| 704 | --negatives | ||
| 705 | Sets all negative values in reconstructed matrix to 0 (except | ||
| 706 | in component output). This was the default in SenseClusters | ||
| 707 | 1.00 and previous. | ||
| 708 | |||
| 709 | --format FORM | ||
| 710 | Specifies the format for displaying output matrix. Default is f16.10. | ||
| 711 | |||
| 712 | --help | ||
| 713 | Displays this message. | ||
| 714 | --version | ||
| 715 | Displays the version information. | ||
| 716 | |||
| 717 | Type 'perldoc svdpackout.pl' to view detailed documentation of svdpackout.\n"; | ||
| 718 | } | ||
| 719 | |||
| 720 | #------------------------------------------------------------------------------ | ||
| 721 | #version information | ||
| 722 | sub showversion() | ||
| 723 | { | ||
| 724 | # print "svdpackout.pl - Version 0.04\n"; | ||
| 725 | print '$Id'; | ||
| 726 | print "\nReconstruct a rank-k matrix from output of SVDPACKC\n"; | ||
| 727 | # print "Copyright (c) 2002-2005, Amruta Purandare & Ted Pedersen.\n"; | ||
| 728 | # print "Date of Last Update: 06/02/2004\n"; | ||
| 729 | } | ||
| 730 | |||
| 731 | ############################################################################# | ||
| 732 |