diff --git a/src/initialize_mdbnomics.m b/src/initialize_mdbnomics.m index 3665002858838660e0ce1bc066c706ab9aadc3f5..a850b622c24ad357d7e1d6e3d13b83aed0bb64f1 100644 --- a/src/initialize_mdbnomics.m +++ b/src/initialize_mdbnomics.m @@ -21,8 +21,7 @@ function initialize_mdbnomics() mdbnomics_src_root = strrep(which('initialize_mdbnomics'), 'initialize_mdbnomics.m', ''); % Set the subfolders to be added in the path. -p = {'utils' ; ... - '/../contrib/jsonlab'}; +p = {'utils'}; % Add missing routines if dynare is not in the path if ~exist('OCTAVE_VERSION', 'builtin') || ~exist('contains','builtin') @@ -49,9 +48,14 @@ end P = cellfun(@(c)[mdbnomics_src_root c], p, 'uni', false); addpath(P{:}); -% Check minimal MATLAB and Octave requirements. +% Check minimal MATLAB requirements. if matlab_ver_less_than('8.5') error('The minimum MATLAB requirement of this package is R2015a.'); end +% Add jsonlab if MATLAB version < R2016b +if matlab_ver_less_than('9.1') + addpath([mdbnomics_src_root '/../contrib/jsonlab']); +end + assignin('caller', 'mdbnomics_src_root', mdbnomics_src_root); diff --git a/src/utils/flatten_editor_series.m b/src/utils/flatten_editor_series.m index 687b3f276c5f5f5054e233171817f9b86cc44b5c..d6d333bcb642801889065a4805d072d7aafacd16 100644 --- a/src/utils/flatten_editor_series.m +++ b/src/utils/flatten_editor_series.m @@ -1,47 +1,47 @@ -function series = flatten_editor_series(series, dbnomics_series) -% function flatten_editor_series(series, dbnomics_series) -% Adapts Time Series Editor series attributes to ease cell array construction. -% -% INPUTS -% series [struct] struct of the filtered series returned by the POST request -% dbnomics_series [struct] struct of the original series returned by the API request -% -% OUTPUTS -% series -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2020 Dynare Team -% -% 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/>. - -series = normalize_period(series); -series = normalize_value(series); - -series.x_frequency = series.frequency; -series = rmfield(series, 'frequency'); -orig_fields = {'provider_code', 'dataset_code', 'dataset_name'}; -for ii = 1:length(orig_fields) - series.(orig_fields{ii}) = dbnomics_series.(orig_fields{ii}); -end - -series.series_code = [dbnomics_series.series_code '_filtered']; -if isfield(dbnomics_series, 'series_name') - series.series_name = [dbnomics_series.series_name, ' (filtered)']; -end -series.filtered = true; -end \ No newline at end of file +function series = flatten_editor_series(series, dbnomics_series) +% function flatten_editor_series(series, dbnomics_series) +% Adapts Time Series Editor series attributes to ease cell array construction. +% +% INPUTS +% series [struct] struct of the filtered series returned by the POST request +% dbnomics_series [struct] struct of the original series returned by the API request +% +% OUTPUTS +% series +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2020 Dynare Team +% +% 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/>. + +series = normalize_period(series); +series = normalize_value(series); + +series.x_frequency = series.frequency; +series = rmfield(series, 'frequency'); +orig_fields = {'provider_code', 'dataset_code', 'dataset_name'}; +for ii = 1:length(orig_fields) + series.(orig_fields{ii}) = dbnomics_series.(orig_fields{ii}); +end + +series.series_code = [dbnomics_series.series_code '_filtered']; +if isfield(dbnomics_series, 'series_name') + series.series_name = [dbnomics_series.series_name, ' (filtered)']; +end +series.filtered = true; +end diff --git a/src/utils/iter_filtered_series.m b/src/utils/iter_filtered_series.m index fae3a73e28cc17d5609020ddee53783b72e57db9..6a1efaa791e724c3e56f8df2e3694209820201ea 100644 --- a/src/utils/iter_filtered_series.m +++ b/src/utils/iter_filtered_series.m @@ -1,82 +1,82 @@ -function filtered_series_list = iter_filtered_series(series_list, dbnomics_filters, apply_endpoint_url) -% function iter_filtered_series(series_list, dbnomics_filters, apply_endpoint_url) -% Adapts series to make POST request. Returns cell array of filtered series. -% -% INPUTS -% series_list [cell array] cell array of series previously requested -% dbnomics_filters [string] string array of filters to apply on series -% apply_endpoint_url [string] modified editor API link -% -% OUTPUTS -% filtered_series_list -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2020 Dynare Team -% -% 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/>. - -editor_apply_endpoint_nb_series_per_post = 100; -opts = weboptions('ContentType','json', 'MediaType','application/json', 'RequestMethod','POST'); - -if size(series_list, 2) > editor_apply_endpoint_nb_series_per_post - grouped_series = mat2cell(series_list,1,repmat(editor_apply_endpoint_nb_series_per_post, size(series_list,1), size(series_list,2))); -else - grouped_series = {series_list}; -end - -for gg = 1:size(grouped_series, 2) - series_list = grouped_series{gg}; - posted_series_list = cell(1,size(series_list,2)); - series_fields = {'x_frequency', 'period_start_day', 'value'}; - posted_series_fields = {'frequency', 'period_start_day', 'value'}; - for series = 1:size(series_list, 2) - if ~iscell(series_list{series}.value) - series_list{series}.value = num2cell(series_list{series}.value); - end - for ii = 1:length(posted_series_fields) - posted_series.(posted_series_fields{ii}) = series_list{series}.(series_fields{ii}); - end - posted_series_list{series} = posted_series; - end - - if matlab_ver_less_than('9.8') - posted_series_list = savejson('',posted_series_list, 'Compact', 1); - posted_series_list = regexprep(posted_series_list,{' [[',']]'},{'[',']'}); - else - posted_series_list = jsonencode(posted_series_list); - end - - json_request = sprintf('{"filters":%s,"series":%s}', dbnomics_filters, posted_series_list); - - try - response = webwrite(apply_endpoint_url, json_request, opts); - catch ME - error_message = ['Could not fetch data from URL: ' apply_endpoint_url ' because: ' ME.identifier]; - if strcmp(ME.identifier, 'MATLAB:webservices:HTTP400StatusCodeError') - error_message = sprintf('%s.\nPlease revise your input for the filter.', error_message); - end - error(error_message); - end - - filtered_series_list = cell(1,size(series_list,2)); - for ii = 1:length(response.filter_results) - filtered_series = flatten_editor_series(response.filter_results(ii).series, series_list{ii}); - filtered_series_list{ii} = filtered_series; - end -end -end \ No newline at end of file +function filtered_series_list = iter_filtered_series(series_list, dbnomics_filters, apply_endpoint_url) +% function iter_filtered_series(series_list, dbnomics_filters, apply_endpoint_url) +% Adapts series to make POST request. Returns cell array of filtered series. +% +% INPUTS +% series_list [cell array] cell array of series previously requested +% dbnomics_filters [string] string array of filters to apply on series +% apply_endpoint_url [string] modified editor API link +% +% OUTPUTS +% filtered_series_list +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2020 Dynare Team +% +% 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/>. + +editor_apply_endpoint_nb_series_per_post = 100; +opts = weboptions('ContentType','json', 'MediaType','application/json', 'RequestMethod','POST'); + +if size(series_list, 2) > editor_apply_endpoint_nb_series_per_post + grouped_series = mat2cell(series_list,1,repmat(editor_apply_endpoint_nb_series_per_post, size(series_list,1), size(series_list,2))); +else + grouped_series = {series_list}; +end + +for gg = 1:size(grouped_series, 2) + series_list = grouped_series{gg}; + posted_series_list = cell(1,size(series_list,2)); + series_fields = {'x_frequency', 'period_start_day', 'value'}; + posted_series_fields = {'frequency', 'period_start_day', 'value'}; + for series = 1:size(series_list, 2) + if ~iscell(series_list{series}.value) + series_list{series}.value = num2cell(series_list{series}.value); + end + for ii = 1:length(posted_series_fields) + posted_series.(posted_series_fields{ii}) = series_list{series}.(series_fields{ii}); + end + posted_series_list{series} = posted_series; + end + + if matlab_ver_less_than('9.1') + posted_series_list = savejson('',posted_series_list, 'Compact', 1); + posted_series_list = regexprep(posted_series_list,{' [[',']]'},{'[',']'}); + else + posted_series_list = jsonencode(posted_series_list); + end + + json_request = sprintf('{"filters":%s,"series":%s}', dbnomics_filters, posted_series_list); + + try + response = webwrite(apply_endpoint_url, json_request, opts); + catch ME + error_message = ['Could not fetch data from URL: ' apply_endpoint_url ' because: ' ME.identifier]; + if strcmp(ME.identifier, 'MATLAB:webservices:HTTP400StatusCodeError') + error_message = sprintf('%s.\nPlease revise your input for the filter.', error_message); + end + error(error_message); + end + + filtered_series_list = cell(1,size(series_list,2)); + for ii = 1:length(response.filter_results) + filtered_series = flatten_editor_series(response.filter_results(ii).series, series_list{ii}); + filtered_series_list{ii} = filtered_series; + end +end +end diff --git a/tests/runalltests.m b/tests/runalltests.m index c23d3c305dc6db395ddbd13d2dacee01360a0db7..1f194a43fc379bb2419d83cd9299464c2a8a3192 100644 --- a/tests/runalltests.m +++ b/tests/runalltests.m @@ -1,64 +1,64 @@ -function runalltests() - -% Copyright (C) 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 -% the Free Software Foundation, either version 3 of the License, or -% (at your option) any later version. -% -% Dynare dseries submodule 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/>. -mdbnomics_src_root = []; % Will be updated by calling initialize_dseries_class(). - -opath = path(); - -system('rm -f failed'); -system('rm -f pass'); - -% Check that the m-unit-tests module is available. -try - initialize_unit_tests_toolbox; -catch - error('Missing dependency: m-unit-tests module is not available.') -end - -% Get path to the current script -unit_tests_root = strrep(which('runalltests'),'runalltests.m',''); - -% Initialize the mdbnomics module -try - initialize_mdbnomics(); -catch - addpath([unit_tests_root '../src']); - initialize_mdbnomics(); -end - -warning off - -if isoctave() - if ~user_has_octave_forge_package('io') - error('Missing dependency: io package is not available.') - end - more off; -end - -r = run_unitary_tests_in_directory(mdbnomics_src_root(1:end-1)); - -delete('*.log'); - -if any(~[r{:,3}]) - system('touch failed'); -else - system('touch pass'); -end - -warning on -path(opath); - -display_report(r); +function runalltests() + +% Copyright (C) 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 +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare dseries submodule 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/>. +mdbnomics_src_root = []; % Will be updated by calling initialize_dseries_class(). + +opath = path(); + +system('rm -f failed'); +system('rm -f pass'); + +% Check that the m-unit-tests module is available. +try + initialize_unit_tests_toolbox; +catch + error('Missing dependency: m-unit-tests module is not available.') +end + +% Get path to the current script +unit_tests_root = strrep(which('runalltests'),'runalltests.m',''); + +% Initialize the mdbnomics module +try + initialize_mdbnomics(); +catch + addpath([unit_tests_root '../src']); + initialize_mdbnomics(); +end + +warning off + +if isoctave() + if ~user_has_octave_forge_package('io') + error('Missing dependency: io package is not available.') + end + more off; +end + +r = run_unitary_tests_in_directory(mdbnomics_src_root(1:end-1)); + +delete('*.log'); + +if any(~[r{:,3}]) + system('touch failed'); +else + system('touch pass'); +end + +warning on +path(opath); + +display_report(r);