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