Commit a7167cdd authored by jayd1860's avatar jayd1860
Browse files

v1.15.1

-- Fix bugs in DeleteSnirfFiles and related functions to use absolute paths without having to change current working directory. This is following the new work flow which alloows multiple groups to be loaded into Homer3 from last commit.
-- Fix bugs in demo_snirf and demo_snirf_readfile and refine demo_snirf_readfile to display clearly all fields using new SnirfClass.Info() method and the new Utils/pretty_print_struct.m function.
-- Update README and generate new snirf_homer3.
-- Small tweaks to unit test display of groupResults file paths.
parent 32d4a574
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
function DeleteSnirfFiles(dirname, snirffiles0)

% Syntax:
%
%   DeleteSnirfFiles()
%   DeleteSnirfFiles(dirname)
%   DeleteSnirfFiles(dirname, snirffiles0)
%
% Description:
%   
%   Delete all .snirf files in group folder. The group folder is same
%   concept as in Homer3 so DeleteSnirfFiles will find all .snirf data 
%   acquisition files in folder dirname. If dirname is not supplied it'll 
%   treat the current working directory as the group folder. If snirffiles0
%   if supplied it'll delete.
%
% Examples:
%
%   1. Delete all .snirf files in the Homer3 Examples directory
%
%       DeleteSnirfFiles('C:\jdubb\workspaces\Homer3\DataTree\AcquiredData\Snirf\Examples')
%
%   2. Delete all .snirf files in the current group folder
%
%       DeleteSnirfFiles()
%
%

if ~exist('dirname','var')
    dirname = pwd;
end
+7 −10
Original line number Diff line number Diff line
@@ -10,13 +10,13 @@ function demo_snirf()
%

% Find the Homer3 Examples folder and cd to it
[~, currdir] = findexamplesdir(); 
examplesDir = findexamplesdir(); 

% Delete any previously generated .snirf files to make sure to start from scratch
DeleteSnirfFiles('standalone');
DeleteSnirfFiles(examplesDir, 'standalone');

% Start with .nirs files
nirsfiles = mydir('./*.nirs');
nirsfiles = mydir([examplesDir, '*.nirs']);

fprintf('\n')

@@ -24,9 +24,10 @@ fprintf('\n')
for ii=1:length(nirsfiles)
    
    % Convert sample .nirs file to SNIRF file (*.snirf) 
    [pname,fname,ext] = fileparts([nirsfiles(ii).pathfull, '/', nirsfiles(ii).filename]);    
    fprintf('Converting %s to %s\n', [pname,'/',fname,ext], [pname,'/',fname,'.snirf']);
    [snirf_saved, snirf_loaded, nirs] = snirf_load_save(nirsfiles(ii).name);
    [pname,fname,ext] = fileparts([examplesDir, nirsfiles(ii).name]);
    pname = convertToStandardPath(pname);
    fprintf('Converting %s to %s\n', [pname, fname, ext], [pname, fname, '.snirf']);
    [snirf_saved, snirf_loaded, nirs] = snirf_load_save([pname, nirsfiles(ii).name]);
    
    % Compare the saved and loaded SnirfClass objects, using overloaded == operator 
    if snirf_saved == snirf_loaded
@@ -36,7 +37,3 @@ for ii=1:length(nirsfiles)
    end
    fprintf('\n');
end

% Return to original folder
cd(currdir);
+109 −62
Original line number Diff line number Diff line
function snirf = demo_snirf_readfile(fname)

% Syntax: 
%
%   snirf = demo_snirf_readfile() 
%
% Description:
%
%   Demo of how to read individual SNIRF fields using SnirfClass methods.
%  
% Examples:
% 
%   In each example, make sure the standard Examples .snirf files are there
%   by first running demo_snirf (only once), then call this function.
%
%       demo_snirf
%
%   1. Read and print data from default .snirf file 
%
%       snirf = demo_snirf_readfile();
%
%   2. Read and print data from named files
%
%       snirf = demo_snirf_readfile('Simple_Probe.snirf');
%       snirf = demo_snirf_readfile('FingerTapping_run3_tdmlproc.snirf');
%       snirf = demo_snirf_readfile('neuro_run01.snirf');
%      
%

REPLACE_CODE_WITH_SNIRF_INFO_METHOD = true;

if nargin==0
    fname = 'neuro_run01.snirf';
end
[rootdirexamples, currdir] = findexamplesdir(); 
fnamefullpath = findfile(rootdirexamples, fname, currdir);

rootdirexamples = findexamplesdir(); 
fnamefullpath = findfile(rootdirexamples, fname);
if isempty(fnamefullpath)
    return;
end

%%%%% This is the actual demo showing how to read a .snirf file on the command line

@@ -26,45 +44,69 @@ fnamefullpath = findfile(rootdirexamples, fname, currdir);
snirf = SnirfClass();

% Use the SnirfClass LoadYYYY method to load whichever field you want 
fprintf('This is a demo of how to load data from a SNIRF file and convert it to .nirs style data.\n');
fprintf('========================================================================================\n');
fprintf('Displaying data from %s\n\n', fnamefullpath);


% Load the SNIRF fields into empty snirf class object, one at a time 
snirf.LoadMetaDataTags(fnamefullpath);
snirf.LoadData(fnamefullpath);
snirf.LoadProbe(fnamefullpath);
snirf.LoadStim(fnamefullpath);
snirf.LoadAux(fnamefullpath);

% Read meta data tags from file
fprintf('Read metaDataTags from %s.\n', fnamefullpath);
snirf.LoadMetaDataTags(fnamefullpath,'/nirs');
% The SnirfClass.Info() method replaces the code in the else statement below. It is essentially 
% the same code adapted to work from within the class. We keep the else statement around for 
% easy viewing of code showing how to load, extract and display SnirfClass data
if REPLACE_CODE_WITH_SNIRF_INFO_METHOD

    snirf.Info();

else 
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Load meta data tags from file and extract the tag names and values for display
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fprintf('    MetaDataTags:\n');
    tags = snirf.GetMetaDataTags();
    for ii=1:length(tags)
    fprintf('metaDataTag(%d): {key = ''%s'', value = ''%s''}\n', ii, tags(ii).key, tags(ii).value);
        fprintf('        Meta data tag #%d: {''%s'', ''%s''}\n', ii, tags(ii).key, tags(ii).value);
    end
    fprintf('\n');
    
% Read data matrix
fprintf('Read data field from %s and extract data matrix.\n', fnamefullpath);
snirf.LoadData(fnamefullpath,'/nirs');
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Load data from file and extract .nirs-style d and ml matrices for display
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fprintf('    Data (.nirs-style display):\n');
    for ii=1:length(snirf.data)
        
        % Display data matrix dimensions and data type
        d = snirf.data(ii).GetDataMatrix();
    s = '';
    for jj=1:ndims(d)
        if jj==1
            s = num2str(size(d,jj));
        else
            s = sprintf('%sx%s', s, num2str(size(d,jj)));
        end
    end    
    fprintf('d: %s %s\n', s, class(d));
        pretty_print_struct(d, 8, 1);
        
        % Display meas list dimensions and data type
        ml = snirf.data(ii).GetMeasList();
        pretty_print_struct(ml, 8, 1);
        
    end
    fprintf('\n');
    
    
% Load Probe and extract .nirs-style SD structure
fprintf('Extract SD structure from %s.\n', fnamefullpath);
snirf.LoadProbe(fnamefullpath,'/nirs');
SD = snirf.Get_SD()
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Load probe and extract .nirs-style SD structure
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fprintf('    Probe (.nirs-style display):\n');
    SD = snirf.GetSDG();
    pretty_print_struct(SD, 8, 1);
    fprintf('\n');
    
    
% Read meta stims from file
fprintf('Read stim data from %s.\n', fnamefullpath);
snirf.LoadStim(fnamefullpath,'/nirs');
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Load stim from file and extract it for display
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fprintf('    Stim (.snirf-style display):\n');
    for ii=1:length(snirf.stim)
        fprintf('        stim(%d): {name = ''%s'', data = [', ii, snirf.stim(ii).name);
        for jj=1:size(snirf.stim(ii).data,1)
@@ -79,6 +121,11 @@ end
    fprintf('\n');
    
    
%%%%% Return to original folder
cd(currdir);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Load aux from file and extract nirs-style data for display
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fprintf('    Aux (.nirs-style display):\n');
    aux = snirf.GetAuxiliary();
    pretty_print_struct(aux, 8, 1);
    
end
+2 −5
Original line number Diff line number Diff line
function [rootdirexamples, currdir] = findexamplesdir()
function rootdirexamples = findexamplesdir()

rootdir = fileparts(which('SnirfClass.m'));
rootdirexamples = [rootdir, '/Examples/'];
rootdirexamples(rootdirexamples=='\') = '/';
currdir = pwd;
cd(rootdirexamples);
rootdirexamples = convertToStandardPath([rootdir, '/Examples/']);
+2 −3
Original line number Diff line number Diff line
function fnamefullpath = findfile(rootdirexamples, fname, currdir)
function fnamefullpath = findfile(rootdirexamples, fname)

fnamefullpath = [rootdirexamples, fname];
if ~exist(fname, 'file')
if ~exist(fnamefullpath, 'file')
    msg = sprintf('%s does not exist. You first have to generate it by running demo_snirf\n', fnamefullpath);
    MenuBox(msg, 'OK');
    fnamefullpath = '';
    cd(currdir);
    return;
end
Loading