From 93f5f1f68c41f01787fa0cf6f9084b9290c0a067 Mon Sep 17 00:00:00 2001 From: Boyd Duffee Date: Tue, 12 May 2026 15:15:58 +0100 Subject: [PATCH 1/2] Added test file for mse and plot methods --- t/04-plot_fit.t | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 t/04-plot_fit.t diff --git a/t/04-plot_fit.t b/t/04-plot_fit.t new file mode 100644 index 0000000..54cb7e2 --- /dev/null +++ b/t/04-plot_fit.t @@ -0,0 +1,66 @@ +use strict; +use warnings; + +use Test::More; + +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'; + ok $vase->{iters} > 0, 'Iterations positive'; +}; + +my $test_plot_png = 'test_plot_fit.png'; +my $test_plot_pdf = 'test_plot_fit.pdf'; + +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"; +}; + +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"; +}; + +done_testing(); + +# cleanup tmp files +END { unlink $test_plot_png, $test_plot_pdf; } From 7deedcac36328f399c5ae0925b86a1e7a0e26f43 Mon Sep 17 00:00:00 2001 From: Boyd Duffee Date: Sat, 23 May 2026 11:41:59 +0100 Subject: [PATCH 2/2] use tempfile() in place of hardcoded filenames --- t/04-plot_fit.t | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/t/04-plot_fit.t b/t/04-plot_fit.t index 54cb7e2..b8e7758 100644 --- a/t/04-plot_fit.t +++ b/t/04-plot_fit.t @@ -3,6 +3,7 @@ use warnings; use Test::More; +use File::Temp qw(tempfile); use FindBin; use PDL; use PDL::NiceSlice; @@ -41,8 +42,8 @@ subtest 'find MSE' => sub { ok $vase->{iters} > 0, 'Iterations positive'; }; -my $test_plot_png = 'test_plot_fit.png'; -my $test_plot_pdf = 'test_plot_fit.pdf'; +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), @@ -50,6 +51,7 @@ subtest 'Save plot to PNG' => sub { ok -e $test_plot_png, "test image $test_plot_png exists"; ok -s $test_plot_png, "test image $test_plot_png not empty"; }; +close $fh1; subtest 'Save plot to PDF' => sub { ok $vase->plot($fit_params, @@ -59,8 +61,6 @@ subtest 'Save plot to PDF' => sub { ok -e $test_plot_pdf, "test image $test_plot_pdf exists"; ok -s $test_plot_pdf, "test image $test_plot_pdf not empty"; }; +close $fh2; done_testing(); - -# cleanup tmp files -END { unlink $test_plot_png, $test_plot_pdf; }