#!/usr/local/bin/perl # NB up til 13 June 93 this didn't evaluate sd correctly. # see also rcv.p # head -2000 _res | histo.p min=-1 max=1 c=1 out=_his # or to get all columns used inhistogram: # histo.p c=0 > _his # histo returns a gaussian fitted to the data, and binned data # for histogram plotting. $min=-1; $max=1; $bins=50; $c=1; $col=-1; $scale=1; $out="_his"; $adjust=0; # for shifting the data. $l=0; # whether to log transform. $offset=0.05; $line = 0; $truemax = - 1e200 ; $truemin = 1e200 ; # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if ( $out ne "" ) { open ( OUT , ">$out" ) ; } else { open ( OUT , STDOUT ) ; $out = "_his" ; } if ( $col != -1 ) { $c = $col ; } # alternative command line arg if ( !$printall ) { if ($c==0 ) { $printall=1; } else { $printall=0; $c--; } } $count = 0.0; $cum=0.0; $sum=0.0; $width=($max-$min)*1.0/(1.0*$bins); printf STDERR "min = %5f, max = %5f, bins = %d. gnuplot\n", $min, $max, $bins; printf STDERR "plot '$out' u 1:2 w boxes, '$out' u 1:3 w l\n"; MAIN: while (<>) { $line++ ; s/^[\s\[]*//; s/\]/ /; if(/^\#/) { next MAIN ; } @fields = split(/[\s\,\t]+/,$_); # was split ( ' ' , $_ ) ; # print "$line :", $#fields , "\n" ; if( $printall ) { $c=0; } if($#fields >= $c) { do { $x = $fields[$c]; if (($x eq "-")|| ($x eq "NA" ) ) { # next DLOOP ; } else { $x += $adjust ; if ( $l > 0 ) { $x = log ( $x ) ; } # log base e I presume if ( $x > $truemax ) { $truemax = $x ; } if ( $x < $truemin ) { $truemin = $x ; } $cum += $x * $x ; $sum += $x ; $count++ ; $i = (($x-$min)/$width) ; if( $i < 0 ) { $i = 0 ; } elsif ( $i > $bins ) { $i = $bins ; } $countarr[$i]++; } } while ( $printall && ( $c++ < $#fields) ) ; } elsif ( $c > 0 ) { printf STDERR "Skipping line %d - %d items %d col\n", $line , $#fields+1 , $c+1 ; } } $xbar = $sum/$count ; $var = ($cum/$count - $xbar*$xbar) ; $sd = ( $var > 0.0000000000001 ) ? sqrt ( $count * $var / ($count-1.0) ) : 1.0 ; $sdz = sqrt( $cum/$count ) ; if ( $scale != 1 ) { $count *= $scale ; } for ( $i = 0 ; $i <= $bins ; $i ++ ) { if ( $scale != 1 ) { $countarr[$i] *= $scale ; } $x = $min + ( $i + 0.5 ) * $width ; printf OUT "%f %f %f %f\n",$x,$countarr[$i]+$offset,&gauss($x-$xbar,$sd)*$count*$width,&gauss($x,$sdz)*$count*$width ; } printf STDERR "count = %d, mu = %5f, SumSquareDevns = %5f, sd about zero = %5f, sd = %5f, range = %f,%f\n", $count, $xbar, $var *$count , $sdz, $sd , $truemin , $truemax ; sub gauss { #usage ( $x-$mu , $sd ) local( $x , $sd ) = @_ ; local($stpi) = sqrt(2*3.1415) ; return ( exp ( - $x*$x/(2*$sd*$sd) ) / ( $stpi * $sd ) ) ; }