| 1 | package PDL::Graphics::Limits; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | require Exporter; | ||
| 7 | |||
| 8 | our @ISA = qw(Exporter); | ||
| 9 | |||
| 10 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 11 | limits | ||
| 12 | ) ] ); | ||
| 13 | |||
| 14 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 15 | |||
| 16 | our @EXPORT = qw( | ||
| 17 | limits | ||
| 18 | ); | ||
| 19 | |||
| 20 | our $VERSION = '0.01'; | ||
| 21 | $VERSION = eval $VERSION; | ||
| 22 | |||
| 23 | |||
| 24 | # Preloaded methods go here. | ||
| 25 | |||
| 26 | use PDL::Core qw( cat pdl ); | ||
| 27 | use PDL::Primitive qw( append ); | ||
| 28 | use PDL::Fit::Polynomial; | ||
| 29 | use PDL::Options; | ||
| 30 | use PDL::Bad; | ||
| 31 | use Carp; | ||
| 32 | use POSIX qw( log10 ); | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | |||
| 37 | ################################################################################ | ||
| 38 | # figure out what's good in a piddle after a possible transformation which could | ||
| 39 | # generate Infs or NaN's. If only everyone used PDL::Bad::UseNaN... | ||
| 40 | sub set_mask | ||
| 41 | { | ||
| 42 | my ( $mask, $data ) = @_; | ||
| 43 | |||
| 44 | if ( $PDL::Bad::Status ) | ||
| 45 | { | ||
| 46 | my $badflag = $data->badflag(); | ||
| 47 | $data->badflag(1); | ||
| 48 | |||
| 49 | $mask .= $PDL::Bad::UseNaN ? (! $data->isbad ) : ( $data->isfinite & ! $data->isbad ); | ||
| 50 | |||
| 51 | $data->badflag($badflag); | ||
| 52 | } else | ||
| 53 | { | ||
| 54 | $mask .= $data->isfinite; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | { | ||
| 61 | package PDL::Graphics::Limits::DSet; | ||
| 62 | |||
| 63 | use PDL::Core qw( cat pdl ); | ||
| 64 | |||
| 65 | *set_mask = \*PDL::Graphics::Limits::set_mask; | ||
| 66 | |||
| 67 | sub new | ||
| 68 | { | ||
| 69 | my $class = shift; | ||
| 70 | my $self = bless {}, $class; | ||
| 71 | |||
| 72 | my ( $min, $max ) = splice( @_, 0, 2 ); | ||
| 73 | |||
| 74 | $self->{Vectors} = [ @_ ]; | ||
| 75 | $self->{MinMax} = [ map{ [ $min, $max ] } 1..@{$self->{Vectors}} ]; | ||
| 76 | |||
| 77 | $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub ndim { scalar @{$_[0]->{Vectors}} } | ||
| 81 | |||
| 82 | sub validate | ||
| 83 | { | ||
| 84 | my ( $self, $attr) = @_; | ||
| 85 | |||
| 86 | my $ivec = 0; | ||
| 87 | my $n; | ||
| 88 | foreach my $vec ( @{$self->{Vectors}} ) | ||
| 89 | { | ||
| 90 | die( 'vector ', $ivec+1, ": no data?\n" ) | ||
| 91 | unless defined $vec->{data}; | ||
| 92 | |||
| 93 | $n = $vec->{data}->nelem unless defined $n; | ||
| 94 | |||
| 95 | # if a data set vector has no transformation function, use the | ||
| 96 | # default in $attr{Trans} | ||
| 97 | $vec->{trans} = $attr->{Trans}[$ivec] | ||
| 98 | if ! exists $vec->{trans} && exists $attr->{Trans}[$ivec]; | ||
| 99 | |||
| 100 | # remove explicitly undefined trans | ||
| 101 | delete $vec->{trans} | ||
| 102 | if exists $vec->{trans} && ! defined $vec->{trans}; | ||
| 103 | |||
| 104 | # ensure that data and errors have the same length. | ||
| 105 | die( 'vector ', $ivec+1, ": attribute $_: ", | ||
| 106 | "inconsistent number of elements", | ||
| 107 | "expected $n, got ", $vec->{$_}->nelem, "\n" ) | ||
| 108 | foreach | ||
| 109 | grep { exists $vec->{$_} && | ||
| 110 | defined $vec->{$_} && | ||
| 111 | $vec->{$_}->nelem != $n } | ||
| 112 | qw( data en ep ); | ||
| 113 | } | ||
| 114 | continue | ||
| 115 | { | ||
| 116 | $ivec++; | ||
| 117 | } | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | sub vector | ||
| 122 | { | ||
| 123 | $_[0]->{Vectors}[$_[1]]; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub set_minmax | ||
| 127 | { | ||
| 128 | my ( $dset, $min, $max, $axis ) = @_; | ||
| 129 | |||
| 130 | my $mm = $dset->{MinMax}[$axis]; | ||
| 131 | |||
| 132 | $mm->[0] = $min if defined $min; | ||
| 133 | $mm->[1] = $max if defined $max; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub upd_minmax | ||
| 137 | { | ||
| 138 | my ( $dset, $min, $max, $axis ) = @_; | ||
| 139 | |||
| 140 | my $mm = $dset->{MinMax}[$axis]; | ||
| 141 | |||
| 142 | $mm->[0] = $min if $mm->[0] > $min; | ||
| 143 | $mm->[1] = $max if $mm->[1] < $max; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub get_minmax | ||
| 147 | { | ||
| 148 | my ( $dset ) = @_; | ||
| 149 | cat( map { pdl( $dset->{MinMax}[$_] ) } 0..$dset->ndim-1 ); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub calc_minmax | ||
| 153 | { | ||
| 154 | my $dset = shift; | ||
| 155 | |||
| 156 | my @axes = @_ ? ( $_[0] ) : ( 0 ..$dset->ndims-1 ); | ||
| 157 | |||
| 158 | $dset->calc_minmax_axis( $_ ) foreach @axes; | ||
| 159 | } | ||
| 160 | |||
| 161 | ##################################################################### | ||
| 162 | # determine the limits for a dataset. | ||
| 163 | sub calc_minmax_axis | ||
| 164 | { | ||
| 165 | my ( $dset, $axis ) = @_; | ||
| 166 | |||
| 167 | my $vec = $dset->{Vectors}[$axis]; | ||
| 168 | my $data = $vec->{data}; | ||
| 169 | |||
| 170 | my $xfrm = defined $vec->{trans}; | ||
| 171 | |||
| 172 | # we need the transformed data point min max in case | ||
| 173 | # a transformed data + error is out of range of the transform | ||
| 174 | # function (e.g. log(0)). | ||
| 175 | |||
| 176 | my @minmax; | ||
| 177 | |||
| 178 | # reuse these as much as possible to reduce memory hit | ||
| 179 | my $tmp; | ||
| 180 | my $mask = PDL::null; | ||
| 181 | |||
| 182 | # i know of no way of determining whether a function can be applied inplace. | ||
| 183 | # assume not. | ||
| 184 | |||
| 185 | # if xfrm is true, $tmp will be an independent piddle, else its an alias for data | ||
| 186 | # no need to create a new piddle unless necessary. | ||
| 187 | $tmp = $xfrm ? $vec->{trans}->($data) : $data; | ||
| 188 | set_mask( $mask, $tmp ); | ||
| 189 | push @minmax, $tmp->where($mask)->minmax; | ||
| 190 | |||
| 191 | if ( defined $vec->{errn} ) | ||
| 192 | { | ||
| 193 | # worry about not overwriting the original data! | ||
| 194 | if ( $xfrm ) { $tmp .= $vec->{trans}->($data - $vec->{errn}) } | ||
| 195 | else { $tmp = $data - $vec->{errn} } | ||
| 196 | set_mask( $mask, $tmp ); | ||
| 197 | push @minmax, $tmp->where($mask)->minmax; | ||
| 198 | } | ||
| 199 | |||
| 200 | if ( defined $vec->{errp} ) | ||
| 201 | { | ||
| 202 | # worry about not overwriting the original data! | ||
| 203 | if ( $xfrm ) { $tmp .= $vec->{trans}->($data + $vec->{errp}) } | ||
| 204 | else { $tmp = $data + $vec->{errp} } | ||
| 205 | set_mask( $mask, $tmp ); | ||
| 206 | push @minmax, $tmp->where($mask)->minmax; | ||
| 207 | } | ||
| 208 | |||
| 209 | my ( $min, $max ) = PDL::Core::pdl( @minmax )->minmax; | ||
| 210 | |||
| 211 | $dset->set_minmax( $min, $max, $axis ); | ||
| 212 | } | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | ##################################################################### | ||
| 217 | |||
| 218 | # based upon PGPLOT's pgrnge routine. | ||
| 219 | sub range_frac | ||
| 220 | { | ||
| 221 | my ( $axis, $frac, $zerofix ) = @_; | ||
| 222 | |||
| 223 | my $expand = $frac * ( $axis->[1] - $axis->[0] ); | ||
| 224 | my $min = $axis->[0] - $expand; | ||
| 225 | my $max = $axis->[1] + $expand; | ||
| 226 | |||
| 227 | if ( $zerofix ) | ||
| 228 | { | ||
| 229 | $min = 0.0 | ||
| 230 | if $min < 0 && $axis->[0] >= 0.0; | ||
| 231 | |||
| 232 | $max = 0.0 | ||
| 233 | if $max > 0 && $axis->[1] <= 0.0; | ||
| 234 | } | ||
| 235 | |||
| 236 | @{$axis} = ( $min, $max ); | ||
| 237 | } | ||
| 238 | |||
| 239 | ##################################################################### | ||
| 240 | |||
| 241 | # based upon PGPLOT's pgrnd routine | ||
| 242 | |||
| 243 | # routine to find the closest "round" number to X, a "round" number | ||
| 244 | # being 1, 2 or 5 times a power of 10. | ||
| 245 | |||
| 246 | # If X is negative, round_pow(X) = -round_pow(abs(X)). | ||
| 247 | # If X is zero, the value returned is zero. | ||
| 248 | |||
| 249 | # round_pow( direction, $x ) | ||
| 250 | # where direction is up, down, or both i.e. | ||
| 251 | # $ub = round ( up => $x ); | ||
| 252 | # $lb = round ( down => $x ); | ||
| 253 | |||
| 254 | our @nice = ( 1, 2, 5, 10 ); | ||
| 255 | our %flip = ( 'up' => 'down', 'down' => 'up' ); | ||
| 256 | sub round_pow | ||
| 257 | { | ||
| 258 | my ( $what, $x ) = @_; | ||
| 259 | |||
| 260 | croak( "incorrect number of arguments" ) | ||
| 261 | unless 2 == @_; | ||
| 262 | |||
| 263 | if ( $x != 0.0 ) | ||
| 264 | { | ||
| 265 | my $xx = abs($x); | ||
| 266 | my $xlog = log10($xx); | ||
| 267 | my $ilog = int($xlog); | ||
| 268 | |||
| 269 | $what = $flip{$what} if $x < 0 ; | ||
| 270 | |||
| 271 | $ilog-- | ||
| 272 | if ( $xlog <= 0 && ( 'down' eq $what || $xlog != $ilog ) ) | ||
| 273 | || | ||
| 274 | ( $xlog > 0 && 'down' eq $what && $xlog == $ilog ) ; | ||
| 275 | |||
| 276 | my $pwr = 10 ** $ilog; | ||
| 277 | my $frac = $xx / $pwr; | ||
| 278 | |||
| 279 | my $i; | ||
| 280 | if ( 'up' eq $what ) | ||
| 281 | { | ||
| 282 | $i = 3; | ||
| 283 | $i = 2 if $frac < $nice[2]; | ||
| 284 | $i = 1 if $frac < $nice[1]; | ||
| 285 | $i = 0 if $frac < $nice[0]; | ||
| 286 | my $t = ( $x < 0 ? -1 : 1 ) * $pwr * $nice[$i]; | ||
| 287 | if(abs($t - $x) < 0.0000001) {$i++} | ||
| 288 | } | ||
| 289 | |||
| 290 | elsif ( 'down' eq $what ) | ||
| 291 | { | ||
| 292 | $i = 0; | ||
| 293 | $i = 1 if $frac > $nice[1]; | ||
| 294 | $i = 2 if $frac > $nice[2]; | ||
| 295 | $i = 3 if $frac > $nice[3]; | ||
| 296 | } | ||
| 297 | |||
| 298 | $x = ( $x < 0 ? -1 : 1 ) * $pwr * $nice[$i]; | ||
| 299 | } | ||
| 300 | |||
| 301 | $x; | ||
| 302 | } | ||
| 303 | |||
| 304 | ##################################################################### | ||
| 305 | |||
| 306 | sub setup_multi | ||
| 307 | { | ||
| 308 | my ( $common, $dim, $keys ) = @_; | ||
| 309 | |||
| 310 | my @arr; | ||
| 311 | |||
| 312 | if ( 'ARRAY' eq ref $common ) | ||
| 313 | { | ||
| 314 | return $common; | ||
| 315 | } | ||
| 316 | |||
| 317 | elsif ( 'HASH' eq ref $common ) | ||
| 318 | { | ||
| 319 | @arr[ 0..($dim-1)] = map { $common->{$_->{data}} } @{$keys}; | ||
| 320 | } | ||
| 321 | |||
| 322 | else | ||
| 323 | { | ||
| 324 | my $value = $common; | ||
| 325 | @arr = ($value) x $dim; | ||
| 326 | } | ||
| 327 | |||
| 328 | \@arr; | ||
| 329 | } | ||
| 330 | |||
| 331 | ##################################################################### | ||
| 332 | # normalize_dsets | ||
| 333 | # | ||
| 334 | # transform the user's heterogeneous list of data sets into a regular | ||
| 335 | # list of data sets, each with the form | ||
| 336 | # { Vectors => \@vectors } | ||
| 337 | # where each vector is a hashref with the following keys: | ||
| 338 | # { data => $data, | ||
| 339 | # en => $err_n, | ||
| 340 | # ep => $err_p, | ||
| 341 | # trans => $trans } | ||
| 342 | |||
| 343 | sub normalize_dsets | ||
| 344 | { | ||
| 345 | my ( $attr, @udsets ) = @_; | ||
| 346 | my @dsets; | ||
| 347 | |||
| 348 | while ( @udsets ) | ||
| 349 | { | ||
| 350 | my $ds = shift @udsets; | ||
| 351 | my $ref = ref $ds; | ||
| 352 | |||
| 353 | # peek inside the array to see what's there. we can have the following | ||
| 354 | # [ scalar|piddle, scalar|piddle, ... ] -> a zero dimensional data set | ||
| 355 | # [ \@a, \@b, \@c, \%d, ... ] -> a bunch of data sets | ||
| 356 | # [ \%h, @keys ] -> a hash with its keys | ||
| 357 | |||
| 358 | # scalar or piddle, turn it into its own data set | ||
| 359 | if ( ! $ref || UNIVERSAL::isa($ds, 'PDL') ) | ||
| 360 | { | ||
| 361 | push @dsets, | ||
| 362 | PDL::Graphics::Limits::DSet->new( $attr->{Min}, $attr->{Max}, | ||
| 363 | { data => PDL::Core::topdl( $ds ) } ); | ||
| 364 | } | ||
| 365 | |||
| 366 | elsif ( 'ARRAY' eq $ref ) | ||
| 367 | { | ||
| 368 | normalize_array( \@dsets, $attr, $ds ); | ||
| 369 | } | ||
| 370 | |||
| 371 | else | ||
| 372 | { | ||
| 373 | die( "data set: ", scalar @dsets + 1, | ||
| 374 | "illegal type in data set list: not an arrayref, scalar, or piddle\n" ); | ||
| 375 | } | ||
| 376 | |||
| 377 | } | ||
| 378 | |||
| 379 | # ensure data sets have the same dimensions | ||
| 380 | my %dim; | ||
| 381 | $dim{$_->ndim}++ foreach @dsets; | ||
| 382 | |||
| 383 | # whoops. only one allowed | ||
| 384 | die( "data sets do not all have the same dimensionality\n" ) | ||
| 385 | if keys %dim > 1; | ||
| 386 | |||
| 387 | ( $attr->{dims} ) = keys %dim; | ||
| 388 | |||
| 389 | # clean up datasets. | ||
| 390 | my $idset = -1; | ||
| 391 | foreach my $dset ( @dsets ) | ||
| 392 | { | ||
| 393 | $idset++; | ||
| 394 | |||
| 395 | eval { $dset->validate( $attr ) }; | ||
| 396 | if ( $@ ) | ||
| 397 | { | ||
| 398 | chomp $@; | ||
| 399 | die( "data set $idset: $@\n" ); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | @dsets; | ||
| 404 | } | ||
| 405 | |||
| 406 | ##################################################################### | ||
| 407 | |||
| 408 | # array refs in data set lists may be just a plain ol' data set, or | ||
| 409 | # it may contain a bunch of other stuff. here we deal with a single | ||
| 410 | # array ref. we tear it apart and (re)build data sets. | ||
| 411 | sub normalize_array | ||
| 412 | { | ||
| 413 | my ( $dsets, $attr, $aref ) = @_; | ||
| 414 | |||
| 415 | # if the first element is a hash, it's either a hash based data set | ||
| 416 | # with a bunch of attributes specific to that hash: | ||
| 417 | # [ \%h, @keys ] -> a hash with its keys | ||
| 418 | # in which case the rest of the elements are scalars, or its | ||
| 419 | # all hashes. | ||
| 420 | |||
| 421 | eval | ||
| 422 | { | ||
| 423 | if ( 'HASH' eq ref $aref->[0] ) | ||
| 424 | { | ||
| 425 | |||
| 426 | # all hashes? | ||
| 427 | if ( @$aref == grep { 'HASH' eq ref $_ } @$aref ) | ||
| 428 | { | ||
| 429 | # can't do anything unless we've been told which hash keys | ||
| 430 | # we should use, as this format doesn't allow local specification | ||
| 431 | die( "must specify hash keys for hash based data set spec\n" ) | ||
| 432 | unless defined $attr->{KeySpec} && scalar @{$attr->{KeySpec}}; | ||
| 433 | |||
| 434 | foreach ( @{$aref} ) | ||
| 435 | { | ||
| 436 | push @$dsets, normalize_hash_dset($attr, $_, @{$attr->{Keys}} ); | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | # hash + scalars? | ||
| 441 | elsif ( @$aref > 1 && 1 == grep { ref $_ } @$aref ) | ||
| 442 | { | ||
| 443 | push @$dsets, normalize_hash_dset( $attr, @{$aref} ) | ||
| 444 | } | ||
| 445 | |||
| 446 | # something wrong | ||
| 447 | else | ||
| 448 | { | ||
| 449 | die( "hash based data specification has an unexpected element" ); | ||
| 450 | } | ||
| 451 | |||
| 452 | } | ||
| 453 | |||
| 454 | # must be a list of vectors as either scalars, piddles, or array | ||
| 455 | # refs (vectors with attributes) | ||
| 456 | else | ||
| 457 | { | ||
| 458 | # for array based data sets, we have to accumulate vectors as we iterate | ||
| 459 | # through the array. they are stored here | ||
| 460 | my @vecs; | ||
| 461 | |||
| 462 | for my $vec ( @$aref ) | ||
| 463 | { | ||
| 464 | my $ref = ref $vec; | ||
| 465 | |||
| 466 | eval | ||
| 467 | { | ||
| 468 | # naked scalar or piddle: data vector with no attributes | ||
| 469 | if ( ! $ref || UNIVERSAL::isa($vec, 'PDL') ) | ||
| 470 | { | ||
| 471 | push @vecs, { data => PDL::Core::topdl( $vec ) }; | ||
| 472 | } | ||
| 473 | |||
| 474 | # array: data vector with attributes | ||
| 475 | elsif ( 'ARRAY' eq $ref ) | ||
| 476 | { | ||
| 477 | push @vecs, normalize_array_vec( $vec ); | ||
| 478 | } | ||
| 479 | |||
| 480 | else | ||
| 481 | { | ||
| 482 | die( 'vector ', @vecs+1, ": unexpected data type ($ref) in list of data sets\n" ); | ||
| 483 | } | ||
| 484 | }; | ||
| 485 | |||
| 486 | if ( $@ ) | ||
| 487 | { | ||
| 488 | chomp $@; | ||
| 489 | die( 'vector ', @vecs+1, ": $@\n" ); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | push @$dsets, | ||
| 494 | PDL::Graphics::Limits::DSet->new( $attr->{Min}, $attr->{Max}, @vecs ) | ||
| 495 | if @vecs; | ||
| 496 | } | ||
| 497 | }; | ||
| 498 | |||
| 499 | if ( $@ ) | ||
| 500 | { | ||
| 501 | chomp $@; | ||
| 502 | die( 'data set ', @$dsets+1, ": $@\n" ); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | ##################################################################### | ||
| 507 | |||
| 508 | # parse an array based vector | ||
| 509 | sub normalize_array_vec | ||
| 510 | { | ||
| 511 | my ( $vec ) = @_; | ||
| 512 | |||
| 513 | # we should have | ||
| 514 | # [ $data, [ $err | $err_n, $err_p ], [ \&func ] ] | ||
| 515 | |||
| 516 | my @el = @$vec; | ||
| 517 | |||
| 518 | die( "too few or too many entries in array based data set spec\n" ) | ||
| 519 | if @el < 1 || @el > 4; | ||
| 520 | |||
| 521 | my %vec; | ||
| 522 | $vec{data} = PDL::Core::topdl( shift @el); | ||
| 523 | |||
| 524 | # if last value is CODE, it's a trans | ||
| 525 | $vec{trans} = pop @el if 'CODE' eq ref $el[-1]; | ||
| 526 | |||
| 527 | if ( exists $el[2] ) | ||
| 528 | { | ||
| 529 | # if we have 3 elements and the last isn't undef, it's an error. | ||
| 530 | # it can't be CODE as we'd have stripped it off in the last statement | ||
| 531 | die( "illegal value for trans func: $el[2]\n" ) | ||
| 532 | if defined $el[2]; | ||
| 533 | |||
| 534 | # we need to turn off trans for this element | ||
| 535 | $vec{trans} = undef; | ||
| 536 | pop @el; | ||
| 537 | } | ||
| 538 | |||
| 539 | # two values? asymmetric errors | ||
| 540 | if ( @el == 2 ) | ||
| 541 | { | ||
| 542 | $vec{errn} = PDL::Core::topdl($el[0]) if defined $el[0]; | ||
| 543 | $vec{errp} = PDL::Core::topdl($el[1]) if defined $el[1]; | ||
| 544 | } | ||
| 545 | |||
| 546 | # one value? symmetric errors | ||
| 547 | elsif ( @el == 1 ) | ||
| 548 | { | ||
| 549 | $vec{errn} = PDL::Core::topdl($el[0]) if defined $el[0]; | ||
| 550 | $vec{errp} = $vec{errn} if defined $vec{errn}; | ||
| 551 | } | ||
| 552 | |||
| 553 | \%vec; | ||
| 554 | } | ||
| 555 | |||
| 556 | ##################################################################### | ||
| 557 | |||
| 558 | # this takes a hash and a hash key spec and generates a regularized | ||
| 559 | # data set array of the form | ||
| 560 | # [ { data => $data, ep => ..., en => ..., trans => }, ... ] | ||
| 561 | sub normalize_hash_dset | ||
| 562 | { | ||
| 563 | my ( $attr, $ds, @keys ) = @_; | ||
| 564 | |||
| 565 | my $KeySpec = $attr->{KeySpec}; | ||
| 566 | |||
| 567 | my @dset; | ||
| 568 | |||
| 569 | die( "too many local VecKeys (", scalar @keys, | ||
| 570 | ") and global VecKeys (", scalar @{$KeySpec}, ")\n" ) | ||
| 571 | if @keys && @{$KeySpec} && @{$KeySpec} <= @keys; | ||
| 572 | |||
| 573 | my @spec; | ||
| 574 | |||
| 575 | # handle local keys | ||
| 576 | if ( @keys ) | ||
| 577 | { | ||
| 578 | my $nvec = 0; | ||
| 579 | for my $key ( @keys ) | ||
| 580 | { | ||
| 581 | my %spec; | ||
| 582 | |||
| 583 | |||
| 584 | # parse the specs for this vector | ||
| 585 | eval { %spec = parse_vecspec( $key ) }; | ||
| 586 | do { chomp $@; die( "vector $nvec: $@" ) } | ||
| 587 | if $@; | ||
| 588 | |||
| 589 | |||
| 590 | # now, merge it with the global KeySpecs | ||
| 591 | |||
| 592 | if ( @{$KeySpec} ) | ||
| 593 | { | ||
| 594 | my $Spec = $KeySpec->[$nvec]; | ||
| 595 | |||
| 596 | foreach ( keys %{$Spec} ) | ||
| 597 | { | ||
| 598 | # only copy from Spec if not present in spec | ||
| 599 | $spec{$_} = $Spec->{$_} if ! exists $spec{$_}; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | push @spec, \%spec; | ||
| 604 | } | ||
| 605 | continue | ||
| 606 | { | ||
| 607 | $nvec++; | ||
| 608 | } | ||
| 609 | |||
| 610 | # handle case where local VecKeys are a subst of global VecKeys | ||
| 611 | while ( @{$KeySpec} > @spec ) | ||
| 612 | { | ||
| 613 | push @spec, $KeySpec->[$nvec++]; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | # no local keys; use global KeySpec | ||
| 618 | else | ||
| 619 | { | ||
| 620 | @spec = @{$KeySpec}; | ||
| 621 | } | ||
| 622 | |||
| 623 | my $nvec = 0; | ||
| 624 | for my $spec ( @spec ) | ||
| 625 | { | ||
| 626 | $nvec++; | ||
| 627 | my %vec; | ||
| 628 | |||
| 629 | die( "vector $nvec: no data spec?\n" ) | ||
| 630 | unless exists $spec->{data}; | ||
| 631 | |||
| 632 | for my $el ( qw( data errn errp trans ) ) | ||
| 633 | { | ||
| 634 | if ( exists $spec->{$el} ) | ||
| 635 | { | ||
| 636 | |||
| 637 | # if not defined, don't bother looking for it in the data set | ||
| 638 | unless ( defined $spec->{$el} ) | ||
| 639 | { | ||
| 640 | # trans is different from the others in that we need to pass | ||
| 641 | # it as undef if $spec->{trans} is undef (as full handling of | ||
| 642 | # trans is done elsewhere. | ||
| 643 | $vec{trans} = undef if 'trans' eq $el; | ||
| 644 | } | ||
| 645 | |||
| 646 | elsif ( exists $ds->{$spec->{$el}} ) | ||
| 647 | { | ||
| 648 | $vec{$el} = $ds->{$spec->{$el}}; | ||
| 649 | } | ||
| 650 | |||
| 651 | elsif ( $attr->{KeyCroak} ) | ||
| 652 | { | ||
| 653 | die( "vector $nvec: missing key in data set hash: ", $spec->{$el}, "\n" ) | ||
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | } | ||
| 658 | |||
| 659 | # missing data; certainly a fatal error. | ||
| 660 | die( "vector $nvec: no data for key $spec->{data}\n" ) | ||
| 661 | unless defined $vec{data}; | ||
| 662 | |||
| 663 | push @dset, \%vec; | ||
| 664 | } | ||
| 665 | |||
| 666 | PDL::Graphics::Limits::DSet->new( $attr->{Min}, $attr->{Max}, @dset ); | ||
| 667 | } | ||
| 668 | |||
| 669 | ##################################################################### | ||
| 670 | # parse specifications for a hash based data set. These are the elements | ||
| 671 | # in the VecKeys attribute. See the docs for more details. | ||
| 672 | # Returns a hashref with keys data, en, ep, trans | ||
| 673 | |||
| 674 | my $colre = qr/[^&<>=]/; | ||
| 675 | |||
| 676 | # these are the different specs available. | ||
| 677 | my %keyre = ( data => qr/^($colre+)/, | ||
| 678 | errn => qr/<($colre*)/, | ||
| 679 | errp => qr/>($colre*)/, | ||
| 680 | err => qr/=($colre*)/, | ||
| 681 | trans => qr/\&($colre*)/ | ||
| 682 | ); | ||
| 683 | |||
| 684 | my %vecspeckeys = ( data => 1, | ||
| 685 | err => 1, | ||
| 686 | errn => 1, | ||
| 687 | errp => 1, | ||
| 688 | trans => 1 ); | ||
| 689 | |||
| 690 | sub parse_vecspec | ||
| 691 | { | ||
| 692 | my ( $ukeys ) = @_; | ||
| 693 | |||
| 694 | my %k; | ||
| 695 | |||
| 696 | # do we get a hash? | ||
| 697 | if ( 'HASH' eq ref $ukeys ) | ||
| 698 | { | ||
| 699 | # complain about keys we don't use | ||
| 700 | my @badkeys = grep { ! defined $vecspeckeys{$_} } keys %$ukeys; | ||
| 701 | die( "illegal keys: ", join(' ,', @badkeys), "\n" ) | ||
| 702 | if @badkeys; | ||
| 703 | |||
| 704 | # copy keys we need | ||
| 705 | do { $k{$_} = $ukeys->{$_} if exists $ukeys->{$_} } | ||
| 706 | foreach keys %vecspeckeys; | ||
| 707 | |||
| 708 | } | ||
| 709 | |||
| 710 | # parse the string. | ||
| 711 | else | ||
| 712 | { | ||
| 713 | |||
| 714 | # make a local copy, as we modify it in place. | ||
| 715 | my $keys = $ukeys; | ||
| 716 | |||
| 717 | # deal with a "default" spec | ||
| 718 | if ( ! defined $keys ) | ||
| 719 | { | ||
| 720 | $keys = ''; | ||
| 721 | } | ||
| 722 | else | ||
| 723 | { | ||
| 724 | # spaces and commas are there for human use only | ||
| 725 | $keys =~ s/[\s,]//g; | ||
| 726 | } | ||
| 727 | |||
| 728 | |||
| 729 | # extract the known specs. | ||
| 730 | my ( $what, $re ); | ||
| 731 | $keys =~ s/$re// and $k{$what} = $1 while( ($what, $re) = each %keyre); | ||
| 732 | |||
| 733 | # if there's anything left, it's bogus | ||
| 734 | die( "illegal key specification: $ukeys\n" ) | ||
| 735 | unless $keys eq ''; | ||
| 736 | |||
| 737 | } | ||
| 738 | |||
| 739 | # check for consistent error bar specs | ||
| 740 | die( "can't specify `=' with `<' or `>'\n" ) | ||
| 741 | if exists $k{err} && ( exists $k{errn} || exists $k{errp} ); | ||
| 742 | |||
| 743 | # error bars are always specified as positive and negative; turn a symmetric | ||
| 744 | # spec into that | ||
| 745 | $k{errn} = $k{errp} = $k{err} if exists $k{err}; | ||
| 746 | delete $k{err}; | ||
| 747 | |||
| 748 | # set empty values to undefined ones | ||
| 749 | do { $k{$_} = undef if $k{$_} eq '' } foreach keys %k; | ||
| 750 | |||
| 751 | %k; | ||
| 752 | } | ||
| 753 | |||
| 754 | sub parse_vecspecs | ||
| 755 | { | ||
| 756 | my $keys = shift; | ||
| 757 | my @specs; | ||
| 758 | |||
| 759 | push @specs, { parse_vecspec($_) } | ||
| 760 | foreach @$keys; | ||
| 761 | |||
| 762 | \@specs; | ||
| 763 | } | ||
| 764 | |||
| 765 | ##################################################################### | ||
| 766 | # normalize user supplied limits | ||
| 767 | |||
| 768 | sub parse_limits | ||
| 769 | { | ||
| 770 | my ( $ndim, $spec, $KeySpec ) = @_; | ||
| 771 | |||
| 772 | $spec = [] unless defined $spec; | ||
| 773 | |||
| 774 | my @limits; | ||
| 775 | |||
| 776 | # array containing limits (as arrays or scalars) | ||
| 777 | if ( 'ARRAY' eq ref $spec ) | ||
| 778 | { | ||
| 779 | # no limits; just move on | ||
| 780 | unless ( @$spec ) | ||
| 781 | { | ||
| 782 | } | ||
| 783 | |||
| 784 | # multi-dimensional data sets | ||
| 785 | elsif ( 'ARRAY' eq ref $spec->[0] ) | ||
| 786 | { | ||
| 787 | my $ilim = 0; | ||
| 788 | for my $vlim ( @$spec ) | ||
| 789 | { | ||
| 790 | $ilim++; | ||
| 791 | die( "Limit spec element $ilim: expected array ref\n" ) | ||
| 792 | if 'ARRAY' ne ref $vlim; | ||
| 793 | |||
| 794 | die( "Limit spec element $ilim: too many values\n" ) | ||
| 795 | if @$vlim > 2; | ||
| 796 | |||
| 797 | die( "Limit spec element $vlim: values must be scalars\n" ) | ||
| 798 | if grep { ref $_ } @$vlim; | ||
| 799 | |||
| 800 | my @lims = @$vlim; | ||
| 801 | $lims[0] = undef unless defined $lims[0]; | ||
| 802 | $lims[1] = undef unless defined $lims[1]; | ||
| 803 | |||
| 804 | push @limits, \@lims; | ||
| 805 | } | ||
| 806 | } | ||
| 807 | |||
| 808 | # one-dimensional data sets | ||
| 809 | elsif ( ! ref $spec->[0] ) | ||
| 810 | { | ||
| 811 | die( "unexpected non-scalar element in Limits spec\n" ) | ||
| 812 | if grep { ref $_ } @$spec; | ||
| 813 | |||
| 814 | my @lims = @$spec; | ||
| 815 | $lims[0] = undef unless defined $lims[0]; | ||
| 816 | $lims[1] = undef unless defined $lims[1]; | ||
| 817 | |||
| 818 | push @limits, \@lims; | ||
| 819 | } | ||
| 820 | |||
| 821 | push @limits, [ undef, undef ] | ||
| 822 | while ( @limits != $ndim ); | ||
| 823 | |||
| 824 | } | ||
| 825 | |||
| 826 | # hash containing vector names and limits | ||
| 827 | elsif ( 'HASH' eq ref $spec ) | ||
| 828 | { | ||
| 829 | # first ensure that VecKeys has been specified | ||
| 830 | die( "cannot use Limits without VecKeys\n" ) | ||
| 831 | unless @$KeySpec; | ||
| 832 | |||
| 833 | # make sure that we've got common keys. | ||
| 834 | my %vecs = map { ( $_->{data} => 1) } @$KeySpec; | ||
| 835 | |||
| 836 | # identify unknown vectors | ||
| 837 | my @badvecs = grep { ! defined $vecs{$_} } keys %$spec; | ||
| 838 | die( 'unknown vector(s): ', join(', ', @badvecs), "\n" ) | ||
| 839 | if @badvecs; | ||
| 840 | |||
| 841 | # work our way through the KeySpec's, filling in values from | ||
| 842 | # $spec as appropriate. | ||
| 843 | for my $kspec ( @$KeySpec ) | ||
| 844 | { | ||
| 845 | my @lims = ( undef, undef ); | ||
| 846 | if ( exists $spec->{$kspec->{data}} ) | ||
| 847 | { | ||
| 848 | my $lspec = $spec->{$kspec->{data}}; | ||
| 849 | $lims[0] = $lspec->{min} if exists $lspec->{min}; | ||
| 850 | $lims[1] = $lspec->{max} if exists $lspec->{max}; | ||
| 851 | } | ||
| 852 | push @limits, \@lims; | ||
| 853 | } | ||
| 854 | } | ||
| 855 | |||
| 856 | # say what? | ||
| 857 | else | ||
| 858 | { | ||
| 859 | die( "Limits attribute value must be a hashref or arrayref\n" ); | ||
| 860 | } | ||
| 861 | |||
| 862 | map { { calc => scalar ( grep { !defined $_ } @{$_} ), range => $_ } } @limits; | ||
| 863 | } | ||
| 864 | |||
| 865 | |||
| 866 | |||
| 867 | ##################################################################### | ||
| 868 | |||
| 869 | sub limits | ||
| 870 | { | ||
| 871 | my $attr = 'HASH' eq ref $_[-1] ? pop @_ : {}; | ||
| 872 | |||
| 873 | my @udsets = @_; | ||
| 874 | |||
| 875 | my %attr = iparse( { | ||
| 876 | Min => -1.8e308, | ||
| 877 | Max => +1.8e308, | ||
| 878 | Bounds => 'minmax', | ||
| 879 | Clean => 'RangeFrac', | ||
| 880 | RangeFrac => 0.05, | ||
| 881 | ZeroFix => 0, | ||
| 882 | VecKeys => [], | ||
| 883 | KeyCroak => 1, | ||
| 884 | Limits => [], | ||
| 885 | Trans => [], | ||
| 886 | }, $attr ); | ||
| 887 | |||
| 888 | # turn Trans and VecKeys into arrays if necessary; may be scalars for 1D | ||
| 889 | # data sets | ||
| 890 | $attr{$_} = [ $attr{$_} ] | ||
| 891 | foreach grep { defined $attr{$_} && 'ARRAY' ne ref $attr{$_} } | ||
| 892 | qw( VecKeys Trans ); | ||
| 893 | |||
| 894 | # parse vector key specs | ||
| 895 | $attr{KeySpec} = parse_vecspecs( $attr{VecKeys} ); | ||
| 896 | |||
| 897 | # normalize data sets to make life easier later. also | ||
| 898 | # counts up the number of dimensions and sets $attr{dims} | ||
| 899 | my @dsets = normalize_dsets( \%attr, @udsets ); | ||
| 900 | |||
| 901 | # set up the Limits | ||
| 902 | my @limits = parse_limits( $attr{dims}, $attr{Limits}, $attr{KeySpec} ); | ||
| 903 | |||
| 904 | if ( 'minmax' eq lc $attr{Bounds} ) | ||
| 905 | { | ||
| 906 | for my $dim ( 0..$attr{dims}-1 ) | ||
| 907 | { | ||
| 908 | # only calculate minmax values for those dims which need them. | ||
| 909 | my $limits = $limits[$dim]; | ||
| 910 | |||
| 911 | foreach ( @dsets ) | ||
| 912 | { | ||
| 913 | # calculate min & max | ||
| 914 | $_->calc_minmax( $dim ) | ||
| 915 | if $limits->{calc}; | ||
| 916 | |||
| 917 | # make sure we pay attention to user specified limits | ||
| 918 | $_->set_minmax( @{$limits->{range}}, $dim ); | ||
| 919 | } | ||
| 920 | } | ||
| 921 | } | ||
| 922 | |||
| 923 | elsif ( 'zscale' eq lc $attr{Bounds} ) | ||
| 924 | { | ||
| 925 | croak( "zscale only good for dim = 2\n" ) | ||
| 926 | unless $attr{dims} == 2; | ||
| 927 | |||
| 928 | foreach my $dset ( @dsets ) | ||
| 929 | { | ||
| 930 | $dset->calc_minmax( 0 ) | ||
| 931 | if $limits[0]{calc}; | ||
| 932 | |||
| 933 | |||
| 934 | if ( $limits[1]{calc} ) | ||
| 935 | { | ||
| 936 | my $y = $dset->vector(1)->{data}; | ||
| 937 | |||
| 938 | # this is a waste, as we don't care about the evaluated | ||
| 939 | # fit values, just the min and max values. since we | ||
| 940 | # get them all anyway, we'll use them. | ||
| 941 | |||
| 942 | my $mask = PDL::null; | ||
| 943 | set_mask( $mask, $y ); | ||
| 944 | |||
| 945 | my $fit = fitpoly1d( $y->where($mask)->qsort, 2 ); | ||
| 946 | $dset->set_minmax( $fit->minmax, 1 ); | ||
| 947 | } | ||
| 948 | |||
| 949 | $dset->set_minmax( @{$limits[$_]{range}}, $_ ) for 0,1; | ||
| 950 | } | ||
| 951 | } | ||
| 952 | else | ||
| 953 | { | ||
| 954 | die( "unknown Bounds type: $attr{Bounds}\n" ); | ||
| 955 | } | ||
| 956 | |||
| 957 | # derive union of minmax limits from data sets | ||
| 958 | my $minmax = PDL::Core::null; | ||
| 959 | $minmax = append( $minmax, $_->get_minmax ) foreach @dsets; | ||
| 960 | |||
| 961 | # get overall minmax limits | ||
| 962 | $minmax = cat(($minmax->minmaximum)[0,1])->transpose; | ||
| 963 | |||
| 964 | my @minmax = map{ [ $minmax->slice(":,$_")->list ] } 0..$attr{dims}-1; | ||
| 965 | |||
| 966 | if ( 'rangefrac' eq lc $attr{Clean} ) | ||
| 967 | { | ||
| 968 | my $RangeFrac = | ||
| 969 | setup_multi( $attr{RangeFrac}, $attr{dims}, $attr{KeySpec} ); | ||
| 970 | |||
| 971 | my $ZeroFix = | ||
| 972 | setup_multi( $attr{ZeroFix}, $attr{dims}, $attr{KeySpec} ); | ||
| 973 | |||
| 974 | range_frac( $minmax[$_], $RangeFrac->[$_], $ZeroFix->[$_] ) | ||
| 975 | for 0..$attr{dims}-1; | ||
| 976 | } | ||
| 977 | |||
| 978 | elsif ( 'roundpow' eq lc $attr{Clean} ) | ||
| 979 | { | ||
| 980 | $_ = [ round_pow( down => $_->[0] ), | ||
| 981 | round_pow( up => $_->[1] ) ] | ||
| 982 | foreach @minmax; | ||
| 983 | } | ||
| 984 | |||
| 985 | elsif ( 'none' eq lc $attr{Clean} ) | ||
| 986 | { | ||
| 987 | # do nothing | ||
| 988 | } | ||
| 989 | |||
| 990 | else | ||
| 991 | { | ||
| 992 | die( "unknown Clean type: $attr{Clean}\n" ); | ||
| 993 | } | ||
| 994 | |||
| 995 | if ( wantarray ) | ||
| 996 | { | ||
| 997 | return map { ( @{$_} ) } @minmax; | ||
| 998 | } | ||
| 999 | else | ||
| 1000 | { | ||
| 1001 | my @key; | ||
| 1002 | if ( @{$attr{KeySpec}} ) | ||
| 1003 | { | ||
| 1004 | @key = map { $_->{data} } @{$attr{KeySpec}}; | ||
| 1005 | } | ||
| 1006 | else | ||
| 1007 | { | ||
| 1008 | @key = map { 'q' . $_ } ( 1 .. $attr{dims} ); | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | return { map { ( $key[$_] => { min => $minmax[$_][0], | ||
| 1012 | max => $minmax[$_][1] } ) } | ||
| 1013 | 0.. ( @minmax - 1 ) }; | ||
| 1014 | } | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | 1; | ||
| 1018 | |||
| 1019 | |||
| 1020 | __END__ |