Commits (4)
function q = merge(o, p) % --*-- Unitary tests --*--
function q = merge(o, p, legacy) % --*-- Unitary tests --*--
% Merge method for dseries objects.
%
% INPUTS
% - o [dseries]
% - p [dseries]
% - legacy [logical] revert to legacy behaviour if `true` (default is `false`),
%
% OUTPUTS
% - q [dseries]
%
% REMARKS
% If dseries objects o and p have common variables, the variables
% in p take precedence except if rewritewithnans is false and p has
% nans.
% in p take precedence except if p has NaNs (the exception can be
% removed by setting the third argument, legacy, equal to true).
% Copyright © 2013-2019 Dynare Team
% Copyright © 2013-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -46,6 +47,10 @@ elseif isempty(o) && isempty(p)
return
end
if nargin<3
legacy = false;
end
if ~isequal(frequency(o), frequency(p))
if isempty(inputname(1))
error('dseries::merge: Cannot merge dseries objects (frequencies are different)!')
......@@ -58,7 +63,9 @@ q = dseries();
[q.name, IBC, ~] = unique([o.name; p.name], 'last');
[list_of_common_variables, iO, iP] = intersect(o.name, p.name);
if ~legacy
[list_of_common_variables, iO, iP] = intersect(o.name, p.name);
end
tex = [o.tex; p.tex];
q.tex = tex(IBC);
......@@ -104,13 +111,15 @@ elseif firstdate(o) >= firstdate(p)
end
Z = [Z1 Z2];
q.data = Z(:,IBC);
if ~isempty(list_of_common_variables)
for i=1:length(iP)
jO = iO(i);
jP = iP(i);
jQ = find(strcmp(o.name{jO}, q.name));
id = isnan(q.data(:,jQ)) & ~isnan(Z1(:,jO)) & isnan(Z2(:,jP));
q.data(id, jQ) = Z1(id,jO);
if ~legacy
if ~isempty(list_of_common_variables)
for i=1:length(iP)
jO = iO(i);
jP = iP(i);
jQ = find(strcmp(o.name{jO}, q.name));
id = isnan(q.data(:,jQ)) & ~isnan(Z1(:,jO)) & isnan(Z2(:,jP));
q.data(id, jQ) = Z1(id,jO);
end
end
end
q_init = firstdate(p);
......@@ -128,13 +137,15 @@ else
end
Z = [Z2 Z1];
q.data = Z(:,IBC);
if ~isempty(list_of_common_variables)
for i=1:length(iP)
jO = iO(i);
jP = iP(i);
jQ = find(strcmp(o.name{jO}, q.name));
id = isnan(q.data(:,jQ)) & isnan(Z1(:,jP)) & ~isnan(Z2(:,jO));
q.data(id, jQ) = Z2(id,jO);
if ~legacy
if ~isempty(list_of_common_variables)
for i=1:length(iP)
jO = iO(i);
jP = iP(i);
jQ = find(strcmp(o.name{jO}, q.name));
id = isnan(q.data(:,jQ)) & isnan(Z1(:,jP)) & ~isnan(Z2(:,jO));
q.data(id, jQ) = Z2(id,jO);
end
end
end
q_init = firstdate(o);
......@@ -247,4 +258,27 @@ q.dates = q_init:q_init+(nobs(q)-1);
%$ t(5) = all(x.dates==dates('1989Q1'):dates('1990Q4'));
%$ end
%$ T = all(t);
%@eof:4
\ No newline at end of file
%@eof:4
%@test:5
%$ % Define two dseries objects.
%$ y = dseries(ones(8,1),'1938Q4', 'u');
%$ z = dseries(NaN(8,1),'1938Q4', 'u');
%$
%$ % Inderectly call merge method via subsasgn.
%$ try
%$ y.u = z.u;
%$ t(1) = true;
%$ catch
%$ t = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(y.vobs, 1);
%$ t(3) = dassert(y.name{1}, 'u');
%$ t(4) = all(isnan(y.data));
%$ t(5) = dassert(y.nobs, z.nobs);
%$ t(6) = dassert(y.dates(1), z.dates(1));
%$ end
%$ T = all(t);
%@eof:5
\ No newline at end of file
......@@ -8,7 +8,7 @@ function A = subsasgn(A,S,B) % --*-- Unitary tests --*--
%! @end deftypefn
%@eod:
% Copyright (C) 2012-2017 Dynare Team
% Copyright © 2012-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -25,7 +25,7 @@ function A = subsasgn(A,S,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
merge_dseries_objects = 1;
merge_dseries_objects = true;
switch length(S)
case 1
......@@ -48,8 +48,8 @@ switch length(S)
error('dseries::subsasgn: (MATLAB/Octave''s regular expressions) Check opening and closing square brackets!')
end
% Loops and regular expressions are not compatible
if length(idArobase) && length(idBracket.open)
error(['dseries::subsasgn: You cannot use implicit loops and regular expressions in the same rule!'])
if ~isempty(idArobase) && ~isempty(idBracket.open)
error('dseries::subsasgn: You cannot use implicit loops and regular expressions in the same rule!')
end
if ~isempty(idArobase)
elements = build_list_of_variables_with_loops({}, idArobase, element, {});
......@@ -76,7 +76,7 @@ switch length(S)
id = find(strcmp(S(1).subs{i},A.name));
if isempty(id)
% Add a new variable a change its name.
B.name(i) = {S(1).subs{i}};
B.name(i) = S(1).subs(i);
B.tex(i) = {name2tex(S(1).subs{i})};
else
% Rename variable and change its content.
......@@ -130,9 +130,9 @@ switch length(S)
error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
end
A.data(tdx,:) = B.data(tdy,:);
merge_dseries_objects = 0;
merge_dseries_objects = false;
elseif isnumeric(B)
merge_dseries_objects = 0;
merge_dseries_objects = false;
if isequal(length(tdx),rows(B))
if isequal(columns(A.data),columns(B))
A.data(tdx,:) = B;
......@@ -152,7 +152,7 @@ switch length(S)
A.tex = name2tex(A.name);
if isempty(A.dates)
if isempty(A.dates.freq)
init = dates('1Y')
init = dates('1Y');
else
init = dates(A.dates.freq, 1, 1);
end
......@@ -174,7 +174,7 @@ switch length(S)
error('dseries::subsasgn: Wrong syntax!')
end
case 2
merge_dseries_objects = 0;
merge_dseries_objects = false;
if ((isequal(S(1).type,'{}') || isequal(S(1).type,'.')) && isequal(S(2).type,'()'))
if isequal(S(1).type,'{}')
sA = extract(A,S(1).subs{:});
......@@ -191,7 +191,7 @@ switch length(S)
end
sA.data(tdx,:) = B.data(tdy,:);
elseif isnumeric(B)
merge_dseries_objects = 0;
merge_dseries_objects = false;
if isequal(length(tdx),rows(B))
if isequal(columns(sA.data),columns(B))
sA.data(tdx,:) = B;
......@@ -215,7 +215,7 @@ switch length(S)
else
error('dseries::subsasgn: Wrong syntax!')
end
A = merge(A,sA);
A = merge(A, sA, true);
else
error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
end
......@@ -231,7 +231,7 @@ if isempty(A)
end
if merge_dseries_objects
A = merge(A,B);
A = merge(A, B, true);
end
%@test:1
......
function initialize_dseries_class()
% Copyright (C) 2015-2019 Dynare Team
% Copyright © 2015-2020 Dynare Team
%
% This code is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
......@@ -16,7 +16,11 @@ function initialize_dseries_class()
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% Get the path to the dseries toolbox.
dseries_src_root = strrep(which('initialize_dseries_class'),'initialize_dseries_class.m','');
dseries_src_root = strrep(which('initialize_dseries_class'), 'initialize_dseries_class.m', '');
% Is the dseries package used as a standalone?
dseries_src_path_s = strsplit(dseries_src_root, filesep());
isstandalone = ~isequal(dseries_src_path_s(end-3:end), {'matlab', 'modules', 'dseries', 'src'}) & isempty(which('dynare'));
% Set the subfolders to be added in the path.
p = {'read'; ...
......@@ -58,7 +62,7 @@ if ~exist('ndim','file')
p{end+1} = 'utilities/missing/ndim';
end
if ~exist('OCTAVE_VERSION') && (~exist('rows','file') || ~exist('columns','file'))
if ~exist('OCTAVE_VERSION', 'builtin') && isstandalone
p{end+1} = 'utilities/missing/dims';
end
......@@ -70,7 +74,7 @@ if ~exist('get_file_extension','file')
p{end+1} = 'utilities/missing/get_file_extension';
end
if exist('OCTAVE_VERSION') && ~exist('user_has_octave_forge_package','file')
if exist('OCTAVE_VERSION', 'builtin') && ~exist('user_has_octave_forge_package','file')
p{end+1} = 'utilities/missing/user_has_octave_forge_package';
end
......@@ -86,23 +90,23 @@ if ~exist('one_sided_hp_filter','file')
p{end+1} = 'utilities/missing/one_sided_hp_filter';
end
if ~exist('OCTAVE_VERSION') && ~exist('ismatrix','builtin')
if ~exist('OCTAVE_VERSION', 'builtin') && ~exist('ismatrix','builtin')
p{end+1} = 'utilities/missing/ismatrix';
end
if ~exist('OCTAVE_VERSION') && ~exist('isrow','builtin')
if ~exist('OCTAVE_VERSION', 'builtin') && ~exist('isrow','builtin')
p{end+1} = 'utilities/missing/isrow';
end
if ~exist('OCTAVE_VERSION') && ~exist('iscolumn','builtin')
if ~exist('OCTAVE_VERSION', 'builtin') && ~exist('iscolumn','builtin')
p{end+1} = 'utilities/missing/iscolumn';
end
if ~exist('OCTAVE_VERSION') && ~exist('strsplit','file')
if ~exist('OCTAVE_VERSION', 'builtin') && ~exist('strsplit','file')
p{end+1} = 'utilities/missing/strsplit';
end
if ~exist('OCTAVE_VERSION') && ~exist('strjoin','file')
if ~exist('OCTAVE_VERSION', 'builtin') && ~exist('strjoin','file')
p{end+1} = 'utilities/missing/strjoin';
end
......