Skip to content

Commit 366ddcb

Browse files
committed
Fix bug in controls and result json conversion, adds comments
1 parent a1ff04e commit 366ddcb

6 files changed

Lines changed: 173 additions & 74 deletions

File tree

API/controlsClass.m

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,8 @@
440440
fwrite(fileID, true, 'uchar');
441441
fclose(fileID);
442442
end
443-
end
444-
445-
%------------------------- Display Methods --------------------------
446-
methods (Access = protected)
447-
function groups = getPropertyGroups(obj)
443+
444+
function dispPropList = getAvailableFields(obj)
448445
masterPropList = struct('parallel', {obj.parallel},...
449446
'procedure', {obj.procedure},...
450447
'display', {obj.display},...
@@ -499,20 +496,29 @@
499496
'boundHandling',...
500497
'adaptPCR'};
501498

499+
500+
dispPropList = masterPropList;
501+
if strcmpi(obj.procedure, 'calculate')
502+
dispPropList = rmfield(masterPropList, [deCell, simplexCell, nsCell, dreamCell, {'updatePlotFreq','updateFreq'}]);
503+
elseif strcmpi(obj.procedure, 'simplex')
504+
dispPropList = rmfield(masterPropList, [deCell, nsCell, dreamCell]);
505+
elseif strcmpi(obj.procedure, 'de')
506+
dispPropList = rmfield(masterPropList, [simplexCell, nsCell, dreamCell]);
507+
% Add the update back...
508+
elseif strcmpi(obj.procedure, 'ns')
509+
dispPropList = rmfield(masterPropList, [simplexCell, deCell, dreamCell, {'updatePlotFreq','updateFreq'}]);
510+
elseif strcmpi(obj.procedure, 'dream')
511+
dispPropList = rmfield(masterPropList, [simplexCell, deCell, nsCell, {'updatePlotFreq','updateFreq'}]);
512+
end
513+
end
514+
end
515+
516+
%------------------------- Display Methods --------------------------
517+
methods (Access = protected)
518+
function groups = getPropertyGroups(obj)
519+
502520
if isscalar(obj)
503-
dispPropList = masterPropList;
504-
if strcmpi(obj.procedure, 'calculate')
505-
dispPropList = rmfield(masterPropList, [deCell, simplexCell, nsCell, dreamCell, {'updatePlotFreq','updateFreq'}]);
506-
elseif strcmpi(obj.procedure, 'simplex')
507-
dispPropList = rmfield(masterPropList, [deCell, nsCell, dreamCell]);
508-
elseif strcmpi(obj.procedure, 'de')
509-
dispPropList = rmfield(masterPropList, [simplexCell, nsCell, dreamCell]);
510-
% Add the update back...
511-
elseif strcmpi(obj.procedure, 'ns')
512-
dispPropList = rmfield(masterPropList, [simplexCell, deCell, dreamCell, {'updatePlotFreq','updateFreq'}]);
513-
elseif strcmpi(obj.procedure, 'dream')
514-
dispPropList = rmfield(masterPropList, [simplexCell, deCell, nsCell, {'updatePlotFreq','updateFreq'}]);
515-
end
521+
dispPropList = obj.getAvailableFields();
516522
groups = matlab.mixin.util.PropertyGroup(dispPropList);
517523
else
518524
groups = getPropertyGroups@matlab.mixin.CustomDisplay(obj);

tests/testJsonToProject/testJsonToProject.m

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
resultFile = {'result', ...
1818
'result_bayes'}
19+
20+
controlsParams = {{'calculate', 'numSimulationPoints', 100},...
21+
{'simplex' 'xTolerance', 19},...
22+
{'de' 'populationSize', 200},...
23+
{'ns' 'nLive', 19},...
24+
{'dream' 'nSamples', 200}}
1925
end
2026

2127
methods(TestClassSetup)
@@ -54,13 +60,9 @@ function testJsonResultConversion(testCase, resultFile)
5460
end
5561
end
5662

57-
function testJsonControlsConversion(testCase)
63+
function testJsonControlsConversion(testCase, controlsParams)
5864
controls = controlsClass();
59-
controls.numSimulationPoints = 100;
60-
controls.xTolerance = 19;
61-
controls.populationSize = 200;
62-
controls.nLive = 140;
63-
controls.nSamples = 200;
65+
controls.setProcedure(controlsParams{1}, controlsParams{2:end});
6466

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

utilities/misc/controlsToJson.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
function controlsToJson(controls,filename)
33

44
% Saves the current controls block as a json..
5-
encoded = jsonencode(controls,ConvertInfAndNaN=false);
5+
encoded = jsonencode(controls.getAvailableFields(),ConvertInfAndNaN=false);
66

77
[path,filename,~] = fileparts(filename);
88
fid = fullfile(path, append(filename, '.json'));

utilities/misc/loadR2.m

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11

22
function [problem,varargout] = loadR2(dirName)
3-
% Loads in a RasCAL2 project...
4-
5-
% Load the project...
6-
projectFile = fullfile(dirName,'project.json');
7-
problem = jsonToProject(projectFile);
8-
9-
% If there is a second argument, also load results..
10-
if nargout > 1
11-
resultsFile = fullfile(dirName, 'results.json');
12-
varargout{1} = jsonToResults(resultsFile);
13-
end
3+
% Loads in a RasCAL2 project.
4+
%
5+
% Examples
6+
% --------
7+
% >>> project = loadR2("r2_example"); % Load project from a given directory
8+
% >>> [project, results] = loadR2("r2_example"); % Load project and result
9+
%
10+
% Parameters
11+
% ----------
12+
% dirName : string or char array
13+
% The path of the R2 project folder.
14+
%
15+
% Returns
16+
% -------
17+
% project : projectClass
18+
% An instance of the ``projectClass`` which should be equivalent to the given R2 problem.
19+
% results : struct
20+
% An optional output. A struct which contains the R2 results if present.
21+
22+
arguments
23+
dirName {mustBeTextScalar, mustBeNonempty}
24+
end
25+
% Load the project.
26+
projectFile = fullfile(dirName,'project.json');
27+
problem = jsonToProject(projectFile);
28+
29+
% If there is a second output, also load results.
30+
if nargout > 1
31+
resultsFile = fullfile(dirName, 'results.json');
32+
if exist(resultsFile, 'file') == 2
33+
varargout{1} = jsonToResults(resultsFile);
34+
else
35+
varargout{1} = struct();
36+
warning("result.json was not found in the project.");
37+
end
38+
end
1439

1540
end

utilities/misc/resultsToJson.m

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,64 @@
22
function resultsToJson(results,filename)
33

44
% Encodes the results into a json file...
5+
tmpResults = struct();
6+
for fn = fieldnames(results)'
7+
tmpResults.(fn{1}) = results.(fn{1});
8+
end
9+
10+
tmpResults.reflectivity = correctCellArray(tmpResults.reflectivity);
11+
tmpResults.simulation = correctCellArray(tmpResults.simulation);
12+
tmpResults.shiftedData = correctCellArray(tmpResults.shiftedData);
13+
tmpResults.backgrounds = correctCellArray(tmpResults.backgrounds);
14+
tmpResults.resolutions = correctCellArray(tmpResults.resolutions );
15+
tmpResults.sldProfiles = makeCellJson(tmpResults.sldProfiles);
16+
tmpResults.layers = makeCellJson(tmpResults.layers);
17+
tmpResults.resampledLayers = makeCellJson(tmpResults.resampledLayers);
18+
19+
encoded = jsonencode(tmpResults,ConvertInfAndNaN=false);
20+
encoded = strrep(encoded, '"[', '[');
21+
encoded = strrep(encoded, ']"', ']');
522

6-
encoded = jsonencode(results,ConvertInfAndNaN=false);
723

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

30+
end
31+
32+
function outputArray = makeCellJson(cellArray)
33+
% The jsonencode function flattens 2d cell arrays this is a workaround to
34+
% avoid flattening by converting to a string array with is not flattened.
35+
[row, col] = size(cellArray, [1, 2]);
36+
outputArray = strings([row, col]);
37+
for i=1:row
38+
for j=1:col
39+
entry = cellArray{i, j};
40+
if size(entry, 1) == 1
41+
entry = {entry};
42+
end
43+
if col == 1
44+
entry = {entry};
45+
end
46+
outputArray(i, j) = jsonencode(entry);
47+
end
48+
end
49+
if row == 1
50+
outputArray = {outputArray};
51+
end
52+
53+
end
54+
55+
function cellArray = correctCellArray(cellArray)
56+
% Corrects array with single row so its written as 2D array in json
57+
[row, col] = size(cellArray, [1, 2]);
58+
for i=1:row
59+
for j=1:col
60+
if size(cellArray{i, j}, 1) == 1
61+
cellArray{i, j} = {cellArray{i, j}};
62+
end
63+
end
64+
end
1465
end

utilities/misc/saveR2.m

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
11

2-
function saveR2(dirName,problem,results,controls)
3-
4-
% Saves the current project in RasCAL2 format...
5-
6-
arguments
7-
dirName {mustBeText}
8-
problem {mustBeA(problem, 'projectClass')}
9-
results
10-
controls {mustBeA(controls, 'controlsClass')}
11-
end
12-
13-
% Check whether the save directory exists, create it if necessary
14-
if exist(dirName,'dir') ~= 7
15-
mkdir(dirName);
16-
end
17-
currentDir = pwd; % Save the current directory name
18-
19-
% Do some custom file housekeeping. We need to copy these to our new
20-
% folder if there are any..
21-
filesTable = problem.customFile.varTable;
22-
for i = size(filesTable,1)
23-
thisFile = fullfile(filesTable{i,'Path'},filesTable{i,'Filename'});
24-
copyfile(thisFile,dirName)
25-
26-
% Change the paths of our custom files in projectClass to point to our
27-
% new files..
28-
problem.setCustomFile(i,'path',fullfile(currentDir,dirName));
29-
end
30-
31-
% Go to our new directory and export the jsons...
32-
cd(dirName);
33-
projectToJson(problem,'project.json');
34-
resultsToJson(results,'results.json');
35-
controlsToJson(controls,'controls.json');
36-
37-
% Go back to our original dir and we are done...
38-
cd(currentDir);
2+
function saveR2(dirName, problem, results, controls)
3+
% Saves the current project in RasCAL2 format.
4+
%
5+
% Examples
6+
% --------
7+
% >>> saveR2("r2_example", problem, results, controls); % Save project to a given directory
8+
%
9+
% Parameters
10+
% ----------
11+
% dirName : string or char array
12+
% The path to save the R2 project files.
13+
% project : projectClass
14+
% An instance of the ``projectClass`` which should be saved.
15+
% results : struct
16+
% A result struct which should be saved.
17+
% controls : controlsClass
18+
% An instance of the ``controlsClass`` which should be saved.
19+
20+
arguments
21+
dirName {mustBeTextScalar, mustBeNonempty}
22+
problem {mustBeA(problem, 'projectClass')}
23+
results
24+
controls {mustBeA(controls, 'controlsClass')}
25+
end
26+
27+
tmpProblem = problem.clone();
28+
% Check whether the save directory exists, create it if necessary
29+
if exist(dirName,'dir') ~= 7
30+
mkdir(dirName);
31+
end
32+
currentDir = pwd; % Save the current directory name
33+
34+
% Do some custom file housekeeping. We need to copy these to our new
35+
% folder if there are any..
36+
filesTable = tmpProblem.customFile.varTable;
37+
for i = 1:size(filesTable,1)
38+
thisFile = fullfile(filesTable{i,'Path'},filesTable{i,'Filename'});
39+
copyfile(thisFile,dirName)
40+
41+
% Change the paths of our custom files in projectClass to point to our
42+
% new files..
43+
tmpProblem.setCustomFile(i,'path',fullfile(currentDir,dirName));
44+
end
45+
46+
% Go to our new directory and export the jsons...
47+
cd(dirName);
48+
projectToJson(problem,'project.json');
49+
resultsToJson(results,'results.json');
50+
controlsToJson(controls,'controls.json');
51+
52+
% Go back to our original dir and we are done...
53+
cd(currentDir);
3954

4055
end

0 commit comments

Comments
 (0)