Skip to content

Commit

Permalink
Merge pull request #11 from davidclemens/refactor-GearKit-data-storage
Browse files Browse the repository at this point in the history
Refactor gear kit data storage
  • Loading branch information
davidclemens authored Dec 7, 2020
2 parents 9b2cfd8 + 6f25e76 commit 9a3d684
Show file tree
Hide file tree
Showing 94 changed files with 3,164 additions and 1,242 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function list = listAllValidInfoVariableType()

[~,list] = enumeration('DataKit.Metadata.validators.validInfoVariableType');
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
classdef validInfoVariableType
enumeration
undefined
Dependant
Independant
end

methods (Static)
tbl = listAllValidInfoVariableType()
end
end
31 changes: 31 additions & 0 deletions +DataKit/+Metadata/@info/addVariable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function obj = addVariable(obj,variable,varargin)


variableCount = numel(variable);

% parse Name-Value pairs
optionName = {'VariableType','VariableRaw','VariableFactor','VariableOffset','VariableCalibrationFunction','VariableOrigin','variableDescription','variableMeasuringDevice'}; % valid options (Name)
optionDefaultValue = {repmat({'Dependant'},1,variableCount),repmat(DataKit.Metadata.variable.undefined,1,variableCount),ones(1,variableCount),zeros(1,variableCount),repmat({@(t,x) x},1,variableCount),repmat({0},1,variableCount),repmat({''},1,variableCount),repmat(GearKit.measuringDevice(),1,variableCount)}; % default value (Value)
[variableType,...
variableRaw,...
variableFactor,...
variableOffset,...
variableCalibrationFunction,...
variableOrigin,...
variableDescription,...
variableMeasuringDevice...
] = internal.stats.parseArgs(optionName,optionDefaultValue,varargin{:}); % parse function arguments

obj.Variable = cat(2,obj.Variable,variable);
obj.VariableRaw = cat(2,obj.VariableRaw,variableRaw);
obj.VariableType = cat(2,obj.VariableType,variableType);
obj.VariableFactor = cat(2,obj.VariableFactor,variableFactor);
obj.VariableOffset = cat(2,obj.VariableOffset,variableOffset);
obj.VariableCalibrationFunction = cat(2,obj.VariableCalibrationFunction,variableCalibrationFunction);
obj.VariableOrigin = cat(2,obj.VariableOrigin,variableOrigin);
obj.VariableDescription = cat(2,obj.VariableDescription,variableDescription);
obj.VariableMeasuringDevice = cat(2,obj.VariableMeasuringDevice,variableMeasuringDevice);

obj = obj.validateProperties;
obj.validateInfoObj;
end
105 changes: 105 additions & 0 deletions +DataKit/+Metadata/@info/info.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
classdef info


properties
Variable(1,:) DataKit.Metadata.variable
VariableRaw(1,:) DataKit.Metadata.variable
VariableType(1,:) DataKit.Metadata.validators.validInfoVariableType
VariableDescription(1,:) cell
VariableFactor(1,:) double
VariableOffset(1,:) double
VariableOrigin(1,:) cell
VariableCalibrationFunction(1,:) cell
VariableMeasuringDevice(1,:) GearKit.measuringDevice
end
properties (Dependent)
VariableId(1,:) {mustBeInteger, mustBeNonnegative, mustBeLessThan(VariableId, 65535)}
VariableUnit(1,:) categorical
VariableRawUnit(1,:) categorical
VariableCount(1,1) double
VariableIsCalibrated(1,:) logical
end
properties (Dependent, Hidden)
NoIndependantVariable
VariableReturnDataType
end

methods
function obj = info(variable,varargin)

if nargin == 0
variable = '';
end
variableCount = numel(variable);

% parse Name-Value pairs
optionName = {'VariableType','VariableFactor','VariableOffset','VariableCalibrationFunction','VariableOrigin','variableDescription','variableMeasuringDevice'}; % valid options (Name)
optionDefaultValue = {repmat({'Dependant'},1,variableCount),ones(1,variableCount),zeros(1,variableCount),repmat({@(x) x},1,variableCount),zeros(1,variableCount),repmat({''},1,variableCount),repmat(GearKit.measuringDevice(),1,variableCount)}; % default value (Value)
[variableType,...
variableFactor,...
variableOffset,...
variableCalibrationFunction,...
variableOrigin,...
variableDescription,...
variableMeasuringDevice...
] = internal.stats.parseArgs(optionName,optionDefaultValue,varargin{:}); % parse function arguments

obj = obj.addVariable(variable,...
'VariableType', variableType,...
'VariableFactor', variableFactor,...
'VariableOffset', variableOffset,...
'VariableCalibrationFunction', variableCalibrationFunction,...
'VariableOrigin', variableOrigin,...
'VariableDescription', variableDescription,...
'VariableMeasuringDevice', variableMeasuringDevice);
end
end
methods
obj = addVariable(obj,variable,varargin)
obj = removeVariable(obj,ind)
tbl = info2table(obj)
obj = selectVariable(obj,variableIdx)
end
methods (Access = private)
obj = validateProperties(obj)
obj = validateInfoObj(obj)
obj = updateVariableRaw(obj)
end

% set methods
methods
function obj = set.Variable(obj,value)

obj = updateVariableRaw(obj);
obj.Variable = value;
end
end
% get methods
methods
function VariableId = get.VariableId(obj)
VariableId = cat(2,obj.Variable.Id);
end
function VariableUnit = get.VariableUnit(obj)
VariableUnit = cat(2,{obj.Variable.Unit});
end
function VariableRawUnit = get.VariableRawUnit(obj)
VariableRawUnit = cat(2,{obj.VariableRaw.Unit});
end
function VariableCount = get.VariableCount(obj)
VariableCount = numel(obj.Variable);
end
function VariableIsCalibrated = get.VariableIsCalibrated(obj)
VariableIsCalibrated = ~cellfun(@(f) strcmp(func2str(f),'@(t,x)x'),obj.VariableCalibrationFunction);
end
function NoIndependantVariable = get.NoIndependantVariable(obj)
NoIndependantVariable = ~any(obj.VariableType == 'Independant');
end
function VariableReturnDataType = get.VariableReturnDataType(obj)
VariableReturnDataType = categorical(cellfun(@class,obj.VariableOrigin,'un',0));
end
% function VariableRaw = get.VariableRaw(obj)
% obj = updateVariableRaw(obj);
% VariableRaw = obj.VariableRaw;
% end
end
end
23 changes: 23 additions & 0 deletions +DataKit/+Metadata/@info/info2table.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function tbl = info2table(obj)

if numel(obj) == 1
try
tbl = table(...
(1:obj.VariableCount)',...
obj.Variable',...
obj.VariableId',...
cellstr(obj.VariableType)',...
obj.VariableUnit',...
obj.VariableDescription',...
obj.VariableOrigin',...
obj.VariableFactor',...
obj.VariableOffset',...
obj.VariableMeasuringDevice',...
'VariableNames',{'VariableIndex','Variable','Id','Type','Unit','Description','Origin','Factor','Offset','MeasuringDevice'});
catch

end
else
error('Only works in a scalar context')
end
end
12 changes: 12 additions & 0 deletions +DataKit/+Metadata/@info/removeVariable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function obj = removeVariable(obj,ind)

obj.Variable(ind) = [];
obj.VariableRaw(ind) = [];
obj.VariableType(ind) = [];
obj.VariableDescription(ind) = [];
obj.VariableFactor(ind) = [];
obj.VariableOffset(ind) = [];
obj.VariableOrigin(ind) = [];
obj.VariableCalibrationFunction(ind) = [];
obj.VariableMeasuringDevice(ind) = [];
end
5 changes: 5 additions & 0 deletions +DataKit/+Metadata/@info/selectVariable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function obj = selectVariable(obj,variableIdx)

ind = setxor(variableIdx,1:obj.VariableCount);
obj = obj.removeVariable(ind);
end
4 changes: 4 additions & 0 deletions +DataKit/+Metadata/@info/updateVariableRaw.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function obj = updateVariableRaw(obj)

obj.VariableRaw(~obj.VariableIsCalibrated) = DataKit.Metadata.variable.undefined;
end
7 changes: 7 additions & 0 deletions +DataKit/+Metadata/@info/validateInfoObj.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function obj = validateInfoObj(obj)

if obj.NoIndependantVariable && obj.VariableCount > 1
error('DataKit:Metadata:info:missingIndependantVariable',...
'An independant variable is missing.')
end
end
25 changes: 25 additions & 0 deletions +DataKit/+Metadata/@info/validateProperties.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function obj = validateProperties(obj)


obj = validateProperty(obj,'VariableRaw',DataKit.Metadata.variable.empty);
obj = validateProperty(obj,'VariableType','Dependant');
obj = validateProperty(obj,'VariableFactor',1);
obj = validateProperty(obj,'VariableOffset',0);
obj = validateProperty(obj,'VariableCalibrationFunction',{@(t,x) x});
obj = validateProperty(obj,'VariableOrigin',{0});
obj = validateProperty(obj,'VariableDescription',{''});
obj = validateProperty(obj,'VariableMeasuringDevice',GearKit.measuringDevice());
end

function obj = validateProperty(obj,prop,default)
propCount = numel(obj.(prop));
if propCount ~= obj.VariableCount
if propCount == 0
% set all to default value
obj.(prop)(1:obj.VariableCount) = default;
elseif propCount < obj.VariableCount
% only set the unset values to the default value
obj.(prop)(propCount + 1:obj.VariableCount) = default;
end
end
end
17 changes: 17 additions & 0 deletions +DataKit/+Metadata/@variable/disp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function disp(obj)

dim = size(obj);
obj = obj(:);

if any(dim == 0)
fprintf('%ux%u variable object\n',dim(1),dim(2))
return
end
objInfo = categorical(strcat(obj.cellstr,...
{' ('},...
arrayfun(@(o) sprintf('%u',o),cat(1,obj.Id),'un',0),...
{', '},...
cat(1,{obj.Unit})',...
{')'}));
disp(reshape(objInfo,dim))
end
17 changes: 17 additions & 0 deletions +DataKit/+Metadata/@variable/id2variable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function obj = id2variable(id)

if ~isnumeric(id)
error('DataKit:Metadata:variable:id2Variable:invalidDataType',...
'The input argument ''id'' has to be numeric.')
end

variableListInfo = DataKit.Metadata.variable.listAllVariableInfo();
[im,imIdx] = ismember(id,variableListInfo{:,'Id'});

if ~all(im)
error('DataKit:Metadata:variable:variableFromId:invalidVariableId',...
'The variable id %u is invalid.',id(find(~im,1)))
end

obj = variableListInfo{imIdx,'Variable'};
end
23 changes: 23 additions & 0 deletions +DataKit/+Metadata/@variable/listAllVariableInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function tbl = listAllVariableInfo()


variableList = enumeration('DataKit.Metadata.variable');
variableProps = properties('DataKit.Metadata.variable');

propClass = cellfun(@class,cellfun(@(p) variableList(1).(p),variableProps,'un',0),'un',0);
tblStruct = struct();
for prop = 1:numel(variableProps)
switch propClass{prop}
case 'char'
column = {variableList(:).(variableProps{prop})}';
case {'single','double','int8','int16','int32','uint8','uint16','uint32','uint64'}
column = cat(1,variableList(:).(variableProps{prop}));
otherwise
error('%s is not implemented yet.',propClass{prop})
end

tblStruct.(variableProps{prop}) = column;
end
tbl = struct2table(tblStruct);
tbl.Variable = variableList;
end
4 changes: 4 additions & 0 deletions +DataKit/+Metadata/@variable/listAllVariables.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function list = listAllVariables()

[~,list] = enumeration('DataKit.Metadata.variable');
end
14 changes: 14 additions & 0 deletions +DataKit/+Metadata/@variable/str2variable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function obj = str2variable(str)

if ~(iscellstr(str) || ischar(str))
error('DataKit:Metadata:variable:str2Variable:invalidDataType',...
'The input argument ''str'' has to be a cellstr or character array.')
end

if iscellstr(str)
obj = cellfun(@(s) DataKit.Metadata.variable(s),str,'un',0);
obj = cat(1,obj{:});
else
obj = DataKit.Metadata.variable(str);
end
end
39 changes: 39 additions & 0 deletions +DataKit/+Metadata/@variable/validateId.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function [bool,info] = validateId(id)

if isa(id,'uint16')
% ok
elseif ~isa(id,'uint16') && isnumeric(id)
try
id = cast(id,'uint16');
catch ME
rethrow(ME)
end
else
error('DataKit:Metadata:variable:validateId:invalidIdDataType',...
'''id'' has to be either a uint16 vector or be convertible to uint16.')
end

nVariables = numel(id);
infoAll = DataKit.Metadata.variable.listAllVariableInfo;
info = repmat(DataKit.table2emptyRow(infoAll),nVariables,1);
idOut = zeros(numel(id),1,'uint16');
for s = 1:nVariables
try
var = DataKit.Metadata.variable.id2variable(id(s));
idOut(s) = var.variable2id;
catch ME
switch ME.identifier
case 'MATLAB:class:CannotConvert'
if isempty(regexpi(ME.message,'is not a member of enumeration ''DataKit.Metadata.variable'''))
rethrow(ME);
end
otherwise
rethrow(ME);
end
end
end

bool = idOut > 0;
[~,imIdx] = ismember(idOut,infoAll{:,'Id'});
info(bool,:) = infoAll(imIdx(bool),:);
end
Loading

0 comments on commit 9a3d684

Please sign in to comment.