LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
ImageFromPH.m
1% ImageFromPH(alpha, A, outFileName, prec)
2%
3% Depicts the given phase-type distribution,
4% and either displays it or saves it to file.
5%
6% Parameters
7% ----------
8% alpha : matrix, shape (1,M)
9% The initial probability vector of the phase-type
10% distribution.
11% A : matrix, shape (M,M)
12% The transient generator matrix of the phase-type
13% distribution.
14% outFileName : string, optional
15% If it is not provided, or equals to 'display', the
16% image is displayed on the screen, otherwise it is
17% written to the file. The file format is deduced
18% from the file name.
19% prec : double, optional
20% Transition rates less then prec are considered to
21% be zero and are left out from the image. The
22% default value is 1e-13.
23%
24% Notes
25% -----
26% The 'graphviz' software must be installed and available
27% in the path to use this feature.
28
29function ImageFromPH(alpha,A,outFileName,prec)
30
31 if ~exist('prec','var')
32 prec = 1e-13;
33 end
34
35 global BuToolsCheckInput;
36 if isempty(BuToolsCheckInput)
37 BuToolsCheckInput = true;
38 end
39
40 if BuToolsCheckInput && ~CheckPHRepresentation(alpha,A)
41 error('ImageFromPH: input isn''t a valid PH representation!');
42 end
43
44 if ~exist('outFileName','var') || strcmp(outFileName,'display')
45 outputFile = '.result.png';
46 displ = true;
47 else
48 outputFile = outFileName;
49 displ = false;
50 end
51
52 inputFile = '.temp.dot';
53
54 fid = fopen(inputFile,'w');
55 fprintf(fid, 'digraph G {\n');
56 fprintf(fid, '\trankdir=LR;\n');
57 fprintf(fid, '\tnode [shape=circle,width=0.3,height=0.3,label=""];\n');
58
59 % nodes
60 for i=1:length(alpha)
61 fprintf(fid, '\tn%d [xlabel=<<i>%g</i>>];\n', i, alpha(i));
62 end
63
64 % transitions to a non-absorbing state
65 for i=1:length(alpha)
66 for j=1:length(alpha)
67 if i~=j && abs(A(i,j))>prec
68 fprintf(fid, '\tn%d -> n%d [label="%g"];\n', i, j, A(i,j));
69 end
70 end
71 end
72
73 % transitions to the absorbing state
74 fprintf(fid, ['\tab [style=filled];\n']);
75 a = -sum(A,2);
76 for i=1:length(alpha)
77 if abs(a(i))>prec
78 fprintf(fid, '\tn%d -> ab [label="%g"];\n', i, a(i));
79 end
80 end
81 fprintf(fid,'}\n');
82 fclose(fid);
83
84 [~,~,ext] = fileparts(outputFile);
85 system(['dot -T', ext(2:end), ' ', inputFile, ' -o ', outputFile]);
86
87 delete (inputFile);
88
89 if displ
90 RGB = imread(outputFile);
91 figure('toolbar','none','units','pixel','position',[0,0,size(RGB,2)+100,size(RGB,1)+100]);
92 image(RGB);
93 axis image;
94 delete(outputFile);
95 end
96end
97