1 function df = df_matassign(df, S, indc, ncol, RHS)
2 %# auxiliary function: assign the dataframe as if it was a matrix
4 %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
6 %% This file is part of Octave.
8 %% Octave is free software; you can redistribute it and/or
9 %% modify it under the terms of the GNU General Public
10 %% License as published by the Free Software Foundation;
11 %% either version 2, or (at your option) any later version.
13 %% Octave is distributed in the hope that it will be useful,
14 %% but WITHOUT ANY WARRANTY; without even the implied
15 %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 %% PURPOSE. See the GNU General Public License for more
19 %% You should have received a copy of the GNU General Public
20 %% License along with Octave; see the file COPYING. If not,
21 %% write to the Free Software Foundation, 51 Franklin Street -
22 %% Fifth Floor, Boston, MA 02110-1301, USA.
25 %# $Id: df_matassign.m 9615 2012-02-10 15:22:57Z cdemills $
30 if (sum (~strcmp (S.subs, ':')) > 2)
31 error("A null assignment can only have one non-colon index.");
33 elseif (sum (~strcmp (S.subs, ':')) > 1)
34 error("A null assignment can only have one non-colon index.");
37 if (strcmp (S.subs(1), ':')) %# removing column/matrix
38 RHS = S; RHS.subs(2) = [];
40 unfolded = df._data{indi}(:, df._rep{indi});
41 unfolded = feval (@subsasgn, unfolded, RHS, []);
42 df._data{indi} = unfolded;
43 if (~isempty (unfolded))
44 df._rep(indi) = 1:size (unfolded, 2);
47 %# remove empty elements
48 indi = cellfun ('isempty', df._data);
49 if (any (indi)) %# nothing left, remove this column
50 df._cnt(2) = df._cnt(2) - sum (indi);
51 indi = ~indi; %# vector of kept data
52 df._name{2} = df._name{2}(indi);
53 df._over{2} = df._over{2}(indi);
54 df._type = df._type(indi);
55 df._data = df._data(indi);
56 df._rep = df._rep(indi);
58 if (size (df._ridx, 3) > 1)
59 df._ridx(:, indc, :) = [];
61 elseif (strcmp (S.subs(2), ':')) %# removing rows
63 if (~isempty (df._name{1}))
64 df._name{1}(indr, :) = [];
65 df._over{1}(indr) = [];
67 df._ridx(indr, :, :) = [];
68 %# to remove a line, iterate on each column
69 df._data = cellfun (@(x) feval(@subsasgn, x, S, []), \
70 df._data, "UniformOutPut", false);
71 if (isa (indr, 'char'))
74 df._cnt(1) = df._cnt(1) - length (indr);
77 df = df_thirddim (df);
81 indc_was_set = ~isempty (indc);
82 if (~indc_was_set) %# initial dataframe was empty
83 ncol = size (RHS, 2); indc = 1:ncol;
87 indr_was_set = ~isempty (indr);
88 %# initial dataframe was empty ?
89 if (~indr_was_set || strcmp (indr, ':'))
91 nrow = max (sum (cellfun ('size', RHS, 1)));
97 nrow = df._cnt(1); %# limit to df numbner of rows
100 %# deduce limit from RHS
101 nrow = size (RHS, 1);
105 elseif (~isempty (indr))
106 if (~isnumeric (indr))
107 %# translate row names to row index
108 [indr, nrow] = df_name2idx (df._name{1}, indr, df._cnt(1), 'row');
111 nrow = length (indr);
114 if (length (S.subs) > 2)
120 rname = cell(0, 0); rname_width = max (1, size (df._name{2}, 2));
121 ridx = []; cname = rname; ctype = rname;
124 if ((length (indc) == df._cnt(2) && size (RHS, 2) >= df._cnt(2)) \
125 || 0 == df._cnt(2) || isempty (S.subs{1}) || isempty (S.subs{2}))
126 %# providing too much information -- remove extra content
127 if (size (RHS, 1) > 1)
128 %# at this stage, verify that the first line doesn't contain
129 %# chars only; use them for column names
130 dummy = cellfun ('class', \
131 RHS(1, ~cellfun ('isempty', RHS(1, :))), \
132 'UniformOutput', false);
133 dummy = strcmp (dummy, 'char');
135 if (length (df._over{2}) >= max (indc) \
136 && ~all (df._over{2}(indc)) && ~isempty (S.subs{2}))
137 warning("Trying to overwrite colum names");
140 cname = RHS(1, :).'; RHS = RHS(2:end, :);
142 nrow = nrow - 1; indr = 1:nrow;
144 %# we know indr, there is no reason that RHS(:, 1) contains
146 if (isempty (S.subs{2}))
147 %# extract columns position from columns names
148 [indc, ncol, S.subs{2}, dummy] = ...
149 df_name2idx (df._name{2}, cname, df._cnt(2), 'column');
150 if (length (dummy) ~= sum (dummy))
151 warning ("Not all RHS column names used");
152 cname = cname(dummy); RHS = RHS(:, dummy);
157 %# at this stage, verify that the first line doesn't contain
158 %# chars only; use them for column types
159 dummy = cellfun ('class', \
160 RHS(1, ~cellfun ('isempty', RHS(1, :))), \
161 'UniformOutput', false);
162 dummy = strcmp (dummy, 'char');
164 if (length (df._over{2}) >= max (indc) \
165 && ~all (df._over{2}(indc)))
166 warning ("Trying to overwrite colum names");
169 ctype = RHS(1, :); RHS = RHS(2:end, :);
171 nrow = nrow - 1; indr = 1:nrow;
176 %# more elements than df width -- try to use the first two as
177 %# row index and/or row name
178 if (size (RHS, 1) > 1)
179 dummy = all (cellfun ('isnumeric', \
180 RHS(~cellfun ('isempty', RHS(:, 1)), 1)));
182 dummy = isnumeric(RHS{1, 1});
184 dummy = dummy && (~isempty (cname) && size (cname{1}, 2) < 1);
186 ridx = cell2mat (RHS(:, 1));
187 %# can it be converted to a list of unique numbers ?
188 if (length (unique (ridx)) == length (ridx))
189 ridx = RHS(:, 1); RHS = RHS(:, 2:end);
190 if (length (df._name{2}) == df._cnt(2) + ncol)
191 %# columns name were pre-filled with too much values
192 df._name{2}(end) = [];
193 df._over{2}(end) = [];
194 if (size (RHS, 2) < ncol)
195 ncol = size (RHS, 2); indc = 1:ncol;
197 elseif (~indc_was_set)
198 ncol = ncol - 1; indc = 1:ncol;
200 if (~isempty (cname)) cname = cname(2:end); endif
201 if (~isempty (ctype)) ctype = ctype(2:end); endif
207 if (size (RHS, 2) > df._cnt(2))
208 %# verify the the first row doesn't contain chars only, use them
210 dummy = cellfun ('class', \
211 RHS(~cellfun ('isempty', RHS(:, 1)), 1), \
212 'UniformOutput', false);
213 dummy = strcmp (dummy, 'char') \
214 && (~isempty (cname) && size (cname{1}, 2) < 1);
216 if (length (df._over{1}) >= max (indr) \
217 && ~all (df._over{1}(indr)))
218 warning("Trying to overwrite row names");
222 rname_width = max ([1; cellfun('size', rname, 2)]);
224 if (length (df._name{2}) == df._cnt(2) + ncol)
225 %# columns name were pre-filled with too much values
226 df._name{2}(end) = [];
227 df._over{2}(end) = [];
228 if (size (RHS, 2) < ncol)
229 ncol = size (RHS, 2); indc = 1:ncol;
231 elseif (~indc_was_set)
232 ncol = ncol - 1; indc = 1:ncol;
234 if (~isempty (cname)) cname = cname(2:end); endif
235 if (~isempty (ctype)) ctype = ctype(2:end); endif
241 %# perform row resizing if columns are already filled
242 if (~isempty (indr) && isnumeric(indr))
243 if (max (indr) > df._cnt(1) && size (df._data, 2) == df._cnt(2))
244 df = df_pad (df, 1, max (indr)-df._cnt(1), rname_width);
248 if (iscell(RHS)) %# we must pad on a column-by-column basis
249 %# verify that each cell contains a non-empty vector, and that sizes
251 %# dummy = cellfun ('size', RHS(:), 2);
252 %# if any (dummy < 1),
253 %# error("cells content may not be empty");
256 %# dummy = cellfun ('size', RHS, 1);
257 %# if any (dummy < 1),
258 %# error("cells content may not be empty");
260 %# if any (diff(dummy) > 0),
261 %# error("cells content with unequal length");
263 %# if 1 < size (RHS, 1) && any (dummy > 1),
264 %# error("cells may only contain scalar");
267 if (size(RHS, 2) > indc)
271 %# the real assignement
272 if (1 == size (RHS, 1)) %# each cell contains one vector
273 fillfunc = @(x) RHS{x};
274 idxOK = logical(indr);
275 else %# use cell2mat to pad on a column-by-column basis
276 fillfunc = @(x) cell2mat (RHS(:, x));
281 if (indc(indi) > df._cnt(2))
282 %# perform dynamic resizing one-by-one, to get type right
283 if (isempty (ctype) || length (ctype) < indc(indi))
284 df = df_pad(df, 2, indc(indi)-df._cnt(2), class(RHS{1, indj}));
286 df = df_pad(df, 2, indc(indi)-df._cnt(2), ctype{indj});
289 if (nrow == df._cnt(1))
292 if (size (RHS, 1) <= 1)
293 switch df._type{indc(indi)}
294 case {'char' } %# use a cell array to hold strings
295 dummy = RHS(:, indj);
297 dummy = fillfunc (indj);
299 dummy = cast(fillfunc (indj), df._type{indc(indi)});
302 %# keeps indexes in sync as cell elements may be empty
303 idxOK = ~cellfun ('isempty', RHS(:, indj));
304 %# intialise dummy so that it can receive "anything"
306 switch (df._type{indc(indi)})
307 case {'char' } %# use a cell array to hold strings
308 dummy = RHS(:, indj);
310 dummy(idxOK, :) = fillfunc (indj); dummy(~idxOK, :) = NA;
312 dummy(idxOK, :) = fillfunc (indj); dummy(~idxOK, :) = NA;
313 dummy = cast(dummy, df._type{indc(indi)});
318 sprintf ("Assignement failed for colum %d, of type %s and length %d,\nwith new content\n%s", \
319 indj, df._type{indc(indi)}, length (indr), disp (RHS(:, indj)));
322 if (size (dummy, 1) < df._cnt(1))
323 dummy(end+1:df._cnt(1), :) = NA;
326 %# partial assignement -- extract actual data and update
327 dummy = df._data{indc(indi)};
329 switch (df._type{indc(indi)})
330 case {'char' } %# use a cell array to hold strings
331 dummy(indr, 1) = RHS(:, indj);
333 dummy(indr, :) = fillfunc (indj);
335 dummy(indr, :) = cast(fillfunc (indj), df._type{indc(indi)});
339 sprintf ("Assignement failed for colum %d, of type %s and length %d,\nwith new content\n%s", \
340 indj, df._type{indc(indi)}, length (indr), disp(RHS(:, indj)));
344 df._data{indc(indi)} = dummy; df._rep{indc(indi)} = 1:size (dummy, 2);
349 %# RHS is either a numeric, either a df
350 if (any (indc > min (size (df._data, 2), df._cnt(2))))
351 df = df_pad(df, 2, max (indc-min (size (df._data, 2), df._cnt(2))),\
354 if (~isempty (inds) && isnumeric(inds) && any (inds > 1))
355 for indi = (1:length (indc))
356 if (max (inds) > length (df._rep{indc(indi)}))
357 df = df_pad(df, 3, max (inds)-length (df._rep{indc(indi)}), \
363 if (isa (RHS, 'dataframe'))
366 if (any (~isna(RHS._ridx)))
367 df._ridx = feval(@subsasgn, df._ridx, S, RHS._ridx);
369 %# skip second dim and copy data
370 S.subs(2) = []; Sorig = S;
371 for indi = (1:length (indc))
372 [df, S] = df_cow(df, S, indc(indi));
373 if (strcmp (df._type(indc(indi)), RHS._type(indi)))
375 df._data{indc(indi)} = feval(@subsasgn, df._data{indc(indi)}, S, \
376 RHS._data{indi}(:, RHS._rep{indi}));
378 disp(lasterr()); disp('line 516 ???'); keyboard
381 df._data{indc(indi)} = feval(@subsasgn, df._data{indc(indi)}, S, \
382 cast(RHS._data{indi}(:, RHS._rep{indi}),\
383 df._type(indc(indi))));
387 if (~isempty (RHS._name{1}))
388 df._name{1}(indr) = genvarname(RHS._name{1}(indr));
389 df._over{1}(indr) = RHS._over{1}(indr);
391 if (~isempty (RHS._src))
392 if (~any (strcmp (cellstr(df._src), cellstr(RHS._src))))
393 df._src = vertcat(df._src, RHS._src);
396 if (~isempty (RHS._cmt))
397 if (~any (strcmp (cellstr(df._cmt), cellstr(RHS._cmt))))
398 df._cmt = vertcat(df._cmt, RHS._cmt);
403 %# RHS is homogenous, pad at once
404 if (isvector (RHS)) %# scalar - vector
405 if (isempty (S.subs))
406 fillfunc = @(x, y) RHS;
408 %# ignore 'column' dimension -- force colum vectors -- use a
409 %# third dim just in case
410 if (isempty (S.subs{1})) S.subs{1} = ':'; endif
412 if (length (S.subs) < 2)
415 if (length (indc) > 1 && length (RHS) > 1)
416 %# set a row from a vector
417 fillfunc = @(x, S, y) feval (@subsasgn, x, S, RHS(y));
419 fillfunc = @(x, S, y) feval (@subsasgn, x, S, RHS);
423 for indi = (1:length (indc))
425 [df, S] = df_cow(df, S, indc(indi));
426 df._data{indc(indi)} = fillfunc (df._data{indc(indi)}, S, indi);
430 disp('line 470 '); keyboard
433 # if ndims(df._data{indc(indi)}) > 2,
434 # %# upstream forgot to give the third dim
435 # dummy = S; dummy.subs(3) = 1;
436 # df._data{indc(indi)} = fillfunc(df._data{indc(indi)}, \
439 # rethrow(lasterr());
443 else %# 2D - 3D matrix
444 S.subs(2) = []; %# ignore 'column' dimension
445 if (isempty (S.subs{1}))
448 %# rotate slices in dim 1-3 to slices in dim 1-2
449 fillfunc = @(x, S, y) feval(@subsasgn, x, S, squeeze(RHS(:, y, :)));
451 for indi = (1:length (indc))
452 [df, S] = df_cow(df, S, indc(indi));
453 df._data{indc(indi)} = fillfunc (df._data{indc(indi)}, S, indi);
457 if (indi < size (RHS, 2) && ~isa (RHS, 'char'))
458 warning (' not all columns of RHS used');
463 %# delayed row padding -- column padding occured before
464 if (~isempty (indr) && isnumeric (indr))
465 if (max (indr) > df._cnt(1) && size (df._data, 2) < df._cnt(2))
466 df = df_pad(df, 1, max (indr)-df._cnt(1), rname_width);
470 %# adjust ridx and rnames, if required
473 if (1 == size (RHS, 1))
474 dummy(indr) = ridx{1};
476 dummy(indr) = vertcat(ridx{indr});
478 if (length (unique (dummy)) ~= length (dummy)) %# || \
479 %# any (diff(dummy) <= 0),
480 error("row indexes are not unique or not ordered");
485 if (~isempty (rname) && (length (df._over{1}) < max (indr) || \
486 all (df._over{1}(indr))))
487 df._name{1}(indr, 1) = genvarname(rname);
488 df._over{1}(1, indr) = false;
490 if (~isempty (cname) && (length (df._over{2}) < max (indc) || \
491 all (df._over{2}(indc))))
493 df._name{2}(indc, 1) = genvarname (cname);
495 disp('line 472 '); keyboard
497 df._over{2}(1, indc) = false;
500 df = df_thirddim (df);