001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * FromFile.java
019: * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
020: *
021: */
022: package weka.classifiers.bayes.net.search.fixed;
023:
024: import weka.classifiers.bayes.BayesNet;
025: import weka.classifiers.bayes.net.BIFReader;
026: import weka.classifiers.bayes.net.ParentSet;
027: import weka.classifiers.bayes.net.search.SearchAlgorithm;
028: import weka.core.Instances;
029: import weka.core.Option;
030: import weka.core.Utils;
031:
032: import java.util.Enumeration;
033: import java.util.Vector;
034:
035: /**
036: <!-- globalinfo-start -->
037: * The FromFile reads the structure of a Bayes net from a file in BIFF format.
038: * <p/>
039: <!-- globalinfo-end -->
040: *
041: <!-- options-start -->
042: * Valid options are: <p/>
043: *
044: * <pre> -B <BIF File>
045: * Name of file containing network structure in BIF format
046: * </pre>
047: *
048: <!-- options-end -->
049: *
050: * @author Remco Bouckaert
051: * @version $Revision: 1.7 $
052: */
053: public class FromFile extends SearchAlgorithm {
054:
055: /** for serialization */
056: static final long serialVersionUID = 7334358169507619525L;
057:
058: /** name of file to read structure from **/
059: String m_sBIFFile = "";
060:
061: /**
062: * Returns a string describing this object
063: * @return a description of the classifier suitable for
064: * displaying in the explorer/experimenter gui
065: */
066: public String globalInfo() {
067: return "The FromFile reads the structure of a Bayes net from a file "
068: + "in BIFF format.";
069: }
070:
071: /**
072: *
073: * @param bayesNet
074: * @param instances the instances to work with
075: * @throws Exception if attribute from BIF file could not be found
076: */
077: public void buildStructure(BayesNet bayesNet, Instances instances)
078: throws Exception {
079: // read network structure in BIF format
080: BIFReader bifReader = new BIFReader();
081: bifReader.processFile(m_sBIFFile);
082: // copy parent sets
083: for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) {
084: int iBIFAttribute = bifReader.getNode(bayesNet
085: .getNodeName(iAttribute));
086: ParentSet bifParentSet = bifReader
087: .getParentSet(iBIFAttribute);
088: for (int iBIFParent = 0; iBIFParent < bifParentSet
089: .getNrOfParents(); iBIFParent++) {
090: String sParent = bifReader.getNodeName(bifParentSet
091: .getParent(iBIFParent));
092: int iParent = 0;
093: while (iParent < instances.numAttributes()
094: && !bayesNet.getNodeName(iParent).equals(
095: sParent)) {
096: iParent++;
097: }
098: if (iParent >= instances.numAttributes()) {
099: throw new Exception("Could not find attribute "
100: + sParent + " from BIF file in data");
101: }
102: bayesNet.getParentSet(iAttribute).addParent(iParent,
103: instances);
104: }
105: }
106: } // buildStructure
107:
108: /**
109: * Set name of network in BIF file to read structure from
110: *
111: * @param sBIFFile the name of the BIF file
112: */
113: public void setBIFFile(String sBIFFile) {
114: m_sBIFFile = sBIFFile;
115: }
116:
117: /**
118: * Get name of network in BIF file to read structure from
119: * @return BIF file name
120: */
121: public String getBIFFile() {
122: return m_sBIFFile;
123: }
124:
125: /**
126: * Returns an enumeration describing the available options.
127: *
128: * @return an enumeration of all the available options.
129: */
130: public Enumeration listOptions() {
131: Vector newVector = new Vector();
132:
133: newVector
134: .addElement(new Option(
135: "\tName of file containing network structure in BIF format\n",
136: "B", 1, "-B <BIF File>"));
137:
138: Enumeration en = super .listOptions();
139: while (en.hasMoreElements())
140: newVector.addElement(en.nextElement());
141:
142: return newVector.elements();
143: }
144:
145: /**
146: * Parses a given list of options. <p/>
147: *
148: <!-- options-start -->
149: * Valid options are: <p/>
150: *
151: * <pre> -B <BIF File>
152: * Name of file containing network structure in BIF format
153: * </pre>
154: *
155: <!-- options-end -->
156: *
157: * @param options the list of options as an array of strings
158: * @throws Exception if an option is not supported
159: */
160: public void setOptions(String[] options) throws Exception {
161: setBIFFile(Utils.getOption('B', options));
162:
163: super .setOptions(options);
164: }
165:
166: /**
167: * Gets the current settings of the search algorithm.
168: *
169: * @return an array of strings suitable for passing to setOptions
170: */
171: public String[] getOptions() {
172: String[] super Options = super .getOptions();
173: String[] options = new String[2 + super Options.length];
174: int current = 0;
175:
176: options[current++] = "-B";
177: options[current++] = "" + getBIFFile();
178:
179: // insert options from parent class
180: for (int iOption = 0; iOption < super Options.length; iOption++) {
181: options[current++] = super Options[iOption];
182: }
183:
184: // Fill up rest with empty strings, not nulls!
185: while (current < options.length) {
186: options[current++] = "";
187: }
188: return options;
189: }
190:
191: } // class FromFile
|