Commit c187f581 authored by jayd1860's avatar jayd1860
Browse files

Merge new feature (v1.58.0) to export and load stim to and from BIDS events TSV files.

parents 93401dc5 37473104
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -10,5 +10,7 @@ don't ask again
% Export HRF Mean Output Style # all child processing elements in one file, one processing element per file
one processing element per file

% Load Stim From TSV File # Yes, No
Yes

% END
+112 −0
Original line number Diff line number Diff line
classdef AcqDataClass < matlab.mixin.Copyable
       
    properties (Access = public)
        bids
    end
    properties (Access = private)
        logger
    end
@@ -76,6 +79,24 @@ classdef AcqDataClass < matlab.mixin.Copyable
    
    methods
        
        % -------------------------------------------------------
        function obj = AcqDataClass(fileobj)
            if nargin == 0
                return
            end
            if iscell(fileobj)
                if ~isempty(fileobj)
                    fileobj = fileobj{1};
                end
            end
            if ~ischar(fileobj)
                fileobj = '';
            end
            obj.LoadBids(fileobj);           
        end


        
        % -------------------------------------------------------
        function Initialize(obj)
            global logger
@@ -83,12 +104,14 @@ classdef AcqDataClass < matlab.mixin.Copyable
        end
        
        
        
        % -------------------------------------------------------
        function err = Error(obj)
            err = obj.GetError();
        end
        
        
        
        % ---------------------------------------------------------
        function msg = GetErrorMsg(obj)
            msg = '';
@@ -106,6 +129,77 @@ classdef AcqDataClass < matlab.mixin.Copyable
        end
        
        
        
        % -------------------------------------------------------
        function err = LoadBids(obj, fileobj)
            err = obj.LoadStimOverride(fileobj);
        end
        

        
        % -------------------------------------------------------
        function status = LoadStimOverride(obj, fileobj)
            global cfg
            status = false;
            cfg = InitConfig(cfg);
            if strcmpi(cfg.GetValue('Load Stim From TSV File'), 'no')
                return
            end
            obj.bids = struct('stim',{{}});            
            if isempty(fileobj)
                return
            end
            [p,f] = fileparts(fileobj);
            if isempty(p)
                p = filesepStandard(pwd);
            end
            k = strfind(f, '_nirs');
            if isempty(k)
                k = length(f)+1;
            end
            fnameTsv = [filesepStandard(p), f(1:k-1), '_events.tsv'];
            file = mydir(fnameTsv);
            if isempty(file)
                return
            end
            obj.bids.stim = readTsv([filesepStandard(p), file(1).name],'numstr2num');
            if isempty(obj.bids.stim)
                return
            end
            s = TsvFile2Snirf(obj.bids.stim);
            obj.stim = s.stim.copy();
            status = true;
        end
        
        
        
        % -------------------------------------------------------
        function err = ReloadStim(obj, fileobj)
            err = 0;
            obj.bids = struct('stim',{{}});            
            if isempty(fileobj)
                return
            end
            [p,f] = fileparts(fileobj);
            if isempty(p)
                p = filesepStandard(pwd);
            end
            k = strfind(f, '_nirs');
            if isempty(k)
                k = length(f)+1;
            end
            fnameTsv = [filesepStandard(p), f(1:k-1), '_events.tsv'];
            file = mydir(fnameTsv);
            if isempty(file)
                return
            end
            obj.bids.stim = readTsv([filesepStandard(p), file(1).name],'numstr2num');
            s = TsvFile2Snirf(obj.bids.stim);
            obj.stim = s.stim.copy();
        end
        
        
        
        % -------------------------------------------------------
        function FreeMemory(obj, filename)
            if ~exist('filename','var')
@@ -118,6 +212,7 @@ classdef AcqDataClass < matlab.mixin.Copyable
        end
        
        
        
        % ---------------------------------------------------------
        function bbox = GetSdgBbox(obj)
            bbox = [];
@@ -150,6 +245,7 @@ classdef AcqDataClass < matlab.mixin.Copyable
        end
        
        
        
        % ----------------------------------------------------------------------------------
        function varval = GetVar(obj, varname)
            if ismethod(obj,['Get_', varname])
@@ -179,6 +275,7 @@ classdef AcqDataClass < matlab.mixin.Copyable
        end
        
        
        
        % ----------------------------------------------------------------------------------
        function t = GetTimeCombined(obj)
            % Function combines the time vectors for all data blocks into one time vectors. 
@@ -204,6 +301,7 @@ classdef AcqDataClass < matlab.mixin.Copyable
        end
        
                
                
        % ----------------------------------------------------------------------------------
        function data = GetStimData(~, ~)
            data = [];
@@ -250,6 +348,20 @@ classdef AcqDataClass < matlab.mixin.Copyable
        
        
        
        % ----------------------------------------------------------------------------------
        function fnameTsv = GetStimTsvFilename(obj)
            fnameTsv = '';
            [pname, fname] = fileparts(obj.GetFilename());
            k = strfind(fname, '_nirs');
            if isempty(k)
                k = length(fname)+1;
            end
            if isempty(fname)
                return;
            end
            fnameTsv = [filesepStandard(pname), fname(1:k-1), '_events.tsv'];
        end
                
    end
    
end
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ for ii = 1:length(datafiles)
    if strcmp(options, 'delete')
        fprintf('Deleting %s\n', [datafiles(ii).rootdir, '/', datafiles(ii).name]);
        delete([datafiles(ii).rootdir, '/', datafiles(ii).name]);
        delete([datafiles(ii).rootdir, '/*_events.tsv']);
    elseif strcmp(options, 'move')
        fprintf('Moving %s to %s\n', [datafiles(ii).rootdir, '/', datafiles(ii).name], [datafiles(ii).rootdir, '/', datafiles(ii).name, '.old']);
        movefile([datafiles(ii).rootdir, '/', datafiles(ii).name], [datafiles(ii).rootdir, '/', datafiles(ii).name, '.old']);
+7 −0
Original line number Diff line number Diff line
function DeleteTsv(rootdir)
if ~exist('rootdir','var')
    rootdir = pwd;
end
rootdir = filesepStandard(rootdir);
Snirf2Tsv(rootdir, 'remove');
+36 −0
Original line number Diff line number Diff line
function Snirf2Tsv(rootdir, options)
if ~exist('rootdir','var')
    rootdir = pwd;
end
if ~exist('options','var')
    options = pwd;
end
rootdir = filesepStandard(rootdir);
files = DataFilesClass(rootdir, 'snirf');
files = files.files;
for ii = 1:length(files)
    if files(ii).IsDir()
        continue
    end
    fname = filesepStandard([files(ii).rootdir, files(ii).name]);
    [pname, fname, ext1] = fileparts(fname);
    k = strfind(fname, '_nirs');
    if isempty(k)
        k = length(fname)+1;
    end
    fnameNew = [filesepStandard(pname), fname(1:k-1), '_events.tsv'];
    src = [filesepStandard(pname), fname, ext1];
    dst = fnameNew;
    if optionExists(options, 'delete') || optionExists(options, 'remove')
        if ispathvalid(dst)
            fprintf('Deleting  %s\n', dst);
            delete(dst);
        end
    else
        fprintf('Converting   %s   to   %s\n', src, dst);
        SnirfFile2Tsv(src, dst, 'removeStim');
    end
end


Loading