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

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