Home > functions > internal > dsMdd2ds.m

dsMdd2ds

PURPOSE ^

% data=MDD2ds(obj,varargin)

SYNOPSIS ^

function data = dsMdd2ds(obj,varargin)

DESCRIPTION ^

% data=MDD2ds(obj,varargin)
 Dependencies:
   Requires the MDD class, which should be part of DynaSim. If not,
   get it here https://github.com/davestanley/MDD
 Author: David Stanley, Boston University, stanleyd@bu.edu

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function data = dsMdd2ds(obj,varargin)
0002 %% data=MDD2ds(obj,varargin)
0003 % Dependencies:
0004 %   Requires the MDD class, which should be part of DynaSim. If not,
0005 %   get it here https://github.com/davestanley/MDD
0006 % Author: David Stanley, Boston University, stanleyd@bu.edu
0007 
0008 % This combines the 2D "vary" sweep into a single dimension. It also
0009 % combines all populations and variables into a single 1D list. Thus, Axis
0010 % 1 is equivalent to Jason's structure array - data(1:9). Axis 4 is
0011 % equivalent to the structure fields in Jason's DynaSim structure.
0012 %
0013 % % % Testing code
0014 % load sample_data_dynasim.mat
0015 % data1=data;
0016 % data2 = data(1);
0017 % d1 = MDD2ds(ds2mdd(data1));
0018 % d2 = MDD2ds(ds2mdd(data2));
0019 % d2b = MDD2ds(squeeze(ds2mdd(data2)));
0020 % % Make sure 1 is identical
0021 % close all;
0022 % dsPlot(data1); dsPlot(d1);
0023 % % Make sure 2 is identical
0024 % dsPlot(data2); dsPlot(d2);
0025 % dsPlot(data2); dsPlot(d2b);
0026 
0027 % If population axis doesn't exist, create it
0028 
0029 
0030 % Add dummy axis for variables if needed
0031 if isempty(obj.findaxis('variables'))
0032     Na = length(obj.axis);
0033     obj.axis(Na+1).name = 'variables';
0034     obj.axis(Na+1).values = {guess_variable_name(obj)};
0035 end
0036 
0037 % Add dummy axis for populations if needed
0038 if isempty(obj.findaxis('populations'))
0039     Na = length(obj.axis);
0040     obj.axis(Na+1).name = 'populations';
0041     obj.axis(Na+1).values = {guess_population_name(obj)};
0042 end
0043 
0044 % Get rid of any empty Dims created by above operations
0045 obj = obj.squeezeRegexp('Dim');
0046 
0047 % Find population and variable axes
0048 pop_axis = obj.findaxis('populations');
0049 var_axis = obj.findaxis('variables');
0050 
0051 % Find varied axes
0052 varied_inds = true(1,ndims(obj)); varied_inds(pop_axis) = false; varied_inds(var_axis) = false;
0053 varied_axis = find(varied_inds);
0054 has_varied = ~isempty(varied_axis);
0055 
0056 % Bring pop and var to front
0057 obj = obj.permute([pop_axis,var_axis, varied_axis(:)']);
0058 
0059 % Find population sizes for each population (will be used MUCH later)
0060 num_pops = size(obj,1);
0061 pop_names = obj.exportAxisVals; pop_names = pop_names{1};
0062 
0063 % Should be populations x variables x varied
0064 
0065 % Merge populations and variables together
0066 obj = obj.mergeDims(1:2);
0067 
0068 % Merge all other varied variables
0069 if has_varied
0070     obj = obj.mergeDims(3:ndims(obj));
0071 end
0072 
0073 % Should now be populations_variables x Dim 1 x varied1_varied2_.... x Dim2
0074 
0075 % Get rid of any leftover axes created by mergeDims
0076 obj = obj.squeezeRegexp('Dim');
0077 
0078 % Should now be populations_variables x varied1_varied2_....
0079 
0080 % Build DynaSim data structure
0081 data = struct;
0082 ax_vals = obj.exportAxisVals;
0083 ax_names = obj.exportAxisNames;
0084 if has_varied
0085     varied = obj.axis(2).axismeta.premerged_names;
0086     varied_vals = obj.axis(2).axismeta.premerged_values;
0087 end
0088 
0089 for j = 1:size(obj,2)                               % Loop through varieds
0090     
0091     % Add actual data
0092     for i = 1:size(obj,1)                           % Loop through populations
0093         data(j).(ax_vals{1}{i}) = obj.data{i,j};
0094     end
0095     
0096     % If there are any varied parameters....
0097     if has_varied
0098         % Add list of varied variables
0099         data(j).varied = varied;
0100 
0101         % Add values of varied variables
0102         for i = 1:length(varied)
0103             if isnumeric(varied_vals{i}(j))
0104                 data(j).(varied{i}) = varied_vals{i}(j);
0105             else
0106                 data(j).(varied{i}) = varied_vals{i}{j};
0107             end
0108         end
0109     end
0110     
0111     % Add other DynaSim info if present
0112     obj_curr = obj.meta.dynasim;
0113     %fc = 'labels'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0114     fc = 'model'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0115     fc = 'simulator_options'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0116     fc = 'time'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0117 end
0118 
0119 % Update data.labels
0120 labels = get_axis_labels(obj,ax_vals);
0121 for j = 1:length(data)
0122     data(j).labels = labels;
0123 end
0124 
0125 % Lastly, update population sizes (data(i).model.specification.populations(j).size)
0126 data = add_pop_sizes(data,obj,num_pops,pop_names);
0127 
0128 data = dsCheckData(data, varargin{:});
0129 
0130 
0131 end
0132 
0133 
0134 function varied_names = only_varieds(all_names)
0135     inds = true(1,length(all_names));
0136     inds(strcmp(all_names,'populations')) = false;
0137     inds(strcmp(all_names,'variables')) = false;
0138     varied_names = all_names(inds);
0139 end
0140 
0141 function data = add_pop_sizes(data,obj,num_pops,pop_names)
0142     % num_pops = size(obj,1);
0143     for i = 1:length(data)
0144         for j = 1:num_pops
0145             obj_temp = obj(['/^' pop_names{j} '/'],i);   % ^ is regexp for begins with
0146 
0147             % Get list of sizes of all variables in this population
0148             pop_sz = cellfun(@(x) size(x,2),obj_temp.data);
0149 
0150             % Find state variable in this population - this one will tell
0151             % us the size of the population
0152             var_names = obj_temp.axis('populations_variables').values;
0153             num_vars = length(var_names);
0154             ind = regexp(lower(var_names),'_v$||_vm$||_x$||_xm$||_y$||_ym$');        % Search for all variables to get ones ending in the name of a state variable
0155             ind = ~cellfun(@isempty,ind);
0156             if all(ind == 0)    % If estimation process failed...
0157                 % warning('State variable name was not one of the following: {V, Vm, X, Xm, Y, Ym}. Defaulting to back up algorithm for guessing.');
0158 
0159                 % Instead, try to find any populations that aren't
0160                 % following the synapse naming convention:
0161                 % (PopPost_PopPre_Variable)
0162                 ind = true(1,num_vars);
0163                 for k = 1:num_pops
0164                     if k == j; continue; end
0165                     searchstr = ['^' pop_names{j} '_' pop_names{k} '_'];
0166                     ind3 = regexp(var_names,searchstr);
0167                     ind3 = ~cellfun(@isempty,ind3);
0168                     ind(ind3) = false;
0169                 end
0170                 ind = ind & pop_sz(:)' ~= 0;
0171 
0172             end
0173             % Select the variable(s) most likely to represent this
0174             % population
0175             pop_sz2 = pop_sz(ind);  % Get rid of empties if any
0176 
0177 
0178     %         % % Make sure that all variables have the same population size
0179     %             % Disabling this error, since some variables with the same
0180     %             % population prefix CAN have different sizes - namely synaptic
0181     %             % state vars will have same size as presynaptic population
0182     %         if any(pop_sz2 ~= mode(pop_sz2)); error('Variables in population %s have differing population sizes',pop_names{j}); end
0183 
0184             % Assign population size to model info
0185             data(i).model.specification.populations(j).size = mode(pop_sz2);
0186         end
0187     end
0188 end
0189 
0190 
0191 function labels = get_axis_labels(obj,ax_vals)
0192     % This approach preserves the ordering in the original list of labels
0193     % (labels_orig). This is important because dsPlot uses the specific
0194     % ordering in order to tell what the core state variables are.
0195     % The current labels are stored in labels_curr. The algorithm is to
0196     % 1. Start with labels_orig and figure out which are already
0197     %    present in labels_curr. Keep these and throw the rest out.
0198     % 2. Any that are not in labels_orig that are in labels_curr will be
0199     %    tacked on at the end
0200 
0201     labels_orig = obj.meta.dynasim.labels;
0202     labels_curr = {ax_vals{1}{:}, 'time'};
0203     ind_keep = false(1,length(labels_orig));
0204     ind_remove = true(1,length(labels_curr));
0205     for i = 1:length(labels_curr)
0206         % If current label found in originals, flag it to keep
0207         ind_temp = strcmp(labels_orig,labels_curr{i});
0208         ind_keep = ind_keep | ind_temp;
0209 
0210         % If it was found, then remove it from the current list
0211         if any(ind_temp)
0212             ind_temp = strcmp(labels_curr,labels_curr{i});
0213             ind_remove(ind_temp) = 0;   % remove from current label
0214         end
0215     end
0216 
0217     labels_orig = labels_orig(ind_keep);
0218     labels_curr = labels_curr(ind_remove);
0219     labels = {labels_orig{:} labels_curr{:}};
0220     
0221     % Make sure 'time' is not the first entry (sometimes this can happen if
0222     % all the labels before "time" get stripped away) by the above
0223     % operations. Having "time" come first causes errors in certain DynaSim
0224     % functions, which use the 1st label to get the variable name, so best
0225     % to avoid this.
0226     if strcmp(labels{1},'time')
0227         labels = circshift(labels,-1);       % Move to back
0228     end
0229     
0230     labels = labels(:)';
0231     
0232     
0233     
0234 end
0235 
0236 
0237 function out = guess_variable_name(obj)
0238     % The first population's state variable should always be the 1st one
0239     % according to DynaSim conventions
0240 
0241     out = dsGet_variables_from_meta(obj);
0242     out = out{1};
0243     if isempty(out)
0244         out = 'v';
0245     end
0246 
0247 end
0248 
0249 
0250 
0251 function out = guess_population_name(obj)
0252     % The first population should always be the 1st label
0253     % according to DynaSim conventions
0254     
0255     out = dsGet_populations_from_meta(obj);
0256     out = out{1};
0257 
0258     if isempty(out)
0259         out = 'E';
0260     end
0261     
0262 end

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