Skip to content

Commit

Permalink
fix figure diagnostics so they actually save and show on report
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccamccabe committed Dec 18, 2024
1 parent ff0a9e3 commit fe0b2c8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 38 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ jobs:
MDOcean/test-results/**/testreport.pdf
MDOcean/code-coverage/**/coverage.xml
MDOcean/**/DynamicValidation_*.pdf
MDOcean/test-results/Figure_*.pdf
94 changes: 78 additions & 16 deletions mdocean/plots/all_figures.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
% Generate all figures used in the paper
function [success_criterion,num_figs,num_tabs,fig_names,tab_names] = all_figures( which_figs, which_tabs, filename_uuid )
function [success_criterion,fig_output,tab_output,...
num_figs,num_tabs,fig_names,tab_names] = all_figures( which_figs, which_tabs, filename_uuid )

if nargin<3
filename_uuid = ''; % required argument for anything running gradient_optim in parallel,
Expand All @@ -18,23 +19,52 @@
which_tabs = 1:num_tabs;
end

fig_output = gobjects(1, length(which_figs));
tab_output(1, 1:length(which_tabs)) = {table()};

%% Define mapping from figures/tables to scripts
% figure_mapping = zeros(1,num_figs);
% figure_mapping(3:4) = 1; % sin desc function demo
% figure_mapping(5) = 2; % JPD
% figure_mapping(6:7) = 3; % pareto front
% figure_mapping(8) = 4; % parameter sensitivities
% figure_mapping(9:10) = 5; % overlaid comparison
%
% table_mapping = zeros(1,num_tabs);
% table_mapping(1:2) = 6; % var bounds
% table_mapping(3) = 7; % parameters
% table_mapping(4) = 8; % validation
% table_mapping(5) = 5; % 3 design comparison
% table_mapping(6) = 9; % location sensitivity
% table_mapping(7) = 10; % convergence

%% figure 1 - RM3 image
fig_names{1} = 'Fig. 1: RM3 image';
if any(which_figs == 1)
% Created in powerpoint
fig1 = figure;
imshow(imread("geometry.png"),'Parent',axes(fig1));
fig_output(which_figs==1) = fig1;
end

%% figure 2 - N2 diagram
fig_names{2} = 'Fig. 2: N2 diagram';
if any(which_figs == 2)
% Created in powerpoint
fig2 = figure;
imshow(imread("n2final2.png"),'Parent',axes(fig2));
fig_output(which_figs==2) = fig2;
end

%% figure 3, 4 - saturation time signal, saturation alpha
fig_names{3} = 'Fig. 3: saturation time signal';
fig_names{4} = 'Fig. 4: saturation alpha';
if any(which_figs == 3 | which_figs == 4)
sin_desc_fcn_demo()
fig4 = gcf;
fig3 = figure(fig4.Number-1);
fig_output(which_figs==3) = fig3;
fig_output(which_figs==4) = fig4;
end

%% figure 5 - JPD multiplication
Expand All @@ -44,6 +74,8 @@
b = var_bounds();
X = [b.X_noms; 1];
plot_power_matrix(X,p)
fig5 = gcf;
fig_output(which_figs==5) = fig5;
end


Expand All @@ -53,73 +85,103 @@
if any(which_figs == 6 | which_figs == 7)
pareto_search(filename_uuid);
pareto_curve_heuristics()
fig7 = gcf;
fig6 = figure(fig7.Number - 1);
fig_output(which_figs==6) = fig6;
fig_output(which_figs==5) = fig5;
end

%% figure 8 - parameter sensitivities
fig_names{8} = 'Fig. 8: parameter sensitivities';
if any(which_figs == 8)
param_sweep(filename_uuid)
fig8 = gcf;
fig_output(which_figs==8) = fig8;
end

%% figure 9, 10 - overlaid geometry, probability CDF
fig_names{9} = 'Fig. 9: overlaid geometry';
fig_names{10} = 'Fig. 10: probability CDF';
if any(which_figs == 9 | which_figs == 10 | which_tabs == 5)
tab_5 = compare(filename_uuid);
tab5 = compare(filename_uuid);
n = gcf().Number;
fig10 = figure(n-1);
fig9 = figure(n-2);
fig_output(which_figs==9) = fig9;
fig_output(which_figs==10) = fig10;
end

%% table 1 - design variables table
tab_names{1} = 'Tab. 1: design variables';
if any(which_tabs == 1)
b = var_bounds();
DV_table = array2table([b.X_mins b.X_noms b.X_maxs], ...
tab1 = array2table([b.X_mins b.X_noms b.X_maxs], ...
'VariableNames',{'Mins','Noms','Maxs'}, 'RowNames', b.var_names(1:end-1));
display(DV_table)
display(tab1)
tab_output{which_tabs==1} = tab1;
end

%% table 2 - constraints table
tab_names{2} = 'Tab. 2: constraints';
if any(which_tabs == 2)
b = var_bounds();
display(b.constraint_names')
tab2 = b.constraint_names';
display(tab2)
tab_output{which_tabs==2} = tab2;
end

%% table 3 - parameters table
tab_names{3} = 'Tab. 3: parameters';
if any(which_tabs == 3)
[~,tab] = parameters();
display(tab)
[~,tab3] = parameters();
display(tab3)
tab_output{which_tabs==3} = tab3;
end

%% table 4 - validation table
tab_names{4} = 'Tab. 4: validation against nominal';
if any(which_tabs == 4)
[~,~,~,~,tab] = validate_nominal_RM3('report');
display(tab)
[~,~,~,~,tab2] = validate_nominal_RM3('wecsim');
display(tab2)
[~,~,~,~,tab4a] = validate_nominal_RM3('report');
display(tab4a)
[~,~,~,~,tab4b] = validate_nominal_RM3('wecsim');
display(tab4b)

% merge table 4a and 4b while preserving row order
sharedCols = intersect(tab4a.Properties.VariableNames, tab4b.Properties.VariableNames);
tab4a.RowNum = (1:length(tab4a.Properties.RowNames))';
tab4b.RowNum = length(tab4a.Properties.RowNames) + (1:length(tab4b.Properties.RowNames))';

tab4 = outerjoin(tab4a, tab4b, 'Keys', [{'RowNum'},sharedCols], 'MergeKeys', true);
tab4 = removevars(tab4,'RowNum');
tab4.Properties.RowNames = [tab4a.Properties.RowNames; tab4b.Properties.RowNames];
display(tab4)

tab_output{which_tabs==4} = tab4;
end

%% table 5 - optimal DVs for 4 designs
tab_names{5} = 'Tab. 5: optimal DVs for 4 designs';
if any(which_tabs == 5)
% computation above with figures 9-10
display(tab_5);
display(tab5);
tab_output{which_tabs==5} = tab5;
end

%% table 6 - optimal DVs for 4 locations
tab_names{6} = 'Tab. 6: optimal DVs for 4 locations';
if any(which_tabs == 6)
tab = location_sensitivity(filename_uuid);
display(tab);
location_flags = tab(strcmp(tab.Row,'flag'),:).Variables;
tab6 = location_sensitivity(filename_uuid);
display(tab6);
tab_output{which_tabs==6} = tab6;
location_flags = tab6(strcmp(tab6.Row,'flag'),:).Variables;
success_criterion(end+1) = {location_flags};
end

%% paragraph 4.2 - convergence for different x0s
tab_names{7} = 'Tab. 7: convergence for different x0s';
if any(which_tabs == 7)
gradient_mult_x0(filename_uuid)
tab7 = gradient_mult_x0(filename_uuid);
tab_output{which_tabs==7} = tab7;
end

end
4 changes: 2 additions & 2 deletions tests/run_tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

p1 = CodeCoveragePlugin.forFile(codeFilePaths, 'Producing', reportFormat);
p2 = XMLPlugin.producingJUnitFormat([test_dir '/junit.xml']);
p3 = TestReportPlugin.producingPDF([test_dir '/testreport.pdf']);
p4 = DiagnosticsRecordingPlugin();
p3 = TestReportPlugin.producingPDF([test_dir '/testreport.pdf'],'IncludingPassingDiagnostics',true);
p4 = DiagnosticsRecordingPlugin('IncludingPassingDiagnostics',true);

runner.addPlugin(p1);
runner.addPlugin(p2);
Expand Down
59 changes: 39 additions & 20 deletions tests/test.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
% class based unit tests, as in https://www.mathworks.com/help/matlab/matlab_prog/class-based-unit-tests.html

properties (Constant)
run_slow_tests = true;
run_slow_tests = false;
slow_figs = [6 7 8];
slow_tabs = 7;
end
Expand All @@ -14,11 +14,13 @@
failed_report
simulated_report
actual_report
econ_fig_report

feasible_wecsim
failed_wecsim
simulated_wecsim
actual_wecsim
econ_fig_wecsim

uuid
end
Expand All @@ -36,7 +38,7 @@
% helper methods to enumerate all figures and tables
methods (Static)
function which_fig_struct = enumerateFigs()
[~,num_figs,num_tabs,fig_names,~] = all_figures( [],[] );
[~,~,~,num_figs,num_tabs,fig_names,~] = all_figures( [],[] );

if ~test.run_slow_tests
num_tabs = num_tabs - length(test.slow_tabs);
Expand All @@ -56,7 +58,7 @@
which_fig_struct = cell2struct(which_figs_cell,fig_names,2);
end
function which_tab_struct = enumerateTabs()
[~,num_figs,num_tabs,~,tab_names] = all_figures( [],[] );
[~,~,~,num_figs,num_tabs,~,tab_names] = all_figures( [],[] );

if ~test.run_slow_tests
num_figs = num_figs - length(test.slow_figs);
Expand All @@ -83,18 +85,20 @@
function runNominalValidation(testCase)
% this is a shared setup because the results are used by both
% validateNominal and validateNominalFeasible
[feas_r, fail_r, sim_r, act_r] = validate_nominal_RM3('report'); % report
[feas_w, fail_w, sim_w, act_w] = validate_nominal_RM3('wecsim'); % wecsim
[feas_r, fail_r, sim_r, act_r, ~, fig_r] = validate_nominal_RM3('report'); % report
[feas_w, fail_w, sim_w, act_w, ~, fig_w] = validate_nominal_RM3('wecsim'); % wecsim

testCase.feasible_report = feas_r;
testCase.failed_report = fail_r;
testCase.simulated_report = sim_r;
testCase.actual_report = act_r;
testCase.econ_fig_report = fig_r;

testCase.feasible_wecsim = feas_w;
testCase.failed_wecsim = fail_w;
testCase.simulated_wecsim = sim_w;
testCase.actual_wecsim = act_w;
testCase.econ_fig_wecsim = fig_w;
end

function generateUUID(testCase)
Expand Down Expand Up @@ -128,35 +132,50 @@ function deleteGeneratedFiles(testCase)

% run every figure and log it
function allFiguresRun(testCase, which_figs, which_tabs)
success_criterion = all_figures(which_figs,which_tabs,testCase.uuid.Value);
if ~isempty(success_criterion)
for i=1:length(success_criterion)
testCase.verifyGreaterThan(success_criterion{i},0);
end
[success_criterion,fig_out,tab_out] = all_figures(which_figs,which_tabs,testCase.uuid.Value);

if isempty(success_criterion)
success_criterion = 1;
else
success_criterion = success_criterion{:};
end

if which_figs ~= 0
fig_name = ['LoggedFigure_' num2str(which_figs)];
fig = gcf;
if which_figs ~= 0 % figure
fig_name = ['Figure_' num2str(which_figs) '_'];

set(fig,'Units','Inches');
pos = get(fig,'Position');
set(fig,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(fig,['../test-results/' fig_name],'-dpdf','-r0')
testCase.log(matlab.unittest.diagnostics.FigureDiagnostic(fig,'Formats','png','Prefix',fig_name))
set(fig_out,'Units','Inches');
pos = get(fig_out,'Position');
set(fig_out,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(fig_out,['../test-results/' fig_name],'-dpdf','-r0')
diagnostic = matlab.unittest.diagnostics.FigureDiagnostic(fig_out,'Prefix',fig_name);
else % table
diagnostic = matlab.unittest.diagnostics.DisplayDiagnostic(tab_out{:});
end

testCase.verifyGreaterThan(success_criterion, 0, diagnostic);

end

function validateNominalReport(testCase, field_report, rel_tol_report)
sim = testCase.simulated_report.(field_report);
act = testCase.actual_report.(field_report);
testCase.verifyEqual(sim, act, 'RelTol',rel_tol_report)
if strcmp(field_report,'LCOE')
diagnostic = matlab.unittest.diagnostics.FigureDiagnostic(testCase.econ_fig_report,'Prefix','econ_validation_report');
else
diagnostic = '';
end
testCase.verifyEqual(sim, act, 'RelTol',rel_tol_report,diagnostic)
end

function validateNominalWecsim(testCase, field_wecsim, rel_tol_wecsim)
sim = testCase.simulated_wecsim.(field_wecsim);
act = testCase.actual_wecsim.(field_wecsim);
testCase.verifyEqual(sim, act, 'RelTol',rel_tol_wecsim)
if strcmp(field_wecsim,'LCOE')
diagnostic = matlab.unittest.diagnostics.FigureDiagnostic(testCase.econ_fig_wecsim,'Prefix','econ_validation_wecsim');
else
diagnostic = '';
end
testCase.verifyEqual(sim, act, 'RelTol',rel_tol_wecsim,diagnostic)
end

end
Expand Down

0 comments on commit fe0b2c8

Please sign in to comment.