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
10 changes: 8 additions & 2 deletions API/events/triggerEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ function triggerEvent(eventType, varargin)
problemStruct = varargin{2};
plotData.reflectivity = result.reflectivity;
plotData.shiftedData = result.shiftedData;
plotData.sldProfiles = result.sldProfiles;
plotData.resampledLayers = result.resampledLayers;
plotData.subRoughs = result.contrastParams.subRoughs;
plotData.resample = problemStruct.resample;
plotData.dataPresent = problemStruct.dataPresent;
plotData.modelType = problemStruct.modelType;
plotData.contrastNames = problemStruct.names.contrasts;

[slds, resampledLayers, ~] = alignALProfiles(problemStruct.geometry, problemStruct.modelType, result.sldProfiles, result.resampledLayers, {});
plotData.sldProfiles = slds;
plotData.resampledLayers = resampledLayers;

eventManager.notify(eventType, plotData);
end
Expand All @@ -62,6 +64,10 @@ function triggerEvent(eventType, varargin)
elseif eventType == coderEnums.eventTypes.Plot
result = varargin{1};
problemStruct = varargin{2};
[slds, resampledLayers, ~] = alignALProfiles(problemStruct.geometry, problemStruct.modelType, result.sldProfiles, result.resampledLayers, {});
result.sldProfiles = slds;
result.resampledLayers = resampledLayers;

subRoughs = result.contrastParams.subRoughs;
nContrast = length(result.reflectivity);
[reflect, nReflect] = packCellArray(result.reflectivity, 1);
Expand Down
2 changes: 1 addition & 1 deletion minimisers/DREAM/functions/DREAM.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
if ~strcmpi(controls.display, coderEnums.displayOptions.Off)
triggerEvent(coderEnums.eventTypes.Message, sprintf('Optimisation terminated by user\n'));
end
break;
return;
end
end

Expand Down
6 changes: 6 additions & 0 deletions minimisers/DREAM/runDREAM.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
% Run the sampler....
[chain,dreamOutput,~] = DREAM(DREAMPar,ParInfo,ratInputs);

if isRATStopped(controls.IPCFilePath)
result = makeEmptyResultStruct(problemStruct.numberOfContrasts, length(problemStruct.fitParams), domains);
outProblemStruct = problemStruct;
return
end

% Combine all chains....
nChains = DREAMPar.nChains;
nParams = DREAMPar.nParams;
Expand Down
3 changes: 2 additions & 1 deletion minimisers/NS/nestedSampler.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@
if ~strcmpi(controls.display, coderEnums.displayOptions.Off)
triggerEvent(coderEnums.eventTypes.Message, sprintf('Optimisation terminated by user\n'));
end
break;
post_samples = [];
return;
end
% update counter
j = j+1;
Expand Down
5 changes: 5 additions & 0 deletions minimisers/NS/runNestedSampler.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
[logZ, nestSamples, postSamples, H] = nestedSampler(data, nLive, nMCMC,...
tolerance, likelihood, model, priorList, fitNames);

if isRATStopped(controls.IPCFilePath)
result = makeEmptyResultStruct(problemStruct.numberOfContrasts, length(problemStruct.fitParams), domains);
return
end

% Process the results...
nParams = length(fitNames);
% chain = nest_samples(:,1:end-1);
Expand Down
69 changes: 69 additions & 0 deletions utilities/plotting/alignALProfiles.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function [sldProfiles, resampledLayers, pIntervals] = alignALProfiles(geometry, modelType, sldProfiles, resampledLayers, pIntervals)
% Aligns the A/L SLD profiles so that the substrates line up by padding the
% start of any shorter than the longest profile. Also adjusts resampled
% layers and prediction interval slds as necessary.
%
% Parameters
% ----------
% geometry : geometryOptions
% The geometry.
% modelType : modelTypes
% The model type.
% sldProfiles : cell
% The sld profiles.
% resampledLayers : cell
% The resampled layers.
% pIntervals : cell
% The slds in the prediction intervals.
%
% Returns
% -------
% sldProfiles : cell
% The sld profiles, adjusted if necessary.
% resampledLayers : cell
% The resampled layers, adjusted if necessary.
% pIntervals : cell
% The slds in the prediction intervals, adjusted if necessary.

if ~strcmpi(geometry,'air/substrate') || strcmpi(modelType,'custom xy')
return
end

% Find the length of the longest profile.
lengths = zeros(size(sldProfiles));
for i = 1:numel(sldProfiles)
lengths(i) = size(sldProfiles{i}, 1);
end

% Get max length and its index
[maxLen, maxPos] = max(lengths, [], "all");
maxXValue = sldProfiles{maxPos}(end,1);

% Get the longest profile...
maxX = sldProfiles{maxPos}(:,1);

% Pad the start of any profiles that are shorter than this
for i = 1:numel(sldProfiles)
thisSLD = sldProfiles{i};
thisLen = size(thisSLD,1);
if thisLen < maxLen
diffLen = maxLen - thisLen;
pad = zeros(diffLen,1);
newY = [pad ; thisSLD(:,2)];
sldProfiles{i} = [maxX(:,1) newY(:)];

% For resampled layers, the pad is just one big layer at the start
thisResam = resampledLayers{i};
if any(thisResam,'all') % not all zeros
totLength = sum(thisResam(:,1));
padLength = maxXValue - totLength;
resamPad = [padLength 0 0];
resampledLayers{i} = [resamPad ; thisResam];
end

if ~isempty(pIntervals)
pIntervals{i} = [zeros(5, diffLen), pIntervals{i}];
end
end
end
end
9 changes: 5 additions & 4 deletions utilities/plotting/bayesShadedPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ function bayesShadedPlot(project, result, options)

controls = controlsClass();
[projectStruct,~] = parseClassToStructs(project,controls);
[sldProfiles, ~, pIntervals] = alignALProfiles(projectStruct.geometry, projectStruct.modelType, result.sldProfiles, result.resampledLayers, result.predictionIntervals.sld);

% Get the reflectivities and SLDs
reflectivityValues = result.predictionIntervals.reflectivity;
sldValues = result.predictionIntervals.sld;
sldValues = pIntervals;

shiftedData = result.shiftedData;
numberOfContrasts = length(shiftedData);
Expand Down Expand Up @@ -118,7 +119,7 @@ function bayesShadedPlot(project, result, options)
subplot(1,2,2); hold on; box on
xlabel('$\textrm{Z} (\AA)$','Interpreter','Latex')
ylabel('$\textrm{SLD} (\AA^{-2})$','Interpreter','Latex')
nColumns = size(result.sldProfiles, 2);
nColumns = size(sldProfiles, 2);
lines = cell(numberOfContrasts * nColumns, 1);
names = cell(numberOfContrasts * nColumns, 1);

Expand All @@ -128,7 +129,7 @@ function bayesShadedPlot(project, result, options)

sld = sldValues{i}(3,:);
limits = sldValues{i};
sldXValues = result.sldProfiles{i}(:,1);
sldXValues = sldProfiles{i}(:,1);

min = limits(vals(1),:);
max = limits(vals(2),:);
Expand All @@ -145,7 +146,7 @@ function bayesShadedPlot(project, result, options)

sld = sldValues(i,:);
limits = sldValues(i,:);
sldXValues = result.sldProfiles(i,:);
sldXValues = sldProfiles(i,:);

for j = 1:2
min = limits{j}(vals(1),:);
Expand Down
64 changes: 3 additions & 61 deletions utilities/plotting/plotRefSLD.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,72 +37,14 @@ function plotRefSLD(project, result, options)

data.reflectivity = result.reflectivity;
data.shiftedData = result.shiftedData;
data.sldProfiles = result.sldProfiles;
data.resampledLayers = result.resampledLayers;
data.dataPresent = projectStruct.dataPresent;
data.subRoughs = result.contrastParams.subRoughs;
data.resample = projectStruct.resample;
data.contrastNames = projectStruct.names.contrasts;

% If we have air/substrate geometry, modify the SLD profiles slightly
% so that the substrates line up (for custom XY the user does this
% themselves)...
if strcmpi(projectStruct.geometry,'air/substrate') && ~strcmpi(projectStruct.modelType,'custom xy')
data = alignALProfiles(data);
end

[slds, resampledLayers, ~] = alignALProfiles(projectStruct.geometry, projectStruct.modelType, result.sldProfiles, result.resampledLayers, {});
data.sldProfiles = slds;
data.resampledLayers = resampledLayers;
plotRefSLDHelper(data, false, options.linearX, options.q4, options.showErrorBar, ...
options.showGrid, options.showLegend, options.shiftValue);
end


function data = alignALProfiles(data)
% Aligns the A/L SLD profiles so that the substrates line up by padding the
% start of any shorter than the longest profile..

% Get the sld profiles out of data
slds = data.sldProfiles;
resamLays = data.resampledLayers;

% Find the length of the longest profile...
f = @(x)size(x{:},1);
lengths = arrayfun(f,slds);
maxPos = find(lengths == max(lengths));

% If maxPos is an array we have many of equal (leng) length.
% We can just pick one of them...
if length(maxPos) > 1
maxPos = maxPos(1);
end

maxLen = lengths(maxPos);
max_XValue = slds{maxPos}(end,1);

% Get the longest profile...
max_X = slds{maxPos}(:,1);

% Pad the start of any profiles that are shorter than this
for i = 1:length(slds)
thisSLD = slds{i};
thisLen = size(thisSLD,1);
if thisLen < maxLen
diffLen = maxLen - thisLen;
pad = zeros(diffLen,1);
newY = [pad ; thisSLD(:,2)];
slds{i} = [max_X(:,1) newY(:)];

% For resampled layers, the pad is just one big layer at the start
thisResam = resamLays{i};
if ~all(thisResam,'All') % not all zeros
totLength = sum(thisResam(:,1));
padLength = max_XValue - totLength;
resamPad = [padLength 0 0];
resamLays{i} = [resamPad ; thisResam];
end
end
end

data.sldProfiles = slds;
data.resampledLayers = resamLays;

end
Loading