-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] merge nii/jnii/hdf5/tsv reading functions self-contained
- Loading branch information
Showing
12 changed files
with
1,362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.