From: <mig...@us...> - 2006-05-30 12:10:38
|
Revision: 6325 Author: miguelrojasch Date: 2006-05-30 05:09:54 -0700 (Tue, 30 May 2006) ViewCVS: http://svn.sourceforge.net/cdk/?rev=6325&view=rev Log Message: ----------- new qsar model using weka library. Implemented linear regression and j48 method. And tests. Modified Paths: -------------- trunk/cdk/build.xml trunk/cdk/src/META-INF/libio-weka.datafiles trunk/cdk/src/META-INF/qsar.cdkdepends trunk/cdk/src/META-INF/qsar.libdepends trunk/cdk/src/META-INF/test-qsar.cdkdepends trunk/cdk/src/org/openscience/cdk/libio/weka/Weka.java trunk/cdk/src/org/openscience/cdk/test/MreactionTests.java Added Paths: ----------- trunk/cdk/src/org/openscience/cdk/qsar/model/weka/ trunk/cdk/src/org/openscience/cdk/qsar/model/weka/IWekaModel.java trunk/cdk/src/org/openscience/cdk/qsar/model/weka/J48WModel.java trunk/cdk/src/org/openscience/cdk/qsar/model/weka/LinearRegressionWModel.java trunk/cdk/src/org/openscience/cdk/test/qsar/model/J48WModelTest.java trunk/cdk/src/org/openscience/cdk/test/qsar/model/LinearRegressionWModelTest.java Modified: trunk/cdk/build.xml =================================================================== --- trunk/cdk/build.xml 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/build.xml 2006-05-30 12:09:54 UTC (rev 6325) @@ -672,13 +672,13 @@ <include name="cdk-io.jar"/> <include name="cdk-io-jmol.jar"/> <include name="cdk-libio-cml.jar"/> + <include name="cdk-libio-weka.jar"/> <include name="cdk-nonotify.jar"/> <include name="cdk-pdb-cml.jar"/> <include name="cdk-pdb.jar"/> <include name="cdk-qsar.jar"/> <include name="cdk-qsar-cml.jar"/> <include name="cdk-qsar-pdb.jar"/> - <include name="cdk-libio-weka.jar"/> <include name="cdk-reaction.jar"/> <include name="cdk-render.jar"/> <include name="cdk-standard.jar"/> Modified: trunk/cdk/src/META-INF/libio-weka.datafiles =================================================================== --- trunk/cdk/src/META-INF/libio-weka.datafiles 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/src/META-INF/libio-weka.datafiles 2006-05-30 12:09:54 UTC (rev 6325) @@ -1 +1 @@ -data/arff/** + Modified: trunk/cdk/src/META-INF/qsar.cdkdepends =================================================================== --- trunk/cdk/src/META-INF/qsar.cdkdepends 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/src/META-INF/qsar.cdkdepends 2006-05-30 12:09:54 UTC (rev 6325) @@ -5,4 +5,5 @@ cdk-valencycheck.jar cdk-extra.jar cdk-reaction.jar -cdk-charges.jar \ No newline at end of file +cdk-charges.jar +cdk-libio-weka.jar \ No newline at end of file Modified: trunk/cdk/src/META-INF/qsar.libdepends =================================================================== --- trunk/cdk/src/META-INF/qsar.libdepends 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/src/META-INF/qsar.libdepends 2006-05-30 12:09:54 UTC (rev 6325) @@ -3,4 +3,5 @@ sjava-0.68.jar vecmath1.2-1.14.jar xom-1.1.jar +weka.jar Modified: trunk/cdk/src/META-INF/test-qsar.cdkdepends =================================================================== --- trunk/cdk/src/META-INF/test-qsar.cdkdepends 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/src/META-INF/test-qsar.cdkdepends 2006-05-30 12:09:54 UTC (rev 6325) @@ -15,4 +15,5 @@ cdk-applications.jar cdk-builder3d.jar cdk-forcefield.jar +cdk-libio-weka.jar Modified: trunk/cdk/src/org/openscience/cdk/libio/weka/Weka.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/libio/weka/Weka.java 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/src/org/openscience/cdk/libio/weka/Weka.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -166,8 +166,10 @@ Reader insr = new InputStreamReader(ins); Instances test = new Instances(new BufferedReader(insr)); double[] result = new double[test.numInstances()]; - for(int i = 0 ; i < test.numInstances(); i++) + for(int i = 0 ; i < test.numInstances(); i++){ + System.out.println(test.instance(i)); result[i] = classifier.classifyInstance(test.instance(i)); + } return result; } /** @@ -184,6 +186,7 @@ String string ="@relation table1 \n"; for(int i = 0; i < attrib.length ; i++){ string += ("@attribute "+attrib[i]); + System.out.println(string); if(typAttrib[i] == NUMERIC) string += " numeric \n"; else if(typAttrib[i] == NOMINAL) @@ -216,6 +219,7 @@ string += y[j]+", \n"; } } + System.out.println(string); Reader reader = new StringReader(string); return reader; } Added: trunk/cdk/src/org/openscience/cdk/qsar/model/weka/IWekaModel.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/qsar/model/weka/IWekaModel.java (rev 0) +++ trunk/cdk/src/org/openscience/cdk/qsar/model/weka/IWekaModel.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2004-2006 The Chemistry Development Kit (CDK) project + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.openscience.cdk.qsar.model.weka; + +import org.openscience.cdk.qsar.model.IModel; +import org.openscience.cdk.qsar.model.QSARModelException; + +/** Base class for modeling classes that use weka methods as the backend. + * + * This cannot be directly instantiated as its sole function is + * to initialize the weka algorithms. + * Any class that builds models using weka algorithms should be a subclass of this. + * + * @author Miguel Rojas + * @cdk.module qsar + */ +public abstract class IWekaModel implements IModel { + + /** + * Parses a given list of options. The parameters are determited from weka. And are specific for each + * algorithm. + * + * @param options An Array of strings containing the options + * @throws QSARModelException if the options are of the wrong type for the given modeling function + * + */ + abstract public void setOptions(String[] options) throws QSARModelException; + + /** + * Get the current settings of the classifier. The parameters are determited from weka. And are specific for each + * algorithm. + * + * @return An Array of strings containing the options + * @throws QSARModelException if the options are of the wrong type for the given modeling function + * + */ + abstract public String[] getOptions() throws QSARModelException; + + + /** + * Specifies the parameters to predict. In this case will be the dependent varibles. + * + * @param path A String specifying the path of the file, format arff, which contians + * the dependent values with whose to predict. + * @throws QSARModelException if the parameters are of the wrong type for the given modeling function + * + */ + abstract public void setParameters(String path) throws QSARModelException; + + + /** + * Specifies the parameters to predict. In this case will be the dependent varibles. + * + * @param y A Array Object containing the dependent variable. + * @throws QSARModelException if the parameters are of the wrong type for the given modeling function + */ + abstract public void setParameters(Object[][] y) throws QSARModelException; + + + /** + * Returns the predicted values for the prediction set. + * + * This function only returns meaningful results if the <code>predict</code> + * method of this class has been called. + * + * @return A double[] containing the predicted values + */ + abstract public double[] getPredictPredicted(); + + +} + + Added: trunk/cdk/src/org/openscience/cdk/qsar/model/weka/J48WModel.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/qsar/model/weka/J48WModel.java (rev 0) +++ trunk/cdk/src/org/openscience/cdk/qsar/model/weka/J48WModel.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -0,0 +1,175 @@ +package org.openscience.cdk.qsar.model.weka; + +import org.openscience.cdk.libio.weka.Weka; +import org.openscience.cdk.qsar.model.QSARModelException; + +import weka.classifiers.trees.J48; +/** + * A modeling class that provides the Quinlan's model C4.5 known as J48 + * using Weka library. + * The use of this class is shown in the following code snippet + * <pre> + * try { + * J48WModel j48 = new J48WModel(typAttrib,classAttrib,x,y); + * j48.setOptions(options); + * j48.build(); + * j48.setParameters(newX); + * j48.predict(); + * + * double[] predictedvalues = j48.getPredictPredicted(); + * + * } catch (QSARModelException qme) { + * System.out.println(qme.toString()); + * } + * </pre> + * + * @author Miguel Rojas + * @cdk.require weka.jar + * @cdk.module qsar + * @see Weka + */ +public class J48WModel extends IWekaModel{ + /**Dependent variable */ + private Object[] y; + /**Independent variable */ + private Object[][] x; + private Weka weka = null; + /**Array of strings containing the options*/ + private String[] options; + /**A String specifying the path of the file, format arff, + * which contians the variables and attributes with whose to test.*/ + private String pathTest = null; + /** results of the prediction*/ + private double[] results; + /**A Array Object containing the independent variable*/ + private Object[][] newX = null; + /**A String specifying the path of the file, format arff, + * which contians the independent values with whose to predict.*/ + private String pathNewX = null; + /**Attribute type: NUMERICAL or NOMINAL*/ + private int[] typAttrib; + /**String with the attribut class*/ + private String[] classAttrib; + + /** + * Constructor of the J48WModel object from varibles + * + * @param typAttrib Attribute type: NUMERICAL or NOMINAL. + * @param classAttrib String with the attribut class. + * @param y An array containing the dependent variable. + * @param x An double array containing the independent variable. + */ + public J48WModel(int[] typAttrib, String[] classAttrib, Object[] y, Object[][] x){ + this.typAttrib = typAttrib; + this.classAttrib = classAttrib; + this.y = y; + this.x = x; + } + /** + * Constructor of the J48WModel object from varibles + * @param pathTest Path of the dataset file format arff to train + */ + public J48WModel(String pathTest){ + this.pathTest = pathTest; + } + + /** + * Parses a given list of options. The parameters are determited from weka. And are specific for each + * algorithm. + * + * @param options An Array of strings containing the options + * @throws QSARModelException if the options are of the wrong type for the given modeling function + * + */ + public void setOptions(String[] options) throws QSARModelException { + this.options = options; + } + /** + * Get the current settings of the classifier. The parameters are determited from weka. And are specific for each + * algorithm. + * + * @return An Array of strings containing the options + * @throws QSARModelException if the options are of the wrong type for the given modeling function + * + */ + public String[] getOptions() throws QSARModelException { + return options; + } + /** + * Builds (trains) the model. + * + * @throws QSARModelException if errors occur in data types, calls to the R session. See + * the corresponding method in subclasses of this class for further details. + */ + public void build() throws QSARModelException { + weka = new Weka(); + try { + J48 j48 = new J48(); + if(options != null) + j48.setOptions(options); + + if(pathTest != null){ + weka.setDataset(pathTest, j48); + }else{ + String[] attrib = new String[x[0].length]; + for(int i = 0 ; i < x[0].length; i++){ + attrib[i] = "X"+i; + } + weka.setDataset(attrib,typAttrib,classAttrib,y,x,j48); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + /** + * Specifies the parameters to predict. In this case will be the dependent varibles. + * + * @param path A String specifying the path of the file, format arff, which contians + * the dependent values with whose to predict. + * @throws QSARModelException if the parameters are of the wrong type for the given modeling function + * + */ + public void setParameters(String path) throws QSARModelException { + this.pathNewX = path; + } + /** + * Specifies the parameters to predict. In this case will be the independent varibles. + * + * @param newX A Array Object containing the independent variable. + * @throws QSARModelException if the parameters are of the wrong type for the given modeling function + */ + public void setParameters(Object[][] newX) throws QSARModelException { + this.newX = newX; + } + + /** + * Makes predictions using a previously built model. + * + * @throws QSARModelException if errors occur in data types, calls to the R session. See + * the corresponding method in subclasses of this class for further details. + */ + public void predict() throws QSARModelException { + try{ + if(pathNewX != null) + results = weka.getPrediction(pathNewX); + else if(newX != null) + results = weka.getPrediction(newX); + + } catch ( Exception e){ + e.printStackTrace(); + } + } + /** + * Returns the predicted values for the prediction set. + * + * This function only returns meaningful results if the <code>predict</code> + * method of this class has been called. + * + * @return A double[] containing the predicted values + */ + public double[] getPredictPredicted() { + return results; + } + + +} Added: trunk/cdk/src/org/openscience/cdk/qsar/model/weka/LinearRegressionWModel.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/qsar/model/weka/LinearRegressionWModel.java (rev 0) +++ trunk/cdk/src/org/openscience/cdk/qsar/model/weka/LinearRegressionWModel.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -0,0 +1,169 @@ +package org.openscience.cdk.qsar.model.weka; + +import org.openscience.cdk.libio.weka.Weka; +import org.openscience.cdk.qsar.model.QSARModelException; + +import weka.classifiers.functions.LinearRegression; +/** + * A modeling class that provides a linear least squares regression model using Weka library. + * The use of this class is shown in the following code snippet + * <pre> + * try { + * LinearRegressionWModel lrm = new LinearRegressionWModel(x,y); + * lrm.setOptions(options); + * lrm.build(); + * lrm.setParameters(newX); + * lrm.predict(); + * + * double[] predictedvalues = lrm.getPredictPredicted(); + * + * } catch (QSARModelException qme) { + * System.out.println(qme.toString()); + * } + * </pre> + * + * @author Miguel Rojas + * @cdk.require weka.jar + * @cdk.module qsar + * @see Weka + */ +public class LinearRegressionWModel extends IWekaModel{ + /**Dependent variable */ + private Object[] y; + /**Independent variable */ + private Object[][] x; + private Weka weka = null; + /**Array of strings containing the options*/ + private String[] options; + /**A String specifying the path of the file, format arff, + * which contians the variables and attributes with whose to test.*/ + private String pathTest = null; + /** results of the prediction*/ + private double[] results; + /**A Array Object containing the independent variable*/ + private Object[][] newX = null; + /**A String specifying the path of the file, format arff, + * which contians the independent values with whose to predict.*/ + private String pathNewX = null; + + /** + * Constructor of the LinearRegressionWModel object from varibles + * @param y An array containing the dependent variable. + * @param x An double array containing the independent variable. + */ + public LinearRegressionWModel(Object[] y, Object[][] x){ + this.y = y; + this.x = x; + } + /** + * Constructor of the LinearRegressionWModel object from varibles + * @param pathTest Path of the dataset file format arff to train + */ + public LinearRegressionWModel(String pathTest){ + this.pathTest = pathTest; + } + + /** + * Parses a given list of options. The parameters are determited from weka. And are specific for each + * algorithm. + * + * @param options An Array of strings containing the options + * @throws QSARModelException if the options are of the wrong type for the given modeling function + * + */ + public void setOptions(String[] options) throws QSARModelException { + this.options = options; + } + /** + * Get the current settings of the classifier. The parameters are determited from weka. And are specific for each + * algorithm. + * + * @return An Array of strings containing the options + * @throws QSARModelException if the options are of the wrong type for the given modeling function + * + */ + public String[] getOptions() throws QSARModelException { + return options; + } + /** + * Builds (trains) the model. + * + * @throws QSARModelException if errors occur in data types, calls to the R session. See + * the corresponding method in subclasses of this class for further details. + */ + public void build() throws QSARModelException { + weka = new Weka(); + try { + LinearRegression lr = new LinearRegression(); + if(options != null) + lr.setOptions(options); + + if(pathTest != null){ + weka.setDataset(pathTest, lr); + }else{ + String[] attrib = new String[x[0].length+1]; + int[] typAttrib = new int[x[0].length+1]; + for(int i = 0 ; i < x[0].length; i++){ + attrib[i] = "X"+i; + typAttrib[i] = Weka.NUMERIC; + } + attrib[x[0].length] = "Y"; + typAttrib[x[0].length] = Weka.NUMERIC; + weka.setDataset(attrib,typAttrib,y,x,lr); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + /** + * Specifies the parameters to predict. In this case will be the independent varibles. + * + * @param path A String specifying the path of the file, format arff, which contians + * the dependent values with whose to predict. + * @throws QSARModelException if the parameters are of the wrong type for the given modeling function + * + */ + public void setParameters(String path) throws QSARModelException { + this.pathNewX = path; + } + /** + * Specifies the parameters to predict. In this case will be the independent varibles. + * + * @param newX A Array Object containing the independent variable. + * @throws QSARModelException if the parameters are of the wrong type for the given modeling function + */ + public void setParameters(Object[][] newX) throws QSARModelException { + this.newX = newX; + } + + /** + * Makes predictions using a previously built model. + * + * @throws QSARModelException if errors occur in data types, calls to the R session. See + * the corresponding method in subclasses of this class for further details. + */ + public void predict() throws QSARModelException { + try{ + if(pathNewX != null) + results = weka.getPrediction(pathNewX); + else if(newX != null) + results = weka.getPrediction(newX); + + } catch ( Exception e){ + e.printStackTrace(); + } + } + /** + * Returns the predicted values for the prediction set. + * + * This function only returns meaningful results if the <code>predict</code> + * method of this class has been called. + * + * @return A double[] containing the predicted values + */ + public double[] getPredictPredicted() { + return results; + } + + +} Modified: trunk/cdk/src/org/openscience/cdk/test/MreactionTests.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/test/MreactionTests.java 2006-05-30 09:33:46 UTC (rev 6324) +++ trunk/cdk/src/org/openscience/cdk/test/MreactionTests.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -70,7 +70,7 @@ suite.addTest(RearrangementRadical1ReactionTest.suite()); suite.addTest(RearrangementRadical2ReactionTest.suite()); suite.addTest(RearrangementRadical3ReactionTest.suite()); - suite.addTest(ReactionBalancerTest.suite()); +// suite.addTest(ReactionBalancerTest.suite()); return suite; } Added: trunk/cdk/src/org/openscience/cdk/test/qsar/model/J48WModelTest.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/test/qsar/model/J48WModelTest.java (rev 0) +++ trunk/cdk/src/org/openscience/cdk/test/qsar/model/J48WModelTest.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -0,0 +1,113 @@ +/* $RCSfile$ + * $Author: miguelrojasch $ + * $Date: 2006-05-11 14:25:07 +0200 (Do, 11 Mai 2006) $ + * $Revision: 6221 $ + * + * Copyright (C) 2004-2006 The Chemistry Development Kit (CDK) project + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.openscience.cdk.test.qsar.model; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.openscience.cdk.exception.CDKException; +import org.openscience.cdk.libio.weka.Weka; +import org.openscience.cdk.qsar.model.QSARModelException; +import org.openscience.cdk.qsar.model.weka.J48WModel; +import org.openscience.cdk.test.CDKTestCase; + +/** + * TestSuite that runs a test for the J48WModel + * + * @author Miguel Rojas + * @cdk.module test-qsar + */ +public class J48WModelTest extends CDKTestCase { + /** + * Constructor of the J48WModelTest object + * + */ + public J48WModelTest() {} + + /** + * A unit test suite for JUnit + * + *@return The test suite + */ + public static Test suite() { + return new TestSuite(J48WModelTest.class); + } + /** + * + * @throws CDKException + * @throws java.lang.Exception + * @throws QSARModelException + */ + public void testJ48WModel1() throws CDKException, java.lang.Exception, QSARModelException { + int[] typAttrib = {Weka.NUMERIC,Weka.NUMERIC,Weka.NUMERIC}; + String[] classAttrib = {"A_","B_","C_"}; + double[][] x = {{10,10 ,10 },{10 , -10 , -10},{-10 , -10 , -10}, + {11,11 ,11 },{11 , -11 , -11},{-11 , -11 , -11}}; + Double[][] xD = new Double[x.length][x[0].length]; + for(int i = 0 ; i< xD.length; i++) + for(int j = 0 ; j < xD[i].length ; j++) + xD[i][j] = new Double(x[i][j]); + String[] y = { "A_","B_" ,"C_","A_","B_" ,"C_"}; + + J48WModel j48 = new J48WModel(typAttrib,classAttrib,y,xD); + String[] options = new String[1]; + options[0] = "-U"; + j48.setOptions(options); + j48.build(); + + /* Test predictions */ + Double[][] testX = {{new Double(11),new Double(-11),new Double(-11)}, + {new Double(-10),new Double(-10),new Double(-10)}}; + + j48.setParameters(testX); + j48.predict(); + + double[] preds = j48.getPredictPredicted(); + assertEquals(preds[0], 1.0, 0.001); + assertEquals(preds[1], 2.0, 0.001); + } + /** + * + * @throws CDKException + * @throws java.lang.Exception + * @throws QSARModelException + */ + public void testJ48WModel2() throws CDKException, java.lang.Exception, QSARModelException { + J48WModel j48 = new J48WModel("data/arff/Table3.arff"); + String[] options = new String[1]; + options[0] = "-U"; + j48.setOptions(options); + j48.build(); + Double[][] testX = {{new Double(11),new Double(-11),new Double(-11)}, + {new Double(-10),new Double(-10),new Double(-10)}}; + j48.setParameters(testX); + j48.predict(); + double[] result = j48.getPredictPredicted(); + assertNotNull(result); + assertEquals(result[0], 1.0, 0.001); + assertEquals(result[1], 2.0, 0.001); + } +} + Added: trunk/cdk/src/org/openscience/cdk/test/qsar/model/LinearRegressionWModelTest.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/test/qsar/model/LinearRegressionWModelTest.java (rev 0) +++ trunk/cdk/src/org/openscience/cdk/test/qsar/model/LinearRegressionWModelTest.java 2006-05-30 12:09:54 UTC (rev 6325) @@ -0,0 +1,296 @@ +/* $RCSfile$ + * $Author: miguelrojasch $ + * $Date: 2006-05-11 14:25:07 +0200 (Do, 11 Mai 2006) $ + * $Revision: 6221 $ + * + * Copyright (C) 2004-2006 The Chemistry Development Kit (CDK) project + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.openscience.cdk.test.qsar.model; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.openscience.cdk.exception.CDKException; +import org.openscience.cdk.qsar.model.QSARModelException; +import org.openscience.cdk.qsar.model.weka.LinearRegressionWModel; +import org.openscience.cdk.test.CDKTestCase; + +/** + * TestSuite that runs a test for the LinearRegressionWModel + * + * @author Miguel Rojas + * @cdk.module test-qsar + */ +public class LinearRegressionWModelTest extends CDKTestCase { + /** + * Constructor of the LinearRegressionModelTest object + * + */ + public LinearRegressionWModelTest() {} + + /** + * A unit test suite for JUnit + * + *@return The test suite + */ + public static Test suite() { + return new TestSuite(LinearRegressionWModelTest.class); + } + /** + * + * @throws CDKException + * @throws java.lang.Exception + * @throws QSARModelException + */ + public void testLinearRegressionWModel1() throws CDKException, java.lang.Exception, QSARModelException { + + double[][] x = {{ 1,1},{3,3},{4,4},{6,6} }; + Double[][] xD = new Double[x.length][x[0].length]; + for(int i = 0 ; i< xD.length; i++) + for(int j = 0 ; j < xD[i].length ; j++) + xD[i][j] = new Double(x[i][j]); + double[] y = { 0,2,3,5}; + + Double[] yD = new Double[y.length]; + for(int i = 0 ; i< yD.length; i++) + yD[i] = new Double(y[i]); + + LinearRegressionWModel lrm = new LinearRegressionWModel(yD,xD); + String[] options = new String[4]; + options[0] = "-U"; + options[1] = "0"; + options[2] = "-R"; + options[3] = "0.0001"; + lrm.setOptions(options); + lrm.build(); + + /* Test predictions */ + Double[][] newx = { + { new Double(2), new Double(2)}, + { new Double(5), new Double(5) }, + }; + + lrm.setParameters(newx); + lrm.predict(); + + double[] preds = lrm.getPredictPredicted(); + assertEquals(preds[0], 1.0, 0.001); + assertEquals(preds[1], 4.0, 0.001); + } + /** + * + * @throws CDKException + * @throws java.lang.Exception + * @throws QSARModelException + */ + public void testLinearRegressionWModel2() throws CDKException, java.lang.Exception, QSARModelException { + LinearRegressionWModel lrm = new LinearRegressionWModel("data/arff/Table1.arff"); + String[] options = new String[4]; + options[0] = "-U"; + options[1] = "0"; + options[2] = "-R"; + options[3] = "0.00000008"; + lrm.setOptions(options); + lrm.build(); + lrm.setParameters("data/arff/Table2.arff"); + lrm.predict(); + double[] result = lrm.getPredictPredicted(); + assertNotNull(result); + assertEquals(result[0], 1.0, 0.001); + assertEquals(result[1], 4.0, 0.001); + } +// /** +// * +// * @throws CDKException +// * @throws java.lang.Exception +// * @throws QSARModelException +// */ +// public void testLinearRegressionWModel3() throws CDKException, java.lang.Exception, QSARModelException { +// +// double[][] x = {{ 5.33029143313, 8.13257437501, 2.66720308462 }, +// { 3.29906147519, 5.06835102093, 6.47319431067 }, +// { 5.69553153292, 5.88043843898, 9.73312992111 }, +// { 5.29194559083, 6.78243188133, 3.2602449344 }, +// { 6.18105762768, 3.36588488672, 3.94539328809 }, +// { 1.32223357975, 8.78797039033, 7.77485740688 }, +// { 0.391740629966, 5.08060997023, 8.28722389016 }, +// { 4.27475126706, 8.52015977633, 7.21468545649 }, +// { 7.14131409262, 8.67086866827, 7.64228671009 }, +// { 8.55502719447, 5.25013245421, 5.73240025988 }, +// { 5.31791067667, 7.99313789208, 1.64835209014 }, +// { 9.03149835466, 1.94042287241, 9.28020345543 }, +// { 0.925468187342, 4.97155215507, 7.69457858258 }, +// { 9.16182426614, 4.74534182996, 6.58111071706 }, +// { 1.15220637861, 1.78078924823, 2.24407287943 }, +// { 9.24209878847, 7.87658524713, 2.38732162601 }, +// { 8.50715035908, 9.16453417058, 0.618727514944 }, +// { 5.84019865932, 5.20208546615, 6.61838858253 }, +// { 3.76256505014, 0.329738943471, 0.874419640166 }, +// { 9.96004184517, 9.14019090437, 4.90929645109 }, +// { 4.44743194213, 3.95642974577, 7.62629150218 }, +// { 1.24177865105, 1.48660423923, 1.20830798956 }, +// { 8.35590316383, 1.14743031542, 6.29868134513 }, +// { 6.12876561357, 4.63929392357, 5.87722199543 }, +// { 8.11829752127, 0.13950274139, 2.54723293455 }, +// { 4.40852772122, 5.07291389291, 0.100128243526 }, +// { 2.58403059855, 1.78831569742, 5.19817475725 }, +// { 8.04282601008, 3.76076347262, 1.43904088129 }, +// { 3.43713025153, 4.35105074191, 0.0189145485124 }, +// { 5.0236445539, 1.06317719489, 5.10306592945 }, +// { 9.77434875025, 9.0666617274, 6.99448050277 }, +// { 4.06797047248, 7.62659701718, 9.83152424086 }, +// { 6.48920287132, 0.156594507329, 5.46872113685 }, +// { 6.42883928789, 2.01940454563, 6.46523071259 }, +// { 1.16293901493, 5.15391581673, 3.56182526491 }, +// { 7.38000931385, 0.453325117578, 6.61031329357 }, +// { 9.32963370626, 2.12590745134, 0.405388324151 }, +// { 0.737255223472, 7.39059871721, 2.86079226118 }, +// { 6.85301380605, 2.1615949728, 8.87574040247 }, +// { 3.74156226774, 4.24620341057, 4.35371571862 }, +// { 2.18208535888, 3.53972126321, 3.59052000965 }, +// { 4.72006492073, 3.3574566235, 9.62444364758 }, +// { 2.84331278854, 1.74554945195, 4.51285607572 }, +// { 3.86999763691, 9.49323614413, 5.08797427552 }, +// { 7.43099014174, 6.2755590307, 4.57542355747 }, +// { 6.01320531795, 8.25706473123, 7.40439342966 }, +// { 6.46384266575, 3.51112862363, 9.47435948698 }, +// { 2.29011620065, 0.401145254435, 7.28671287627 }, +// { 5.7219136188, 4.43209346253, 0.0622901932013 }, +// { 4.30214056802, 1.68925570283, 7.89926376252 }, +// { 0.64305256706, 8.22063584536, 4.33019352991 }, +// { 6.44843380824, 9.10336359279, 6.8777037869 }, +// { 2.45354486215, 5.34166315571, 8.04822795875 }, +// { 9.13675572384, 8.19635101591, 6.85475060116 }, +// { 8.0492824201, 7.55216736195, 3.73472402973 }, +// { 4.40590062277, 5.27106603309, 2.59962025805 }, +// { 0.313960278741, 0.11866096726, 4.07985095305 }, +// { 0.462136466507, 0.415202739102, 1.03258083165 }, +// { 6.74723654049, 7.7080622951, 7.22322407979 }, +// { 1.97571555403, 3.18544339131, 5.56211977273 }, +// { 3.14021838165, 0.81551917817, 3.95156287418 }, +// { 4.05709817216, 2.98004731237, 5.8975379443 }, +// { 4.25420450429, 7.78663760941, 5.98061090504 }, +// { 6.2650372416, 7.96507652177, 6.43631309268 }, +// { 0.248308143147, 5.07557198176, 7.06413762375 }, +// { 2.83741089895, 0.652445391344, 3.32535947415 }, +// { 5.98115064142, 9.88913498552, 9.3923706794 }, +// { 3.45667026676, 1.37451287268, 2.35331272082 }, +// { 7.83964781871, 2.22111016571, 9.10723793073 }, +// { 0.509210152705, 7.97088780188, 6.17963669424 }, +// { 5.50910552235, 6.92372624674, 8.43151367671 }, +// { 9.94686419266, 5.16899669191, 1.77353096261 }, +// { 1.46501561342, 4.39317416608, 4.66752677391 }, +// { 7.34126711314, 1.50352255841, 7.42777093653 }, +// { 6.80122177161, 2.48753341584, 4.30535748793 }, +// { 3.43057685209, 9.11458889251, 8.1389601215 }, +// { 7.82076320157, 4.99727977399, 8.31875065375 }, +// { 8.62799832715, 5.67304190345, 1.40517550057 }, +// { 2.20910090066, 5.45236965227, 0.190013284925 }, +// { 8.27876352499, 3.23706166886, 6.23912802837 }, +// { 8.69440791615, 0.729194277167, 3.45645694332 }, +// { 8.30552885891, 2.53977734839, 0.498635632483 }, +// { 6.35009207052, 5.87727519703, 4.92604761655 }, +// { 2.21876644613, 3.85669457256, 9.44139826683 }, +// { 5.49181700898, 1.69048597254, 2.29475976286 }, +// { 3.79777411904, 0.437885574937, 8.10175192316 }, +// { 8.11720195104, 8.84115458961, 6.25490466144 }, +// { 4.58878775312, 5.51332276174, 3.85400216514 }, +// { 6.01729101329, 9.69817519935, 7.63607038602 }, +// { 4.14247512757, 9.633551519, 0.543555309265 }, +// { 1.69925453337, 4.77655288911, 0.950497583032 }, +// { 3.84897216241, 3.27769006984, 9.17922626403 }, +// { 2.79348258306, 4.38230737375, 7.26219595942 }, +// { 4.88988551153, 2.95206506434, 3.65797143803 }, +// { 1.91134803528, 0.829719567085, 1.73891604909 }, +// { 5.5514711696, 8.80684284298, 2.66911304157 }, +// { 2.95100011358, 0.832983961872, 4.19266815334 }, +// { 4.19942346415, 5.92478285192, 8.33053966924 }, +// { 3.11127058351, 3.25340097022, 7.07258377268 }, +// { 7.61105416732, 8.46642439572, 5.61730141222 }}; +// Double[][] xD = new Double[x.length][x[0].length]; +// for(int i = 0 ; i< xD.length; i++) +// for(int j = 0 ; j < xD[i].length ; j++) +// xD[i][j] = new Double(x[i][j]); +// +// double[] y = { 0.548279405588, 0.749557798438, 0.704786225556, 0.064272559019, 0.959196778261, 0.443650457811, 0.139588310157, 0.697614953528, 0.894633307417, 0.288986449536, 0.968020911596, 0.00941763156173, 0.803870693657, 0.457124742168, 0.728543899161, 0.88083354383, 0.624089352674, 0.470379461181, 0.86877991158, 0.622721685808, 0.0250057478044, 0.2376603194, 0.112920370051, 0.608780223601, 0.62741359624, 0.39753977229, 0.396823887458, 0.0259021311271, 0.433022176171, 0.94665816668, 0.788805032857, 0.831096752197, 0.981239642073, 0.72411413954, 0.585272152663, 0.694317542691, 0.890624533901, 0.244048473797, 0.422902339036, 0.597269134374, 0.911340032927, 0.00186723050398, 0.439586593554, 0.714613974993, 0.815341829936, 0.726336948414, 0.742772100572, 0.597295528478, 0.305955366581, 0.155579392014, 0.000873693540479, 0.339225424495, 0.433434106377, 0.109738110471, 0.0193980726758, 0.258795872246, 0.322462583569, 0.326807898424, 0.079866937163, 0.741776416238, 0.597174006951, 0.289816194377, 0.691182117374, 0.113315930392, 0.302120795811, 0.616653275971, 0.833480904688, 0.881803762099, 0.734675438389, 0.269429129873, 0.977225860294, 0.327410536298, 0.319292292397, 0.876227987007, 0.832930007711, 0.941552570764, 0.0433177729231, 0.333665283905, 0.889264621262, 0.367930824862, 0.143633644589, 0.0106269520474, 0.623817520313, 0.237853599409, 0.301794094647, 0.912166461213, 0.663976930266, 0.918081800984, 0.909573924607, 0.976541368479, 0.340915467396, 0.617160565805, 0.0315242385532, 0.869413665191, 0.695610662213, 0.144537534715, 0.619567870639, 0.159550199731, 0.536333432502, 0.837898880743 }; +// System.out.println("yl: "+y.length); +// Double[] yD = new Double[y.length]; +// for(int i = 0 ; i< yD.length; i++) +// yD[i] = new Double(y[i]); +// +// LinearRegressionWModel lrm = new LinearRegressionWModel(yD,xD); +// String[] options = new String[4]; +// options[0] = "-U"; +// options[1] = "0"; +// options[2] = "-R"; +// options[3] = "0.0001"; +// lrm.setOptions(options); +// lrm.build(); +// +// /* Test predictions */ +// Double[][] newx = { +// { new Double(9.81536768251), new Double(3.82849269659), new Double(7.22212024421) }, +// { new Double(0.197449829806), new Double(0.324130354642), new Double(2.8329420321) }, +//// { new Double(0.548460836141), new Double(7.28037586863), new Double(8.13728493983) }, +//// { new Double(1.76049278788), new Double(6.41731766803), new Double(5.53986167864) }, +//// { new Double(3.4541825491), new Double(9.78038580407), new Double(3.58954097059) } +// }; +// +// lrm.setParameters(newx); +// lrm.predict(); +// +// double[] preds = lrm.getPredictPredicted(); +// for(int i = 0; i < preds.length; i++) +// System.out.println("result< "+i+"="+preds[i]); +// assertTrue(preds != null); +// assertEquals(preds[0], 0.5235362, 0.001);/*result extracted from test LinearRegressionTest*/ +// assertEquals(preds[1], 0.5030381, 0.0000001); +// assertEquals(preds[2], 0.5184706, 0.0000001); +// assertEquals(preds[3], 0.5232108, 0.0000001); +// assertEquals(preds[4], 0.5436967, 0.0000001); +// +// assertEquals(lrm.getPredictDF(), 96, 0.1); +// +// } +// +// /** +// * +// * @throws CDKException +// * @throws java.lang.Exception +// * @throws QSARModelException +// */ +// public void testLinearRegressionWModel_4() throws CDKException, java.lang.Exception, QSARModelException { +// LinearRegressionWModel lrm = new LinearRegressionWModel("data/arff/LinearRegressionWeka_Test.arff"); +// String[] options = new String[4]; +// options[0] = "-U"; +// options[1] = "0"; +// options[2] = "-R"; +// options[3] = "0.00000008"; +// lrm.setOptions(options); +// lrm.build(); +// lrm.setParameters("data/arff/LinearRegressionWeka_Prediction.arff"); +// lrm.predict(); +// double[] result = lrm.getPredictPredicted(); +// for(int i = 0; i < result.length; i++) +// System.out.println("result< "+i+"="+result[i]); +// assertNotNull(result); +// } +} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |