Home > functions > internal > dsCheckData.m

dsCheckData

PURPOSE ^

CHECKDATA - Standardize data structure and auto-populate missing fields

SYNOPSIS ^

function data = dsCheckData(data, varargin)

DESCRIPTION ^

CHECKDATA - Standardize data structure and auto-populate missing fields

 Usage:
   data=dsCheckData(data)

 Input: DynaSim data structure, data matrix [time x cells], or cell array of data matrices

 Output:
   - DynaSim data structure organization (standardized):
     data.labels           : list of state variables and monitors recorded
     data.(state_variables): state variable data matrix [time x cells]
     data.(monitors)       : monitor data matrix [time x cells]
     data.time             : time vector [time x 1]
     data.simulator_options: simulator options used to generate simulated data
     data.model            : model used to generate simulated data
     [data.varied]         : list of varied model components
     [data.results]        : list of derived data sets created by post-processing

 See also: dsSimulate, dsImport, dsExportData

 Author: Jason Sherfey, PhD <jssherfey@gmail.com>
 Copyright (C) 2016 Jason Sherfey, Boston University, USA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function data = dsCheckData(data, varargin)
0002 %CHECKDATA - Standardize data structure and auto-populate missing fields
0003 %
0004 % Usage:
0005 %   data=dsCheckData(data)
0006 %
0007 % Input: DynaSim data structure, data matrix [time x cells], or cell array of data matrices
0008 %
0009 % Output:
0010 %   - DynaSim data structure organization (standardized):
0011 %     data.labels           : list of state variables and monitors recorded
0012 %     data.(state_variables): state variable data matrix [time x cells]
0013 %     data.(monitors)       : monitor data matrix [time x cells]
0014 %     data.time             : time vector [time x 1]
0015 %     data.simulator_options: simulator options used to generate simulated data
0016 %     data.model            : model used to generate simulated data
0017 %     [data.varied]         : list of varied model components
0018 %     [data.results]        : list of derived data sets created by post-processing
0019 %
0020 % See also: dsSimulate, dsImport, dsExportData
0021 %
0022 % Author: Jason Sherfey, PhD <jssherfey@gmail.com>
0023 % Copyright (C) 2016 Jason Sherfey, Boston University, USA
0024 
0025 %% auto_gen_test_data_flag argin
0026 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0027 if options.auto_gen_test_data_flag
0028   varargs = varargin;
0029   varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0030   varargs(end+1:end+2) = {'unit_test_flag',1};
0031   argin = [{data}, varargs]; % specific to this function
0032 end
0033 
0034 %%
0035 % check data type
0036 if iscell(data) && isnumeric(data{1})
0037   % assume cell array of data matrices [time x cells]
0038   % convert to structure
0039   tmp=data;
0040   clear data
0041   for i=1:numel(tmp)
0042     data.(['pop1_data' num2str(i)])=tmp{i};
0043   end
0044 elseif isnumeric(data)
0045   % assume data matrix [time x cells]
0046   % convert to structure
0047   warning off %warning('off','MATLAB:warn_r14_stucture_assignment');
0048   data.pop1_data=data;
0049   warning on
0050 end
0051 
0052 % check label info
0053 if ~isfield(data,'labels')
0054   % add .labels including all existing fields
0055   data.labels=fieldnames(data);
0056 end
0057 
0058 % check for presence of all data in labels
0059 for f=1:length(data(1).labels)
0060   if ~isfield(data,data(1).labels{f})
0061     error('"%s" not found in data structure. remove from "labels" or add data.',data.labels{f});
0062   end
0063 end
0064 
0065 % check for info on what was varied
0066 if isfield(data,'varied')
0067   for f=1:length(data(1).varied)
0068     if ~isfield(data,data(1).varied{f})
0069       error('"%s" not found in data structure. remove from "varied" or add info on what was varied.',data.varied{f});
0070     end
0071   end
0072 end
0073 
0074 % check for results
0075 if isfield(data,'results')
0076   for f=1:length(data(1).results)
0077     if ~isfield(data,data(1).results{f})
0078       error('"%s" not found in data structure. remove from "results" or add to data structure.',data.results{f});
0079     end
0080   end
0081 end
0082 
0083 % check for time vector and set to index if not present
0084 if ~isfield(data,'time')
0085   dt=.01;
0086   data.time(:,1)=(1:size(data(1).(data(1).labels{1}),1))*dt;
0087   if ~ismember('time',data.labels)
0088     data.labels{end+1}='time';
0089   end
0090 end
0091 
0092 % check for optional fields and set to empty if not present
0093 if ~isfield(data,'simulator_options')
0094   %[data(1:length(data)).simulator_options]=deal([]);
0095 end
0096 if ~isfield(data,'model')
0097   [data(1:length(data)).model]=deal([]);
0098   for i=1:length(data)
0099     data(i).model.specification.populations.name='pop1';
0100     data(i).model.specification.populations.size=size(data(i).(data(i).labels{1}),2);
0101   end
0102 end
0103 
0104 % reorder fields (labels, time, ...)
0105 fields=fieldnames(data);
0106 fields=setdiff(fields,{'labels','time'});
0107 data=orderfields(data,{'labels','time',fields{:}});
0108 
0109 %% auto_gen_test_data_flag argout
0110 if options.auto_gen_test_data_flag
0111   argout = {data}; % specific to this function
0112   
0113   dsUnitSaveAutoGenTestData(argin, argout);
0114 end
0115 
0116 end %fn

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