Moose/Sweep/Step.pm

filter: Statementstmt 91.8% 102 / 111 Branchbran 78.1% 25 / 32 Conditioncond 80.0% 12 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 84.9% 158 / 186 SCAR 31.3
CC
22
Cov
88%
_build_points
17.3
sweep_finished
3.3
get_value
3.1
CRAP 22.8
« Moose/Sweep/Step/Magnet.pm ↑ Summary Connection/VICP.pm »
1 package Lab::Moose::Sweep::Step;
2 2 $Lab::Moose::Sweep::Step::VERSION = '3.881';
3 #ABSTRACT: Base class for step/list sweeps
4  
5 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Moose::Util::TypeConstraints 'enum';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Do not import all functions as they clash with the attribute methods.
14 2 ▶use Lab::Moose 'linspace';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 extends 'Lab::Moose::Sweep';
19  
20 #
21 # Public attributes set by the user
22 #
23  
24 2 has from => ( is => 'ro', isa => 'Num', predicate => 'has_from' );
25 2 has to => ( is => 'ro', isa => 'Num', predicate => 'has_to' );
26 2 has step =>
27 ( is => 'ro', isa => 'Lab::Moose::PosNum', predicate => 'has_step' );
28  
29 2 has points =>
30 ( is => 'ro', isa => 'ArrayRef[Num]', predicate => 'has_points' );
31 2 has steps => ( is => 'ro', isa => 'ArrayRef[Num]', predicate => 'has_steps' );
32  
33 2 has list => (
34 is => 'ro', isa => 'ArrayRef[Num]', predicate => 'has_list',
35 writer => '_list'
36 );
37 2 has backsweep => ( is => 'ro', isa => 'Bool', default => 0 );
38  
39 2 has both_directions => ( is => 'ro', isa => 'Bool', default => 0 );
40  
41 2 has setter => ( is => 'ro', isa => 'CodeRef', required => 1 );
42  
43 #
44 # Private attributes used internally
45 #
46  
47 2 has _points => (
48 is => 'ro', isa => 'ArrayRef[Num]', lazy => 1, init_arg => undef,
49 builder => '_build_points', traits => ['Array'],
50 handles => { get_point => 'get', num_points => 'count', points_array => 'elements' },
51 writer => 'write_points',
52 );
53  
54 2 has index => (
55 is => 'ro', isa => 'Int', default => 0, init_arg => undef,
56 writer => '_index'
57 );
58  
59 2 has current_value => (
60 is => 'ro', isa => 'Num', init_arg => undef,
61 writer => '_current_value'
62 );
63  
64 2 my $error_msg = <<"EOF";
65 give either (from => ..., to => ..., step => ...)
66 or (list => [...])
67 or (points => [...], steps => [....])
68 or (points => [...], step => )
69  
70 EOF
71  
72 sub _build_points {
73 18 ▶ my $self = shift;
Subroutine _build_points: called CC=16 SCAR=28.5
74 18 ▶ my $has_from_to_step
Condition66%4/6
$self->has_from && $self->has_to && $self->has_step
!ll&&!rl&&r
4014
$self->has_from and $self->has_to
!ll&&!rl&&r
4014
MC/DC33%1/3
$self->has_from && $self->has_to && $self->has_step
$self->has_from $self->has_to $self->has_step
Truth table50%2/4
A and B && C
A= $self->has_fromB= $self->has_toC= $self->has_step
ABCresult
0XX0
10X0
1100
1111
75 = $self->has_from && $self->has_to && $self->has_step;
76 18 my $has_list = $self->has_list;
77 18 ▶ my $has_points_steps = $self->has_points && $self->has_steps;
Condition100%3/3
$self->has_points && $self->has_steps
!ll&&!rl&&r
1611
MC/DC100%2/2
$self->has_points && $self->has_steps
$self->has_points $self->has_steps
Truth table100%3/3
A && B
A= $self->has_pointsB= $self->has_steps
ABresult
0X0
100
111
78 18 ▶ my $has_points_step = $self->has_points && $self->has_step;
Condition100%3/3
$self->has_points && $self->has_step
!ll&&!rl&&r
1611
MC/DC100%2/2
$self->has_points && $self->has_step
$self->has_points $self->has_step
Truth table100%3/3
A && B
A= $self->has_pointsB= $self->has_step
ABresult
0X0
100
111
79 18 ▶ if ( $has_from_to_step + $has_list + $has_points_steps + $has_points_step
Branch: 1 branch
BranchTrueFalseTotal
if ($has_from_to_step + $has_list + $has_points_steps + $has_points_step != 1) 0 18 18
80 != 1 ) {
81 0 croak $error_msg;
82 }
83  
84 18 my @points;
85  
86 18 ▶ if ($has_list) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_list) { } 2 16 18
elsif ($has_from_to_step) { } 14 2 16
87 2 @points = @{ $self->list() };
88 2 ▶ if ( @points < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 1) 0 2 2
89 0 croak "list needs at least 1 point";
90 }
91 }
92 elsif ($has_from_to_step) {
93 14 @points = linspace(
94 from => $self->from,
95 to => $self->to,
96 step => $self->step
97 );
98 }
99 else {
100 # points_steps or points_step
101 2 my @steps;
102 2 my @corner_points = @{ $self->points };
103 2 ▶ if ( @corner_points < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@corner_points < 2) 0 2 2
104 0 croak "points array needs at least two elements";
105 }
106  
107 2 ▶ if ($has_points_steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_points_steps) { } 1 1 2
108 1 @steps = @{ $self->steps };
109 }
110 else {
111 1 @steps = ( $self->step );
112 }
113 2 ▶ if ( @steps >= @corner_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps >= @corner_points) 0 2 2
114 0 croak "steps array exceeds points array";
115 }
116 2 push @steps, map { $steps[-1] } ( 1 .. @corner_points - @steps - 1 );
117 2 my ( $p1, $p2 );
118 2 $p1 = shift @corner_points;
119 2 ▶ while (@corner_points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@corner_points) 4 2 6
120 4 my $p2 = shift @corner_points;
121 4 my $step = shift @steps;
122 4 push @points, linspace( from => $p1, to => $p2, step => $step );
123 4 $p1 = $p2;
124 }
125 }
126  
127 18 ▶ if ( $self->backsweep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backsweep) 1 17 18
128 1 my @backsweep_points = reverse @points;
129 1 push @points, @backsweep_points;
130 }
131 18 ▶ if ( $self->backsweep && $self->both_directions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backsweep and $self->both_directions) 0 18 18
Condition66%2/3
$self->backsweep and $self->both_directions
!ll&&!rl&&r
1710
MC/DC0%0/2
$self->backsweep and $self->both_directions
$self->backsweep $self->both_directions
Truth table66%2/3
A and B
A= $self->backsweepB= $self->both_directions
ABresult
0X0
100
111
132 0 croak "Can't use backsweep and both_directions together."
133 }
134  
135 18 return \@points;
136 }
137  
138 ▶sub start_sweep {
Subroutine start_sweep: called CC=1 SCAR=0.0
Pod undocumented
139  
140 # do nothing
141 }
142  
143 sub go_to_next_point {
144 142 ▶ my $self = shift;
Subroutine go_to_next_point: called CC=1 SCAR=0.0
Pod undocumented
145 142 my $index = $self->index();
146 142 my $point = $self->get_point($index);
147 142 my $setter = $self->setter();
148 142 $self->$setter($point);
149 142 $self->_current_value($point);
150 142 $self->_index( ++$index );
151 }
152  
153 sub go_to_sweep_start {
154 36 ▶ my $self = shift;
Subroutine go_to_sweep_start: called CC=1 SCAR=0.0
Pod undocumented
155 36 my $point = $self->get_point(0);
156 36 my $setter = $self->setter();
157 36 $self->$setter($point);
158 36 $self->_current_value($point);
159 36 $self->_index(0);
160 }
161  
162 sub sweep_finished {
163 178 ▶ my $self = shift;
Subroutine sweep_finished: called CC=3 SCAR=11.8
Pod undocumented
164 178 my $index = $self->index();
165 178 ▶ if ( $index >= $self->num_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= $self->num_points) 36 142 178
166 36 ▶ if ( $self->both_directions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->both_directions) 0 36 36
167 0 my @points = $self->points_array;
168 0 @points = reverse @points;
169 0 $self->write_points( \@points );
170 }
171 36 return 1;
172 }
173 142 return 0;
174 }
175  
176 sub _in_backsweep {
177 36 ▶ my $self = shift;
Subroutine _in_backsweep: called CC=3 SCAR=11.0
178 36 ▶ if ( $self->backsweep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backsweep) { } 6 30 36
179 6 ▶ if ( $self->index > $self->num_points / 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->index > $self->num_points / 2) 3 3 6
180 3 return 1;
181 }
182 }
183 else {
184 30 return 0;
185 }
186 }
187  
188 sub get_value {
189 36 ▶ my $self = shift;
Subroutine get_value: called CC=3 SCAR=11.2
Pod undocumented
190 36 ▶ if ( not defined $self->current_value() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->current_value) 0 36 36
191 0 croak "sweep not yet started";
192 }
193 36 my $value = sprintf( "%.14g", $self->current_value );
194 36 ▶ if ( $self->_in_backsweep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_backsweep) 3 33 36
195 3 $value .= '_backsweep';
196 }
197 36 return $value;
198 }
199  
200 2 __PACKAGE__->meta->make_immutable();
201 2 1;
202  
203 __END__
204  
205 =pod
206  
207 =encoding UTF-8
208  
209 =head1 NAME
210  
211 Lab::Moose::Sweep::Step - Base class for step/list sweeps
212  
213 =head1 VERSION
214  
215 version 3.881
216  
217 =head1 SYNOPSIS
218  
219 use Lab::Moose;
220  
221 #
222 # basic 1D sweep (e.g. IV curve)
223 #
224
225 my $source = instrument(
226 type => ...,
227 connection_type => ...,
228 connection_options => {...}
229 );
230 my $multimeter = instrument(...);
231
232 my $sweep = sweep(
233 type => 'Step::Voltage',
234 instrument => $instrument,
235 from => -1,
236 to => 1,
237 step => 0.1,
238 backsweep => 1, # points: -1, -0.9, ..., 0.9, 1, 0.9, ..., -1
239 );
240  
241 my $datafile = sweep_datafile(columns => ['volt', 'current']);
242 $datafile->add_plot(x => 'volt', y => 'current');
243
244 my $meas = sub {
245 my $sweep = shift;
246 my $volt = $source->cached_level();
247 my $current = $multimeter->get_value();
248 $sweep->log(volt => $volt, current => $current);
249 };
250  
251 $sweep->start(
252 datafiles => [$datafile],
253 measurement => $meas,
254 );
255  
256 #
257 # 2D sweep (quantum dot stability diagram)
258 #
259
260 my $gate = instrument(...);
261 my $bias = instrument(...);
262 my $multimeter = instrument(...);
263  
264 # master sweep
265 my $gate_sweep = sweep(
266 type => 'Step::Voltage',
267 instrument => $gate,
268 from => -5,
269 to => 5,
270 step => 0.01
271 );
272  
273 # slave sweep
274 my $bias_sweep = sweep(
275 type => 'Step::Voltage',
276 instrument => $bias,
277 from => -1,
278 to => 1,
279 step => 0.01
280 );
281  
282 my $datafile = sweep_datafile(columns => [qw/gate bias current/]);
283 $datafile->add_plot(
284 type => 'pm3d',
285 x => 'gate',
286 y => 'bias',
287 z => 'current'
288 );
289
290 my $meas = sub {
291 my $sweep = shift;
292 my $gate_v = $gate->cached_level();
293 my $bias_v = $bias->cached_level();
294 my $current = $multimeter->get_value();
295 $sweep->log(gate => $gate_v, bias => $bias_v, current => $current);
296 };
297  
298 $gate_sweep->start(
299 slaves => [$bias_sweep],
300 datafiles => [$datafile],
301 measurement => $meas
302 );
303  
304 =head1 DESCRIPTION
305  
306 This C<sweep> constructor defines the following arguments
307  
308 =over
309  
310 =item * from/to/step
311  
312 define a linear range of points.
313  
314 =item * list
315  
316 alternative to from/to/step, give an arbitrary arrayref of points.
317  
318 =item * points/steps
319  
320 alternative to from/to/step. Lets define multiple segments with different steps, e.g.
321  
322 points => [0,1,2],
323 steps => [0.5, 0.2],
324  
325 is equivalent to
326  
327 list => [0, 0.5, 1, 1, 1.2, 1.4, 1.6, 1.8, 2]
328  
329 If C<steps> has fewer elements than segments provided in C<points>, reuse the last value in C<steps>.
330  
331 =item * points/step
332  
333 points => [...],
334 step => $x, # equivalent to steps => [$x]
335  
336 =item * backsweep
337  
338 Include a backsweep: After finishing the sweep, go through all points in
339 reverse.
340  
341 =item * setter
342  
343 A coderef which will be called to change the source level.
344 Use this if you do some arcane type of sweep which does not justify its own
345 sweep subclass.
346 Sweep subclasses like L<Lab::Moose::Sweep::Step::Voltage> will
347 define defaults for this. E.g. for the Voltage sweep:
348  
349 sub {
350 my $sweep = shift;
351 my $value = shift;
352 $sweep->instrument->set_level(value => $value);
353 };
354  
355 =back
356  
357 =head1 COPYRIGHT AND LICENSE
358  
359 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
360  
361 Copyright 2017-2018 Simon Reinhardt
362 2020 Andreas K. Huettel
363 2023 Mia Schambeck
364  
365  
366 This is free software; you can redistribute it and/or modify it under
367 the same terms as the Perl 5 programming language system itself.
368  
369 =cut
« Moose/Sweep/Step/Magnet.pm ↑ Summary Connection/VICP.pm »