| 1 | package Lab::Moose; | ||||||||||
| 2 | 27 | $Lab::Moose::VERSION = '3.881'; | |||||||||
| 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 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 27 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 27 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 27 | ▶ | use MooseX::Params::Validate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 27 | ▶ | use Moose::Util::TypeConstraints qw/subtype as where message coerce from via/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 27 | ▶ | use Module::Load; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 27 | ▶ | use Lab::Moose::Connection; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 27 | ▶ | use Lab::Moose::DataFolder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 27 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 27 | our @ISA = qw(Exporter); | |||||||||
| 18 | |||||||||||
| 19 | # FIXME: export 'use warnings; use strict; into caller' | ||||||||||
| 20 | |||||||||||
| 21 | our @EXPORT | ||||||||||
| 22 | 27 | = 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 | ▶ | __PACKAGE__->export_to_level( 1, @_ ); | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 43 | strict->import(); | |||||||||
| 32 | 43 | warnings->import(); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub instrument { | ||||||||||
| 36 | 31 | ▶ | my %args = validated_hash( | ||||||||
|
Subroutine
instrument: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 37 | \@_, | ||||||||||
| 38 | type => { isa => 'Str', optional => 1 }, | ||||||||||
| 39 | MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1, | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | 31 | my $type = delete $args{type}; | |||||||||
| 43 | 31 | ▶ | if ( defined $type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 31 | $type = "Lab::Moose::Instrument::$type"; | |||||||||
| 45 | } | ||||||||||
| 46 | else { | ||||||||||
| 47 | 0 | $type = "Lab::Moose::Instrument"; | |||||||||
| 48 | } | ||||||||||
| 49 | 31 | load $type; | |||||||||
| 50 | |||||||||||
| 51 | 31 | return $type->new(%args); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | sub datafolder { | ||||||||||
| 56 | 73 | ▶ | return Lab::Moose::DataFolder->new(@_); | ||||||||
|
Subroutine
datafolder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | sub datafile { | ||||||||||
| 61 | 37 | ▶ | my (%args) = validated_hash( | ||||||||
|
Subroutine
datafile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | \@_, | ||||||||||
| 63 | type => { isa => 'Str', default => 'Gnuplot' }, | ||||||||||
| 64 | MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1 | ||||||||||
| 65 | ); | ||||||||||
| 66 | |||||||||||
| 67 | 37 | my $type = delete $args{type}; | |||||||||
| 68 | |||||||||||
| 69 | 37 | $type = "Lab::Moose::DataFile::$type"; | |||||||||
| 70 | |||||||||||
| 71 | 37 | load $type; | |||||||||
| 72 | |||||||||||
| 73 | 37 | return $type->new(%args); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | sub linspace { | ||||||||||
| 78 | 188 | ▶ | my ( $from, $to, $step, $exclude_from ) = validated_list( | ||||||||
|
Subroutine
linspace: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 79 | \@_, | ||||||||||
| 80 | from => { isa => 'Num' }, | ||||||||||
| 81 | to => { isa => 'Num' }, | ||||||||||
| 82 | step => { isa => 'Num' }, | ||||||||||
| 83 | exclude_from => { isa => 'Bool', default => 0 }, | ||||||||||
| 84 | ); | ||||||||||
| 85 | |||||||||||
| 86 | 188 | $step = abs($step); | |||||||||
| 87 | 188 | ▶ | my $sign = $to > $from ? 1 : -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | |||||||||||
| 89 | 188 | my @steps; | |||||||||
| 90 | 188 | ▶ | for ( my $i = $exclude_from ? 1 : 0;; ++$i ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 277 | my $point = $from + $i * $sign * $step; | |||||||||
| 92 | 277 | ▶ | if ( ( $point - $to ) * $sign >= 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 188 | last; | |||||||||
| 94 | } | ||||||||||
| 95 | 89 | push @steps, $point; | |||||||||
| 96 | } | ||||||||||
| 97 | 188 | return ( @steps, $to ); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | sub sweep { | ||||||||||
| 102 | 20 | ▶ | my (%args) = validated_hash( | ||||||||
|
Subroutine
sweep: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 103 | \@_, | ||||||||||
| 104 | type => { isa => 'Str' }, | ||||||||||
| 105 | MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1 | ||||||||||
| 106 | ); | ||||||||||
| 107 | |||||||||||
| 108 | 20 | my $type = delete $args{type}; | |||||||||
| 109 | |||||||||||
| 110 | 20 | $type = "Lab::Moose::Sweep::$type"; | |||||||||
| 111 | |||||||||||
| 112 | 20 | load $type; | |||||||||
| 113 | |||||||||||
| 114 | 20 | return $type->new(%args); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | sub sweep_datafile { | ||||||||||
| 118 | 17 | ▶ | my (%args) = validated_hash( | ||||||||
|
Subroutine
sweep_datafile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 119 | \@_, | ||||||||||
| 120 | filename => { isa => 'Str', default => 'data' }, | ||||||||||
| 121 | MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1 | ||||||||||
| 122 | ); | ||||||||||
| 123 | |||||||||||
| 124 | 17 | my $class = 'Lab::Moose::Sweep::DataFile'; | |||||||||
| 125 | 17 | load $class; | |||||||||
| 126 | 17 | return $class->new( params => \%args ); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | # Some often used subtypes | ||||||||||
| 130 | |||||||||||
| 131 | subtype 'Lab::Moose::PosNum', | ||||||||||
| 132 | as 'Num', | ||||||||||
| 133 | 43 | ▶ | where { $_ >= 0 }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 134 | 0 | ▶ | message {"$_ is not a positive number"}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 135 | |||||||||||
| 136 | subtype 'Lab::Moose::PosInt', | ||||||||||
| 137 | as 'Int', | ||||||||||
| 138 | 5 | ▶ | where { $_ >= 0 }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 139 | 0 | ▶ | message {"$_ is not a positive integer number"}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 140 | |||||||||||
| 141 | 27 | subtype 'ArrayRefOfInstruments', | |||||||||
| 142 | as 'ArrayRef[Lab::Moose::Instrument]'; | ||||||||||
| 143 | |||||||||||
| 144 | coerce 'ArrayRefOfInstruments', | ||||||||||
| 145 | 17 | ▶ | from 'Lab::Moose::Instrument', via { [ $_ ] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 146 | |||||||||||
| 147 | 27 | 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.881 | ||||||||||
| 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 | ||||||||||