| 1 | package Lab::Moose::Instrument::DisplayXY; | ||
| 2 | $Lab::Moose::Instrument::DisplayXY::VERSION = '3.881'; | ||
| 3 | #ABSTRACT: Display with y vs x traces Role for Lab::Moose::Instrument | ||
| 4 | |||
| 5 | use v5.20; | ||
| 6 | |||
| 7 | |||
| 8 | use PDL::Core qw/pdl cat nelem sclr/; | ||
| 9 | use PDL::NiceSlice; | ||
| 10 | use PDL::Graphics::Gnuplot; | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | use Moose::Role; | ||
| 14 | use Lab::Moose; | ||
| 15 | use Lab::Moose::Plot; | ||
| 16 | use MooseX::Params::Validate; | ||
| 17 | use Lab::Moose::Instrument qw/ | ||
| 18 | timeout_param | ||
| 19 | precision_param | ||
| 20 | validated_getter | ||
| 21 | validated_setter | ||
| 22 | validated_channel_getter | ||
| 23 | validated_channel_setter | ||
| 24 | /; | ||
| 25 | |||
| 26 | requires qw( | ||
| 27 | get_StartX | ||
| 28 | get_StopX | ||
| 29 | get_Xpoints_number | ||
| 30 | get_traceX | ||
| 31 | get_traceY | ||
| 32 | get_traceXY | ||
| 33 | get_NameX | ||
| 34 | get_UnitX | ||
| 35 | get_NameY | ||
| 36 | get_UnitY | ||
| 37 | get_log_header | ||
| 38 | get_plot_title | ||
| 39 | display_trace | ||
| 40 | ); | ||
| 41 | |||
| 42 | has plotXY => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Lab::Moose::Plot', | ||
| 45 | init_arg => undef, | ||
| 46 | writer => '_plotXY', | ||
| 47 | predicate => 'has_plotXY' | ||
| 48 | ); | ||
| 49 | |||
| 50 | has xlabel => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'Str', | ||
| 53 | init_arg => undef, | ||
| 54 | predicate => 'has_xlabel' | ||
| 55 | ); | ||
| 56 | |||
| 57 | has ylabel => ( | ||
| 58 | is => 'rw', | ||
| 59 | isa => 'Str', | ||
| 60 | init_arg => undef, | ||
| 61 | predicate => 'has_ylabel' | ||
| 62 | ); | ||
| 63 | |||
| 64 | has datafolder => ( | ||
| 65 | is => 'rw', | ||
| 66 | isa => 'Lab::Moose::DataFolder', | ||
| 67 | init_arg => undef, | ||
| 68 | predicate => 'has_datafolder' | ||
| 69 | ); | ||
| 70 | |||
| 71 | has filename => ( | ||
| 72 | is => 'rw', | ||
| 73 | isa => 'Str', | ||
| 74 | init_arg => undef, | ||
| 75 | predicate => 'has_filename' | ||
| 76 | ); | ||
| 77 | |||
| 78 | has datafile => ( | ||
| 79 | is => 'rw', | ||
| 80 | isa => 'Lab::Moose::DataFile', | ||
| 81 | init_arg => undef, | ||
| 82 | predicate => 'has_datafile' | ||
| 83 | ); | ||
| 84 | |||
| 85 | |||
| 86 | sub get_traceXY { | ||
| 87 | my ( $self, %args ) = @_; | ||
| 88 | |||
| 89 | my $traceY = $self->get_traceY(%args); | ||
| 90 | my $traceX = $self->get_traceX(%args); | ||
| 91 | |||
| 92 | return cat( $traceX, $traceY ); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | sub get_traceX { | ||
| 97 | my ( $self, %args ) = @_; | ||
| 98 | my $trace = delete $args{trace}; | ||
| 99 | |||
| 100 | my $start = $self->get_StartX(%args); | ||
| 101 | my $stop = $self->get_StopX(%args); | ||
| 102 | my $num_points = $self->get_Xpoints_number(%args); | ||
| 103 | my $traceX = pdl linspaced_array( $start, $stop, $num_points ); | ||
| 104 | return $traceX; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub linspaced_array { | ||
| 108 | my ( $start, $stop, $num_points ) = @_; | ||
| 109 | |||
| 110 | my $num_intervals = $num_points - 1; | ||
| 111 | |||
| 112 | if ( $num_intervals == 0 ) { | ||
| 113 | |||
| 114 | # Return a single point. | ||
| 115 | return [$start]; | ||
| 116 | } | ||
| 117 | |||
| 118 | my @result; | ||
| 119 | |||
| 120 | for my $i ( 0 .. $num_intervals ) { | ||
| 121 | my $f = $start + ( $stop - $start ) * ( $i / $num_intervals ); | ||
| 122 | push @result, $f; | ||
| 123 | } | ||
| 124 | |||
| 125 | return \@result; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | sub get_xlabel_based_on_traceXY { | ||
| 130 | my ( $self, %args ) = @_; | ||
| 131 | my $traceXY = $args{traceXY}; | ||
| 132 | if ( !$self->has_xlabel ) { | ||
| 133 | if ( $traceXY ( 0, 0 ) == $traceXY ( -1, 0 ) ) { | ||
| 134 | |||
| 135 | # zero span | ||
| 136 | $self->xlabel( "Counts of zero span at" . " " | ||
| 137 | . $self->get_NameX() . " " | ||
| 138 | . sclr( $traceXY ( 0, 0 ) ) . " " | ||
| 139 | . $self->get_UnitX() ); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | $self->xlabel( $self->get_NameX(%args) . " (" | ||
| 143 | . $self->get_UnitX(%args) | ||
| 144 | . ")" ); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return $self->xlabel; | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | sub get_ylabel { | ||
| 152 | my ( $self, %args ) = @_; | ||
| 153 | if ( !$self->has_ylabel ) { | ||
| 154 | $self->ylabel( $self->get_NameY() . " (" . $self->get_UnitY() . ")" ); | ||
| 155 | } | ||
| 156 | return $self->ylabel; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | sub get_traces_data { | ||
| 161 | my ( $self, %args ) = @_; | ||
| 162 | my @traces = @{ delete $args{traces} }; # arrays are tricky | ||
| 163 | my $all_traces; | ||
| 164 | for my $tr (@traces) { | ||
| 165 | my $data; | ||
| 166 | if ( !defined $all_traces ) { | ||
| 167 | |||
| 168 | # first time | ||
| 169 | $data = $self->get_traceXY( trace => $tr, %args ); | ||
| 170 | $all_traces = $data; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | # acquire and add/glue only Y values | ||
| 174 | $data = $self->get_traceY( trace => $tr, %args ); | ||
| 175 | $all_traces = $all_traces->glue( 1, $data ); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | return $all_traces; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub display_trace_data { | ||
| 183 | my ( $self, %args ) = @_; | ||
| 184 | my $trace = delete $args{trace}; | ||
| 185 | my $traceXY = delete $args{traceXY}; | ||
| 186 | |||
| 187 | if ( !$self->has_plotXY ) { | ||
| 188 | my $plotXY = Lab::Moose::Plot->new(); | ||
| 189 | $self->_plotXY($plotXY); | ||
| 190 | } | ||
| 191 | my $plotXY = $self->plotXY(); | ||
| 192 | |||
| 193 | my $plot_function = 'plot'; | ||
| 194 | if ( $plotXY->gpwin->{replottable} ) { | ||
| 195 | |||
| 196 | # this makes multiple traces on the same plot possible | ||
| 197 | $plot_function = 'replot'; | ||
| 198 | } | ||
| 199 | |||
| 200 | my $x = $traceXY ( :, 0 ); | ||
| 201 | my $y = $traceXY ( :, 1 ); | ||
| 202 | if ( $x ( 0, 0 ) == $x ( -1, 0 ) ) { | ||
| 203 | |||
| 204 | # zero span | ||
| 205 | $x = PDL::Basic::xvals($x); # replacing X with its indexes | ||
| 206 | } | ||
| 207 | my @data = [ $x, $y ]; | ||
| 208 | |||
| 209 | my %plot_options = ( | ||
| 210 | xlab => | ||
| 211 | $self->get_xlabel_based_on_traceXY( traceXY => $traceXY, %args ), | ||
| 212 | ylab => $self->get_ylabel(%args), | ||
| 213 | title => $self->get_plot_title(%args), | ||
| 214 | ); | ||
| 215 | |||
| 216 | my %curve_options = ( | ||
| 217 | with => 'lines', | ||
| 218 | legend => $self->trace_num_to_name( trace => $trace ), | ||
| 219 | ); | ||
| 220 | $plotXY->$plot_function( | ||
| 221 | plot_options => \%plot_options, | ||
| 222 | curve_options => \%curve_options, | ||
| 223 | data => @data, | ||
| 224 | ); | ||
| 225 | return $traceXY; | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | sub display_trace { | ||
| 230 | my ( $self, %args ) = @_; | ||
| 231 | my $trace = delete $args{trace}; | ||
| 232 | my $traceXY = $self->display_traces( %args, traces => [$trace] ); | ||
| 233 | return $traceXY; | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | sub display_traces { | ||
| 238 | my ( $self, %args ) = @_; | ||
| 239 | my $all_traces = $self->get_traces_data(%args); | ||
| 240 | my @traces = @{ delete $args{traces} }; # arrays are tricky | ||
| 241 | my $x = $all_traces ( :, 0 ); | ||
| 242 | my $traceXY; | ||
| 243 | my $cnt = 0; | ||
| 244 | for my $tr (@traces) { | ||
| 245 | $cnt++; | ||
| 246 | my $y = $all_traces ( :, $cnt ); | ||
| 247 | my $data = $x; | ||
| 248 | $data = $data->glue( 1, $y ); | ||
| 249 | $self->display_trace_data( %args, trace => $tr, traceXY => $data ); | ||
| 250 | } | ||
| 251 | return $all_traces; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub trace_num_to_name { | ||
| 256 | my ( $self, %args ) = @_; | ||
| 257 | my $trace = delete $args{trace}; | ||
| 258 | my $name = "trace" . $trace; | ||
| 259 | return $name; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub log_traces { | ||
| 263 | my ( $self, %args ) = @_; | ||
| 264 | my $all_traces = $self->get_traces_data(%args); | ||
| 265 | my @traces = @{ delete $args{traces} }; # arrays are tricky | ||
| 266 | my @columns = $self->get_NameX(%args); | ||
| 267 | push @columns, | ||
| 268 | ( map { $self->trace_num_to_name( trace => $_ ) } @traces ); | ||
| 269 | |||
| 270 | if ( not $self->has_datafolder() ) { | ||
| 271 | $self->datafolder( datafolder() ); | ||
| 272 | } | ||
| 273 | my $datafolder = $self->datafolder(); | ||
| 274 | |||
| 275 | if ( not $self->has_filename() ) { | ||
| 276 | $self->filename('data.dat'); | ||
| 277 | } | ||
| 278 | my $filename = $self->filename(); | ||
| 279 | |||
| 280 | if ( not $self->has_datafile() ) { | ||
| 281 | $self->datafile( | ||
| 282 | datafile( | ||
| 283 | type => 'Gnuplot', | ||
| 284 | folder => $datafolder, | ||
| 285 | filename => $filename, | ||
| 286 | columns => [@columns] | ||
| 287 | ) | ||
| 288 | ); | ||
| 289 | } | ||
| 290 | my $datafile = $self->datafile(); | ||
| 291 | |||
| 292 | my $header = $self->get_log_header(%args); | ||
| 293 | $datafile->log_comment( comment => $header ); | ||
| 294 | |||
| 295 | $datafile->log_block( | ||
| 296 | block => $all_traces, | ||
| 297 | ); | ||
| 298 | |||
| 299 | return $all_traces; | ||
| 300 | } | ||
| 301 | |||
| 302 | 1; | ||
| 303 | |||
| 304 | __END__ | ||
| 305 | |||
| 306 | =pod | ||
| 307 | |||
| 308 | =encoding UTF-8 | ||
| 309 | |||
| 310 | =head1 NAME | ||
| 311 | |||
| 312 | Lab::Moose::Instrument::DisplayXY - Display with y vs x traces Role for Lab::Moose::Instrument | ||
| 313 | |||
| 314 | =head1 VERSION | ||
| 315 | |||
| 316 | version 3.881 | ||
| 317 | |||
| 318 | =head1 DESCRIPTION | ||
| 319 | |||
| 320 | Basic commands to grab and display XY traces | ||
| 321 | |||
| 322 | =head1 NAME | ||
| 323 | |||
| 324 | Lab::Moose::Instrument::DisplayXY - Role of Generic XY display | ||
| 325 | |||
| 326 | =head1 METHODS | ||
| 327 | |||
| 328 | Driver assuming this role must implements the following high-level method: | ||
| 329 | |||
| 330 | =head2 C<get_traceXY> | ||
| 331 | |||
| 332 | $data = $sa->traceXY(timeout => 10, trace => 2); | ||
| 333 | |||
| 334 | Perform a single sweep and return the resulting spectrum as a 2D PDL: | ||
| 335 | |||
| 336 | [ | ||
| 337 | [x1, x2, x3, ..., xN], | ||
| 338 | [y1, y2, y3, ..., yN], | ||
| 339 | ] | ||
| 340 | |||
| 341 | I.e. the first dimension runs over the sweep points. | ||
| 342 | |||
| 343 | This method accepts a hash with the following options: | ||
| 344 | |||
| 345 | =over | ||
| 346 | |||
| 347 | =item B<timeout> | ||
| 348 | |||
| 349 | timeout for the sweep operation. If this is not given, use the connection's | ||
| 350 | default timeout. | ||
| 351 | |||
| 352 | =item B<trace> | ||
| 353 | |||
| 354 | number of the trace (1..3) and similar. | ||
| 355 | |||
| 356 | =back | ||
| 357 | |||
| 358 | =head2 get_traceX | ||
| 359 | |||
| 360 | $data = $sa->traceX(timeout => 10); | ||
| 361 | |||
| 362 | Return X points of a trace in a 1D PDL: | ||
| 363 | |||
| 364 | =head2 get_traceY | ||
| 365 | |||
| 366 | $data = $inst->get_traceY(timeout => 1, trace => 2, precision => 'single'); | ||
| 367 | |||
| 368 | Return Y points of a given trace in a 1D PDL: | ||
| 369 | |||
| 370 | =over | ||
| 371 | |||
| 372 | =item B<timeout> | ||
| 373 | |||
| 374 | timeout for the sweep operation. If this is not given, use the connection's | ||
| 375 | default timeout. | ||
| 376 | |||
| 377 | =item B<trace> | ||
| 378 | |||
| 379 | number of the trace 1, 2, 3 and so on. Defaults to 1. | ||
| 380 | It is hardware depended and validated by C<validate_trace_papam>, | ||
| 381 | which need to be implemented by a specific instrument driver. | ||
| 382 | |||
| 383 | =item B<precision> | ||
| 384 | |||
| 385 | floating point type. Has to be 'single' or 'double'. Defaults to 'single'. | ||
| 386 | |||
| 387 | =back | ||
| 388 | |||
| 389 | =head2 get_traces_data | ||
| 390 | |||
| 391 | $inst->get_traces_data(timeout => 1, traces => [1, 2, 3], precision => 'single'); | ||
| 392 | |||
| 393 | Return the traces data as a 2D PDL in order of appearance in C<traces> array. | ||
| 394 | |||
| 395 | [ | ||
| 396 | [x_1, x_2, x_3, ..., x_N], | ||
| 397 | [y1_1, y1_2, y1_3, ..., y1_N], | ||
| 398 | [y2_1, y2_2, y2_3, ..., y2_N], | ||
| 399 | [y3_1, y3_2, y3_3, ..., y3_N], | ||
| 400 | ............................. | ||
| 401 | ] | ||
| 402 | |||
| 403 | =head2 display_trace_data | ||
| 404 | |||
| 405 | $inst->display_trace_data(timeout => 1, trace => 2, traceXY => $data, precision => 'single'); | ||
| 406 | |||
| 407 | Display a given trace data. C<traceXY> is 2D pdl with trace X and Y values | ||
| 408 | |||
| 409 | [ | ||
| 410 | [x_1, x_2, x_3, ..., x_N], | ||
| 411 | [y_1, y_2, y_3, ..., y_N], | ||
| 412 | ] | ||
| 413 | |||
| 414 | =head2 display_trace | ||
| 415 | |||
| 416 | $inst->display_trace(timeout => 1, trace => 2, precision => 'single'); | ||
| 417 | |||
| 418 | Displays a single trace data on a computer screen. It adds a new trace to the plot. | ||
| 419 | |||
| 420 | The function internally calls C<display_traces>, so it is equivalent to | ||
| 421 | |||
| 422 | $inst->display_traces(timeout => 1, traces => [ 2 ], precision => 'single'); | ||
| 423 | |||
| 424 | Return the trace data as a 2D PDL in order of appearance in C<traces> array. | ||
| 425 | |||
| 426 | [ | ||
| 427 | [x_1, x_2, x_3, ..., x_N], | ||
| 428 | [y_1, y_2, y_3, ..., y_N] | ||
| 429 | ] | ||
| 430 | |||
| 431 | =head2 display_traces | ||
| 432 | |||
| 433 | $inst->display_traces(timeout => 1, traces => [1, 2, 3], precision => 'single'); | ||
| 434 | |||
| 435 | Displays required traces data on a computer screen. It adds plots if called several times. | ||
| 436 | |||
| 437 | Return the traces data as a 2D PDL in order of appearance in C<traces> array. | ||
| 438 | |||
| 439 | [ | ||
| 440 | [x_1, x_2, x_3, ..., x_N], | ||
| 441 | [y1_1, y1_2, y1_3, ..., y1_N], | ||
| 442 | [y2_1, y2_2, y2_3, ..., y2_N], | ||
| 443 | [y3_1, y3_2, y3_3, ..., y3_N], | ||
| 444 | ............................. | ||
| 445 | ] | ||
| 446 | |||
| 447 | =head2 get_StartX and get_StopX | ||
| 448 | |||
| 449 | Returns start and stop values of X. | ||
| 450 | |||
| 451 | =head2 get_Xpoints_number | ||
| 452 | |||
| 453 | Returns number of points in the trace. | ||
| 454 | |||
| 455 | =head1 COPYRIGHT AND LICENSE | ||
| 456 | |||
| 457 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 458 | |||
| 459 | Copyright 2018 Eugeniy E. Mikhailov, Simon Reinhardt | ||
| 460 | 2020 Andreas K. Huettel | ||
| 461 | |||
| 462 | |||
| 463 | This is free software; you can redistribute it and/or modify it under | ||
| 464 | the same terms as the Perl 5 programming language system itself. | ||
| 465 | |||
| 466 | =cut |