Skip to content

Commit

Permalink
Adds code to ensure parameter array indices are valid (#236)
Browse files Browse the repository at this point in the history
* Refactors code to be more explicit about use of resolution parameters

* Adds new routine "checkIndices.m"

* Refactors "checkIndices" to be called from "parseClassToStruct"

* Updates errors in "checkIndices"

* Addresses review comments

* Fixes tests

* Restores defaults for "contrastDomainRatios"
  • Loading branch information
DrPaulSharp authored Jun 14, 2024
1 parent 02d87fb commit 2257620
Show file tree
Hide file tree
Showing 36 changed files with 241 additions and 55 deletions.
60 changes: 60 additions & 0 deletions API/checkIndices.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function checkIndices(problemStruct)
% Make sure that the indices provided lie within the bounds of the
% 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)));
end
end

numQzshifts = length(problemStruct.qzshifts);
for i = 1:length(problemStruct.contrastQzshifts)
index = problemStruct.contrastQzshifts(i);
if index < 1 || index > numQzshifts
throw(exceptions.indexOutOfRange(sprintf('contrastQzshifts(%i) is %i, which is outside the range of qzshifts', i, index)));
end
end

numScalefactors = length(problemStruct.scalefactors);
for i = 1:length(problemStruct.contrastScalefactors)
index = problemStruct.contrastScalefactors(i);
if index < 1 || index > numScalefactors
throw(exceptions.indexOutOfRange(sprintf('contrastScalefactors(%i) is %i, which is outside the range of scalefactors', i, index)));
end
end

numBulkIns = length(problemStruct.bulkIn);
for i = 1:length(problemStruct.contrastBulkIns)
index = problemStruct.contrastBulkIns(i);
if index < 1 || index > numBulkIns
throw(exceptions.indexOutOfRange(sprintf('contrastBulkIns(%i) is %i, which is outside the range of bulkIn', i, index)));
end
end

numBulkOuts = length(problemStruct.bulkOut);
for i = 1:length(problemStruct.contrastBulkOuts)
index = problemStruct.contrastBulkOuts(i);
if index < 1 || index > numBulkOuts
throw(exceptions.indexOutOfRange(sprintf('contrastBulkOuts(%i) is %i, which is outside the range of bulkOut', i, index)));
end
end

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

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

22 changes: 13 additions & 9 deletions API/parseClassToStructs.m
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,29 @@

% Here we need to do the same with the contrastResolutions array
contrastResolutions = inputStruct.contrastResolutions;
resolutionNames = inputStruct.resolutionParamNames;
resolutionTypes = inputStruct.resolutionTypes;
contrastRes = zeros(1, length(contrastResolutions));

resolutionParamNames = inputStruct.resolutionParamNames;
contrastResolutionParams = zeros(1, length(contrastResolutions));

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

if strcmpi(thisType,'data')
% Resolution is in the datafile. Set contrastRes to zero
contrastRes(i) = -1;
% Resolution is in the datafile. Set contrastResolutionParams to -1
contrastResolutionParams(i) = -1;
else
% Resolution is a resolutionParam, the name of which should
% be in the first column of resolutionValues
whichResolutionParamName = inputStruct.resolutionValues{thisResol,1};

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

Expand Down Expand Up @@ -324,7 +326,7 @@
problemStruct.contrastScalefactors = inputStruct.contrastScalefactors;
problemStruct.contrastBulkIns = inputStruct.contrastBulkIns;
problemStruct.contrastBulkOuts = inputStruct.contrastBulkOuts;
problemStruct.contrastResolutions = contrastRes;
problemStruct.contrastResolutionParams = contrastResolutionParams;
problemStruct.backgroundParams = inputStruct.backgroundParamValues; %inputStruct.backgrounds; % **** note backPar workaround (todo) ****
problemStruct.qzshifts = inputStruct.qzshiftValues;
problemStruct.scalefactors = inputStruct.scalefactorValues;
Expand Down Expand Up @@ -358,6 +360,8 @@
problemStruct.fitLimits = [];
problemStruct.otherLimits = [];

% Make sure the indices cannot lie outside of the arrays
checkIndices(problemStruct)

%% Now deal with the controls class
controls.procedure = inputControls.procedure;
Expand Down
2 changes: 2 additions & 0 deletions API/projectClass/contrastsClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@

if isfield(thisContrast, 'domainRatio')
contrastDomainRatios(i) = find(strcmpi(thisContrast.domainRatio,allowedNames.domainRatioNames));
else
contrastDomainRatios(i) = -1;
end

contrastBackgrounds(i) = find(strcmpi(thisContrast.background,allowedNames.backgroundNames));
Expand Down
2 changes: 1 addition & 1 deletion compile/fullCompile/makeCompileArgsFull.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
ARGS_1_1.contrastScalefactors = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBulkIns = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBulkOuts = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastResolutions = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastResolutionParams = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.backgroundParams = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.qzshifts = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.scalefactors = 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 @@ -22,7 +22,7 @@
ARGS_1_1.contrastScalefactors = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBulkIns = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastBulkOuts = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastResolutions = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.contrastResolutionParams = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.backgroundParams = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.qzshifts = coder.typeof(0,[1 maxArraySize],[0 1]);
ARGS_1_1.scalefactors = coder.typeof(0,[1 maxArraySize],[0 1]);
Expand Down
2 changes: 1 addition & 1 deletion examples/domains/customXY/domainsCustomXYMain.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
problem.addBulkOut('SLD H2O', -0.6e-6, -0.56e-6, -0.5e-6);

% Add the custom file...
problem.addCustomFile('Domain Layer','domainsXY','matlab',pwd);
problem.addCustomFile('Domain Layer','domainsXY.m','matlab',pwd);

% Make a contrast
problem.addContrast('name', 'D2O',...
Expand Down
8 changes: 4 additions & 4 deletions targetFunctions/+domainsTF/customLayers.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
% Extract individual parameters from problemStruct
[numberOfContrasts, geometry, contrastBackgroundIndices, contrastQzshiftIndices,...
contrastScalefactorIndices, contrastBulkInIndices, contrastBulkOutIndices,...
contrastResolutionIndices, contrastDomainRatioIndices, backgroundParamArray,...
contrastResolutionParamIndices, contrastDomainRatioIndices, backgroundParamArray,...
qzshiftArray, scalefactorArray, bulkInArray, bulkOutArray, resolutionParamArray,...
domainRatioArray, dataPresent, nParams, params, ~, resample,...
contrastBackgroundActions, cCustFiles, useImaginary] = extractProblemParams(problemStruct);

calcSld = controls.calcSldDuringFit;
parallel = controls.parallel;
resampleParams = controls.resampleParams;
Expand Down Expand Up @@ -107,7 +107,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),contrastDomainRatioIndices(i),...
contrastResolutionParamIndices(i),contrastDomainRatioIndices(i),...
backgroundParamArray,qzshiftArray,scalefactorArray,bulkInArray,...
bulkOutArray,resolutionParamArray,domainRatioArray,dataPresent(i),...
data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
Expand All @@ -128,7 +128,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),contrastDomainRatioIndices(i),...
contrastResolutionParamIndices(i),contrastDomainRatioIndices(i),...
backgroundParamArray,qzshiftArray,scalefactorArray,bulkInArray,...
bulkOutArray,resolutionParamArray,domainRatioArray,dataPresent(i),...
data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
Expand Down
6 changes: 3 additions & 3 deletions targetFunctions/+domainsTF/customXY.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
% Extract individual parameters from problemStruct
[numberOfContrasts, ~, contrastBackgroundIndices, contrastQzshiftIndices,...
contrastScalefactorIndices, contrastBulkInIndices, contrastBulkOutIndices,...
contrastResolutionIndices, contrastDomainRatioIndices, backgroundParamArray,...
contrastResolutionParamIndices, contrastDomainRatioIndices, backgroundParamArray,...
qzshiftArray, scalefactorArray, bulkInArray, bulkOutArray, resolutionParamArray,...
domainRatioArray, dataPresent, nParams, params, ~, ~, contrastBackgroundActions,...
cCustFiles, useImaginary] = extractProblemParams(problemStruct);
Expand Down Expand Up @@ -99,7 +99,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),contrastDomainRatioIndices(i),...
contrastResolutionParamIndices(i),contrastDomainRatioIndices(i),...
backgroundParamArray,qzshiftArray,scalefactorArray,bulkInArray,...
bulkOutArray,resolutionParamArray,domainRatioArray,dataPresent(i),...
data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
Expand All @@ -119,7 +119,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),contrastDomainRatioIndices(i),...
contrastResolutionParamIndices(i),contrastDomainRatioIndices(i),...
backgroundParamArray,qzshiftArray,scalefactorArray,bulkInArray,...
bulkOutArray,resolutionParamArray,domainRatioArray,dataPresent(i),...
data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
Expand Down
6 changes: 3 additions & 3 deletions targetFunctions/+domainsTF/standardLayers.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
% Extract individual parameters from problemStruct
[numberOfContrasts, geometry, contrastBackgroundIndices, contrastQzshiftIndices,...
contrastScalefactorIndices, contrastBulkInIndices, contrastBulkOutIndices,...
contrastResolutionIndices, contrastDomainRatioIndices, backgroundParamArray,...
contrastResolutionParamIndices, contrastDomainRatioIndices, backgroundParamArray,...
qzshiftArray, scalefactorArray, bulkInArray, bulkOutArray, resolutionParamArray,...
domainRatioArray, dataPresent, nParams, params, ~, resample,...
contrastBackgroundActions, ~, useImaginary] = extractProblemParams(problemStruct);
Expand Down Expand Up @@ -111,7 +111,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),contrastDomainRatioIndices(i),...
contrastResolutionParamIndices(i),contrastDomainRatioIndices(i),...
backgroundParamArray,qzshiftArray,scalefactorArray,bulkInArray,...
bulkOutArray,resolutionParamArray,domainRatioArray,dataPresent(i),...
data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
Expand All @@ -133,7 +133,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),contrastDomainRatioIndices(i),...
contrastResolutionParamIndices(i),contrastDomainRatioIndices(i),...
backgroundParamArray,qzshiftArray,scalefactorArray,bulkInArray,...
bulkOutArray,resolutionParamArray,domainRatioArray,dataPresent(i),...
data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
Expand Down
8 changes: 4 additions & 4 deletions targetFunctions/+nonPolarisedTF/customLayers.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
% Extract individual parameters from problemStruct
[numberOfContrasts, geometry, contrastBackgroundIndices, contrastQzshiftIndices,...
contrastScalefactorIndices, contrastBulkInIndices, contrastBulkOutIndices,...
contrastResolutionIndices, ~, backgroundParamArray, qzshiftArray,...
contrastResolutionParamIndices, ~, backgroundParamArray, qzshiftArray,...
scalefactorArray, bulkInArray, bulkOutArray, resolutionParamArray, ~,...
dataPresent, nParams, params, ~, resample, contrastBackgroundActions, cCustFiles,...
useImaginary] = extractProblemParams(problemStruct);

calcSld = controls.calcSldDuringFit;
parallel = controls.parallel;
resampleParams = controls.resampleParams;
Expand Down Expand Up @@ -72,7 +72,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),backgroundParamArray,qzshiftArray,...
contrastResolutionParamIndices(i),backgroundParamArray,qzshiftArray,...
scalefactorArray,bulkInArray,bulkOutArray,resolutionParamArray,...
dataPresent(i),data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
contrastBackgroundActions(i),nParams,parallel,resampleParams,...
Expand All @@ -93,7 +93,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),backgroundParamArray,qzshiftArray,...
contrastResolutionParamIndices(i),backgroundParamArray,qzshiftArray,...
scalefactorArray,bulkInArray,bulkOutArray,resolutionParamArray,...
dataPresent(i),data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
contrastBackgroundActions(i),nParams,parallel,resampleParams,...
Expand Down
6 changes: 3 additions & 3 deletions targetFunctions/+nonPolarisedTF/customXY.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
% Extract individual parameters from problemStruct
[numberOfContrasts, ~, contrastBackgroundIndices, contrastQzshiftIndices,...
contrastScalefactorIndices, contrastBulkInIndices, contrastBulkOutIndices,...
contrastResolutionIndices, ~, backgroundParamArray, qzshiftArray,...
contrastResolutionParamIndices, ~, backgroundParamArray, qzshiftArray,...
scalefactorArray, bulkInArray, bulkOutArray, resolutionParamArray, ~,...
dataPresent, nParams, params, ~, ~, contrastBackgroundActions, cCustFiles,...
useImaginary] = extractProblemParams(problemStruct);
Expand Down Expand Up @@ -68,7 +68,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),backgroundParamArray,qzshiftArray,...
contrastResolutionParamIndices(i),backgroundParamArray,qzshiftArray,...
scalefactorArray,bulkInArray,bulkOutArray,resolutionParamArray,...
dataPresent(i),data{i},dataLimits{i},simLimits{i},...
repeatLayers{i},contrastBackgroundActions(i),nParams,parallel,...
Expand All @@ -86,7 +86,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i),...
contrastResolutionIndices(i),backgroundParamArray,qzshiftArray,...
contrastResolutionParamIndices(i),backgroundParamArray,qzshiftArray,...
scalefactorArray,bulkInArray,bulkOutArray,resolutionParamArray,...
dataPresent(i),data{i},dataLimits{i},simLimits{i},...
repeatLayers{i},contrastBackgroundActions(i),nParams,parallel,...
Expand Down
6 changes: 3 additions & 3 deletions targetFunctions/+nonPolarisedTF/standardLayers.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
% Extract individual parameters from problemStruct
[numberOfContrasts, geometry, contrastBackgroundIndices, contrastQzshiftIndices,...
contrastScalefactorIndices, contrastBulkInIndices, contrastBulkOutIndices,...
contrastResolutionIndices, ~, backgroundParamArray, qzshiftArray,...
contrastResolutionParamIndices, ~, backgroundParamArray, qzshiftArray,...
scalefactorArray, bulkInArray, bulkOutArray, resolutionParamArray, ~,...
dataPresent, nParams, params, ~, resample, contrastBackgroundActions, ~,...
useImaginary] = extractProblemParams(problemStruct);
Expand Down Expand Up @@ -78,7 +78,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i), ...
contrastResolutionIndices(i),backgroundParamArray,qzshiftArray,...
contrastResolutionParamIndices(i),backgroundParamArray,qzshiftArray,...
scalefactorArray,bulkInArray,bulkOutArray,resolutionParamArray,...
dataPresent(i),data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
contrastBackgroundActions(i),nParams,parallel,resampleParams,...
Expand All @@ -99,7 +99,7 @@
] = contrastCalculation(contrastBackgroundIndices(i),...
contrastQzshiftIndices(i),contrastScalefactorIndices(i),...
contrastBulkInIndices(i),contrastBulkOutIndices(i), ...
contrastResolutionIndices(i),backgroundParamArray,qzshiftArray,...
contrastResolutionParamIndices(i),backgroundParamArray,qzshiftArray,...
scalefactorArray,bulkInArray,bulkOutArray,resolutionParamArray,...
dataPresent(i),data{i},dataLimits{i},simLimits{i},repeatLayers{i},...
contrastBackgroundActions(i),nParams,parallel,resampleParams,...
Expand Down
Loading

0 comments on commit 2257620

Please sign in to comment.