diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/parameter_learning/readInputStructure.m | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/parameter_learning/readInputStructure.m')
| -rw-r--r-- | sourcecodes/parameter_learning/readInputStructure.m | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/sourcecodes/parameter_learning/readInputStructure.m b/sourcecodes/parameter_learning/readInputStructure.m new file mode 100644 index 00000000..6b3cbece --- /dev/null +++ b/sourcecodes/parameter_learning/readInputStructure.m @@ -0,0 +1,72 @@ +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 |
