diff --git a/API/controlsClass.m b/API/controlsClass.m index e79fa186d..620d09259 100644 --- a/API/controlsClass.m +++ b/API/controlsClass.m @@ -440,11 +440,8 @@ fwrite(fileID, true, 'uchar'); fclose(fileID); end - end - - %------------------------- Display Methods -------------------------- - methods (Access = protected) - function groups = getPropertyGroups(obj) + + function dispPropList = getAvailableFields(obj) masterPropList = struct('parallel', {obj.parallel},... 'procedure', {obj.procedure},... 'display', {obj.display},... @@ -499,20 +496,29 @@ 'boundHandling',... 'adaptPCR'}; + + dispPropList = masterPropList; + if strcmpi(obj.procedure, 'calculate') + dispPropList = rmfield(masterPropList, [deCell, simplexCell, nsCell, dreamCell, {'updatePlotFreq','updateFreq'}]); + elseif strcmpi(obj.procedure, 'simplex') + dispPropList = rmfield(masterPropList, [deCell, nsCell, dreamCell]); + elseif strcmpi(obj.procedure, 'de') + dispPropList = rmfield(masterPropList, [simplexCell, nsCell, dreamCell]); + % Add the update back... + elseif strcmpi(obj.procedure, 'ns') + dispPropList = rmfield(masterPropList, [simplexCell, deCell, dreamCell, {'updatePlotFreq','updateFreq'}]); + elseif strcmpi(obj.procedure, 'dream') + dispPropList = rmfield(masterPropList, [simplexCell, deCell, nsCell, {'updatePlotFreq','updateFreq'}]); + end + end + end + + %------------------------- Display Methods -------------------------- + methods (Access = protected) + function groups = getPropertyGroups(obj) + if isscalar(obj) - dispPropList = masterPropList; - if strcmpi(obj.procedure, 'calculate') - dispPropList = rmfield(masterPropList, [deCell, simplexCell, nsCell, dreamCell, {'updatePlotFreq','updateFreq'}]); - elseif strcmpi(obj.procedure, 'simplex') - dispPropList = rmfield(masterPropList, [deCell, nsCell, dreamCell]); - elseif strcmpi(obj.procedure, 'de') - dispPropList = rmfield(masterPropList, [simplexCell, nsCell, dreamCell]); - % Add the update back... - elseif strcmpi(obj.procedure, 'ns') - dispPropList = rmfield(masterPropList, [simplexCell, deCell, dreamCell, {'updatePlotFreq','updateFreq'}]); - elseif strcmpi(obj.procedure, 'dream') - dispPropList = rmfield(masterPropList, [simplexCell, deCell, nsCell, {'updatePlotFreq','updateFreq'}]); - end + dispPropList = obj.getAvailableFields(); groups = matlab.mixin.util.PropertyGroup(dispPropList); else groups = getPropertyGroups@matlab.mixin.CustomDisplay(obj); diff --git a/tests/testJsonToProject/testJsonToProject.m b/tests/testJsonToProject/testJsonToProject.m index 5f450b125..da87b8bb1 100644 --- a/tests/testJsonToProject/testJsonToProject.m +++ b/tests/testJsonToProject/testJsonToProject.m @@ -16,6 +16,12 @@ resultFile = {'result', ... 'result_bayes'} + + controlsParams = {{'calculate', 'numSimulationPoints', 100},... + {'simplex' 'xTolerance', 19},... + {'de' 'populationSize', 200},... + {'ns' 'nLive', 19},... + {'dream' 'nSamples', 200}} end methods(TestClassSetup) @@ -54,13 +60,9 @@ function testJsonResultConversion(testCase, resultFile) end end - function testJsonControlsConversion(testCase) + function testJsonControlsConversion(testCase, controlsParams) controls = controlsClass(); - controls.numSimulationPoints = 100; - controls.xTolerance = 19; - controls.populationSize = 200; - controls.nLive = 140; - controls.nSamples = 200; + controls.setProcedure(controlsParams{1}, controlsParams{2:end}) controlsToJson(controls, "test.json"); controls2 = jsonToControls("test.json"); diff --git a/utilities/misc/controlsToJson.m b/utilities/misc/controlsToJson.m index 95151476b..d8d28563a 100644 --- a/utilities/misc/controlsToJson.m +++ b/utilities/misc/controlsToJson.m @@ -2,7 +2,7 @@ function controlsToJson(controls,filename) % Saves the current controls block as a json.. -encoded = jsonencode(controls,ConvertInfAndNaN=false); +encoded = jsonencode(controls.getAvailableFields(),ConvertInfAndNaN=false); [path,filename,~] = fileparts(filename); fid = fullfile(path, append(filename, '.json')); diff --git a/utilities/misc/loadR2.m b/utilities/misc/loadR2.m new file mode 100644 index 000000000..6f91c6d56 --- /dev/null +++ b/utilities/misc/loadR2.m @@ -0,0 +1,40 @@ + +function [problem,varargout] = loadR2(dirName) + % Loads in a RasCAL2 project. + % + % Examples + % -------- + % >>> project = loadR2("r2_example"); % Load project from a given directory + % >>> [project, results] = loadR2("r2_example"); % Load project and result + % + % Parameters + % ---------- + % dirName : string or char array + % The path of the R2 project folder. + % + % Returns + % ------- + % project : projectClass + % An instance of the ``projectClass`` which should be equivalent to the given R2 problem. + % results : struct + % An optional output. A struct which contains the R2 results if present. + + arguments + dirName {mustBeTextScalar, mustBeNonempty} + end + % Load the project. + projectFile = fullfile(dirName,'project.json'); + problem = jsonToProject(projectFile); + + % If there is a second output, also load results. + if nargout > 1 + resultsFile = fullfile(dirName, 'results.json'); + if exist(resultsFile, 'file') == 2 + varargout{1} = jsonToResults(resultsFile); + else + varargout{1} = struct(); + warning("result.json was not found in the project."); + end + end + +end \ No newline at end of file diff --git a/utilities/misc/resultsToJson.m b/utilities/misc/resultsToJson.m index 814c456fb..c5108ea14 100644 --- a/utilities/misc/resultsToJson.m +++ b/utilities/misc/resultsToJson.m @@ -2,8 +2,21 @@ function resultsToJson(results,filename) % Encodes the results into a json file... +tmpResults = struct(); +for fn = fieldnames(results)' + tmpResults.(fn{1}) = results.(fn{1}); +end -encoded = jsonencode(results,ConvertInfAndNaN=false); +tmpResults.reflectivity = correctCellArray(tmpResults.reflectivity, false); +tmpResults.simulation = correctCellArray(tmpResults.simulation, false); +tmpResults.shiftedData = correctCellArray(tmpResults.shiftedData, false); +tmpResults.backgrounds = correctCellArray(tmpResults.backgrounds, false); +tmpResults.resolutions = correctCellArray(tmpResults.resolutions, false); +tmpResults.sldProfiles = correctCellArray(tmpResults.sldProfiles, true); +tmpResults.layers = correctCellArray(tmpResults.layers, true); +tmpResults.resampledLayers = correctCellArray(tmpResults.resampledLayers, true); + +encoded = jsonencode(tmpResults,ConvertInfAndNaN=false); [path,filename,~] = fileparts(filename); fid = fullfile(path, append(filename, '.json')); @@ -11,4 +24,25 @@ function resultsToJson(results,filename) fprintf(fid,'%s',encoded); fclose(fid); +end + + +function cellArray = correctCellArray(cellArray, domain) +% Corrects array with single row so its written as 2D array in json + [row, col] = size(cellArray, [1, 2]); + for i=1:row + for j=1:col + if size(cellArray{i, j}, 1) == 1 + cellArray{i, j} = {cellArray{i, j}}; + end + end + end + if domain && size(cellArray, 2) == 1 + for i=1:row + cellArray{i, 1} = {cellArray{i, 1}}; + end + end + if domain && size(cellArray, 1) == 1 + cellArray = {cellArray}; + end end \ No newline at end of file diff --git a/utilities/misc/saveR2.m b/utilities/misc/saveR2.m new file mode 100644 index 000000000..25db503a3 --- /dev/null +++ b/utilities/misc/saveR2.m @@ -0,0 +1,55 @@ + +function saveR2(dirName, problem, results, controls) + % Saves the current project in RasCAL2 format. + % + % Examples + % -------- + % >>> saveR2("r2_example", problem, results, controls); % Save project to a given directory + % + % Parameters + % ---------- + % dirName : string or char array + % The path to save the R2 project files. + % project : projectClass + % An instance of the ``projectClass`` which should be saved. + % results : struct + % A result struct which should be saved. + % controls : controlsClass + % An instance of the ``controlsClass`` which should be saved. + + arguments + dirName {mustBeTextScalar, mustBeNonempty} + problem {mustBeA(problem, 'projectClass')} + results + controls {mustBeA(controls, 'controlsClass')} + end + + tmpProblem = problem.clone(); + % Check whether the save directory exists, create it if necessary + if exist(dirName,'dir') ~= 7 + mkdir(dirName); + end + currentDir = pwd; % Save the current directory name + + % Do some custom file housekeeping. We need to copy these to our new + % folder if there are any.. + filesTable = tmpProblem.customFile.varTable; + for i = 1:size(filesTable,1) + thisFile = fullfile(filesTable{i,'Path'},filesTable{i,'Filename'}); + copyfile(thisFile,dirName) + + % Change the paths of our custom files in projectClass to point to our + % new files.. + tmpProblem.setCustomFile(i,'path',fullfile(currentDir,dirName)); + end + + % Go to our new directory and export the jsons... + cd(dirName); + projectToJson(problem,'project.json'); + resultsToJson(results,'results.json'); + controlsToJson(controls,'controls.json'); + + % Go back to our original dir and we are done... + cd(currentDir); + +end \ No newline at end of file