-
Notifications
You must be signed in to change notification settings - Fork 4
Json convert #406
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
Merged
Merged
Json convert #406
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a8e1cad
Made a start on 'projectToJson' via 'toStruct'. Realised using indivi…
arwelHughes 5ba81dd
Done proj2json except contrasts
arwelHughes cc569a2
projectToJson matches the supplied json, but differs from an existing…
arwelHughes 3a6647c
Fixed testMain type. Passes when comparing to json, fails when compar…
arwelHughes 7c8a906
Both directions json convert now works (and running) for Standard Lay…
arwelHughes 59de20a
Added custom file support
arwelHughes 700b450
Added underscore to 'function_name' field is custom files table (or c…
arwelHughes 3df0ffb
Madified json encoding
arwelHughes 839a601
Added jsonToResults.m
arwelHughes 5bf55be
Results now working for Bayes
arwelHughes a0ed73d
Now added controlsClass()/Json routines
arwelHughes a0b520c
Staging commit
arwelHughes a203075
projectToJson now handles domains
arwelHughes a4e273c
Staging commit
arwelHughes adbe2fc
Results reload finally seems to be working
arwelHughes 5a8ee52
Moved json savers and loaders to /utilities/misc
arwelHughes 8a849c6
Conversions now seem to be working correctly
arwelHughes 1da8223
Removes unneeded files and adds tests
StephenNneji cdbf978
Addresses review comments
StephenNneji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
|
|
||
| function controlsToJson(controls,filename) | ||
|
|
||
| % Saves the current controls block as a json.. | ||
| encoded = jsonencode(controls,ConvertInfAndNaN=false); | ||
| encoded = replace(encoded,'Infinity','Inf'); | ||
|
|
||
| [path,filename,~] = fileparts(filename); | ||
| fid = fullfile(path, append(filename, '.json')); | ||
| fid = fopen(fid,'w'); | ||
| fprintf(fid,'%s',encoded); | ||
| fclose(fid); | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
| function controls = jsonToControls(filename) | ||
|
|
||
| % Loads in a controls json, and contructs the controlsClass from it... | ||
|
|
||
| % Load the file... | ||
| jsonControls = jsondecode(fileread(filename)); | ||
|
|
||
| % Make a controlsClass() | ||
| controls = controlsClass(); | ||
|
|
||
| fieldNames = fieldnames(jsonControls); | ||
| for i = 1:length(fieldNames) | ||
| controls.(fieldNames{i}) = jsonControls.(fieldNames{i}); | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
|
|
||
| function results = jsonToResults(filename) | ||
|
|
||
| % Loads in the results from a json results file, and converts it so the | ||
| % format required for matlabRAT. The conversion mainly means converting | ||
| % flattened array back to cells.. | ||
|
|
||
| % Load in the json array | ||
| [path,filename,~] = fileparts(filename); | ||
| file = fullfile(path, append(filename, '.json')); | ||
| jsonRes = jsondecode(fileread(file)); | ||
|
|
||
| % Go through converting all arrays to cells (where the arrays have been | ||
| % squeezed). If they are already cells, the subfunctions will just keep the | ||
| % original.. | ||
| results.reflectivity = simArray2Cells(jsonRes.reflectivity); | ||
| results.simulation = simArray2Cells(jsonRes.simulation); | ||
| results.shiftedData = dataArray2Cells(jsonRes.shiftedData); | ||
| results.backgrounds = simArray2Cells(jsonRes.backgrounds); | ||
| results.resolutions = simArray2Cells(jsonRes.resolutions); | ||
| results.sldProfiles = sldArray2Cells(jsonRes.sldProfiles,numel(results.reflectivity)); | ||
| results.layers = sldArray2Cells(jsonRes.layers,numel(results.reflectivity)); % Layers array is in the same format as the SLDs | ||
| results.resampledLayers = sldArray2Cells(jsonRes.resampledLayers,numel(results.reflectivity)); | ||
|
|
||
| % These correctly load in as structs or arrays.... | ||
| results.calculationResults = jsonRes.calculationResults; | ||
| results.contrastParams = jsonRes.contrastParams; | ||
| results.fitParams = jsonRes.fitParams'; | ||
| results.fitNames = jsonRes.fitNames; | ||
|
|
||
| % contrastParams.resample has the wrong shape... | ||
| results.contrastParams.resample = results.contrastParams.resample'; | ||
|
|
||
| % If we have Bayes results, we have extra work to do.... | ||
| if isfield(jsonRes,'predictionIntervals') | ||
| results.predictionIntervals = jsonRes.predictionIntervals; | ||
| results.predictionIntervals.reflectivity = simArray2Cells(results.predictionIntervals.reflectivity); | ||
| results.predictionIntervals.sld = simArray2Cells(results.predictionIntervals.sld); | ||
|
|
||
| results.confidenceIntervals = jsonRes.confidenceIntervals; | ||
| results.dreamParams = jsonRes.dreamParams; | ||
| results.dreamOutput = jsonRes.dreamOutput; | ||
| results.nestedSamplerOutput = jsonRes.nestedSamplerOutput; | ||
| results.chain = jsonRes.chain; | ||
| end | ||
|
|
||
| end | ||
|
|
||
|
|
||
| % ---------------------------------------------------------- | ||
|
|
||
| function outCell = sldArray2Cells(inArray,nContrasts) | ||
|
|
||
| % Check we don't already have cells.. | ||
| if iscell(inArray) | ||
| nArrays = numel(inArray); | ||
| if nArrays == nContrasts % (not domains) | ||
| outCell = inArray; | ||
| else % ..domains - [n x 2] cell array | ||
| outCell = reshape(inArray,nContrasts,2); | ||
| end | ||
| else | ||
|
|
||
| % Get number of profiles... | ||
| nProfiles = size(inArray,1); | ||
|
|
||
| % Make them into cells.... | ||
| for i = 1:nProfiles | ||
| thisSLD = inArray(i,:,:,:); | ||
| thisSLD = squeeze(thisSLD); % Collapse singleton dims.... | ||
| outCell{i,1} = thisSLD; | ||
| end | ||
|
|
||
| % If domains, reorganise the cells... | ||
| if nProfiles > nContrasts | ||
| outCell = transpose(reshape(outCell,nContrasts,2)); | ||
| end | ||
| end | ||
|
|
||
| % We have an annoying automatic reshape of single layers into columns. | ||
| % Fix this... | ||
| for i = 1:numel(outCell) | ||
| if iscolumn(outCell{i}) | ||
| outCell{i} = transpose(outCell{i}); | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| % -------------------------------------------------------------- | ||
|
|
||
| function outCell = dataArray2Cells(inArray) | ||
|
|
||
| % Check we don't already have cells.. | ||
| if iscell(inArray) | ||
| outCell = reshape(inArray,numel(inArray),1); | ||
| else | ||
|
|
||
| % Get number of profiles... | ||
| nProfiles = size(inArray,1); | ||
|
|
||
| for i = 1:nProfiles | ||
| thisDat = inArray(i,:,:,:); | ||
| thisDat = squeeze(thisDat); % Collapse singleton dims.... | ||
| outCell{i,1} = thisDat; | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| % -------------------------------------------------------------- | ||
|
|
||
| function outCell = simArray2Cells(inArray) | ||
|
|
||
| % Check we don't already have cells.. | ||
| if iscell(inArray) | ||
| outCell = inArray; | ||
| else | ||
| % Get 3rd dimension of array... | ||
| dims = size(inArray,1); | ||
|
|
||
| for i = 1:dims | ||
| thisArray = inArray(i,:,:); | ||
| outCell{i,1} = squeeze(thisArray); | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| % ---------------------------------------------------------- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.