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

Merge branch...

Merge branch '49-moglichkeit-datenvariablen-zu-ubergeben-anstelle-von-dateipfaden' into 'development'

Resolve "Möglichkeit Datenvariablen zu übergeben anstelle von Dateipfaden"

See merge request !51
parents d5a9f416 93859dbf
No related branches found
No related tags found
2 merge requests!62Introduce changes for V1.0 RC 1,!51Resolve "Möglichkeit Datenvariablen zu übergeben anstelle von Dateipfaden"
Pipeline #650177 passed
classdef dataPath < handle
%DATAPATH stores the datapaths to the research data
% usage of a class is neccessary to support variables, argument
% validation and storing variables as tempory files
properties (SetAccess = protected)
DataPaths (1,:) cell % dataPaths
tmpPath % path to TMP files
ID %ID
end
methods
function obj = dataPath(inputPaths,ID)
%DATAPATH Construct an instance of this class
% start with argument validation
obj.ID = ID;
%catch non cell inputs in inputPaths
if ~iscell(inputPaths)
inputPaths = {inputPaths}; %Cell array
end
isStruct = false([1,numel(inputPaths)]);
% strings will cause problems, therefore chars are used
for i=1:numel(inputPaths)
if isstring(inputPaths{i})
inputPaths{i} = char(inputPaths{i});
end
% check for Variable inputs
if isstruct(inputPaths{i})
isStruct(i) = true;
elseif ~ischar(inputPaths{i})
obj.throwError();
end
end
obj.DataPaths = inputPaths;
% create temporary file from all Variables
if any(isStruct)
obj.vars2file(isStruct);
end
% final check if all paths are valid
mustBeDataPath(obj);
end
function mustBeDataPath(obj)
if ~isempty(obj.DataPaths)
%checks if input is a valid DataPath object
tf = ~isfile(obj.DataPaths);
if any(tf)
dataPath.throwError();
end
end
end
function cleanTmpFile(obj)
if ~isempty(obj.tmpPath)
delete(obj.tmpPath);
end
end
%% non local functions
vars2file(obj,isvar);
end
methods (Static)
function throwError()
%THROWERROR throws an error Dialog for invalid Data
eidType = 'mustBeDataPath:notaValidFilePath';
msgType = 'DataPaths must contain file-path(s) or Variables';
throwAsCaller(MException(eidType,msgType))
end
end %static
end %class
function obj = vars2file(obj,isStruct)
%vars2file creates a temporary file for all variables in inputPaths
% the number of inputPaths is reduced by the number of variables
tmpEnv= getenv('TMP');
tmpDir = fullfile(tmpEnv,'PlotID');
if ~isfolder(tmpDir)
mkdir(tmpDir);
end
obj.tmpPath = fullfile(tmpDir,[obj.ID,'_vars.mat']);
% save only the first struct (intended)
struct = obj.DataPaths{isStruct};
save(obj.tmpPath,'-struct','struct');
% remove variable from Datapath
obj.DataPaths(isStruct) = [];
% add tmppath as Datapath
obj.DataPaths{end+1} = obj.tmpPath;
end
......@@ -2,20 +2,21 @@ function Publish(DataPaths,scriptPath, figure, options)
%%Publish(DataPaths,scriptPath, ID, figure, options) saves plot, data and measuring script
%
% DataPaths contains the path(s) to the research data, for multiple files
% use a cell array
% use a cell array or ONE struct of variables (only the first struct will
% be exported)
% scriptPath contains the path to the script, this can be provided with
% the simple call of scriptPath = [mfilename('fullpath'),'.m']
%
% Options:
% Location sets the storage location. 'local' sets the storage location
% to the current folder (an export folder will be created), 'manual' opens
% a explorer window, where you can choose the folder. If you define a export
% path in the config file, this will used per defaul (exportPath).
% an explorer window, where you can choose the folder. If you define a export
% path in the config file, this will used per default (exportPath).
% remote path, that is defined in the config file.
% Two Methods are implemented 'individual' stores the data for
% each plot while 'centralized' uses a data folder and uses reference links
% to the original data (hdf5 only).
% 'ParentFolder' is the folder Name where the exported data is stored if an
% 'ParentFolder' is the folder name where the exported data is stored if an
% path is used, PlotId will use this path as storagePath
% 'ConfigFileName' is needed for handling multiple config files (see example)
% 'CSV' turns a summary table of all exports on or off
......@@ -26,7 +27,7 @@ function Publish(DataPaths,scriptPath, figure, options)
% 'ForcePublish' will publish even if one step was not successful (not recommended)
arguments
DataPaths {mustBeDataPath} % location of the data-file(s)
DataPaths % location of the data-file(s) and/or ONE struct of variables
scriptPath (1,:) {mustBeText} % location of the matlab script
figure (1,:) {mustBeFigure} % Checks if figure is a figure object
options.Location {mustBeMember(options.Location ,{'local','exportPath','manual','CI-Test'})} = 'local' % storage path
......@@ -37,22 +38,10 @@ arguments
options.CSV (1,1) {mustBeNumericOrLogical} = true
options.ShowMessages(1,1) {mustBeNumericOrLogical} = false
options.ForcePublish (1,1) {mustBeNumericOrLogical} = false %publish anyway
end
%% argument validation
%catch string and non cell inputs in DataPaths
if ~iscell(DataPaths)
DataPaths = {DataPaths}; %Cell array
end
% strings will cause problems, therefore chars are used
for i=1:numel(DataPaths)
if isstring(DataPaths{i})
DataPaths{i} = char(DataPaths{i});
end
end
if isstring(scriptPath)
scriptPath = char(scriptPath);
end
......@@ -95,6 +84,9 @@ if isempty(ID)
warning(msg);
end
%% Create a Datapath object from Input
dataObj = PlotID.dataPath(DataPaths,ID);
%% read config file
% there is only one config Object (handleClass)
configObj = PlotID.config(options.ConfigFileName);
......@@ -124,7 +116,7 @@ switch options.Location
if isfolder(configObj.exportPath)
storPath = configObj.exportPath;
else
msg = ['Your Export folder ', storPath, newline,...
msg = ['Your Export folder ', configObj.exportPath, newline,...
'does not exist, check the config file - publishing not possible!'];
dlgObj.error(msg);
end
......@@ -197,9 +189,9 @@ switch options.Method
% Check if the new plot is based on the original data-set
% copy the data(once)
for i=1:numel(DataPaths)
for i=1:numel(dataObj.DataPaths)
% check if identical file exists (status = 1)
[~, idx] = PlotID.fileCompare(DataPaths{i},fList);
[~, idx] = PlotID.fileCompare(dataObj.DataPaths{i},fList);
% create Linked HDF5 files for identical files
if any(idx)
fList.path = fullfile(fList.folder,fList.name);
......@@ -215,10 +207,10 @@ switch options.Method
end
else % no identical file exists
%Copy the file in data and create the links (if hdf5)
[dataPath, status, msg] = PlotID.createFileCopy(DataPaths{i},'data',storPath,ID,'dataCentral');
[dataPath, status, msg] = PlotID.createFileCopy(dataObj.DataPaths{i},'data',storPath,ID,'dataCentral');
pathToData = strrep(dataPath,currentPath,'');
%WIP
if contains(DataPaths{i},{'.h5','.hdf5'}) % Linking only for HDF5
if contains(dataObj.DataPaths{i},{'.h5','.hdf5'}) % Linking only for HDF5
% and create also linked files in the plot folder
linkedHDFPath = fullfile(storPath,folderName);
[status] = PlotID.createLinkedHDF5(pathToData,linkedHDFPath);
......@@ -228,7 +220,7 @@ switch options.Method
clear DataFolderName
case 'individual'
% Create a copy of the research data
[~, status, msg] = PlotID.createFileCopy(DataPaths,folderName,storPath,ID, 'data');
[~, status, msg] = PlotID.createFileCopy(dataObj.DataPaths,folderName,storPath,ID, 'data');
end
%temporary:
dlgObj.rdFilesPublished = status;
......@@ -282,11 +274,13 @@ if dlgObj.success || options.ForcePublish
end
status = movefile(oldPath,newPath); %rename directory
end
% delete tmp data
dataObj.cleanTmpFile();
%% CSV export
if options.CSV
T = table();
T.research_Data = DataPaths'; T.PlotID(:) = {ID};
T.research_Data = dataObj.DataPaths'; T.PlotID(:) = {ID};
T.Author(:) = {configObj.configData.Author};
T.Script_Name(:) = {scriptPath};
T.Storage_Location(:) = {newPath};
......@@ -310,21 +304,5 @@ function tf = mustBeFigure(h)
tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');
end
function mustBeDataPath(DataPaths)
%checks if input is a valid DataPath object
if ~iscell(DataPaths)
DataPaths = {DataPaths};
end
tf = zeros(1,numel(DataPaths));
for i=1:numel(DataPaths)
tf(i) = ~isfile(DataPaths{i});
end
if any(tf)
eidType = 'mustBeDataPath:notaValidFilePath';
msgType = 'DataPaths must contain file-path(s)';
throwAsCaller(MException(eidType,msgType))
end
end
......@@ -8,7 +8,7 @@ if ~iscell(filePaths)
filePaths = {filePaths};
end
try
%try
storagePaths = cell(numel(filePaths,1));
for i = 1:numel(filePaths)
FileNameAndLocation = filePaths{i};
......@@ -70,12 +70,12 @@ try
end
status = true;
msg =([type, ' successfully published']);
catch
status = false;
warning([type,' export was not successful'])
if exist('errorMSG')
error(errorMSG);
end
end %try
% catch
% status = false;
% warning([type,' export was not successful'])
% if exist('errorMSG')
% error(errorMSG);
% end
% end %try
end %function
# V0.3
- Add changelog
- Add support for file or function name as scriptPath
- Add support for passing Variables in publish
{
"Author": "CI-Test",
"ProjectID": "CI-001"
}
\ No newline at end of file
File moved
clear; clc; close all;
%% Example Script - How to pass Variables
% This script how to pass variables instead or additional to the DataPaths
%% Data (only necessary for this example)
[x, y, datapath] = createExampleData('matlab');
scriptPath = mfilename('fullpath');
%% Plotting
% This is still part of a normal script to produce plots.
% Make sure to save each figure in a variable to pass it to PlotID-functions.
fig1 = figure;
plot(x,y,'-k'); box off; set(gca, 'TickDir', 'out', 'YLim', [0,4]);
[fig1, ID] = PlotID.TagPlot(fig1);
%% ---- 2. Publishing -----
% You can pass an abitrary number of variables to Publish.
% IMPORTANT: they must be collected and passed in ONE struct
% if multiple structs are passed, only the first one will be stored!
% You can additionally add datapaths and combine the methods.
% Passing multiple structs
%Example: Passing the variables x,y to publish
s.x =x; s.y=y; % Save both variables in struct s
% Build the locations cell with the struct and one path
locations = {s,'test_data.mat'};
%call publish
PlotID.Publish(locations,scriptPath, fig1)
% Your plot, script, the variables x,y and the path that your provided
% are now published.
......@@ -68,6 +68,7 @@ PlotID.Publish(locations,scriptPath, fig2)
% ---------------------------------
%% Further examples and help
% You find more examples in the Examples folder:
% - Passing Variables
% - Advanced usage
% - Working with HDF5-files
% - Using a central data folder
......
......@@ -29,8 +29,10 @@ The **ProjectID** is your custom project number, it well be the prefix of the ID
`PlotID.Publish(DataPaths,scriptPath, figure, options)`
`scriptPath` contains either the path to the script, this can be provided with the simple call of `scriptPath = [mfilename('fullpath'),'.m']` or the script or function name that is used for creating your plot. \
`DataPaths` contains the path(s) to the research data, for multiple files you can use a cell array (they must be at the path). \
`DataPaths` contains the path(s) to the research data, for multiple files you can use a cell array (they must be at the path). It is also possible to pass an arbitrary number of variables. All variables **must be** passed in **one** struct. \
`figure` is the figure that should be published.
**Please take also a look on the examples provided in the Example folder**.
# PlotID.TagPlot()
......@@ -72,7 +74,9 @@ FriendlyID Changes the Hex Number to a human friendly *datetime* and *dateStr*.
# PlotID.Publish()
`Publish(DataPaths,scriptPath, figure, options)` \
Publishes saves plot, data and measuring script
Location sets the storage location. 'local' sets the storage location to the current folder (an export folder will be created), 'server' is a remote path, that is defined in the config file. Two Methods are implemented 'individual' stores the data for each plot while 'centralized' uses a data folder and uses reference links to the original data (hdf5 only). ParentFolder is the folder Name where the exported data is stored if a path is used, PlotId will use this path a storagePath
Location sets the storage location. 'local' sets the storage location to the current folder (an export folder will be created), 'manual' opens an explorer window, where you can choose the desired folder. If you define a export path in the config file, this will used per default (exportPath).
Two Methods are implemented 'individual' stores the data for each plot while 'centralized' uses a data folder and uses reference links to the original data (hdf5 only). ParentFolder is the folder name where the exported data is stored if a path is used, PlotId will use this path a storagePath.
<details><summary>detailed description</summary>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment