Skip to content

Commit e6640d3

Browse files
authored
Json convert (#406)
1 parent 8a4c1a3 commit e6640d3

9 files changed

Lines changed: 554 additions & 4 deletions

File tree

‎tests/testJsonToProject/result.json‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

‎tests/testJsonToProject/result_bayes.json‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

‎tests/testJsonToProject/testJsonToProject.m‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
'DSPC_custom_layers', ...
1414
'DSPC_custom_XY', ...
1515
'DSPC_standard_layers'}
16+
17+
resultFile = {'result', ...
18+
'result_bayes'}
1619
end
1720

1821
methods(TestClassSetup)
@@ -28,9 +31,44 @@ function setWorkingFolder(testCase)
2831
end
2932

3033
% test that JSON to Project successfully converts projects
31-
methods (Test, ParameterCombination='exhaustive')
34+
methods (Test)
3235
function testJsonConversion(testCase, file)
33-
jsonToProject(append(file, ".json"));
36+
project = jsonToProject(append(file, ".json"));
37+
projectToJson(project, "test.json");
38+
project2 = jsonToProject("test.json");
39+
40+
props = properties(project);
41+
for i = 1:length(props)
42+
testCase.verifyEqual(project.(props{i}), project2.(props{i}));
43+
end
44+
end
45+
46+
function testJsonResultConversion(testCase, resultFile)
47+
result = jsonToResults(append(resultFile, ".json"));
48+
resultsToJson(result, "test.json");
49+
result2 = jsonToResults("test.json");
50+
51+
props = properties(result);
52+
for i = 1:length(props)
53+
testCase.verifyEqual(result.(props{i}), result2.(props{i}));
54+
end
55+
end
56+
57+
function testJsonControlsConversion(testCase)
58+
controls = controlsClass();
59+
controls.numSimulationPoints = 100;
60+
controls.xTolerance = 19;
61+
controls.populationSize = 200;
62+
controls.nLive = 140;
63+
controls.nSamples = 200;
64+
65+
controlsToJson(controls, "test.json");
66+
controls2 = jsonToControls("test.json");
67+
68+
props = properties(controls);
69+
for i = 1:length(props)
70+
testCase.verifyEqual(controls.(props{i}), controls2.(props{i}));
71+
end
3472
end
3573
end
3674

‎utilities/misc/controlsToJson.m‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
function controlsToJson(controls,filename)
3+
4+
% Saves the current controls block as a json..
5+
encoded = jsonencode(controls,ConvertInfAndNaN=false);
6+
encoded = replace(encoded,'Infinity','Inf');
7+
8+
[path,filename,~] = fileparts(filename);
9+
fid = fullfile(path, append(filename, '.json'));
10+
fid = fopen(fid,'w');
11+
fprintf(fid,'%s',encoded);
12+
fclose(fid);
13+
14+
end

‎utilities/misc/jsonToControls.m‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
function controls = jsonToControls(filename)
3+
4+
% Loads in a controls json, and contructs the controlsClass from it...
5+
6+
% Load the file...
7+
jsonControls = jsondecode(fileread(filename));
8+
9+
% Make a controlsClass()
10+
controls = controlsClass();
11+
12+
fieldNames = fieldnames(jsonControls);
13+
for i = 1:length(fieldNames)
14+
controls.(fieldNames{i}) = jsonControls.(fieldNames{i});
15+
end
16+
17+
end

‎utilities/misc/jsonToProject.m‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
for i=1:length(json_struct.parameters)
2424
p = json_struct.parameters(i);
2525
if p.name == "Substrate Roughness"
26-
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);
26+
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);
2727
else
2828
project.addParameter(p.name, p.min, p.value, p.max, p.fit, p.prior_type, p.mu, p.sigma);
2929
end
@@ -103,7 +103,8 @@
103103
end
104104

105105

106-
if json_struct.model == "domains"
106+
if json_struct.calculation == "domains"
107+
project.removeDomainRatio(1);
107108
for i=1:length(json_struct.domain_ratios)
108109
p = json_struct.domain_ratios(i);
109110
project.addDomainRatio(p.name, p.min, p.value, p.max, p.fit, p.prior_type, p.mu, p.sigma);
@@ -119,6 +120,7 @@
119120
'scalefactor', p.scalefactor, ...
120121
'resample', p.resample, ...
121122
'domainRatio', p.domain_ratio, ...
123+
'resolution',p.resolution, ...
122124
'model', p.model);
123125
end
124126
else
@@ -132,6 +134,7 @@
132134
'bulkOut', p.bulk_out, ...
133135
'scalefactor', p.scalefactor, ...
134136
'resample', p.resample, ...
137+
'resolution',p.resolution, ...
135138
'model', p.model);
136139
end
137140
end

‎utilities/misc/jsonToResults.m‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
2+
function results = jsonToResults(filename)
3+
4+
% Loads in the results from a json results file, and converts it so the
5+
% format required for matlabRAT. The conversion mainly means converting
6+
% flattened array back to cells..
7+
8+
% Load in the json array
9+
[path,filename,~] = fileparts(filename);
10+
file = fullfile(path, append(filename, '.json'));
11+
jsonRes = jsondecode(fileread(file));
12+
13+
% Go through converting all arrays to cells (where the arrays have been
14+
% squeezed). If they are already cells, the subfunctions will just keep the
15+
% original..
16+
results.reflectivity = simArray2Cells(jsonRes.reflectivity);
17+
results.simulation = simArray2Cells(jsonRes.simulation);
18+
results.shiftedData = dataArray2Cells(jsonRes.shiftedData);
19+
results.backgrounds = simArray2Cells(jsonRes.backgrounds);
20+
results.resolutions = simArray2Cells(jsonRes.resolutions);
21+
results.sldProfiles = sldArray2Cells(jsonRes.sldProfiles,numel(results.reflectivity));
22+
results.layers = sldArray2Cells(jsonRes.layers,numel(results.reflectivity)); % Layers array is in the same format as the SLDs
23+
results.resampledLayers = sldArray2Cells(jsonRes.resampledLayers,numel(results.reflectivity));
24+
25+
% These correctly load in as structs or arrays....
26+
results.calculationResults = jsonRes.calculationResults;
27+
results.contrastParams = jsonRes.contrastParams;
28+
results.fitParams = jsonRes.fitParams';
29+
results.fitNames = jsonRes.fitNames;
30+
31+
% contrastParams.resample has the wrong shape...
32+
results.contrastParams.resample = results.contrastParams.resample';
33+
34+
% If we have Bayes results, we have extra work to do....
35+
if isfield(jsonRes,'predictionIntervals')
36+
results.predictionIntervals = jsonRes.predictionIntervals;
37+
results.predictionIntervals.reflectivity = simArray2Cells(results.predictionIntervals.reflectivity);
38+
results.predictionIntervals.sld = simArray2Cells(results.predictionIntervals.sld);
39+
40+
results.confidenceIntervals = jsonRes.confidenceIntervals;
41+
results.dreamParams = jsonRes.dreamParams;
42+
results.dreamOutput = jsonRes.dreamOutput;
43+
results.nestedSamplerOutput = jsonRes.nestedSamplerOutput;
44+
results.chain = jsonRes.chain;
45+
end
46+
47+
end
48+
49+
50+
% ----------------------------------------------------------
51+
52+
function outCell = sldArray2Cells(inArray,nContrasts)
53+
54+
% Check we don't already have cells..
55+
if iscell(inArray)
56+
nArrays = numel(inArray);
57+
if nArrays == nContrasts % (not domains)
58+
outCell = inArray;
59+
else % ..domains - [n x 2] cell array
60+
outCell = reshape(inArray,nContrasts,2);
61+
end
62+
else
63+
64+
% Get number of profiles...
65+
nProfiles = size(inArray,1);
66+
67+
% Make them into cells....
68+
for i = 1:nProfiles
69+
thisSLD = inArray(i,:,:,:);
70+
thisSLD = squeeze(thisSLD); % Collapse singleton dims....
71+
outCell{i,1} = thisSLD;
72+
end
73+
74+
% If domains, reorganise the cells...
75+
if nProfiles > nContrasts
76+
outCell = transpose(reshape(outCell,nContrasts,2));
77+
end
78+
end
79+
80+
% We have an annoying automatic reshape of single layers into columns.
81+
% Fix this...
82+
for i = 1:numel(outCell)
83+
if iscolumn(outCell{i})
84+
outCell{i} = transpose(outCell{i});
85+
end
86+
end
87+
88+
end
89+
90+
% --------------------------------------------------------------
91+
92+
function outCell = dataArray2Cells(inArray)
93+
94+
% Check we don't already have cells..
95+
if iscell(inArray)
96+
outCell = reshape(inArray,numel(inArray),1);
97+
else
98+
99+
% Get number of profiles...
100+
nProfiles = size(inArray,1);
101+
102+
for i = 1:nProfiles
103+
thisDat = inArray(i,:,:,:);
104+
thisDat = squeeze(thisDat); % Collapse singleton dims....
105+
outCell{i,1} = thisDat;
106+
end
107+
end
108+
109+
end
110+
111+
% --------------------------------------------------------------
112+
113+
function outCell = simArray2Cells(inArray)
114+
115+
% Check we don't already have cells..
116+
if iscell(inArray)
117+
outCell = inArray;
118+
else
119+
% Get 3rd dimension of array...
120+
dims = size(inArray,1);
121+
122+
for i = 1:dims
123+
thisArray = inArray(i,:,:);
124+
outCell{i,1} = squeeze(thisArray);
125+
end
126+
end
127+
128+
end
129+
130+
% ----------------------------------------------------------

0 commit comments

Comments
 (0)