Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions tests/testJsonToProject/result.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/testJsonToProject/result_bayes.json

Large diffs are not rendered by default.

42 changes: 40 additions & 2 deletions tests/testJsonToProject/testJsonToProject.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
'DSPC_custom_layers', ...
'DSPC_custom_XY', ...
'DSPC_standard_layers'}

resultFile = {'result', ...
'result_bayes'}
end

methods(TestClassSetup)
Expand All @@ -28,9 +31,44 @@ function setWorkingFolder(testCase)
end

% test that JSON to Project successfully converts projects
methods (Test, ParameterCombination='exhaustive')
methods (Test)
function testJsonConversion(testCase, file)
jsonToProject(append(file, ".json"));
project = jsonToProject(append(file, ".json"));
projectToJson(project, "test.json");
project2 = jsonToProject("test.json");

props = properties(project);
for i = 1:length(props)
testCase.verifyEqual(project.(props{i}), project2.(props{i}));
end
end

function testJsonResultConversion(testCase, resultFile)
result = jsonToResults(append(resultFile, ".json"));
resultsToJson(result, "test.json");
result2 = jsonToResults("test.json");

props = properties(result);
for i = 1:length(props)
testCase.verifyEqual(result.(props{i}), result2.(props{i}));
end
end

function testJsonControlsConversion(testCase)
controls = controlsClass();
controls.numSimulationPoints = 100;
controls.xTolerance = 19;
controls.populationSize = 200;
controls.nLive = 140;
controls.nSamples = 200;

controlsToJson(controls, "test.json");
controls2 = jsonToControls("test.json");

props = properties(controls);
for i = 1:length(props)
testCase.verifyEqual(controls.(props{i}), controls2.(props{i}));
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions utilities/misc/controlsToJson.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

function controlsToJson(controls,filename)

% Saves the current controls block as a json..
encoded = jsonencode(controls,ConvertInfAndNaN=false);
encoded = replace(encoded,'Infinity','Inf');

[path,filename,~] = fileparts(filename);
fid = fullfile(path, append(filename, '.json'));
fid = fopen(fid,'w');
fprintf(fid,'%s',encoded);
fclose(fid);

end
17 changes: 17 additions & 0 deletions utilities/misc/jsonToControls.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

function controls = jsonToControls(filename)

% Loads in a controls json, and contructs the controlsClass from it...

% Load the file...
jsonControls = jsondecode(fileread(filename));

% Make a controlsClass()
controls = controlsClass();

fieldNames = fieldnames(jsonControls);
for i = 1:length(fieldNames)
controls.(fieldNames{i}) = jsonControls.(fieldNames{i});
end

end
7 changes: 5 additions & 2 deletions utilities/misc/jsonToProject.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
for i=1:length(json_struct.parameters)
p = json_struct.parameters(i);
if p.name == "Substrate Roughness"
project.setParameter(i, 'min', p.min, 'value', p.value, 'max', p.max, 'fit', p.fit, 'prior', p.prior_type, 'mu', p.mu, 'sigma', p.sigma);
project.setParameter(1, 'min', p.min, 'value', p.value, 'max', p.max, 'fit', p.fit, 'prior', p.prior_type, 'mu', p.mu, 'sigma', p.sigma);
else
project.addParameter(p.name, p.min, p.value, p.max, p.fit, p.prior_type, p.mu, p.sigma);
end
Expand Down Expand Up @@ -103,7 +103,8 @@
end


if json_struct.model == "domains"
if json_struct.calculation == "domains"
project.removeDomainRatio(1);
for i=1:length(json_struct.domain_ratios)
p = json_struct.domain_ratios(i);
project.addDomainRatio(p.name, p.min, p.value, p.max, p.fit, p.prior_type, p.mu, p.sigma);
Expand All @@ -119,6 +120,7 @@
'scalefactor', p.scalefactor, ...
'resample', p.resample, ...
'domainRatio', p.domain_ratio, ...
'resolution',p.resolution, ...
'model', p.model);
end
else
Expand All @@ -132,6 +134,7 @@
'bulkOut', p.bulk_out, ...
'scalefactor', p.scalefactor, ...
'resample', p.resample, ...
'resolution',p.resolution, ...
'model', p.model);
end
end
Expand Down
130 changes: 130 additions & 0 deletions utilities/misc/jsonToResults.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

function results = jsonToResults(filename)

% Loads in the results from a json results file, and converts it so the
% format required for matlabRAT. The conversion mainly means converting
% flattened array back to cells..

% Load in the json array
[path,filename,~] = fileparts(filename);
file = fullfile(path, append(filename, '.json'));
jsonRes = jsondecode(fileread(file));

% Go through converting all arrays to cells (where the arrays have been
% squeezed). If they are already cells, the subfunctions will just keep the
% original..
results.reflectivity = simArray2Cells(jsonRes.reflectivity);
results.simulation = simArray2Cells(jsonRes.simulation);
results.shiftedData = dataArray2Cells(jsonRes.shiftedData);
results.backgrounds = simArray2Cells(jsonRes.backgrounds);
results.resolutions = simArray2Cells(jsonRes.resolutions);
results.sldProfiles = sldArray2Cells(jsonRes.sldProfiles,numel(results.reflectivity));
results.layers = sldArray2Cells(jsonRes.layers,numel(results.reflectivity)); % Layers array is in the same format as the SLDs
results.resampledLayers = sldArray2Cells(jsonRes.resampledLayers,numel(results.reflectivity));

% These correctly load in as structs or arrays....
results.calculationResults = jsonRes.calculationResults;
results.contrastParams = jsonRes.contrastParams;
results.fitParams = jsonRes.fitParams';
results.fitNames = jsonRes.fitNames;

% contrastParams.resample has the wrong shape...
results.contrastParams.resample = results.contrastParams.resample';

% If we have Bayes results, we have extra work to do....
if isfield(jsonRes,'predictionIntervals')
results.predictionIntervals = jsonRes.predictionIntervals;
results.predictionIntervals.reflectivity = simArray2Cells(results.predictionIntervals.reflectivity);
results.predictionIntervals.sld = simArray2Cells(results.predictionIntervals.sld);

results.confidenceIntervals = jsonRes.confidenceIntervals;
results.dreamParams = jsonRes.dreamParams;
results.dreamOutput = jsonRes.dreamOutput;
results.nestedSamplerOutput = jsonRes.nestedSamplerOutput;
results.chain = jsonRes.chain;
end

end


% ----------------------------------------------------------

function outCell = sldArray2Cells(inArray,nContrasts)

% Check we don't already have cells..
if iscell(inArray)
nArrays = numel(inArray);
if nArrays == nContrasts % (not domains)
outCell = inArray;
else % ..domains - [n x 2] cell array
outCell = reshape(inArray,nContrasts,2);
end
else

% Get number of profiles...
nProfiles = size(inArray,1);

% Make them into cells....
for i = 1:nProfiles
thisSLD = inArray(i,:,:,:);
thisSLD = squeeze(thisSLD); % Collapse singleton dims....
outCell{i,1} = thisSLD;
end

% If domains, reorganise the cells...
if nProfiles > nContrasts
outCell = transpose(reshape(outCell,nContrasts,2));
end
end

% We have an annoying automatic reshape of single layers into columns.
% Fix this...
for i = 1:numel(outCell)
if iscolumn(outCell{i})
outCell{i} = transpose(outCell{i});
end
end

end

% --------------------------------------------------------------

function outCell = dataArray2Cells(inArray)

% Check we don't already have cells..
if iscell(inArray)
outCell = reshape(inArray,numel(inArray),1);
else

% Get number of profiles...
nProfiles = size(inArray,1);

for i = 1:nProfiles
thisDat = inArray(i,:,:,:);
thisDat = squeeze(thisDat); % Collapse singleton dims....
outCell{i,1} = thisDat;
end
end

end

% --------------------------------------------------------------

function outCell = simArray2Cells(inArray)

% Check we don't already have cells..
if iscell(inArray)
outCell = inArray;
else
% Get 3rd dimension of array...
dims = size(inArray,1);

for i = 1:dims
thisArray = inArray(i,:,:);
outCell{i,1} = squeeze(thisArray);
end
end

end

% ----------------------------------------------------------
Loading