#!/usr/local/bin/perl # hintond.p # Makes hinton diagram from a file containing a list of floats in ascii # produces encapsulated postscript # - but I am not sure that the bounding box is right! # # usage # hintond.p o=hd.ps zero=0.0 range=2.0 rows=4 20 20 20 1 weights # the string of integers are number-per-line as in returns.p # # transformation rules: # 0 linear from min to max defined by zero and range # 1 log from min to max # # examples: (here rand.p is /usr/export/home/mackay/book/FIGS/compon/rand.p , # which creates about 40*20 random numbers) # rand.p | hintond.p zero=0.0 range=1.0 rows=40 background=0.0 20 # rand.p | hintond.p zero=0.0 range=1.0 rows=40 background=1.0 o=hd2.ps stroke="stroke" 20 # rand.p | hintond.p zero=0.0 range=1.0 rows=40 pos=0.0 background=1.0 o=hd2.ps stroke="stroke" 20 # rand.p | hintond.p zero=0.0 range=1.0 rows=40 pos=0.0 background=1.0 o=hd2.ps gridy=1 margin=0 20 # hintond.p # # if the dhs are used then the int arguments on command line only affect the # box width via the largest int given # eg # hintond.p rows=6 dh=1 8 w # hintond.p bellh=21 bellw=6 h=5 maxbox=2 m=1 dh=1 serpent=1 12 b11g # when there is a dh file then thhe rows need not be specified, but can be, # to override default. # # additional option: greyscale=1 -> makes all boxes same size # and shows the number by grayscale instead. # example usage appropriate for bijan: # in _tools/hintond: # hintond.p zero=0.0 range=16.0 rows=4 greyscale=1 o=f.ps pos=0.0 background=1.0 margin=0 4 < f # example for the case where f contains 32 lines, to be displayed as # 8 blocks of 4*4: # hintond.p zero=0.0 range=16.0 rows=32 greyscale=1 o=f.ps pos=0.0 background=1.0 margin=0 bellh=4 bellw=4 4 < f $dobackground = 1 ; # whether to do background boxes $renderzeroes = 0 ; # whether to plot boxes of zero size # new in june 99 $horizontaltweak90 = -2 ; # for the top labels when they go upright $horizontaltweak0 = -2 ; # for the top labels $verticaltweak = -2.3 ; # for the side labels $labels= 0 ; $laborient = 90 ; # orientation for the labels along the x axis $labelfile = "" ; $fontsize = 8 ; $xoffset = 10 ; $yoffset = 10 ; $labeltopside = 1 ; # whether to label the topside # labels to be added along the sidelines $verbose=0; $greyscale = 0 ; # original default. if set to 1, boxes are fixed size # and number is shown by greylevel. $serpent=0 ; # whether to weave to and fro # as the big blocks are laid. $direction=1 ; # initial direction to go in. $miniserpent = 0 ; $minidirection = 1 ; $bellh=0; # the number of rows that can be done before a `carriage return' # to a new column. $bellw=0; # the number of rectabgles before a new row of them started $bellgap=10; # how much space between pics. $dh=0; $dhfile=""; $background=0.5; # colour of surrounding box $neg = 0.0 ; # colour of negatives $pos = 1.0 ; # colour of positive areas ( 1.0 is white ) $o="hd.ps"; $h=10; # box space $maxbox=0; # max box radius , set to h/2 below if not changed from 0 $m=4; # margin $margin=""; $zero = 0.0 ; $range = 1.0 ; $rotate = 0 ; # whether to rotate the stuff $r = 0 ; # whether to give log transformation $rows=0; # number of rows (is calculated from dh if dh is given) $cols=0; $skippy=0; # if skippy > 0 then every skippy columns a space is inserted. # not yet done. $grid=0; # $x0=0; $y0=0; $x0=40; $y0=40; $stroke = ""; # change this to "stroke" to get lines round all boxes. # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $l=0; while ( $ARGV[0]=~ /^(\d+)/ ) { $num[$l++]=$1; if($1 > $cols ) {$cols = $1; } shift; } $lmax = $l ; # what remains in argv is used as filenames for input if ( $dhfile ne "" ) { $dh = 1 ; } elsif ( $dh == 1 ) { # there had better be another ARGV, the weight file, to which .d is added. $dhfile = $ARGV[0].".d" ; print "going to read dh from $dhfile\n" ; } if ( $margin ne "" ) { $m = $margin ; } if ( $maxbox == 0 ) { $maxbox = $h / 2 ; } if ( $dh ) { open ( DH , "<$dhfile" ) ; @deltas = ; # reads in the whole lot $joined = join ( ' ', @deltas ) ; $num_of_rows = 0 ; @dhs = split ( /[ \t\n]+/ , $joined ) ; foreach $arg ( @dhs ) { if ( $arg =~ /\b0\b/ ) { $num_of_rows ++ ; } } if ( !$rows ) { $rows = $num_of_rows ; } $K = $#dhs + 1 ; print "read in dh array - $K weights in $num_of_rows rows\n" ; close ( DH ) ; } if ( !$rows ) { print "Warning, no rows given on command line or in dhfile\n" ; } $onewidth = ( $cols * $h ) + ( 2 * $m ) ; # if ( $bellh == 0 ) { $numbellhs = 1 ; # $numbellws = 1 ; $oneheight = ( $rows * $h ) + ( 2 * $m ) ; } else { $oneheight = ( $bellh * $h ) + ( 2 * $m ) ; $numbellhs = int ( ($rows / $bellh) + 0.49) ; # weird behaviour of this: # print $rows / $bellh , " ? " , ( ($rows % $bellh) > 0 ? 0 : 1 ) ; if ( $bellw != 0 ) { $numbellws = int ( $numbellhs / $bellw + 0.999 ) ; } else{ $height = $oneheight ; $numbellws = 1 ; } } if($verbose) { print "numbellhs = $numbellhs: " ; print "numbellws = $numbellws: cols = $cols\n" ; } if ( $bellw == 0 ) { $width = $onewidth * $numbellhs + ( $numbellhs - 1 ) * $bellgap ; } else { $width = $onewidth * $bellw + ( $bellw - 1 ) * $bellgap ; print "width = $onewidth * $bellw + ( $bellw - 1 ) * $bellgap = $width\n" ; } $height = $numbellws * $oneheight + ( $numbellws - 1 ) * $bellgap ; print "height = $numbellws * $oneheight + ( $numbellws - 1 ) * $bellgap = $height\n" if($verbose); open ( OUT , ">$o" ) ; print OUT "%!\n" ; print OUT "%%Title: none\n" ; print OUT "%%Creator: hintond.p\n" ; print OUT "%%Pages: 1\n" ; print OUT "%%BoundingBox:" ; if ( $labels ) { $extra = $xoffset ; } else { $extra = 0 ; } if( $rotate == 1 ) { printf OUT " %d %d %d %d\n" , $y0, $x0-$extra , $y0 + $height+$extra , $x0 + $width ; } else { printf OUT " %d %d %d %d\n" , $x0-$extra ,$y0 , $x0 + $width ,$y0 + $height+$extra ; } print OUT "%%EndComments\n" ; print OUT "/\$F2psDict 32 dict def \n" ; print OUT "\$F2psDict begin\n" ; print OUT " \$F2psDict /mtrx matrix put\n" ; print OUT "\n" ; print OUT " end\n" ; print OUT " /\$F2psBegin {\$F2psDict begin /\$F2psEnteredState save def} def\n" ; print OUT " /\$F2psEnd {\$F2psEnteredState restore end} def\n" ; print OUT " %%EndProlog\n" ; print OUT "\n" ; print OUT "\$F2psBegin\n" ; print OUT "1 setlinecap 1 setlinejoin\n" ; print OUT "0.10 setlinewidth\n" ; $/ = ""; # read in by the paragraph $* = 1 ; if ( $r == 1 ) { $zero = log( $zero ) ; $range = log( $range ) ; } if ( $dobackground ) { # Do the background box(es) print OUT "% background boxes\n" ; for ( $i = 1 , $x=$x0+$onewidth/2 , $y = $y0 + $oneheight/2 ; $i <= $numbellhs ; ){ &box ( $x , $y , $onewidth/2 , $oneheight/2 , $background , "stroke") ; if ( $gridy ) { for ( $yy = $y - $oneheight/2 + $gridy * $h + $h / 2 ; $yy < $y +$oneheight / 2 - $m ; $yy += $gridy * $h ) { &line ( $x - $onewidth / 2 , $yy , $x + $onewidth / 2 , $yy ) ; } # } # if ( $grid ) { for ( $xx = $x - $onewidth/2 + $grid * $h - $h / 2 ; $xx < $x0 + $onewidth/2 - $m ; $xx += $grid * $h ) { &line ( $xx , $y - $oneheight / 2 , $xx , $y + $oneheight / 2 ) ; } } $i++ ; $x+=$onewidth + $bellgap ; if ( $bellw && ( ($i+$bellw-1) % $bellw == 0 ) ) { $x=$x0+$onewidth/2 ; $y += $oneheight + $bellgap ; } } } $l=0; $thiscol=0; $currentrow = 1 ; $x = $orig_startx = $startx = $x0 + $m + $h / 2 ; $y = $starty = $y0 + $m + $h / 2 ; $megacol = 1 ; $k = 0 ; # reserved name for the current weight number while( ( $_ = <> ) && ( $currentrow <= $rows) ){ s/^\s*//; # get rid of initial gap on line s/^#[^\n]*\n//g; # get rid of comment lines @fields=split ; if ( $#fields >= 0 ) { # corrected this from > 0 16 june 94 for ( $i = 0 ; $i <= $#fields; $i++) # <= is right. { # print $fields[$i] ; # print " "; if ( $max && $fields[$i] > $max ) { $fields[$i] = $max ; } if ( $min && $fields[$i] < $min ) { $fields[$i] = $min ; } $radius = &size ( $fields[$i] ) * $maxbox ; if ( &sign ( $fields[$i] ) < 0.0 ) { $g = $neg ; # 0.0 ; } else { $g = $pos ; } # 1.0 if ( ( !( $fields[$i] == $zero ) ) || $renderzeroes ) { if ( ( $stroke ne "" ) || ( $g != $background ) ) { if ( $greyscale == 0 ) { &box ( $x , $y , $radius , $radius , $g , $stroke ) ; } else { &box ( $x , $y , $maxbox , $maxbox , $radius/$maxbox , $stroke ) ; } } } if ( $dh == 0 ) { $thiscol++; $x+= $minidirection * $h ; while($thiscol==@num[$l]){ &end_of_row ( ) ; } } else { # the dhs determine the number in each row, and the spaces. if ( $dhs[$k] == 0 ) { &end_of_row ( ) ; } else { $thiscol += $dhs[$k] ; $x += $h * $dhs[$k] ; } $k ++ ; } if( $currentrow > $rows ) { last; } } } # print "\n"; }# # add labels if ( $labels ){ &preparelabels ; print "# row labels first\n" ; open (LAB, "<$labelfile" ) ; @labels = ; @labs = split( '\n' , $labels[0] ) ; $l=0; $thiscol=0; $currentrow = 1 ; $x = $orig_startx - 2*$h - $xoffset ; $y = $starty + $verticaltweak ; $lll= 0 ; while( ( $currentrow <= $rows) ){ $lab=$labs[$lll] ; # print "$currentrow/$rows $lab \n" ; &label ( $x , $y , $lab , 0 ) ; $y += $h ; $currentrow ++ ; $lll ++ ; } print "# then col labels\n" ; $l=0; $thiscol=0; $currentrow = 1 ; $x = $orig_startx + $h/2 + $horizontaltweak90 ; # displace a little because font offset if ( $laborient == 0 ) { $x += $horizontaltweak0 - $horizontaltweak90 - $h/2 ; } if ( !$labeltopside ) { $y = $starty - $yoffset ; } else { $y = $starty +$height -$h + $yoffset ; } $i = 0 ; while( ( $i <= $#fields) ){ $lab=$labs[$lll] ; # &label ( $x , $y , $lab , $laborient ) ; $thiscol++; $x+= $h ; # $i ++ ; $lll ++ } } print OUT "\$F2psEnd\nshowpage\n" ; sub preparelabels { print OUT "%\n" ; print OUT "% Labels start here\n" ; print OUT "%\n" ; # print OUT "/Times-Roman findfont % Get the basic font\n" ; print OUT "/Courier findfont % Get the basic font\n" ; print OUT "$fontsize scalefont % Scale the font to 20 points\n" ; print OUT "setfont % Make it the current font\n" ; } sub label { # usage (x,y,string,orientation) local( $y , $x , $s , $o ) = @_ ; if($rotate==0) { # global variable $tmpo = $x ; $x = $y ; $y = $tmpo ; } print OUT "newpath\n" ; printf OUT "%6.2f %6.2f moveto\n" , $x , $y ; if ( $o != 0 ) { print OUT "gsave\n" ; print OUT "$o rotate\n" ; } print OUT "($s) show\n" ; if ( $o != 0 ) { print OUT "grestore\n" ; } } sub size { # returns something between 0 and 1 #usage ( $x ) local( $x ) = @_ ; if ( $r == 1 ) { $x = log ( $x ) ; } $ans = ($x-$zero)/$range ; if ( $ans < -0.0 ) { $ans = - $ans ; } if ( $ans > 1.0 ) { $ans = 1.0 ; } return sqrt($ans) ; } sub sign { # returns -1 / 1 #usage ( $x ) local( $x ) = @_ ; if ( $r == 1 ) { $x = log ( $x ) ; } $ans = ($x-$zero) ; if ( $ans > 0.0 ) { $ans = 1.0 ; } else { $ans = -1.0 ; } return $ans ; } sub box { # draws a box centred on x,y with half width specified. # to get a box with an edge, set $stroke=stroke ; #usage ( $x , $y , $xsize , $ysize, $gray ) local( $y , $x , $ysize, $xsize, $gray , $stroke ) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; $tmpo = $xsize ; $xsize = $ysize ; $ysize = $tmpo ; } printf OUT "newpath %6.4f %6.4f moveto %6.4f %6.4f lineto %6.4f %6.4f lineto %6.4f %6.4f lineto closepath gsave %6.4f setgray fill grestore %s\n" , $x+$xsize, $y+$ysize, $x+$xsize, $y-$ysize, $x-$xsize, $y-$ysize, $x-$xsize, $y+$ysize, $gray , $stroke ; } sub line { # draws a line #usage ( $x , $y , $x2 , $y2 ) ; local( $y , $x , $y2, $x2 ) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; $tmpo = $x2 ; $x2 = $y2 ; $y2 = $tmpo ; } printf OUT "newpath %6.4f %6.4f moveto %6.4f %6.4f lineto stroke\n" , $x,$y, $x2,$y2 ; } sub end_of_row { $l++; $y += $h ; $currentrow ++ ; if($l>=$lmax){$l=0;} if ($bellh && (($currentrow+$bellh-1)%$bellh)==0) { $megacol ++ ; if ( $bellw && ( ( ($megacol+$bellw-1) % $bellw ) == 0 ) ) { $starty += $oneheight + $bellgap ; if ( $serpent ) { $direction *= -1 ; } else { $startx = $orig_startx ; } } else { $startx += $direction * ( $onewidth + $bellgap ) ; } $y = $starty ; } if ( $miniserpent ) { $minidirection *= -1 ; $x+= $minidirection * $h ; } # not compatible with dhs. else { $x = $startx ; } $thiscol=0; }