#!/usr/local/bin/perl # binnify: Bins continuous data and displays histograms # Copyright (C) 2000 Ramin Nakisa # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require "getopts.pl"; &Getopts('schvgn:u:l:'); if (defined($opt_h)) { print <= 0.5 ) { return( int( $f ) + 1 ); } else { return( int( $f ) ); } } if ( $opt_n <= 0 ) { die "Must have a number of bins >= 1.\n"; } $MinVal = 1.0e10; $MaxVal = -1.0e10; $line = 0; while (<>) { chop; $value[$line] = $_; if ( $value[$line] < $MinVal ) { $MinVal = $value[$line]; } if ( $value[$line] > $MaxVal ) { $MaxVal = $value[$line]; } $line++; } if ( defined( $opt_l ) ) { $MinVal = $opt_l; } if ( defined( $opt_u ) ) { $MaxVal = $opt_u; } $NumberOfLines = $line; if ( $MaxVal == $MinVal ) { die "Error: Lowest value equals highest value.\n"; } $DataRange = $MaxVal - $MinVal; $BinWidth = $DataRange / $opt_n; for ( $i = 0; $i < $opt_n; $i++ ) { $BinCount[$i] += 0; $BinMin[$i] = $MinVal + $i * $BinWidth; $BinMax[$i] = $MinVal + ( $i + 1 ) * $BinWidth; } for ( $i = 0; $i < $NumberOfLines; $i++ ) { # $BinIndex = &round( ($opt_n-1.0) * ( $value[$i] - $MinVal ) / $DataRange ); $BinIndex = int( ( 1.0 * $value[$i] - $MinVal ) / $BinWidth ); if ( $BinIndex > $opt_n - 1 ) { $BinIndex = $opt_n - 1; } if ( $BinIndex < 0 ) { $BinIndex = 0; } if ( defined( $opt_v ) ) { printf "Value[%d]: %6.4f BinIndex: %d\n", $i, $value[$i], $BinIndex; } if ( defined( $opt_c ) ) { printf "%d\n", $BinIndex; } $BinCount[$BinIndex]++; } $MaxBinCount = -1; for ( $i = 0; $i < $opt_n; $i++ ) { if ( $BinCount[$i] > $MaxBinCount ) { $MaxBinCount = $BinCount[$i]; } } # Ensure histogram for xgobi drops to zero axis for first bar if ( defined( $opt_g ) ) { printf "%6.4f %6d\n", $BinMin[0], 0; } for ( $i = 0; $i < $opt_n; $i++ ) { if ( defined( $opt_v ) ) { printf "Bin: %4d MinVal: %6.4f MaxVal: %6.4f Count: %6d\n", $i, $BinMin[$i], $BinMax[$i], $BinCount[$i]; } elsif ( defined( $opt_s ) ) { printf "%10g ", ($BinMin[$i] + $BinMax[$i])/2.0; printf "%6d ", $BinCount[$i]; for ( $j = 0; $j < &round( 60.0 * $BinCount[$i] / $MaxBinCount ); $j++ ) { print "*"; } print "\n"; } elsif ( defined( $opt_g ) ) { printf "%6.4g %6d\n%6.4g %6d\n", $BinMin[$i], $BinCount[$i], $BinMax[$i], $BinCount[$i]; } elsif ( !defined( $opt_c ) ) { printf "%6.4g %-6d\n", ($BinMin[$i] + $BinMax[$i])/2.0, $BinCount[$i]; } } # Ensure histogram for xgobi drops to zero axis for final bar if ( defined( $opt_g ) ) { printf "%6.4g %6d\n", $BinMax[$opt_n-1], 0; }