Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions API/RATMain.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end
[problemStruct,result,bayesResults] = runDREAM(problemStruct,problemLimits,controls,priors);
otherwise
error('The procedure "%s" is not supported. The procedure must be one of "%s"', controls.procedure, strjoin(fieldnames(coderEnums.procedures), '", "'));
coderException(coderEnums.errorCodes.invalidOption, 'The procedure "%s" is not supported. The procedure must be one of "%s"', controls.procedure, strjoin(fieldnames(coderEnums.procedures), '", "'));
end

% Then just do a final calculation to fill in SLD if necessary
Expand All @@ -48,7 +48,7 @@
end

else
error("RAT cannot proceed without at least one contrast defined in the project");
coderException(coderEnums.errorCodes.domainError, 'RAT cannot proceed without at least one contrast defined in the project');
end

end
1 change: 1 addition & 0 deletions API/enums/coderEnums.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
allowedTypes = allowedTypes.toStruct()
boundHandlingOptions = boundHandlingOptions.toStruct()
calculationTypes = calculationTypes.toStruct()
errorCodes = errorCodes.toStruct()
displayOptions = displayOptions.toStruct()
eventTypes = eventTypes.toStruct()
geometryOptions = geometryOptions.toStruct()
Expand Down
21 changes: 21 additions & 0 deletions API/enums/errorCodes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
classdef errorCodes < customEnum
methods (Static)
function s = toStruct()
s = customEnum.toStruct(mfilename('class'));
end

function v = values()
v = customEnum.values(mfilename('class'));
end

function e = fromValue(value)
e = customEnum.fromValue(mfilename('class'), value);
end
end

enumeration
unknown (0)
invalidOption (1)
domainError (2)
end
end
3 changes: 2 additions & 1 deletion addPaths.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
eventCompilePath;
fullfile(root,'compile');
fullfile(root,'compile','customWrapper');
fullfile(root,'compile','exceptions');
fullfile(root,'compile','fullCompile');
fullfile(root,'compile','reflectivityCalculation');

Expand Down Expand Up @@ -61,7 +62,7 @@

addpath(root);
setappdata(0, 'root', root);
includedir = {fullfile(root, 'compile', 'customWrapper'), eventCompilePath};
includedir = {fullfile(root, 'compile', 'customWrapper'), fullfile(root, 'compile', 'exceptions'), eventCompilePath};
setappdata(0, 'includeDirs', includedir);

% Add the folder with the eventManager dynamic library to the system path so
Expand Down
4 changes: 2 additions & 2 deletions compile/events/eventHelper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef WRAPPER_H
#define WRAPPER_H
#ifndef EVENT_HELPER_HPP
#define EVENT_HELPER_HPP

#include <memory>
#include <cstdlib>
Expand Down
19 changes: 19 additions & 0 deletions compile/exceptions/coderException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CODER_EXCEPTION_HPP
#define CODER_EXCEPTION_HPP

#include <stdexcept>

// Throws the appropriate std exception for the given error code defined
// in the errorCodes.m
namespace{
void coderException(int errorCode, const char* msg)
{
if (errorCode == 1)
throw std::invalid_argument(msg);
else if (errorCode == 2)
throw std::domain_error(msg);
else
throw std::exception(msg);
}
}
#endif // CODER_EXCEPTION_HPP
14 changes: 14 additions & 0 deletions compile/exceptions/coderException.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function coderException(errorCode, message, varargin)
% Ensures a proper exception is thrown in the generated C++ code.
% The arguments should be the errorCode integer, error message as a char array (which can be a formatspec)
% and other parameters if message is a formatspec.
%
% coderException(coderEnums.errorCodes.invalidOption, 'The model type is not supported')
% coderException(coderEnums.errorCodes.invalidOption, 'The model type "%s" is not supported', modelType)

if coder.target('C++')
coder.cinclude('coderException.hpp');
coder.ceval('coderException', errorCode, sprintf(message, varargin{:}));
end
error(message, varargin{:});
end
8 changes: 4 additions & 4 deletions minimisers/DREAM/functions/checkDREAM.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
if DREAMPar.nChains < (2 * DREAMPar.delta) + 1
% Error -- not enough chains to do sampling -- increase number of chains!
stop = true;
error('DREAM ERROR: Insufficient number of chains -> Use at least DREAMPar.nChains = %1g chains \n',((2 * DREAMPar.delta) + 1));
coderException(coderEnums.errorCodes.domainError, 'DREAM ERROR: Insufficient number of chains -> Use at least DREAMPar.nChains = %1g chains \n',((2 * DREAMPar.delta) + 1));
end

% Check parameter ranges
if strcmp(paramInfo,'latin')
% Error -- if lhs is used -> requires explicit parameter ranges
if ( sum(isinf(paramInfo.min)) == DREAMPar.nParams )
stop = true;
error('DREAM ERROR: latinHypercubeSampling hypercube sampling selected but parameter ranges not defined -> Set paramInfo.min and paramInfo.max!!\n');
coderException(coderEnums.errorCodes.domainError, 'DREAM ERROR: latinHypercubeSampling hypercube sampling selected but parameter ranges not defined -> Set paramInfo.min and paramInfo.max!!\n');
end
end

Expand All @@ -52,9 +52,9 @@
if ( isfield(Meas_info,'Sigma') == 1 ) && ( isfield(Meas_info,'n') == 0 )
if (prod(size(Meas_info.Sigma)) ~= prod(size(Meas_info.MeasData))) && ( prod(size(Meas_info.Sigma)) > 1 )
% Error -- Meas_info.Sigma incorrect length!!
error('DREAM ERROR: Heteroscedastic error, but length of Meas_info.Sigma is not equal to that of the observations!!\n');
coderException(coderEnums.errorCodes.domainError, 'DREAM ERROR: Heteroscedastic error, but length of Meas_info.Sigma is not equal to that of the observations!!');
elseif ( sum(Meas_info.Sigma<=0) > 0 )
% Error -- Meas_info.Sigma is negative!!
error('DREAM ERROR: At least one value of the specified Meas_info.Sigma is negative or zero!!\n');
coderException(coderEnums.errorCodes.domainError, 'DREAM ERROR: At least one value of the specified Meas_info.Sigma is negative or zero!!');
end
end
8 changes: 4 additions & 4 deletions minimisers/DREAM/functions/multrnd.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
% multinomial distribution with n trials and k outcomes, where the probability for each
% simulation is,
% n!
% ---------------------- � p1^n1 � p2^n2 � . . . � pk^nk .
% n1! � n2! � . . . � nk!
% ---------------------- � p1^n1 � p2^n2 � . . . � pk^nk .
% n1! � n2! � . . . � nk!
%
% Then, a single sample {n1, n2, . . . , nk} have a multinomial joint distribution with
% parameters n and p1, p2, . . . , pk. The parameter n is called the number of trials;
Expand Down Expand Up @@ -82,12 +82,12 @@
if nargin == 2
m = 1;
else
error('You need to input at least two arguments.');
coderException(coderEnums.errorCodes.domainError, 'You need to input at least two arguments.');
end
end

if (length(n)~=1) | (fix(n) ~= n) | (n < 0)
error('n must be a positive integer.');
coderException(coderEnums.errorCodes.domainError, 'n must be a positive integer.');
end

P = sum(p);
Expand Down
3 changes: 1 addition & 2 deletions minimisers/DREAM/functions/removeOutlier.m
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@
triggerEvent(coderEnums.eventTypes.Message, 'DREAMPar.nChains > 60; using Peirce r-values for DREAMPar.nChains is 60');
end
if N < 2
error('Insufficient number of chains to apply Peirce diagnostic');
return;
coderException(coderEnums.errorCodes.domainError, 'Insufficient number of chains to apply Peirce diagnostic');
end
end

Expand Down
6 changes: 3 additions & 3 deletions minimisers/NS/nestedSampler.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@

% check certain values are positive integers or zero
if mod(nMCMC, 1) ~= 0 || nMCMC < 0
error('NS Error: nMCMC must be an integer >= 0')
coderException(coderEnums.errorCodes.domainError, 'NS Error: nMCMC must be an integer >= 0')
end

if mod(nLive, 1) ~= 0 || nLive < 0
error('NS Error: nLive must be an integer >= 0')
coderException(coderEnums.errorCodes.domainError, 'NS Error: nLive must be an integer >= 0')
end

% draw the set of initial live points from the prior
Expand All @@ -102,7 +102,7 @@
p4 = prior(i,3);
livepoints(:,i) = 10.^(log10(p3) + (log10(p4)-log10(p3))*rand(nLive,1));
else
error('Unrecognised prior for param %d', int32(i));
coderException(coderEnums.errorCodes.invalidOption, 'Unrecognised prior for param %d', int32(i));
end
end

Expand Down
1 change: 1 addition & 0 deletions rmPaths.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
fullfile(root,'compile');
fullfile(root,'compile','customWrapper');
fullfile(root,'compile','events');
fullfile(root,'compile','exceptions');
fullfile(root,'compile','fullCompile');
fullfile(root,'compile','reflectivityCalculation');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
shiftedData(:,2) = shiftedData(:,2) - backData;
shiftedData(:,3) = sqrt(shiftedData(:,3).^2 + backError.^2); % Propagate the errors
otherwise
error('"%s" does not represent a valid contrast background action.', backgroundAction);
coderException(coderEnums.errorCodes.invalidOption, '"%s" does not represent a valid contrast background action.', backgroundAction);
end

% Reduce data to original three columns
Expand Down
2 changes: 1 addition & 1 deletion targetFunctions/common/callReflectivity/callReflectivity.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
end
end
otherwise
error('The reflectivity type "%s" is not supported', refType);
coderException(coderEnums.errorCodes.invalidOption, 'The reflectivity type "%s" is not supported', refType);
end

simulation(:,2) = simRef(:);
Expand Down
2 changes: 1 addition & 1 deletion targetFunctions/common/constructBackground.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
thisBackground = feval(funcName, background(:,1), paramsArray);
end
else
error('Background functions in languages other than MATLAB are not supported.');
coderException(coderEnums.errorCodes.invalidOption, 'Background functions in languages other than MATLAB are not supported.');
end

background(:,2) = background(:,2) + thisBackground;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
actualSize = coder.ceval('convertVector2Ptr', outArray, coder.wref(tempOutput));

if size ~= actualSize
error('The output of the custom function with size %d does not match the specified size (%d x %d).', actualSize, outputSize(2), outputSize(1))
coderException(coderEnums.errorCodes.domainError, 'The output of the custom function with size %.0f does not match the specified size (%.0f x %.0f).', actualSize, outputSize(2), outputSize(1));
end
output = reshape(tempOutput, [outputSize(2), outputSize(1)])';
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
% Calling matlab from other languages should be implemented in their wrapper
sRough = 0;
output = zeros([0 0]);
error("This is not supported!");
coderException(coderEnums.errorCodes.unknown, 'This is not supported!');
end
6 changes: 3 additions & 3 deletions targetFunctions/reflectivityCalculation.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
subRoughs] = normalTF.customXY(problemStruct,controls);

otherwise
error('The model type "%s" is not supported', modelType);
coderException(coderEnums.errorCodes.invalidOption, 'The model type "%s" is not supported', modelType);
end

%case coderEnums.calculationTypes.OilWater
Expand Down Expand Up @@ -82,11 +82,11 @@
subRoughs] = domainsTF.customXY(problemStruct,controls);

otherwise
error('The model type "%s" is not supported', modelType);
coderException(coderEnums.errorCodes.invalidOption, 'The model type "%s" is not supported', modelType);
end

otherwise
error('The calculation type "%s" is not supported', targetFunction);
coderException(coderEnums.errorCodes.invalidOption, 'The calculation type "%s" is not supported', targetFunction);
end

% Make the result struct
Expand Down