From: Rajarshi G. <raj...@us...> - 2005-05-23 20:53:22
|
Update of /cvsroot/cdk/cdk/src/org/openscience/cdk/qsar/model/R In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18508/src/org/openscience/cdk/qsar/model/R Modified Files: CNNRegressionModel.java LinearRegressionModel.java PLSRegressionModel.java RModel.java Log Message: saveModel was converted to a static method of the RModel class, since it is not specific to any specific modeling class. setModelName() was made more robust so that if the object is not in the R session the program will not crash. loadModel() was converted to an abtsract method so that each modeling class must implement it - this makes implementing new modeling classes cleaner since we don't need to fiddle with RModel. This approach also makes checking that a correct model type is loaded by the given class, easier. Added a new test to test model saving, loading and renaming Index: CNNRegressionModel.java =================================================================== RCS file: /cvsroot/cdk/cdk/src/org/openscience/cdk/qsar/model/R/CNNRegressionModel.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- CNNRegressionModel.java 23 May 2005 04:31:27 -0000 1.12 +++ CNNRegressionModel.java 23 May 2005 20:53:08 -0000 1.13 @@ -411,6 +411,23 @@ } /** + * Loads a CNNRegresionModel object from disk in to the current session. + * + * + * @param filename The disk file containing the model + */ + public void loadModel(String fileName) throws QSARModelException { + // should probably check that the filename does exist + Object model = (Object)revaluator.call("loadModel", new Object[]{ (Object)fileName }); + String modelName = (String)revaluator.call("loadModel.getName", new Object[] { (Object)fileName }); + + if (model.getClass().getName().equals("org.openscience.cdk.qsar.model.R.CNNRegressionModelFit")) { + this.modelfit = (CNNRegressionModelFit)model; + this.setModelName(modelName); + } else throw new QSARModelException("The loaded model was not a CNNRegressionModel"); + } + + /** * Gets final value of the fitting criteria. * * This method only returns meaningful results if the <code>build</code> Index: LinearRegressionModel.java =================================================================== RCS file: /cvsroot/cdk/cdk/src/org/openscience/cdk/qsar/model/R/LinearRegressionModel.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- LinearRegressionModel.java 23 May 2005 04:31:27 -0000 1.9 +++ LinearRegressionModel.java 23 May 2005 20:53:08 -0000 1.10 @@ -361,6 +361,22 @@ } } + /** + * Loads an LinearRegressionModel object from disk in to the current session. + * + * @param filename The disk file containing the model + */ + public void loadModel(String fileName) throws QSARModelException { + // should probably check that the fileName does exist + Object model = (Object)revaluator.call("loadModel", new Object[]{ (Object)fileName }); + String modelName = (String)revaluator.call("loadModel.getName", new Object[] { (Object)fileName }); + + if (model.getClass().getName().equals("org.openscience.cdk.qsar.model.R.LinearRegressionModelFit")) { + this.modelfit =(LinearRegressionModelFit)model; + this.setModelName(modelName); + } else throw new QSARModelException("The loaded model was not a LinearRegressionModel"); + } + /* interface to fit object */ /** Index: PLSRegressionModel.java =================================================================== RCS file: /cvsroot/cdk/cdk/src/org/openscience/cdk/qsar/model/R/PLSRegressionModel.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- PLSRegressionModel.java 23 May 2005 04:31:27 -0000 1.5 +++ PLSRegressionModel.java 23 May 2005 20:53:08 -0000 1.6 @@ -283,6 +283,23 @@ } } + /** + * Loads a PLSRegressionModel object from disk in to the current session. + * + * @param filename The disk file containing the model + */ + public void loadModel(String fileName) throws QSARModelException { + // should probably check that the filename does exist + Object model = (Object)revaluator.call("loadModel", new Object[]{ (Object)fileName }); + String modelName = (String)revaluator.call("loadModel.getName", new Object[] { (Object)fileName }); + + if (model.getClass().getName().equals("org.openscience.cdk.qsar.model.R.PLSRegressionModelFit")) { + this.modelfit = (PLSRegressionModelFit)model; + this.setModelName(modelName); + } else throw new QSARModelException("The loaded model was not a PLSRegressionModel"); + } + + /** * Sets parameters required for building a PLS model or using one for prediction. Index: RModel.java =================================================================== RCS file: /cvsroot/cdk/cdk/src/org/openscience/cdk/qsar/model/R/RModel.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- RModel.java 23 May 2005 04:31:27 -0000 1.10 +++ RModel.java 23 May 2005 20:53:08 -0000 1.11 @@ -152,7 +152,7 @@ * @param modelname The name of the model as returned by \code{getModelName}. * @param filename The file to which the model should be saved */ - public void saveModel(String modelname, String filename) throws QSARModelException { + public static void saveModel(String modelname, String filename) throws QSARModelException { if (filename.equals("") || filename == null) { filename = modelname+".rda"; } @@ -166,17 +166,7 @@ } } - /** - * Loads an R model from disk in to the current session. - * - * @param filename The disk file containing the model - * @return A String containing the model name - */ - public String loadModel(String filename) { - // should probably check that the filename does exist - String modelName = (String)revaluator.call("loadModel", new Object[]{ (Object)filename }); - return(modelName); - } + /** * Get the name of the model. * @@ -199,26 +189,23 @@ * to disk (see {@link RModel.saveModel}) and then later loads it, the loaded * model may overwrite a model in that session. In this situation, this method * can be used to assign a name to the model. - *<p> - * TODO: Code needs to be included to check that the object referred to - * by modelName does exist in the R session, otherwise the reassignment - * will fail. * * @param modelName The name of the model * */ - public void setModelName(String modelName) { + public void setModelName(String newName) { + if (this.modelName != null && this.modelName.equals(newName)) return; String oldName = this.modelName; if (oldName != null) { - revaluator.voidEval(modelName + " <- " + oldName +";"); - revaluator.voidEval("rm("+oldName+");"); + revaluator.voidEval("if ('"+oldName+"' %in% ls()) {"+newName+"<-"+oldName+";rm("+oldName+")}"); } - this.modelName = modelName; + this.modelName = newName; } abstract public void build() throws QSARModelException; abstract public void predict() throws QSARModelException; abstract public void setParameters(String key, Object obj) throws QSARModelException; + abstract public void loadModel(String fileName) throws QSARModelException; } |