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