Home > functions > internal > dsModifications2Vary.m

dsModifications2Vary

PURPOSE ^

Adds fields tmpdata.varied and tmpdata.(varied{1}) ...

SYNOPSIS ^

function data = dsModifications2Vary(data,modifications,options,modifications_set,sim)

DESCRIPTION ^

 Adds fields tmpdata.varied and tmpdata.(varied{1}) ...
 tmpdata.varied({N}) to tmpdata. This contains information about
 parameters varied across each sim.

 Usage:
   data = modifications2Vary(data,modifications)
   data = modifications2Vary(data,modifications,options)
   data = modifications2Vary(data,modifications,options,modifications_set,sim)

 Inputs:
   data: DynaSim data structure
   modifications: modifications to make to specification structure
       {X,Y,Z; X,Y,Z; ...}
       X = population name or connection source->target
       Y = thing to modify ('name', 'size', or parameter name)
       set Y=Z if Y = name, size, or value
       Note: (X1,X2) or (Y1,Y2): modify these simultaneously in the same way
       Note: modifications is often found in options.modifications


 Inputs (Optional):
   options : Options structure containing experiment OR precision
   fields (see dsSimulate)
   modifications_set : Additional modifications, produced by
                       dsVary2Modifications(options.vary,model);
   sim : simulation number

 Outputs:
   data: DynaSim data structure

 Examples:
   tmpdata = dsModifications2Vary(tmpdata,options.modifications,options,modifications_set,sim);
   tmp_data = dsModifications2Vary(tmp_data,modifications);


 Author: Dave Stanley; based on prepare_varied_metadata by ??? (Jason
 Sherfey?); Boston University; 2017

 See also: dsVary2Modifications

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function data = dsModifications2Vary(data,modifications,options,modifications_set,sim)
0002     % Adds fields tmpdata.varied and tmpdata.(varied{1}) ...
0003     % tmpdata.varied({N}) to tmpdata. This contains information about
0004     % parameters varied across each sim.
0005     %
0006     % Usage:
0007     %   data = modifications2Vary(data,modifications)
0008     %   data = modifications2Vary(data,modifications,options)
0009     %   data = modifications2Vary(data,modifications,options,modifications_set,sim)
0010     %
0011     % Inputs:
0012     %   data: DynaSim data structure
0013     %   modifications: modifications to make to specification structure
0014     %       {X,Y,Z; X,Y,Z; ...}
0015     %       X = population name or connection source->target
0016     %       Y = thing to modify ('name', 'size', or parameter name)
0017     %       set Y=Z if Y = name, size, or value
0018     %       Note: (X1,X2) or (Y1,Y2): modify these simultaneously in the same way
0019     %       Note: modifications is often found in options.modifications
0020     %
0021     %
0022     % Inputs (Optional):
0023     %   options : Options structure containing experiment OR precision
0024     %   fields (see dsSimulate)
0025     %   modifications_set : Additional modifications, produced by
0026     %                       dsVary2Modifications(options.vary,model);
0027     %   sim : simulation number
0028     %
0029     % Outputs:
0030     %   data: DynaSim data structure
0031     %
0032     % Examples:
0033     %   tmpdata = dsModifications2Vary(tmpdata,options.modifications,options,modifications_set,sim);
0034     %   tmp_data = dsModifications2Vary(tmp_data,modifications);
0035     %
0036     %
0037     % Author: Dave Stanley; based on prepare_varied_metadata by ??? (Jason
0038     % Sherfey?); Boston University; 2017
0039     %
0040     % See also: dsVary2Modifications
0041 
0042 
0043     % add things varied to tmpdata
0044     mods={};
0045     if ~isempty(modifications)
0046       mods=cat(1,mods,expand_modifications(modifications));
0047     end
0048 
0049     if nargin > 3
0050         if ~isempty(modifications_set{sim})
0051           tmp_mods=expand_modifications(modifications_set{sim});
0052           mods=cat(1,mods,tmp_mods);
0053         end
0054     end
0055 
0056     if nargin > 2
0057         if isa(options.experiment,'function_handle')
0058           for j=1:length(data)
0059             data(j).simulator_options.modifications=mods;
0060           end
0061         end
0062     end
0063 
0064     if ~isempty(mods)
0065       if isfield(data,'varied')
0066         varied=data(1).varied;
0067       else
0068         varied={};
0069       end
0070 
0071       for ii=1:size(mods,1)
0072         % prepare valid field name for thing varied:
0073         fld=[mods{ii,1} '_' mods{ii,2}];
0074 
0075         % convert arrows and periods to underscores
0076         fld=regexprep(fld,'(->)|(<-)|(-)|(\.)','_');
0077 
0078         % remove brackets and parentheses
0079         fld=regexprep(fld,'[\[\]\(\)\{\}]','');
0080 
0081         % remove spaces
0082         fld=regexprep(fld,'[\ ]','');
0083 
0084         for j=1:length(data)
0085           data(j).(fld)=mods{ii,3};
0086         end
0087 
0088         if ~ismember(fld,varied)
0089           varied{end+1}=fld;
0090         end
0091       end
0092 
0093       for j=1:length(data)
0094         data(j).varied=varied;
0095       end
0096     end
0097     % convert tmpdata to single precision
0098     if nargin > 2
0099         if strcmp(options.precision,'single')
0100           for j=1:length(data)
0101             for k=1:length(data(j).labels)
0102               fld=data(j).labels{k};
0103               data(j).(fld)=single(data(j).(fld));
0104             end
0105           end
0106         end
0107     end
0108 end
0109 
0110 function modifications=expand_modifications(mods)
0111 % purpose: expand simultaneous modifications into larger list
0112 modifications={};
0113 for i=1:size(mods,1)
0114   % get object list without grouping symbols: ()[]{}
0115   objects=regexp(mods{i,1},'[^\(\)\[\]\{\},]+','match');
0116   variables=regexp(mods{i,2},'[^\(\)\[\]\{\},]+','match');
0117 
0118   for j=1:length(objects)
0119     for k=1:length(variables)
0120       thisMod = mods{i,3};
0121 
0122       if all(size(thisMod) == [1,1]) %same val for each obj and var
0123         modifications(end+1,1:3)={objects{j},variables{k},thisMod};
0124       elseif (size(thisMod,1) > 1) && (size(thisMod,2) == 1) %same val for each obj, diff for each var
0125         modifications(end+1,1:3)={objects{j},variables{k},thisMod(k)};
0126       elseif (size(thisMod,1) == 1) && (size(thisMod,2) > 1) %same val for each var, diff for each obj
0127         modifications(end+1,1:3)={objects{j},variables{k},thisMod(j)};
0128       elseif (size(thisMod,1) > 1) && (size(thisMod,2) > 1) %diff val for each var and obj
0129         modifications(end+1,1:3)={objects{j},variables{k},thisMod(k,j)};
0130       else
0131         error('Unknown modification type (likely due to excess dims)')
0132       end %if
0133     end %k
0134   end %j
0135 end %i
0136 end  %fun

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005