blib/lib/PDLDM/Rank.pm

filter: Statementstmt 98.7% 163 / 165 Branchbran 92.4% 61 / 66 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 6 totaltotal 94.9% 245 / 258 CC 36 SCAR 18.7
CC
36
Cov
97%
EstimateUniqueRankWithDups
8.0
EstimateTiedRank
7.0
EstimateTiedRankWithDups
7.0
CRAP 6.5
↑ Summary
1 package PDLDM::Rank;
2
3 1 ▶use 5.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 require Exporter;
7
8 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $VERSION = 2015.071601;
10
11 1 require Exporter;
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT_OK = qw(TiedRank EstimateTiedRank EstimateTiedRankWithDups UniqueRank EstimateUniqueRankWithDups) ;
14
15 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 1 ▶sub EstimateTiedRank{#Estimate the tied rank of an unseen instance
Subroutine EstimateTiedRank: called CC=7 SCAR=19.5
Pod undocumented
18 1 my $insts = $_[0]; #Unseen instances
19 1 my $mypdl = $_[1]; #Original Dataset of which Tied Ranks are already calculated
20 1 my $ranked_pdl = $_[2]; # Tied Ranked dataset of $mypdl
21
22 1 my $rows = $mypdl->getdim(0);
23 1 my $cols = $mypdl->getdim(1);
24
25 1 my $insts_rows = $insts->getdim(0);
26
27 1 my $inst_ranked = PDL->zeros($insts_rows,$cols) -1;
28 1 my $inst_uniqueness = PDL->ones($insts_rows,$cols);
29
30
31 1 ▶ for (my $this_col=0; $this_col < $cols; $this_col++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_col < $cols) 2 1 3
32 2 my $original_values = $mypdl->dice_axis(1,$this_col)->flat;
33 2 my $sorted_indx = qsorti($original_values);
34 2 my $sorted_values = $original_values->dice($sorted_indx);
35
36 2 ▶ for (my $this_inst_raw=0; $this_inst_raw < $insts_rows; $this_inst_raw++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_inst_raw < $insts_rows) 8 2 10
37 8 my $this_value = $insts->dice($this_inst_raw,$this_col)->flat;
38
39 8 ▶ for (my $i = 0; $i < $rows; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 36 1 37
40 36 my $sorted_val = $sorted_values->dice($i)->flat;
41 36 ▶ if ($this_value == $sorted_val) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_value == $sorted_val) { } 3 33 36
elsif ($this_value < $sorted_val) { } 4 29 33
42 3 SetValue($inst_ranked->dice($this_inst_raw,$this_col),
43 $ranked_pdl->dice($sorted_indx->dice($i),$this_col));
44 3 SetValue($inst_uniqueness->dice($this_inst_raw,$this_col),0,0);
45 3 last;
46 }elsif ($this_value < $sorted_val) {
47 4 SetValue($inst_ranked->dice($this_inst_raw,$this_col),$i);
48 4 last;
49 }
50 }
51 8 ▶ if ($inst_ranked->dice($this_inst_raw,$this_col) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inst_ranked->dice($this_inst_raw, $this_col) == -1) 1 7 8
52 1 SetValue($inst_ranked->dice($this_inst_raw,$this_col),$rows);
53 }
54
55 }
56 }
57
58 1 return ($inst_ranked,$inst_uniqueness);
59 }
60
61 1 ▶sub EstimateUniqueRankWithDups{#Estimate the tied rank of an unseen instance
Subroutine EstimateUniqueRankWithDups: called CC=8 SCAR=20.8
Pod undocumented
62 1 my $insts = $_[0]; #Unseen instances
63 1 my $mypdl = $_[1]; #Original Dataset of which Tied Ranks are already calculated
64 1 my $ranked_pdl = $_[2]; # Tied Ranked dataset of $mypdl
65 1 my $duplicates_pdl = $_[3]; # Number of duplicates in the tied Ranked dataset of $mypdl
66
67 1 my $rows = $mypdl->getdim(0);
68 1 my $cols = $mypdl->getdim(1);
69
70 1 my $insts_rows = $insts->getdim(0);
71
72 1 my $inst_ranked = PDL->zeros($insts_rows,$cols) -1;
73 1 my $inst_duplicates = PDL->zeros($insts_rows,$cols);
74
75
76 1 ▶ for (my $this_col=0; $this_col < $cols; $this_col++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_col < $cols) 2 1 3
77 2 my $original_values = $mypdl->dice_axis(1,$this_col)->flat;
78 2 my $sorted_indx = qsorti($original_values);
79 2 my $sorted_values = $original_values->dice($sorted_indx);
80
81 2 ▶ for (my $this_inst_raw=0; $this_inst_raw < $insts_rows; $this_inst_raw++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_inst_raw < $insts_rows) 8 2 10
82 8 my $this_value = $insts->dice($this_inst_raw,$this_col)->flat;
83
84 8 ▶ for (my $i = 0; $i < $rows; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 36 1 37
85 36 my $sorted_val = $sorted_values->dice($i)->flat;
86 36 ▶ if ($this_value == $sorted_val) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_value == $sorted_val) { } 3 33 36
elsif ($this_value < $sorted_val) { } 4 29 33
87 3 SetValue($inst_ranked->dice($this_inst_raw,$this_col),
88 $ranked_pdl->dice($sorted_indx->dice($i),$this_col));
89 3 SetValue($inst_duplicates->dice($this_inst_raw,$this_col),
90 $duplicates_pdl->dice($sorted_indx->dice($i),$this_col));
91 3 last;
92 }elsif ($this_value < $sorted_val) {
93 4 ▶ if ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) { } 2 2 4
94 2 SetValue($inst_ranked->dice($this_inst_raw,$this_col),
95 $ranked_pdl->dice($sorted_indx->dice($i-1),$this_col));
96 2 last;
97 }else{
98 2 SetValue($inst_ranked->dice($this_inst_raw,$this_col),0);
99 2 last;
100 }
101 }
102 }
103 8 ▶ if ($inst_ranked->dice($this_inst_raw,$this_col) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inst_ranked->dice($this_inst_raw, $this_col) == -1) 1 7 8
104 1 SetValue($inst_ranked->dice($this_inst_raw,$this_col),$ranked_pdl->dice($sorted_indx->dice($rows-1),$this_col));
105 }
106
107 }
108 }
109
110 1 return ($inst_ranked,$inst_duplicates);
111 }
112
113 1 ▶sub EstimateTiedRankWithDups{#Estimate the tied rank of an unseen instance
Subroutine EstimateTiedRankWithDups: called CC=7 SCAR=19.5
Pod undocumented
114 1 my $insts = $_[0]; #Unseen instances
115 1 my $mypdl = $_[1]; #Original Dataset of which Tied Ranks are already calculated
116 1 my $ranked_pdl = $_[2]; # Tied Ranked dataset of $mypdl
117 1 my $duplicates_pdl = $_[3]; # Number of duplicates in the tied Ranked dataset of $mypdl
118
119 1 my $rows = $mypdl->getdim(0);
120 1 my $cols = $mypdl->getdim(1);
121
122 1 my $insts_rows = $insts->getdim(0);
123
124 1 my $inst_ranked = PDL->zeros($insts_rows,$cols) -1;
125 1 my $inst_duplicates = PDL->zeros($insts_rows,$cols);
126
127
128 1 ▶ for (my $this_col=0; $this_col < $cols; $this_col++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_col < $cols) 2 1 3
129 2 my $original_values = $mypdl->dice_axis(1,$this_col)->flat;
130 2 my $sorted_indx = qsorti($original_values);
131 2 my $sorted_values = $original_values->dice($sorted_indx);
132
133 2 ▶ for (my $this_inst_raw=0; $this_inst_raw < $insts_rows; $this_inst_raw++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_inst_raw < $insts_rows) 8 2 10
134 8 my $this_value = $insts->dice($this_inst_raw,$this_col)->flat;
135
136 8 ▶ for (my $i = 0; $i < $rows; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 36 1 37
137 36 my $sorted_val = $sorted_values->dice($i)->flat;
138 36 ▶ if ($this_value == $sorted_val) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_value == $sorted_val) { } 3 33 36
elsif ($this_value < $sorted_val) { } 4 29 33
139 3 SetValue($inst_ranked->dice($this_inst_raw,$this_col),
140 $ranked_pdl->dice($sorted_indx->dice($i),$this_col));
141 3 SetValue($inst_duplicates->dice($this_inst_raw,$this_col),
142 $duplicates_pdl->dice($sorted_indx->dice($i),$this_col));
143 3 last;
144 }elsif ($this_value < $sorted_val) {
145 4 SetValue($inst_ranked->dice($this_inst_raw,$this_col),$i);
146 4 last;
147 }
148 }
149 8 ▶ if ($inst_ranked->dice($this_inst_raw,$this_col) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inst_ranked->dice($this_inst_raw, $this_col) == -1) 1 7 8
150 1 SetValue($inst_ranked->dice($this_inst_raw,$this_col),$rows);
151 }
152
153 }
154 }
155
156 1 return ($inst_ranked,$inst_duplicates);
157 }
158
159 1 ▶sub TiedRank{ #Calculate Tied Ranks
Subroutine TiedRank: called CC=7 SCAR=19.5
Pod undocumented
160 1 my $mypdl = $_[0];
161 1 my $rows = $mypdl->getdim(0);
162 1 my $cols = $mypdl->getdim(1);
163 1 my $ranked_pdl = PDL->zeros($rows,$cols);
164 1 my $duplicates_pdl = PDL->zeros($rows,$cols);
165
166 1 ▶ for (my $this_col=0; $this_col < $cols; $this_col++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_col < $cols) 2 1 3
167 2 my $sorted_indx = qsorti($mypdl->dice_axis(1,$this_col)->flat);
168 2 my $last_ranked_indx = -1;
169 2 my $last_rank = 0;
170 2 my $last_value = $mypdl->dice($sorted_indx->dice(0),$this_col);
171 2 my $duplicate_count = 1;
172
173 2 ▶ for (my $i=1; $i <= $rows; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $rows) 20 2 22
174 20 my $this_value = 0;
175 20 ▶ if ($i < $rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 18 2 20
176 18 $this_value =$mypdl->dice($sorted_indx->dice($i),$this_col);
177 }
178 20 ▶ if (($this_value > $last_value) || ($i == $rows)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_value > $last_value or $i == $rows) { } 11 9 20
Condition100%3/3
$this_value > $last_value or $i == $rows
l!l&&r!l&&!r
929
MC/DC100%2/2
$this_value > $last_value or $i == $rows
$this_value > $last_value $i == $rows
Truth table100%3/3
A or B
A= $this_value > $last_valueB= $i == $rows
ABresult
1X1
011
000
179 11 my $average_rank = ((2 * $last_rank) + $duplicate_count + 1) / 2;
180 #my $ranked_slice = PDL->ones($duplicate_count,1) * $average_rank;
181 11 my $rank_start_indx = $last_ranked_indx + 1;
182 11 $last_ranked_indx = $last_ranked_indx + $duplicate_count;
183
184 11 ▶ for (my $tmp_rank_indx=$rank_start_indx;$tmp_rank_indx <= $last_ranked_indx; $tmp_rank_indx++){
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_rank_indx <= $last_ranked_indx) 20 11 31
185 20 my $tmp_correct_indx = sclr $sorted_indx->dice($tmp_rank_indx);
186 20 SetValue($ranked_pdl->slice("($tmp_correct_indx),($this_col)"),$average_rank);
187 20 SetValue($duplicates_pdl->slice("($tmp_correct_indx),($this_col)"),$duplicate_count);
188 }
189
190 11 $duplicate_count = 1;
191 11 $last_value = $this_value;
192 11 $last_rank = $i;
193 }else{
194 9 $duplicate_count++;
195 }
196 }
197 }
198 1 return ($ranked_pdl,$duplicates_pdl);
199 }
200
201 1 ▶sub UniqueRank{ #Calculate Unique Ranks
Subroutine UniqueRank: called CC=7 SCAR=19.5
Pod undocumented
202 1 my $mypdl = $_[0];
203 1 my $rows = $mypdl->getdim(0);
204 1 my $cols = $mypdl->getdim(1);
205 1 my $ranked_pdl = PDL->zeros($rows,$cols);
206 1 my $duplicates_pdl = PDL->zeros($rows,$cols);
207
208 1 ▶ for (my $this_col=0; $this_col < $cols; $this_col++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_col < $cols) 2 1 3
209 2 my $sorted_indx = qsorti($mypdl->dice_axis(1,$this_col)->flat);
210 2 my $last_ranked_indx = -1;
211 2 my $last_rank = 0;
212 2 my $last_value = $mypdl->dice($sorted_indx->dice(0),$this_col);
213 2 my $duplicate_count = 1;
214
215 2 ▶ for (my $i=1; $i <= $rows; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $rows) 20 2 22
216 20 my $this_value = 0;
217 20 ▶ if ($i < $rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 18 2 20
218 18 $this_value =$mypdl->dice($sorted_indx->dice($i),$this_col);
219 }
220 20 ▶ if (($this_value > $last_value) || ($i == $rows)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_value > $last_value or $i == $rows) { } 11 9 20
Condition100%3/3
$this_value > $last_value or $i == $rows
l!l&&r!l&&!r
929
MC/DC100%2/2
$this_value > $last_value or $i == $rows
$this_value > $last_value $i == $rows
Truth table100%3/3
A or B
A= $this_value > $last_valueB= $i == $rows
ABresult
1X1
011
000
221 11 my $this_rank = $last_rank + 1;
222 #my $ranked_slice = PDL->ones($duplicate_count,1) * $average_rank;
223 11 my $rank_start_indx = $last_ranked_indx + 1;
224 11 $last_ranked_indx = $last_ranked_indx + $duplicate_count;
225
226 11 ▶ for (my $tmp_rank_indx=$rank_start_indx;$tmp_rank_indx <= $last_ranked_indx; $tmp_rank_indx++){
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_rank_indx <= $last_ranked_indx) 20 11 31
227 20 my $tmp_correct_indx = sclr $sorted_indx->dice($tmp_rank_indx);
228 20 SetValue($ranked_pdl->slice("($tmp_correct_indx),($this_col)"),$this_rank);
229 20 SetValue($duplicates_pdl->slice("($tmp_correct_indx),($this_col)"),$duplicate_count);
230 }
231
232 11 $duplicate_count = 1;
233 11 $last_value = $this_value;
234 11 $last_rank = $this_rank;
235 }else{
236 9 $duplicate_count++;
237 }
238 }
239 }
240 1 return ($ranked_pdl,$duplicates_pdl);
241 }
242
243 113 ▶sub SetValue{
Subroutine SetValue: called CC=5 SCAR=19.1
Pod undocumented
244 113 my $this_slice = $_[0];
245 113 my $new_values = $_[1];
246 113 my $ops = $_[2]; #0 = Substitute, 1 = add, 2= substract
247
248 113 ▶ if (!(defined($ops))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ops) 110 3 113
249 110 $ops = 0;
250 }
251
252 113 ▶ if ($ops == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ops == 0) { } 113 0 113
elsif ($ops == 1) { } 0 0 0
elsif ($ops == 2) { } 0 0 0
253 113 $this_slice .= $new_values;
254 }elsif($ops == 1){
255 0 $this_slice += $new_values;
256 }elsif($ops == 2){
257 0 $this_slice -= $new_values;
258 }
259 }
260
261 1 1;
262
263 __END__
↑ Summary