-
Notifications
You must be signed in to change notification settings - Fork 6
Added utilities for saving and loading RasCAL2 files.... #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
e68ef7f
604cad4
a1ff04e
7ead514
eda2a53
2fc5abd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,22 @@ | |
|
|
||
| resultFile = {'result', ... | ||
| 'result_bayes'} | ||
|
|
||
| controlsParams = {{'calculate', 'numSimulationPoints', 100},... | ||
| {'simplex' 'xTolerance', 19},... | ||
| {'de' 'populationSize', 200},... | ||
| {'ns' 'nLive', 19},... | ||
| {'dream' 'nSamples', 200}} | ||
| r2Tests = {{'normalReflectivity', 'standardLayers', 'standardLayersDSPCScript'},... | ||
| {'domains', 'customLayers', 'domainsCustomLayersScript'},... | ||
| {'domains', 'customXY', 'domainsCustomXYScript'}} | ||
| end | ||
|
|
||
| methods(TestClassSetup) | ||
| function addDataPath(testCase) | ||
| import matlab.unittest.fixtures.PathFixture | ||
| path = fullfile(getappdata(0, 'root'), 'tests', 'testJsonToProject'); | ||
| testCase.applyFixture(PathFixture(path)) | ||
| testCase.applyFixture(PathFixture(path)); | ||
| end | ||
| function setWorkingFolder(testCase) | ||
| import matlab.unittest.fixtures.WorkingFolderFixture; | ||
|
|
@@ -54,13 +63,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"); | ||
|
|
@@ -69,6 +74,54 @@ function testJsonControlsConversion(testCase) | |
| for i = 1:length(props) | ||
| testCase.verifyEqual(controls.(props{i}), controls2.(props{i})); | ||
| end | ||
| end | ||
|
|
||
| function testR2Conversion(testCase, r2Tests) | ||
| scriptName = r2Tests{3}; | ||
| outFolder = ['save_' scriptName]; | ||
|
|
||
| copyfile(fullfile(getappdata(0, 'root'), 'examples', r2Tests{1}, r2Tests{2}), ... | ||
| fullfile(pwd)) | ||
| evalc(scriptName); | ||
|
|
||
| controls = controlsClass(); | ||
| [~, project, result] = evalc('RAT(problem, controls);'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: instead of just: It works faster and much more clear code.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to hide output, but I can do the same with controls.display = 'off' so I will replace |
||
|
|
||
| saveR2(outFolder, project, result, controls) | ||
| testCase.verifyEqual(exist(fullfile(pwd, outFolder, 'results.json'), 'file'), 2) | ||
| testCase.verifyEqual(exist(fullfile(pwd, outFolder, 'project.json'), 'file'), 2) | ||
| testCase.verifyEqual(exist(fullfile(pwd, outFolder, 'controls.json'), 'file'), 2) | ||
| for i=1:project.customFile.rowCount | ||
| customFile = project.customFile.varTable{i, 2}; | ||
| testCase.verifyEqual(exist(fullfile(pwd, outFolder, customFile), 'file'), 2) | ||
| end | ||
|
|
||
| [project2, result2] = loadR2(outFolder); | ||
| props = properties(project); | ||
| for i = 1:length(props) | ||
| % verifies the problem name, model type and geometry | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Request if I understand it correctly: All Test between
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed now, I refactored and forgot to remove loop |
||
| testCase.verifyEqual(project.experimentName, project2.experimentName); | ||
| testCase.verifyEqual(project.modelType, project2.modelType); | ||
| testCase.verifyEqual(project.geometry, project2.geometry); | ||
|
|
||
| % verifies the count of problem properties | ||
| testCase.verifyEqual(project.contrasts.numberOfContrasts, project2.contrasts.numberOfContrasts); | ||
| testCase.verifyEqual(project.parameters.rowCount, project2.parameters.rowCount); | ||
| testCase.verifyEqual(project.bulkOut.rowCount, project2.bulkOut.rowCount); | ||
| testCase.verifyEqual(project.background.backgrounds.rowCount, project2.background.backgrounds.rowCount); | ||
| testCase.verifyEqual(project.data.rowCount, project2.data.rowCount); | ||
| testCase.verifyEqual(project.scalefactors.rowCount, project2.scalefactors.rowCount); | ||
| if isa(project.layers, 'layersClass') | ||
| testCase.verifyEqual(project.layers.rowCount, project2.layers.rowCount); | ||
| end | ||
| testCase.verifyEqual(project.bulkIn.rowCount, project2.bulkIn.rowCount); | ||
| end | ||
|
|
||
| props = properties(result); | ||
| for i = 1:length(props) | ||
| testCase.verifyEqual(result.(props{i}), result2.(props{i})); | ||
| end | ||
| close all | ||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
request:
This may be previous contribution, but this test tests nothing. Structure contains no properties.
row 60 should be:
And tests are failing if you do this. Seems on shape only but...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed