Moose/DataFile/Read.pm

filter: Statementstmt 93.1% 68 / 73 Branchbran 64.2% 18 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 84.3% 102 / 121 CC 17 SCAR 20.1
CC
17
Cov
85%
read_gnuplot_format
13.7
get_blocks
9.1
BEGIN
1.0
CRAP 7.5
« Moose/Instrument/Log.pm ↑ Summary Moose/Connection/USB.pm »
1 package Lab::Moose::DataFile::Read;
2 6 $Lab::Moose::DataFile::Read::VERSION = '3.931';
3 #ABSTRACT: Read a gnuplot-style 2D data file
4  
5 6 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use MooseX::Params::Validate 'validated_list';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Moose::Util::TypeConstraints 'enum';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::Util qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #use PDL::Core qw/pdl cat dog/;
15 6 ▶use Fcntl 'SEEK_SET';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 our @EXPORT = qw/read_gnuplot_format/;
21  
22  
23 # produce 2D PDL for each block. Cat them into a 3d PDL
24 2 ▶sub get_blocks {
Subroutine get_blocks: called CC=9 SCAR=22.1
Pod undocumented
25 2 my ( $fh, $num_columns ) = validated_list(
26 \@_,
27 fh => { isa => 'FileHandle', optional => 1 },
28 num_columns => { isa => 'Int' },
29 );
30  
31 2 my @blocks;
32 my @rows;
33 2 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 15 2 17
34 15 ▶ if ( $line =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/u) 2 13 15
35 2 next;
36 }
37 13 ▶ if ( $line =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/u) 2 11 13
38  
39 # Finish block. Need check for number of rows if we have
40 # multiple subsequent blank lines
41 2 ▶ if ( @rows > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows > 0) 2 0 2
42  
43 # Give \@rows, not @rows to get a 2D piddle if we
44 # only have a single row.
45 2 push @blocks, pdl( \@rows );
46 2 @rows = ();
47 }
48 2 next;
49 }
50  
51 # awk splitting behaviour
52 11 my @nums = split( ' ', $line );
53 11 ▶ if ( @nums != $num_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nums != $num_columns) 0 11 11
54 0 die "num cols not $num_columns";
55 }
56 11 push @rows, [@nums];
57 }
58 2 ▶ if ( @rows > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows > 0) 2 0 2
59 2 push @blocks, pdl( \@rows );
60 }
61  
62 # bring blocks to same number of rows: reshape and add NaNs.
63 2 my $max_rows = List::Util::max( map { ( $_->dims )[1] } @blocks );
64  
65 2 for my $block (@blocks) {
66 4 my $rows = ( $block->dims() )[1];
67 4 ▶ if ( $rows < $max_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows < $max_rows) 2 2 4
68 2 $block->reshape( $num_columns, $max_rows );
69 2 $block->slice(":,${rows}:-1") .= "NaN";
70 }
71 }
72  
73 2 return PDL::cat(@blocks);
74 }
75  
76 2 ▶sub read_gnuplot_format {
Subroutine read_gnuplot_format: called CC=9 SCAR=26.2
Pod documented
77 2 my ( $type, $fh, $file, $num_columns ) = validated_list(
78 \@_,
79 type => { isa => enum( [qw/columns maps bare/] ) },
80 fh => { isa => 'FileHandle', optional => 1 },
81 file => { isa => 'Str', optional => 1 },
82 num_columns => { isa => 'Int' },
83 );
84  
85 2 ▶ if ( !( $fh || $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh or $file) 0 2 2
Condition66%2/3
$fh or $file
l!l&&r!l&&!r
110
MC/DC0%0/2
$fh or $file
$fh $file
Truth table66%2/3
A or B
A= $fhB= $file
ABresult
1X1
011
000
86 0 croak "read_2d_gnuplot_format needs either 'fh' or 'file' argument";
87 }
88  
89 2 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 1 1 2
90 1 ▶ open $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or croak("cannot open file ${file}: $!") 1 0 1
91 or croak "cannot open file $file: $!";
92 }
93  
94 # Rewind filehandle.
95 2 ▶ seek $fh, 0, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or croak("cannot seek: $!") 2 0 2
96 or croak "cannot seek: $!";
97  
98 2 my $blocks = get_blocks( fh => $fh, num_columns => $num_columns );
99  
100 # $blocks is 3D PDL with following dims
101 # 0st dim: column
102 # 1st dim: row (in block)
103 # 2nd dim: block
104  
105 2 ▶ if ( $type eq 'bare' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'bare') { } 0 2 2
elsif ($type eq 'columns') { } 2 0 2
elsif ($type eq 'maps') { } 0 0 0
106 0 return $blocks;
107 }
108 elsif ( $type eq 'columns' ) {
109  
110 # merge blocks
111 2 my $result = $blocks->clump( 1, 2 );
112  
113 # switch row/column dimensions
114 2 $result = $result->xchg( 0, 1 );
115  
116 # return one pdl for each column
117 2 return dog($result);
118 }
119 elsif ( $type eq 'maps' ) {
120  
121 # 3D gnuplot data file (two x values, three y value):
122 # x11 y11 z11
123 # x12 y12 z12
124 # x13 y13 z13
125 #
126 # x21 y21 z21
127 # x22 y22 z22
128 # x23 y23 z23
129 #
130 # x-cordinate changes with each block: x11, x12 and x13 will be equal
131 # in most cases (exception: sweep of B-field or temperature where
132 # they will be almost equal.
133 #
134 # Parse into three 2x3 piddles for x, y and z data
135 # (first piddle dim (x) goes to the right):
136  
137 # x11 x21
138 # x12 x22
139 # x13 x23
140  
141 # y11 y21
142 # y12 y22
143 # y13 y23
144  
145 # and same for z
146 0 my $result = $blocks->xchg( 0, 2 );
147 0 return dog($result);
148 }
149 }
150  
151 6 1;
152  
153 __END__
« Moose/Instrument/Log.pm ↑ Summary Moose/Connection/USB.pm »