diff options
| author | ziejd2 | 2019-01-28 16:37:57 -0600 |
|---|---|---|
| committer | ziejd2 | 2019-01-28 16:37:57 -0600 |
| commit | 995f6673b5e725d6907ccd4f8e25033e8524f116 (patch) | |
| tree | 5df08d9503394eab1190a01a640f3c338e8c74d6 /sourcecodes/parameter_learning | |
| parent | bb9d93322abf825368dedaafc2376ef8fdfc1e2f (diff) | |
| download | BNW-995f6673b5e725d6907ccd4f8e25033e8524f116.tar.gz | |
Deleting old versions of files
Diffstat (limited to 'sourcecodes/parameter_learning')
27 files changed, 251 insertions, 2770 deletions
diff --git a/sourcecodes/parameter_learning/Predictmultipleintrvention.m b/sourcecodes/parameter_learning/Predictmultipleintrvention.m deleted file mode 100644 index 1b9fa2f4..00000000 --- a/sourcecodes/parameter_learning/Predictmultipleintrvention.m +++ /dev/null @@ -1,95 +0,0 @@ -function Predictmultipleintrvention(pre) -dfile=strcat(pre,'structure_input.txt'); -sfile=dfile; -dfile=strcat(pre,'continuous_input.txt'); -nnodefile=strcat(pre,'nnode.txt'); - -fnnode = fopen(nnodefile,'r'); -nnodes = fscanf(fnnode,'%d'); - -fvarnamefile=strcat(pre,'varname.txt'); - -varfile = fopen(fvarnamefile,'r'); - -Std_flag=true; -[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); - -[bnet]=parameterLearning(bnet,cases); - -fvarfile=strcat(pre,'var.txt'); -fvar = fopen(fvarfile,'r'); -select_var_new = fscanf(fvar,'%d'); - -nm = numel(select_var_new); - -varlabels = cell(1,nm); -varbuffer = fgetl(varfile); %get header line as a string -for j=1:nm - [varnext,varbuffer] = strtok(varbuffer); - varlabels{j} = varnext; - for i=1:nnodes - if strcmp(varlabels{j},labels{i}) - select_var_new(j)=i; - end - end - -end - - - - -fvardfile=strcat(pre,'vardata.txt'); - -fvard = fopen(fvardfile,'r'); - -select_var_data_new = fscanf(fvard,'%f'); - -means_orig = cell(1,nnodes); -stdevs_orig = cell(1,nnodes); -labels_orig = cell(1,nnodes); -%Read in original means and standard deviations -mapfile = strcat(pre,'map.txt'); -fmap = fopen(mapfile,'r'); -for i=1:nnodes - buffer = fgetl(mapfile); - temp = cell(1,4); - for j=1:4 - [next,buffer] = strtok(buffer); - temp{j} = next; - end - labels_orig{i} = temp{1}; - means_orig{i} = str2num(temp{4}); - stdevs_orig{i} = str2num(temp{3}); -end -fclose(fmap); - -%Need to map the means and stdevs to the correct labels -means = cell(1,nnodes); -stdevs = cell(1,nnodes); -%Read in labels in new order. -labelsnew = cell(1,nnodes); -mapdatafile = strcat(pre,'mapdata.txt'); -fmapdata = fopen(mapdatafile,'r'); -buffer = fgetl(fmapdata); -for i = 1:nnodes - [next,buffer ] = strtok(buffer); - labelsnew{i} = next; -end -fclose(fmapdata); -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labelsnew{i},labels_orig{j}) - means{i} = means_orig{j}; - stdevs{i} = stdevs_orig{j}; - break - end - end -end - -filename=strcat(pre,'net_figure_new.txt'); - -drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); - -writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); - -end diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultiple.m b/sourcecodes/parameter_learning/code_backup/Predictmultiple.m deleted file mode 100644 index 9d107628..00000000 --- a/sourcecodes/parameter_learning/code_backup/Predictmultiple.m +++ /dev/null @@ -1,72 +0,0 @@ -function Predictmultiple(pre) -dfile=strcat(pre,'structure_input.txt'); -sfile=dfile; -dfile=strcat(pre,'continuous_input.txt'); -nnodefile=strcat(pre,'nnode.txt'); - -fnnode = fopen(nnodefile,'r'); -nnodes = fscanf(fnnode,'%d'); - -Std_flag=true; -[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); - -[bnet]=parameterLearning(bnet,cases); - -fvarfile=strcat(pre,'var.txt'); -fvar = fopen(fvarfile,'r'); -select_var_new = fscanf(fvar,'%d'); - -fvardfile=strcat(pre,'vardata.txt'); -fvard = fopen(fvardfile,'r'); -select_var_data_new = fscanf(fvard,'%f'); - -means_orig = cell(1,nnodes); -stdevs_orig = cell(1,nnodes); -labels_orig = cell(1,nnodes); -%Read in original means and standard deviations -mapfile = strcat(pre,'map.txt'); -fmap = fopen(mapfile,'r'); -for i=1:nnodes - buffer = fgetl(mapfile); - temp = cell(1,3); - for j=1:3 - [next,buffer] = strtok(buffer); - temp{j} = next; - end - labels_orig{i} = temp{1}; - means_orig{i} = str2num(temp{3}); - stdevs_orig{i} = str2num(temp{2}); -end -fclose(fmap); - -%Need to map the means and stdevs to the correct labels -means = cell(1,nnodes); -stdevs = cell(1,nnodes); -%Read in labels in new order. -labelsnew = cell(1,nnodes); -mapdatafile = strcat(pre,'mapdata.txt'); -fmapdata = fopen(mapdatafile,'r'); -buffer = fgetl(fmapdata); -for i = 1:nnodes - [next,buffer ] = strtok(buffer); - labelsnew{i} = next; -end -fclose(fmapdata); -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labelsnew{i},labels_orig{j}) - means{i} = means_orig{j}; - stdevs{i} = stdevs_orig{j}; - break - end - end -end - - -filename=strcat(pre,'net_figure_new.txt'); - -drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); - -writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); - -end diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m b/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m deleted file mode 100644 index e9f741f2..00000000 --- a/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m +++ /dev/null @@ -1,95 +0,0 @@ -function Predictmultipleintrvention(pre) -dfile=strcat(pre,'structure_input.txt'); -sfile=dfile; -dfile=strcat(pre,'continuous_input.txt'); -nnodefile=strcat(pre,'nnode.txt'); - -fnnode = fopen(nnodefile,'r'); -nnodes = fscanf(fnnode,'%d'); - -fvarnamefile=strcat(pre,'varname.txt'); - -varfile = fopen(fvarnamefile,'r'); - -Std_flag=true; -[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); - -[bnet]=parameterLearning(bnet,cases); - -fvarfile=strcat(pre,'var.txt'); -fvar = fopen(fvarfile,'r'); -select_var_new = fscanf(fvar,'%d'); - -nm = numel(select_var_new); - -varlabels = cell(1,nm); -varbuffer = fgetl(varfile); %get header line as a string -for j=1:nm - [varnext,varbuffer] = strtok(varbuffer); - varlabels{j} = varnext; - for i=1:nnodes - if strcmp(varlabels{j},labels{i}) - select_var_new(j)=i; - end - end - -end - - - - -fvardfile=strcat(pre,'vardata.txt'); - -fvard = fopen(fvardfile,'r'); - -select_var_data_new = fscanf(fvard,'%f'); - -means_orig = cell(1,nnodes); -stdevs_orig = cell(1,nnodes); -labels_orig = cell(1,nnodes); -%Read in original means and standard deviations -mapfile = strcat(pre,'map.txt'); -fmap = fopen(mapfile,'r'); -for i=1:nnodes - buffer = fgetl(mapfile); - temp = cell(1,3); - for j=1:3 - [next,buffer] = strtok(buffer); - temp{j} = next; - end - labels_orig{i} = temp{1}; - means_orig{i} = str2num(temp{3}); - stdevs_orig{i} = str2num(temp{2}); -end -fclose(fmap); - -%Need to map the means and stdevs to the correct labels -means = cell(1,nnodes); -stdevs = cell(1,nnodes); -%Read in labels in new order. -labelsnew = cell(1,nnodes); -mapdatafile = strcat(pre,'mapdata.txt'); -fmapdata = fopen(mapdatafile,'r'); -buffer = fgetl(fmapdata); -for i = 1:nnodes - [next,buffer ] = strtok(buffer); - labelsnew{i} = next; -end -fclose(fmapdata); -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labelsnew{i},labels_orig{j}) - means{i} = means_orig{j}; - stdevs{i} = stdevs_orig{j}; - break - end - end -end - -filename=strcat(pre,'net_figure_new.txt'); - -drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); - -writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); - -end diff --git a/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m b/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m deleted file mode 100644 index c9d0692c..00000000 --- a/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m +++ /dev/null @@ -1,37 +0,0 @@ -function [ ] = checkDiscreteNodes( bnet, cases) - %checkDiscreteNodes Checks if states of discrete nodes are be integers from 1 to M - % where M is the number of states of the node. (M should be the same as - % node_sizes in the bnet). - % - %Input: - % bnet: BNT bnet - % cases: cell array of data - % -% -node_sizes = bnet.node_sizes; -dnodes = bnet.dnodes; -ndisc = size(dnodes,2); -ncases = size(cases,2); - -%check to see that all data for discrete nodes are integers -for i = 1:ndisc - inode = dnodes(i); - data = cases(inode,:); - isize = node_sizes(inode); - states = zeros(1,isize); - for j = 1:isize - states(j) = j; - end - for j = 1:ncases - k = int64(data{j}); - if ~any(k==states) - error(['Discrete nodes must be integers from 1 to the number of states']); - end - end -end - - -end - - - diff --git a/sourcecodes/parameter_learning/code_backup/checkStructure.m b/sourcecodes/parameter_learning/code_backup/checkStructure.m deleted file mode 100644 index b4de9403..00000000 --- a/sourcecodes/parameter_learning/code_backup/checkStructure.m +++ /dev/null @@ -1,78 +0,0 @@ -function [ labels, cases, dag, node_sizes, ord_flag ] = checkStructure(labels, cases, dag, node_sizes) - %checkStructure Check to see if nodes are sorted correctly. Nodes must be - % in topological order (i.e., parents before children) before parameter - % learning can take place. This function performs this sorting. - % - %Input and output have the same meaning. The output has just been - %topologically ordered. - % labels = cell array with the names of the nodes. - % cases = cell array with the data. - % dag = matrix with the strucutre of the network. - % node_sizes = vector with the size of each node. - -%make connections array -%count how big you need the connections array to be -nnodes = size(dag,1); -narcs = 0; -for i = 1:nnodes - for j = 1:nnodes - if dag(i,j) == 1 - narcs = narcs + 1; - end - end -end -%fill connections array with label names -connections = cell(narcs,2); -ncount = 0; -for i = 1:nnodes - for j = 1:nnodes - if dag(i,j) == 1 - ncount = ncount + 1; - connections{ncount,1} = labels{i}; - connections{ncount,2} = labels{j}; - end - end -end - -%get topologically sorted dag and labels -[new_dag, new_labels] = mk_adj_mat(connections, labels, 1); - -%check to see if order changed -ord_flag = 0; -for i = 1:nnodes - if ~strcmp(new_labels{i},labels{i}) - ord_flag = 1; - end -end - -if ord_flag - %get new ordering of nodes - order = cell(1,nnodes); - for i = 1:nnodes - for j = 1:nnodes - if strcmp(new_labels{j},labels{i}) - order{i} = j; - end - end - end - - %reorder cases and node_sizes - new_cases = cell(size(cases)); - for i = 1:nnodes - new_cases(order{i},:) = cases(i,:); - end - new_node_sizes = zeros(1,nnodes); - for i = 1:nnodes - new_node_sizes(order{i}) = node_sizes(i); - end - - - dag = new_dag; - cases = new_cases; - node_sizes = new_node_sizes; - labels = new_labels; -end - -end -%end checkStructure.m - diff --git a/sourcecodes/parameter_learning/code_backup/drawFigure.m b/sourcecodes/parameter_learning/code_backup/drawFigure.m deleted file mode 100644 index f84bffa3..00000000 --- a/sourcecodes/parameter_learning/code_backup/drawFigure.m +++ /dev/null @@ -1,390 +0,0 @@ -function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) -%drawFigure writes the parameters and data that are needed to draw the -%structure of a Bayesian network for BNW. -% This is the first function that - - - -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. -% -% -%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 -A=cell2mat(cases'); -Amax=max(A); -Amin=min(A); - -%Create an empty evidence cell array. -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); -[engine,loglik] = enter_evidence(engine,evidence); - -%Open the file, and write the nodes to a file. -fileID = fopen(filename,'w'); -%%% The number of nodes -fprintf(fileID,'%i\n',nnodes); - -%Get canvas size - -labels_temp = cellstr(labels); -[x,y] = make_layout(bnet.dag); -%[x,y] = layout_dag(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 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 - %cases(i) - % MAX(cases(i)) - % MIN(cases(i)) - [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 - x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; - fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); - end; - end; -end -%fprintf(fileID,'%s\t %\n',labels_temp{:}); - - -fclose(fileID); - -end - - -function [x_dim, y_dim] = canvasSize(nnodes,x,y) -%canvasSize Function to calculate the size of the canvas to -% build the network structure - - -%I am going to assume that the node size will be -% height = 150, width = 250 -% so there will be a node spacing of -% 200 (in y-dim) and 300 (in x-dim). -y_space = 200; -x_space = 300; - -%Set default minimum x and y dimensions -x_dim = 1200; -y_dim = 1200; - -%get unique y values -y_unique = unique(y); -size_y = size(y_unique,2); -y_dim_temp = size_y*y_space; - -%get the maximum nodes in any layer -size_x = zeros(1,size_y); -for i = 1:size_y, - for j = 1:nnodes, - if y_unique(i) == y(j), - size_x(1,i) = size_x(1,i) + 1; - end; - end; -end; -size_x = max(size_x); -x_dim_temp = size_x*x_space; - -if x_dim_temp > x_dim, - x_dim = x_dim_temp; -end; - -if y_dim_temp > y_dim, - y_dim = y_dim_temp; -end; -end - -function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) -%Function to calculate 101 points of Gaussian function to use in plotting -% Gets the probability density of the mean value and 50 evenly spaced -% points up to 3Sigma below the mean and 50 evenly space points up to -% 3Sigma above the mean. -%maxval -%minval -x_vals = zeros(101,1); -y_vals = zeros(101,1); - -%x_vals(1,1) = mu - 3*Sigma; -x_vals(1,1) = minval - 1; -gap=((maxval+1)-(minval - 1))/100; -%x_vals(1,1) = 0;%mu - 3*Sigma; -for i = 1:100, - % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; - x_vals(i+1,1) = x_vals(i,1) + gap; - %x_vals(i+1,1) = x_vals(i,1) + 1/100; -end - -for i = 1:101, - y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); -end - -end diff --git a/sourcecodes/parameter_learning/code_backup/drawFigure.m~ b/sourcecodes/parameter_learning/code_backup/drawFigure.m~ deleted file mode 100644 index 404a65f7..00000000 --- a/sourcecodes/parameter_learning/code_backup/drawFigure.m~ +++ /dev/null @@ -1,388 +0,0 @@ -function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) -%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. -% -% -%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 -A=cell2mat(cases'); -Amax=max(A); -Amin=min(A); - -%Create an empty evidence cell array. -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); -[engine,loglik] = enter_evidence(engine,evidence); - -%Open the file, and write the nodes to a file. -fileID = fopen(filename,'w'); -%%% The number of nodes -fprintf(fileID,'%i\n',nnodes); - -%Get canvas size - -labels_temp = cellstr(labels); -[x,y] = make_layout(bnet.dag); -%[x,y] = layout_dag(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 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 - %cases(i) - % MAX(cases(i)) - % MIN(cases(i)) - [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 - x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; - fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); - end; - end; -end -%fprintf(fileID,'%s\t %\n',labels_temp{:}); - - -fclose(fileID); - -end - - -function [x_dim, y_dim] = canvasSize(nnodes,x,y) -%canvasSize Function to calculate the size of the canvas to -% build the network structure - - -%I am going to assume that the node size will be -% height = 150, width = 250 -% so there will be a node spacing of -% 200 (in y-dim) and 300 (in x-dim). -y_space = 200; -x_space = 300; - -%Set default minimum x and y dimensions -x_dim = 1200; -y_dim = 1200; - -%get unique y values -y_unique = unique(y); -size_y = size(y_unique,2); -y_dim_temp = size_y*y_space; - -%get the maximum nodes in any layer -size_x = zeros(1,size_y); -for i = 1:size_y, - for j = 1:nnodes, - if y_unique(i) == y(j), - size_x(1,i) = size_x(1,i) + 1; - end; - end; -end; -size_x = max(size_x); -x_dim_temp = size_x*x_space; - -if x_dim_temp > x_dim, - x_dim = x_dim_temp; -end; - -if y_dim_temp > y_dim, - y_dim = y_dim_temp; -end; -end - -function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) -%Function to calculate 101 points of Gaussian function to use in plotting -% Gets the probability density of the mean value and 50 evenly spaced -% points up to 3Sigma below the mean and 50 evenly space points up to -% 3Sigma above the mean. -%maxval -%minval -x_vals = zeros(101,1); -y_vals = zeros(101,1); - -%x_vals(1,1) = mu - 3*Sigma; -x_vals(1,1) = minval - 1; -gap=((maxval+1)-(minval - 1))/100; -%x_vals(1,1) = 0;%mu - 3*Sigma; -for i = 1:100, - % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; - x_vals(i+1,1) = x_vals(i,1) + gap; - %x_vals(i+1,1) = x_vals(i,1) + 1/100; -end - -for i = 1:101, - y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); -end - -end diff --git a/sourcecodes/parameter_learning/code_backup/drawFigureM.m b/sourcecodes/parameter_learning/code_backup/drawFigureM.m deleted file mode 100644 index 91b8698f..00000000 --- a/sourcecodes/parameter_learning/code_backup/drawFigureM.m +++ /dev/null @@ -1,230 +0,0 @@ -function [] = drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) -%drawFigureM writes the parameters and data that are needed to draw the -%structure of a Bayesian network after added evidence or intervention - -fileID = fopen(filename,'w'); - - -A=cell2mat(cases'); -Amax=max(A); -Amin=min(A); - - -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); - -m = size(selectvar,1); - -ev_dat = zeros(1,nnodes); -for i = 1:m, - di=selectvar(i,1); - ev_dat(di)=selectdata(i,1); -%Need to standardized evidence for continuous nodes. - if bnet.node_sizes(di) == 1 - ev_dat(di) = (ev_dat(di) - means{di}) / stdevs{di}; - end - evidence{di}=ev_dat(di); - fprintf(fileID,'%i\t',di); -end - -fprintf(fileID,'\n'); - -[engine,loglik]=enter_evidence(engine,evidence); - -%Open the file, and write the nodes to a file. -%%% 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 - x_vals(j,1) = x_vals(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 - if bnet.node_sizes(i) == 1, - fprintf(fileID,'%6.4f\t%6.4f\n',ev_dat(i)*stdevs{i}+means{i},1); - else - fprintf(fileID,'%6.4f\t%6.4f\n',ev_dat(i),1); - endif - end - -end - -fclose(fileID); -end - - - - - - - - - -function [x_dim, y_dim] = canvasSize(nnodes,x,y) -%canvasSize Function to calculate the size of the canvas to -% build the network structure - - -%I am going to assume that the node size will be -% height = 150, width = 250 -% so there will be a node spacing of -% 200 (in y-dim) and 300 (in x-dim). -y_space = 200; -x_space = 300; - -%Set default minimum x and y dimensions -x_dim = 1200; -y_dim = 1200; - -%get unique y values -y_unique = unique(y); -size_y = size(y_unique,2); -y_dim_temp = size_y*y_space; - -%get the maximum nodes in any layer -size_x = zeros(1,size_y); -for i = 1:size_y, - for j = 1:nnodes, - if y_unique(i) == y(j), - size_x(1,i) = size_x(1,i) + 1; - end; - end; -end; -size_x = max(size_x); -x_dim_temp = size_x*x_space; - -if x_dim_temp > x_dim, - x_dim = x_dim_temp; -end; - -if y_dim_temp > y_dim, - y_dim = y_dim_temp; -end; -end - -function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) -%Function to calculate 101 points of Gaussian function to use in plotting -% Gets the probability density of the mean value and 50 evenly spaced -% points up to 3Sigma below the mean and 50 evenly space points up to -% 3Sigma above the mean. -%maxval -%minval -x_vals = zeros(101,1); -y_vals = zeros(101,1); - -%x_vals(1,1) = mu - 3*Sigma; -x_vals(1,1) = minval - 1; -gap=((maxval+1)-(minval - 1))/100; -%x_vals(1,1) = 0;%mu - 3*Sigma; -for i = 1:100, - % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; - x_vals(i+1,1) = x_vals(i,1) + gap; - %x_vals(i+1,1) = x_vals(i,1) + 1/100; -end - -for i = 1:101, - y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); -end - -end diff --git a/sourcecodes/parameter_learning/code_backup/getParams.m b/sourcecodes/parameter_learning/code_backup/getParams.m deleted file mode 100644 index 31f84ffb..00000000 --- a/sourcecodes/parameter_learning/code_backup/getParams.m +++ /dev/null @@ -1,22 +0,0 @@ -function [ bnet ] = getParams( bnet, cases ) -%getParams Code to initialize CPT and do parameter learning. -%This will be very basic for now. I can add more options later. - -dnodes = bnet.dnodes; -cnodes = bnet.cnodes; -nnodes = size(dnodes,2)+size(cnodes,2); - -%make dnodes tabular_CPT -for i = 1:size(dnodes,2) - bnet.CPD{dnodes(i)} = tabular_CPD(bnet,dnodes(i)); -end - -for i = 1:size(cnodes,2) - bnet.CPD{cnodes(i)} = gaussian_CPD(bnet,cnodes(i)); -end - -bnet = learn_params(bnet,cases); - - -end - diff --git a/sourcecodes/parameter_learning/code_backup/parameterLearning.m b/sourcecodes/parameter_learning/code_backup/parameterLearning.m deleted file mode 100644 index 872e94b1..00000000 --- a/sourcecodes/parameter_learning/code_backup/parameterLearning.m +++ /dev/null @@ -1,17 +0,0 @@ -function [ bnet ] = parameterLearning( bnet,cases,engine_name ) -%parameterLearning Do parameter learning and inference - -%engine is an optional argument -if nargin < 3 - engine_name = 'jtree_inf_engine'; -end - - -%First do parameter learning with all the data -[bnet] = getParams(bnet,cases); - - - - -end - diff --git a/sourcecodes/parameter_learning/code_backup/prepareInput.m b/sourcecodes/parameter_learning/code_backup/prepareInput.m deleted file mode 100644 index 838dcd2c..00000000 --- a/sourcecodes/parameter_learning/code_backup/prepareInput.m +++ /dev/null @@ -1,294 +0,0 @@ -function [ ] = prepareInput( pre ) - % - % This function takes files that are uploaded to BNW and creates output - % files that can be used for structure and parameter learning. - % It replaces php code that was previously in bn_file_load_gom.php. - % There are several improvements in performance and ease of use: - % 1) Loading files is significantly (~5x) faster for large input files. - % 2) The allowed values for discrete variables are more flexible. - % (e.g., A genotype variable be 'B' and 'D' instead of having - % to replace to make them '1' and '2'.) - % 3) Continuous variables may be identified as continuous in some cases - % even if there is not a period. - % 4) The states of discrete variables should be correctly ordered in - % almost all cases. - % 5) An additional output file is written that will let users check if - % the input file has been uploaded and parsed correctly. - % 6) Future updates to this code should be easier than updating the php. - % - % - % Input: ???continuous_input_orig.txt - % This is the input file that is uploaded to BNW. - % It is directly written out by the BNW php code with no modification. - % The file format is a header line containing the variable names - % followed by the data, with each case in a row. - % - % Output: There are many output files. - % 1) The main output file is ???continuous_input.txt that can be - % used by the structure learning code and parameter learning codes. - % The first line is variable names, the second line is the node type - % (continuous nodes should have 1, discrete nodes have the number - % of states), and the rest is the data. - % 2) A new output file is ???input_desc.txt, a file that describes the - % data so users can check that it has been parsed correctly. - % 3) ???nlevels.txt: The states of discrete variables. - % 4) ???name.txt: The names of the variables as uploaded. - % 5) ???type.txt: The number of states for each variables - % (1 indicates a continuous variable.) - % 6/7) ???nnode.txt and ???nrows.txt: number of nodes and cases - % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and - % ???parent.txt: Files with default values for structure learning. - % - -% open file for input, include error handling -dfile=strcat(pre,'continuous_input_orig.txt'); - -fin = fopen(dfile,'r'); -if fin < 0 - error(['Could not open ',dfile,' for input']); -end - -% Get the number of cases (the number of rows in the file excluding the header) -ncases = fskipl(fin,Inf) - 1; - -frewind(fin); - -% Read in first line to get the number of nodes and the node labels. -buffer = fgetl(fin); %get header line as a string -nnodes = numel(strfind(buffer,"\t")) + 1; -labels = cell(1,nnodes); -for j=1:nnodes - [next,buffer] = strtok(buffer); - labels{j} = next; -end - -% Read in the data -data = cell(ncases,nnodes); -for i = 1:ncases - buffer = fgetl(fin); - for j = 1:nnodes - [next,buffer] = strtok(buffer); - data{i,j} = next; - end -end - -% Determine whether or not the nodes are continuous or discrete. -% First, treat them as all discrete and get the states and number of stats(levels). -levels = cell(1,nnodes); -states = []; -for j = 1:nnodes - states{end+1} = unique(data(:,j)); - levels{j} = size(states{j},1); -end - -reason = cell(1,nnodes); -%Now do some checks to see if nodes are discrete or continuous -for j = 1:nnodes - % If there are 3 or less unique values, I will assume that the node is discrete. - if levels{j} < 4; - reason{j} = "It was determined to be discrete because there are a small number (<4) of possible values."; - continue - % If there are as many unique values as a third of the number of cases, - % I will assume that the node is continuous. - elseif levels{j} > ncases/3; - levels{j} = 1; - reason{j} = "It was determined to be continuous because there are a large number of possible values compared to the number of cases."; - continue - % If there are more than twenty unique values, - % I will assume that the node is continuous. - elseif levels{j} > 20; - levels{j} = 1; - reason{j} = "It was determined to be continuous because there are many (>20) possible values."; - continue - % Otherwise, I will scan through the individual values. - % If any of the values contain a '.', I will assume it is continuous. - else - reason{j} = "It was determined to be discrete by default."; - period_test = 0; - column = data(:,j); - k = 1; - while period_test == 0 - period_test = sum(cell2mat(strfind(column(k),"."))); - if period_test != 0; - reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period(.)."; - levels{j} = 1; - end - k++; - if k > ncases - break - end - end - end -end - -%I need to check if any discrete nodes are listed after continuous nodes. -%If so, I need to rearrange the columns. -max_disc = 0; -min_cont = nnodes + 1; -for i = 1:nnodes - if levels{i} > 1 - max_disc = i; - elseif min_cont == nnodes+1 - min_cont = i; - end -end -%If max_disc > min_cont, you need to rearrange the nodes -% to put the discrete nodes first. -if max_disc > min_cont - levels_old = levels; - labels_old = labels; - data_old = data; - states_old = states; - reason_old = reason; - new_order = {}; - for i=1:nnodes - if levels_old{i} > 1 - new_order{end+1} = i; - end - end - for i=1:nnodes - if levels_old{i} == 1 - new_order{end+1} = i; - end - end - labels = {}; - levels = {}; - states = {}; - reason = {}; - for i =1:nnodes - labels{i} = labels_old{new_order{i}}; - levels{i} = levels_old{new_order{i}}; - states{i} = states_old{new_order{i}}; - reason{i} = reason_old{new_order{i}}; - for j=1:ncases - data{j,i} = data_old{j,new_order{i}}; - end - end - -endif - - -%Write other files that are used by BNW for this key. -%The first group of files establish default settings for structure learning. -outfile = strcat(pre,'white.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'From\tTo\n'); -fclose(fout); - -outfile = strcat(pre,'ban.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'From\tTo\n'); -fclose(fout); - -outfile = strcat(pre,'k.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'1\n'); -fclose(fout); - -outfile = strcat(pre,'parent.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'4\n'); -fclose(fout); - -outfile = strcat(pre,'thr.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'0.5\n'); -fclose(fout); - - -%The next group of files have information about the uploaded file. -outfile = strcat(pre,'name.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); -fclose(fout); - -outfile = strcat(pre,'nnode.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%i\n',nnodes); -fclose(fout); - -outfile = strcat(pre,'nrows.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%i\n',ncases); -fclose(fout); - -outfile = strcat(pre,'type.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); -fprintf(fout,'%i\t',levels{1:end-1}); -fprintf(fout,'%i\n',levels{end}); -fclose(fout); - -%This output file contains the states for discrete nodes. -% The unique matlab function already sorts the states. -outfile = strcat(pre,'nlevels.txt'); -fout = fopen(outfile,'w'); -for i = 1:nnodes - if levels{i} > 1 - fprintf(fout,'%s\t',labels{i},states{i}{1:end-1}); - fprintf(fout,'%s\n',states{i}{end}); - end -end -fclose(fout); - - -%Print a file with a short description of the input. -descfile = strcat(pre,'input_desc.txt'); -dout = fopen(descfile,'w'); -fprintf(dout,['As loaded, the input file had the following properties:\n\n']); -dout = fopen(descfile,'a'); -fprintf(dout,'There are %i variables and %i cases(rows).\n',size(labels,2),ncases); -fprintf(dout,'The variable names are:\n'); -fprintf(dout,'%s\t',labels{1:end-1}); -fprintf(dout,'%s\n\n',labels{end}); -for i=1:nnodes - if levels{i} == 1 - fprintf(dout,'%s is a continuous variable.\n',labels{i}); - fprintf(dout,'%s\n',reason{i}); - column = str2double(data(:,i)); - colmean = mean(column); - colstd = std(column); - fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) - else - fprintf(dout,'%s is a discrete variable with %i states.\n',labels{i},levels{i}); - fprintf(dout,'%s\n',reason{i}); - fprintf(dout,'The states are: '); - fprintf(dout,'%s ',states{i}{1:end-1}); - fprintf(dout,'%s\n\n',states{i}{end}); - end -end -fclose(fout); - -outfile = strcat(pre,'continuous_input.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); -fprintf(fout,'%i\t',levels{1:end-1}); -fprintf(fout,'%i\n',levels{end}); -%Need to replace states in discrete variables with integers for BNT -for i = 1:nnodes - if levels{i} > 1 - for j = 1:ncases - for k=1:size(states{i},1) - if data{j,i} == states{i}{k} - data{j,i} = sprintf('%i',num2cell(k){1});; - break - end - end - end - end -end -for i = 1:ncases - fprintf(fout,'%s\t',data{i,1:end-1}); - fprintf(fout,'%s\n',data{i,end}); -end -fclose(fout); - - - - - -end -% end of prepareInput.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/prepareInput.m~ b/sourcecodes/parameter_learning/code_backup/prepareInput.m~ deleted file mode 100644 index 9fc0f97f..00000000 --- a/sourcecodes/parameter_learning/code_backup/prepareInput.m~ +++ /dev/null @@ -1,294 +0,0 @@ -function [ ] = prepareInput( pre ) - % - % This function takes files that are uploaded to BNW and creates output - % files that can be used for structure and parameter learning. - % It replaces php code that was previously in bn_file_load_gom.php. - % There are several improvements in performance and ease of use: - % 1) Loading files is significantly (~5x) faster for large input files. - % 2) The allowed values for discrete variables are more flexible. - % (e.g., A genotype variable be 'B' and 'D' instead of having - % to replace to make them '1' and '2'.) - % 3) Continuous variables may be identified as continuous in some cases - % even if there is not a period. - % 4) The states of discrete variables should be correctly ordered in - % almost all cases. - % 5) An additional output file is written that will let users check if - % the input file has been uploaded and parsed correctly. - % 6) Future updates to this code should be easier than updating the php. - % - % - % Input: ???continuous_input_orig.txt - % This is the input file that is uploaded to BNW. - % It is directly written out by the BNW php code with no modification. - % The file format is a header line containing the variable names - % followed by the data, with each case in a row. - % - % Output: There are many output files. - % 1) The main output file is ???continuous_input.txt that can be - % used by the structure learning code and parameter learning codes. - % The first line is variable names, the second line is the node type - % (continuous nodes should have 1, discrete nodes have the number - % of states), and the rest is the data. - % 2) A new output file is ???input_desc.txt, a file that describes the - % data so users can check that it has been parsed correctly. - % 3) ???nlevels.txt: The states of discrete variables. - % 4) ???name.txt: The names of the variables as uploaded. - % 5) ???type.txt: The number of states for each variables - % (1 indicates a continuous variable.) - % 6/7) ???nnode.txt and ???nrows.txt: number of nodes and cases - % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and - % ???parent.txt: Files with default values for structure learning. - % - -% open file for input, include error handling -dfile=strcat(pre,'continuous_input_orig.txt'); - -fin = fopen(dfile,'r'); -if fin < 0 - error(['Could not open ',dfile,' for input']); -end - -% Get the number of cases (the number of rows in the file excluding the header) -ncases = fskipl(fin,Inf) - 1; - -frewind(fin); - -% Read in first line to get the number of nodes and the node labels. -buffer = fgetl(fin); %get header line as a string -nnodes = numel(strfind(buffer,"\t")) + 1; -labels = cell(1,nnodes); -for j=1:nnodes - [next,buffer] = strtok(buffer); - labels{j} = next; -end - -% Read in the data -data = cell(ncases,nnodes); -for i = 1:ncases - buffer = fgetl(fin); - for j = 1:nnodes - [next,buffer] = strtok(buffer); - data{i,j} = next; - end -end - -% Determine whether or not the nodes are continuous or discrete. -% First, treat them as all discrete and get the states and number of stats(levels). -levels = cell(1,nnodes); -states = []; -for j = 1:nnodes - states{end+1} = unique(data(:,j)); - levels{j} = size(states{j},1); -end - -reason = cell(1,nnodes); -%Now do some checks to see if nodes are discrete or continuous -for j = 1:nnodes - % If there are 3 or less unique values, I will assume that the node is discrete. - if levels{j} < 4; - reason{j} = "This was determined to be discrete because there are few (<4) different values."; - continue - % If there are as many unique values as a third of the number of cases, - % I will assume that the node is continuous. - elseif levels{j} > ncases/3; - levels{j} = 1; - reason{j} = "This was determined to be continuous because there are a large number of different values compared to the number of cases."; - continue - % If there are more than twenty unique values, - % I will assume that the node is continuous. - elseif levels{j} > 20; - levels{j} = 1; - reason{j} = "This was determined to be continuous because there are many (>20) possible values."; - continue - % Otherwise, I will scan through the individual values. - % If any of the values contain a '.', I will assume it is continuous. - else - reason{j} = "This variable was determined to be discrete."; - period_test = 0; - column = data(:,j); - k = 1; - while period_test == 0 - period_test = sum(cell2mat(strfind(column(k),"."))); - if period_test != 0; - reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period (".")."; - levels{j} = 1; - end - k++; - if k > ncases - break - end - end - end -end - -%I need to check if any discrete nodes are listed after continuous nodes. -%If so, I need to rearrange the columns. -max_disc = 0; -min_cont = nnodes + 1; -for i = 1:nnodes - if levels{i} > 1 - max_disc = i; - elseif min_cont == nnodes+1 - min_cont = i; - end -end -%If max_disc > min_cont, you need to rearrange the nodes -% to put the discrete nodes first. -if max_disc > min_cont - levels_old = levels; - labels_old = labels; - data_old = data; - states_old = states; - reason_old = reason; - new_order = {}; - for i=1:nnodes - if levels_old{i} > 1 - new_order{end+1} = i; - end - end - for i=1:nnodes - if levels_old{i} == 1 - new_order{end+1} = i; - end - end - labels = {}; - levels = {}; - states = {}; - reason = {}; - for i =1:nnodes - labels{i} = labels_old{new_order{i}}; - levels{i} = levels_old{new_order{i}}; - states{i} = states_old{new_order{i}}; - reason{i} = reason_old{new_order{i}}; - for j=1:ncases - data{j,i} = data_old{j,new_order{i}}; - end - end - -endif - - -%Write other files that are used by BNW for this key. -%The first group of files establish default settings for structure learning. -outfile = strcat(pre,'white.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'From\tTo\n'); -fclose(fout); - -outfile = strcat(pre,'ban.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'From\tTo\n'); -fclose(fout); - -outfile = strcat(pre,'k.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'1\n'); -fclose(fout); - -outfile = strcat(pre,'parent.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'4\n'); -fclose(fout); - -outfile = strcat(pre,'thr.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'0.5\n'); -fclose(fout); - - -%The next group of files have information about the uploaded file. -outfile = strcat(pre,'name.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); -fclose(fout); - -outfile = strcat(pre,'nnode.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%i\n',nnodes); -fclose(fout); - -outfile = strcat(pre,'nrows.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%i\n',ncases); -fclose(fout); - -outfile = strcat(pre,'type.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); -fprintf(fout,'%i\t',levels{1:end-1}); -fprintf(fout,'%i\n',levels{end}); -fclose(fout); - -%This output file contains the states for discrete nodes. -% The unique matlab function already sorts the states. -outfile = strcat(pre,'nlevels.txt'); -fout = fopen(outfile,'w'); -for i = 1:nnodes - if levels{i} > 1 - fprintf(fout,'%s\t',labels{i},states{i}{1:end-1}); - fprintf(fout,'%s\n',states{i}{end}); - end -end -fclose(fout); - - -%Print a file with a short description of the input. -descfile = strcat(pre,'input_desc.txt'); -dout = fopen(descfile,'w'); -fprintf(dout,['As loaded, the input file had the following properties:\n\n']); -dout = fopen(descfile,'a'); -fprintf(dout,'There are %i variables and %i cases(rows)\n',size(labels,2),ncases); -fprintf(dout,'The variable names are:\n'); -fprintf(dout,'%s\t',labels{1:end-1}); -fprintf(dout,'%s\n\n',labels{end}); -for i=1:nnodes - if levels{i} == 1 - fprintf(dout,'%s is a continuous variable\n',labels{i}); - fprintf(dout,'%s\n',reason{i}); - column = str2double(data(:,i)); - colmean = mean(column); - colstd = std(column); - fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) - else - fprintf(dout,'%s is a discrete variable with %i states\n',labels{i},levels{i}); - fprintf(dout,'%s\n',reason{i}); - fprintf(dout,'The states are: '); - fprintf(dout,'%s ',states{i}{1:end-1}); - fprintf(dout,'%s\n\n',states{i}{end}); - end -end -fclose(fout); - -outfile = strcat(pre,'continuous_input.txt'); -fout = fopen(outfile,'w'); -fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); -fprintf(fout,'%i\t',levels{1:end-1}); -fprintf(fout,'%i\n',levels{end}); -%Need to replace states in discrete variables with integers for BNT -for i = 1:nnodes - if levels{i} > 1 - for j = 1:ncases - for k=1:size(states{i},1) - if data{j,i} == states{i}{k} - data{j,i} = sprintf('%i',num2cell(k){1});; - break - end - end - end - end -end -for i = 1:ncases - fprintf(fout,'%s\t',data{i,1:end-1}); - fprintf(fout,'%s\n',data{i,end}); -end -fclose(fout); - - - - - -end -% end of prepareInput.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/readInput.m b/sourcecodes/parameter_learning/code_backup/readInput.m deleted file mode 100644 index 891d7f36..00000000 --- a/sourcecodes/parameter_learning/code_backup/readInput.m +++ /dev/null @@ -1,63 +0,0 @@ -function [ labels, cases, bnet, node_sizes, data,labelsold] = readInput( dfile, sfile, nnodes, std_flag ) - %readInput is to be used when reading in a network with a known structure - % - %Input: - % dfile = name of the file containing the data (required) - % sfile = name of the file containing the structure (required) - % nnodes = number of nodes in the network (required) - % std_flag = flag for whether or not to standardize the data. - % (optional-- Default is FALSE) - % - % See readInputData.m and readInputStructure.m for description of the - % format of the dfile and sfile, respectively. - % - %Output: - % labels = cell array with the names of the nodes. - % cases = cell array with the data. - % bnet = BNT bayesian network with the input structure. - -if nargin < 4 - std_flag = false(1); -end - - -% read in the file with the data -[labelsold,node_sizes,cases, data] = readInputData(dfile,nnodes); - - -% read in the file with the structure -[dag] = readInputStructure(sfile,labelsold); - - -% check the ordering of the nodes and reorder if necessary -[labels,cases,dag,node_sizes,ord_flag] = checkStructure(labelsold,cases,dag,node_sizes); - -dcount = 0; -for i = 1:nnodes - if node_sizes(i) ~= 1 - dcount = dcount + 1; - end -end -discrete = zeros(1,dcount); -dcount = 0; -for i = 1:nnodes - if node_sizes(i) ~= 1 - dcount = dcount + 1; - discrete(dcount) = i; - end -end - -bnet = mk_bnet(dag,node_sizes,'discrete',discrete,'names',labels); - -%bnet.dag - -checkDiscreteNodes(bnet,cases); - -% standardize continuous data to have a mean = 0 and std = 1 -if (std_flag) - [cases] = standardizeData(labels,node_sizes,cases); -end - - -end -% end of readInput.m diff --git a/sourcecodes/parameter_learning/code_backup/readInputData.m b/sourcecodes/parameter_learning/code_backup/readInputData.m deleted file mode 100644 index 706e2751..00000000 --- a/sourcecodes/parameter_learning/code_backup/readInputData.m +++ /dev/null @@ -1,75 +0,0 @@ -function [ labels , node_sizes, cases, data] = readInputData( dfile , nnodes ) - % readColData reads data from a file containing data in columns - % that have text titles, and possibly other header text - % - % Input: - % dfile = name of the file containing the data.(required) - % nnodes = number of columns in the data file. (required) - % - % Function assumes the following format for the input file: - % 1) First line has labels for each of the nodes. There cannot - % be spaces in any node label. - % 2) The next line is the "node_sizes" of the nodes. If the - % nodes are discrete, this number will be equal to the number - % of states. If the nodes are continuous, they should be - % equal to 1. The function assumes that any nodes with - % node_size = 1 is continuous. - % 3) The rest of the file is numeric data. The data in the input - % data has the number of columns equal to the number of - % nodes in the network and the number of rows equal to - % the number of samples. - % - % - % Output: - % labels = cell array with node (column) labels. - % node_sizes = vector with the size of each node - % cases = cell array with the data. The cases array is transposed - % in comparison with the input data to agree with the format of - % cell data used in BNT. - -% open file for input, include error handling -fin = fopen(dfile,'r'); -if fin < 0 - error(['Could not open ',dfile,' for input']); -end - -% Read in first line to get the node labels. -labels = cell(1,nnodes); -buffer = fgetl(fin); %get header line as a string -for j=1:nnodes - [next,buffer] = strtok(buffer); - labels{j} = next; -end - -% Read in the data. Use the vetorized fscanf function to load all -% numerical values into one vector. Then reshape this vector into a -% matrix. - -data = fscanf(fin,'%f'); % Load the numerical values into one long vector - - - - -nd = length(data); % total number of data points -nr = nd/nnodes; % number of rows; check (next statement) to make sure -if nr ~= round(nd/nnodes) - fprintf(1,'\ndata: nrow = %f\tncol = %d\n',nr,nnodes); - fprintf(1,'number of data points = %d does not equal nrow*ncol\n',nd); - error('data is not rectangular') -end - -data = reshape(data,nnodes,nr)'; % have to transpose the reshaped array - - -node_sizes = zeros(1,nnodes); -for j = 1:nnodes - node_sizes(j) = data(1,j); -end - -nr = nr - 1; -data(1,:) = []; -cases = cell(nnodes,nr); -cases(:,:) = num2cell(data'); - -end -% end of readInputData.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/readInputStructure.m b/sourcecodes/parameter_learning/code_backup/readInputStructure.m deleted file mode 100644 index 6b3cbece..00000000 --- a/sourcecodes/parameter_learning/code_backup/readInputStructure.m +++ /dev/null @@ -1,72 +0,0 @@ -function [ dag ] = readInputStructure( sfile, labels ) -%readInputStructure Read in file with structure information - % - %Input: - % sfile = name of the file containing the data (required) - % labels = cell array with node labels. (required) - % nnodes = number of columns in the data file. (required) - % - % Function assumes the following format for the structure input file: - % 1) The first line has node labels. These must be the same as - % in the input data file. They cannot contain spaces. - % 2) The remainder of the file contains the structure of the dag. - % The structure of a graph is a N-by-N matrix, where N is the - % number of nodes. There are 1's in the matrix representing - % parent-child relationships. For each 1, the row indicates - % the parent and the column indicates the child. For - % example, a 1 in the (2,3) position of the matrix indicates - % that there is an arc pointing from node 2 to node 3. - % - % - % Output: - % dag = matrix with the structure. -% -% Read in first line of the structure file -% open file for input, include error handling -fin = fopen(sfile,'r'); -if fin < 0 - error(['Could not open ',sfile,' for input']); -end - -nnodes = size(labels,2); -% Read in first line to get the node labels. -labels_test = cell(1,nnodes); -buffer = fgetl(fin); %get header line as a string -for j=1:nnodes - [next,buffer] = strtok(buffer); - labels_test{j} = next; -end - -for j=1:nnodes - if labels_test{j} ~= labels{j} - fprintf(['Label of node ',j,' is not consistent in input and structure files']) - end -end - -data = fscanf(fin,'%f'); - -nd = length(data); % total number of data points -nr = nd/nnodes; % number of rows; check (next statement) to make sure -if nr ~= round(nd/nnodes) - fprintf(1,'\ndata: nrow = %f\tncol = %d\n',nr,nnodes); - fprintf(1,'number of data points = %d does not equal nrow*ncol\n',nd); - error('Structure file does not have the correct dimensions (1)') -end -% check to make sure that structure is square -if nr ~= nnodes - error('Structure file does not have the correct dimensions (2)') -end - -data = reshape(data,nnodes,nr)'; % have to transpose the reshaped array - - -dag = zeros(nnodes,nnodes); -for i = 1:size(data,1) - for j = 1:size(data,2) - dag(i,j) = data(i,j); - end -end - - -end -% end of readInputStructure.m diff --git a/sourcecodes/parameter_learning/code_backup/runBN_initial.m b/sourcecodes/parameter_learning/code_backup/runBN_initial.m deleted file mode 100644 index 0deff1b5..00000000 --- a/sourcecodes/parameter_learning/code_backup/runBN_initial.m +++ /dev/null @@ -1,57 +0,0 @@ -function runBN_initial(pre) -sfile=strcat(pre,'structure_input.txt'); -dfile=strcat(pre,'continuous_input.txt'); - -nnodefile=strcat(pre,'nnode.txt'); -fnnode = fopen(nnodefile,'r'); -nnodes = fscanf(fnnode,'%d'); - - -mapfilename=strcat(pre,'mapdata.txt'); -mapvalfilename=strcat(pre,'map.txt'); - -mapfile = fopen(mapfilename,'w'); - -mapval = fopen(mapvalfilename,'w'); - - -Std_flag=true; -[labels,cases,bnet,node_sizes,data,labelsold]=readInput(dfile,sfile,nnodes,Std_flag); -s=std(data,0,1); -m=mean(data); - -for i=1:nnodes - fprintf(mapval,'%s\t%f\t%f\n',labelsold{i},s(i),m(i)); -end - -fprintf(mapfile,'%s',labels{1}); -for i=2:nnodes - fprintf(mapfile,'\t%s',labels{i}); -end -fprintf(mapfile,'\n'); -fclose(mapval); -fclose(mapfile); - -%Need to rearrange the means and stdevs to match the new labeling. -means = cell(1,nnodes); -stdevs = cell(1,nnodes); -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labels{i},labelsold{j}) - means{i} = m(j); - stdevs{i} = s(j); - break - end - end -end - - -[bnet]=parameterLearning(bnet,cases); - -filename=strcat(pre,'net_figure.txt'); - -drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means); - -writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m); - -end diff --git a/sourcecodes/parameter_learning/code_backup/standardizeData.m b/sourcecodes/parameter_learning/code_backup/standardizeData.m deleted file mode 100644 index db5e04c7..00000000 --- a/sourcecodes/parameter_learning/code_backup/standardizeData.m +++ /dev/null @@ -1,25 +0,0 @@ -function [ cases ] = standardizeData( labels, node_sizes, cases ) -%standardizeData standardizes continuous nodes so they have a mean = 0 -% and standard deviation = 1 - - -nnodes = size(labels,2); - -%fprintf(['Standardizing data for continuous nodes\n']) -for i = 1:nnodes - if node_sizes(i) == 1 - temp = cell2num(cases(i,:)); - [temp] = standardize(temp); - cases(i,:) = num2cell(temp); - end -end - -%write standardized data to file -%fprintf(['Standardized data is written to file standardized_data.txt\n']) -%fout = 'standardized_data.txt'; -%txt = sprintf([repmat('%s\t',1,size(labels,2))],labels{:}); -%dlmwrite(fout,txt,''); -%dlmwrite(fout,cell2num(cases'),'-append','delimiter','\t'); - -end - diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters.m b/sourcecodes/parameter_learning/code_backup/writeParameters.m deleted file mode 100644 index 0790a8e2..00000000 --- a/sourcecodes/parameter_learning/code_backup/writeParameters.m +++ /dev/null @@ -1,106 +0,0 @@ -function [] = writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m) -%Writes a file that contains the parameters of the network with no evidence. - - -%%Get the types of the nodes. -typefile = strcat(pre,'type.txt'); -ftype = fopen(typefile,'r'); -types = cell(1,nnodes); -buffer = fgetl(ftype); -buffer = fgetl(ftype); -for j = 1:nnodes - [next,buffer] = strtok(buffer); - types{j} = uint16(str2num(next)); -end - -max_states = 0; -disc_nodes = 0; -for j = 1:nnodes - if types{j} > max_states - max_states = types{j}; - end - if types{j} > 1 - disc_nodes = disc_nodes + 1; - end -end - -%Add 1 to max_states to account for node name -max_states = max_states + 1; - -%%Get mapping of discrete levels. -levelfile = strcat(pre,'nlevels.txt'); -flevels = fopen(levelfile,'r'); -levels = cell(disc_nodes,max_states); -ndisc_nodes = 0; -for i=1:disc_nodes - ndisc_nodes = ndisc_nodes + 1; - buffer = fgetl(flevels); - for j = 1:max_states - [next,buffer] = strtok(buffer); - if j == 1 - levels{i,j} = next; - else -% levels{i,j} = uint16(str2num(next)); - levels{i,j} = next; - end - if length(buffer) < 1 - break - end - end -end - - -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); -[engine,loglik] = enter_evidence(engine,evidence); - -%Open output file. -filename = strcat(pre,'parameters.txt'); -fileID = fopen(filename,'w'); - -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labelsold{i},labels{j}); - nodeid = j; - break - end - end - predict = marginal_nodes(engine,nodeid); - %%%Print the name of the node - fprintf(fileID,'%s\n',labels{nodeid}); - %%%Print the type of node - if bnet.node_sizes(nodeid) == 1; - line = 'Continuous node\n'; - fprintf(fileID,line); - %%% 'i' in the line below is correct: m and s are had original node labeling - adj_mu = predict.mu*s(i)+m(i); - adj_sigma = s(i)*predict.Sigma; - fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); - else - line = 'Discrete node with %i states\n'; - fprintf(fileID,line,bnet.node_sizes(nodeid)); - %line = 'Probability of each state\n'; - %fprintf(fileID,line); - nodeid2 = 0; - for k = 1:ndisc_nodes, - if strcmp(levels{k,1},labels{nodeid}), - nodeid2 = k; - break - end - end - for j = 1:bnet.node_sizes(nodeid), - %%%For discrete nodes, the state and the percent of that state -% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); - fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); - end; - fprintf(fileID,'\n') - - end -end - - - -fclose(fileID); - -end - diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m b/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m deleted file mode 100644 index fc24e2e5..00000000 --- a/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m +++ /dev/null @@ -1,151 +0,0 @@ -function [] = writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) -%Writes a file that contains the parameters of the network after entering evidence. - -%Read in original node labels to get node IDs. -infile = strcat(pre,'continuous_input.txt'); -fin = fopen(infile,'r'); -labelsold = cell(1,nnodes); -buffer = fgetl(fin); -for j = 1:nnodes - [next,buffer] = strtok(buffer); - labelsold{j} = next; -end -fclose(fin); - - -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); - -m = size(selectvar,1); - -%%Get the types of the nodes. -typefile = strcat(pre,'type.txt'); -ftype = fopen(typefile,'r'); -types = cell(1,nnodes); -buffer = fgetl(ftype); -buffer = fgetl(ftype); -for j = 1:nnodes - [next,buffer] = strtok(buffer); - types{j} = uint16(str2num(next)); -end - -max_states = 0; -disc_nodes = 0; -for j = 1:nnodes - if types{j} > max_states - max_states = types{j}; - end - if types{j} > 1 - disc_nodes = disc_nodes + 1; - end -end - -%Add 1 to max_states to account for node name -max_states = max_states + 1; - -%%Get mapping of discrete levels. -levelfile = strcat(pre,'nlevels.txt'); -flevels = fopen(levelfile,'r'); -levels = cell(disc_nodes,max_states); -ndisc_nodes = 0; -for i=1:disc_nodes - ndisc_nodes = ndisc_nodes + 1; -buffer = fgetl(flevels); -for j = 1:max_states - [next,buffer] = strtok(buffer); - if j == 1 - levels{i,j} = next; - else -% levels{i,j} = uint16(str2num(next)); - levels{i,j} = next; - end - if length(buffer) < 1 - break - end - end -end - - -ev_dat = zeros(1,nnodes); -for i = 1:m, - di=selectvar(i,1); - ev_dat(di)=selectdata(i,1); -%Need to standardize evidence for continuous nodes. - if bnet.node_sizes(di) == 1, - ev_dat(di) = (ev_dat(di) - means{di})/stdevs{di}; - end - evidence{di} = ev_dat(di); -end - -[engine,loglik]=enter_evidence(engine,evidence); - -%Open output file. -filename = strcat(pre,'parameters_ev.txt'); -fileID = fopen(filename,'w'); - -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labelsold{i},labels{j}); - nodeid = j; - break - end - end - %%%Print the name of the node - fprintf(fileID,'%s\n',labels{nodeid}); - predict = marginal_nodes(engine,nodeid); - if isempty(evidence{nodeid}) - %%%Print the type of node - if bnet.node_sizes(nodeid) == 1; - line = 'Continuous parameters considering evidence:\n'; - fprintf(fileID,line); - %line = 'Mean and standard deviation of Gaussian distribution\n'; - %fprintf(fileID,line); - adj_mu = predict.mu*stdevs{nodeid}+means{nodeid}; - adj_sigma = stdevs{nodeid}*predict.Sigma; - fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); - else - line = 'Probability of states considering evidence:\n'; - fprintf(fileID,line); - nodeid2 = 0; - for k = 1:ndisc_nodes, - if strcmp(levels{k,1},labels{nodeid}), - nodeid2 = k; - break - end - end - for j = 1:bnet.node_sizes(nodeid), - %%%For discrete nodes, the state and the percent of that state -% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); - fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); - end; - fprintf(fileID,'\n') - end - else - if bnet.node_sizes(nodeid) == 1; - line = 'Evidence was observed for this node. The observed value was:\n'; - fprintf(fileID,line); - adj_mu = ev_dat(nodeid)*stdevs{nodeid}+means{nodeid}; - fprintf(fileID,'%6.4f\n\n',adj_mu); - else - nodeid2 = 0; - for k = 1:ndisc_nodes, - if strcmp(levels{k,1},labels{nodeid}), - nodeid2 = k; - break - end - end - line = 'Evidence was observed for this node. The observed state was:\n'; - fprintf(fileID,line); - state_ev = uint16(ev_dat(nodeid)); -% fprintf(fileID,'%i\n\n',levels{nodeid2,state_ev+1}); - fprintf(fileID,'%s\n\n',levels{nodeid2,state_ev+1}); - end - end -end - - - -fclose(fileID); - -end - diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters_int.m b/sourcecodes/parameter_learning/code_backup/writeParameters_int.m deleted file mode 100644 index ed92d593..00000000 --- a/sourcecodes/parameter_learning/code_backup/writeParameters_int.m +++ /dev/null @@ -1,186 +0,0 @@ -function [] = writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) -%Writes a file that contains the parameters of the network after intervention. - - -%First read input file to get node labels to get node IDs. -infile = strcat(pre,'continuous_input.txt'); -fin = fopen(infile,'r'); -labelsold = cell(1,nnodes); -buffer = fgetl(fin); -for j = 1:nnodes - [next,buffer] = strtok(buffer); - labelsold{j} = next; -end - -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); - -m = size(selectvar,1); - -%%Get the types of the nodes. -typefile = strcat(pre,'type.txt'); -ftype = fopen(typefile,'r'); -types = cell(1,nnodes); -buffer = fgetl(ftype); -buffer = fgetl(ftype); -for j = 1:nnodes - [next,buffer] = strtok(buffer); - types{j} = uint16(str2num(next)); -end - -max_states = 0; -disc_nodes = 0; -for j = 1:nnodes - if types{j} > max_states - max_states = types{j}; - end - if types{j} > 1 - disc_nodes = disc_nodes + 1; - end -end - -%Add 1 to max_states to account for node name -max_states = max_states + 1; - -%%Get mapping of discrete levels. -levelfile = strcat(pre,'nlevels.txt'); -flevels = fopen(levelfile,'r'); -levels = cell(disc_nodes,max_states); -ndisc_nodes = 0; -for i=1:disc_nodes - ndisc_nodes = ndisc_nodes + 1; -buffer = fgetl(flevels); -for j = 1:max_states - [next,buffer] = strtok(buffer); - if j == 1 - levels{i,j} = next; - else -% levels{i,j} = uint16(str2num(next)); - levels{i,j} = next; - end - if length(buffer) < 1 - break - end - end -end - - -ev_dat = zeros(1,nnodes); -for i = 1:m, - di=selectvar(i,1); - ev_dat(di)=selectdata(i,1); -%Need to standardize evidence for continuous nodes. - if bnet.node_sizes(di) == 1, - ev_dat(di) = (ev_dat(di) - means{di})/stdevs{di}; - end - evidence{di} = ev_dat(di); -end - -[engine,loglik]=enter_evidence(engine,evidence); - -%Get list of nodes that are children, grandchildren, etc. of intervened nodes -%int_nodes contains the list of these children nodes -int_nodes = zeros(1,nnodes); -%new_nodes is just a temporary array to know when to keep looking -new_nodes = zeros(1,nnodes); -for i = 1:nnodes - if !isempty(evidence{i}); - new_nodes(i) = 1; - int_nodes(i) = 1; - end -end -while sum(new_nodes) != 0 - new_nodes_old = new_nodes; - new_nodes = zeros(1,nnodes); - for i = 1:nnodes - if new_nodes_old(i) == 1 - for j = 1:nnodes - if int_nodes(j) == 0 - if bnet.dag(i,j) == 1, - new_nodes(j) = 1; - end - end - end - end - end - for i = 1:nnodes - if new_nodes(i) == 1; - int_nodes(i) = 1; - end - end -end - - -%Open output file. -filename = strcat(pre,'parameters_ev.txt'); -fileID = fopen(filename,'w'); - -for i = 1:nnodes - for j = 1:nnodes - if strcmp(labelsold{i},labels{j}); - nodeid = j; - break - end - end - %check to see if this is a node impacted by intervention - if int_nodes(nodeid) == 1 - %%%Print the name of the node - fprintf(fileID,'%s\n',labels{nodeid}); - predict = marginal_nodes(engine,nodeid); - if isempty(evidence{nodeid}) - %%%Print the type of node - if bnet.node_sizes(nodeid) == 1; - line = 'Continuous parameters considering intervention:\n'; - fprintf(fileID,line); - %line = 'Mean and standard deviation of Gaussian distribution\n'; - %fprintf(fileID,line); - adj_mu = predict.mu*stdevs{nodeid}+means{nodeid}; - adj_sigma = stdevs{nodeid}*predict.Sigma; - fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); - else - line = 'Probability of states considering intervention:\n'; - fprintf(fileID,line); - nodeid2 = 0; - for k = 1:ndisc_nodes, - if strcmp(levels{k,1},labels{nodeid}), - nodeid2 = k; - break - end - end - for j = 1:bnet.node_sizes(nodeid), - %%%For discrete nodes, the state and the percent of that state -% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); - fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); - end; - fprintf(fileID,'\n') - end - else - if bnet.node_sizes(nodeid) == 1; - line = 'Intervention on this node assigned the following value:\n'; - fprintf(fileID,line); - adj_mu = ev_dat(nodeid)*stdevs{nodeid}+means{nodeid}; - fprintf(fileID,'%6.4f\n\n',adj_mu); - else - nodeid2 = 0; - for k = 1:ndisc_nodes, - if strcmp(levels{k,1},labels{nodeid}), - nodeid2 = k; - break - end - end - line = 'Intervention on this node assigned the following state:\n'; - fprintf(fileID,line); - state_ev = uint16(ev_dat(nodeid)); -% fprintf(fileID,'%i\n\n',levels{nodeid2,state_ev+1}); - fprintf(fileID,'%s\n\n',levels{nodeid2,state_ev+1}); - end - end - end -end - - - -fclose(fileID); - -end - diff --git a/sourcecodes/parameter_learning/getParams.m b/sourcecodes/parameter_learning/getParams.m deleted file mode 100644 index 31f84ffb..00000000 --- a/sourcecodes/parameter_learning/getParams.m +++ /dev/null @@ -1,22 +0,0 @@ -function [ bnet ] = getParams( bnet, cases ) -%getParams Code to initialize CPT and do parameter learning. -%This will be very basic for now. I can add more options later. - -dnodes = bnet.dnodes; -cnodes = bnet.cnodes; -nnodes = size(dnodes,2)+size(cnodes,2); - -%make dnodes tabular_CPT -for i = 1:size(dnodes,2) - bnet.CPD{dnodes(i)} = tabular_CPD(bnet,dnodes(i)); -end - -for i = 1:size(cnodes,2) - bnet.CPD{cnodes(i)} = gaussian_CPD(bnet,cnodes(i)); -end - -bnet = learn_params(bnet,cases); - - -end - diff --git a/sourcecodes/parameter_learning/kfoldCrossValid.m b/sourcecodes/parameter_learning/kfoldCrossValid.m index 6306c511..02f34866 100644 --- a/sourcecodes/parameter_learning/kfoldCrossValid.m +++ b/sourcecodes/parameter_learning/kfoldCrossValid.m @@ -149,6 +149,9 @@ for i = 1:ncases fprintf(fileID,'%6.4f\t%6.4f\n',kfoldPredictions(i,:)); end +fflush(fileID); +fclose(fileID); + end @@ -283,6 +286,9 @@ for i = 1:ncases fprintf(fileID,'%6.4f\n',kfoldPredictions(i,end)); end +fflush(fileID); +fclose(fileID); + end diff --git a/sourcecodes/parameter_learning/looCrossValid.m b/sourcecodes/parameter_learning/looCrossValid.m index 67b04409..840e486e 100644 --- a/sourcecodes/parameter_learning/looCrossValid.m +++ b/sourcecodes/parameter_learning/looCrossValid.m @@ -126,6 +126,10 @@ for i = 1:ncases fprintf(fileID,'%6.4f\t%6.4f\n',loopredictions(i,:)); end +fflush(fileID); +fclose(fileID); + + end @@ -247,4 +251,7 @@ for i = 1:ncases fprintf(fileID,'%6.4f\n',loopredictions(i,end)); end +fflush(fileID); +fclose(fileID); + end diff --git a/sourcecodes/parameter_learning/modifyEdges.m b/sourcecodes/parameter_learning/modifyEdges.m new file mode 100644 index 00000000..2b336710 --- /dev/null +++ b/sourcecodes/parameter_learning/modifyEdges.m @@ -0,0 +1,133 @@ +function [ ] = modifyEdges( pre_old, pre_new ) + % This function will allow users to add or delete edges from the network. + % + % Input: + % 1) pre_oldstructure_input.txt + % Original structure file. + % 2) pre_olddel_edge.txt + % A list of the edges that should be deleted. + % 3) pre_oldadd_edge.txt + % A list of the edges that should be added. + % + % Output: + % pre_newstructure_input.txt-- structure file with edges added/deleted. + % + + +% open file for input, include error handling +dfile=strcat(pre_old,'structure_input.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")); +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the edges +edges = cell(nnodes,nnodes); +for i = 1:nnodes + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + edges{i,j} = next; + end +end + + +% open file with edges to be deleted +dedgefile=strcat(pre_old,'del_edge.txt'); +fin2 = fopen(dedgefile,'r'); +ndel=fskipl(fin2,Inf) - 1; +frewind(fin2); + +delfrom = cell(1,ndel); +delto = cell(1,ndel); + +buffer = fgetl(fin2); %get header line +for j=1:ndel + buffer = fgetl(fin2); %get line with actual variables + [next,buffer] = strtok(buffer); + delfrom{j} = next; + [next,buffer] = strtok(buffer); + delto{j} = next; +end + +%get index of edges to delete +idel_from = []; +idel_to = []; +for j = 1:ndel + for k = 1:nnodes + if strcmp(labels{k},delfrom{j}) + idel_from = [idel_from;k]; + endif + if strcmp(labels{k},delto{j}) + idel_to = [idel_to;k]; + endif + end +end + +for i = 1:ndel + edges(idel_from(i),idel_to(i)) = "0"; +end + +fclose(fin2); + +% open file with edges to be added +aedgefile=strcat(pre_old,'add_edge.txt'); +fin3 = fopen(aedgefile,'r'); +nadd=fskipl(fin3,Inf) - 1; +frewind(fin3); + +addfrom = cell(1,nadd); +addto = cell(1,nadd); + +buffer = fgetl(fin3); %get header line +for j=1:nadd + buffer = fgetl(fin3); %get line with actual variables + [next,buffer] = strtok(buffer); + addfrom{j} = next; + [next,buffer] = strtok(buffer); + addto{j} = next; +end + +%get index of edges to added +iadd_from = []; +iadd_to = []; +for j = 1:nadd + for k = 1:nnodes + if strcmp(labels{k},addfrom{j}) + iadd_from = [iadd_from;k]; + endif + if strcmp(labels{k},addto{j}) + iadd_to = [iadd_to;k]; + endif + end +end + +for i = 1:nadd + edges(iadd_from(i),iadd_to(i)) = "1"; +end + + + + + +outfile = strcat(pre_new,'structure_input.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +for i = 1:nnodes + fprintf(fout,'%s\t',edges{i,1:end-1}); + fprintf(fout,'%s\n',edges{i,end}); +end +fclose(fout); + +end diff --git a/sourcecodes/parameter_learning/prepareInput.m b/sourcecodes/parameter_learning/prepareInput.m index 5268f872..3147d54e 100644 --- a/sourcecodes/parameter_learning/prepareInput.m +++ b/sourcecodes/parameter_learning/prepareInput.m @@ -1,4 +1,5 @@ function [ ] = prepareInput( pre ) + % Jan. 2019: Modifying to allow for missing data. % % This function takes files that are uploaded to BNW and creates output % files that can be used for structure and parameter learning. @@ -74,6 +75,11 @@ for i = 1:ncases end end +% Remove all rows that have missing data from data file +remove_count = sum(any(strcmp(data,"NA"),2)); +data(any(strcmp(data,"NA"),2),:)=[]; +ncases = ncases - remove_count; + % Determine whether or not the nodes are continuous or discrete. % First, treat them as all discrete and get the states and number of stats(levels). levels = cell(1,nnodes); @@ -242,6 +248,7 @@ dout = fopen(descfile,'w'); fprintf(dout,['As loaded, the input file had the following properties:\n\n']); dout = fopen(descfile,'a'); fprintf(dout,'There are %i variables and %i cases(rows).\n',size(labels,2),ncases); +fprintf(dout,'%i cases(rows) have been removed because they contained NA (missing data).\n',remove_count); fprintf(dout,'The variable names are:\n'); fprintf(dout,'%s\t',labels{1:end-1}); fprintf(dout,'%s\n\n',labels{end}); diff --git a/sourcecodes/parameter_learning/removeNodes.m b/sourcecodes/parameter_learning/removeNodes.m new file mode 100644 index 00000000..8740d999 --- /dev/null +++ b/sourcecodes/parameter_learning/removeNodes.m @@ -0,0 +1,94 @@ +function [ ] = removeNodes( pre_old, pre_new ) + % This function will allow users to delete variables from uploaded input file. + % For example, if an input file contains 20 variables, but the user is + % only interested in using 10 of these variables in a particular model, + % they can use this function to delete the variable. + % + % + % Input: + % 1) pre_oldcontinuous_input_orig.txt + % This is the original input file that is uploaded to BNW. + % It is directly written out by the BNW php code with no modification. + % The file format is a header line containing the variable names + % followed by the data, with each case in a row. + % 2) pre_olddel_var.txt + % This is a list of the names of the variables that should be delete. + % + % Output: + % pre_newcontinuous_input_orig.txt-- input file with variables deleted. + % + + +% open file for input, include error handling +dfile=strcat(pre_old,'continuous_input_orig.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Get the number of cases (the number of rows in the file excluding the header) +ncases = fskipl(fin,Inf) - 1; + +frewind(fin); + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")) + 1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data +data = cell(ncases,nnodes); +for i = 1:ncases + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + data{i,j} = next; + end +end + + +% open file for input, include error handling +dvarfile=strcat(pre_old,'del_var.txt'); +fin2 = fopen(dvarfile,'r'); +if fin2 < 0 + dellabels = {}; + ndel = 0; +else + buffer = fgetl(fin2); %get header line as a string + ndel = numel(strfind(buffer," ")) + 1; + dellabels = cell(1,ndel); + for j=1:ndel + [next,buffer] = strtok(buffer); + dellabels{j} = next; + end +end + +%get index of variables to delete +delindex = []; +for j=1:nnodes + for k = 1:ndel + if strcmp(labels{j},dellabels{k}) + delindex = [delindex;j]; + endif + end +end + +labels(:,[delindex])=[]; +data(:,[delindex])=[]; + +outfile = strcat(pre_new,'continuous_input_orig.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +for i = 1:ncases + fprintf(fout,'%s\t',data{i,1:end-1}); + fprintf(fout,'%s\n',data{i,end}); +end +fclose(fout); + +end diff --git a/sourcecodes/parameter_learning/testSetPredictions.m b/sourcecodes/parameter_learning/testSetPredictions.m index 0fff197d..23a50868 100644 --- a/sourcecodes/parameter_learning/testSetPredictions.m +++ b/sourcecodes/parameter_learning/testSetPredictions.m @@ -273,6 +273,8 @@ for i = 1:ntestcases fprintf(fileID,'%6.4f\t%6.4f\n',predictions(i,:)); end +fflush(fileID); +fclose(fileID); end @@ -353,7 +355,8 @@ for i = 1:ntestcases fprintf(fileID,'%6.4f\n',predictions(i,end)); end - +fflush(fileID); +fclose(fileID); |
