Skip to content

Commit

Permalink
[feat] merge nii/jnii/hdf5/tsv reading functions self-contained
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 28, 2024
1 parent b482c8f commit 2e43586
Show file tree
Hide file tree
Showing 12 changed files with 1,362 additions and 0 deletions.
33 changes: 33 additions & 0 deletions getvarfrom.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function p = getvarfrom(ws, name)
%
% p=getvarfrom(ws,name)
%
% get variable value by name from specified work-space
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% ws: name of the work-space, for example, 'base'
% name: name string of the variable
%
% output:
% p: the value of the specified variable, if the variable does not
% exist, return empty array
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

wsname = ws;
if (~iscell(ws))
wsname = cell(1);
wsname{1} = ws;
end

p = [];
for i = 1:length(wsname)
isdefined = evalin(wsname{i}, ['exist(''' name ''')']);
if (isdefined == 1)
p = evalin(wsname{i}, name);
break
end
end
92 changes: 92 additions & 0 deletions loadbidstsv.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
function data = loadbidstsv(tsvfile, delim)
%
% data = loadbidstsv(tsvfile)
% or
% data = loadbidstsv(tsvfile, delim)
%
% Loading a BIDS-formatted .tsv (tab-separated values) or .tsv.gz file as a
% struct; numerical fields are converted to floating-point data records
% when possible; the header of the file is parsed to define sub-field
% names
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% tsvfile: the path to the .tsv file
% delim: (optional) if not set, tab ('\t') is used as column delimiter
%
% examples:
% data = loadbidstsv('participants.tsv');
%
% license:
% BSD license, see LICENSE_BSD.txt files for details
%
% -- this function is part of JBIDS toolbox (https://neurojson.org/#software)
%

if (nargin < 2)
delim = sprintf('\t');
end

data = struct;

if (~isempty(regexp(tsvfile, '\.[Gg][Zz]$', 'once')))
finput = fopen(tsvfile, 'rb');
tsvdata = fread(finput, inf, 'uint8=>uint8');
fclose(finput);

if (~exist('gzipdecode', 'file'))
error('To process zipped files, you must install gzipdecode.m from the JSONLab toolbox: http://github.com/fangq/jsonlab');
end
fid = char(gzipdecode(tsvdata));
clear tsvdata;
[header, endpos] = regexp(fid, '([^\n\r]*)', 'once', 'tokens', 'end');
if (~isempty(header))
header = header{1};
fid = fid((endpos + 1):end);
end
else
fid = fopen(tsvfile, 'rt');
header = fgetl(fid);
header = regexprep(header, '\s*$', '');
end

if (isempty(header))
return
end

if (exist('strsplit'))
cols = strsplit(header, delim);
else
cols = regexp(header, '\t*([^\t]*)\t*', 'tokens');
cols = cellfun(@(x) x{:}, cols, 'uniformoutput', 0);
end
cols = cellfun(@encodevarname, cols, 'uniformoutput', 0);
if (~isempty(cols))
body = textscan(fid, [repmat('%s\t', 1, length(cols) - 1), '%s'], 'delimiter', '\t');
if (length(body) ~= length(cols))
error('invalid tsv');
end
for i = 1:length(body)
try
% bodynum = cellfun(@(x) sscanf(regexprep(x,'^n/a$','NaN'), '%f'), body{i}, 'uniformoutput', 0);
bodynum = cellfun(@(x) sscanf(x, '%f\t'), body{i}, 'uniformoutput', 0);
if (exist('isna'))
len = cellfun(@(x) numel(x) * (~isna(sum(x))), bodynum);
else
len = cellfun(@numel, bodynum);
end
if (any(len))
body{i}(len > 0) = bodynum(len > 0);
if (all(len))
body{i} = cell2mat(body{i});
end
end
catch ME
warning(ME.message);
end
data.(cols{i}) = body{i}(:).';
end
end

fclose(fid);
Loading

0 comments on commit 2e43586

Please sign in to comment.