forked from NeuroJSON/jsnirfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
savesnirf.m
85 lines (79 loc) · 2.91 KB
/
savesnirf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function savesnirf(data, outfile,varargin)
%
% savesnirf(snirfdata, fname)
% or
% savesnirf(snirfdata, fname, 'Param1',value1, 'Param2',value2,...)
%
% Load an HDF5 based SNIRF file, and optionally convert it to a JSON
% file based on the JSNIRF specification:
% https://github.com/NeuroJSON/jsnirf
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% snirfdata: a raw SNIRF data, preprocessed SNIRF data or JSNIRF
% data (root object must be SNIRFData)
% fname: the output SNIRF (.snirf) or JSNIRF data file name (.jnirs, .bnirs)
%
% output:
% data: a MATLAB structure with the grouped data fields
%
% example:
% data=loadsnirf('test.snirf');
% savesnirf(data,'newfile.snirf');
%
% this file is part of JSNIRF specification: https://github.com/NeuroJSON/jsnirf
%
% License: GPLv3 or Apache 2.0, see https://github.com/NeuroJSON/jsnirf for details
%
if(nargin<2 || ~ischar(outfile))
error('you must provide data and a file name');
end
opt=varargin2struct(varargin{:});
if(~isfield(opt,'root'))
opt.rootname='';
end
if(~isfield(opt,'variablelengthstring'))
opt.variablelengthstring=1;
end
if(isfield(data,'SNIRFData'))
data.nirs=data.SNIRFData;
data.formatVersion=data.SNIRFData.formatVersion;
data.nirs=rmfield(data.nirs,'formatVersion');
data=rmfield(data,'SNIRFData');
if(~isempty(regexp(data.formatVersion, '^1\.', 'once')))
if(length(data.nirs.data.measurementList) == 1 && length(data.nirs.data.measurementList.sourceIndex) > 1)
data.nirs.data.measurementList=soa2aos(data.nirs.data.measurementList);
end
end
end
if(~isempty(outfile))
if(~isempty(regexp(outfile,'\.[Hh]5$', 'once')))
saveh5(data,outfile,opt);
elseif(~isempty(regexp(outfile,'\.[Ss][Nn][Ii][Rr][Ff]$', 'once')))
data.nirs.data=forceindex(data.nirs.data,'measurementList');
data.nirs=forceindex(data.nirs,'data');
data.nirs=forceindex(data.nirs,'stim');
data.nirs=forceindex(data.nirs,'aux');
saveh5(data,outfile,opt);
elseif(~isempty(regexp(outfile,'\.[Jj][Nn][Ii][Rr][Ss]$', 'once'))|| ~isempty(regexp(outfile,'\.[Jj][Ss][Oo][Nn]$', 'once')))
savejson('SNIRFData',data,'FileName',outfile,opt);
elseif(regexp(outfile,'\.[Mm][Aa][Tt]$'))
save(outfile,'data');
elseif(regexp(outfile,'\.[Bb][Nn][Ii][Rr][Ss]$'))
savebj('SNIRFData',data,'FileName',outfile,opt);
else
error('only support .snirf, .h5, .jnirs, .bnirs and .mat files');
end
end
% force adding index 1 to the group name for singular struct and cell
function newroot=forceindex(root,name)
newroot=root;
fields=fieldnames(newroot);
idx=find(ismember(fields,name));
if(~isempty(idx) && length(newroot.(name))==1)
newroot.(sprintf('%s1',name))=newroot.(name);
newroot=rmfield(newroot,name);
fields{idx(1)}=sprintf('%s1',name);
newroot=orderfields(newroot,fields);
end