From 565c96b85594b2e4fe71b6d8522a6664d780f0f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3ra=20Kocsis?= <dora@dynare.org> Date: Tue, 14 Apr 2020 17:25:01 +0200 Subject: [PATCH] cosmetics and error handling --- README.md | 39 +++++++++++--------------------- src/fetch_series.m | 2 +- src/fetch_series_by_api_link.m | 3 ++- src/initialize_mdbnomics.m | 8 +++---- src/utils/fetch_series_page.m | 17 +++++++++++--- src/utils/iter_filtered_series.m | 8 +++++-- src/utils/normalize_value.m | 5 +++- 7 files changed, 43 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 215fddb..0802496 100644 --- a/README.md +++ b/README.md @@ -42,35 +42,22 @@ Example: >> df_id = fetch_series('series_ids', 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN'); -The returned data is stored in the `df_id` variable. Its type is a cell array. To display the first 3 rows of the array +The returned data is stored in the `df_id` variable. Its type is a cell array. To display the first 3 rows of the array as a table (including the column headers), type: - >> df_id(1:4,:) - - 6�16 cell array - - Columns 1 through 5 - - {'x_frequency'} {'provider_code'} {'dataset_code'} {'dataset_name' } {'series_code' } - {'annual' } {'AMECO' } {'ZUTN' } {'Unemployment ra�'} {'EA19.1.0.0.0.ZUTN'} - {'annual' } {'AMECO' } {'ZUTN' } {'Unemployment ra�'} {'EA19.1.0.0.0.ZUTN'} - {'annual' } {'AMECO' } {'ZUTN' } {'Unemployment ra�'} {'EA19.1.0.0.0.ZUTN'} - - Columns 6 through 11 - - {'series_name' } {'original_period'} {'period' } {'original_value'} {'value'} {'freq'} - {'Annually � (Per�'} {'1960' } {'1960-01-01'} {'NA' } {[ NaN]} {'a' } - {'Annually � (Per�'} {'1961' } {'1961-01-01'} {'NA' } {[ NaN]} {'a' } - {'Annually � (Per�'} {'1962' } {'1962-01-01'} {'NA' } {[ NaN]} {'a' } - - Columns 12 through 16 - - {'unit' } {'geo' } {'Frequency'} {'Unit' } {'Country' } - {'percentage-of-a�'} {'ea19'} {'Annually' } {'(Percentage of �'} {'Euro area'} - {'percentage-of-a�'} {'ea19'} {'Annually' } {'(Percentage of �'} {'Euro area'} - {'percentage-of-a�'} {'ea19'} {'Annually' } {'(Percentage of �'} {'Euro area'} - + >> tab_id = cell2table(df_id(2:end,:), 'VariableNames', df_id(1,:)); + >> tab_id(1:3,:) + + 3×16 table + x_frequency provider_code dataset_code dataset_name series_code series_name original_period period original_value value freq unit geo Frequency Unit Country + ___________ _____________ ____________ __________________________________________________________________ _____________________ ____________________________________________________________ _______________ ______________ ______________ _____ _____ ___________________________________ ________ ____________ _____________________________________ _____________ + + {'annual'} {'AMECO'} {'ZUTN'} {'Unemployment rate: total :- Member States: definition EUROSTAT'} {'EA19.1.0.0.0.ZUTN'} {'Annually – (Percentage of active population) – Euro area'} {'1960'} {'1960-01-01'} {'NA'} NaN {'a'} {'percentage-of-active-population'} {'ea19'} {'Annually'} {'(Percentage of active population)'} {'Euro area'} + {'annual'} {'AMECO'} {'ZUTN'} {'Unemployment rate: total :- Member States: definition EUROSTAT'} {'EA19.1.0.0.0.ZUTN'} {'Annually – (Percentage of active population) – Euro area'} {'1961'} {'1961-01-01'} {'NA'} NaN {'a'} {'percentage-of-active-population'} {'ea19'} {'Annually'} {'(Percentage of active population)'} {'Euro area'} + {'annual'} {'AMECO'} {'ZUTN'} {'Unemployment rate: total :- Member States: definition EUROSTAT'} {'EA19.1.0.0.0.ZUTN'} {'Annually – (Percentage of active population) – Euro area'} {'1962'} {'1962-01-01'} {'NA'} NaN {'a'} {'percentage-of-active-population'} {'ea19'} {'Annually'} {'(Percentage of active population)'} {'Euro area'} + >> + In such cell array, you will always find at least those columns: * `x_frequency`: (harmonized frequency generated by DBnomics) diff --git a/src/fetch_series.m b/src/fetch_series.m index b437dcf..5ef4655 100644 --- a/src/fetch_series.m +++ b/src/fetch_series.m @@ -95,7 +95,7 @@ if ~isa(p.Results.dimensions, 'function_handle') if isempty(p.Results.provider_code) || isempty(p.Results.dataset_code) error('When you use dimensions, you must specifiy provider_code and dataset_code.'); end - api_link = sprintf('%s/%s/%s?observations=1&dimensions=%s', series_base_url, p.Results.provider_code, p.Results.dataset_code, p.Results.dimensions); %jsonencode( + api_link = sprintf('%s/%s/%s?observations=1&dimensions=%s', series_base_url, p.Results.provider_code, p.Results.dataset_code, p.Results.dimensions); end if ~isempty(p.Results.series_code) diff --git a/src/fetch_series_by_api_link.m b/src/fetch_series_by_api_link.m index 1092c45..adc6fc7 100644 --- a/src/fetch_series_by_api_link.m +++ b/src/fetch_series_by_api_link.m @@ -47,6 +47,7 @@ p.addOptional('max_nb_series', NaN, @isnumeric); p.addOptional('editor_api_base_url', default_editor_base_url, validStringInput); p.KeepUnmatched = false; p.parse(api_link, varargin{:}); + [datasets_dimensions, series_dims_by_dataset_code, series_list]= iter_series_info(api_link, p.Results.max_nb_series); if isempty(series_list) @@ -166,7 +167,7 @@ else end end series_length=series_length+length(flat_series.value); - end + end end end diff --git a/src/initialize_mdbnomics.m b/src/initialize_mdbnomics.m index 3cf356e..7db683f 100644 --- a/src/initialize_mdbnomics.m +++ b/src/initialize_mdbnomics.m @@ -38,7 +38,7 @@ if ~exist('matlab_ver_less_than','file') end if ~exist('octave_ver_less_than','file') - p{end+1} = 'missing/matlab_ver_less_than'; + p{end+1} = 'missing/octave_ver_less_than'; end if exist('OCTAVE_VERSION', 'builtin') && ~exist('user_has_octave_forge_package','file') @@ -50,10 +50,8 @@ P = cellfun(@(c)[mdbnomics_src_root c], p, 'uni', false); addpath(P{:}); % Check minimal MATLAB and Octave requirements. -if isoctave && octave_ver_less_than('6') - error('The minimum Octave requirement of this package is version 6.'); -elseif matlab_ver_less_than('8.5') - error('The minimum MATLAB requirement of this package is R2015a.'); +if (isoctave && octave_ver_less_than('6')) || matlab_ver_less_than('8.5') + error('The minimum MATLAB requirement of this package is R2015a, and the minimum Octave requirement of this package is version 6.'); end assignin('caller', 'mdbnomics_src_root', mdbnomics_src_root); diff --git a/src/utils/fetch_series_page.m b/src/utils/fetch_series_page.m index cbe2061..8ecfc35 100644 --- a/src/utils/fetch_series_page.m +++ b/src/utils/fetch_series_page.m @@ -44,8 +44,19 @@ catch ME error(error_message); end -series_page = response_json.series; -if ~isempty(series_page) - assert(series_page.offset == offset); +if isempty(response_json.errors) + series_page = response_json.series; + if ~isempty(series_page) + assert(series_page.offset == offset); + end +else + error_ = response_json.errors; + if ~isempty(error_.dataset_code) && isempty(error_.series_code) + error('%s for: %s. Please revise your input for dataset code.', error_.message, error_.dataset_code); + elseif ~isempty(error_.provider_code) && isempty(error_.series_code) + error('%s for: %s. Please revise your input for series provider.', error_.message, error_.provider_code); + elseif ~isempty(error_.series_code) + error('%s for: %s. Please revise your input for series code.', error_.message, error_.series_code); + end end end diff --git a/src/utils/iter_filtered_series.m b/src/utils/iter_filtered_series.m index a837748..a7fb491 100644 --- a/src/utils/iter_filtered_series.m +++ b/src/utils/iter_filtered_series.m @@ -57,8 +57,12 @@ for gg = 1:size(grouped_series, 2) try response = webwrite(apply_endpoint_url, json_request, opts); - catch - error("Invalid response from Time Series Editor (JSON expected)"); + 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)); diff --git a/src/utils/normalize_value.m b/src/utils/normalize_value.m index b2d8545..fb2b896 100644 --- a/src/utils/normalize_value.m +++ b/src/utils/normalize_value.m @@ -30,7 +30,10 @@ function series = normalize_value(series) % along with Dynare. If not, see <http://www.gnu.org/licenses/>. if iscell(series.value) - series.original_value = series.value; +% series.original_value = cellfun(@char, series.value, 'UniformOutput', false); + series.original_value = cellfun(@num2str,series.value,'un',0); + +% series.original_value = char(series.value); series.value(strcmp(series.value, 'NA')) = {NaN}; else series.original_value = num2cell(series.value); -- GitLab