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