home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7400 < prev    next >
Encoding:
Text File  |  1992-12-14  |  6.7 KB  |  282 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!caen!uvaarpa!mmdf
  3. From: Dov Grobgeld <dov@menora.weizmann.ac.il>
  4. Subject: Re: Statistic routines ?
  5. Message-ID: <1992Dec14.075602.5410@uvaarpa.Virginia.EDU>
  6. Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
  7. Reply-To: dov@menora.weizmann.ac.il
  8. Organization: The Internet
  9. Date: Mon, 14 Dec 1992 07:56:02 GMT
  10. Lines: 270
  11.  
  12. For all it's worth. Here are a couple of routines that I have written
  13. that I find very useful in my data manipulations.
  14.  
  15. <<File: histbin>>
  16. #!/usr/local/bin/perl
  17. #############################################################################
  18. #  histbin
  19. #
  20. #  version 0.1
  21. #
  22. #  Bin data for a histogram.
  23. #
  24. #  Dov Grobgeld
  25. #  Department of Chemical Physics
  26. #  The Weizmann Institute of Science
  27. #  Rehovot, Israel
  28. #  Email: dov@menora.weizmann.ac.il
  29. #
  30. #  1992-06-16
  31. #############################################################################
  32.  
  33. # Get options
  34. while (($_ = $ARGV[0]) =~ /^-/) {
  35.     shift;
  36.     if (/-nBins$/i) {
  37.         $nBins= $ARGV[0];
  38.         shift;
  39.     }
  40.     elsif (/-intv$/i) {
  41.         $binMin= $ARGV[0]; shift;
  42.         $binMax= $ARGV[0]; shift;
  43.     }
  44.     elsif (/-n(orm)?/i) {
  45.         $normalize++;
  46.     }
  47.     elsif (/-h(elp)?$|-u(sage)?$/i) {
  48.         print<<EOH
  49. histbin -  Bins data according to user specifications
  50.  
  51. Syntax:
  52.  
  53.     histbin [-nBins n] [-intv min max] [-norm] <file>
  54.  
  55.     where <file> is a text file containing a column of values to bin.
  56.  
  57. Options:
  58.  
  59.     -nBins n        Number of bins. Default is n=100.
  60.     -intv min max   Interval for binning. Default is min=0 and max=1.
  61.     -norm           Normalize the histogram.
  62. EOH
  63. ;
  64.     exit;
  65.     }
  66. }
  67.  
  68.  
  69. # Set default values
  70. $binMin=0.0 unless defined $binMin;
  71. $binMax=1.0 unless defined $binMax;
  72. $nBins=100 unless defined $nBins;
  73.  
  74. # Construct index array...
  75. for $i (0..$nBins-1) {
  76.     $xBins[$i]= $binMin + (1.0 * $i / $nBins) * ($binMax - $binMin);
  77. }
  78.  
  79. # Clear the bin array
  80. for $i (0..$nBins-1) {
  81.     $binCount[$i]=0;
  82. }
  83.  
  84. # Read the data file
  85. while (<>) {
  86.    next if (/^#/);  # Skip comments
  87.    ($x=$_) =~ s/^\s+//; $x=~ s/\n//;
  88.    next unless $x=~ /^\d+(\.\d*)?([de][\+\-]?\d+)?$/o;
  89.    if (++$numEntries % 100 == 0) {printf STDERR ("%5d\r",$numEntries); }
  90.    $binIdx= int(1.0 * ($x-$binMin) * $nBins / ($binMax - $binMin));
  91.    $binCount[$binIdx]++ unless $binIdx < 0;
  92.    if ($binIdx >= $nBins || $binIdx < 0) {
  93.        printf STDERR "Warning! Value $x outside of binning range\n";
  94.    }
  95. }
  96.  
  97. # print the data
  98. for $i (0..$nBins-1) {
  99.    if ($normalize) {$binCount[$i] = $binCount[$i]/$numEntries; }
  100.    printf ("%12.8f %15.8g\n",$xBins[$i], $binCount[$i]);
  101. }
  102.  
  103. print STDERR "Done binning $numEntries values!\n";
  104.  
  105.  
  106. <<File: histrebin>>
  107. #!/usr/local/bin/perl
  108. ##########################################################################
  109. #  histrebin
  110. #
  111. #  version 0.1
  112. #
  113. #  Reads a file of bin values and frequency count and rebins it by
  114. #  a user defined factor. Optionally normalizes the histogram.
  115. #
  116. #  Dov Grobgeld
  117. #  Department of Chemical Physics
  118. #  The Weizmann Institute of Science
  119. #  Rehovot, Israel
  120. #  Email: dov@menora.weizmann.ac.il
  121. #
  122. #  1992-06-16
  123. ##########################################################################
  124.  
  125. # Get options
  126. while (($_ = $ARGV[0]) =~ /^-/) {
  127.     shift;
  128.     if (/-n(orm)?$/) {
  129.         $normalize++;
  130.     }
  131.     elsif (/-s(trip)?$/) {
  132.         $stripComment++;
  133.     }
  134.     elsif (/-h(elp)?$|-u(sage)$/) {
  135.     print <<EOHELP
  136. histrebin - rebin a histogram
  137.  
  138. Syntax:
  139.  
  140.      histrebin [-n] [-s] <file> <factor>
  141.  
  142.      where <file> is the file to rebin by a a factor of <factor>. <factor>
  143.      will default to 1, if left out.
  144.  
  145.      histrebin will sum the values of each <factor> bins and create one
  146.      new bin with the accumulated sum.
  147.  
  148. Options:
  149.  
  150.   -n   will cause the histogram to be normalized.
  151.   -s   will strip comments.
  152.  
  153. Examples:
  154.  
  155.     1. Rebin a histogram by a factor of 4. This would e.g. create 25
  156.        bins instead of 100.
  157.  
  158.        histrebin binned100.dat 4 > binned25.dat
  159.  
  160.     2. Sum all the 100 bins in the file binned100.dat
  161.  
  162.        histrebin binned100.dat 100
  163.  
  164.     3. Normalize the histogram in binned100.dat
  165.  
  166.        histrebin -norm binned100.dat > binned100norm.dat
  167.  
  168. EOHELP
  169. ;   exit;
  170.     }
  171.     else {
  172.         die "Unknown option $_!\n";
  173.     }
  174. }
  175.  
  176. $infile=shift;
  177. open (IN, $infile) || die "No such file $infile!\n";
  178. ($factor=shift) || ($factor=1);
  179. if ($factor==0) { die "Invalid factor!\n"; }
  180.  
  181. # If normalize then go through the whole file and sum the histogram...
  182. $totalsum=0.0;
  183. if ($normalize) {
  184.     while (<IN>) {
  185.         next if (/^#/);
  186.         if (/^\s*(\S+)\s+(\S+)/) { $totalsum += $2; }
  187.     }
  188.     close(IN);
  189.     open(IN, $infile);
  190. }
  191.  
  192. $bunchindex=0;     # Number of bins summed into the bunch
  193. $bunchsum=0;       # Sum of all entries in the bin
  194. $binlow=0;         # Lower limit of the bin
  195. while (<IN>) {
  196.     if (/^#/) {
  197.       print unless ($stripComment);
  198.     }
  199.     elsif (/^\s*(\S+)\s+(\S+)/) {   # Assume naively that these are numbers...
  200.         $bunchindex++;
  201.         $binlow = 0.0 + $1 if ($bunchindex == 1);  # Convert to floating point
  202.         $bunchsum += $2;
  203.         if ($bunchindex == $factor) {
  204.             $bunchsum= 1.0 * $bunchsum/$totalsum if $normalize;
  205.             printf("%8.5f %10.8g\n", $binlow, $bunchsum);
  206.             $bunchsum=0;
  207.             $bunchindex=0;
  208.         }
  209.     }
  210. }
  211.  
  212. <<File: cutcol>>
  213. #!/usr/local/bin/perl
  214. ##############################################################################
  215. #  cutcol
  216. #
  217. #  Version 0.1
  218. #
  219. #  Cut columns out of a data file. Great as a prefilter for xgraph.
  220. #
  221. #  Dov Grobgeld
  222. #  Email: dov@menora.weizmann.ac.il
  223. #  1992-06-16
  224. ###############################################################################
  225. while (($_= $ARGV[0]) =~ /^-/) {
  226.     shift;
  227.     if (/-c$/) {    # Other columns than default ones
  228.         $col1= $ARGV[0]; shift;
  229.         $col2= $ARGV[0]; shift;
  230.     }
  231.     elsif (/-s$/) {
  232.         $strip++;
  233.     }
  234.     elsif (/^-h(elp)?$|^-u(sage)?$/) {
  235.         print <<EOH
  236. cutcol  -  Cuts 2 columns out of a file
  237.  
  238. Syntax:
  239.  
  240.     cutcol [-c n1 n2] [-h] [-s]
  241.  
  242. Options:
  243.  
  244.     -c n1 n2    Specify columns to cut out other than the default n1=1, n2=2
  245.  
  246.     -h          This help file
  247.  
  248.     -s          Strip comments
  249. EOH
  250. ;
  251.         exit;
  252.     }
  253. }
  254.  
  255. # Default values
  256.  
  257. $col1=1 unless defined $col1;
  258. $col2=2 unless defined $col2;
  259.  
  260. $col1--; $col2--; # Normalize to zero
  261.  
  262. while(<>) {
  263.     if (/^\#/) {
  264.       print unless $strip;
  265.       next;
  266.     }
  267.     s/^\s+//; # Strip leading spaces
  268.     @columns= split;
  269.     print "$columns[$col1] $columns[$col2]\n";
  270. }
  271.  
  272.  
  273.  
  274. --
  275.                                                         ___   ___
  276.                                                       /  o  \   o \
  277. Dov Grobgeld                                         ( o  o  ) o   |
  278. The Weizmann Institute of Science, Israel             \  o  /o  o /
  279. "Where the tree of wisdom carries oranges"              | |   | |
  280.                                                        _| |_ _| |_
  281.                                        
  282.