Commit 5a544615 authored by Jay Dubb's avatar Jay Dubb
Browse files

v1.31.15

-- Fix issues with naming data tree naming conflicts. Ask user to rename files that have same name asa the folder containing them to avoid the processed output files overwriting other processed output.
-- Enhance MenuBox.m 'don't ask again' checkbox functionality by accepting option 'ask every time
parent 65ea30c2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -35,4 +35,7 @@ homerOutput
% Output File Name # 
groupResults.mat

% Fix File Name Conflicts # don't ask again, ask every time
ask every time

% END
+126 −20
Original line number Diff line number Diff line
@@ -16,31 +16,48 @@ classdef DataFilesClass < handle
        function obj = DataFilesClass(varargin)
            obj.type = '';
            obj.pathnm = pwd;
            skipconfigfile = false;
            obj.nfiles = 0;
            obj.err = -1;
            obj.errmsg = {};
            
            skipconfigfile = false;
            askToFixNameConflicts = [];
            
            if nargin>0
            if nargin==0
                return
            end            
            if nargin==1
                obj.pathnm = varargin{1};
            end            
            obj.pathnm = filesepStandard(obj.pathnm,'full');

            if nargin>1
            if nargin==2
                obj.pathnm = varargin{1};
                obj.type = varargin{2};
                if obj.type(1)=='.'
                    obj.type(1)='';
            end            
            if nargin==3
                obj.pathnm = varargin{1};
                obj.type = varargin{2};
                if strcmp(varargin{3}, 'standalone')
                    skipconfigfile = true;
                end
            obj.errmsg = {};
            
            if nargin>2
            end            
            if nargin==4
                obj.pathnm = varargin{1};
                obj.type = varargin{2};
                if strcmp(varargin{3}, 'standalone')
                    skipconfigfile = true;
                end
                askToFixNameConflicts = varargin{4};
            end
                        
            obj.config = struct('RegressionTestActive','');
            if skipconfigfile==false
            if obj.type(1)=='.'
                obj.type(1)='';
            end
            obj.pathnm = filesepStandard(obj.pathnm,'full');
            
            % Configuration parameters
            obj.config = struct('RegressionTestActive','','AskToFixNameConflicts',1);
            cfg = ConfigFileClass();
            if skipconfigfile==false
                str = cfg.GetValue('Regression Test Active');
                if strcmp(str,'true')
                    obj.config.RegressionTestActive=true;
@@ -50,11 +67,17 @@ classdef DataFilesClass < handle
            else
                obj.config.RegressionTestActive=false;
            end
            if ~isempty(askToFixNameConflicts)
                obj.config.AskToFixNameConflicts = askToFixNameConflicts;
            elseif strcmp(cfg.GetValue('Fix File Name Conflicts'), sprintf('don''t ask again'))
                obj.config.AskToFixNameConflicts = 0;
            end
            
            if nargin==0
                return;
            end
            
            obj.err = 0;
            obj.files = mydir(obj.pathnm);
            GetDataSet(obj);
        end
@@ -63,9 +86,10 @@ classdef DataFilesClass < handle
        % -----------------------------------------------------------------------------------
        function GetDataSet(obj)
            if exist(obj.pathnm, 'dir')~=7
                error(sprintf('Invalid subject folder: ''%s''', obj.pathnm));
                error('Invalid subject folder: ''%s''', obj.pathnm);
            end
            obj.findDataSet(obj.type);
            obj.ErrorCheckName();
        end

        
@@ -117,6 +141,7 @@ classdef DataFilesClass < handle
        end
        
        
        
        % ----------------------------------------------------
        function getDataFile(obj, filename)
            obj.files = mydir(filename);
@@ -124,8 +149,80 @@ classdef DataFilesClass < handle
        
        
        
        % ----------------------------------------------------
        function ErrorCheckName(obj)
            for ii = length(obj.files):-1:1
                if obj.files(ii).ErrorCheckName()<0
                    q = obj.AskToFixNameConflicts(ii);                    
                    if q == 1
                        obj.files(ii).FixNameConflict();
                    else
                        obj.files(ii).NameConflictFixed();
                    end
                end
                if obj.files(ii).GetError()<0
                    obj.err = -1;
                end
            end
        end
        
        
        
        % --------------------------------------------------------------------------
        function answer = AskToFixNameConflicts(obj, ii)
            answer = 0;
            if obj.config.AskToFixNameConflicts == 0
                obj.files(ii).NameConflictFixed();
                return
            end
            q = MenuBox(obj.GetErrorMsg(ii), {'YES','NO'},[],[],'askEveryTimeOptions');
            if q(1) == 0
                return;
            end
            if length(q)>1 && q(2) == 1
                cfg = ConfigFileClass();
                cfg.SetValue('Fix File Name Conflicts', sprintf('don''t ask again'));
                cfg.Save()
                obj.config.AskToFixNameConflicts = 0;
            end
            if q(1)==2
                obj.files(ii).NameConflictFixed();
            end
            answer = q(1);
        end
        
        
        
        % -----------------------------------------------------
        function errmsg = GetErrorMsg(obj, ii)
            p1      = fileparts(obj.files(ii).GetName());
            [p2,f2] = fileparts(filesepStandard(obj.files(ii).pathfull,'nameonly:file'));
            [~,f3]  = fileparts(p2);
            if isfile_private(obj.files(ii).GetName())
                filetype = 'file';
            else
                filetype = 'folder';
            end
            containingFolder = '';
            if obj.files(ii).GetError() == -1
                containingFolder = p1;
            end
            if obj.files(ii).GetError() == -2
                containingFolder = f2;
            end
            if obj.files(ii).GetError() == -3
                containingFolder = f3;
            end
            msg{1} = sprintf('WARNING: The current %s (%s) has the same name as the folder (%s) containing it. ', filetype, obj.files(ii).GetName(), containingFolder);
            msg{2} = sprintf('All %ss should have a different name than the folder containing them, otherwise ', ['F',filetype(2:end)]);
            msg{3} = sprintf('it may cause incorrect results in processing. Do you want to rename this %s?', filetype);
            errmsg = [msg{:}];            
        end
        

        
        % -------------------------------------------------------
        function pushbuttonLoadDataset_Callback(obj, hObject)
        function pushbuttonLoadDataset_Callback(~, hObject)
            hp = get(hObject,'parent');
            hc = get(hp,'children');
            for ii=1:length(hc)
@@ -172,10 +269,19 @@ classdef DataFilesClass < handle
                    if strcmp(filesepStandard([ps,'/',fs], 'nameonly'), filesepStandard([pd,'/',fd], 'nameonly'))
                        found(ii) = 1;
                        break;
                    else
                        dbgpt = 1;
                    end
                end
            end
        end
        
        
        
        % ----------------------------------------------------------
        function err = GetError(obj)
            err = obj.err;
        end
        
    end
end
+110 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ classdef FileClass < matlab.mixin.Copyable
        datenum

        % FileClass specific properties
        namePrev
        idx
        subjdir
        subjdiridx
@@ -19,11 +20,13 @@ classdef FileClass < matlab.mixin.Copyable
        map2group
        pathfull
        err
        logger
    end
    
    methods

        function obj = FileClass(varargin)
            global logger
            
            obj.name       = '';
            obj.date       = '';
@@ -31,6 +34,7 @@ classdef FileClass < matlab.mixin.Copyable
            obj.isdir      = 0;
            obj.datenum    = 0;
            
            obj.namePrev   = '';
            obj.idx        = 0;
            obj.subjdir    = '';
            obj.subjdiridx = 0;
@@ -38,6 +42,7 @@ classdef FileClass < matlab.mixin.Copyable
            obj.map2group  = struct('iSubj',0,'iRun',0);
            obj.pathfull   = '';
            obj.err        = -1;          % Assume file is not loadable
            obj.logger     = InitLogger(logger);            
            
            if nargin==0
                return;
@@ -87,7 +92,7 @@ classdef FileClass < matlab.mixin.Copyable
        
        
        % -----------------------------------------------------------
        function file_struct = Dirname2Struct(obj, dirnameFull)
        function file_struct = Dirname2Struct(~, dirnameFull)
            file_struct = [];
            [~, dirname] = fileparts(dirnameFull);
            dirs = dir([dirnameFull, '/..']);
@@ -211,6 +216,108 @@ classdef FileClass < matlab.mixin.Copyable
        end

        
        % ----------------------------------------------------
        function err = ErrorCheckName(obj)
            a = obj.name;
            [p1,f1] = fileparts(obj.name);
            [p2,f2] = fileparts(filesepStandard(obj.pathfull,'nameonly:file'));
            [~,f3]  = fileparts(p2);
            if strcmp(f1, p1)
                obj.err = -1;
            end
            if strcmp(f1, f2)
                obj.err = -2;
            end
            if strcmp(f1, f3)
                obj.err = -3;
            end
            err = obj.err;
        end
        
        
                
        % ----------------------------------------------------
        function FixNameConflict(obj)
            if obj.err == 0
                return
            end
            keeptrying = true;
            while keeptrying
                [p,f,e] = fileparts(obj.name);
                suggestedRenaming = obj.SuggestRenaming(f,e);
                [rootpath, newname] = SaveFileGUI(suggestedRenaming, p, '', 'rename');
                newnameFull = [rootpath, newname];
                if isempty(newnameFull)
                    % Means user chnaged mind and canceled. So we call it fixed
                    obj.NameConflictFixed()
                    return;
                end
                if strcmp(newname, [f,e])
                    q = MenuBox('ERROR: The file name has not been renamed. Do you want to try again?', {'YES','NO'});
                    if q==1
                        continue;
                    else
                        return;
                    end
                end
                if pathscompare(obj.name, newnameFull)
                    return;
                end
                if ~isempty(e)
                    d = dir([filesepStandard(p),f,'.*']);
                    [p2,f2] = fileparts(newnameFull);
                    for ii = 1:length(d)
                        [~,~,e1] = fileparts(d(ii).name);
                        obj.logger.Write(sprintf('FileClass: Renaming  %s to %s\n', [filesepStandard(p),f,e1], [filesepStandard(p2),f2,e1]));
                        movefile([filesepStandard(p),f,e1], [filesepStandard(p2),f2,e1]);
                    end
                else
                    obj.logger.Write(sprintf('FileClass: Renaming  %s to %s\n', obj.name, newnameFull));
                    movefile(obj.name, newnameFull);
                end
                obj.namePrev = obj.name;
                obj.name = [filesepStandard(p), newname];
                keeptrying = false;
            end
        end
        
        
        
        % -----------------------------------------------------------------
        function name = SuggestRenaming(~, fname, ext)
            n = 1;
            base = fname;
            if isempty(ext)
                addon = 's';
            else
                addon = 'r';
            end
            name = sprintf('%s_%s%d%s', base, addon, n, ext);
            while ispathvalid(name)
                n = n+1;
                name = sprintf('%s_%s%d%s', base, addon, n, ext);
            end
        end
        
        
        % -----------------------------------------------------
        function NameConflictFixed(obj)
            obj.err = 0;
        end
        
        
        % -----------------------------------------------------
        function name = GetName(obj)
            name = obj.name;
        end
        
        
        % -----------------------------------------------------
        function err = GetError(obj)
            err = obj.err;
        end
        
        
    end
       
end
 No newline at end of file
+16 −12
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ while files.isempty()
    switch fmt
        case {'snirf','.snirf'}
            files = DataFilesClass(dirnameGroup, 'snirf');
            filesSrc = DataFilesClass(dirnameGroup, 'nirs');           
            filesSrc = DataFilesClass(dirnameGroup, 'nirs', '', false);           
            if ~filesSrc.isempty()
                nfolders = length(filesSrc.files)-filesSrc.nfiles;
                if nfolders==0
@@ -125,6 +125,10 @@ while files.isempty()
                    files = DataFilesClass();
                    return;
                end
                
                % Change current folder to new group
                cd(dirnameGroup)
                
            case {'snirfonly'}
                files = [];
        end
+22 −7
Original line number Diff line number Diff line
@@ -222,13 +222,22 @@ classdef DataTreeClass < handle
                
                % Get file names and load them into DataTree
                while length(obj.groups) < iGnew
                    
                    % Find group folder and it's acqiosition files                    
                    obj.files    = FileClass().empty();
                    obj.filesErr = FileClass().empty();
                    
                    dataInit     = DataFilesClass();
                    dataInitPrev = DataFilesClass();
                    iter = 1;
                    while dataInit.GetError() < 0
                        dataInit = FindFiles(obj.dirnameGroups{kk}, fmt, options);
                        if isempty(dataInit) || dataInit.isempty()
                            return;
                        end
                        dataInitPrev(iter) = dataInit;
                        obj.dirnameGroups{kk} = dataInit.pathnm;
                        iter = iter+1;
                    end                    
                    obj.files = dataInit.files;
                                        
                    % Print file and folder numbers stats
@@ -239,6 +248,7 @@ classdef DataTreeClass < handle
                    obj.logger.Write(sprintf('DataTreeClass.FindAndLoadGroups: Found %d data files in %d folders\n', ...
                            dataInit.nfiles, nfolders));
                    
                    % Now load group files to data tree
                    obj.LoadGroup(iGnew, procStreamCfgFile, options);
                    if length(obj.groups) < iGnew
                        if obj.FoundDataFilesInOtherFormat(dataInit, kk)
@@ -247,6 +257,11 @@ classdef DataTreeClass < handle
                            break;
                        end
                    end
                    
                    % Clean up any obsolete files in output folder if names
                    % of files or folder was changed
                    obj.groups(iGnew).CleanUpOutput(dataInitPrev);
                                        
                end
                
            end
Loading