| 1 | package Text::NumericData::App::txdhistogram; | ||
| 2 | |||
| 3 | use Text::NumericData::App; | ||
| 4 | |||
| 5 | # This is just a placeholder because of a past build system bug. | ||
| 6 | # The one and only version for Text::NumericData is kept in | ||
| 7 | # the Text::NumericData module itself. | ||
| 8 | our $VERSION = '1'; | ||
| 9 | $VERSION = eval $VERSION; | ||
| 10 | |||
| 11 | #the infostring says it all | ||
| 12 | my $infostring = "create histogrms of textual data files | ||
| 13 | |||
| 14 | $0 3 < data.dat | ||
| 15 | would print out a histogram of column 3 in data.dat | ||
| 16 | |||
| 17 | You can also choose to apply a weight to the data points an influence the binning. Default value for the colum is the last one present in the file."; | ||
| 18 | |||
| 19 | our @ISA = ('Text::NumericData::App'); | ||
| 20 | |||
| 21 | sub new | ||
| 22 | { | ||
| 23 | my $class = shift; | ||
| 24 | |||
| 25 | return $class->SUPER::new | ||
| 26 | ({ | ||
| 27 | parconf=> | ||
| 28 | { | ||
| 29 | info=>$infostring # default version | ||
| 30 | # default author | ||
| 31 | # default copyright | ||
| 32 | } | ||
| 33 | , pardef=>[ | ||
| 34 | 'binwidth', 0, 'w', 'width of one bin represented by one histogram point (overrides bincount)' | ||
| 35 | , 'binpoint', -1, 'p', 'align fixed-width bins to this value if >= 0; align to beginning of range if < 0' | ||
| 36 | , 'bincount', 10, 'n', 'specify fixed number of bins (dividing min-max range)' | ||
| 37 | , 'discrete', 0, 'd', 'do not use binning, count on discrete points (text string comparison, no rounding!)' | ||
| 38 | , 'weightcol', 0, 'W', 'column containing weights to add instead of simple counting' | ||
| 39 | , 'cumulative', 0, 'c', 'add columns with cumulative counts at/below and at/above the current bin' | ||
| 40 | ] | ||
| 41 | , filemode=>1 | ||
| 42 | , pipemode=>1 | ||
| 43 | , pipe_init=>\&preinit | ||
| 44 | , pipe_file=>\&process_file | ||
| 45 | }); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub preinit | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | $self->{col} = shift @{$self->{argv}}; | ||
| 52 | $self->{guesscol} = not defined $self->{col}; | ||
| 53 | unless($self->{guesscol} or $self->{col} >= 1) | ||
| 54 | { | ||
| 55 | print STDERR "Need column >= 1!\n"; | ||
| 56 | return -1; | ||
| 57 | } | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub process_file | ||
| 62 | { | ||
| 63 | my $self = shift; | ||
| 64 | my $param = $self->{param}; | ||
| 65 | my $txd = $self->{txd}; | ||
| 66 | |||
| 67 | my $col = $self->{guesscol} | ||
| 68 | ? $txd->columns() | ||
| 69 | : $self->{col}; | ||
| 70 | |||
| 71 | --$col; # Now used as index. | ||
| 72 | my $min = $txd->{data}[0][$col]; | ||
| 73 | my $max = $min; | ||
| 74 | |||
| 75 | # Figure out range and binning. | ||
| 76 | for(@{$txd->{data}}) | ||
| 77 | { | ||
| 78 | $min = $_->[$col] if $_->[$col] < $min; | ||
| 79 | $max = $_->[$col] if $_->[$col] > $max; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $range = $max - $min; | ||
| 83 | |||
| 84 | # A bit messy, but let's play safe on empty range. | ||
| 85 | unless($range > 0) | ||
| 86 | { | ||
| 87 | # Nothing at all? | ||
| 88 | return unless @{$txd->{data}}; | ||
| 89 | # Everything in one place? Let's add it up. | ||
| 90 | my $val = $txd->{data}[0][$col]; | ||
| 91 | my $mass = $txd->{records}; | ||
| 92 | if($param->{weightcol}) | ||
| 93 | { | ||
| 94 | my $wc = $param->{weightcol}-1; | ||
| 95 | $mass = 0; | ||
| 96 | # more elaborate work | ||
| 97 | for(@{$txd->{data}}) | ||
| 98 | { | ||
| 99 | next unless (defined $_->[$col] and defined $_->[$wc]); | ||
| 100 | $mass += $_->[$wc]; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | print {$self->{out}} ${$txd->data_line([$val, $mass])}; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Got a real range, got to get out some bins. | ||
| 108 | my $binwidth = $param->{binwidth}; | ||
| 109 | my $binpoint = $min; | ||
| 110 | if($binwidth > 0) | ||
| 111 | { | ||
| 112 | $binpoint = $param->{binpoint} | ||
| 113 | unless $param->{binpoint} < 0; | ||
| 114 | } else | ||
| 115 | { | ||
| 116 | return unless $param->{bincount} > 0; | ||
| 117 | $binwidth = $range / $param->{bincount}; | ||
| 118 | } | ||
| 119 | return unless $binwidth > 0; | ||
| 120 | |||
| 121 | my $align = int(($min-$binpoint)/$binwidth); | ||
| 122 | ++$align | ||
| 123 | if($binpoint+$align*$binwidth > $min); | ||
| 124 | $binpoint += $align*$binwidth; | ||
| 125 | |||
| 126 | my @hist; # list of bin sums, coordinates added later | ||
| 127 | my @points; | ||
| 128 | |||
| 129 | if($param->{discrete}) | ||
| 130 | { | ||
| 131 | my %dhist; | ||
| 132 | if($param->{weightcol}) | ||
| 133 | { | ||
| 134 | my $wc = $param->{weightcol}-1; | ||
| 135 | for(@{$txd->{data}}) | ||
| 136 | { | ||
| 137 | # Rather paranoid, that. | ||
| 138 | next unless (defined $_->[$col] and defined $_->[$wc]); | ||
| 139 | $dhist{$_->[$col]} += $_->[$wc]; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | else | ||
| 143 | { | ||
| 144 | # Finally, the simple "normal" case. | ||
| 145 | for(@{$txd->{data}}) | ||
| 146 | { | ||
| 147 | next unless defined $_->[$col]; | ||
| 148 | ++$dhist{$_->[$col]}; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | for(sort {$a <=> $b} keys %dhist) | ||
| 153 | { | ||
| 154 | push(@points, $_); | ||
| 155 | push(@hist, $dhist{$_}); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | # For clarity and perhaps efficiency: Duplicate this, avoiding inner branching. | ||
| 161 | if($param->{weightcol}) | ||
| 162 | { | ||
| 163 | my $wc = $param->{weightcol}-1; | ||
| 164 | for(@{$txd->{data}}) | ||
| 165 | { | ||
| 166 | # Rather paranoid, that. | ||
| 167 | next unless (defined $_->[$col] and defined $_->[$wc]); | ||
| 168 | # Compute zero-based bin index. | ||
| 169 | my $bin = int(($_->[$col]-$binpoint)/$binwidth); | ||
| 170 | # Add weight to that bin. | ||
| 171 | $hist[$bin] += $_->[$wc]; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | else | ||
| 175 | { | ||
| 176 | # Finally, the simple "normal" case. | ||
| 177 | for(@{$txd->{data}}) | ||
| 178 | { | ||
| 179 | next unless defined $_->[$col]; | ||
| 180 | my $bin = int(($_->[$col]-$binpoint)/$binwidth); | ||
| 181 | ++$hist[$bin]; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | for(my $i=0; $i<@hist; ++$i) | ||
| 186 | { | ||
| 187 | $points[$i] = $binpoint+(0.5+$i)*$binwidth; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | my @below; | ||
| 192 | my @above; | ||
| 193 | my $asum = 0; | ||
| 194 | my $bsum = 0; | ||
| 195 | for(my $i=0; $i<@hist; ++$i) | ||
| 196 | { | ||
| 197 | $bsum += $hist[$i]; | ||
| 198 | $asum += $hist[$#hist-$i]; | ||
| 199 | $below[$i] = $bsum; | ||
| 200 | $above[$#hist-$i] = $asum; | ||
| 201 | } | ||
| 202 | |||
| 203 | # Got the bin values, construct data lines out of them. | ||
| 204 | for(my $i=0; $i<@hist; ++$i) | ||
| 205 | { | ||
| 206 | my $mass = $hist[$i]; | ||
| 207 | # Ensure that zero is zero and not just some empty string. | ||
| 208 | $mass = 0 unless defined $mass; | ||
| 209 | my $point = $points[$i]; | ||
| 210 | my @line = ($point, $mass); | ||
| 211 | push(@line, $below[$i], $above[$i]) | ||
| 212 | if $param->{cumulative}; | ||
| 213 | print {$self->{out}} ${$txd->data_line(\@line)}; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | 1; |