Skip to content

Commit

Permalink
Adds code to convert backgrounds to background parameters (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPaulSharp authored Jun 17, 2024
1 parent 2257620 commit ce5a9e6
Show file tree
Hide file tree
Showing 29 changed files with 57 additions and 27 deletions.
8 changes: 4 additions & 4 deletions API/checkIndices.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ function checkIndices(problemStruct)
% corresponding array.

numBackgroundParams = length(problemStruct.backgroundParams);
for i = 1:length(problemStruct.contrastBackgrounds)
index = problemStruct.contrastBackgrounds(i);
if index < 1 || index > numBackgroundParams
throw(exceptions.indexOutOfRange(sprintf('contrastBackgrounds(%i) is %i, which is outside the range of backgroundParams', i, index)));
for i = 1:length(problemStruct.contrastBackgroundParams)
index = problemStruct.contrastBackgroundParams(i);
if (index < 1 && index ~= -1) || index > numBackgroundParams
throw(exceptions.indexOutOfRange(sprintf('contrastBackgroundParams(%i) is %i, which is outside the range of backgroundParams', i, index)));
end
end

Expand Down
32 changes: 29 additions & 3 deletions API/parseClassToStructs.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,33 @@

end

% Convert contrastBackgrounds to parameter indices
contrastBackgrounds = inputStruct.contrastBackgrounds;
backgroundTypes = inputStruct.backgroundTypes;

backgroundParamNames = inputStruct.backgroundParamNames;
contrastBackgroundParams = zeros(1, length(contrastBackgrounds));

for i = 1:length(contrastBackgrounds)
% Check the type of the background that each contrast is pointing to.
% If it is a constant, point to the number of the corresponding
% background param. If it's data, then set it to -1
thisBack = contrastBackgrounds(i); % Which background
thisType = backgroundTypes{thisBack}; % What type is it?

if strcmpi(thisType,'data')
% Background is in the datafile. Set contrastBackgroundParams to -1
contrastBackgroundParams(i) = -1;
else
% Background is a backgroundParam, the name of which should
% be in the first column of backgroundValues
whichBackgroundParamName = inputStruct.backgroundValues{thisBack,1};

% Find which backgroundParam this is, and set contrastBackgroundParams to this number
contrastBackgroundParams(i) = find(strcmpi(whichBackgroundParamName,backgroundParamNames));
end
end

% Here we need to do the same with the contrastResolutions array
contrastResolutions = inputStruct.contrastResolutions;
resolutionTypes = inputStruct.resolutionTypes;
Expand All @@ -260,8 +287,7 @@
whichResolutionParamName = inputStruct.resolutionValues{thisResol,1};

% Find which resolutionParam this is, and set contrastResolutionParams to this number
resolutionParamNumber = find(strcmpi(whichResolutionParamName,resolutionParamNames));
contrastResolutionParams(i) = resolutionParamNumber;
contrastResolutionParams(i) = find(strcmpi(whichResolutionParamName,resolutionParamNames));
end
end

Expand Down Expand Up @@ -320,7 +346,7 @@
problemStruct.numberOfContrasts = inputStruct.numberOfContrasts;
problemStruct.geometry = inputStruct.geometry;
problemStruct.useImaginary = inputStruct.useImaginary;
problemStruct.contrastBackgrounds = inputStruct.contrastBackgrounds;
problemStruct.contrastBackgroundParams = contrastBackgroundParams;
problemStruct.contrastBackgroundActions = backgroundActions;
problemStruct.contrastQzshifts = inputStruct.contrastQzshifts;
problemStruct.contrastScalefactors = inputStruct.contrastScalefactors;
Expand Down
2 changes: 1 addition & 1 deletion compile/fullCompile/makeCompileArgsFull.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ARGS_1_1.numberOfContrasts = coder.typeof(0);
ARGS_1_1.geometry = coder.typeof('X',[1 maxArraySize],[0 1]);
ARGS_1_1.useImaginary = coder.typeof(true,[1 1],[0 0]);
ARGS_1_1.contrastBackgrounds = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBackgroundParams = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBackgroundActions = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastQzshifts = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastScalefactors = coder.typeof(0,[1 maxArraySize],[0 1]);
Expand Down
2 changes: 1 addition & 1 deletion compile/reflectivityCalculation/makeCompileArgs.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ARGS_1_1.numberOfContrasts = coder.typeof(0);
ARGS_1_1.geometry = coder.typeof('X',[1 maxArraySize],[0 1]);
ARGS_1_1.useImaginary = coder.typeof(true,[1 1],[0 0]);
ARGS_1_1.contrastBackgrounds = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBackgroundParams = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBackgroundActions = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastQzshifts = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastScalefactors = coder.typeof(0,[1 maxArraySize],[0 1]);
Expand Down
8 changes: 4 additions & 4 deletions examples/domains/standardLayers/domainsStandardLayersScript.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
'bulk out' };

Layer3 = {'Layer 3',... % Name of the layer
'L2 thick',... % Layer thickness
'L2 SLD',... % Layer SLD
'L2 Rough',... % Layer roughness
'L2 Hydr',... % hydration (precent)
'L3 thick',... % Layer thickness
'L3 SLD',... % Layer SLD
'L3 Rough',... % Layer roughness
'L3 Hydr',... % hydration (precent)
'bulk out' };

problem.addLayerGroup({Layer1, Layer2, Layer3});
Expand Down
12 changes: 8 additions & 4 deletions targetFunctions/common/backSort.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function [outBackgroundParam,outQzshift,outScalefactor,outBulkIn,outBulkOut,outResolutionParam] = backSort(contrastBackgrounds,contrastQzshifts,contrastScalefactors,contrastBulkIns,contrastBulkOuts,contrastResolutionParams,backgroundParams,qzshifts,scalefactors,bulkIn,bulkOut,resolutionParams)
function [outBackgroundParam,outQzshift,outScalefactor,outBulkIn,outBulkOut,outResolutionParam] = backSort(contrastBackgroundParams,contrastQzshifts,contrastScalefactors,contrastBulkIns,contrastBulkOuts,contrastResolutionParams,backgroundParams,qzshifts,scalefactors,bulkIn,bulkOut,resolutionParams)
% Distributes the background and shift values among the different contrasts
%
% USAGE::
%
% [backgroundParams,qzshift,scalefactor,bulkIn,bulkOut,resolutionParams] = backsort(contrastBackgrounds,contrastQzshifts,contrastScalefactors,contrastBulkIns,contrastBulkOuts,contrastResolutions,backs,qzshifts,scalefactor,bulkIn,bulkOut,res)
%
% INPUTS:
% * contrastBackgrounds: Which background value is associated with each contrast
% * contrastBackgroundParams: Which background value is associated with each contrast
% * contrastQzshifts: Which qz_shift value is associated with each contrast
% * contrastScalefactors: Which scalefactor value is associated with each contrast
% * contrastBulkIns: Which BulkIn value is associated with each contrast
Expand All @@ -27,8 +27,12 @@
% * outBulkOut: list of actual bulkOut values for each contrast
% * outResolution: list of actual resolution parameter for each contrast

outBackgroundParam = backgroundParams(contrastBackgrounds);

if contrastBackgroundParams ~= -1
outBackgroundParam = backgroundParams(contrastBackgroundParams);
else
outBackgroundParam = -1; % Negative value means we have a data background.
end

outQzshift = qzshifts(contrastQzshifts);

outScalefactor = scalefactors(contrastScalefactors);
Expand Down
4 changes: 2 additions & 2 deletions targetFunctions/common/extractProblemParams.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function [numberOfContrasts, geometry, contrastBackgrounds, contrastQzshifts, contrastScalefactors, contrastBulkIns, contrastBulkOuts,...
function [numberOfContrasts, geometry, contrastBackgroundParams, contrastQzshifts, contrastScalefactors, contrastBulkIns, contrastBulkOuts,...
contrastResolutionParams, contrastDomainRatios, backgroundParams, qzshifts, scalefactors, bulkIn, bulkOut, resolutionParams, domainRatio,...
dataPresent, nParams, params, numberOfLayers, resample, contrastBackgroundActions, contrastCustomFiles, useImaginary] = extractProblemParams(problemStruct)

%Extract individual parameters from problem

numberOfContrasts = problemStruct.numberOfContrasts;
geometry = problemStruct.geometry;
contrastBackgrounds = problemStruct.contrastBackgrounds;
contrastBackgroundParams = problemStruct.contrastBackgroundParams;
contrastBackgroundActions = problemStruct.contrastBackgroundActions;
contrastQzshifts = problemStruct.contrastQzshifts;
contrastScalefactors = problemStruct.contrastScalefactors;
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/domainsTFReflectivityCalculation/domainsCustomXYInputs.mat
Binary file not shown.
Binary file not shown.
Binary file modified tests/domainsTFReflectivityCalculation/domainsCustomXYTFParams.mat
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ function testCheckIndices(testCase)

% Test Background Param Error
testInput = testCase.problemStruct;
testInput.contrastBackgrounds(1) = 0;
testInput.contrastBackgroundParams(1) = 0;
testCase.verifyError(@() checkIndices(testInput), exceptions.indexOutOfRange.errorID);
testInput = testCase.problemStruct;
testInput.contrastBackgrounds(1) = 4;
testInput.contrastBackgroundParams(1) = 4;
testCase.verifyError(@() checkIndices(testInput), exceptions.indexOutOfRange.errorID);

% Test Qzshift Error
Expand Down Expand Up @@ -296,13 +296,13 @@ function testParseCells(testCase)
end

function testExtractProblemParams(testCase)
[numberOfContrasts, geometry, contrastBackgrounds, contrastQzshifts, contrastScalefactors, contrastBulkIns, contrastBulkOuts,...
[numberOfContrasts, geometry, contrastBackgroundParams, contrastQzshifts, contrastScalefactors, contrastBulkIns, contrastBulkOuts,...
contrastResolutionParams, contrastDomainRatios, backgroundParams, qzshifts, scalefactors, bulkIn, bulkOut, resolutionParams, domainRatio,...
dataPresent, nParams, params, numberOfLayers, resample, backgroundParamsType, contrastCustomFiles, useImaginary] = extractProblemParams(testCase.problemStruct);

testCase.verifyEqual(numberOfContrasts, testCase.problemStruct.numberOfContrasts, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(geometry, testCase.problemStruct.geometry, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastBackgrounds, testCase.problemStruct.contrastBackgrounds, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastBackgroundParams, testCase.problemStruct.contrastBackgroundParams, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastQzshifts, testCase.problemStruct.contrastQzshifts, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastScalefactors, testCase.problemStruct.contrastScalefactors, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastBulkIns, testCase.problemStruct.contrastBulkIns, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/nonPolarisedTFReflectivityCalculation/customXYInputs.mat
Binary file not shown.
Binary file modified tests/nonPolarisedTFReflectivityCalculation/customXYOutputs.mat
Binary file not shown.
Binary file modified tests/nonPolarisedTFReflectivityCalculation/customXYTFParams.mat
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ function testCheckIndices(testCase)

% Test Background Param Error
testInput = testCase.problemStruct;
testInput.contrastBackgrounds(1) = 0;
testInput.contrastBackgroundParams(1) = 0;
testCase.verifyError(@() checkIndices(testInput), exceptions.indexOutOfRange.errorID);
testInput = testCase.problemStruct;
testInput.contrastBackgrounds(1) = 4;
testInput.contrastBackgroundParams(1) = 4;
testCase.verifyError(@() checkIndices(testInput), exceptions.indexOutOfRange.errorID);

% Test Qzshift Error
Expand Down Expand Up @@ -296,13 +296,13 @@ function testParseCells(testCase)
end

function testExtractProblemParams(testCase)
[numberOfContrasts, geometry, contrastBackgrounds, contrastQzshifts, contrastScalefactors, contrastBulkIns, contrastBulkOuts,...
[numberOfContrasts, geometry, contrastBackgroundParams, contrastQzshifts, contrastScalefactors, contrastBulkIns, contrastBulkOuts,...
contrastResolutionParams, contrastDomainRatios, backgroundParams, qzshifts, scalefactors, bulkIn, bulkOut, resolutionParams, domainRatio,...
dataPresent, nParams, params, numberOfLayers, resample, backgroundParamsType, contrastCustomFiles, useImaginary] = extractProblemParams(testCase.problemStruct);

testCase.verifyEqual(numberOfContrasts, testCase.problemStruct.numberOfContrasts, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(geometry, testCase.problemStruct.geometry, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastBackgrounds, testCase.problemStruct.contrastBackgrounds, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastBackgroundParams, testCase.problemStruct.contrastBackgroundParams, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastQzshifts, testCase.problemStruct.contrastQzshifts, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastScalefactors, testCase.problemStruct.contrastScalefactors, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
testCase.verifyEqual(contrastBulkIns, testCase.problemStruct.contrastBulkIns, 'RelTol', testCase.tolerance, 'AbsTol', testCase.absTolerance);
Expand Down
Binary file modified tests/testProjectConversion/DSPCBilayerProjectClass.mat
Binary file not shown.
Binary file modified tests/testProjectConversion/monolayerVolumeModelProjectClass.mat
Binary file not shown.

0 comments on commit ce5a9e6

Please sign in to comment.