tutorial_gauss_figures.m

Back to Tutorial: Assessing coupling dynamics

tutorials/gauss/

% TUTORIAL_GAUSS_FIGURES
% Plots figures for the estimated partial transfer entropy between the
% three non-linearly coupled Gaussian processes
%
% tutorial_gauss_figures(results, compact_flag, web_flag)
%
% where
%
% RESULTS is a struct generated by function tutorial_gauss_analysis.
%
% With COMPACT_FLAG = 1 all forward flows will be plotted
% in one figure and all backward flows in another figure. Otherwise, a
% individual figure will be generated for each flow direction.
%
% WEB_FLAG = 1 indicates that the figures should be generated using image
% format .png. If WEB_FLAG = 0, the figures will be generated in .eps
% format. Note that to generate .png figures, the MATLAB function 
% export_fig is required. You can get export_fig from this URL:
%
% http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig 
%
%
% See also: tutorial_gauss_data, tutorial_gauss_analysis

% Description: Generate the tutorial figures
% Documentation: tutorial_gauss.txt
% Author: German Gomez-Herrero

function fig_handle = tutorial_gauss_figures(results, compact_flag, web_flag)

IN_FILE = 'tutorial_gauss_analysis';  % analysis results file

% include library export_fig
addpath(genpath('../common/export_fig/'));

if nargin < 3 || isempty(web_flag),
    web_flag = 0;
end

if nargin < 2 || isempty(compact_flag),
    compact_flag = false;
end

if nargin < 1 || isempty(results),
    s = load([IN_FILE '.mat']);
    results = s.results;
elseif ischar(data),
    s = load(data);
    results = s.results;
end

if web_flag && ~exist('export_fig','file'),
    error('export_fig is required for generating .PNG figures.');
end

EXPORT_FIG_MAG = 4;             % magnification factor for export_fig. 
                                % Decrease this value if you experience
                                % memory problems             
PRINT_DEVICE = '-depsc';
format.FONT_NAME = 'Arial';
format.BG_COLOR = 'white';
format.FORE_COLOR = 'black';
format.AXIS = [1 1500 -0.02 0.2];               % axis range
format.XTICK = [1 250 500 750 1000 1250 1500];  % x-axis labels
format.COLOR = {'black', 'red', 'blue'};
format.TRACE_THICKNESS = 1.5;
if web_flag,
    format.FONT_SIZE = 9;
    format.XSIZE = 10;                         % paper height in cm
    format.YSIZE = 3.5;                        % paper width in cm
else
    format.FONT_WEIGHT = 'normal';
    format.FONT_SIZE = 7;
    format.XSIZE = 6;
    format.YSIZE = 4.5;
end

% formatting options
format.format_figure = @(h) set(h, 'Color', format.BG_COLOR, ...
    'InvertHardcopy', 'off', ...
    'renderer', 'painters', ...
    'PaperUnits', 'centimeters', ...
    'PaperSize', [format.XSIZE format.YSIZE], ...
    'PaperPositionMode', 'manual', ...
    'PaperPosition', [0 0 format.XSIZE format.YSIZE], ...
    'Position', [0 0 format.XSIZE*50 format.YSIZE*50]);

format.axis = format.AXIS;

format.format_legend = @(h) set(h, 'Color', format.BG_COLOR, ...
    'FontName', format.FONT_NAME, ...
    'FontSize', format.FONT_SIZE, ...
    'FontWeight', format.FONT_WEIGHT, ...
    'textcolor', format.FORE_COLOR, ...
    'edgecolor', format.FORE_COLOR);

format.format_text = @(h) set(h, 'FontName', format.FONT_NAME, ...
    'FontSize', format.FONT_SIZE, ...
    'FontWeight', format.FONT_WEIGHT, ...
    'color', format.FORE_COLOR);

format.format_axes = @(h) set(h,'FontName', format.FONT_NAME, ...
    'FontSize', format.FONT_SIZE, ...
    'FontWeight', format.FONT_WEIGHT, ...
    'color', format.BG_COLOR, ...
    'xcolor', format.FORE_COLOR, ...
    'ycolor', format.FORE_COLOR, ...
    'XTick', format.XTICK);

format.format_area = @(h, color) set(h, 'FaceColor', color, ...
    'EdgeColor', color);

format.format_trace = {@(h) set(h, 'LineWidth', format.TRACE_THICKNESS, ...
    'Color', 'black'); ...
    @(h) set(h, 'LineWidth', format.TRACE_THICKNESS, ...
    'Color', 'red'); ...
    @(h) set(h, 'LineWidth', format.TRACE_THICKNESS, ...
    'Color', 'blue')};

format.color = format.COLOR;

if compact_flag,
    fig_handle = zeros(2, 1);
else
    fig_handle = zeros(6, 1);
end

if compact_flag,
    % generates only 2 figures
    fig_handle(1) = figure_multiple_flow({results.pte21, results.pte32, ...
        results.pte31}, {results.pte21sig, results.pte32sig, ...
        results.pte31sig}, 'PTE21, PTE32, PTE31', {'T_{y\leftarrow x|z}', ...
        'T^{en}_{z\leftarrow y|x}','T_{z\leftarrow x|y}'}, format);
    if web_flag,        
        export_fig('tutorial-gauss-fig1.png','-png',...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE,'tutorial-gauss-fig1');
    end

    fig_handle(2) = figure_multiple_flow({results.pte12, results.pte23, ...
        results.pte13}, {results.pte21sig, results.pte32sig, ...
        results.pte31sig}, 'PTE12, PTE23, PTE13', {'T_{x\leftarrow y|z}',...
        'T^{en}_{y\leftarrow z|x}','T_{x\leftarrow z|y}'}, format);
    if web_flag,
        export_fig('tutorial-gauss-fig2.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE,'tutorial-gauss-fig2');
    end

else
    % generates 6 figures (one per flow direction)
    fig_handle(1) = figure_single_flow(results.pte21, results.pte21sig, ...
        'PTE21', 'T^{en}_{y\leftarrow x|z}', results.alpha, format);
    if web_flag,
        export_fig('tutorial-gauss-fig21.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE, 'tutorial-gauss-fig21');
    end

    fig_handle(2) = figure_single_flow(results.pte12, results.pte12sig, ...
        'PTE12', 'T^{en}_{x\leftarrow y|z}', results.alpha, format);
    if web_flag,
        export_fig('tutorial-gauss-fig12.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE, 'tutorial-gauss-fig12');
    end

    fig_handle(3) = figure_single_flow(results.pte32, results.pte32sig, ...
        'PTE32', 'T^{en}_{z\leftarrow y|x}', results.alpha, format);
    if web_flag,        
        export_fig('tutorial-gauss-fig32.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE, 'tutorial-gauss-fig32');
    end

    fig_handle(4) = figure_single_flow(results.pte23, results.pte23sig, ...
        'PTE23', 'T^{en}_{y\leftarrow z|x}', results.alpha, format);
    if web_flag,
        export_fig('tutorial-gauss-fig23.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE, 'tutorial-gauss-fig23');
    end

    fig_handle(5) = figure_single_flow(results.pte31, results.pte31sig, ...
        'PTE31', 'T^{en}_{z\leftarrow x|y}', results.alpha, format);
    if web_flag,
        export_fig('tutorial-gauss-fig31.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE, 'tutorial-gauss-fig31');
    end

    fig_handle(6) = figure_single_flow(results.pte13, results.pte13sig, ...
        'PTE13', 'T^{en}_{x\leftarrow z|y}', results.alpha, format);
    if web_flag,
        export_fig('tutorial-gauss-fig13.png','-png', ...
            ['-m' num2str(EXPORT_FIG_MAG)]);
    else
        print(PRINT_DEVICE, 'tutorial-gauss-fig13');
    end

end

function h = figure_multiple_flow(data, data_sig, name, leg_str, format)
% plot multiple flow directions in one figure

h = figure;
format.format_figure(h);
set(h, 'Name', name);

sig_values = data_sig{1};
sig_values(isnan(sig_values)) =  0;
format.format_area(area(sig_values),[201 201 201]./256); % gray
hold on;
sig_values = data_sig{2};
sig_values(isnan(sig_values)) =  0;
format.format_area(area(sig_values), [251 205 201]./256); % soft red
sig_values = data_sig{3};
sig_values(isnan(sig_values)) =  0;
format.format_area(area(sig_values), [203 229 253]./256); % soft blue

format.format_trace{1}(plot(data{1}));
format.format_trace{2}(plot(data{2}));
format.format_trace{3}(plot(data{3}));

grid on;
axis(format.axis);
format.format_text(xlabel('Time (samples)'));
format.format_text(ylabel('PTE (nats)'));
format.format_legend(legend(leg_str));
format.format_axes(gca);

function h = figure_single_flow(data, data_sig, name, leg_str, alpha, format)
% plots a single flow direction

h = figure;
format.format_figure(h);
set(h, 'Name', name);

sig_values = data_sig;
sig_values(isnan(sig_values)) =  0;
format.format_area(area(sig_values), [190 190 190]./256); % gray
hold on;
format.format_trace{1}(plot(data));

grid on;
axis(format.axis);
format.format_text(xlabel('Time (samples)'));
format.format_text(ylabel('PTE (nats)'));
legend_handle = legend(['p = ' num2str(alpha)], leg_str);
format.format_legend(legend_handle);
legend_shrink(legend_handle);
format.format_axes(gca);