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
14 changes: 12 additions & 2 deletions API/projectClass/projectClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,12 @@ function delete(obj)

function obj = setContrastModel(obj, row, model)
% Edits the model of an existing contrast parameter. Expects
% the index of contrast parameter and cell array of layer names
% the index of contrast parameter and cell array of layer
% names. Multiple models can be set simultaneously by using
% an 1D array of indices or cell of strings
%
% project.setContrastModel(1, {'layer 1'})
% project.setContrastModel(1:3, {'layer 1'})

% Make a different allowed list depending on whether
% it is custom or layers
Expand All @@ -820,7 +823,14 @@ function delete(obj)
end

% Call the setContrastModel method
obj.contrasts.setContrastModel(row, obj.modelType, allowedValues, model);
if isText(row)
row = {row};
elseif isnumeric(row)
row = num2cell(row);
end
for i=1:length(row)
obj.contrasts.setContrastModel(row{i}, obj.modelType, allowedValues, model);
end
end

% ----------------------------------------------------------------
Expand Down
34 changes: 20 additions & 14 deletions examples/miscellaneous/bayesBenchmark/directCompare2D.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
%Do the direct calculation first.
%Start by converting the R1 project to
%the R2 case...
[d2oproblem,controls] = r1ToProblemDef('defaultProject.mat');
root = getappdata(0, 'root');
d2oproblem = r1ToProjectClass(fullfile(root, 'utilities', 'misc', 'defaultR1ProjectTemplate.mat'));

controls = controlsClass();
controls.calcSldDuringFit = true;
controls.nsTolerance = 1;
controls.nLive = 500;
d2oProblem.fitScalefactor = 0;


% Run nested sampler
controls.procedure = 'bayes';
controls.procedure = 'ns';
[NSProb,NSResults] = RAT(d2oproblem,controls);


Expand All @@ -22,19 +25,21 @@
probArray = zeros(gridSize, gridSize);

%Make a vector of roughness values..
outProb = NSProb;
minRough = outProb.paramLimits{1}(1);
maxRough = outProb.paramLimits{1}(2);
paramsTable = NSProb.parameters.varTable;
minRough = paramsTable{1,2};
maxRough = paramsTable{1,4};
roughVector = linspace(minRough, maxRough, gridSize);

%Also background...
minBack = outProb.backgroundLimits{1}(1);
maxBack = outProb.backgroundLimits{1}(2);
backParsTable = NSProb.background.backgroundParams.varTable;
minBack = backParsTable{1,2};
maxBack = backParsTable{1,4};
backsVector = linspace(minBack, maxBack, gridSize);

% %..and scalefactor
% minScale = outProb.scalefactorLimits{1}(1);
% maxScale = outProb.scalefactorLimits{1}(2);
% scalefactorTable = NSProb.scalefactors.varTable;
% minScale = scalefactorTable{1,2};
% maxScale = scalefactorTable{1,4};
% scaleVector = linspace(minScale, maxScale, gridSize);


Expand All @@ -55,14 +60,15 @@
counter = 1;
totalGrid = gridSize^2;
controls.procedure = 'calculate';
controls.display = 'off';

for r = 1:gridSize
for b = 1:gridSize
thisRough = roughVector(r);
thisBack = backsVector(b);

d2oproblem.params(1) = thisRough;
d2oproblem.backgrounds(1) = thisBack;
NSProb.setParameter(1,'value',thisRough);
NSProb.setBackgroundParam(1,'value',thisBack);

[outProblem,results] = RAT(d2oproblem,controls);
thisChi = results.calculationResults.sumChi;
Expand Down Expand Up @@ -105,9 +111,9 @@
scaleDist = squeeze(scaleDist);
%figure(2); clf; contour(backsVector, scaleVector, scaleDist)
scaleDist = sum(scaleDist,2);
scaleDist = [scaleVector(:) scaleDist(:)];
figure(3); subplot(1,2,3); hold on
plot(scaleDist(:,1), scaleDist(:,2),'linewidth',2);
% scaleDist = [scaleVector(:) scaleDist(:)];
% figure(3); subplot(1,2,3); hold on
% plot(scaleDist(:,1), scaleDist(:,2),'linewidth',2);
% % scaleDist = [scaleVector(:) scaleDist(:)];
%
%
Expand Down
20 changes: 8 additions & 12 deletions examples/miscellaneous/bayesBenchmark/directCompare3D.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
%Comparison of MCMC, NS and direct calculation.
clear
useSaved = true;
useSaved = false;

%Do the direct calculation first.
%Start by converting the R1 project to
%the R2 case...
d2oproblem = r1ToProjectClass('defaultProject.mat');

root = getappdata(0, 'root');
d2oproblem = r1ToProjectClass(fullfile(root, 'utilities', 'misc', 'defaultR1ProjectTemplate.mat'));
d2oproblem.setScalefactor(1,'min',0.07);
d2oproblem.setScalefactor(1,'max',0.13);

controls = controlsDef();
controls = controlsClass();
controls.calcSldDuringFit = true;
[outProb,results] = RAT(d2oproblem,controls);
figure(1); clf
Expand All @@ -19,9 +19,7 @@
fprintf('Chi squared in %d \n',thisChisq);

% Run MCMC
controls.procedure = 'bayes';
controls.nsimu = 50000;
controls.repeats = 3;
controls.procedure = 'dream';
[bayesProb,bayesResults] = RAT(d2oproblem,controls);

%Use a 30 x 30 grid.
Expand Down Expand Up @@ -84,9 +82,7 @@

%%
if useSaved
probArray = load('probArray.mat');
probArray = probArray.probArray;

probArray = load('probArray.mat').probArray;
else

[problemStruct,problemLimits,priors,controls] = parseClassToStructs(d2oproblem,controls);
Expand All @@ -107,15 +103,15 @@
problemStruct.fitParams(2) = thisBack;
problemStruct.fitParams(3) = thisScale;
problemStruct = unpackParams(problemStruct,controls.checks);
[problem,results] = reflectivity_calculation(problemStruct,problemLimits,controls);
results = reflectivityCalculation(problemStruct,problemLimits,controls);

% d2oproblem.setParameter(1,'value',thisRough);
% d2oproblem.setBackgroundParam(1,'value',thisBack);
% d2oproblem.setScalefactor(1,'value',thisScale);
%
% [outProblem,results] = RAT(d2oproblem,controls);

thisChi = problem.calculations.sumChi;
thisChi = results.calculationResults.sumChi;
probArray(r,b,s) = exp(-thisChi/2);
percent = (counter/totalGrid)*100;
fprintf('Calculated %1.1f percent \n',percent);
Expand Down
7 changes: 7 additions & 0 deletions tests/testProjectClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,13 @@ function testContrast(testCase)
testCase.verifyEqual(testCase.project.contrasts.contrasts{1}.model, {'Hydrogenated Heads', 'Deuterated Heads'}, 'setContrastModel method not working');
testCase.project.setContrastModel('Another Bilayer', {'Deuterated Heads', 'Hydrogenated Heads'});
testCase.verifyEqual(testCase.project.contrasts.contrasts{1}.model, {'Deuterated Heads', 'Hydrogenated Heads'}, 'setContrastModel method not working');
testCase.project.addContrast('First Bilayer');
testCase.project.setContrastModel(1:2, {'Deuterated Heads'});
testCase.verifyEqual(testCase.project.contrasts.contrasts{1}.model, {'Deuterated Heads'}, 'setContrastModel method not working');
testCase.verifyEqual(testCase.project.contrasts.contrasts{2}.model, {'Deuterated Heads'}, 'setContrastModel method not working');
testCase.project.setContrastModel({'First Bilayer', 'Another Bilayer'}, {'Hydrogenated Heads'});
testCase.verifyEqual(testCase.project.contrasts.contrasts{1}.model, {'Hydrogenated Heads'}, 'setContrastModel method not working');
testCase.verifyEqual(testCase.project.contrasts.contrasts{2}.model, {'Hydrogenated Heads'}, 'setContrastModel method not working');
testCase.verifyError(@() testCase.project.setContrastModel(3, {}), exceptions.indexOutOfRange.errorID)
testCase.verifyError(@() testCase.project.setContrastModel('Bilayer', {}), exceptions.nameNotRecognised.errorID);
end
Expand Down
2 changes: 1 addition & 1 deletion tests/testProjectClassToR1.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function defineVariables(testCase)
testCase.inputStandardProjectClass = 'DSPCBilayerProjectClass.mat';
testCase.inputStandardStruct = 'DSPCBilayerStructInput.mat';
testCase.outputStandardStruct = 'DSPCBilayerStructOutput.mat';
testCase.defaultProject = 'defaultProject.mat';
testCase.defaultProject = 'defaultR1ProjectTemplate.mat';
testCase.outputCustomStruct = 'monolayerVolumeModel.mat';
testCase.inputCustomProjectClass = 'monolayerVolumeModelProjectClass.mat';
testCase.modelFile = 'monolayerVolumeCustomLayerModel.m';
Expand Down
4 changes: 2 additions & 2 deletions utilities/misc/projectClassToR1.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
% outputs r1 struct. The optional arguments are:
%
% r1Problem (struct): The r1 struct which is updated with equivalent projectClass values.
% [default] defaultProject.mat.
% [default] defaultR1ProjectTemplate.mat.
% dirName (char): The directory where the files are saved.
% [default] newDirectory.
% dirPath (char): The path where the new directory is created.
Expand All @@ -28,7 +28,7 @@
r1Problem = pars.r1Problem;
if isfield(r1Problem,'empty')
% load default project if r1Problem not provided
r1Problem = load('defaultProject.mat').problem;
r1Problem = load('defaultR1ProjectTemplate.mat').problem;
end

if ~exist(fullName,'dir') && saveTheProject
Expand Down
4 changes: 4 additions & 0 deletions utilities/plotting/bayesShadedPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function bayesShadedPlot(problem, result, options)
subplot(1,2,1)
set(gca,'YScale','log','XScale','log');
hold on; box on
xlabel('$\textrm{Q}_{z} (\AA^{-1})$', 'Interpreter', 'Latex')
ylabel('Reflectivity', 'Interpreter', 'Latex')

for i = 1:numberOfContrasts

Expand Down Expand Up @@ -92,6 +94,8 @@ function bayesShadedPlot(problem, result, options)

% Now plot the SLDs
subplot(1,2,2); hold on; box on
xlabel('$\textrm{Z} (\AA)$','Interpreter','Latex')
ylabel('$\textrm{SLD} (\AA^{-2})$','Interpreter','Latex')

if ~isDomains

Expand Down
4 changes: 4 additions & 0 deletions utilities/plotting/plotRefSLDHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function plotRefSLDHelper(data, noDelay, linearX, q4, showErrorBar, showGrid, sh
set(gca,'YGrid','on','XGrid','on');
end
hold on
xlabel('$\textrm{Q}_{z} (\AA^{-1})$', 'Interpreter', 'Latex')
ylabel('Reflectivity', 'Interpreter', 'Latex')
lines = cell(numberOfContrasts, 1);
mult = 1;
q4Data = 1;
Expand Down Expand Up @@ -80,6 +82,8 @@ function plotRefSLDHelper(data, noDelay, linearX, q4, showErrorBar, showGrid, sh
set(gca,'YGrid','on','XGrid','on');
end
hold on
xlabel('$\textrm{Z} (\AA)$','Interpreter','Latex')
ylabel('$\textrm{SLD} (\AA^{-2})$','Interpreter','Latex')
nColumns = size(data.sldProfiles, 2);
lines = cell(numberOfContrasts * nColumns, 1);
names = cell(numberOfContrasts * nColumns, 1);
Expand Down