annotate toolboxes/FullBNT-1.0.7/GraphViz/draw_hmm.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function draw_hmm(A, varargin)
wolffd@0 2 % DRAW_HMM Make a picture of the HMM using dotty
wolffd@0 3 % function draw_hmm(A, ...)
wolffd@0 4 %
wolffd@0 5 % For details on dotty, see http://www.research.att.com/sw/tools/graphviz
wolffd@0 6 %
wolffd@0 7 % If A(i,j) > thresh, we draw and arc from state i to state j.
wolffd@0 8 %
wolffd@0 9 % Optional arguments (name/value pairs) [default]
wolffd@0 10 %
wolffd@0 11 % thresh - [1e-1]
wolffd@0 12 % obsprob - If B(i,o) > 0, we include "o" in the name of state i.
wolffd@0 13 % e.g., if state 5 emits 1,3,7, its label becomes "5: 1 3 7".
wolffd@0 14 % startprob - ifstartprob(i) > 0, the state name will be prefixed with "+".
wolffd@0 15 % endprob - if endprob(i) > 0, the state name will be appended with "-".
wolffd@0 16 % filename - if [], we write to 'tmp.dot', convert this to 'tmp.ps'
wolffd@0 17 % using 'dot -Tps tmp.dot -o tmp.ps', and then call ghostview to display the result.
wolffd@0 18 % dot and gv must be on your system path.
wolffd@0 19 % If filename ~= [], we just generate the dot file, and do not
wolffd@0 20 % convert it to postscript or call ghostview.
wolffd@0 21
wolffd@0 22 [thresh, B, startprob, endprob, filename] = ...
wolffd@0 23 process_options(varargin, 'thresh', 1e-1, 'obsprob', [], 'startprob', [], 'endprob', [], ...
wolffd@0 24 'filename', []);
wolffd@0 25
wolffd@0 26 Q = length(A);
wolffd@0 27
wolffd@0 28 arclabel = cell(Q,Q);
wolffd@0 29 G = zeros(Q,Q);
wolffd@0 30 for i=1:Q
wolffd@0 31 for j=1:Q
wolffd@0 32 if A(i,j) < thresh
wolffd@0 33 arclabel{i,j} = '';
wolffd@0 34 else
wolffd@0 35 G(i,j) = 1;
wolffd@0 36 arclabel{i,j} = sprintf('%5.3f', A(i,j));
wolffd@0 37 end
wolffd@0 38 end
wolffd@0 39 end
wolffd@0 40
wolffd@0 41
wolffd@0 42 nodelabel = cell(1,Q);
wolffd@0 43 for i=1:Q
wolffd@0 44 % annotate start/stop states
wolffd@0 45 if ~isempty(startprob) & ~approxeq(startprob(i), 0)
wolffd@0 46 start = '+';
wolffd@0 47 else
wolffd@0 48 start = '';
wolffd@0 49 end
wolffd@0 50 if ~isempty(endprob) & ~approxeq(hmm.endprob(i), 0)
wolffd@0 51 stop = '-';
wolffd@0 52 else
wolffd@0 53 stop = '';
wolffd@0 54 end
wolffd@0 55 label = sprintf('%s%d%s :', start, i, stop);
wolffd@0 56
wolffd@0 57 if ~isempty(B)
wolffd@0 58 output_label = mk_output_label(B);
wolffd@0 59 label = strcat(label, output_label);
wolffd@0 60 end
wolffd@0 61
wolffd@0 62 nodelabel{i} = label;
wolffd@0 63 end
wolffd@0 64
wolffd@0 65
wolffd@0 66 if isempty(filename)
wolffd@0 67 filename = 'tmp.dot';
wolffd@0 68 %mkdot(G, filename, arclabel, nodelabel)
wolffd@0 69 graph_to_dot(G, 'filename', filename, 'arc_label', arclabel, 'node_label', nodelabel);
wolffd@0 70 fprintf('converting from .ps to .dot\n')
wolffd@0 71 !dot -Tps tmp.dot -o tmp.ps
wolffd@0 72 !gv tmp.ps &
wolffd@0 73 else
wolffd@0 74 graph_to_dot(G, 'filename', filename, 'arc_label', arclabel, 'node_label', nodelabel);
wolffd@0 75 %mkdot(G, filename, arclabel, nodelabel)
wolffd@0 76 end
wolffd@0 77
wolffd@0 78
wolffd@0 79 %%%%%%%%%
wolffd@0 80
wolffd@0 81 function label = mk_output_label(B)
wolffd@0 82
wolffd@0 83 [Q O] = size(B);
wolffd@0 84 label = '';
wolffd@0 85
wolffd@0 86 if 0
wolffd@0 87 % print most probable symbols
wolffd@0 88 for i=1:Q
wolffd@0 89 m = max(B(i,:));
wolffd@0 90 ndx = find(abs(B(i,:) - repmat(m,1,O)) < 1e-2);
wolffd@0 91 %ndx = find(B(i,:)==m);
wolffd@0 92 %label = sprintf('%d,', ndx);
wolffd@0 93 end
wolffd@0 94 end
wolffd@0 95
wolffd@0 96 if 0
wolffd@0 97 % print prob distrib over all symbols
wolffd@0 98 for o=1:O
wolffd@0 99 if approxeq(B(i,o), 0)
wolffd@0 100 %
wolffd@0 101 else
wolffd@0 102 label = strcat(label, sprintf('%d(%3.2f),', o, B(i,o)));
wolffd@0 103 end
wolffd@0 104 end
wolffd@0 105 end
wolffd@0 106
wolffd@0 107 if 1
wolffd@0 108 % print all non-zero symbols
wolffd@0 109 chars = ['a' 'b' 'c'];
wolffd@0 110 for o=1:O
wolffd@0 111 if approxeq(B(i,o), 0)
wolffd@0 112 %
wolffd@0 113 else
wolffd@0 114 label = strcat(label, sprintf('%s', chars(o)));
wolffd@0 115 end
wolffd@0 116 end
wolffd@0 117 end