Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions t/04-plot_fit.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use strict;
use warnings;

use Test::More;

use File::Temp qw(tempfile);
use FindBin;
use PDL;
use PDL::NiceSlice;
use Physics::Ellipsometry::VASE;

my $vase = Physics::Ellipsometry::VASE->new(layers => 1);
$vase->load_data("$FindBin::Bin/data/sample.dat");

my $model = sub {
my ($params, $x) = @_;

my $a = $params->(0);
my $b = $params->(1);
my $c = $params->(2);
my $d = $params->(3);

my $wavelength = $x->(:,0);

my $psi = $a - $b * $wavelength;
my $delta = $c + $d * $wavelength;

return cat($psi, $delta)->flat;
};

ok $vase->set_model($model), 'set_model works';

my $initial_params = pdl [65, 0.05, 80, 0.1];
my $fit_params = $vase->fit($initial_params);

is_deeply [list $fit_params], [65.0, 0.05, 80.0, 0.10], 'fit parameters correct';

subtest 'find MSE' => sub {
my $mse = $vase->mse($fit_params, nparams => 4);
ok $mse < 0.000001, 'Mean Squared Error very small';
is $mse, 0, 'Mean Squared Error is zero';
Comment on lines +40 to +41
ok $vase->{iters} > 0, 'Iterations positive';
Comment on lines +40 to +42
};

my ($fh1, $test_plot_png) = tempfile(SUFFIX => '.png', UNLINK => 1);
my ($fh2, $test_plot_pdf) = tempfile(SUFFIX => '.pdf', UNLINK => 1);

subtest 'Save plot to PNG' => sub {
ok $vase->plot($fit_params, output => $test_plot_png),
'can plot a png image';
ok -e $test_plot_png, "test image $test_plot_png exists";
ok -s $test_plot_png, "test image $test_plot_png not empty";
Comment on lines +45 to +52
};
close $fh1;

subtest 'Save plot to PDF' => sub {
ok $vase->plot($fit_params,
output => $test_plot_pdf,
title => 'Linear Model Fit'),
'can plot a pdf file';
ok -e $test_plot_pdf, "test image $test_plot_pdf exists";
ok -s $test_plot_pdf, "test image $test_plot_pdf not empty";
};
Comment on lines +48 to +63
close $fh2;

done_testing();