List of usage examples for weka.core.pmml PMMLFactory getPMMLModel
public static PMMLModel getPMMLModel(InputStream stream, Logger log) throws Exception
From source file:org.pentaho.di.scoring.WekaScoringData.java
License:Open Source License
/** * Loads a serialized model. Models can either be binary serialized Java * objects, objects deep-serialized to xml, or PMML. * * @param modelFile a <code>File</code> value * @return the model/*from w w w. j a va2 s . c o m*/ * @throws Exception if there is a problem laoding the model. */ public static WekaScoringModel loadSerializedModel(String modelFile, LogChannelInterface log, VariableSpace space) throws Exception { Object model = null; Instances header = null; int[] ignoredAttsForClustering = null; modelFile = space.environmentSubstitute(modelFile); FileObject modelF = KettleVFS.getFileObject(modelFile); if (!modelF.exists()) { throw new Exception(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoring.Error.NonExistentModelFile", space.environmentSubstitute(modelFile))); //$NON-NLS-1$ } InputStream is = KettleVFS.getInputStream(modelF); BufferedInputStream buff = new BufferedInputStream(is); if (modelFile.toLowerCase().endsWith(".xml")) { //$NON-NLS-1$ // assume it is PMML model = PMMLFactory.getPMMLModel(buff, null); // we will use the mining schema as the instance structure header = ((PMMLModel) model).getMiningSchema().getMiningSchemaAsInstances(); buff.close(); } else if (modelFile.toLowerCase().endsWith(".xstreammodel")) { //$NON-NLS-1$ log.logBasic(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringData.Log.LoadXMLModel")); //$NON-NLS-1$ if (XStream.isPresent()) { Vector v = (Vector) XStream.read(buff); model = v.elementAt(0); if (v.size() == 2) { // try and grab the header header = (Instances) v.elementAt(1); } buff.close(); } else { buff.close(); throw new Exception( BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringData.Error.CantLoadXMLModel")); //$NON-NLS-1$ } } else { InputStream stream = buff; if (modelFile.toLowerCase().endsWith(".gz")) { //$NON-NLS-1$ stream = new GZIPInputStream(buff); } ObjectInputStream oi = new ObjectInputStream(stream); model = oi.readObject(); // try and grab the header header = (Instances) oi.readObject(); if (model instanceof weka.clusterers.Clusterer) { // try and grab any attributes to be ignored during clustering try { ignoredAttsForClustering = (int[]) oi.readObject(); } catch (Exception ex) { // Don't moan if there aren't any :-) } } oi.close(); } WekaScoringModel wsm = WekaScoringModel.createScorer(model); wsm.setHeader(header); if (wsm instanceof WekaScoringClusterer && ignoredAttsForClustering != null) { ((WekaScoringClusterer) wsm).setAttributesToIgnore(ignoredAttsForClustering); } wsm.setLog(log); return wsm; }
From source file:trainableSegmentation.WekaSegmentation.java
License:GNU General Public License
/** * Read header classifier from a .model file * @param filename complete path and file name * @return false if error//from w ww . j av a 2 s .c om */ public boolean loadClassifier(String filename) { AbstractClassifier newClassifier = null; Instances newHeader = null; File selected = new File(filename); try { InputStream is = new FileInputStream(selected); if (selected.getName().endsWith(ClassifierPanel.PMML_FILE_EXTENSION)) { PMMLModel model = PMMLFactory.getPMMLModel(is, null); if (model instanceof PMMLClassifier) newClassifier = (PMMLClassifier) model; else throw new Exception("PMML model is not a classification/regression model!"); } else { if (selected.getName().endsWith(".gz")) is = new GZIPInputStream(is); ObjectInputStream objectInputStream = new ObjectInputStream(is); newClassifier = (AbstractClassifier) objectInputStream.readObject(); try { // see if we can load the header newHeader = (Instances) objectInputStream.readObject(); } catch (Exception e) { IJ.error("Load Failed", "Error while loading train header"); return false; } finally { objectInputStream.close(); } } } catch (Exception e) { IJ.error("Load Failed", "Error while loading classifier"); e.printStackTrace(); return false; } try { // Check if the loaded information corresponds to current state of the segmentator // (the attributes can be adjusted, but the classes must match) if (false == adjustSegmentationStateToData(newHeader)) { IJ.log("Error: current segmentator state could not be updated to loaded data requirements (attributes and classes)"); return false; } } catch (Exception e) { IJ.log("Error while adjusting data!"); e.printStackTrace(); return false; } this.classifier = newClassifier; this.trainHeader = newHeader; return true; }