Skip to content

Commit

Permalink
Removes "modelType" from contrast routines
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPaulSharp committed Feb 14, 2025
1 parent fab5159 commit 2b9cb6e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 76 deletions.
12 changes: 6 additions & 6 deletions API/projectClass/baseContrasts.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
end
end

function obj = addContrast(obj, modelType, allowedNames, varargin)
function obj = addContrast(obj, allowedNames, varargin)
% Add a contrast to the class
% A class can be added with no input parameters, just a class
% name, or a set of key-value pairs.
Expand All @@ -101,7 +101,7 @@
inputVals = varargin;
end

thisContrast = parseContrastInput(obj, modelType, allowedNames, inputVals);
thisContrast = parseContrastInput(obj, allowedNames, inputVals);
thisContrast = obj.setDefaultValues(thisContrast);

obj.contrasts{end+1} = thisContrast;
Expand Down Expand Up @@ -142,7 +142,7 @@

end

function obj = setContrastModel(obj, row, modelType, allowedNames, model)
function obj = setContrastModel(obj, row, allowedNames, model)
% Set the value of the model parameter in a contrast.
% The expected input is the contrast (specified either by name
% or index), the model type, the allowed values (either layers
Expand All @@ -152,11 +152,11 @@
% "addContrast" or "setContrast".
%
% contrasts.setContrastModel(1, 'standard layers', allowedNames, 'Oxide Model')
obj.setContrast(row, modelType, allowedNames, 'model', model);
obj.setContrast(row, allowedNames, 'model', model);

end

function obj = setContrast(obj, row, modelType, allowedNames, varargin)
function obj = setContrast(obj, row, allowedNames, varargin)
% Set a value within a contrast.
% The expected input is the contrast (specified either by name
% or index), the allowed values for all parameters and a
Expand Down Expand Up @@ -188,7 +188,7 @@
% Check to see if the inputs are valid
% Raise a warning if we try to set the model as this should be
% done elsewhere
inputBlock = parseContrastInput(obj, modelType, allowedNames, varargin);
inputBlock = parseContrastInput(obj, allowedNames, varargin);

if isfield(inputBlock, 'name') && ~isempty(inputBlock.name)
thisContrast.name = inputBlock.name;
Expand Down
22 changes: 11 additions & 11 deletions API/projectClass/contrastsClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@

end

function inputBlock = parseContrastInput(obj, modelType, allowedNames, inputValues)
function inputBlock = parseContrastInput(obj, allowedNames, inputValues)
% Parse the parameters given for the contrast, assigning
% default values to those unspecified and ensuring specified
% values are of the correct type, and included in the list of
% allowed names where necessary.
%
% contrastsClass.parseContrastInput(modelType, allowedNames, ...
% contrastsClass.parseContrastInput(allowedNames, ...
% 'name', 'Contrast Name', ...
% 'background', 'Background H2O')
defaultName = '';
Expand All @@ -211,7 +211,6 @@
expectedBulkOut = cellstr(allowedNames.bulkOutNames);
expectedScalefactor = cellstr(allowedNames.scalefactorNames);
expectedResolution = cellstr(allowedNames.resolutionNames);
expectedModel = cellstr(allowedNames.modelNames);

p = inputParser;
p.PartialMatching = false;
Expand All @@ -238,7 +237,7 @@
addParameter(p,'domainRatio', defaultDomainRatio, @(x) obj.validateExactString(x,expectedDomainRatio));
end

addParameter(p,'model', defaultModel, @(x) obj.validateContrastModel(x,modelType,expectedModel));
addParameter(p,'model', defaultModel, @(x) obj.validateContrastModel(x,allowedNames));

parse(p, inputValues{:});
inputBlock = p.Results;
Expand All @@ -254,23 +253,24 @@ function validateExactString(~, input, allowedNames)
end
end

function validateContrastModel(obj, model, modelType, allowedModelNames)
function validateContrastModel(obj, model, allowedNames)
modelArray = cellstr(model);

% Check the input is as expected
modelType = validateOption(modelType, 'modelTypes', obj.invalidTypeMessage).value;
if any(strcmpi(modelType, {modelTypes.CustomLayers.value, modelTypes.CustomXY.value}))
% Check the input is as expected - modelNames tells us what the
% model type is
if all(size(allowedNames.modelNames) == size(allowedNames.customFileNames)) && all(cellfun(@strcmp, allowedNames.modelNames, allowedNames.customFileNames))
if length(modelArray) > 1
throw(exceptions.invalidValue('Only one model value is allowed for custom models'));
end
elseif strcmpi(modelType, modelTypes.StandardLayers.value) && obj.domainsCalc
elseif obj.domainsCalc && all(size(allowedNames.modelNames) == size(allowedNames.domainContrastNames)) && all(cellfun(@strcmp, allowedNames.modelNames, allowedNames.domainContrastNames))
if length(modelArray) ~= 2
throw(exceptions.invalidValue('Exactly two model values are required for ''standard layers'' with domains'));
end
end

for i = 1:length(modelArray)
if ~strcmpi(modelArray{i}, allowedModelNames)
throw(exceptions.nameNotRecognised(sprintf('Model component name "%s" is not recognised. The allowed names are: "%s".', modelArray{i}, strjoin(allowedModelNames, '", "'))));
if ~strcmpi(modelArray{i}, allowedNames.modelNames)
throw(exceptions.nameNotRecognised(sprintf('Model component name "%s" is not recognised. The allowed names are: "%s".', modelArray{i}, strjoin(allowedNames.modelNames, '", "'))));
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion API/projectClass/domainContrastsClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
contrastStruct.contrastLayers = contrastLayers;
end

function inputBlock = parseContrastInput(obj, ~, allowedNames, inputValues)
function inputBlock = parseContrastInput(obj, allowedNames, inputValues)
% Parse the parameters given for the contrast, assigning
% default values to those unspecified and ensuring specified
% values are of the correct type, and included in the list of
Expand Down
8 changes: 4 additions & 4 deletions API/projectClass/domainsClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
allowedValues = obj.getAllAllowedNames();

% Call the setContrastModel method
obj.contrasts.setContrastModel(row, obj.modelType, allowedValues, model);
obj.contrasts.setContrastModel(row, allowedValues, model);
end

% -------------------------------------------------------------------
Expand Down Expand Up @@ -128,7 +128,7 @@
% project.addDomainContrast('domainContrast 1', 'bulkIn', 'Silicon');
if isa(obj.domainContrasts, 'domainContrastsClass')
allowedNames = obj.getAllAllowedNames();
obj.domainContrasts.addContrast(obj.modelType, allowedNames, varargin{:});
obj.domainContrasts.addContrast(allowedNames, varargin{:});
else
throw(exceptions.invalidProperty(sprintf('Domain Contrasts are not defined for the model type: %s', obj.modelType)));
end
Expand Down Expand Up @@ -158,7 +158,7 @@
allowedValues = obj.getAllAllowedNames();

% Call the setContrast method
obj.domainContrasts.setContrast(row, obj.modelType, allowedValues, varargin{:});
obj.domainContrasts.setContrast(row, allowedValues, varargin{:});
else
throw(exceptions.invalidProperty(sprintf('Domain Contrasts are not defined for the model type: %s', obj.modelType)));
end
Expand All @@ -171,7 +171,7 @@
% project.setDomainContrastModel(1, {'layer 1'})
if isa(obj.domainContrasts, 'domainContrastsClass')
allowedValues = obj.getAllAllowedNames();
obj.domainContrasts.setContrastModel(row, obj.modelType, allowedValues, model);
obj.domainContrasts.setContrastModel(row, allowedValues, model);
else
throw(exceptions.invalidProperty(sprintf('Domain Contrasts are not defined for the model type: %s', obj.modelType)));
end
Expand Down
36 changes: 19 additions & 17 deletions API/projectClass/projectClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ function delete(obj)
%
% project.addContrast('contrast 1', 'bulkIn', 'Silicon');
allowedNames = obj.getAllAllowedNames();
obj.contrasts.addContrast(obj.modelType, allowedNames, varargin{:});
obj.contrasts.addContrast(allowedNames, varargin{:});
end

function obj = removeContrast(obj, row)
Expand All @@ -803,7 +803,7 @@ function delete(obj)
allowedValues = obj.getAllAllowedNames();

% Call the setContrast method
obj.contrasts.setContrast(row, obj.modelType, allowedValues, varargin{:});
obj.contrasts.setContrast(row, allowedValues, varargin{:});
end

function obj = setContrastModel(obj, row, model)
Expand All @@ -823,7 +823,7 @@ function delete(obj)
row = num2cell(row);
end
for i=1:length(row)
obj.contrasts.setContrastModel(row{i}, obj.modelType, allowedValues, model);
obj.contrasts.setContrastModel(row{i}, allowedValues, model);
end
end

Expand Down Expand Up @@ -1286,20 +1286,8 @@ function setLayersAndContrasts(obj, oldModel)

% Contrasts are a cell array rather than a table
% Need to handle resample and model fields separately
for i=1:obj.contrasts.numberOfContrasts

reducedStruct = rmfield(obj.contrasts.contrasts{i}, {'resample', 'model'});
contrastParams = string(namedargs2cell(reducedStruct));
contrastSpec = options.objName + ".addContrast(" + join(repmat("'%s'", 1, length(contrastParams)), ", ") + ");\n";
script = script + sprintf(contrastSpec, contrastParams);
script = script + sprintf(options.objName + ".setContrast(%d, 'resample', %s);\n", i, string(obj.contrasts.contrasts{i}.resample));
if ~isempty(obj.contrasts.contrasts{i}.model)
script = script + sprintf(options.objName + ".setContrast(%d, 'model', {" + join(repmat("'%s'", 1, length(obj.contrasts.contrasts{i}.model))) +"});\n", i, obj.contrasts.contrasts{i}.model{:});
end
script = script + newline;

end

% Also need to do domain contrasts before experimental
% contrasts
if isprop(obj, 'domainContrasts') && isa(obj.domainContrasts, 'domainContrastsClass')
for i=1:obj.domainContrasts.numberOfContrasts

Expand All @@ -1314,6 +1302,20 @@ function setLayersAndContrasts(obj, oldModel)

end
end

for i=1:obj.contrasts.numberOfContrasts

reducedStruct = rmfield(obj.contrasts.contrasts{i}, {'resample', 'model'});
contrastParams = string(namedargs2cell(reducedStruct));
contrastSpec = options.objName + ".addContrast(" + join(repmat("'%s'", 1, length(contrastParams)), ", ") + ");\n";
script = script + sprintf(contrastSpec, contrastParams);
script = script + sprintf(options.objName + ".setContrast(%d, 'resample', %s);\n", i, string(obj.contrasts.contrasts{i}.resample));
if ~isempty(obj.contrasts.contrasts{i}.model)
script = script + sprintf(options.objName + ".setContrast(%d, 'model', {" + join(repmat("'%s'", 1, length(obj.contrasts.contrasts{i}.model))) +"});\n", i, obj.contrasts.contrasts{i}.model{:});
end
script = script + newline;

end
end
end

Expand Down
Loading

0 comments on commit 2b9cb6e

Please sign in to comment.