extract.m 6.24 KB
Newer Older
1
function p = extract(o, varargin) % --*-- Unitary tests --*--
2
3

% Extract some variables from a database.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
4

5
% Copyright (C) 2012-2017 Dynare Team
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

22
23
useimplicitloops = false;
useregularexpression = false;
24
usewildcardparameter = false;
25

26
p = dseries();
27
28
29
30
31

% Get the names of the variables to be extracted from dseries object B.
VariableName_ = {};
for i=1:nargin-1
    VariableName = varargin{i};
32
    % Implicit loop.
33
34
35
36
    idArobase = strfind(VariableName,'@');
    if mod(length(idArobase),2)
        error('dseries::extract: (Implicit loops) The number of @ symbols must be even!')
    end
37
38
39
    if ~isempty(idArobase)
        useimplicitloops = true;
    end
40
    % Regular expression.
41
42
    idBracket.open = strfind(VariableName,'[');
    idBracket.close = strfind(VariableName,']');
43
44
45
46
    if ~isempty(idBracket.open) && ~isempty(idBracket.close)
        if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName))
            useregularexpression = true;
        end
47
    end
48
    % Wildcard parameter.
49
    if ~useregularexpression
50
        idStar = strfind(VariableName,'~');
51
52
53
54
        if ~isempty(idStar)
            usewildcardparameter = true;
            useregularexpression = true;
            VariableName = sprintf('[%s]', VariableName);
55
            VariableName = strrep(VariableName, '~', '\w*');
56
57
58
59
60
61
62
        end
    end
    % Check that square brackets are not used, unless extract method is called with a regular expression.
    if ~useregularexpression && ~(isempty(idBracket.open) && isempty(idBracket.close))
        error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!')
    end
    if useregularexpression && usewildcardparameter && ~(isempty(idBracket.open) && isempty(idBracket.close))
63
64
65
        error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!')
    end
    % Check that square brackets in regular expressions
66
    if useregularexpression && ~usewildcardparameter && ~isequal(length(idBracket.open),length(idBracket.open))
Houtan Bastani's avatar
Houtan Bastani committed
67
        error('dseries::extract: (MATLAB/Octave''s regular expressions) Check opening and closing square brackets!')
68
    end
69
    % Loops and regular expressions are not compatible
70
71
    if useregularexpression && useimplicitloops
        error('dseries::extract: You cannot use implicit loops and regular expressions in the same rule!')
72
73
    end
    % Update the list of variables.
74
    if useimplicitloops
75
        VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_);
76
    elseif useregularexpression
77
        VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1), usewildcardparameter);
78
79
80
81
82
83
84
85
86
87
88
    else
        VariableName_ = varargin(:);
    end
end

% Remove trailing white spaces if any
VariableName_ = strtrim(VariableName_);

% Get indices of the selected variables
idVariableName = NaN(length(VariableName_),1);
for i = 1:length(idVariableName)
89
    idx = find(strcmp(VariableName_{i},o.name));
90
91
92
93
94
95
    if isempty(idx)
        error(['dseries::extract: Variable ' VariableName_{i} ' is not a member of ' inputname(1) '!'])
    end
    idVariableName(i) = idx;
end

96
97
98
99
p.data = o.data(:,idVariableName);
p.dates = o.dates;
p.name = o.name(idVariableName);
p.tex = o.tex(idVariableName);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
100
p.ops = o.ops(idVariableName);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
101

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
%@test:1
%$ % Define a data set.
%$ A = rand(10,24);
%$
%$ % Define names
%$ A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';};
%$
%$ % Instantiate a time series object.
%$ ts1 = dseries(A,[],A_name,[]);
%$
%$ % Call the tested method.
%$ a = ts1{'GDP_@1,2,3,4,5@'};
%$ b = ts1{'@GDP,HICP@_1'};
%$
%$ % Expected results.
%$ e1.data = A(:,1:5);
%$ e1.nobs = 10;
%$ e1.vobs = 5;
%$ e1.name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'};
%$ e1.freq = 1;
%$ e1.init = dates(1,1);
%$ e2.data = A(:,[1, 13]);
%$ e2.nobs = 10;
%$ e2.vobs = 2;
%$ e2.name = {'GDP_1';'HICP_1'};
%$ e2.freq = 1;
%$ e2.init = dates(1,1);
%$
%$ % Check results.
%$ t(1) = dassert(e1.data,a.data);
%$ t(2) = dassert(e1.nobs,a.nobs);
%$ t(3) = dassert(e1.vobs,a.vobs);
%$ t(4) = dassert(e1.name,a.name);
%$ t(5) = dassert(e1.init,a.init);
%$ t(6) = dassert(e2.data,b.data);
%$ t(7) = dassert(e2.nobs,b.nobs);
%$ t(8) = dassert(e2.vobs,b.vobs);
%$ t(9) = dassert(e2.name,b.name);
%$ t(10) = dassert(e2.init,b.init);
%$ T = all(t);
%@eof:1


%@test:2
%$ % Define a data set.
%$ A = rand(10,24);
%$
%$ % Define names
%$ A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';};
%$
%$ % Instantiate a time series object.
%$ ts1 = dseries(A,[],A_name,[]);
%$
%$ % Call the tested method.
%$ try
%$   a = ts1{'GDP_@1,2,3,4,55@'};
%$   t = 0;
%$ catch
%$   t = 1;
%$ end
%$
%$ T = all(t);
%@eof:2


%@test:3
%$ % Define a data set.
%$ A = rand(10,24);
%$
%$ % Define names
%$ A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';};
%$
%$ % Instantiate a time series object.
%$ ts1 = dseries(A,[],A_name,[]);
%$
%$ % Call the tested method.
%$ try
%$   a = ts1{'@GDP,HICP@_@1,2,3,4,5@'};
%$   t = 1;
%$ catch
%$   t = 0;
%$ end
%$
%$ if t(1)
%$   t(2) = dassert(a.name,{'GDP_1';'GDP_2';'GDP_3';'GDP_4';'GDP_5';'HICP_1';'HICP_2';'HICP_3';'HICP_4';'HICP_5'});
%$ end
%$
%$ T = all(t);
190
%@eof:3