Skip to content
Snippets Groups Projects
Commit 46b51124 authored by Lemmer, Jan's avatar Lemmer, Jan
Browse files

Merge branch '40-feature-advanced-meta-data-in-json-file' into 'development'

Add config Class, Implement Wizard

See merge request !29
parents d304128d 75e1a369
No related branches found
No related tags found
2 merge requests!30Fix CI,!29Add config Class, Implement Wizard
Pipeline #591886 failed
classdef config < handle
%CONFIG class handles methoths and attributes related to the config
%file
% Detailed explanation goes here
% handle class used for dynamicy property updates
properties (SetAccess = protected)
configData
configFileName
end
methods
function obj = config(configFileName)
%CONFIG Construct an instance of this class
% Detailed explanation goes here
obj.configFileName = configFileName;
try
txt = fileread(obj.configFileName);
obj.configData = jsondecode(txt);
checkConfig(obj);
catch
msg = ['no valid config File with the filename ',...
obj.configFileName, ' found.' newline,...
'Please enter the required information manually'];
disp(msg);
obj.configData = obj.wizard('initilise');
m = input('Do you want to save the config, Y/N [Y]:','s');
if ismember(m,{'Y','y'})
obj.writeConfig(fullfile(pwd));
end
end%try
end
function outputArg = checkConfig(obj)
%checkConfig validates the config file
% TODo;
outputArg = true;
end
function writeConfig(obj,path)
%writeConfig writes the config file to path
% TODo;
fid = fopen(fullfile(path,obj.configFileName),'w');
txt = jsonencode(obj.configData,'PrettyPrint',true);
%fprintf does not write paths correctly !!!
fwrite(fid,txt);
fclose(fid);
end
function configData = addPublishOptions(obj,mode)
%writeConfig writes the config file to path
% TODo;
obj.configData.options = obj.plotID_options(mode);
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
outputArg = obj.Property1 + inputArg;
end
end
methods (Static)
configStruct = wizard(mode)
optionStruct = plotID_options(input)
end
end
function [options] = plotID_options(input)
%PLOTID_OPTIONS Summary of this function goes here
% Detailed explanation goes here
options = struct();
switch input
case 'default' %same as Arguments Block
options.Location = 'local'; % storage path
options.Method = 'individual';
options.ParentFolder = 'export';
options.ConfigFileName = 'config.json';%individual config names possible
options.CopyUserFCN = true;
options.CSV = false;
options.ShowMessages = true;
options.ForcePublish = false; %publish anyway
case 'debug'
options.Location = 'local'; % storage path
options.Method = 'individual';
options.ParentFolder = 'export';
options.ConfigFileName = 'config.json';%individual config names possible
options.CopyUserFCN = true;
options.CSV = true;
options.ShowMessages = true;
options.ForcePublish = true; %publish anyway
end
end
function config = wizard(mode)
%wizard creates config files depending on the selected mode
% initilise ask only for the input that is neccessary to run plotID
config = struct();
switch mode
case 'initilise'
config.Author = inputRequired('your Name');
config.ProjectID = inputRequired('your Project Number');
msg = ['Do you want to add a remote path?' newline,...
'Otherwise your files will be stored locally.' newline,...
'You can add this later by rerunning the initilisation.'];
disp(msg);
m = input('Do you want add a remothe path, Y/N [Y]:','s');
if ismember(m,{'Y','y'})
config.ServerPath = uigetdir();
end
otherwise
error('wizard mode undefined in CLASS config');
end %switch
end
function usrTxt = inputRequired(msg)
%Input Dialog that repeats if left empty
inputRequired = true;
while inputRequired
prompt = ['Please enter ', msg , ':'];
inpt = input(prompt,'s');
if isempty(inpt)
disp('Input must not be empty!');
else
usrTxt = inpt;
inputRequired = false;
end
end
end
\ No newline at end of file
......@@ -67,16 +67,24 @@ if isempty(ID)
warning(msg);
end
%% read config file
% there is only one config Object (handleClass)
configObj = PlotID.config(options.ConfigFileName);
if isfield(configObj.configData, 'options')
% is working but prune to user errors
options = configObj.configData.options;
end
% try
% txt = fileread('config.json');
% config = jsondecode(txt);
% catch
% dlgObj.configError = true;
% end
% Error and MSG handeling
dlgObj = PlotID.userDLG(ID,options);
%% read config file
try
txt = fileread('config.json');
config = jsondecode(txt);
catch
dlgObj.configError = true;
end
%% storage location
switch options.Location
......@@ -183,26 +191,38 @@ dlgObj.rdFilesPublished = status;
dlgObj.userMSG(msg);
%% Write Config File
if ~dlgObj.configError %config File must exist
% copy config file
configPath = PlotID.createFileCopy('config.json',folderName,...
storPath,ID, 'data');
else
configPath = fullfile(storPath,folderName,'config.json');
config = struct();
exportPath = fullfile(storPath,folderName);
configObj.writeConfig(exportPath);
% if ~dlgObj.configError
% % copy config file
% configPath = PlotID.createFileCopy('config.json',folderName,...
% storPath,ID, 'data');
% else
% %create config file
% configPath = fullfile(storPath,folderName,'config.json');
% config = options;
% end
% %write config
% fid = fopen(char(configPath),'w');
% txt = jsonencode(config,'PrettyPrint',true);
% fprintf(fid,txt);
% fclose(fid);
% add further Metadata
meta =struct();
if ispc
config.author = getenv('USERNAME');
end
meta.author = getenv('USERNAME');
end
% add further Metadata
config.ProjectID = ID;
config.CreationDate = datestr(now);
config.MatlabVersion = version;
config.ToolboxVersions = pList;
%write config
fid = fopen(char(configPath),'w');
txt = jsonencode(config,'PrettyPrint',true);
meta.ProjectID = ID;
meta.CreationDate = datestr(now);
meta.MatlabVersion = version;
meta.ToolboxVersions = pList;
% write meta
metaPath = fullfile(storPath,folderName,'plotID_data.json');
fid = fopen(char(metaPath),'w');
txt = jsonencode(meta,'PrettyPrint',true);
fprintf(fid,txt);
fclose(fid);
......
......@@ -20,24 +20,13 @@ arguments
options.DistanceToAxis {mustBeReal} = .01 % relative distance
options.Position (1,2) {mustBeVector} = [1,0.4] % default for east
options.Rotation (1,1) {mustBeReal} = NaN
options.ConfigFileName (1,:) {mustBeText} = 'config.json'
end
if isempty(options.ProjectID)
try
txt = fileread('config.json');
config = jsondecode(txt);
catch
config =struct;
config.ProjectID = '';
warning("No ProjectID was definded and no config.json could be found");
end
if ~isempty(config.ProjectID)
options.ProjectID = config.ProjectID;
else
warning('no project options.ProjectID defined')
end
configObj = PlotID.config(options.ConfigFileName);
configData = configObj.configData;
options.ProjectID = configData.ProjectID;
end
switch options.Location
......
function [status] = initilise()
%INITILISE Initilisation for first time úsage
end
......@@ -8,8 +8,18 @@ names = strcat(names, ext); % add ext for comparison
% Get a list of all .m files that are part of Plot id
packageContent = what('PlotID');
% packageContent.classes has no extensions
PltID_flist = [packageContent.m; strcat(packageContent.classes,'.m')];
PltID_classlist = packageContent.classes;
% Class Methods need to be listed in an additional function
Class_flist = cell(1,numel(packageContent.classes)); %preallocate
for i=1:numel(packageContent.classes)
temp = what(['PlotID',filesep,'@',PltID_classlist{i}]);
Class_flist{i} = temp.m;
end
Class_flist = vertcat(Class_flist{:});
% all plotID .m files:
PltID_flist = [packageContent.m; Class_flist];
% Comparison and filter
fListClean = fList(~ismember(names,PltID_flist));
end
......
%% This is the PlotID Initialisation file
% Please run this script before using PlotID
%% Step 1: Add plotID location to you MATLAB path
% This is necessary to use PlotID in your personal MATLAB Scripts
addpath(pwd);
savepath;
%% Section 2 Config File
% To make things easy, you should use the config file. The following wizard
% will help you create one:
writeConfig = true;
% Ask User if current config should be overwritten
if isfile('config.json')
m = input('Do you want to overwrite the current config file, Y/N [Y]:','s');
writeConfig = ismember(m,{'Y','y'});
end
if writeConfig
config = PlotID.config.wizard('initilise');
fid = fopen('config.json','w');
txt = jsonencode(config,'PrettyPrint',true);
fwrite(fid,txt);
fclose(fid);
end
%%
disp('Initialisition done.')
%% Example Script
% This Script is meant to demonstrate the capabilities of the PlotID tool.
%% Clear Environment
clear; clc; close all;
addpath('CI_files'); % Test scripts
try
delete testdata2.h5;
end
%% Set ProjectID
% ProjectID can also be set in the config file
% Leave empty for using the ID from the config file
ProjectID = 'FST01';
%% Data
% Creating Random Data to use as data-file
x = linspace(0,7);
y = rand(1,100)+2;
dataset1 = 'test_data.mat';
% use absolute paths for good practise
dataset1 = fullfile(pwd, dataset1);
save(dataset1,'x','y');
% some data as .h5
x1 = linspace(0,2*pi);
y1 = sin(x1)+.5*sin(2*x1)+2;
% define file path & name
fpath = fullfile(pwd,"./testdata2.h5");
dataset2 = fullfile(pwd,'testdata2.h5');
% create hdf5 file and dataset > write data to hdf5 file / dataset
h5create(fpath, "/x1", size(x1), "Datatype", class(x1))
h5create(fpath, "/y1", size(y1), "Datatype", class(y1))
h5write(fpath, "/x1", x1)
h5write(fpath, "/y1", y1)
%% Plotting
% This is still part of a normal script to produce plots.
% Make sure to save each figure in a variable to pass to PlotID-functions.
fig(1) = figure;
set(gcf,'Units','centimeters','PaperUnits','centimeters','PaperSize',[9 7],...
'Position',[5 5 9 7]);
plot(x,y,'Color',0.5*[1 1 1]);
box off; hold on;
plot(x1,y1,'-k');
set(gca, 'TickDir', 'out', 'YLim', [0,4],'YTick',[0:1:4],'XLim',[0,6]);
%fstplt.setfiguresize('1/2ppt16:9');
%fstplt.pimpplot;
%% Example 1: single plot based on two data-sets
%% Tag the plot
% PlotID Implementation starts here.
% TagPlot adds a visible ID to the figure(s) and to the figures property
% 'Tag'
[fig, ID] = PlotID.TagPlot(fig, 'ProjectID', ProjectID);
%% Publishing
% Second part of plotID
% The functions needs the file location, the location of the data and the
% figure and can take several options.
path.script = mfilename('fullpath'); % filename of the m.script
% file names of the datasets
path.rdata = {dataset1,dataset2} ; % don't forget the extension
PlotID.Publish(path, ID, fig(1), 'Location', 'local' ,'Method','individual')
%% End
{
"Author": "Example Author",
"ProjectID": "AB01",
"ServerPath": "\\\\Server\\path\\folder"
}
......@@ -62,8 +62,8 @@ set(gca, 'TickDir', 'out', 'YLim', [0,4]);
% PlotID Implementation starts here.
% TagPlot adds a visible ID to the figure(s) and to the figures property
% 'Tag'
[fig, ID] = PlotID.TagPlot(fig, 'ProjectID', ProjectID);
% [fig, ID] = PlotID.TagPlot(fig, 'ProjectID', ProjectID);
[fig, ID] = PlotID.TagPlot(fig);
%% Publishing
% Second part of plotID
% The functions needs the file location of the script, the location of the
......@@ -76,7 +76,7 @@ scriptPath = [mfilename('fullpath'),'.m'];
%(defined above:) dataset1 = 'test_data.mat'; dataset2 = 'testdata2.h5'
locations = {dataset1,dataset2} ; % don't forget the extension
locations = {string(dataset1),dataset2}
locations = {string(dataset1),dataset2};
%call publishing
PlotID.Publish(locations,scriptPath, fig(1), 'Location', 'local' ,'Method','individual')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment