From 95f202ade96f491949fd887568fc56a83afddd2b Mon Sep 17 00:00:00 2001
From: Laura Nervo <laura.nervo@esrf.fr>
Date: Fri, 13 Dec 2013 11:44:45 +0100
Subject: [PATCH] gtCheckParameters/gtModifyStructure : fixed bug with
 structures with structure as field, handling correctly the filling of those
 parameters by default build_structure  : only comments make_parameters :
 Added debug flag

Signed-off-by: Laura Nervo <laura.nervo@esrf.fr>
---
 zUtil_Parameters/build_structure.m   | 20 +++---
 zUtil_Parameters/gtCheckParameters.m | 98 ++++++++++++++++++----------
 zUtil_Parameters/gtModifyStructure.m | 29 ++++----
 zUtil_Parameters/make_parameters.m   | 17 +++--
 4 files changed, 102 insertions(+), 62 deletions(-)

diff --git a/zUtil_Parameters/build_structure.m b/zUtil_Parameters/build_structure.m
index f903d05b..2c37615f 100644
--- a/zUtil_Parameters/build_structure.m
+++ b/zUtil_Parameters/build_structure.m
@@ -1,29 +1,27 @@
-function structure = build_structure(list,  separator,  debug)
+function structure = build_structure(list, separator, debug)
 % BUILD_STRUCTURE  Builds some structure starting from a list template in 'list'
-%     structure = build_structure(list,  separator,  debug)
-%     -----------------------------------------------------
+%     structure = build_structure(list, [separator], [debug])
+%     -------------------------------------------------------
 %     
 %     INPUT:
-%       list       = <struct>     cell structure with names, description,
-%                                 classes and usertypes columns
-%       separator  = <string>     separator for sub-structures in list struct {'__'}
-%                                 i.e. list.index__strategy --> parameters.index.strategy
+%       list          = <struct>     cell structure with names, description,
+%                                    classes and usertypes columns
+%       separator     = <string>     separator for sub-structures in list struct {'__'}
+%                                    i.e. list.index__strategy --> parameters.index.strategy
 %                                      'strategy' is a sub-structure of 'index'.
-%                                 PS. list.match_calib --> 'match_calib' is a field of
-%                                     'list'. 'calib' is not a sub-structure of 'match'.
+%                                    PS. list.match_calib --> 'match_calib' is a field of
+%                                      'list'. 'calib' is not a sub-structure of 'match'.
 %
 %     OUTPUT:
 %       structure     = <struct>     structure built from 'list'
 %       substructures = <string>     array of sub-structures found in each field of
 %                                    'structure'
 %
-%
 %     Version 001 18-11-2011 by LNervo
 %       For each sub-structure an empty structure is created properly and
 %       the name is given in the output 'substructures'
 
 
-% default separator is '__'
 if ~exist('separator', 'var') || isempty(separator)
     separator = '__';
 end
diff --git a/zUtil_Parameters/gtCheckParameters.m b/zUtil_Parameters/gtCheckParameters.m
index 8ae5510e..00689d90 100644
--- a/zUtil_Parameters/gtCheckParameters.m
+++ b/zUtil_Parameters/gtCheckParameters.m
@@ -40,8 +40,6 @@ function [parameters, tocheck, list] = gtCheckParameters(parameters, section_nam
 global GT_MATLAB_HOME
 
 
-
-
 if ~any( ismember( fieldnames(parameters), section_name ) ) 
     if any( ismember(section_name, {'version','acq','xop','cryst','labgeo','samgeo','prep',...
         'seg','match','index','fsim','rec','fed','oar'}) )
@@ -64,7 +62,7 @@ end
 
 out = GtConditionalOutput(debug);
 
-if diaryFlag
+if (diaryFlag)
     % save output in diary
     dirname = gtGetLastDirName(pwd);
     filename = gtLastFileName([section_name '_check_' dirname],'new','log');
@@ -73,7 +71,7 @@ end
 
 % gets the defaults values
 out.odisp(' ')
-[default_parameters, default_list] = make_parameters(2); % 0.025 sec
+[default_parameters, default_list] = make_parameters(2, debug); % 0.025 sec
 out.odisp(' ')
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -105,12 +103,14 @@ list_tocheck = cell(0,5);
 % Fields used in the code : get only used VARIABLES and update info
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-
 [used_vars, output_all, singles_vars] = get_used_names(output_all, section_name, patterns, false); % 0.050 sec
-
 % take unique used names
 used_names    = unique(singles_vars);
 
+% keep patterns for later (column shape)
+patterns = patterns';
+% find sections relative to section_name
+[~, sections_] = findStringIntoCell(fieldnames(default_list), {section_name});
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Criterion #1 : check used VS saved parameters
@@ -139,29 +139,37 @@ if ~isempty( findValueIntoCell(checkType, {'used', 'all'}) )
 
             sfDisplayParameterUse(found_default_names, tocheck, 'used in the code but default:');
 
-            tmp_par = cell2struct(cell(size(found_default_names)), found_default_names, 1);
-            parameters.(section_name) = gtAddMatFile(parameters.(section_name), tmp_par, false, true, true);
-
+            [ind_, sections_toadd_] = findStringIntoCell(sections_, found_default_names);
+            if ~isempty(sections_toadd_)
+                sections_toadd_(end+1,1) = {section_name};
+            end
+            tmp_list = cell2struct( cellfun(@(num) default_list.(num), sections_toadd_, 'UniformOutput', false), sections_toadd_, 1);
             % modify added parameters that are default
-            [~, tmp_list, ~] = findValueIntoCell(default_list.(section_name), found_default_names);
-            action = repmat({'added to parameters'},size(tmp_list,1),1);
-            parameters.(section_name) = gtModifyStructure(parameters.(section_name), tmp_list, [], 'Adding default missing parameters used in the code');
-            list_tocheck = [list_tocheck; [tmp_list action]];
+            [~, tmp_list.(section_name), ~] = findValueIntoCell(default_list.(section_name), found_default_names);
+            action = repmat({'added to parameters'},size(tmp_list.(section_name),1),1);
+
+            tmp_par = build_structure(tmp_list, '__', debug);
+            parameters.(section_name) = gtAddMatFile(parameters.(section_name), tmp_par.(section_name), false, true, true);
+
+            parameters.(section_name) = gtModifyStructure(parameters.(section_name), tmp_list.(section_name), [], 'Adding default missing parameters used in the code');
+            list_tocheck = [list_tocheck; [tmp_list.(section_name) action]];
         end
 
         % #2 B
         found_notdefault_names = missing_used_names(~default);
         if ~isempty(found_notdefault_names)
             % find info relative to missing fields
-            [~, tocheck, ~] = findValueIntoCell(output_all, found_notdefault_names);
-
+            [~, tocheck, nottocheck] = findValueIntoCell(output_all, found_notdefault_names);
+            found_notdefault_names = setdiff(found_notdefault_names, nottocheck');
+            
             outlist = sfDisplayParameterUse(found_notdefault_names, tocheck, 'used in the code but not default:');
 
             tmp_par = cell2struct(cell(size(found_notdefault_names)), found_notdefault_names, 1);
             parameters.(section_name)   = gtAddMatFile(parameters.(section_name), tmp_par, false, true, true);
             % build list for new pars
-            tmp_list = [found_notdefault_names outlist cellfun(@(num) class(parameters.(section_name).(num)), found_notdefault_names, 'UniformOutput', false) ...
-                       repmat({-1},length(found_notdefault_names),1)];
+            tmp_list = [found_notdefault_names outlist ...
+                        cellfun(@(num) class(parameters.(section_name).(num)), found_notdefault_names, 'UniformOutput', false) ...
+                        repmat({-1},length(found_notdefault_names),1)];
             default_list.(section_name) = [default_list.(section_name); tmp_list];
             % add gtModifyStructure for the added parameters
             [parameters.(section_name), default_list.(section_name)] = gtModifyStructure(parameters.(section_name), default_list.(section_name), -1, 'Adding not default missing parameters used in the code:');
@@ -211,13 +219,19 @@ if ~isempty( findValueIntoCell(checkType, {'default', 'all'}) )
 
             sfDisplayParameterUse(found_default_names, tocheck, 'used in the code but default but not saved:');
 
-            tmp_par = cell2struct(cell(size(found_default_names)), found_default_names, 1);
-            parameters.(section_name) = gtAddMatFile(parameters.(section_name), tmp_par, false, true, true);
-
+            [ind_, sections_toadd_] = findStringIntoCell(sections_, found_default_names);
+            if ~isempty(sections_toadd_)
+                sections_toadd_(end+1,1) = {section_name};
+            end
+            tmp_list = cell2struct( cellfun(@(num) default_list.(num), sections_toadd_, 'UniformOutput', false), sections_toadd_, 1);
             % modify added parameters that are default
-            [~, tmp_list, ~] = findValueIntoCell(default_list.(section_name), found_default_names);
-            action = repmat({'added to parameters'},size(tmp_list,1),1);
-            parameters.(section_name) = gtModifyStructure(parameters.(section_name), tmp_list, [], 'Adding default missing parameters used in the code:\nYou should an expert user; if not, press Quit...');
+            [~, tmp_list.(section_name), ~] = findValueIntoCell(default_list.(section_name), found_default_names);
+            action = repmat({'added to parameters'},size(tmp_list.(section_name),1),1);
+
+            tmp_par = build_structure(tmp_list, '__', debug);
+            parameters.(section_name) = gtAddMatFile(parameters.(section_name), tmp_par.(section_name), false, true, true);
+
+            parameters.(section_name) = gtModifyStructure(parameters.(section_name), tmp_list, [], 'Adding default missing parameters used in the code:\nYou should be an expert user; if not, press Quit...');
             list_tocheck = [list_tocheck; [tmp_list action]];
         end
         
@@ -227,12 +241,24 @@ if ~isempty( findValueIntoCell(checkType, {'default', 'all'}) )
 
             sfDisplayParameterUse(notfound_default_names, [], 'not used in the code but default !');
 
-            [ind, ~, ~] = findValueIntoCell(default_list.(section_name), notfound_default_names);
-            tmp_list = default_list.(section_name);
-            tmp_list_notfound = tmp_list(ind(:,1),:);
-            tmp_list_notfound(:,2) = {tmp_list_notfound(:,2)};
-            tmp_list(ind(:,1),:) = [];
-            default_list.(section_name) = tmp_list;
+            [ind_, sections_toadd_] = findStringIntoCell(sections_, notfound_default_names);
+            if ~isempty(sections_toadd_)
+                sections_toadd_(end+1,1) = {section_name};
+            end
+            tmp_list = cell2struct( cellfun(@(num) default_list.(num), sections_toadd_, 'UniformOutput', false), sections_toadd_, 1);
+            tmp_list_notfound = [];
+            for ii=1:length(sections_toadd_)
+                [~, tmp_list.(sections_toadd_{ii}), ~] = findValueIntoCell(default_list.(sections_toadd_{ii}), notfound_default_names);
+                
+                [ind,~,~]=findValueIntoCell(default_list.(sections_toadd_{ii}), notfound_default_names);
+                if ~isempty(ind)
+                    tmp_list_notfound = [tmp_list_notfound; default_list.(sections_toadd_{ii})(ind(:,1),:)];
+                    default_list.(sections_toadd_{ii})(ind(:,1),:) = [];
+                elseif ismember(sections_toadd_{ii}, sections_(ind_))
+                    tmp_list_notfound = [tmp_list_notfound; default_list.(sections_toadd_{ii})];
+                    default_list = rmfield(default_list, sections_toadd_{ii});
+                end
+            end
             action = repmat({'removed from default'},size(tmp_list_notfound,1),1);
             out.odisp('  Removed from default parameters and default list !')
             list_tocheck = [list_tocheck; [tmp_list_notfound action]];
@@ -277,8 +303,11 @@ if ~isempty( findValueIntoCell(checkType, {'extra', 'all'}) )
             outlist = sfDisplayParameterUse(found_extra_names, tocheck, 'used in the code but not default:');
 
             % build list for new pars
-            tmp_list = [found_extra_names outlist cellfun(@(num) class(parameters.(section_name).(num)), found_extra_names, 'UniformOutput', false) repmat({-2}, length(found_extra_names), 1)];
+            tmp_list = [found_extra_names outlist ...
+                        cellfun(@(num) class(parameters.(section_name).(num)), found_extra_names, 'UniformOutput', false) ...
+                        repmat({-2}, length(found_extra_names), 1)];
             default_list.(section_name) = [default_list.(section_name); tmp_list];
+
             % add gtModifyStructure for the added parameters
             [parameters.(section_name), default_list.(section_name)] = gtModifyStructure(parameters.(section_name), default_list.(section_name), -2, 'Now possible to remove or not extra fields used in the code:');
             [~, tmp_list, ~] = findValueIntoCell(default_list.(section_name), {-2});
@@ -295,8 +324,9 @@ if ~isempty( findValueIntoCell(checkType, {'extra', 'all'}) )
             sfDisplayParameterUse(notfound_extra_names, [], 'saved but not used in the code !');
 
             action = repmat({'removed from parameters'},size(notfound_extra_names,1),1);
-            list_tocheck = [list_tocheck; [notfound_extra_names repmat({''},size(notfound_extra_names,1),1) cellfun(@(num) class(parameters.(section_name).(num)), notfound_extra_names, 'UniformOutput', false) ...
-                                     cell(size(notfound_extra_names,1),1) action]];
+            list_tocheck = [list_tocheck; [notfound_extra_names repmat({''},size(notfound_extra_names,1),1) ...
+                                           cellfun(@(num) class(parameters.(section_name).(num)), notfound_extra_names, 'UniformOutput', false) ...
+                                           cell(size(notfound_extra_names,1),1) action]];
             parameters.(section_name) = rmfield(parameters.(section_name), notfound_extra_names);
         end
 
@@ -321,7 +351,7 @@ if ~isempty(list_tocheck)
     disp(['Parameters to be checked for section ''' section_name ''':'])
     disp(list_tocheck)
 
-    if diaryFlag
+    if (diaryFlag)
         textfile = write_mod_parameters(section_name, list_tocheck);
 
         disp('A log file is saved here:')
@@ -342,7 +372,7 @@ if nargout > 1
     end
 end
 
-if diaryFlag
+if (diaryFlag)
     diary('off')
 end
 
diff --git a/zUtil_Parameters/gtModifyStructure.m b/zUtil_Parameters/gtModifyStructure.m
index a802d007..76f26ec0 100644
--- a/zUtil_Parameters/gtModifyStructure.m
+++ b/zUtil_Parameters/gtModifyStructure.m
@@ -309,18 +309,17 @@ clear ndx2 ndx3 tmpnames
 
 assignin('caller', 'dim', dim);
 
+CellStructure = struct();
 count=0;
 for ii=1:length(fnames)
-    if isempty(list) || any(strcmpi(list, fnames{ii}))
+    if isempty(list) || any(ismember(list, fnames{ii}))
+      
+        % which item from list?
+        count=count+1;
+        
         % get the field value
         val=Structure.(fnames{ii});
 
-        % skip this if it's a structure, or if it's a matrix
-        if isstruct(val) || iscell(val) || (length(size(val))>1 && (size(val,1)>1 && size(val, 2)>1))
-            continue;
-        end
-        % otherwise, count item
-        count=count+1;
         % field name
         CellStructure(count).name       = fnames{ii};
         CellStructure(count).transposed = false; % default
@@ -328,19 +327,25 @@ for ii=1:length(fnames)
         CellStructure(count).color      = 'black'; % default
         CellStructure(count).curr_type  = class(val);
         CellStructure(count).val        = val;
-        
-        % which item from list?
+
         listndx=findValueIntoCell(list, fnames(ii));
         if ~isempty(listndx) && ~isempty(captions) && ~isempty(types)
             CellStructure(count).caption = captions{listndx(:,1)};
             CellStructure(count).s_type  = types{listndx(:,1)};
-            CellStructure(count).type = dim.def_types.(CellStructure(count).s_type);
+            if ~ismember(CellStructure(count).s_type, {'cell','struct'})
+                CellStructure(count).type = dim.def_types.(CellStructure(count).s_type);
+            else
+                CellStructure(count).type = [];
+            end
         else
             gtError(['The type for parameter number ' num2str(count) ' is empty!'])
         end
         
+        % skip this if it's a structure, or if it's a matrix
+        if isstruct(val) || iscell(val) || (length(size(val))>1 && (size(val,1)>1 && size(val, 2)>1))
+            continue;
+        end
         % set up the type of the current parameters
-        
         if ~isempty(val)
             if ~strcmpi(CellStructure(count).curr_type, CellStructure(count).s_type)
                 CellStructure(count).error = true;
@@ -674,7 +679,7 @@ for ii = first : last
             'Style', 'checkbox', ...
             'Value', CellStructure(ii).val,...
             'Position', [dim.bposx+dim.iwidth+dim.bposx+dim.lwidth+dim.bposx+(dim.bwidth/2), dim.bposy, dim.iwidth, dim.bheight]);
-    else
+    elseif ~isempty(CellStructure(ii).type)
       
         if ((CellStructure(ii).type == 1) && ~ischar(CellStructure(ii).val))
             % It is a double and is not converted, yet.
diff --git a/zUtil_Parameters/make_parameters.m b/zUtil_Parameters/make_parameters.m
index edd5519f..13f2df0f 100644
--- a/zUtil_Parameters/make_parameters.m
+++ b/zUtil_Parameters/make_parameters.m
@@ -1,13 +1,14 @@
-function [parameters, list] = make_parameters(version)
+function [parameters, list] = make_parameters(version, debug)
 % MAKE_PARAMETERS  Creates a parameters.mat file with empty fields
-%     [parameters, list] = make_parameters(version)
-%     ---------------------------------------------
+%     [parameters, list] = make_parameters(version, [debug])
+%     ------------------------------------------------------
 %
 %     INPUT:
 %       version    = <double>     -1 (loads a specific parameters.mat file)
 %                                  0 (old parameters (<2011))
 %                                  1 (single phase / old matching parameters)
 %                                  2 (multiple phases parameters)
+%       debug      = <logical>     print comments on screen {false}
 %
 %     OUTPUT:
 %       parameters = <struct>      parameters file
@@ -20,7 +21,13 @@ function [parameters, list] = make_parameters(version)
 %       Creates a .mat file with description for each field in the parameters.mat
 
 
-fprintf('Getting the field list of parameters...')
+if ~exist('debug','var') || isempty(debug)
+    debug = false;
+end
+
+out = GtConditionalOutput(debug);
+
+out.fprintf('Getting the field list of parameters...')
 
 switch (version)
     case -1
@@ -42,6 +49,6 @@ switch (version)
         parameters.version.number = 2;
 end
 
-fprintf('...Done!\n')
+out.fprintf('...Done!\n')
 
 end % end of function
-- 
GitLab