Home > data-utility > granger_causality > gcause_libs > visualize_directed_graph.m

visualize_directed_graph

PURPOSE ^

weight_matrix = results_gcause.gcause_mat;

SYNOPSIS ^

function visualize_directed_graph(weight_matrix, weight_sig, node_texts, save_name)

DESCRIPTION ^

 weight_matrix = results_gcause.gcause_mat;
 node_texts = var_module_list;

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function visualize_directed_graph(weight_matrix, weight_sig, node_texts, save_name)
0002 
0003 % weight_matrix = results_gcause.gcause_mat;
0004 % node_texts = var_module_list;
0005 [num_nodes, m] = size(weight_matrix);
0006 if num_nodes ~= m
0007     error('Weight matrix should have the same number of rows and columns');
0008 end
0009 
0010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0011 len_unit = 10;
0012 len_side = 6;
0013 len_gap = (len_unit - len_side)/2;
0014 height = len_unit * ceil(num_nodes/2);
0015 width = len_unit * 2 + len_gap;
0016 
0017 % if user wants to use this function and save it as an individual figure
0018 if nargin > 3
0019     h = figure('Position', [50 50 50*width 50*height],'Visible', 'off');
0020 end
0021 hold on;
0022 xlim([0 width]);
0023 ylim([0 height]);
0024 num_rows = ceil(num_nodes / 2);
0025 for nidx = 1:num_nodes
0026     rowi = ceil(nidx/2.1);
0027     columni = mod(nidx+1, 2);
0028     x = columni*len_unit + len_gap*(columni+1);
0029     y = (num_rows-rowi)*len_unit + len_gap;
0030     rectangle('Position', [x y len_side len_side], 'Curvature',1, 'LineWidth', 3);
0031     center_x = x+len_side/2;
0032     center_y = y+len_side/2;
0033     text(center_x, center_y, node_texts{nidx}, 'HorizontalAlignment', 'center', 'FontSize', 15);
0034 
0035     if nidx == 2
0036         % to this node
0037         if num_nodes > 2
0038             arrow_start = [x-len_side-0.5 center_y+1.8];
0039             arrow_end = [x+0.5 center_y+1.8];
0040             text_loc = [x-len_side/2 center_y+1.8];
0041             draw_weighted_arrow(arrow_start, arrow_end, ...
0042                 weight_matrix(nidx, nidx-1), text_loc, weight_sig(nidx, nidx-1));
0043 
0044             % from this node
0045             arrow_start = [x-0.2 center_y];
0046             arrow_end = [x-len_side+0.2 center_y];
0047             text_loc = [x-len_side/2 center_y];
0048             draw_weighted_arrow(arrow_start, arrow_end, ...
0049                 weight_matrix(nidx-1, nidx), text_loc, weight_sig(nidx-1, nidx));
0050         else
0051             arrow_start = [x-len_side center_y+1];
0052             arrow_end = [x center_y+1];
0053             text_loc = [x-len_side/2 center_y+1];
0054             draw_weighted_arrow(arrow_start, arrow_end, ...
0055                 weight_matrix(nidx, nidx-1), text_loc, weight_sig(nidx, nidx-1));
0056 
0057             % from this node
0058             arrow_start = [x center_y-1];
0059             arrow_end = [x-len_side center_y-1];
0060             text_loc = [x-len_side/2 center_y-1];
0061             draw_weighted_arrow(arrow_start, arrow_end, ...
0062                 weight_matrix(nidx-1, nidx), text_loc, weight_sig(nidx-1, nidx));
0063         end
0064     elseif nidx == 3
0065         % to this node
0066         arrow_start = [center_x-1 y+len_side+2*len_gap];
0067         arrow_end = [center_x-1 y+len_side];
0068         text_loc = [center_x-1.8 y+len_side+len_gap];
0069         draw_weighted_arrow(arrow_start, arrow_end, ...
0070             weight_matrix(nidx, nidx-2), text_loc, weight_sig(nidx, nidx-2));
0071         
0072         % from this node
0073         arrow_start = [center_x+1 y+len_side];
0074         arrow_end = [center_x+1 y+len_side+2*len_gap];
0075         text_loc = [center_x+0.2 y+len_side+len_gap];
0076         draw_weighted_arrow(arrow_start, arrow_end, ...
0077             weight_matrix(nidx-2, nidx), text_loc, weight_sig(nidx-2, nidx));
0078         
0079         % to this node
0080         arrow_start = [center_x+len_unit-0.5 center_y+len_unit-1.5];
0081         arrow_end = [center_x+len_gap-0.5 center_y+len_gap+0.5];
0082         text_loc = [arrow_start(1,1)-2 arrow_start(1,2)-1];
0083         draw_weighted_arrow(arrow_start, arrow_end, ...
0084             weight_matrix(nidx, nidx-1), text_loc, weight_sig(nidx, nidx-1));
0085         
0086         % from this node
0087         arrow_start = [center_x+len_gap+0.5 center_y+len_gap-0.5];
0088         arrow_end = [center_x+len_unit+0.5 center_y+len_unit-2.5];
0089         text_loc = [arrow_start(1,1)+1 arrow_start(1,2)+1];
0090         draw_weighted_arrow(arrow_start, arrow_end, ...
0091             weight_matrix(nidx-1, nidx), text_loc, weight_sig(nidx-1, nidx));
0092     elseif nidx == 4
0093         % to this node
0094         arrow_start = [center_x-len_unit+0.5 center_y+len_unit-1.5];
0095         arrow_end = [center_x-len_gap center_y+len_gap+0.5];
0096         text_loc = [arrow_start(1,1)+0.5 arrow_start(1,2)-1.5];
0097         draw_weighted_arrow(arrow_start, arrow_end, ...
0098             weight_matrix(nidx, nidx-3), text_loc, weight_sig(nidx, nidx-3));
0099         
0100         % from this node
0101         arrow_start = [center_x-len_gap-1 center_y+len_gap-0.5];
0102         arrow_end = [center_x-len_unit-0.5 center_y+len_unit-2.5];
0103         text_loc = [arrow_start(1,1)-2 arrow_start(1,2)+0.5];
0104         draw_weighted_arrow(arrow_start, arrow_end, ...
0105             weight_matrix(nidx-3, nidx), text_loc, weight_sig(nidx-3, nidx));
0106         
0107         % to this node
0108         arrow_start = [center_x-1 y+len_side+2*len_gap];
0109         arrow_end = [center_x-1 y+len_side];
0110         text_loc = [center_x-0.2 y+len_side+len_gap];
0111         draw_weighted_arrow(arrow_start, arrow_end, ...
0112             weight_matrix(nidx, nidx-2), text_loc, weight_sig(nidx, nidx-2));
0113         
0114         % from this node
0115         arrow_start = [center_x+1 y+len_side];
0116         arrow_end = [center_x+1 y+len_side+2*len_gap];
0117         text_loc = [center_x+2 y+len_side+len_gap];
0118         draw_weighted_arrow(arrow_start, arrow_end, ...
0119             weight_matrix(nidx-2, nidx), text_loc, weight_sig(nidx-2, nidx));
0120         
0121         % to this node
0122         arrow_start = [x-len_side+0.2 center_y];
0123         arrow_end = [x-0.2 center_y];
0124         text_loc = [x-len_side/2 center_y-0.8];
0125         draw_weighted_arrow(arrow_start, arrow_end, ...
0126             weight_matrix(nidx, nidx-1), text_loc, weight_sig(nidx, nidx-1));
0127         
0128         % from this node
0129         arrow_start = [x+0.6 center_y-2];
0130         arrow_end = [x-len_side-0.6 center_y-2];
0131         text_loc = [x-len_side/2 center_y-2.8];
0132         draw_weighted_arrow(arrow_start, arrow_end, ...
0133             weight_matrix(nidx-1, nidx), text_loc, weight_sig(nidx-1, nidx));
0134     end
0135 end
0136 
0137 hold off;
0138 set(gca,'Visible','off');
0139 
0140 if nargin > 3
0141     set(h,'PaperPositionMode','auto');
0142     saveas(h, save_name);
0143     close(h);
0144 end

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005