Moose/Stabilizer.pm

filter: Statementstmt 87.0% 67 / 77 Branchbran 61.5% 16 / 26 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 81.8% 99 / 121 CC 15 SCAR 25.2
CC
15
Cov
80%
stabilize
20.8
BEGIN
1.0
BEGIN
1.0
CRAP 12.4
« Moose/Instrument/Keithley2000.pm ↑ Summary Moose/Instrument/OI_ITC503.pm »
1 package Lab::Moose::Stabilizer;
2 2 $Lab::Moose::Stabilizer::VERSION = '3.931';
3 #ABSTRACT: Sensor stabilizer subroutine
4  
5 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Lab::Moose ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::Params::Validate 'validated_list';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Time::HiRes qw/time sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Lab::Moose::Countdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Statistics::Descriptive ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 our @EXPORT = qw/stabilize/;
18  
19 # inspired by old Lab::XPRESS stabilization routines
20  
21  
22 1 ▶sub stabilize {
Subroutine stabilize: called CC=15 SCAR=30.4
Pod documented
23 my (
24 1 $instrument, $setpoint, $getter, $tolerance_setpoint,
25 $tolerance_std_dev,
26 $measurement_interval, $observation_time, $max_stabilization_time,
27 $on_fail, $verbose
28 )
29 = validated_list(
30 \@_,
31 instrument => { isa => 'Object' },
32 setpoint => { isa => 'Num' },
33 getter => { isa => 'CodeRef | Str' },
34 tolerance_setpoint => { isa => 'Lab::Moose::PosNum' },
35 tolerance_std_dev => { isa => 'Lab::Moose::PosNum' },
36 measurement_interval => { isa => 'Lab::Moose::PosNum' },
37 observation_time => { isa => 'Lab::Moose::PosNum' },
38 max_stabilization_time =>
39 { isa => 'Maybe[Lab::Moose::PosNum]', optional => 1 },
40 on_fail => { isa => 'CodeRef | Str', optional => 1 },
41 verbose => { isa => 'Bool' },
42 );
43  
44 1 my @points = ();
45  
46 1 my $num_points = int( $observation_time / $measurement_interval );
47 1 ▶ if ( $num_points == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_points == 0) 0 1 1
48 0 $num_points = 1;
49 }
50  
51 # enable autoflush
52 1 my $autoflush = STDOUT->autoflush();
53 1 my $start_time = time();
54  
55 1 while (1) {
56 14 my $new_value = $instrument->$getter();
57 14 ▶ if ( not looks_like_number($new_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($new_value)) 0 14 14
58 0 croak "$new_value is not a number";
59 }
60 14 push @points, $new_value;
61 14 ▶ if ( @points > $num_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points > $num_points) 4 10 14
62 4 shift @points;
63 }
64  
65 14 ▶ if ( @points == $num_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points == $num_points) { } 5 9 14
66 5 my $crit_stddev;
67 my $crit_setpoint;
68  
69 5 my $stat = Statistics::Descriptive::Full->new();
70 5 $stat->add_data(@points);
71  
72 5 my $std_dev = $stat->standard_deviation();
73 5 ▶ if ( $std_dev < $tolerance_std_dev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($std_dev < $tolerance_std_dev) 1 4 5
74 1 $crit_stddev = 1;
75 }
76  
77 5 my $median = $stat->median();
78 5 ▶ if ( abs( $setpoint - $median ) < $tolerance_setpoint ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $setpoint - $median < $tolerance_setpoint) 4 1 5
79 4 $crit_setpoint = 1;
80 }
81  
82 5 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 5 5
83 0 printf(
84 "Setpoint: %.6e, Value: %.6e, std_dev: %.6e, median: %.6e ",
85 $setpoint, $new_value, $std_dev, $median
86 );
87 }
88 5 ▶ if ( $crit_stddev and $crit_setpoint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crit_stddev and $crit_setpoint) { } 1 4 5
Condition66%2/3
$crit_stddev and $crit_setpoint
!ll&&!rl&&r
401
MC/DC50%1/2
$crit_stddev and $crit_setpoint
$crit_stddev $crit_setpoint
Truth table66%2/3
A and B
A= $crit_stddevB= $crit_setpoint
ABresult
0X0
100
111
89 1 printf("reached stabilization criterion \n");
90 1 last;
91 }
92 else {
93 4 printf("\r");
94 }
95  
96 }
97 else {
98 9 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 9 9
99 0 printf(
100 "Setpoint: %.6e, Value: %.6e, need more points... \r",
101 $setpoint, $new_value
102 );
103 }
104 }
105  
106 13 ▶ if ( $measurement_interval > 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($measurement_interval > 5) { } 0 13 13
107 0 countdown(
108 $measurement_interval,
109 "Measurement interval: Sleeping for "
110 );
111 }
112 else {
113 13 sleep($measurement_interval);
114 }
115  
116 13 ▶ if ( defined $max_stabilization_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_stabilization_time) 0 13 13
117 0 ▶ if ( time() - $start_time > $max_stabilization_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Lab::Moose::Stabilizer::time() - $start_time > $max_stabilization_time) 0 0 0
118 0 printf(
119 "Reached maximum stabilization time \n"
120 );
121 0 ▶ if ( defined $on_fail ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_fail) 0 0 0
122 0 $on_fail->();
123 };
124 0 last;
125 }
126 }
127 }
128  
129 # reset autoflush to previous value
130 1 STDOUT->autoflush($autoflush);
131 }
132  
133 2 1;
134  
135 __END__
« Moose/Instrument/Keithley2000.pm ↑ Summary Moose/Instrument/OI_ITC503.pm »