blib/lib/PDLDM/Common.pm

filter: Statementstmt 73.2% 63 / 86 Branchbran 41.6% 10 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 6 totaltotal 65.1% 84 / 129 CC 13 SCAR 21.7
CC
13
Cov
66%
SetValue
30.0
WritePDL3DtoCSV
12.0
NormalizeData
3.0
CRAP 8.8
↑ Summary
1 package PDLDM::Common;
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.071403;
10
11 1 require Exporter;
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT_OK = qw(NormalizeData WritePDL2DtoCSV ReadCSVtoPDL WritePDL3DtoCSV WritePDL3DtoCSV SetValue GetSampleWithoutReplacements);
14
15 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Math::Random::MT::Auto qw(rand shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 1 ▶sub NormalizeData{
Subroutine NormalizeData: called CC=3 SCAR=11.0
Pod undocumented
20 1 my $data = $_[0];
21 1 my $rows = $data->getdim(0);
22 1 my $cols = $data->getdim(1);
23 1 ▶ for (my $this_col=0; $this_col < $cols; $this_col++){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_col < $cols) 3 1 4
24 3 my $this_raw = $data->slice(":,($this_col)");
25 3 my $min_data = transpose(minimum($this_raw));
26 3 my $max_data = transpose(maximum($this_raw));
27 3 ▶ if (abs($min_data - $max_data) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $min_data - $max_data > 0) 3 0 3
28 3 $this_raw .= ($this_raw - $min_data) / ($max_data - $min_data);
29 }
30 }
31 }
32
33 1 ▶sub WritePDL2DtoCSV{
Subroutine WritePDL2DtoCSV: called CC=2 SCAR=6.9
Pod undocumented
34 1 my $mypdl = $_[0];
35 1 my $csv_file = $_[1];
36
37 1 unlink($csv_file);
38 1 open FF, ">" . $csv_file ;
39 1 my $rows = $mypdl->getdim(0);
40 1 ▶ for (my $r = 0; $r < $rows; $r++){
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $rows) 10 1 11
41 10 print FF join(",",@{unpdl $mypdl->slice("($r),:")}) ;
42 10 print FF "\n";
43 }
44 1 close FF;
45
46 }
47
48 1 ▶sub ReadCSVtoPDL{
Subroutine ReadCSVtoPDL: called CC=2 SCAR=6.9
Pod undocumented
49 1 my $csv_file = $_[0];
50 1 open my $fh, "<", $csv_file;
51 1 my @CSVData;
52 1 my $csv = Text::CSV->new();
53 1 ▶ while ( my $row = $csv->getline( $fh ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 10 1 11
54 10 push @CSVData, $row;
55 }
56 1 return transpose(pdl(@CSVData));
57 }
58
59 0 ▶sub WritePDL3DtoCSV{
Subroutine WritePDL3DtoCSV: not called CC=3 SCAR=24.8
Pod undocumented
60 0 my $mypdl = $_[0];
61 0 my $csv_file = $_[1];
62
63 0 unlink($csv_file);
64 0 open FF, ">" . $csv_file ;
65 0 my $d1rows = $mypdl->getdim(0);
66 0 my $d2rows = $mypdl->getdim(1);
67 0 print FF join(",",@{unpdl $mypdl->shape}) . "\n" ;
68 0 ▶ for (my $d1r = 0; $d1r < $d1rows; $d1r++){
Branch: 1 branch
BranchTrueFalseTotal
if ($d1r < $d1rows) 0 0 0
69 0 ▶ for (my $d2r = 0; $d2r < $d2rows; $d2r++){
Branch: 1 branch
BranchTrueFalseTotal
if ($d2r < $d2rows) 0 0 0
70 0 print FF join(",",@{unpdl $mypdl->slice("($d1r),($d2r),:")}) ;
71 0 print FF "\n";
72 }
73 }
74 0 close FF;
75 }
76
77 0 ▶sub SetValue{
Subroutine SetValue: not called CC=5 SCAR=34.0
Pod undocumented
78 0 my $this_slice = $_[0];
79 0 my $new_values = $_[1];
80 0 my $ops = $_[2]; #0 = Substitute, 1 = add, 2= substract
81
82 0 ▶ if (!(defined($ops))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ops) 0 0 0
83 0 $ops = 0;
84 }
85
86 0 ▶ if ($ops == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ops == 0) { } 0 0 0
elsif ($ops == 1) { } 0 0 0
elsif ($ops == 2) { } 0 0 0
87 0 $this_slice .= $new_values;
88 }elsif($ops == 1){
89 0 $this_slice += $new_values;
90 }elsif($ops == 2){
91 0 $this_slice -= $new_values;
92 }
93 }
94
95 1 ▶sub GetSampleWithoutReplacements{
Subroutine GetSampleWithoutReplacements: called CC=3 SCAR=11.0
Pod undocumented
96 1 my $matrix_size = $_[0];
97 1 my $sample_size = $_[1];
98
99 1 my $sample_h = {};
100 1 my $sample_count = 0;
101
102 1 ▶ while ($sample_count < $sample_size){
Branch: 1 branch
BranchTrueFalseTotal
if ($sample_count < $sample_size) 10 1 11
103 10 my $rn = floor(rand($matrix_size));
104 10 ▶ if (!exists($sample_h->{$rn})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $sample_h->{$rn}) 10 0 10
105 10 $sample_h->{$rn} = 1;
106 10 $sample_count++;
107 }
108 }
109
110 1 return pdl (keys %{$sample_h});
111 }
112
113 # Preloaded methods go here.
114
115 1 1;
116 __END__
↑ Summary