about summary refs log tree commit diff
path: root/BNW_parameter_learning/drawFigure.m
diff options
context:
space:
mode:
authorziejd22018-04-25 16:43:19 -0500
committerziejd22018-04-25 16:43:19 -0500
commit74b673ba4a706085201a5610b938ff98f08f641d (patch)
treecb39006ea1a39499e00dbbb0e0097087a4567031 /BNW_parameter_learning/drawFigure.m
parenta781cb1ff2e7ae6de0f686bd02cd279261485b1e (diff)
downloadBNW-74b673ba4a706085201a5610b938ff98f08f641d.tar.gz
Bug fixes, code comments, and minor changes
Diffstat (limited to 'BNW_parameter_learning/drawFigure.m')
-rw-r--r--BNW_parameter_learning/drawFigure.m191
1 files changed, 11 insertions, 180 deletions
diff --git a/BNW_parameter_learning/drawFigure.m b/BNW_parameter_learning/drawFigure.m
index fa963a4b..599d8f3b 100644
--- a/BNW_parameter_learning/drawFigure.m
+++ b/BNW_parameter_learning/drawFigure.m
@@ -1,186 +1,17 @@
-function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata)

+function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means)

 %drawFigure writes the parameters and data that are needed to draw the

-%structure of a Bayesian network.

-

-

-if nargin < 8,

-    drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means);

-else

-    drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata);

-end;

-

-end

-

-

-

-function [] = drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata)

-%Function to use if there is no entered evidence. 

-%         

+%structure of a Bayesian network for BNW.

+% This is the function that is called to create the initial

+%   net_figure file for the network (before evidence or intervention).

 %

-%Before each printed line, I will have a line that starts with %%%

-% that describes what will be on that line

-

-%Create an empty evidence cell array.

-

-%val=cases;

-%for i = 1:nnodes

-% val(i,1)=val(i,2);

-

-%end

-

-A=cell2mat(cases');

-Amax=max(A);

-Amin=min(A);

-

-

-evidence = cell(1,nnodes);

-engine = jtree_inf_engine(bnet);

-

-evidence{selectvar}=selectdata;

-

-[engine,loglik]=enter_evidence(engine,evidence);

-

-%Open the file, and write the nodes to a file.

-fileID = fopen(filename,'w');

-

-%%%%Evidence node

-fprintf(fileID,'%i\n',selectvar);

-%%% The number of nodes

-fprintf(fileID,'%i\n',nnodes);

-%Get canvas size

-labels_temp = cellstr(labels);

-[x,y] = make_layout(bnet.dag);

-

-x = x - min(x);

-y = 1 - y;

-y = y - min(y);

-

-[x_dim,y_dim] = canvasSize(nnodes,x,y);

-

-%%% The dimensions of the canvas for the javascript code

-fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim)

-

-x = x*x_dim;

-y = y*y_dim;

-for i = 1:nnodes,

-%%% The name and X- and Y-positions of each node

-    fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i)));

-end

-

-%Get the number of parents and children for each node.

-num_par = zeros(1,nnodes);

-%For parents, sum down columns

-for i = 1:nnodes,

-    for j = 1:nnodes,

-        if bnet.dag(j,i) == 1,

-            num_par(i) = num_par(i) + 1;

-        end

-    end

-end

-num_child = zeros(1,nnodes);

-for i = 1:nnodes,

-    for j = 1:nnodes,

-        if bnet.dag(i,j) == 1,

-            num_child(i) = num_child(i) + 1;

-        end

-    end

-end

-

-

-for i = 1:nnodes,

-    %%% The name and type of each node (1=continuous, the number of states

-    %%% if it is discrete

-    fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i));

-    %%% The size of the node, I am going to keep them 

-    %%% 250(width) by 150(height) for now

-    %Could modify this to change the width based on the length of the node

-    %name

-    fprintf(fileID,'%i\t%i\n',250,150);

-    %%% The number of parents of the node, and the parents

-    if num_par(i) == 0;

-        %%% If no parents:

-        fprintf(fileID,'%i\n',num_par(i));

-    else

-        parents = zeros(1,num_par(i));

-        k = 1;

-        for j = 1:nnodes,

-           if bnet.dag(j,i) == 1,

-             parents(1,k) = j;

-             k = k + 1;

-           end

-        end

-        format = '%i\t';

-        for j = 1:num_par(i)-1,

-            format = strcat(format,'%i\t');

-        end

-        format = strcat(format,'%i\n');

-        %%%If there are parents:

-        fprintf(fileID,format,num_par(i),parents(1,:));

-    end

-    

-    

-    %%% The number of children of the node, and the children

-    if num_child(i) == 0;

-        %%% If no children:

-        fprintf(fileID,'%i\n',num_child(i));

-    else

-        children = zeros(1,num_child(i));

-        k = 1;

-        for j = 1:nnodes,

-           if bnet.dag(i,j) == 1,

-             children(1,k) = j;

-             k = k + 1;

-           end

-        end

-        format = '%i\t';

-        for j = 1:num_child(i)-1,

-            format = strcat(format,'%i\t');

-        end

-        format = strcat(format,'%i\n');

-        %%%If there are parents:

-        fprintf(fileID,format,num_child(i),children(1,:));

-    end

-    

-    predict = marginal_nodes(engine,i);

-    if isempty(evidence{i})

-      if bnet.node_sizes(i) ~= 1,

-        for j = 1:bnet.node_sizes(i),

-            %%%For discrete nodes, the state and the percent of that state

-            fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j));

-        end;

-      else

-

-        [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i));

-        %%%For continuous nodes, print x and the pdf of a normal curve.

-        for j = 1:101,

-            %%Undo standardization

-            xvals(j,1) = xvals(j,1)*stdevs{i}+means{i}

-            fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1));

-        end;

-      end;

-    else

-      fprintf(fileID,'%6.4f\t%6.4f\n',selectdata,1);

-    end

-    

-end

-%fprintf(fileID,'%s\t %\n',labels_temp{:});

-

-

-fclose(fileID);

-

-end

-

-

-

-

-

-

-function [] = drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means)

-%Function to use if there is no entered evidence. 

-%         

 %

-%Before each printed line, I will have a line that starts with %%%

-% that describes what will be on that line

+% The output is the file specified by 'filename'.

+%  For BNW, this file is called: ???net_figure.txt

+%    where ??? is the prefix.

+% 

+% drawFigure is called by runBN_intial.m

+%

+

 A=cell2mat(cases');

 Amax=max(A);

 Amin=min(A);