File Coverage

blib/lib/Lab/Moose.pm
Criterion Covered Total %
statement 60 61 98.3
branch 7 8 87.5
condition n/a
subroutine 16 16 100.0
pod 6 6 100.0
total 89 91 97.8


line stmt bran cond sub pod time code
1             package Lab::Moose;
2             $Lab::Moose::VERSION = '3.880';
3             #ABSTRACT: Convenient loaders and constructors for L<Lab::Moose::Instrument>, L<Lab::Moose::Sweep>, L<Lab::Moose::DataFolder> and L<Lab::Moose::DataFile>
4              
5 27     27   19645241 use v5.20;
  27         327  
6              
7 27     27   179 use warnings;
  27         88  
  27         791  
8 27     27   176 use strict;
  27         87  
  27         716  
9              
10 27     27   1269 use MooseX::Params::Validate;
  27         1291712  
  27         238  
11 27     27   15622 use Moose::Util::TypeConstraints qw/subtype as where message coerce from via/;
  27         80  
  27         311  
12 27     27   33322 use Module::Load;
  27         11126  
  27         218  
13 27     27   14068 use Lab::Moose::Connection;
  27         135  
  27         1270  
14 27     27   17029 use Lab::Moose::DataFolder;
  27         136  
  27         1184  
15 27     27   259 use Carp;
  27         72  
  27         24605  
16              
17             our @ISA = qw(Exporter);
18              
19             # FIXME: export 'use warnings; use strict; into caller'
20              
21             our @EXPORT
22             = qw/instrument datafolder datafile linspace sweep sweep_datafile/;
23              
24              
25             # Enable "use warnings; use strict" in caller.
26             # See https://www.perlmonks.org/?node_id=1095522
27             # and https://metacpan.org/pod/Import::Into
28              
29             sub import {
30 43     43   4602 __PACKAGE__->export_to_level( 1, @_ );
31 43         729 strict->import();
32 43         25291 warnings->import();
33             }
34              
35             sub instrument {
36 31     31 1 16148 my %args = validated_hash(
37             \@_,
38             type => { isa => 'Str', optional => 1 },
39             MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
40             );
41              
42 31         9847 my $type = delete $args{type};
43 31 50       131 if ( defined $type ) {
44 31         116 $type = "Lab::Moose::Instrument::$type";
45             }
46             else {
47 0         0 $type = "Lab::Moose::Instrument";
48             }
49 31         186 load $type;
50              
51 31         2845 return $type->new(%args);
52             }
53              
54              
55             sub datafolder {
56 73     73 1 17596 return Lab::Moose::DataFolder->new(@_);
57             }
58              
59              
60             sub datafile {
61 37     37 1 251 my (%args) = validated_hash(
62             \@_,
63             type => { isa => 'Str', default => 'Gnuplot' },
64             MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1
65             );
66              
67 37         3832 my $type = delete $args{type};
68              
69 37         108 $type = "Lab::Moose::DataFile::$type";
70              
71 37         145 load $type;
72              
73 37         3695 return $type->new(%args);
74             }
75              
76              
77             sub linspace {
78 188     188 1 1309 my ( $from, $to, $step, $exclude_from ) = validated_list(
79             \@_,
80             from => { isa => 'Num' },
81             to => { isa => 'Num' },
82             step => { isa => 'Num' },
83             exclude_from => { isa => 'Bool', default => 0 },
84             );
85              
86 188         77058 $step = abs($step);
87 188 100       516 my $sign = $to > $from ? 1 : -1;
88              
89 188         437 my @steps;
90 188 100       424 for ( my $i = $exclude_from ? 1 : 0;; ++$i ) {
91 277         504 my $point = $from + $i * $sign * $step;
92 277 100       636 if ( ( $point - $to ) * $sign >= 0 ) {
93 188         369 last;
94             }
95 89         147 push @steps, $point;
96             }
97 188         647 return ( @steps, $to );
98             }
99              
100              
101             sub sweep {
102 20     20 1 1131 my (%args) = validated_hash(
103             \@_,
104             type => { isa => 'Str' },
105             MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1
106             );
107              
108 20         4720 my $type = delete $args{type};
109              
110 20         71 $type = "Lab::Moose::Sweep::$type";
111              
112 20         88 load $type;
113              
114 20         1890 return $type->new(%args);
115             }
116              
117             sub sweep_datafile {
118 17     17 1 255 my (%args) = validated_hash(
119             \@_,
120             filename => { isa => 'Str', default => 'data' },
121             MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1
122             );
123              
124 17         2713 my $class = 'Lab::Moose::Sweep::DataFile';
125 17         85 load $class;
126 17         1945 return $class->new( params => \%args );
127             }
128              
129             # Some often used subtypes
130              
131             subtype 'Lab::Moose::PosNum',
132             as 'Num',
133             where { $_ >= 0 },
134             message {"$_ is not a positive number"};
135              
136             subtype 'Lab::Moose::PosInt',
137             as 'Int',
138             where { $_ >= 0 },
139             message {"$_ is not a positive integer number"};
140              
141             subtype 'ArrayRefOfInstruments',
142             as 'ArrayRef[Lab::Moose::Instrument]';
143              
144             coerce 'ArrayRefOfInstruments',
145             from 'Lab::Moose::Instrument', via { [ $_ ] };
146              
147             1;
148              
149             __END__
150              
151             =pod
152              
153             =encoding UTF-8
154              
155             =head1 NAME
156              
157             Lab::Moose - Convenient loaders and constructors for L<Lab::Moose::Instrument>, L<Lab::Moose::Sweep>, L<Lab::Moose::DataFolder> and L<Lab::Moose::DataFile>
158              
159             =head1 VERSION
160              
161             version 3.880
162              
163             =head1 SYNOPSIS
164              
165             use Lab::Moose;
166              
167             my $vna = instrument(
168             type => 'RS_ZVA',
169             connection_type => 'LinuxGPIB',
170             connection_options => {timeout => 2}
171             );
172              
173             my $folder = datafolder();
174             my $file = datafile(
175             type => 'Gnuplot',
176             folder => $folder,
177             filename => 'data.dat',
178             columns => ['gate', 'bias', 'current'],
179             );
180              
181             my $meta_file = datafile(
182             type => 'Meta',
183             folder => $folder,
184             filename => 'file.yml'
185             );
186              
187             my @points = linspace(from => -1, to => 1, step => 0.1);
188              
189             =head1 SUBROUTINES
190              
191             =head2 instrument
192              
193             Load an instrument driver module and call the constructor.
194              
195             Create instrument with new connection:
196              
197             my $instr = instrument(
198             type => 'RS_SMB',
199             connection_type => 'VXI11',
200             connection_options => {host => '192.168.2.23'},
201             # other driver specific options
202             foo => 'ON',
203             bar => 'OFF',
204             );
205              
206             Create instrument with existing connection:
207              
208             my $instr = instrument(
209             type => $type,
210             connection => $connection_object,
211             # driver specific options
212             foo => 'ON',
213             bar => 'OFF',
214             );
215              
216             =head3 Creating a generic instrument driver
217              
218             To create a generic instrument driver, leave the C<type> attribute undefined.
219             This can be useful when testing out new equipment before writing a new driver.
220              
221             use Lab::Moose;
222              
223             my $instrument = instrument(
224             connection_type => 'USB',
225             connection_options => {vid => 0x0957, pid => 0x0607}
226             );
227              
228             # Use low-level methods provided by the connection: write, query, clear
229             print $instrument->query(command => "*IDN?");
230              
231             =head2 datafolder
232              
233             my $folder = datafolder(%args);
234              
235             Create a new L<Lab::Moose::DataFolder>.
236              
237             =head2 datafile
238              
239             my $file = datafile(type => $type, %args);
240              
241             Load Lab::Moose::DataFile::C<$type> and call it's C<new> method with C<%args>.
242              
243             The default type is L<'Gnuplot'|Lab::Moose::DataFile::Gnuplot>.
244              
245             =head2 linspace
246              
247             # create array (-1, -0.9, ..., 0.9, 1)
248             my @points = linspace(from => -1, to => 1, step => 0.1);
249              
250             # create array without first point (-0.9, ..., 1)
251             my @points = linspace(from => -1, to => 1, step => 0.1, exclude_from => 1);
252              
253             =head2 sweep/sweep_datafile
254              
255             These are described in a separate tutorial: L<Lab::Measurement::Tutorial>.
256              
257             =head1 COPYRIGHT AND LICENSE
258              
259             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
260              
261             Copyright 2016 Simon Reinhardt
262             2017 Andreas K. Huettel, Simon Reinhardt
263             2018-2019 Simon Reinhardt
264             2020 Andreas K. Huettel
265             2021 Fabian Weinelt
266              
267              
268             This is free software; you can redistribute it and/or modify it under
269             the same terms as the Perl 5 programming language system itself.
270              
271             =cut