comparison m-toolbox/classes/@smodel/setParams.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % SETPARAMS Set the property 'params' AND 'values'
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SETPARAMS Set the property 'params' AND 'values'
5 %
6 % CALL: obj = obj.setParams({'a', 'b'}, {1 2});
7 % obj = obj.setParams({'a', 'b'});
8 % obj = obj.setParams(plist('params', {'a', 'b'}, 'values', {1 2}));
9 % obj = obj.setParams(plist('params', {'a', 'b'}));
10 %
11 % INPUTS: obj - one ltpda model.
12 % pl - to set the name with a plist specify only one plist with
13 % only the key-words 'params' and 'values'.
14 %
15 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'setParams')">Parameters Description</a>
16 %
17 % VERSION: $Id: setParams.m,v 1.14 2011/05/09 11:01:45 mauro Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function varargout = setParams(varargin)
22
23 % Check if this is a call from a class method
24 callerIsMethod = utils.helper.callerIsMethod;
25
26 if callerIsMethod
27 objs = varargin{1};
28 params = varargin{2}; % cell-array
29 vals = varargin{3}; % cell-array
30
31 else
32 % Check if this is a call for parameters
33 if utils.helper.isinfocall(varargin{:})
34 varargout{1} = getInfo(varargin{3});
35 return
36 end
37
38 %%% Collect input variable names
39 in_names = cell(size(varargin));
40 for ii = 1:nargin,in_names{ii} = inputname(ii);end
41
42 [objs, obj_invars, rest] = utils.helper.collect_objects(varargin(:), '', in_names);
43 [pls, invars, rest] = utils.helper.collect_objects(rest(:), 'plist');
44 [pst, invars, rest] = utils.helper.collect_objects(rest(:), 'pest');
45
46 vals = {};
47 params = {};
48 %%% If pls contains only one plist with the two keys 'params' and
49 %%% 'values' then set the property with a plist.
50 if length(pls) == 1 && isa(pls, 'plist') && pls.isparam('params')
51 params = cellstr(find(pls, 'params'));
52 if pls.isparam('values')
53 vals = pls.find('values');
54 if isnumeric(vals)
55 vals = num2cell(vals);
56 end
57 end
58 end
59
60 % Look for the parameter values
61 if numel(rest) == 1 && iscell(rest{1})
62 % setParams({'a1', 'a1'})
63 params = [params rest{1}];
64 elseif numel(rest) == 2 && iscell(rest{1}) && ( iscell(rest{2}) || isnumeric(rest{2}))
65 % setParams({'a1', 'a2'}, [1 2])
66 % setParams({'a1', 'a2'}, {1 2})
67 params = [params rest{1}];
68 if iscell(rest{2})
69 vals = [vals rest{2}];
70 else
71 vals = [vals num2cell(rest{2})];
72 end
73 elseif numel(rest) == 2 && ischar(rest{1}) && isnumeric(rest{2})
74 params = rest(1);
75 vals = rest(2);
76 elseif ~isempty(rest) && iscellstr(rest)
77 params = rest;
78 end
79
80 %%% if params are set with pest object
81 if isa(pst, 'pest')
82 params = pst.names;
83 vals = pst.y;
84 end
85
86 if isempty(params)
87 error('### Please provide at least the params, either directly or inside a plist');
88 end
89
90 % Combine input plists and default PLIST
91 pls = combine(pls, getDefaultPlist());
92
93 end % callerIsMethod
94
95 % Convert 'params' and 'values' into a cell-array
96 if ischar(params)
97 params = cellstr(params);
98 end
99 if ~iscell(vals)
100 vals = num2cell(vals);
101 end
102
103 if ~isempty(vals) && numel(params) ~= numel(vals)
104 error('### Please specify one value per parameter.');
105 end
106
107 %%% decide whether we modify the input smodel object, or create a new one.
108 objs = copy(objs, nargout);
109
110 for objLoop = 1:numel(objs)
111
112 % if 'vals' are not empty but the 'values' of the object then create
113 % default values for object 'values'
114 if ~isempty(vals) && isempty(objs(objLoop).values)
115 objs(objLoop).values = cell(size(objs(objLoop).params));
116 end
117
118 for ll = 1:numel(params)
119 pname = params{ll};
120 % get index of this param
121 idx = strcmp(objs(objLoop).params, pname);
122 if any(idx)
123 % param already exist
124 if ~isempty(vals)
125 objs(objLoop).values{idx} = vals{ll};
126 end
127 else
128 % add the new param
129 objs(objLoop).params = [objs(objLoop).params params(ll)];
130 if ~isempty(vals)
131 objs(objLoop).values = [objs(objLoop).values vals(ll)];
132 else
133 if ~isempty(objs(objLoop).values)
134 objs(objLoop).values = [objs(objLoop).values cell(1)];
135 end
136 end
137 end
138 end
139
140 if ~callerIsMethod
141 plh = pls.pset('params', params);
142 plh.pset('values', vals);
143 objs(objLoop).addHistory(getInfo('None'), plh, obj_invars(objLoop), objs(objLoop).hist);
144 end
145 end
146
147 % Set output
148 varargout = utils.helper.setoutputs(nargout, objs);
149
150 end
151
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 % Local Functions %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %
158 % FUNCTION: getInfo
159 %
160 % DESCRIPTION: Get Info Object
161 %
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164 function ii = getInfo(varargin)
165 if nargin == 1 && strcmpi(varargin{1}, 'None')
166 sets = {};
167 pl = [];
168 else
169 sets = {'Default'};
170 pl = getDefaultPlist();
171 end
172 % Build info object
173 ii = minfo(mfilename, mfilename('class'), 'ltpda', utils.const.categories.helper, '$Id: setParams.m,v 1.14 2011/05/09 11:01:45 mauro Exp $', sets, pl);
174 end
175
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 %
178 % FUNCTION: getDefaultPlist
179 %
180 % DESCRIPTION: Get Default Plist
181 %
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184 function plout = getDefaultPlist()
185 persistent pl;
186 if ~exist('pl', 'var') || isempty(pl)
187 pl = buildplist();
188 end
189 plout = pl;
190 end
191
192 function pl = buildplist()
193 pl = plist();
194
195 % Params
196 p = param({'params', 'A cell-array of parameter names.'}, paramValue.EMPTY_CELL);
197 pl.append(p);
198
199 % Value
200 p = param({'values', 'A cell-array of values, one for each parameter to set.'}, paramValue.EMPTY_CELL);
201 pl.append(p);
202
203 end
204