Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/development'
Browse files Browse the repository at this point in the history
  • Loading branch information
jayd1860 committed May 2, 2024
2 parents 666ab6d + f83beb3 commit a6b592d
Show file tree
Hide file tree
Showing 65 changed files with 2,351 additions and 423 deletions.
333 changes: 294 additions & 39 deletions DataTree/AcquiredData/DataFiles/DataFilesClass.m

Large diffs are not rendered by default.

81 changes: 74 additions & 7 deletions DataTree/AcquiredData/DataFiles/FileClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
err
logger
errmsg
errcodeUnvalidated
end

methods
Expand All @@ -40,8 +41,8 @@
obj.rootdir = '';
obj.errmsg = ''; % Assume file is not loadable
obj.logger = InitLogger(logger);
obj.errmsg = '';

obj.errcodeUnvalidated = -9999;

if nargin==0
return;
Expand Down Expand Up @@ -88,7 +89,13 @@ function Add(obj, obj2)
obj.filename = obj2.name;
obj.name = getPathRelative(rootpath, obj2.rootdir);
obj.rootdir = obj2.rootdir;
obj.err = 0; % Set error to NO ERROR
obj.date = obj2.date;
obj.datenum = datestr2datenum(obj.date);
if obj.IsFile()
obj.err = obj.errcodeUnvalidated; % Set error to unvalidated
else
obj.err = 0;
end
end


Expand Down Expand Up @@ -434,7 +441,7 @@ function Loaded(obj)
if isempty(obj.name)
return;
end
if obj.err ~= 0
if (obj.err ~= 0) && (obj.IsValidated)
return;
end
b = false;
Expand All @@ -443,19 +450,22 @@ function Loaded(obj)

% ----------------------------------------------------
function err = ErrorCheckName(obj)
err = 0;
[p1,f1] = fileparts(obj.name);
[p2,f2] = fileparts(filesepStandard(obj.rootdir,'nameonly:file'));
[~,f3] = fileparts(p2);
if strcmp(f1, p1)
obj.err = -1;
err = -1;
end
if strcmp(f1, f2)
obj.err = -2;
err = -2;
end
if strcmp(f1, f3)
obj.err = -3;
err = -3;
end
if err ~= 0
obj.err = err;
end
err = obj.err;
end


Expand Down Expand Up @@ -545,6 +555,7 @@ function NameConflictFixed(obj)
% -----------------------------------------------------
function SetError(obj, errmsg)
obj.errmsg = errmsg;
obj.err = -1;
end


Expand All @@ -553,6 +564,62 @@ function SetError(obj, errmsg)
msg = obj.errmsg;
end


% -----------------------------------------------------
function SetValid(obj)
obj.err = 0;
end


% -----------------------------------------------------
function SetErrorUnvalidated(obj)
obj.err = obj.errcodeUnvalidated;
end


% -----------------------------------------------------
function b = IsValidated(obj)
b = false;
if obj.err == obj.errcodeUnvalidated
return
end
b = true;
end



% -----------------------------------------------------
function b = IsUnValidated(obj)
b = true;
if obj.err == obj.errcodeUnvalidated
return
end
b = false;
end



% ----------------------------------------------------------
function b = eq(obj, obj2)
b = false;
if ~strcmp(obj.name, obj2.name)
return;
end
if ~strcmp(obj.date, obj2.date)
return;
end
if obj.isdir ~= obj2.isdir
return;
end
if obj.bytes ~= obj2.bytes
return;
end
if obj.datenum ~= obj2.datenum
return;
end
b = true;
end

end

end
16 changes: 15 additions & 1 deletion DataTree/AcquiredData/DataFiles/Hdf5/HDF5_DatasetLoad.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
end

try
dsetid = H5D.open(gid, name);

if ~H5L.exists(gid, name, 'H5P_DEFAULT')
switch(class(val0))
case 'char'
val = '';
case 'cell'
val = {};
otherwise
val = [];
end
return
end

dsetid = H5D.open(gid, name);
% NOTE: HDF5 stores contiguous muti-dimensional arrays in row-major order.
% Matlab stores them in row-major order. We want to transpose the loaded data
% it back to Matlab's column-major storage order and thus get back the
Expand All @@ -29,3 +41,5 @@
val = [];
end
end


12 changes: 6 additions & 6 deletions DataTree/AcquiredData/DataFiles/Hdf5/HDF5_Transpose.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function val = HDF5_Transpose(val, options)
function val = HDF5_Transpose(val, option)

if ~exist('options','var')
options = '';
if ~exist('option','var')
option = '';
end

% Matlab stores contiguous muti-dimensional arrays in column-major order.
% HDF5 stores them in row-major order. We want to transpose the data to agree
% with the file format's storage order.
if (~isrow(val) && ~iscolumn(val)) || ... % Matrices
~isempty(findstr('multidim', options)) || ... % Force multi-dimensional even if vector
~isempty(findstr('2D', options)) || ... % Force 2D even if vector
~isempty(findstr('3D', options)) % Force 3D even if vector
~isempty(findstr('multidim', option)) || ... % Force multi-dimensional even if vector
~isempty(findstr('2D', option)) || ... % Force 2D even if vector
~isempty(findstr('3D', option)) % Force 3D even if vector

val = permute(val, ndims(val):-1:1);

Expand Down
112 changes: 81 additions & 31 deletions DataTree/AcquiredData/DataFiles/Hdf5/hdf5write_safe.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function err = hdf5write_safe(fileobj, name, val, options)
err = -1;
if isempty(val)
if ~exist('val','var')
return;
end
if ~exist('options','var')
options = '';
end

force_scalar = false;
force_array = false;
force_vector = false;
if any(strcmp(options, 'array'))
force_array = true;
elseif any(strcmp(options, 'scalar'))
force_scalar = true;
elseif any(strcmp(options, 'vector'))
force_vector = true;
end

% Identify type of val and use SNIRF v1.1-compliant write function
Expand All @@ -32,25 +32,21 @@

% Create dataset
if iscell(val) || isstring(val)
if length(val) > 1 && ~force_scalar || force_array % Returns true for single strings, believe it or not
if (length(val) > 1) || force_array % Returns true for single strings, believe it or not
write_string_array(fid, name, val);
else
write_string(fid, name, val);
end
elseif ischar(val)
write_string(fid, name, val);
elseif isfloat(val)
if length(val) > 1 && ~force_scalar || force_array
elseif isfloat(val) || isinteger(val)
if force_vector
write_numeric_vector(fid, name, val);
elseif (length(val) > 1) || force_array
write_numeric_array(fid, name, val);
else
write_numeric(fid, name, val);
end
elseif isinteger(val)
if length(val) > 1 && ~force_scalar || force_array
write_numeric_array(fid, name, val); % As of now, no integer arrays exist
else
write_integer(fid, name, val);
end
else
warning(['An unrecognized variable was saved to ', name])
end
Expand Down Expand Up @@ -82,50 +78,76 @@




% -----------------------------------------------------------------
function err = write_numeric(fid, name, val)
tid = H5T.copy('H5T_NATIVE_DOUBLE');
warning off; % Suppress the int truncation warning
hdftype = convert_to_hdf5_type(val);
tid = H5T.copy(hdftype);
sid = H5S.create('H5S_SCALAR');
dsid = H5D.create(fid, name, tid, sid, 'H5P_DEFAULT');
H5D.write(dsid, tid, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', val);
err = 0;
warning on;




% -----------------------------------------------------------------
function err = write_integer(fid, name, val)
warning off; % Suppress the int truncation warning
tid = H5T.copy('H5T_NATIVE_ULONG');
sid = H5S.create('H5S_SCALAR');
dsid = H5D.create(fid, name, tid, sid, 'H5P_DEFAULT');
H5D.write(dsid, tid, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', int32(val));
function err = write_numeric_array(fid, name, data)
err = 0;
warning on;
data = HDF5_Transpose(data, '2D');
sizedata = size(data);
n = sizedata;

tid = -1;
sid = -1;
gid = -1;
dsid = -1;

maxdims = n;

hdftype = convert_to_hdf5_type(data);

try

sid = H5S.create_simple(numel(n), fliplr(n), fliplr(maxdims));
gid = HDF5_CreateGroup(fid, fileparts(name));
dsid = H5D.create(gid, name, hdftype, sid, 'H5P_DEFAULT');
H5D.write(dsid, 'H5ML_DEFAULT', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', data);

catch

% Clean up; Close everything
cleanUp(tid, sid, gid, dsid);
err = -1;
return;

end
cleanUp(tid, sid, gid, dsid);




% -----------------------------------------------------------------
function err = write_numeric_array(fid, name, data)
function err = write_numeric_vector(fid, name, data)
err = 0;
data = HDF5_Transpose(data);
sizedata = size(data);
if sizedata(1) == 1 || sizedata(2) == 1
n = length(data);
else
n = sizedata;
end
data = HDF5_Transpose(data, '2D');
sizedata = length(data);
n = sizedata;

tid = -1;
sid = -1;
gid = -1;
dsid = -1;

maxdims = n;
hdftype = convert_to_hdf5_type(data);
try

sid = H5S.create_simple(numel(n), fliplr(n), fliplr(maxdims));
sid = H5S.create_simple(numel(n), fliplr(n), fliplr(maxdims));
gid = HDF5_CreateGroup(fid, fileparts(name));
dsid = H5D.create(gid, name, 'H5T_NATIVE_DOUBLE', sid, 'H5P_DEFAULT');
dsid = H5D.create(gid, name, hdftype, sid, 'H5P_DEFAULT');
H5D.write(dsid, 'H5ML_DEFAULT', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', data);

catch
Expand All @@ -140,6 +162,7 @@




% ------------------------------------------------------
function cleanUp(tid, sid, gid, dsid)
if ~isnumeric(tid)
Expand All @@ -156,3 +179,30 @@ function cleanUp(tid, sid, gid, dsid)
end




% -------------------------------------------------------
function hdftype = convert_to_hdf5_type(val)
hdftype = '';
switch class(val)
case 'int8'
hdftype = 'H5T_STD_8LE';
case 'uint8'
hdftype = 'H5T_STD_U8LE';
case 'int16'
hdftype = 'H5T_STD_16LE';
case 'uint16'
hdftype = 'H5T_STD_U16LE';
case 'int32'
hdftype = 'H5T_STD_32LE';
case 'uint32'
hdftype = 'H5T_STD_U32LE';
case 'int64'
hdftype = 'H5T_STD_64LE';
case 'uint64'
hdftype = 'H5T_STD_U64LE';
case 'double'
hdftype = 'H5T_NATIVE_DOUBLE';
end


Loading

0 comments on commit a6b592d

Please sign in to comment.