| 1 | package Term::Fabulous::Chart::Transform; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 1 | ▶ | use Exporter 'import'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | our @EXPORT_OK = qw(parse_transforms apply_transforms transform_names); | ||
| 12 | |||
| 13 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use List::Util qw(max min sum0 first); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use POSIX qw(floor); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use Scalar::Util qw(looks_like_number); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 0 | ▶ | use Term::Fabulous::Check qw(describe); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | |||
| 19 | sub _is_number ($value) { | ||
| 20 | return defined $value && !ref $value && looks_like_number($value) && $value == $value && $value - $value == 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _defined_ys ($ys) { | ||
| 24 | return grep { defined } @$ys; | ||
| 25 | } | ||
| 26 | |||
| 27 | # Each transform: the arguments it takes [ least, most, defaults ], a | ||
| 28 | # check of them, and a function from (xs, ys, args) to new (xs, ys). | ||
| 29 | my %TRANSFORM = ( | ||
| 30 | normalize => { | ||
| 31 | arguments => [ 0, 2, [ 0, 1 ] ], | ||
| 32 | apply => sub ( $xs, $ys, $low, $high ) { | ||
| 33 | my @values = _defined_ys($ys) or return ( $xs, $ys ); | ||
| 34 | my ( $least, $most ) = ( min(@values), max(@values) ); | ||
| 35 | my $range = $most - $least; | ||
| 36 | return ( $xs, [ map { defined ? ( $range ? $low + ( $_ - $least ) / $range * ( $high - $low ) : $low ) : undef } @$ys ] ); | ||
| 37 | }, | ||
| 38 | }, | ||
| 39 | share => { | ||
| 40 | arguments => [ 0, 0, [] ], | ||
| 41 | apply => sub ( $xs, $ys ) { | ||
| 42 | my $total = sum0( map { abs } _defined_ys($ys) ); | ||
| 43 | return ( $xs, [ map { defined && $total ? $_ / $total : defined ? 0 : undef } @$ys ] ); | ||
| 44 | }, | ||
| 45 | }, | ||
| 46 | zscore => { | ||
| 47 | arguments => [ 0, 0, [] ], | ||
| 48 | apply => sub ( $xs, $ys ) { | ||
| 49 | my @values = _defined_ys($ys) or return ( $xs, $ys ); | ||
| 50 | my $mean = sum0(@values) / @values; | ||
| 51 | my $deviation = sqrt( sum0( map { ( $_ - $mean )**2 } @values ) / @values ); | ||
| 52 | return ( $xs, [ map { defined ? ( $deviation ? ( $_ - $mean ) / $deviation : 0 ) : undef } @$ys ] ); | ||
| 53 | }, | ||
| 54 | }, | ||
| 55 | index => { | ||
| 56 | arguments => [ 0, 1, [100] ], | ||
| 57 | apply => sub ( $xs, $ys, $base ) { | ||
| 58 | my $start = first { defined && $_ != 0 } @$ys; | ||
| 59 | return ( $xs, [ map { defined && defined $start ? $_ / $start * $base : undef } @$ys ] ); | ||
| 60 | }, | ||
| 61 | }, | ||
| 62 | cumulative => { | ||
| 63 | arguments => [ 0, 0, [] ], | ||
| 64 | apply => sub ( $xs, $ys ) { | ||
| 65 | my $total = 0; | ||
| 66 | return ( $xs, [ map { defined ? ( $total += $_ ) : undef } @$ys ] ); | ||
| 67 | }, | ||
| 68 | }, | ||
| 69 | difference => { | ||
| 70 | arguments => [ 0, 0, [] ], | ||
| 71 | apply => sub ( $xs, $ys ) { | ||
| 72 | my $previous; | ||
| 73 | return ( $xs, [ map { my $change = defined $_ && defined $previous ? $_ - $previous : undef; $previous = $_ if defined; $change } @$ys ] ); | ||
| 74 | }, | ||
| 75 | }, | ||
| 76 | rate => { | ||
| 77 | arguments => [ 0, 1, [1] ], | ||
| 78 | apply => sub ( $xs, $ys, $per ) { | ||
| 79 | my ( @rates, $before ); | ||
| 80 | foreach my $index ( 0 .. $#$ys ) { | ||
| 81 | my $y = $ys->[$index]; | ||
| 82 | push @rates, defined $y && defined $before && $xs->[$index] != $before->[0] ? ( $y - $before->[1] ) / ( $xs->[$index] - $before->[0] ) * $per : undef; | ||
| 83 | $before = [ $xs->[$index], $y ] if defined $y; | ||
| 84 | } | ||
| 85 | return ( $xs, \@rates ); | ||
| 86 | }, | ||
| 87 | }, | ||
| 88 | moving_average => { | ||
| 89 | arguments => [ 1, 2, [ undef, 'trailing' ] ], | ||
| 90 | check => sub ( $window, $align ) { | ||
| 91 | return "a window of at least 1 point" unless defined $window && $window =~ /\A[1-9][0-9]*\z/; | ||
| 92 | return "an alignment of 'trailing' or 'center'" unless $align eq 'trailing' || $align eq 'center'; | ||
| 93 | return undef; | ||
| 94 | }, | ||
| 95 | apply => sub ( $xs, $ys, $window, $align ) { | ||
| 96 | my ( $before, $after ) = $align eq 'center' ? ( int( ( $window - 1 ) / 2 ), $window - 1 - int( ( $window - 1 ) / 2 ) ) : ( $window - 1, 0 ); | ||
| 97 | return ( $xs, [ map { _window_mean( $ys, $_, $before, $after ) } 0 .. $#$ys ] ); | ||
| 98 | }, | ||
| 99 | }, | ||
| 100 | exponential => { | ||
| 101 | arguments => [ 1, 1, [] ], | ||
| 102 | check => sub ($alpha) { _is_number($alpha) && $alpha > 0 && $alpha <= 1 ? undef : "a smoothing factor greater than 0 and at most 1" }, | ||
| 103 | apply => sub ( $xs, $ys, $alpha ) { | ||
| 104 | my $smooth; | ||
| 105 | return ( $xs, [ map { defined ? ( $smooth = defined $smooth ? $alpha * $_ + ( 1 - $alpha ) * $smooth : $_ ) : undef } @$ys ] ); | ||
| 106 | }, | ||
| 107 | }, | ||
| 108 | median => { | ||
| 109 | arguments => [ 1, 1, [] ], | ||
| 110 | check => sub ($window) { defined $window && $window =~ /\A[1-9][0-9]*\z/ ? undef : "a window of at least 1 point" }, | ||
| 111 | apply => sub ( $xs, $ys, $window ) { | ||
| 112 | my ( $before, $after ) = ( int( ( $window - 1 ) / 2 ), $window - 1 - int( ( $window - 1 ) / 2 ) ); | ||
| 113 | my @medians = map { | ||
| 114 | my $index = $_; | ||
| 115 | !defined $ys->[$index] ? undef : do { | ||
| 116 | my @near = sort { $a <=> $b } grep { defined } @$ys[ max( 0, $index - $before ) .. min( $#$ys, $index + $after ) ]; | ||
| 117 | @near % 2 ? $near[ $#near / 2 ] : ( $near[ @near / 2 - 1 ] + $near[ @near / 2 ] ) / 2; | ||
| 118 | } | ||
| 119 | } 0 .. $#$ys; | ||
| 120 | return ( $xs, \@medians ); | ||
| 121 | }, | ||
| 122 | }, | ||
| 123 | gaussian => { | ||
| 124 | arguments => [ 1, 1, [] ], | ||
| 125 | check => sub ($sigma) { _is_number($sigma) && $sigma > 0 ? undef : "a positive standard deviation (in points)" }, | ||
| 126 | apply => sub ( $xs, $ys, $sigma ) { | ||
| 127 | my $reach = int( 3 * $sigma + 0.5 ); | ||
| 128 | my @weights = map { exp( -( $_**2 ) / ( 2 * $sigma**2 ) ) } -$reach .. $reach; | ||
| 129 | my @smooth; | ||
| 130 | foreach my $index ( 0 .. $#$ys ) { | ||
| 131 | if ( !defined $ys->[$index] ) { push @smooth, undef; next } | ||
| 132 | my ( $total, $weight ) = ( 0, 0 ); | ||
| 133 | foreach my $offset ( -$reach .. $reach ) { | ||
| 134 | next if $index + $offset < 0; | ||
| 135 | my $y = $ys->[ $index + $offset ] // next; | ||
| 136 | $total += $y * $weights[ $offset + $reach ]; | ||
| 137 | $weight += $weights[ $offset + $reach ]; | ||
| 138 | } | ||
| 139 | push @smooth, $total / $weight; | ||
| 140 | } | ||
| 141 | return ( $xs, \@smooth ); | ||
| 142 | }, | ||
| 143 | }, | ||
| 144 | scale => { | ||
| 145 | arguments => [ 1, 1, [] ], | ||
| 146 | check => sub ($factor) { _is_number($factor) ? undef : "a number to multiply by" }, | ||
| 147 | apply => sub ( $xs, $ys, $factor ) { | ||
| 148 | ( $xs, [ map { defined ? $_ * $factor : undef } @$ys ] ) | ||
| 149 | }, | ||
| 150 | }, | ||
| 151 | offset => { | ||
| 152 | arguments => [ 1, 1, [] ], | ||
| 153 | check => sub ($amount) { _is_number($amount) ? undef : "a number to add" }, | ||
| 154 | apply => sub ( $xs, $ys, $amount ) { | ||
| 155 | ( $xs, [ map { defined ? $_ + $amount : undef } @$ys ] ) | ||
| 156 | }, | ||
| 157 | }, | ||
| 158 | clip => { | ||
| 159 | arguments => [ 2, 2, [] ], | ||
| 160 | check => sub ( $low, $high ) { | ||
| 161 | ( !defined $low || _is_number($low) ) | ||
| 162 | && ( !defined $high || _is_number($high) ) | ||
| 163 | && ( !defined $low || !defined $high || $low <= $high ) ? undef : "a lowest and a highest value (undef for none)"; | ||
| 164 | }, | ||
| 165 | apply => sub ( $xs, $ys, $low, $high ) { | ||
| 166 | return ( $xs, [ map { !defined ? undef : defined $low && $_ < $low ? $low : defined $high && $_ > $high ? $high : $_ } @$ys ] ); | ||
| 167 | }, | ||
| 168 | }, | ||
| 169 | abs => { | ||
| 170 | arguments => [ 0, 0, [] ], | ||
| 171 | apply => sub ( $xs, $ys ) { | ||
| 172 | ( $xs, [ map { defined ? abs : undef } @$ys ] ) | ||
| 173 | }, | ||
| 174 | }, | ||
| 175 | sort => { | ||
| 176 | arguments => [ 0, 0, [] ], | ||
| 177 | apply => sub ( $xs, $ys ) { | ||
| 178 | my @order = sort { $xs->[$a] <=> $xs->[$b] } 0 .. $#$xs; | ||
| 179 | return ( [ @$xs[@order] ], [ @$ys[@order] ] ); | ||
| 180 | }, | ||
| 181 | }, | ||
| 182 | resample => { | ||
| 183 | arguments => [ 1, 2, [ undef, 'mean' ] ], | ||
| 184 | check => sub ( $interval, $aggregate ) { | ||
| 185 | return "a positive interval" unless _is_number($interval) && $interval > 0; | ||
| 186 | return "an aggregate of mean, sum, min, max, first, last or count" unless $aggregate =~ /\A(?:mean|sum|min|max|first|last|count)\z/; | ||
| 187 | return undef; | ||
| 188 | }, | ||
| 189 | apply => sub ( $xs, $ys, $interval, $aggregate ) { | ||
| 190 | my ( %bucket, @starts ); | ||
| 191 | foreach my $index ( 0 .. $#$xs ) { | ||
| 192 | my $start = floor( $xs->[$index] / $interval ) * $interval; | ||
| 193 | push @starts, $start unless $bucket{$start}; | ||
| 194 | push @{ $bucket{$start} }, $ys->[$index] if defined $ys->[$index]; | ||
| 195 | $bucket{$start} //= []; | ||
| 196 | } | ||
| 197 | @starts = sort { $a <=> $b } @starts; | ||
| 198 | return ( \@starts, [ map { _aggregate( $aggregate, $bucket{$_} ) } @starts ] ); | ||
| 199 | }, | ||
| 200 | }, | ||
| 201 | downsample => { | ||
| 202 | arguments => [ 1, 1, [] ], | ||
| 203 | check => sub ($count) { defined $count && $count =~ /\A[0-9]+\z/ && $count >= 3 ? undef : "a number of points of at least 3" }, | ||
| 204 | apply => \&_largest_triangles, | ||
| 205 | }, | ||
| 206 | regression => { | ||
| 207 | arguments => [ 0, 0, [] ], | ||
| 208 | apply => sub ( $xs, $ys ) { | ||
| 209 | my @pairs = map { [ $xs->[$_], $ys->[$_] ] } grep { defined $ys->[$_] } 0 .. $#$xs; | ||
| 210 | return ( $xs, [ (undef) x @$ys ] ) if @pairs < 2; | ||
| 211 | my ( $mean_x, $mean_y ) = ( sum0( map { $_->[0] } @pairs ) / @pairs, sum0( map { $_->[1] } @pairs ) / @pairs ); | ||
| 212 | my $spread = sum0( map { ( $_->[0] - $mean_x )**2 } @pairs ); | ||
| 213 | my $slope = $spread ? sum0( map { ( $_->[0] - $mean_x ) * ( $_->[1] - $mean_y ) } @pairs ) / $spread : 0; | ||
| 214 | return ( $xs, [ map { $mean_y + $slope * ( $_ - $mean_x ) } @$xs ] ); | ||
| 215 | }, | ||
| 216 | }, | ||
| 217 | ); | ||
| 218 | |||
| 219 | sub _window_mean ( $ys, $index, $before, $after ) { | ||
| 220 | return undef unless defined $ys->[$index]; | ||
| 221 | my @near = grep { defined } @$ys[ max( 0, $index - $before ) .. min( $#$ys, $index + $after ) ]; | ||
| 222 | return sum0(@near) / @near; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub _aggregate ( $aggregate, $values ) { | ||
| 226 | return scalar @$values if $aggregate eq 'count'; | ||
| 227 | return undef unless @$values; | ||
| 228 | return sum0(@$values) / @$values if $aggregate eq 'mean'; | ||
| 229 | return sum0(@$values) if $aggregate eq 'sum'; | ||
| 230 | return min(@$values) if $aggregate eq 'min'; | ||
| 231 | return max(@$values) if $aggregate eq 'max'; | ||
| 232 | return $values->[0] if $aggregate eq 'first'; | ||
| 233 | return $values->[-1]; | ||
| 234 | } | ||
| 235 | |||
| 236 | # Largest-Triangle-Three-Buckets: keeps the points that shape the line, | ||
| 237 | # so peaks and dips survive. Points without a value are dropped. | ||
| 238 | sub _largest_triangles ( $xs, $ys, $count ) { | ||
| 239 | my @points = map { [ $xs->[$_], $ys->[$_] ] } grep { defined $ys->[$_] } 0 .. $#$xs; | ||
| 240 | return ( [ map { $_->[0] } @points ], [ map { $_->[1] } @points ] ) if @points <= $count; | ||
| 241 | my @kept = ( $points[0] ); | ||
| 242 | my $size = ( @points - 2 ) / ( $count - 2 ); | ||
| 243 | my $chosen = 0; | ||
| 244 | foreach my $bucket ( 0 .. $count - 3 ) { | ||
| 245 | my ( $from, $to ) = ( int( $bucket * $size ) + 1, int( ( $bucket + 1 ) * $size ) + 1 ); | ||
| 246 | my ( $next_from, $next_to ) = ( $to, min( int( ( $bucket + 2 ) * $size ) + 1, scalar @points ) ); | ||
| 247 | my @next = @points[ $next_from .. $next_to - 1 ]; | ||
| 248 | my ( $average_x, $average_y ) = ( sum0( map { $_->[0] } @next ) / @next, sum0( map { $_->[1] } @next ) / @next ); | ||
| 249 | my ( $best, $largest ) = ( $from, -1 ); | ||
| 250 | foreach my $index ( $from .. $to - 1 ) { | ||
| 251 | my $area = abs( ( $points[$chosen][0] - $average_x ) * ( $points[$index][1] - $points[$chosen][1] ) - ( $points[$chosen][0] - $points[$index][0] ) * ( $average_y - $points[$chosen][1] ) ); | ||
| 252 | ( $best, $largest ) = ( $index, $area ) if $area > $largest; | ||
| 253 | } | ||
| 254 | push @kept, $points[$best]; | ||
| 255 | $chosen = $best; | ||
| 256 | } | ||
| 257 | push @kept, $points[-1]; | ||
| 258 | return ( [ map { $_->[0] } @kept ], [ map { $_->[1] } @kept ] ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub transform_names () { | ||
| 262 | my @names = sort keys %TRANSFORM; | ||
| 263 | return @names; | ||
| 264 | } | ||
| 265 | |||
| 266 | # One step: a name, [ name, arguments ], or a code reference. | ||
| 267 | sub _step ( $owner, $name, $step ) { | ||
| 268 | return [ 'code', sub ( $xs, $ys ) { _checked_result( $owner, $name, $step->( [@$xs], [@$ys] ) ) } ] if ref $step eq 'CODE'; | ||
| 269 | my ( $transform, @arguments ) = ref $step eq 'ARRAY' ? @$step : ($step); | ||
| 270 | croak "$owner: every step of $name must be a transform name (" . join( ', ', transform_names() ) . "), [ name, arguments ] or a code reference, got " . describe($transform) | ||
| 271 | unless defined $transform && !ref $transform && $TRANSFORM{$transform}; | ||
| 272 | my $spec = $TRANSFORM{$transform}; | ||
| 273 | my ( $least, $most, $defaults ) = $spec->{arguments}->@*; | ||
| 274 | croak "$owner: the $name step '$transform' takes " . ( $least == $most ? $least : "$least to $most" ) . " argument" . ( $most == 1 ? '' : 's' ) . ", got " . scalar(@arguments) | ||
| 275 | if @arguments < $least || @arguments > $most; | ||
| 276 | my @values = map { $_ < @arguments ? $arguments[$_] : $defaults->[$_] } 0 .. $most - 1; | ||
| 277 | if ( $spec->{check} ) { | ||
| 278 | my $problem = $spec->{check}->(@values); | ||
| 279 | croak "$owner: the $name step '$transform' needs $problem, got " . join( ', ', map { describe($_) } @arguments ) if defined $problem; | ||
| 280 | } | ||
| 281 | foreach my $value ( grep { defined } @values ) { | ||
| 282 | croak "$owner: the arguments of the $name step '$transform' must be plain values, got " . describe($value) if ref $value; | ||
| 283 | } | ||
| 284 | return [ $transform, sub ( $xs, $ys ) { $spec->{apply}->( $xs, $ys, @values ) } ]; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub _checked_result ( $owner, $name, @result ) { | ||
| 288 | croak "$owner: a $name code reference must return two array references of the same length (xs, ys)" | ||
| 289 | unless @result == 2 && ref $result[0] eq 'ARRAY' && ref $result[1] eq 'ARRAY' && $result[0]->@* == $result[1]->@*; | ||
| 290 | return @result; | ||
| 291 | } | ||
| 292 | |||
| 293 | # A transform specification as a list of steps: a name, a code | ||
| 294 | # reference, or an array of steps. | ||
| 295 | sub parse_transforms ( $owner, $name, $spec ) { | ||
| 296 | return [] unless defined $spec; | ||
| 297 | my @steps = ref $spec eq 'ARRAY' ? @$spec : ($spec); | ||
| 298 | croak "$owner: $name is a list of steps; write [ [ '$steps[0]', ... ] ] for one step with arguments" | ||
| 299 | if @steps > 1 && defined $steps[0] && !ref $steps[0] && $TRANSFORM{ $steps[0] } && grep { defined && !ref && looks_like_number($_) } @steps[ 1 .. $#steps ]; | ||
| 300 | return [ map { _step( $owner, $name, $_ ) } @steps ]; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub apply_transforms ( $steps, $xs, $ys ) { | ||
| 304 | ( $xs, $ys ) = $_->[1]->( $xs, $ys ) foreach @$steps; | ||
| 305 | return ( $xs, $ys ); | ||
| 306 | } | ||
| 307 | |||
| 308 | 1; | ||
| 309 | |||
| 310 | __END__ | ||