report_table.m 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
classdef report_table < handle
    % report_table Class
    %
    % Copyright (C) 2013-2019 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/>.
    properties (Access = private, Constant = true)
        titleFormatDefalut = {'\large'}
    end
    properties (Access = private)
        series = {}
25
        table_data = {}               % The table data
26 27 28 29 30 31 32 33 34 35 36 37 38
        % Not documented
        preamble = {''}
        afterward = {''}
    end
    properties (SetAccess = private)
        tableDirName = 'tmpRepDir'    % The name of the folder in which to store this table. Default: tmpRepDir.
        tableName = ''                % The name to use when saving this table. Default: something of the form table_pg1_sec2_row1_col3.tex.
        title = {''}                  % Table Title. Default: none.
        titleFormat = ''              % A string representing the valid LATEX markup to use on title. The number of cell array entries must be equal to that of the title option if you do not want to use the default value for the title (and subtitles). Default: \large\bfseries.
        showHlines = false            % Whether or not to show horizontal lines separating the rows. Default: false.
        showVlines = false            %
        vlineAfter = ''               % Show a vertical line after the specified date (or dates if a cell array of dates is passed). Default: empty.
        vlineAfterEndOfPeriod = false % Show a vertical line after the end of every period (i.e. after every year, after the fourth quarter, etc.). Default: false.
Houtan Bastani's avatar
Houtan Bastani committed
39
        data = ''                     % The dseries that provides the data for the table. Default: none.
40 41 42 43 44 45
        seriesToUse = ''              % The names of the series contained in the dseries provided to the data option. If empty, use all series provided to data option. Default: empty.
        range = {}                    % The date range of the data to be displayed. Default: all.
        precision = 1                 % The number of decimal places to report in the table data (rounding done via the round half away from zero method). Default: 1.
        writeCSV = false              % Whether or not to write a CSV file containing the data displayed in the table. The file will be saved in the directory specified by tableDirName with the same base name as specified by tableName with the ending .csv. Default: false.
        highlightRows = {''}          % A cell array containing the colors to use for row highlighting. See shadeColor for how to use colors with reports. Highlighting for a specific row can be overridden by using the tableRowColor option to addSeries. Default: empty.
    end
46
    methods
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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
        function o = report_table(varargin)
            %function o = report_table(varargin)
            % Report_Table Class Constructor
            %
            % INPUTS
            %   varargin        0 args  : empty report_table object
            %                   1 arg   : must be report_table object (return a copy of arg)
            %                   > 1 args: option/value pairs (see structure below for options)
            %
            % OUTPUTS
            %   o     [report_table]  report_table object
            %
            % SPECIAL REQUIREMENTS
            %   none
            o.titleFormat = o.titleFormatDefalut;
            if nargin == 0
                return
            elseif nargin == 1
                assert(isa(varargin{1}, 'report_table'), ...
                    'With one arg to the Report_Table constructor, you must pass a report_table object');
                o = varargin{1};
                return
            end

            if round(nargin/2) ~= nargin/2
                error('Options to Report_Table constructor must be supplied in name/value pairs.');
            end

            % Octave 5.1.0 has not implemented `properties` and issues a warning when using `fieldnames`
            warning('off')
            optNames = fieldnames(o);
            warning('on')

            % overwrite default values
            for pair = reshape(varargin, 2, [])
                ind = find(strcmpi(optNames, pair{1}));
                assert(isempty(ind) || length(ind) == 1);
                if ~isempty(ind)
                    o.(optNames{ind}) = pair{2};
                else
                    error('%s is not a recognized option to the Report_Table constructor.', pair{1});
                end
            end

            if ~iscell(o.range)
                o.range = {o.range};
            end

            if isdates(o.vlineAfter)
                o.vlineAfter = {o.vlineAfter};
            end

            % Check options provided by user
            if ischar(o.title)
                o.title = {o.title};
            end

            if ischar(o.titleFormat)
                o.titleFormat = {o.titleFormat};
            end
            if length(o.title) ~= length(o.titleFormat)
                o.titleFormat = repmat(o.titleFormatDefalut, 1, length(o.title));
            end
            assert(islogical(o.showHlines), '@report_table.report_table: showHlines must be true or false');
            assert(islogical(o.showVlines), '@report_table.report_table: showVlines must be true or false');
            assert(isint(o.precision) && o.precision >= 0, '@report_table.report_table: precision must be a non-negative integer');
            assert(isempty(o.range) || length(o.range) <=2 && allCellsAreDatesRange(o.range), ...
                ['@report_table.report_table: range is specified as a dates range, e.g. ' ...
                '''dates(''1999q1''):dates(''1999q3'')''.']);
            assert(isempty(o.data) || isdseries(o.data), ...
                '@report_table.report_table: data must be a dseries');
            assert(isempty(o.seriesToUse) || iscellstr(o.seriesToUse), ...
                '@report_table.report_table: seriesToUse must be a cell array of string(s)');
            assert(isempty(o.vlineAfter) || allCellsAreDates(o.vlineAfter), ...
                '@report_table.report_table: vlineAfter must be a dates');
            if o.showVlines
                o.vlineAfter = '';
            end
            assert(islogical(o.vlineAfterEndOfPeriod), ...
                '@report_table.report_table: vlineAfterEndOfPeriod must be true or false');
            assert(iscellstr(o.title), ...
                '@report_table.report_table: title must be a cell array of string(s)');
            assert(iscellstr(o.titleFormat), ...
                '@report_table.report_table: titleFormat must be a cell array of string(s)');
            assert(ischar(o.tableName), '@report_table.report_table: tableName must be a string');
            assert(ischar(o.tableDirName), '@report_table.report_table: tableDirName must be a string');
            assert(islogical(o.writeCSV), '@report_table.report_table: writeCSV must be either true or false');
            assert(iscellstr(o.highlightRows), '@report_table.report_table: highlightRowsmust be a cell string');

            % using o.seriesToUse, create series objects and put them in o.series
            if ~isempty(o.data)
                if isempty(o.seriesToUse)
                    for i=1:o.data.vobs
                        o.series{end+1} = report_series('data', o.data{o.data.name{i}});
                    end
                else
                    for i=1:length(o.seriesToUse)
                        o.series{end+1} = report_series('data', o.data{o.seriesToUse{i}});
                    end
                end
            end
            o.data = '';
            o.seriesToUse = '';
        end
    end
152
    methods (Hidden = true)
153 154
        o = addData(o, varargin)
        o = addSeries(o, varargin)
155
        write(o, fid, pg, sec, row, col, rep_dir)
156 157
    end
    methods (Access = private)
158
        o = writeTableFile(o, pg, sec, row, col, rep_dir)
159 160 161 162 163 164
    end
end

function tf = allCellsAreDatesRange(dcell)
%function tf = allCellsAreDatesRange(dcell)
% Determines if all the elements of dcell are a range of dates
Houtan Bastani's avatar
Houtan Bastani committed
165 166
%
% INPUTS
167
%   dcell     cell of dates
Houtan Bastani's avatar
Houtan Bastani committed
168 169
%
% OUTPUTS
170
%   tf        true if every entry of dcell is a range of dates
Houtan Bastani's avatar
Houtan Bastani committed
171 172 173 174
%
% SPECIAL REQUIREMENTS
%   none

175
% Copyright (C) 2014-2015 Dynare Team
Houtan Bastani's avatar
Houtan Bastani committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
%
% 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/>.

192 193 194 195 196
assert(iscell(dcell));
tf = true;
for i=1:length(dcell)
    if ~(isdates(dcell{i}) && dcell{i}.ndat >= 2)
        tf = false;
Houtan Bastani's avatar
Houtan Bastani committed
197
        return
Houtan Bastani's avatar
Houtan Bastani committed
198
    end
199
end
200 201
end

202 203 204 205 206 207 208 209 210 211 212 213
function tf = allCellsAreDates(dcell)
%function tf = allCellsAreDates(dcell)
% Determines if all the elements of dcell are dates objects
%
% INPUTS
%   dcell     cell of dates objects
%
% OUTPUTS
%   tf        true if every entry of dcell is a dates object
%
% SPECIAL REQUIREMENTS
%   none
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
% Copyright (C) 2014-2015 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/>.
231

232 233 234 235 236
assert(iscell(dcell));
tf = true;
for i=1:length(dcell)
    if ~isdates(dcell{i})
        tf = false;
Houtan Bastani's avatar
Houtan Bastani committed
237
        return
238 239
    end
end
240
end