blib/lib/PDL/Dims.pm

filter: Statementstmt 66.1% 399 / 603 Branchbran 41.7% 162 / 388 Conditioncond 38.8% 33 / 85 MC/DCmcdc 12.9% 7 / 54 Subroutinesub 78.9% 30 / 38 Podpod 86.2% 25 / 29 totaltotal 54.8% 656 / 1197 CC 248 SCAR 39.1
CC
248
Cov
55%
_fix_old
240.0
drot
96.4
is_sane
90.7
CRAP 50.1
↑ Summary
1 #!/usr/bin/perl
2  
3 package PDL::Dims;
4  
5 4 our $VERSION = '0.019';
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 4 ▶use parent 'Exporter','PDL';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #use base 'PDL';
12 #@PDL::Dims::ISA=qw/PDL Exporter/;
13 4 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use PDL::NiceSlice;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use PDL::Lite;
18 #use PDL::Ufunc qw(min max);
19 #use PDL::Core qw(approx sclr);
20 #no PDL::IO::Pic;
21 #use Math::Units ; #qw(convert);
22 4 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 our @EXPORT=qw(is_equidistant hpar drot diminfo dnumeric vals spacing is_sane i2pos pos2i initdim idx didx dimsize dimname sln rmdim nagg dinc dmin dmax nreduce nop ncop copy_dim active_slice nsqueeze);
24  
25  
26 4 my @keys = qw(pos rot size num spacing unit min max inc vals index dummy);
27  
28 0 ▶sub hpar { #
Subroutine hpar: not called CC=7 SCAR=40.3
Pod undocumented
29 0 my $self=shift;
30 #say $self->info;
31 0 my $class=shift; # e.g. dicom, ascconv, a dimname, etc.
32 0 my $item=shift;
33 0 my $value=shift;
34 #print "hpar: class $class item $item value $value ref ",$self->hdr->{$class},"\n";
35 0 ▶ if (defined ($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) { } 0 0 0
36 0 ▶ if (defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item) { } 0 0 0
37 0 ▶ $self->hdr->{$class}={} unless (ref $self->hdr->{$class} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$class} eq "HASH" 0 0 0
38 0 ▶ $self->hdr->{$class}->{$item}=$value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
39 #print "hpar: $class $item: ",$self->hdr->{$class}->{$item},"\n";
40 0 return $self->hdr->{$class}->{$item};
41 } else {
42 0 ▶ $self->hdr->{$class}=$value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
43 0 return $self->hdr->{$class};
44 }
45 } else {
46 0 ▶ $self->sethdr($value) if (ref $value eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 0 0 0
47 0 return $self->gethdr;
48 }
49 }
50  
51  
52 0 ▶sub is_rot {
Subroutine is_rot: not called CC=3 SCAR=24.8
Pod undocumented
53 #my $self=shift;
54 0 my $rot=shift;
55 #return if any ($rot->shape-$self->ndims);
56 0 ▶ return unless all (approx($rot->transpose,$rot->inv));
Branch: 1 branch
BranchTrueFalseTotal
unless all(approx($rot->transpose, $rot->inv)) 0 0 0
57 #say "inv=transpose",approx(abs(det($rot)),1);
58 0 ▶ return unless ( approx (abs(det ($rot)),1));
Branch: 1 branch
BranchTrueFalseTotal
unless approx(abs det($rot), 1) 0 0 0
59 #say "det=1";
60 0 1;
61 }
62  
63 36 ▶sub drot {
Subroutine drot: called CC=17 SCAR=45.7
Pod undocumented
64 36 my $self=shift;
65 36 my $d=shift; # first
66 36 my $e=shift; # second dimension
67 36 my $v=shift; # value
68 36 my $rot=$self->hdr->{rot};
69 36 ▶ unless (eval {$rot->isa('PDL')} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $rot->isa("PDL") } }) 4 32 36
70 4 $self->hdr->{rot}=zeroes($self->ndims,$self->ndims);
71 }
72 36 ▶ if (eval{all($v->shape==$self->ndims) and $v->ndims==2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { all($v->shape == $self->ndims) and $v->ndims == 2 } }) 3 33 36
Condition33%1/3
all($v->shape == $self->ndims) && $v->ndims == 2
!ll&&!rl&&r
003
MC/DC0%0/2
all($v->shape == $self->ndims) && $v->ndims == 2
all($v->shape == $self->ndims) $v->ndims == 2
Truth table33%1/3
A && B
A= all($v->shape == $self->ndims)B= $v->ndims == 2
ABresult
0X0
100
111
73 3 return ($self->hdr->{rot}=$v);
74 } # the whole matrix
75 33 ▶ unless (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 33 0 33
76 33 ▶ return $rot unless (defined $d or defined $e);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d or defined $e 33 0 33
Condition33%1/3
defined $d or defined $e
l!l&&r!l&&!r
0033
MC/DC0%0/2
defined $d or defined $e
defined $d defined $e
Truth table33%1/3
A or B
A= defined $dB= defined $e
ABresult
1X1
011
000
77 # say "drot: colum $d $e";
78 0 ▶ return $rot($d,) unless defined $e;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 0 0
79 0 ▶ return $rot(,$e) unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 0 0
80 0 return $rot($d,$e);
81 }
82 0 ▶ if (eval{$v->isa('PDL')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $v->isa('PDL') } }) { } 0 0 0
83 0 ▶ if ($v->squeeze->ndims==1 and $v->nelem==$rot->dim(0)) # vector
Branch: 3 branches
BranchTrueFalseTotal
if ($v->squeeze->ndims == 1 and $v->nelem == $rot->dim(0)) { } 0 0 0
elsif ($v->nelem == 1) { } 0 0 0
elsif (not defined $d || defined $e) { } 0 0 0
Condition0%0/3
$v->squeeze->ndims == 1 and $v->nelem == $rot->dim(0)
!ll&&!rl&&r
000
MC/DC0%0/2
$v->squeeze->ndims == 1 and $v->nelem == $rot->dim(0)
$v->squeeze->ndims == 1 $v->nelem == $rot->dim(0)
Truth table0%0/3
A and B
A= $v->squeeze->ndims == 1B= $v->nelem == $rot->dim(0)
ABresult
0X0
100
111
84 {
85 0 ▶ return $self->hdr->{rot}->($d,).=$v if($v->dim(1)<=1);
Branch: 1 branch
BranchTrueFalseTotal
if $v->dim(1) <= 1 0 0 0
86 0 ▶ return $self->hdr->{rot}->(,$e).=$v if($v->dim(0)<=1);
Branch: 1 branch
BranchTrueFalseTotal
if $v->dim(0) <= 1 0 0 0
87 } elsif ($v->nelem==1) {
88 0 return $self->hdr->{rot}->($d,$e).=$v;
89 ▶ } elsif (!defined ($d) and !defined ($e)) { # and is_rot($v)) {
Condition0%0/3
defined $d || defined $e
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $d || defined $e
defined $d defined $e
Truth table0%0/3
A || B
A= defined $dB= defined $e
ABresult
1X1
011
000
90 # say "is_rot $v";
91 0 return $self->hdr->{rot}=$v;
92 }
93 } else { # a perl scalar
94 0 return $self->hdr->{rot}->($d,$e).=$v;
95 }
96 }
97  
98 0 ▶sub _fix_old { # This fixes data stored from version <=0.002
Subroutine _fix_old: not called CC=15 SCAR=54.8
99 0 my $self=shift;
100 0 for my $d (@{dimname($self)}) {
101 0 for my $p (keys %{$self->hdr->{$d}}) {
102 0 ▶ if ((my $n=$p) =~s/^dim//) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $n = $p) =~ s/^dim//u) 0 0 0
103 #say "$p -> $n ", $self->hdr->{$d}->{$p};
104 0 $self->hdr->{$d}->{$n}=$self->hdr->{$d}->{$p};
105 #say "$p -> $n ", $self->hdr->{$d}->{$n};
106 0 delete $self->hdr->{$d}->{$p};
107 #say "$p -> $n ", $self->hdr->{$d}->{$n};
108 }
109 }
110 0 ▶ dnumeric($self,$d,1) unless ($d eq 'channel');
Branch: 1 branch
BranchTrueFalseTotal
unless $d eq "channel" 0 0 0
111 0 dimsize($self,$d,$self->dim(didx($self,$d)));
112 #say "$d size: ",dimsize($self,$d),' ',dmin($self,$d),' min; max: ',dmax($self,$d);
113 #say "spacing ",spacing($self,$d);
114 0 ▶ spacing($self,$d,1) if ($d eq qw/x y z t/);
Branch: 1 branch
BranchTrueFalseTotal
if $d eq ('???', '???', '???', "t") 0 0 0
115 0 ▶ if ($d eq 'channel') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "channel") 0 0 0
116 #say "Channel";
117 #say dimsize($self,$d);
118 #spacing($self,$d,0);
119 #dnumeric($self,$d,0);
120 }
121 0 ▶ if ($d eq 'channel' and dimsize($self,$d)==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "channel" and dimsize($self, $d) == 1) 0 0 0
Condition0%0/3
$d eq "channel" and dimsize($self, $d) == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$d eq "channel" and dimsize($self, $d) == 1
$d eq "channel" dimsize($self, $d) == 1
Truth table0%0/3
A and B
A= $d eq "channel"B= dimsize($self, $d) == 1
ABresult
0X0
100
111
122 0 vals($self,$d,['combined',]);
123 #say "channel: ",@{vals($self,$d)};
124 }
125 0 ▶ if (!dimsize($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (dimsize($self, $d)) 0 0 0
126 #say "$d has size 1!";
127 0 dmax($self,$d,dmin($self,$d));
128 0 dinc($self,$d,0);
129 }
130 0 ▶ if (spacing($self,$d) and $d ne 't') {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d) and $d ne "t") 0 0 0
Condition0%0/3
spacing($self, $d) and $d ne "t"
!ll&&!rl&&r
000
MC/DC0%0/2
spacing($self, $d) and $d ne "t"
spacing($self, $d) $d ne "t"
Truth table0%0/3
A and B
A= spacing($self, $d)B= $d ne "t"
ABresult
0X0
100
111
131 0 ▶ dinc($self,$d,(dmax($self,$d)-dmin($self,$d))/((dimsize($self,$d)-1)||1)) ;
Condition0%0/2
dimsize($self, $d) - 1 || 1
l!l
00
MC/DC0%0/1
dimsize($self, $d) - 1 || 1
dimsize($self, $d) - 1
Truth table0%0/2
A
A= dimsize($self, $d) - 1
Aresult
11
00
132 }
133 0 ▶ if ($d eq 't') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "t") 0 0 0
134 #say "t: inc ",dinc($self,$d);
135 0 dmax($self,$d,dmin($self,$d)+(dimsize($self,$d)-1)*dinc($self,$d));
136 }
137 #say "$d size: ",dimsize($self,$d),' ',dmin($self,$d),' min; inc: ',dinc($self,$d),' inc - max ',dmax($self,$d);
138 }
139 0 ▶ drot ($self,undef,undef,identity($self->ndims)) unless any drot($self); # initialize rotation matrix with unity
Branch: 1 branch
BranchTrueFalseTotal
unless any(drot($self)) 0 0 0
140 }
141 # returns a list of all dims for one parameter -- intended as an auxillary for internal use only
142  
143 7 ▶sub _dimpar {
Subroutine _dimpar: called CC=5 SCAR=16.7
144 7 my $self=shift;
145 7 my $p=shift; # par
146 #say "All of ".%{$self->hdr}.", $p";
147 7 ▶ return unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 7 7
148 7 my @s;
149 7 for my $i (@{dimname($self)}) {
150 #say "name $i $p";
151 #next unless chomp $i;
152 #say "name $i: ",$self->hdr->{$i}->{$p};
153 17 ▶ barf ("Unknown dim $i") unless (ref ($self->hdr->{$i}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$i} eq "HASH" 0 17 17
154 17 ▶ if (ref ($self->hdr->{$i}->{$p}) eq 'PDL') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->hdr->{$i}{$p} eq 'PDL') { } 0 17 17
155 0 push @s,list $self->hdr->{$i}->{$p};
156 } else {
157 17 push @s,$self->hdr->{$i}->{$p};
158 }
159 }
160 #say "Dims ".@{dimname($self)}."Par $p: @s";
161 7 return @s;
162 }
163  
164 73 ▶sub dnumeric {
Subroutine dnumeric: called CC=7 SCAR=20.2
Pod documented
165 73 my $self=shift;
166 73 my $d=shift;
167 73 my $v=shift;
168 73 ▶ return [_dimpar($self,'num')] if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 0 73 73
Condition33%1/3
not $d and wantarray
!ll&&!rl&&r
7300
MC/DC0%0/2
not $d and wantarray
not $d wantarray
Truth table33%1/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
169 73 ▶ return [_dimpar($self,'num')] unless ($d);
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 73 73
170 73 ▶ return undef unless (ref ($self->hdr->{$d}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 73 73
171 #barf "numeric? $d $v!", $self->hdr->{num} if ($d eq 'a' and $v==1);
172 73 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 44 29 73
173 44 $self->hdr->{$d}->{num}=$v;
174 }
175 73 ▶ if ($self->hdr->{$d}->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hdr->{$d}{"num"}) 49 24 73
176 #warn "numeric: $d $v" if ($v and $d eq 'a');
177 #barf "Channel is not numeric ! " if ($d eq 'channel');
178 # for (vals($self,$d)) {looks_like_number $_ || barf "$d has non-numeric values";}
179 }
180 73 return $self->hdr->{$d}->{num};
181 }
182  
183 0 ▶sub is_equidistant {
Subroutine is_equidistant: not called CC=4 SCAR=30.0
Pod undocumented
184 0 my $self=shift;
185 0 my $th=shift;
186 0 ▶ return unless (ref $self eq 'PDL');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq "PDL" 0 0 0
187 0 ▶ return 1 if $self->nelem<3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nelem < 3 0 0 0
188 0 my $d=$self(1:)-$self(:-2);
189 0 ▶ return 1 if all approx($d,$d(0),$th);
Branch: 1 branch
BranchTrueFalseTotal
if all(approx($d, $d->slice(0), $th)) 0 0 0
190 }
191 134 ▶sub spacing {
Subroutine spacing: called CC=6 SCAR=18.7
Pod documented
192 134 my $self=shift;
193 134 my $d=shift; # dimname
194 134 my $v=shift; # value
195 134 ▶ return [_dimpar($self,'spacing')] if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 0 134 134
Condition33%1/3
not $d and wantarray
!ll&&!rl&&r
13400
MC/DC0%0/2
not $d and wantarray
not $d wantarray
Truth table33%1/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
196 134 ▶ return [_dimpar($self,'spacing')] unless ($d) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 134 134
197 134 ▶ return undef unless (ref ($self->hdr->{$d}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 134 134
198 134 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 65 69 134
199 65 int $v;
200 65 $self->hdr->{$d}->{spacing}=$v;
201 }
202 134 return $self->hdr->{$d}->{spacing}; #, $r{$d};
203 }
204  
205 3 ▶sub diminfo {
Subroutine diminfo: called CC=1 SCAR=0.0
Pod documented
206 3 my $self=shift;
207 3 my $str;
208 3 $str=join (', ',map{ $_."\[".didx($self,$_)."]=>".dimsize($self,$_) } (dimname ($self))) ;
209 3 $str.="\nscaling ".pdl(dinc($self));
210 3 $str.="\nRotation matrix: ".shape(drot($self));
211 3 $str.=$self->info;
212 3 return $str;
213 }
214 7 ▶sub is_sane {
Subroutine is_sane: called CC=19 SCAR=45.1
Pod documented
215 7 my $self=shift;
216 7 ▶ return "piddle" unless eval{ $self->isa('PDL')};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("PDL") } } 0 7 7
217 7 ▶ return "name" unless dimname($self);
Branch: 1 branch
BranchTrueFalseTotal
unless dimname($self) 0 7 7
218 7 ▶ return "ndims" unless ($self->ndims==1+$#{dimname($self)});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ndims == 1 + $#{dimname($self);} 1 6 7
219 #say "global done.";
220 6 ▶ return "rot_square ".drot($self)->info if (drot($self)->ndims!=2);
Branch: 1 branch
BranchTrueFalseTotal
if drot($self)->ndims != 2 0 6 6
221 6 ▶ return "rot_dims ".diminfo(($self)) #." != ".$self->ndims
Branch: 1 branch
BranchTrueFalseTotal
if any(drot($self)->shape - $self->ndims) 0 6 6
222 if(any(drot($self)->shape-$self->ndims));
223 6 ▶ return "rot_det" unless (approx (abs(det(drot($self))),1));
Branch: 1 branch
BranchTrueFalseTotal
unless approx(abs det(drot($self)), 1) 1 5 6
224 5 ▶ return "rot_invertible" unless (eval{all(approx(drot($self)->transpose , drot($self)->inv))});
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { all(approx(drot($self)->transpose, drot($self)->inv)) } } 0 5 5
225 5 for my $n (@{dimname($self)}) {
226 18 ▶ next unless (chomp $n);
Branch: 1 branch
BranchTrueFalseTotal
unless chomp $n 18 0 18
227 # say "Checking dim $n for $self->hdr->{self}";
228 0 ▶ return "size ".$n unless ($self->dim(didx($self,$n))==dimsize($self,$n));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dim(didx($self, $n)) == dimsize($self, $n) 0 0 0
229 0 ▶ return "index " .$n unless (idx($self,$n)<dimsize($self,$n));
Branch: 1 branch
BranchTrueFalseTotal
unless idx($self, $n) < dimsize($self, $n) 0 0 0
230 0 ▶ return "index " .$n unless (idx($self,$n)>=0);
Branch: 1 branch
BranchTrueFalseTotal
unless idx($self, $n) >= 0 0 0 0
231 # say "size and index";
232 0 ▶ if (spacing($self,$n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $n)) { } 0 0 0
233 # say "inc";
234 0 ▶ return "inc ".$n unless dinc($self,$n);
Branch: 1 branch
BranchTrueFalseTotal
unless dinc($self, $n) 0 0 0
235 # say dmax($self,$n), dmin($self,$n)+dinc($self,$n)*(dimsize($self,$n)-1);
236 # say "minmax $n";
237 0 ▶ return "minmax ".$n unless (approx(dmax($self,$n)),
Branch: 1 branch
BranchTrueFalseTotal
unless approx(dmax($self, $n)), dmin($self, $n) + dinc($self, $n) * (dimsize($self, $n) - 1) 0 0 0
238 dmin($self,$n)+dinc($self,$n)*(dimsize($self,$n)-1));
239 # say "pos: $n";
240 # say (dmax($self,$n),i2pos($self,$n,pos2i($self,$n,dmax($self,$n))));
241 # say "Index: ",(pos2i($self,$n,dmax($self,$n)));
242 # say "Numeric: $n" unless (dnumeric( $self,$n));
243 0 ▶ return "pos ".$n unless (approx(dmax($self,$n),i2pos($self,$n,pos2i($self,$n,dmax($self,$n)))));
Branch: 1 branch
BranchTrueFalseTotal
unless approx(dmax($self, $n), i2pos($self, $n, pos2i($self, $n, dmax($self, $n)))) 0 0 0
244 } else {
245 # say "vals $n";
246 0 ▶ return "vals ".$n unless (dimsize($self,$n)==$#{vals($self,$n)}+1);
Branch: 1 branch
BranchTrueFalseTotal
unless dimsize($self, $n) == $#{vals($self, $n);} + 1 0 0 0
247 # say "val_pos $n";
248 # say "Numeric: $n" unless (eval {dnumeric( $self,$n)});
249 #say i2pos($self,$n,dimsize($self,$n)-1);
250 #say pos2i($self,$n,i2pos($self,$n,dimsize($self,$n)-1))," 1";
251 #say (dimsize($self,$n) , 1+
252 #pos2i($self,$n,i2pos($self,$n,dimsize($self,$n)-1)));
253 return "pos ".$n unless (dimsize($self,$n)-1 == @{
254 0 ▶ pos2i($self,$n,i2pos($self,$n,dimsize($self,$n)-1))}-[-1]);
Branch: 1 branch
BranchTrueFalseTotal
unless dimsize($self, $n) - 1 == @{pos2i($self, $n, i2pos($self, $n, dimsize($self, $n) - 1));} - [-1] 0 0 0
255 }
256 }
257 5 return 0+0;
258 }
259  
260 137 ▶sub dimsize { # sets size for a dim
Subroutine dimsize: called CC=8 SCAR=21.1
Pod documented
261 137 my $self=shift;
262 137 my $d=shift; # dimname
263 137 my $v=shift; # value
264 #say "return all ", _dimpar($self,'size') if (!$d and wantarray);
265 137 ▶ return _dimpar($self,'size') if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 0 137 137
Condition66%2/3
not $d and wantarray
!ll&&!rl&&r
13610
MC/DC0%0/2
not $d and wantarray
not $d wantarray
Truth table66%2/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
266 return #([values %{$self->hdr->{dimsize}}]
267 # ,[keys %{$self->hdr->{dimsize}}],
268 #)
269 137 ▶ [_dimpar($self,'size')]
Branch: 1 branch
BranchTrueFalseTotal
unless $d 1 136 137
270 unless ($d) ;
271 #barf ("Unknown dim $d") unless (ref ($self->hdr->{$d}) eq 'HASH');
272 136 ▶ return undef unless (ref ($self->hdr->{$d}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 136 136
273 #say "size $d $v",$self->info;
274 136 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 32 104 136
275 32 ▶ return undef unless (ref ($self->hdr->{$d}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 32 32
276 32 int $v;
277 32 $self->hdr->{$d}->{size}=$v;
278 32 ▶ idx($self,$d,$v-1) if (idx($self,$d)>=$v-1);
Branch: 1 branch
BranchTrueFalseTotal
if idx($self, $d) >= $v - 1 7 25 32
279 }
280 136 return $self->hdr->{$d}->{size}; #, $r{$d};
281 }# get/set name of a dim by number
282  
283 71 ▶sub dimname {
Subroutine dimname: called CC=7 SCAR=19.5
Pod documented
284 71 my $self=shift;
285 71 my $d=shift; # dim number
286 71 my $n=shift; # new name
287 71 ▶ barf "Not a piddle!" unless eval{$self->isa('PDL')};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("PDL") } } 0 71 71
288 71 ▶ $self->hdr->{dimnames}=[] unless defined $self->hdr->{dimnames};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->hdr->{"dimnames"} 7 64 71
289 #say keys %{$self->hdr} unless defined $d;
290 71 ▶ return @{$self->hdr->{dimnames}} if (! defined ($d) and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $d and wantarray 3 68 71
Condition100%3/3
not defined $d and wantarray
!ll&&!rl&&r
29393
MC/DC100%2/2
not defined $d and wantarray
not defined $d wantarray
Truth table100%3/3
not(A) and B
A= not defined $dB= wantarray
ABresult
0X0
100
111
291 68 ▶ return $self->hdr->{dimnames} unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 39 29 68
292 #barf "Unknown dim $d" unless (ref $self->hdr->{dimnames} eq 'HASH');
293 29 ▶ $self->hdr->{dimnames}->[$d]=$n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 25 4 29
294 29 return $self->hdr->{dimnames}->[$d];
295 }
296  
297 52 ▶sub dinc {
Subroutine dinc: called CC=6 SCAR=18.0
Pod documented
298 52 my $self=shift;
299 52 my $d=shift; # dimname
300 52 my $v=shift; # max
301 52 ▶ return _dimpar($self,'inc') if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 3 49 52
Condition66%2/3
not $d and wantarray
!ll&&!rl&&r
4903
MC/DC50%1/2
not $d and wantarray
not $d wantarray
Truth table66%2/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
302 49 ▶ return [_dimpar($self,'inc')] unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 49 49
303 49 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 28 21 49
304 #say "dinc: $d $v";
305 28 ▶ return undef unless (ref $self->hdr->{$d} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 28 28
306 28 $self->hdr->{$d}->{inc}=$v;
307 28 spacing ($self,$d,1);
308 28 dnumeric ($self,$d,1);
309 28 $self->hdr->{$d}->{vals}=undef; #
310 }
311 49 return $self->hdr->{$d}->{inc}; #, $r{$d};
312 }
313  
314 28 ▶sub dmax {
Subroutine dmax: called CC=7 SCAR=22.4
Pod documented
315 28 my $self=shift;
316 28 my $d=shift; # dimname
317 28 my $v=shift; # max
318 28 ▶ return _dimpar($self,'max') if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 0 28 28
Condition33%1/3
not $d and wantarray
!ll&&!rl&&r
2800
MC/DC0%0/2
not $d and wantarray
not $d wantarray
Truth table33%1/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
319 28 ▶ return [_dimpar($self,'max')] unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 28 28
320 #say "$d ".$self->hdr;
321 28 ▶ return undef if (defined $v and ref $self->hdr->{$d} ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and ref $self->hdr->{$d} ne "HASH" 0 28 28
Condition33%1/3
defined $v and ref $self->hdr->{$d} ne "HASH"
!ll&&!rl&&r
0280
MC/DC0%0/2
defined $v and ref $self->hdr->{$d} ne "HASH"
defined $v ref $self->hdr->{$d} ne "HASH"
Truth table33%1/3
A and B
A= defined $vB= ref $self->hdr->{$d} ne "HASH"
ABresult
0X0
100
111
322 28 ▶ $self->hdr->{$d}->{max}=$v if defined ($v);
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 28 0 28
323 28 return $self->hdr->{$d}->{max}; #, $r{$d};
324 }
325  
326 sub unit { # physical unit of axis
327 my $self=shift;
328 my $d=shift;
329 my $u=shift;
330 return _dimpar($self,'unit') if (!$d and wantarray);
331 return [_dimpar($self,'unit')] unless $d;
332 return undef if (defined $u and ref $self->hdr->{$d} ne 'HASH');
333 #barf "Unknown dim $d" unless (ref $self->hdr->{$d} eq 'HASH');
334 $self->hdr->{$d}->{unit}=$u if defined ($u);
335 return $self->hdr->{$d}->{unit}; #, $r{$d};
336 }
337  
338 42 ▶sub dmin {
Subroutine dmin: called CC=7 SCAR=20.8
Pod documented
339 42 my $self=shift;
340 42 my $d=shift; # dimname
341 42 my $v=shift; # min
342 42 ▶ return _dimpar($self,'min') if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 0 42 42
Condition33%1/3
not $d and wantarray
!ll&&!rl&&r
4200
MC/DC0%0/2
not $d and wantarray
not $d wantarray
Truth table33%1/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
343 42 ▶ return [_dimpar($self,'min')] unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 42 42
344 42 ▶ return undef if (defined $v and ref $self->hdr->{$d} ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and ref $self->hdr->{$d} ne "HASH" 0 42 42
Condition66%2/3
defined $v and ref $self->hdr->{$d} ne "HASH"
!ll&&!rl&&r
14280
MC/DC0%0/2
defined $v and ref $self->hdr->{$d} ne "HASH"
defined $v ref $self->hdr->{$d} ne "HASH"
Truth table66%2/3
A and B
A= defined $vB= ref $self->hdr->{$d} ne "HASH"
ABresult
0X0
100
111
345 #barf "Unknown dim $d" unless (ref $self->hdr->{$d} eq 'HASH');
346 42 ▶ $self->hdr->{$d}->{min}=$v if defined ($v);
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 28 14 42
347 42 return $self->hdr->{$d}->{min}; #, $r{$d};
348 }
349  
350  
351 86 ▶sub didx { # set/get index - complementary to dimname
Subroutine didx: called CC=8 SCAR=21.3
Pod documented
352 86 my $self=shift;
353 86 my $d=shift; # dimname
354 86 ▶ return _dimpar($self,'pos') if (!$d and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not $d and wantarray 0 86 86
Condition66%2/3
not $d and wantarray
!ll&&!rl&&r
8330
MC/DC0%0/2
not $d and wantarray
not $d wantarray
Truth table66%2/3
not(A) and B
A= not $dB= wantarray
ABresult
0X0
100
111
355 86 ▶ return [_dimpar($self,'pos')] unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 3 83 86
356 83 ▶ if (ref $d eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d eq 'ARRAY') { } 0 83 83
357 0 @$d;
358 } else {
359 83 my $n=shift; # new position
360 83 ▶ return if (ref $self->hdr->{$d} ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->hdr->{$d} ne "HASH" 12 71 83
361 71 ▶ barf "Unknown dim $d" unless (ref $self->hdr->{$d} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 71 71
362 71 ▶ $self->hdr->{$d}->{pos}=$n if defined $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 25 46 71
363 #say "type $self idx $d ".$self->hdr->{$d}->{pos};
364 71 return $self->hdr->{$d}->{pos};
365 }
366 }
367  
368 25 ▶sub initdim {
Subroutine initdim: called CC=28 SCAR=40.8
Pod documented
369 25 my $self=shift;
370 25 ▶ barf "initdim: not a piddle!" unless $self->isa("PDL");
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("PDL") 0 25 25
371 25 ▶ my $d=shift || return ; # name
Condition50%1/2
shift() || (return)
l!l
250
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
372 25 my %p=@_;
373 #say "Init dim $d ...";
374 25 $self->hdr; #ensure the header is intialised.
375 25 my $res=$self;
376 25 ▶ if (ref $res->hdr->{$d} eq 'HASH'){ # dimname exists and is a hash
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res->hdr->{$d} eq 'HASH') { } 0 25 25
377 0 my $n= didx($res,$d);
378 0 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 0 0 0
379 0 $p{pos}=$n; #just to make sure
380 0 barf "$d exists at pos $n! (".%{$res->hdr->{$d}}."\n";
381 } else {
382 #say (keys (%{$res->hdr->{$d}}),'-keys');
383 0 warn "$d is defined but not a dim! ",%{$res->hdr->{$d}};
384 }
385 } else {
386 25 ▶ $res->hdr->{ndims}=0 unless ($res->hdr->{ndims});
Branch: 1 branch
BranchTrueFalseTotal
unless $res->hdr->{"ndims"} 7 18 25
387 25 ▶ if ((!defined $p{pos}) or ($p{pos}>$res->hdr->{ndims})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $p{"pos"} or $p{"pos"} > $res->hdr->{"ndims"}) 24 1 25
Condition100%3/3
not defined $p{"pos"} or $p{"pos"} > $res->hdr->{"ndims"}
l!l&&r!l&&!r
1591
MC/DC100%2/2
not defined $p{"pos"} or $p{"pos"} > $res->hdr->{"ndims"}
not defined $p{"pos"} $p{"pos"} > $res->hdr->{"ndims"}
Truth table100%3/3
not(A) or B
A= not defined $p{"pos"}B= $p{"pos"} > $res->hdr->{"ndims"}
ABresult
1X1
011
000
388 24 $p{pos}=$res->hdr->{ndims};
389 }
390 #say keys $res->hdr->{$d};
391 #say "pars: ",%p;
392 25 $res->hdr->{$d}=\%p;
393 25 say "Creating dim $d at pos. $p{pos}; Ndims ".$res->hdr->{ndims},$res->info;
394 25 ▶ if ($p{pos}<$res->hdr->{ndims}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"pos"} < $res->hdr->{"ndims"}) 0 25 25
395 0 ▶ for (my $i=$res->hdr->{ndims}-1;$i>=$p{pos};$i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $p{"pos"}) 0 0 0
396 #say dimname($res,$i)," initdim at pos $p{pos} ndims: ",$res->hdr->{ndims}, " $i";
397 0 dimname($res,$i+1,dimname($res,$i-0)); # shift the position up!
398 0 didx($res,dimname($res,$i),$i+1);
399 }
400 }
401 25 ▶ if ($p{pos}<$self->hdr->{ndims}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"pos"} < $self->hdr->{"ndims"}) 0 25 25
402 0 ▶ for (my $i=$self->hdr->{ndims}-1;$i>=$p{pos};$i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $p{"pos"}) 0 0 0
403 #say dimname($self,$i)," initdim at pos $p{pos} ndims: ",$self->hdr->{ndims}, " $i";
404 0 dimname($self,$i+1,dimname($self,$i-0)); # shift the position up!
405 0 didx($self,dimname($self,$i),$i+1);
406 }
407 0 didx ($res,$d,$p{pos});
408 0 $res->hdr->{$d}=\%p;
409 0 dimname ($res,$p{pos},$d);
410 }
411 25 didx ($self,$d,$p{pos});
412 25 $self->hdr->{$d}=\%p;
413 25 dimname ($self,$p{pos},$d);
414 }
415 25 ▶ $p{size}=$self->dim($p{pos}) unless ($p{size});
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"size"} 6 19 25
416 warn "initdim, dim $d: Size ($p{size}) does not mnatch piddle dim at pos $p{pos} ",$self->dim($p{pos})
417 25 ▶ unless ($p{size}==$self->dim($p{pos}));
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"size"} == $self->dim($p{"pos"}) 9 16 25
418 25 ▶ dimsize ($self,$d,($p{size}||1));
Condition50%1/2
$p{'size'} || 1
l!l
250
MC/DC0%0/1
$p{'size'} || 1
$p{'size'}
Truth table50%1/2
A
A= $p{'size'}
Aresult
11
00
419 25 ▶ spacing($self,$d,1) unless defined spacing($self,$d);
Branch: 1 branch
BranchTrueFalseTotal
unless defined spacing($self, $d) 12 13 25
420 #say "P: ",%p;
421 #say "Dim $d: ",ref $p{vals},"; ",%p;
422 #say "Dim $d: ",@{$p{vals}} if (ref $p{vals}); #,"; ",@{$p{vals}};
423 #say "Set values $d",ref($p{vals});
424 25 ▶ if (ref ($p{vals}) eq 'ARRAY') {# or (dimsize($self,$d) == 1 and defined $p{val})) {# and !spacing($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p{"vals"} eq "ARRAY") 4 21 25
425 #say "Set values $d";
426 4 my @v=@{$p{vals}};
427 #say "Values: @v";
428 #barf "Wrong size of values list! $d " unless ($#{$p{vals}}==dimsize($self,$d)-1);
429 4 vals ($self,$d,$p{vals});
430 #warn "numeric $d" ,dnumeric($self,$d);#,($p{num}||1));
431 #dmin($self,$d,vals($self,$d,0));
432 #dmax($self,$d,vals($self,$d,dimsize($self,$d)));
433 }
434 25 ▶ unless (spacing($self,$d)){
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d)) { } 21 4 25
435 #barf "$d !";
436 } else {
437 #say "equal spaced, numeric values $d";
438 21 ▶ $p{num}=1 unless defined $p{num};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{"num"} 11 10 21
439 21 ▶ if ($p{inc} and $p{max}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($p{'inc'} and $p{'max'}) { } 10 11 21
elsif ($p{'inc'}) { } 0 11 11
elsif ($p{'max'}) { } 0 11 11
Condition66%2/3
$p{"inc"} and $p{"max"}
!ll&&!rl&&r
11010
MC/DC50%1/2
$p{"inc"} and $p{"max"}
$p{"inc"} $p{"max"}
Truth table66%2/3
A and B
A= $p{"inc"}B= $p{"max"}
ABresult
0X0
100
111
440 barf ("Increment and maximum don't fit! ($d min $p{min} max $p{max} inc $p{inc} "
441 .dimsize($self,$d))
442 10 ▶ unless (approx(pdl ($p{max}-$p{min}) , pdl((dimsize($self,$d)-1)*$p{inc} )));
Branch: 1 branch
BranchTrueFalseTotal
unless approx(pdl($p{"max"} - $p{"min"}), pdl((dimsize($self, $d) - 1) * $p{"inc"})) 0 10 10
443 } elsif ($p{inc}) {
444 0 $p{max}=$p{inc}*(dimsize($self,$d)-1)+$p{min};
445 } elsif ($p{max}) {
446 0 ▶ $p{inc}=($p{max}-$p{min})/((dimsize($self,$d)-1)||1);
Condition0%0/2
dimsize($self, $d) - 1 || 1
l!l
00
MC/DC0%0/1
dimsize($self, $d) - 1 || 1
dimsize($self, $d) - 1
Truth table0%0/2
A
A= dimsize($self, $d) - 1
Aresult
11
00
447 } else {
448 11 $p{max}=dimsize($self,$d)-1;
449 11 $p{inc}=1;
450 }
451 21 ▶ dmin ($self,$d,$p{min}||0);
Condition50%1/2
$p{'min'} || 0
l!l
021
MC/DC0%0/1
$p{'min'} || 0
$p{'min'}
Truth table50%1/2
A
A= $p{'min'}
Aresult
11
00
452 21 dinc ($self,$d,$p{inc});
453 21 dmax ($self,$d,$p{max}); #||(dimsize($self,$d)-1)*$p{inc};
454 21 spacing($self,$d,1);
455 21 dnumeric($self,$d,);
456 }
457 25 $self->hdr->{ndims}++;
458 #say "initdim: ndims ",$self->hdr->{ndims} , diminfo $self;
459 25 ▶ idx($self,$d,($p{index}||0));#dmin($self,$d)));
Condition50%1/2
$p{'index'} || 0
l!l
025
MC/DC0%0/1
$p{'index'} || 0
$p{'index'}
Truth table50%1/2
A
A= $p{'index'}
Aresult
11
00
460 25 my $res=$self;
461 25 ▶ if ($p{dummy} ) { # insert dummy dimension of size size at pos.
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"dummy"}) 0 25 25
462 #say "dummy: $p{pos} $p{size}";
463 0 $res=$res->dummy($p{pos},$p{size});
464 }
465 25 $res->sethdr($self->hdr_copy);
466 #say diminfo ($res);
467 25 return $res;
468 #say "Done. ($d)";
469 }
470  
471 13 ▶sub copy_dim {
Subroutine copy_dim: called CC=5 SCAR=23.4
Pod documented
472 13 my $old=shift;
473 13 my $new=shift;
474 13 my $dim=shift;
475 13 ▶ unless (exists $old->hdr->{$dim}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old->hdr->{$dim}) { } 13 0 13
476 0 my $err=is_sane($new);
477 0 ▶ warn "copy_dims: inconsistent dims (input), $err ! ",diminfo $old if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
478 0 for my $dim (dimname($old),){
479 0 my $d=dclone($old->hdr->{$dim});
480 0 initdim($new,$dim,%$d);
481 }
482 0 $err=is_sane($new);
483 0 ▶ warn "copy_dims: inconsistent dims (new), $err ! ",diminfo $new if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
484 } else {
485 13 my $d=dclone($old->hdr->{$dim});
486 13 $$d{pos}=shift;
487 13 initdim($new,$dim,%$d);
488 }
489 }
490  
491 3 ▶sub rmdim {
Subroutine rmdim: called CC=4 SCAR=14.0
Pod documented
492 3 my $self=shift;
493 3 my $d=shift;
494 3 ▶ return unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 3 3
495 #say "removing $d ".didx($self,$d);;
496 3 my $idx=didx($self,$d);
497 #say @{$self->hdr->{dimnames}},didx($self,$d); # cut out the array
498 3 splice @{$self->hdr->{dimnames}},$idx,1; # cut out the array
499 #say @{$self->hdr->{dimnames}},didx($self,$d); # cut out the array
500 3 for my $i ($idx..$self->hdr->{ndims}-1) {
501 3 didx($self,dimname($self,$i),$i); #update position of following dims
502 }
503 3 delete $self->hdr->{$d};
504 3 ▶ barf "This should be undefined! ",$self->hdr->{$d} if defined ($self->hdr->{$d});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->hdr->{$d} 0 3 3
505 #barf "This should be undefined! ",$self->hdr->{dimnamesd} if defined ($self->hdr->{$d});
506 3 $self->hdr->{ndims}--;
507 #say "rmdim: ",diminfo $self;
508 }
509  
510 0 ▶sub unit {
Subroutine unit: not called CC=7 SCAR=40.3
Pod documented
511 #my $self=shift;
512 0 my $self=shift;
513 0 ▶ die "I don't have data to work on (unit)" unless defined $self->hdr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->hdr 0 0 0
514 #say "$self type array ";
515 0 my $index=shift;
516 0 ▶ return _dimpar($self,'unit') if (! defined ($index) and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $index and wantarray 0 0 0
Condition0%0/3
not defined $index and wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $index and wantarray
not defined $index wantarray
Truth table0%0/3
not(A) and B
A= not defined $indexB= wantarray
ABresult
0X0
100
111
517 0 ▶ return [_dimpar($self,'unit')] unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
518 #barf ("Unknown dim $index")
519 0 ▶ return undef unless (ref ($self->hdr->{$index}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$index} eq "HASH" 0 0 0
520 0 ▶ if (defined (my $v=shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = shift())) 0 0 0
521 0 $self->hdr->{$index}->{unit}=$v ;
522 }
523 0 return $self->hdr->{$index}->{unit};
524 }
525  
526 78 ▶sub idx {
Subroutine idx: called CC=9 SCAR=23.9
Pod documented
527 #my $self=shift;
528 78 my $self=shift;
529 78 ▶ barf "I don't have data to work on (idx)" unless defined $self->hdr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->hdr 0 78 78
530 #say "$self type array ";
531 78 my $index=shift;
532 78 ▶ return _dimpar($self,'index') if (! defined ($index) and wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $index and wantarray 0 78 78
Condition33%1/3
not defined $index and wantarray
!ll&&!rl&&r
7800
MC/DC0%0/2
not defined $index and wantarray
not defined $index wantarray
Truth table33%1/3
not(A) and B
A= not defined $indexB= wantarray
ABresult
0X0
100
111
533 78 ▶ return [_dimpar($self,'index')] unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 78 78
534
535 78 ▶ return undef unless (ref ($self->hdr->{$index}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$index} eq "HASH" 0 78 78
536 78 ▶ if (defined (my $v=shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = shift())) 39 39 78
537 39 ▶ $v<0? 0: $v;
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 0 39 39
538 39 ▶ $v>=dimsize($self,$index)? dimsize($self,$index)-1 : $v;
Branch: 1 branch
BranchTrueFalseTotal
$v >= dimsize($self, $index) ? : 0 39 39
539 39 $self->hdr->{$index}->{index}=int $v ;
540 }
541 78 return $self->hdr->{$index}->{index};
542 }
543  
544 10 ▶sub vals { #individual values of dims -- like the position along an axis in world coords., echo times
Subroutine vals: called CC=20 SCAR=45.0
Pod documented
545 10 my $self=shift;
546 10 my $d=shift; #dim
547 10 ▶ return unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 10 10
548 10 ▶ $self->hdr->{$d}->{vals}=[] unless defined $self->hdr->{$d}->{vals};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->hdr->{$d}{"vals"} 0 10 10
549 #say "Vals: $d ",@{$self->hdr->{$d}->{vals}};
550 10 my $i=shift; #index or array ref
551 10 ▶ if (defined $i) { # set the whole array or access individual points
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 4 6 10
552 #say "vals: $d $i";
553 4 ▶ if (ref $i eq 'ARRAY' and dimsize($self,$d) == $#{$i}+1) { #ref to values array
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i eq 'ARRAY' and dimsize($self, $d) == $#{$i;} + 1) { } 4 0 4
Condition33%1/3
ref $i eq "ARRAY" and dimsize($self, $d) == $#{$i;} + 1
!ll&&!rl&&r
004
MC/DC0%0/2
ref $i eq "ARRAY" and dimsize($self, $d) == $#{$i;} + 1
ref $i eq "ARRAY" dimsize($self, $d) == $#{$i;} + 1
Truth table33%1/3
A and B
A= ref $i eq "ARRAY"B= dimsize($self, $d) == $#{$i;} + 1
ABresult
0X0
100
111
554 4 ▶ barf "Array size does not match dimsize" unless ($#$i==dimsize($self,$d)-1);
Branch: 1 branch
BranchTrueFalseTotal
unless $i->$#* == dimsize($self, $d) - 1 0 4 4
555 4 $self->hdr->{$d}->{vals}=$i ;
556 4 spacing ($self,$d,0); #->hdr->{$d}->{spacing}=0;
557 4 $self->hdr->{$d}->{inc}=undef;
558 4 ▶ for (vals($self,$d)) {looks_like_number $_ || dnumeric($self,$d,0);}
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) or dnumeric($self, $d, 0) 0 16 16
559  
560 4 ▶ if (dnumeric($self,$d)){
Branch: 1 branch
BranchTrueFalseTotal
if (dnumeric($self, $d)) { } 0 4 4
561 0 ▶ barf "not numeric @$i" if ($$i[0] eq 'a');
Branch: 1 branch
BranchTrueFalseTotal
if $i->[0] eq "a" 0 0 0
562 0 dmin($self,$d,min(pdl $i));
563 0 dmax($self,$d,max(pdl $i));
564 } else {
565 4 $self->hdr->{$d}->{max}=undef;
566 4 $self->hdr->{$d}->{min}=undef;
567 }
568 #say "$d: setting vals @$i";
569 } else { #individual values
570 0 my $v=shift; #value
571 0 ▶ if ( defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 0 0 0
572 0 $self->hdr->{$d}->{vals}->[$i]=$v ;
573 0 spacing ($self,$d,0); #->hdr->{$d}->{spacing}=0;
574 0 $self->hdr->{$d}->{inc}=undef;
575 0 ▶ for (vals($self,$d)) {looks_like_number $_ || dnumeric($self,$d,0);}
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) or dnumeric($self, $d, 0) 0 0 0
576 0 ▶ if (dnumeric($self,$d)){
Branch: 1 branch
BranchTrueFalseTotal
if (dnumeric($self, $d)) { } 0 0 0
577 #barf "not numeric $i" if ($i eq 'a');
578 0 dmin($self,$d,min(pdl $i));
579 0 dmax($self,$d,max(pdl $i));
580 } else {
581 0 $self->hdr->{$d}->{max}=undef;
582 0 $self->hdr->{$d}->{min}=undef;
583 }
584 }
585 0 ▶ if (spacing($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d)) 0 0 0
586 0 return dmin($self,$d)+$i*dinc($self,$d);
587 }
588 0 return $self->hdr->{$d}->{vals}->[$i];
589 }
590 #} else {
591 }
592 10 ▶ if (spacing($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d)) 0 10 10
593 0 ▶ return (list (dmin($self,$d)+dinc($self,$d)*sequence(dimsize($self,$d)))) if (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
594 0 ▶ return [list (dmin($self,$d)+dinc($self,$d)*sequence(dimsize($self,$d)))] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
595 }
596 10 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 6 4 10
597 6 return @{$self->hdr->{$d}->{vals}};
598 } else {
599 4 return $self->hdr->{$d}->{vals};
600 }
601 }
602  
603  
604 #transformations between piddle and world coords.
605 0 ▶sub i2pos{
Subroutine i2pos: not called CC=5 SCAR=34.0
Pod documented
606 0 my $self=shift; # dataset
607 0 ▶ my $d=shift || return; # dimname
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
608 0 my $i=shift ; #value
609 0 ▶ barf "Unknown dim $d" unless (ref $self->hdr->{$d} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 0 0
610 0 ▶ $i=idx($self,$d) unless (defined $i);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 0 0
611 0 ▶ if (spacing($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d)) { } 0 0 0
612 0 return $i*dinc($self,$d)+dmin($self,$d);
613 } else {
614 0 return vals($self,$d,$i);
615 }
616 }
617  
618 2 ▶sub pos2i{
Subroutine pos2i: called CC=9 SCAR=29.0
Pod documented
619 2 my $self=shift;
620 2 ▶ (my $d=shift) || return;
Branch: 1 branch
BranchTrueFalseTotal
my $d = shift() or return 2 0 2
621 2 my $i=shift ; # value
622 2 ▶ barf "Unknown dim $d" unless (ref $self->hdr->{$d} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->hdr->{$d} eq "HASH" 0 2 2
623 2 ▶ if (spacing($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d)) { } 0 2 2
624 0 my $res=rint (($i-dmin($self,$d))/dinc($self,$d));
625 0 ▶ $res=dimsize($self,$d)-1 if ($res>=dimsize($self,$d));
Branch: 1 branch
BranchTrueFalseTotal
if $res >= dimsize($self, $d) 0 0 0
626 0 ▶ $res=0 if ($res<0);
Branch: 1 branch
BranchTrueFalseTotal
if $res < 0 0 0 0
627 0 return $res;
628 } else {
629 #say "searching for $i ",$self->hdr->{$d}->{num},".";
630 #say "Num? ",dnumeric($self,$d,undef);
631 2 my @a=vals($self,$d);
632 #say "Num? ",dnumeric($self,$d,undef);
633 #say "searching for $i ",$self->hdr->{$d}->{num},".";
634 0 ▶ my (@res)=grep { $a[$_] == $i } (0 .. $#a)if (dnumeric($self,$d));
Branch: 1 branch
BranchTrueFalseTotal
if dnumeric($self, $d) 0 2 2
635 2 ▶ my (@res)=grep { $a[$_] eq $i } (0 .. $#a)unless (dnumeric($self,$d));
Branch: 1 branch
BranchTrueFalseTotal
unless dnumeric($self, $d) 2 0 2
636 #my (@res)=grep { chomp($a[$_]) eq chomp('c') } (0 .. $#a)unless (dnumeric($self,$d));
637  
638 #say "pos2i: returning @res";
639 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
640 0 return @res ;
641 } else {
642 2 return $res[0];
643 }
644 }
645 }
646  
647 0 ▶sub nsqueeze :lvalue {
Subroutine nsqueeze: not called CC=3 SCAR=24.8
Pod documented
648 0 my $self=shift;
649 #my @except=@_;
650 #say $ret->info;
651 #ay $ret->info;
652 # my $d=shift;
653 0 my $ret;
654 # if (defined didx($slf,$d) {
655 # barf "size of $d is >1" if (dimsize($sslf,$d)-1);
656 # $ret=sln($ret,$d=>'(0)',);
657 # }
658 0 $ret=$self->squeeze;
659 0 $ret->sethdr($self->hdr_copy);
660 0 for my $i (@{dimname($self)}) {
661 #say "keeping $i ".dimsize($self,$i) unless (dimsize($self,$i)==1);
662 #say "Removing $i ".dimsize($self,$i) if (dimsize($self,$i)==1);
663 0 ▶ rmdim($ret,$i) if (dimsize($self,$i)==1);
Branch: 1 branch
BranchTrueFalseTotal
if dimsize($self, $i) == 1 0 0 0
664 }
665 #say "names: ",@{dimname($ret)};
666 #say "nsqueeze: return ",$ret->info, @{dimname($ret)};
667 0 return $ret;
668 }
669  
670 1 ▶sub nagg { # aggregate function
Subroutine nagg: called CC=8 SCAR=21.9
Pod documented
671 #no PDL::NiceSlice;
672 1 my $self=shift;
673 1 ▶ barf "nagg: not a piddle!" unless eval{$self->isa('PDL')};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("PDL") } } 0 1 1
674 1 my $op=shift; #
675 1 my $d=shift; # dimension
676 #say "nagg: ",dimname($self);
677 1 ▶ return unless (defined didx($self,$d));
Branch: 1 branch
BranchTrueFalseTotal
unless defined didx($self, $d) 0 1 1
678 1 my $res=$self->mv(didx($self,$d),0);
679 #say $res->info;
680 1 ▶ if (eval {$res->can($op)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $res->can($op) } }) { } 1 0 1
681 1 $res=$res->$op(@_);
682 } else {
683 0 $res=&$op($res,@_);
684 }
685 1 ▶ barf "nagg: Result undefined for $op on $d." unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 1 1
686 1 ▶ if (eval {$res->nelem} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $res->nelem } }) 1 0 1
687 1 ▶ barf "not an aggregate function! $op", $self->info,$res->info if ($self->ndims==$res->ndims);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ndims == $res->ndims 0 1 1
688 #if ($res->nelem==$self->nelem-1)
689 1 ▶ if ($res->ndims==$self->ndims-1)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->ndims == $self->ndims - 1) 1 0 1
690 {
691 1 $res->sethdr($self->hdr_copy);
692 # say "nagg: $d ",dimname($self);
693 1 rmdim ($res,$d);
694 # say "nagg: $d ",dimname($res);
695 }
696 }
697 #say "nagg: ",diminfo($res);
698 1 return ($res);
699 }
700 # boundaries of dims in appropriate units (mm, s, ..)
701  
702 0 ▶sub active_slice :lvalue { #
Subroutine active_slice: not called CC=7 SCAR=40.3
Pod documented
703 0 my $self=shift;
704 0 my @except=@_;
705 0 my @idx=list (rint(pdl idx($self)));
706 #say "j ".idx($img{$type});
707 0 my @n=(dimname($self));
708 0 ▶ barf "active_slice: not consistent size $#n ",$self->ndims-1 unless ($#n==$self->ndims-1);
Branch: 1 branch
BranchTrueFalseTotal
unless $#n == $self->ndims - 1 0 0 0
709 #say "self: @n";
710 0 my $str;
711 my @rm;
712 0 for my $i (0.. $#n) {
713 0 ▶ unless (/$n[$i]/ ~~ @except){
Branch: 1 branch
BranchTrueFalseTotal
unless (qr/$n[$i]/u ~~ \@except) 0 0 0
714 #say "Selecting $i $n[$i] $idx[$i]";
715 0 push @rm,$n[$i];
716 0 $str.="$idx[$i]";
717 }
718 0 ▶ $str.=',' unless ($i==$#n);
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $#n 0 0 0
719 }
720 #say "$str ",$self->info;
721 0 my $ret=$self->slice($str); #->nsqueeze;
722 0 $ret+=0;
723 0 $ret->sethdr($self->hdr_copy);
724 #say $ret->info;
725 0 for my $i (0.. $#n) {
726 0 ▶ unless (/$n[$i]/ ~~ @except){
Branch: 1 branch
BranchTrueFalseTotal
unless (qr/$n[$i]/u ~~ \@except) 0 0 0
727 #say "$i $n[$i] ",idx($self,$n[$i]);
728 0 dimsize($ret,'channel',1);
729 #say "$i $n[$i] ",idx($self,$n[$i]);
730 0 dimsize($ret,$n[$i],1);
731 0 my $id=idx($ret,$n[$i]);
732 0 idx($ret,$n[$i],0);
733 0 vals($ret,$n[$i],0,vals($self,$n[$i],$id));
734 #dmin($ret,$n[$i],vals($self,$n[$i],$id));
735 #dmax($ret,$n[$i],vals($self,$n[$i],$id));
736 }
737 }
738 #say "active_slice: ",$self->info," return ",@{dimname($ret)},$ret->info;
739 0 $ret;
740 }
741  
742 1 ▶sub nreduce { # analogue to PDL::Reduce, perform operation on named rather than numbered dims
Subroutine nreduce: called CC=2 SCAR=6.9
Pod documented
743 1 my $self=shift;
744 1 my $op=shift; # operation - passed on to reduce
745 1 require PDL::Reduce;
746 1 my @list=@_;
747 1 my @d=map {didx($self,$_)} @list;
748 #say "reduce $op @d (@list)";
749 1 my $ret;
750 1 $ret= $self->reduce($op,@d);
751 1 $ret->sethdr($self->hdr_copy);
752 1 for my $d (@list) {
753 # say "removing $d";
754 2 rmdim ($ret,$d);
755 }
756 #say "nreduce: ",$ret->info, @{dimname($ret)};
757 1 return $ret;
758 }
759  
760 2 ▶sub nop { # wrapper to functions like sin, exp, rotate operating on one named dimension
Subroutine nop: called CC=6 SCAR=18.0
Pod documented
761 2 my $self=shift;
762 2 my $op=shift;
763 2 my $dim=shift;
764 2 my @a=@_;
765 2 my $res=pdl $self;
766 2 ▶ if (eval{$self->isa('PDL')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->isa("PDL") } }) 2 0 2
767 2 my @n=@{dimname($self)};
768 #say "nop: (self) @n";
769 #say "nop: ",diminfo $self;
770 #my $arg=shift; # suitably shaped second argument
771 #say $self;
772 #say "dim $dim, pos ",didx($self,$dim)," ",%{$self->hdr->{$dim}};
773  
774 2 ▶ $dim=dimname($self,0) unless defined $dim; # trivial
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dim 1 1 2
775 2 $res=$res->mv(didx($self,$dim),0);
776 }
777 2 ▶ if ($op eq 'rotate'){
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq 'rotate') { } 1 1 2
778 1 my $s=shift;
779 #say "schifing $res by $s";
780 1 $res=$res->rotate($s);
781 #say "schifing $res by $s";
782 } else {
783 #say "nop: ",diminfo $self,"op: $op, @a";
784 1 ▶ if (eval {$res->can($op)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $res->can($op) } }) { } 1 0 1
785 1 $res=$res->$op(@a,);
786 } else {
787 0 $res=&$op($res,@a);
788 }
789 }
790 2 ▶ if (eval{$self->isa('PDL')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->isa("PDL") } }) 2 0 2
791 2 $res=$res->mv(0,didx($self,$dim));
792 2 $res->sethdr($self->hdr_copy);
793 }
794 #say "self $self, op $res, mv ",$res->mv(0,didx($res,$dim));
795 #say "nop: return", $self->info,@{dimname($res)},$res->info;
796 2 return ($res);
797 }
798  
799 3 ▶sub ncop { # named combine operation -- +*-/ ...
Subroutine ncop: called CC=24 SCAR=38.5
Pod documented
800 3 my $self=shift;
801 3 my $other=shift;
802 3 my $op=shift;
803 3 my $res;
804 #say "ncop: start self, other, ",$self->info, $other->info;
805 3 ▶ unless (eval {$self->isa('PDL')}) { # self is a scalar
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->isa("PDL") } }) 0 3 3
806 0 $self=pdl($self);
807 0 ▶ if (eval {$self->can($op)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->can($op) } }) { } 0 0 0
808 0 $res=$self->$op($other,@_);
809 } else {
810 0 $res=&$op($self,$other,@_);
811 }
812 0 ▶ if (eval {$other->isa('PDL')}) {# both are scalars ?
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $other->isa("PDL") } }) 0 0 0
813 0 $res->sethdr($other->hdr_copy);
814 #say "ncop: other ",diminfo $other;
815 0 ▶ barf "ncop: $op changed dimensions. Use nagg or nreduce for aggregate functions"
Branch: 1 branch
BranchTrueFalseTotal
unless $other->nelem == $res->nelem 0 0 0
816 unless ($other->nelem==$res->nelem);
817 }
818 #say "ncop: res ",diminfo $res;
819 0 return $res;
820 }
821 3 ▶ unless (eval {$other->isa('PDL')}) { # other is a scalar
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $other->isa("PDL") } }) 0 3 3
822 0 ▶ if (eval {$self->can($op)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->can($op) } }) { } 0 0 0
823 0 $res=$self->$op($other,@_);
824 } else {
825 0 $res=&$op($self,$other,@_);
826 }
827 0 ▶ barf "ncop: $op changed dimensions. Use nagg or nreduce for aggregate functions"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->nelem == $res->nelem 0 0 0
828 unless ($self->nelem==$res->nelem);
829 0 $res->sethdr($self->hdr_copy);
830 0 return $res;
831 }
832 #say $self->info, $other->info;
833 3 my @d=@{dimname($self)};
834 3 my @e=@{dimname($other)};
835 #say "self @d other @e";
836 3 my $m=0;
837 3 my @nother=(); # new order in other
838 3 my @nself=(); # new order in self
839 3 my @tself=(); # thread dims
840 3 my @tother=();
841 3 my @aself;
842 my @aother;
843 3 my @uother; # use other, even though self contains the dim
844 3 my @add;
845 3 my @co=();
846 3 my $i=0;
847 3 my $j=0;
848 #say "keys self",keys %{$self->hdr};
849 #say "keys other",keys %{$other->hdr};
850 3 for my $ds (@d) { # loop over dims in $self
851 6 my $n=didx($self,$ds);
852 #push @aself,$ds;
853 6 ▶ if (defined ($m=didx($other,$ds))) { # dim $ds exists also in other
Branch: 1 branch
BranchTrueFalseTotal
if (defined($m = didx($other, $ds))) { } 3 3 6
854 3 push @nself,$n;
855 3 push @nother,$m;
856 3 ▶ if (dimsize($self,$ds)==1) {
Branch: 1 branch
BranchTrueFalseTotal
if (dimsize($self, $ds) == 1) 0 3 3
857 0 ▶ push @uother,$ds if (dimsize($other,$ds)>1);
Branch: 1 branch
BranchTrueFalseTotal
if dimsize($other, $ds) > 1 0 0 0
858 }
859 # say "$ds $m $n";
860 # say "self $ds $m $n i $i";
861 3 push @co,$i+0;
862 #say "co sn @co";
863 3 $i++;
864 } else {
865 3 push @tself,$n;
866 # say "$ds $m $n i: $i ", ($other->ndims+$i);
867 3 push @co,($other->ndims+$j);
868 #say "co st @co";
869 3 $j++;
870 }
871 }
872 3 for my $ds (@e) { # loop over dims in $other
873 #push @other,$ds;
874 12 my $n=didx($other,$ds);
875 12 ▶ if (defined ($m=didx($self,$ds))) { # if present in self, already done
Branch: 1 branch
BranchTrueFalseTotal
if (defined($m = didx($self, $ds))) { } 3 9 12
876 3 1;
877 } else {
878 9 push @tother,$n;
879 9 push @add,$ds;
880 9 push @co,$i+0;
881 #say "co ot @co";
882 #say "other $ds $m $n";
883 9 $i++;
884 }
885 }
886 #say "Co: @co";
887 # reorder piddles
888 3 ▶ push @aother,@nother if defined ($nother[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $nother[0] 3 0 3
889 3 ▶ push @aother,@tother if defined ($tother[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tother[0] 3 0 3
890 3 ▶ push @aself,@nself if defined ($nself[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $nself[0] 3 0 3
891 3 ▶ push @aself,@tself if defined ($tself[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tself[0] 3 0 3
892 3 my $ns=$self->reorder(@aself);
893 3 my $no=$other->reorder(@aother);
894 #say "keys self",keys %{$self->hdr};
895 #say "keys other",keys %{$other->hdr};
896 # insert missing dimensions
897 3 for my $n (0..$#tother) { # fill in dummy dims
898 9 $ns=$ns->dummy($#nself+1,1);
899 }
900 #say "ncop: @aother @aself ",$ns->info,$no->info;
901 #say $self->info,$other->info;
902 #### perform the operation
903 # perform the operation either as method or function
904 3 ▶ if (eval {$ns->can($op)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $ns->can($op) } }) { } 2 1 3
905 #unshift @_,0 if ($op eq "atan2" and $_[0] != 0);
906 2 $res=$ns->$op($no,@_);
907 } else{
908 #unshift @_,0 if ($op eq "atan2");# and $_[0] != 0);
909 1 $res=&$op($ns,$no,@_) ;
910 }
911 3 my $rot=zeroes($res->ndims,$res->ndims);
912 3 my $i=$self->ndims;
913 3 for my $ds (0..$i-1) {
914 6 for my $ds (0..$i-1) {
915 # $rot($i,$i)
916 }
917 }
918 3 for my $ds (@uother) {
919 0 my $i=didx($res,$ds);
920 0 rmdim $res,$ds;
921 0 copy_dim($other,$res,$ds,$i);
922 }
923 3 for my $ds (0..$#add) {
924 #say "copy $add[$ds]",keys %{$res->hdr};
925 #initdim($res,$add[$ds]);
926 9 copy_dim($other,$res,$add[$ds],$i);
927 # say @{dimname($res)};
928 9 $i++;
929 }
930 3 $res->hdr->{ndims}=$i;
931 #say "self",diminfo $res;
932 #say @{dimname($res)};
933 #say "Co @co";
934 #say "ncop: returning ... ".$res->info;
935 #my $err=is_sane($res) ;
936 #barf "ncop: sanity check failed $err" if $err;
937 #say "ncop ",diminfo($res);
938 3 return $res;
939 }
940  
941 4 ▶sub sln :lvalue { # returns a slice by dimnames and patterns
Subroutine sln: called CC=14 SCAR=31.8
Pod documented
942 4 my $self=shift;
943 4 my %s=@_; # x=>'0:54:-2', t=>47, ... #
944 4 my $str;
945 #say "sln: args @_, ".$self->hdr->{dimnames},%s;
946 #say ("dimnames @{$self->dimname}");
947 4 ▶ my @n=@{$s{names}||dimname($self)};
Condition33%1/3
$s{'names'} || dimname($self)
l!l&&r!l&&!r
040
MC/DC0%0/2
$s{'names'} || dimname($self)
$s{'names'} dimname($self)
Truth table33%1/3
A || B
A= $s{'names'}B= dimname($self)
ABresult
1X1
011
000
948 #say "dims @n";
949 4 for my $i (0.. $#n) {
950 # say "$i $n[$i] $s{$n[$i]}";
951 8 ▶ $str.=$s{$n[$i]} if defined ($s{$n[$i]});
Branch: 1 branch
BranchTrueFalseTotal
if defined $s{$n[$i]} 7 1 8
952 8 ▶ $str.=',' unless ($i==$#n);
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $#n 4 4 8
953 }
954 #say "sln: slice string $str";
955 4 my $ret=$self->slice($str);
956 4 $ret->sethdr($self->hdr_copy);
957 #say $ret->info;
958 4 for my $d (@n) {
959 8 $str=$s{$d};
960 #say "$d $str";
961 8 ▶ next unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 7 8
962 7 chomp $str;
963 7 ▶ if ($str =~/\(\s*\d+\s*\)/) { # e.g. (0) - reduce
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\(\s*\d+\s*\)/u) 0 7 7
964 0 rmdim ($ret,$d);
965 0 next;
966 }
967 7 $str=~m/([+-]?\d+)(:([+-]?\d+)(:([+-]?\d+))?)?/; # regex for a:b:c, a,b,c numbers
968 #say "$d: 1 $1 2 $2 3 $3 4 $4 5 $5";
969 7 ▶ my $step=int ($5)||1;
Condition50%1/2
int $5 || 1
l!l
07
MC/DC0%0/1
int $5 || 1
int $5
Truth table50%1/2
A
A= int $5
Aresult
11
00
970 7 ▶ my $size=int abs((($3||$1)-$1)/$step)+1; #
Condition66%2/3
$3 || $1
l!l&&r!l&&!r
034
MC/DC50%1/2
$3 || $1
$3 $1
Truth table66%2/3
A || B
A= $3B= $1
ABresult
1X1
011
000
971 7 my $min=int min pdl($1+0,$3+0);
972 7 my $max=int max pdl($1+0,$3+0);
973 #say "min $min max $max size $size str $str vals ";
974 7 dimsize($ret,$d,$size);
975 7 ▶ if (spacing ($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (spacing($self, $d)) { } 7 0 7
976 7 dinc($ret,$d,$step*dinc($self,$d));
977 7 dmin($ret,$d,dmin($self,$d)+$step*dinc($self,$d)*($min % dimsize($self,$d)));
978 7 dmax($ret,$d,dmin($ret,$d)+$step*dinc($self,$d)*(dimsize($ret,$d)-1));
979 #dmax($ret,$d,vals($self,$d,$max % dimsize($self,$d)));
980 7 idx($ret,$d,sclr (pdl(idx($self,$d))->clip(0,dimsize($ret,$d)-1)));
981 #say "sln: idx ($d):",idx($ret,$d);
982 } else {
983 #say "min $min max $max size $size str $str vals ";
984 #say "vals $d: ",vals($self,$d);
985
986 0 ▶ if (dnumeric($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (dnumeric($self, $d)) { } 0 0 0
987 0 my $v=pdl([vals($self,$d),])->($str) ;
988 0 vals($ret,$d,[vals($self,$d,[list $v])]);
989 0 idx($ret,$d,sclr (pdl(idx($self,$d))->clip(dmin($ret,$d),dmax($ret,$d))));
990 } else {
991 0 my $v=sequence(dimsize($self,$d))->($str);
992 0 my @values;
993 #say "vals: $d size $size str $str" ,$v->info;
994 0 for my $ix (0.. $v->nelem-1) {
995 #say "$ix $v ",$v($ix);
996 0 push @values,vals($self,$d,sclr ($v($ix)));
997 #say "$ix $v ",$v($ix);
998 }
999 0 vals($ret,$d,[@values]);
1000 0 idx($ret,$d,0);
1001 #say "sln, vals ($d): ", vals($ret,$d);
1002 }
1003 #vals($res,$d,list ();
1004  
1005 0 ▶ if (dnumeric($self,$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (dnumeric($self, $d)) 0 0 0
1006 #dmin($ret,$d,min($v));
1007 #dmax($ret,$d,min($v));
1008 }
1009 }
1010 #dimsize($ret, $n,
1011 }
1012 #say "sln: return ",diminfo($ret);
1013 4 $ret;
1014  
1015 }
1016  
1017 4 1;
1018 #BEGIN {
1019 # if ($_[0] eq q/-d/) {
1020 # require Carp;
1021 # $SIG{__DIE__} = sub {print Carp::longmess(@_); die;};
1022 # }
1023 #}
1024  
1025  
1026 =head1 SYNOPSIS
1027  
1028 If PDL is about arrays, PDL::Dims makes them into hashes.
1029  
1030 What it provides is a framework for addressing data not by numbered indices in
1031 numbered dimensions but by meaningful names and values.
1032  
1033 In PDL::Dims the user does not need to know the internal structure, i.e. order of
1034 dimensions. It renders calls to functions like mv, reshape, reorder, ... unnecessary.
1035  
1036 use PDL::Dims;
1037
1038 my $data= .... # some way to load data
1039 print $data->Info;
1040 # PDL: Double D [256,256,20,8,30]
1041 # Now name the first dim x, running from -12 to 12 cm
1042 initdim ($data, 'x',unit=>'cm',dmin=>-12,dmax=>12);
1043 initdim ($data,'y',pos=>1,size=>256, # these are not necessary but you can set them explicitely
1044 dmin=>-12, dinc=>0.078125, unit=>'cm' # min -12 max 8
1045 initdim ($data,'n',vals=[@list]); # 20 objects with names in @list
1046 initdim ($data,'t',spacing=>0,unit=>'s', vals=>[10,15,25,40,90,120,240,480); # sampled at different time points
1047 initdim ($data,'z',min=>30,max=>28,unit=>'mm'); yet another way
1048  
1049 # x,y,z are equally spaced numeric, n is non-numeric, t is numeric but at arbitrary distances
1050 ...
1051  
1052 # load or define $mask with dims x,y,z equal to that of data
1053 $i=ncop($data,$mask,'mult',0); # multiply mask onto data.
1054 # Since mask has only x,y,z, the dims of $data are unchanged.
1055 # x,y,z, in $mask or $data must be either 1 or equal.
1056
1057 # Calculate the average over my region of interest:
1058 $sum=nreduce($i,'avg','x','y','z');
1059
1060 ...
1061  
1062 Now you want to associate your images with other data recorded at each time point:
1063 # $data2: PDL [8, 100] (t,pars)
1064 $more_complex=ncop($data,$data2,'plus',0);
1065 #This will produce a piddle of size [256,256,20,8,30,100]
1066
1067 # if you want to average over every second object between n = 6 - 12:
1068 $avg=nagg(sln($data,n=>'6:12:2'),'average','n');
1069  
1070  
1071  
1072 =head1 DESCRIPTION
1073  
1074 This module is an extension to PDL by giving the dimensions a meaningful name,
1075 values or ranges. The module also provides wrappers to perform most PDL
1076 operations based on named dimensions. Each dimension is supposed to have a unique name.
1077  
1078 If you prefer methods over functions, say
1079  
1080 bless $piddle, "PDL::Dims";
1081  
1082 Names of dims can be any string (x,y,z,t,city, country, lattitude, fruit,pet, ...)
1083  
1084 Each dim has its own hash with several important keys;
1085  
1086 =cut #my @keys = qw(pos size num spacing min max inc vals index dummy);
1087  
1088 =over
1089  
1090 =item * pos - dimension index 0..ndims-1
1091  
1092 =item * dimnames - a string by which the dim is accessed
1093  
1094 =item * index - current position within a dim. PDL::Dims knows the concept of a current position within a piddle
1095  
1096 =item * dummy - if true, initdim creates a dim of size at pos
1097  
1098 =item * num - dimension has numeric properties; if false, no min/max/inc are calculated. E.g. a dim pet may have values for dog, cat, fish, rabbit, mouse, parrot, min/max don't make sense in this case.
1099  
1100 =item * spacing - if true, values are evenly spaced; inc is undef if false
1101  
1102 =item * vals - list all values along a dim, if not evenly spaced each value is stored. Can cause memory issues in very large dims
1103  
1104 =item * min/max - minimum and maximum of numeric dimensions
1105  
1106 =item * inc - step between two indices in equally spaced dimensions
1107  
1108 =item * unit - the unit of vals.
1109  
1110 =back
1111  
1112  
1113 =head1 SUBROUTINES/METHODS
1114  
1115  
1116  
1117 The module has two different types of functions.
1118  
1119 First, there are several utitility functions to manipulate and retrieve the dimension info.
1120 Currently, they only work on the metadata, no operations on the piddle are performed.
1121 It is your responsibility to call the appropriate function whenever piddle dims change.
1122  
1123 Then there are functions to perform most PDL operations based on named dimensions instead
1124 of numbered dims. Wrappers to slice (sln), reduce (nreduce) and other functions
1125 (nop, ncop) are implemented.
1126  
1127 The following parameters are currently used by PDL::Dims. It is *strongly* discouraged
1128 to access them directly except during calls to initdim. Use the functions listed below
1129 to manipulate and retrieve values.
1130  
1131 They are usually called like that:
1132  
1133 func ($piddle , [$dim, [value]]);
1134  
1135 if no dim is given, an array reference to all values are returned.
1136  
1137 Otherwise, it returns the given value for a particular $dim, setting it to $value if defined.
1138  
1139  
1140 =head2 diminfo
1141  
1142  
1143 $infostr = diminfo($piddle);
1144
1145 An extended version of PDLs info, showing dimnames and sizes (from header).
1146  
1147 =head2 dimsize
1148  
1149 dimsize($piddle,[$dim,[$size]]);
1150  
1151 set/get dimension size of piddle by name.
1152  
1153 =head2 dimname
1154  
1155 dimname($piddle,[$position,[$name]]);
1156  
1157 set/get dimension size of piddle by name.
1158  
1159 =head2 idx
1160
1161 idx($piddle,[$name,[$current]])
1162  
1163 set/get current index values by name
1164  
1165 =head2 didx
1166  
1167 didx($piddle,[$name,[$pos]]);
1168  
1169 get/set position of dims by their names
1170  
1171 =head2 i2pos
1172  
1173 =head2 pos2i
1174  
1175 i2pso($piddle,$dim,[$index]);
1176 pso2i($piddle,$dim,$value);
1177  
1178 Converts value from/to index. So you can say, for example, if you have a piddle counting stuff in different houses,
1179  
1180 sln($house,pets=>pos2i($house,pets,'dog'),rooms=>pos2i(housee,rooms,'kitchen'));
1181  
1182 something more realistic: Imagine you have two images of the same object but with different scaling and resolution or orientation. You want to extract what's between y = 10 and 20 cm of both images
1183  
1184 ya10=pos2i($a,'y',10);
1185 ya20=pos2i($a,'y',20);
1186 $slice=sln($a,y=>"$ya10:$ya20");
1187  
1188 or if you want to resample something, the index $yb in $b corresponds to the index $ya in $a:
1189  
1190 $ya=pos2i($a,'y',i2pos($b,'y',$yb));
1191  
1192  
1193  
1194 =head2 dmin
1195  
1196 dmin($piddle,[$dim,[$min]]);
1197  
1198 get/set minimum value for a dimension
1199  
1200 =head2 dmax
1201  
1202 dmax($piddle,[$name,[$max]]);
1203  
1204 get/set maximum value for a dimension
1205  
1206 =head2 dinc
1207  
1208 dinc($piddle,[$name,[$increment]]);
1209  
1210 get/set maximum value for a dimension
1211  
1212 =head2 vals
1213  
1214 vals($piddle,$dim,[$index ,[$val]| $array_ref])
1215  
1216 get/set values of an axis. As a third argument you can either supply an index,
1217 then you can access that element or a reference to an array of size dimsize supplying
1218 values for that dim.
1219  
1220 Please take a look on i2pos, pos2i, spacing and dnumeric to better understand the behaviour.
1221  
1222  
1223 =head2 unit
1224
1225 unit($piddle,[$dim,[$unit]]);
1226  
1227 get/set the unit parameter. In the future, each assignment or operation
1228 should be units-avare, e.g. converting autmatically from mm to km or feet,
1229 preventing you from adding apples to peas ...
1230  
1231 =head2 spacing
1232  
1233 spacing ($piddle,[$dim,[$spacing]]);
1234  
1235 if true, the spacing between indices is assumed to be equal.
1236  
1237 =head2 dnumeric
1238
1239 dnumeric ($piddl,[$dim,[$bool]]);
1240  
1241 get/set the flag if the piddle dim has numeric or other units. This has influence
1242 on vals, dmin, dmax, dinc.
1243  
1244 =head2 initdim
1245  
1246 initializes a dimenson
1247  
1248 usage:
1249 initdim($piddle,$dimname,[%args]);
1250  
1251 Arguments are the fields listed above. This should be called repeatedly after piddle
1252 creation to match the structure.
1253  
1254 If pos is not provided, the next free position is used. If you call
1255  
1256 initidim ($piddle,$name)
1257  
1258 N times on an N-dimensional piddle, it will create N dimensions with sizes corresponding to the
1259 dim of the piddle.
1260  
1261 =head2 rmdim
1262  
1263 removes a dminenso
1264  
1265 rmdim($piddle,$dim);
1266  
1267  
1268  
1269 =head2 copy_dim
1270  
1271 copies the diminfo from one piddle to the next.
1272  
1273 copy_dim($a,$b,[$dim,[$pos]]);
1274  
1275 calls initdim on $b with parameters from $a. You can supply a new position. If you
1276 call it with just two arguments, all dims are copied.
1277  
1278 =head2 sln
1279  
1280 sln ($piddle,%slices);
1281  
1282 This replaces slice and it's derivatives.
1283  
1284 perform slicing based on names. Returns correct dimension info. Unspecified
1285 dims are returned fully. If you want to squeeze, use
1286 sln($piddle, $dim=>"($value)");
1287
1288 or nsqueeze on the result, if you want all squeezed. Both will do the necessary rmdim calls for you,
1289  
1290  
1291 Example:
1292 $sl=sln($a,x=>'0:10:2',t=>'4:',);
1293  
1294 You can omit dims; it parses each key-value pair and constructs a slice string,
1295 so what typically works for slice, works here.
1296  
1297 =head2 nop
1298  
1299 This is the way to perform non-aggregate operations on one piddle which work only on one dim,
1300 like e.g. rotate.
1301  
1302 usage:
1303 $res=nop($a,$operation,[@args]);
1304  
1305  
1306 =head2 ncop
1307  
1308 This is the way to perform any operations involving two operands when using PDL::Dims.
1309  
1310 operates on two piddles, combining them by names. Equally named dimensions have
1311 to obey the usual threding rules. For opertators like '+' use the named version
1312 and an additional argument of 0.
1313  
1314 usage:
1315 $c=ncop($a,$b,$operation,[@args]);
1316  
1317 =head2 nagg
1318  
1319 Use to perform aggregate functions, that reduce the dim by 1.
1320  
1321 nagg($piddle,$operation,$dim,[@args]);
1322  
1323 Use this to perform sumover, average, ...
1324  
1325  
1326 =head2 nreduce
1327  
1328 a wrapper around reduce (requires PDL::Reduce) calling with names instead.
1329  
1330 nreduce($piddle,$operations,@dimnames);
1331  
1332 =head2 active_slice
1333  
1334 A conveniance function, similar to sln, but it returns a slice at the current position (index).
1335  
1336 useage
1337  
1338 $slice=active_slice($piddle,@ignore);
1339  
1340 returns the current selection (as accessed by idx) as a slice. Returns full dims on supplied dim list.
1341  
1342 Call nsqueeze afterwards if that is what you want.
1343  
1344 =head2 nsqueeze
1345  
1346 A wrapper to squeeze. It makes the appropriate header updates (i.e. calls to rmdim).
1347  
1348  
1349 =head2 is_sane
1350  
1351 A sanity check for piddles. This is your friend when working with PDL::Dims!
1352  
1353 $err=is_sane($piddle)
1354  
1355  
1356 returns 0 upon success, otherwise the first inconsisteny found is returned. This may
1357 change in future releases.
1358  
1359 =head1 AUTHOR
1360  
1361 Ingo Schmid
1362  
1363  
1364  
1365  
1366 =head1 LICENSE AND COPYRIGHT
1367  
1368 Copyright 2014 Ingo Schmid.
1369  
1370 This program is free software; you can redistribute it and/or modify it
1371 under the terms of the the Artistic License (2.0). You may obtain a
1372 copy of the full license at:
1373  
1374 L<http://www.perlfoundation.org/artistic_license_2_0>
1375  
1376 Any use, modification, and distribution of the Standard or Modified
1377 Versions is governed by this Artistic License. By using, modifying or
1378 distributing the Package, you accept this license. Do not use, modify,
1379 or distribute the Package, if you do not accept this license.
1380  
1381 If your Modified Version has been derived from a Modified Version made
1382 by someone other than you, you are nevertheless required to ensure that
1383 your Modified Version complies with the requirements of this license.
1384  
1385 This license does not grant you the right to use any trademark, service
1386 mark, tradename, or logo of the Copyright Holder.
1387  
1388 This license includes the non-exclusive, worldwide, free-of-charge
1389 patent license to make, have made, use, offer to sell, sell, import and
1390 otherwise transfer the Package with respect to any patent claims
1391 licensable by the Copyright Holder that are necessarily infringed by the
1392 Package. If you institute patent litigation (including a cross-claim or
1393 counterclaim) against any party alleging that the Package constitutes
1394 direct or contributory patent infringement, then this Artistic License
1395 to you shall terminate on the date that such litigation is filed.
1396  
1397 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1398 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1399 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1400 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1401 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1402 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1403 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1404 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1405  
1406  
1407 =cut
1408  
1409 4 1; # End of PDL::Dims
↑ Summary