Package weka.core.converters.ConverterUtils

Examples of weka.core.converters.ConverterUtils.DataSource


      if (selected != -1) {
  ViewerDialog dialog = new ViewerDialog(null);
  String filename = m_List.getSelectedValue().toString();
  int result;
  try {
    DataSource source = new DataSource(filename);
    result = dialog.showDialog(source.getDataSet());
    // nasty workaround for Windows regarding locked files:
    // if file Reader in Loader is not closed explicitly, we cannot
    // overwrite the file.
    source = null;
    System.gc();
View Full Code Here


  public void run() {
    // Copy the current state of things
    m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_First"));
    CostMatrix costMatrix = null;
    Instances inst = new Instances(m_Instances);
    DataSource source = null;
          Instances userTestStructure = null;
    // additional vis info (either shape type or point size)
    FastVector plotShape = new FastVector();
    FastVector plotSize = new FastVector();
    Instances predInstances = null;
  
    // for timing
    long trainTimeStart = 0, trainTimeElapsed = 0;

          try {
            if (m_TestLoader != null && m_TestLoader.getStructure() != null) {
              m_TestLoader.reset();
              source = new DataSource(m_TestLoader);
              userTestStructure = source.getStructure();
            }
          } catch (Exception ex) {
            ex.printStackTrace();
          }
    if (m_EvalWRTCostsBut.isSelected()) {
      costMatrix = new CostMatrix((CostMatrix) m_CostMatrixEditor
          .getValue());
    }
    boolean outputModel = m_OutputModelBut.isSelected();
    boolean outputConfusion = m_OutputConfusionBut.isSelected();
    boolean outputPerClass = m_OutputPerClassBut.isSelected();
    boolean outputSummary = true;
          boolean outputEntropy = m_OutputEntropyBut.isSelected();
    boolean saveVis = m_StorePredictionsBut.isSelected();
    boolean outputPredictionsText = m_OutputPredictionsTextBut.isSelected();
    if (m_OutputAdditionalAttributesText.getText().equals("")) {
      m_OutputAdditionalAttributesRange = null;
    }
    else {
      m_OutputAdditionalAttributesRange = new Range(m_OutputAdditionalAttributesText.getText());
      m_OutputAdditionalAttributesRange.setUpper(inst.numAttributes() - 1);
    }

    String grph = null;

    int testMode = 0;
    int numFolds = 10;
          double percent = 66;
    int classIndex = m_ClassCombo.getSelectedIndex();
    Classifier classifier = (Classifier) m_ClassifierEditor.getValue();
    Classifier template = null;
    try {
      template = Classifier.makeCopy(classifier);
    } catch (Exception ex) {
      m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_LogMessage_Text_First") + ex.getMessage());
    }
    Classifier fullClassifier = null;
    StringBuffer outBuff = new StringBuffer();
    String name = (new SimpleDateFormat("HH:mm:ss - "))
    .format(new Date());
    String cname = classifier.getClass().getName();
    if (cname.startsWith("weka.classifiers.")) {
      name += cname.substring("weka.classifiers.".length());
    } else {
      name += cname;
    }
          String cmd = m_ClassifierEditor.getValue().getClass().getName();
          if (m_ClassifierEditor.getValue() instanceof OptionHandler)
            cmd += " " + Utils.joinOptions(((OptionHandler) m_ClassifierEditor.getValue()).getOptions());
    Evaluation eval = null;
    try {
      if (m_CVBut.isSelected()) {
        testMode = 1;
        numFolds = Integer.parseInt(m_CVText.getText());
        if (numFolds <= 1) {
    throw new Exception(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Exception_Text_First"));
        }
      } else if (m_PercentBut.isSelected()) {
        testMode = 2;
        percent = Double.parseDouble(m_PercentText.getText());
        if ((percent <= 0) || (percent >= 100)) {
    throw new Exception(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Exception_Text_Second"));
        }
      } else if (m_TrainBut.isSelected()) {
        testMode = 3;
      } else if (m_TestSplitBut.isSelected()) {
        testMode = 4;
        // Check the test instance compatibility
        if (source == null) {
    throw new Exception(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Exception_Text_Third"));
        }
        if (!inst.equalHeaders(userTestStructure)) {
    throw new Exception(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Exception_Text_Fourth"));
        }
              userTestStructure.setClassIndex(classIndex);
      } else {
        throw new Exception(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Exception_Text_Fifth"));
      }
      inst.setClassIndex(classIndex);

      // set up the structure of the plottable instances for
      // visualization
            if (saveVis) {
              predInstances = setUpVisualizableInstances(inst);
              predInstances.setClassIndex(inst.classIndex()+1);
            }

      // Output some header information
      m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_LogMessage_Text_Second") + cname);
      m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_LogMessage_Text_Third") + cmd);
      if (m_Log instanceof TaskLogger) {
        ((TaskLogger)m_Log).taskStarted();
      }
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_First"));
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Second") + cname);
      if (classifier instanceof OptionHandler) {
        String [] o = ((OptionHandler) classifier).getOptions();
        outBuff.append(" " + Utils.joinOptions(o));
      }
      outBuff.append("\n");
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Fourth") + inst.relationName() + '\n');
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Sixth") + inst.numInstances() + '\n');
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Eigth") + inst.numAttributes() + '\n');
      if (inst.numAttributes() < 100) {
        for (int i = 0; i < inst.numAttributes(); i++) {
    outBuff.append("              " + inst.attribute(i).name()
             + '\n');
        }
      } else {
        outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Twelveth"));
      }

      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Thirteenth"));
      switch (testMode) {
        case 3: // Test on training
    outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Fourteenth"));
    break;
        case 1: // CV mode
    outBuff.append("" + numFolds + Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Sixteenth"));
    break;
        case 2: // Percent split
    outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Seventeenth") + percent
        + Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Eighteenth"));
    break;
        case 4: // Test on user split
    if (source.isIncremental())
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Nineteenth"));
    else
      outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_Twentyth")
          + source.getDataSet().numInstances() + Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentyFirst"));
    break;
      }
            if (costMatrix != null) {
               outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentySecond"))
               .append(costMatrix.toString()).append("\n");
            }
      outBuff.append("\n");
      m_History.addResult(name, outBuff);
      m_History.setSingle(name);
     
      // Build the model and output it.
      if (outputModel || (testMode == 3) || (testMode == 4)) {
        m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Second"));

        trainTimeStart = System.currentTimeMillis();
        classifier.buildClassifier(inst);
        trainTimeElapsed = System.currentTimeMillis() - trainTimeStart;
      }

      if (outputModel) {
        outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentySixth"));
        outBuff.append(classifier.toString() + "\n");
        outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentyEighth") +
           Utils.doubleToString(trainTimeElapsed / 1000.0,2)
           + " " + Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentyNineth"));
        m_History.updateResult(name);
        if (classifier instanceof Drawable) {
    grph = null;
    try {
      grph = ((Drawable)classifier).graph();
    } catch (Exception ex) {
    }
        }
        // copy full model for output
        SerializedObject so = new SerializedObject(classifier);
        fullClassifier = (Classifier) so.getObject();
      }
     
      switch (testMode) {
        case 3: // Test on training
        m_Log.statusMessage("Evaluating on training data...");
        eval = new Evaluation(inst, costMatrix);
       
        if (outputPredictionsText) {
    printPredictionsHeader(outBuff, inst, "training set");
        }

        for (int jj=0;jj<inst.numInstances();jj++) {
    processClassifierPrediction(inst.instance(jj), classifier,
              eval, predInstances, plotShape,
              plotSize);
   
    if (outputPredictionsText) {
      outBuff.append(predictionText(classifier, inst.instance(jj), jj+1));
    }
    if ((jj % 100) == 0) {
      m_Log.statusMessage("Evaluating on training data. Processed "
              +jj+" instances...");
    }
        }
        if (outputPredictionsText) {
    outBuff.append("\n");
        }
        outBuff.append("=== Evaluation on training set ===\n");
        break;

        case 1: // CV mode
        m_Log.statusMessage("Randomizing instances...");
        int rnd = 1;
        try {
    rnd = Integer.parseInt(m_RandomSeedText.getText().trim());
    // System.err.println("Using random seed "+rnd);
        } catch (Exception ex) {
    m_Log.logMessage("Trouble parsing random seed value");
    rnd = 1;
        }
        Random random = new Random(rnd);
        inst.randomize(random);
        if (inst.attribute(classIndex).isNominal()) {
    m_Log.statusMessage("Stratifying instances...");
    inst.stratify(numFolds);
        }
        eval = new Evaluation(inst, costMatrix);
     
        if (outputPredictionsText) {
    printPredictionsHeader(outBuff, inst, "test data");
        }

        // Make some splits and do a CV
        for (int fold = 0; fold < numFolds; fold++) {
    m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Eighth")
            + (fold + 1) + Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Nineth"));
    Instances train = inst.trainCV(numFolds, fold, random);
    eval.setPriors(train);
    m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Tenth")
            + (fold + 1) + Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Twelveth"));
    Classifier current = null;
    try {
      current = Classifier.makeCopy(template);
    } catch (Exception ex) {
      m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_LogMessage_Text_Fifth") + ex.getMessage());
    }
    current.buildClassifier(train);
    Instances test = inst.testCV(numFolds, fold);
    m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Eleventh")
            + (fold + 1) + Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Twelveth"));
    for (int jj=0;jj<test.numInstances();jj++) {
      processClassifierPrediction(test.instance(jj), current,
                eval, predInstances, plotShape,
                plotSize);
      if (outputPredictionsText) {
        outBuff.append(predictionText(current, test.instance(jj), jj+1));
      }
    }
        }
        if (outputPredictionsText) {
    outBuff.append("\n");
        }
        if (inst.attribute(classIndex).isNominal()) {
    outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_ThirtyThird"));
        } else {
    outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_ThirtyFourth"));
        }
        break;
   
        case 2: // Percent split
        if (!m_PreserveOrderBut.isSelected()) {
    m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Thirteenth"));
    try {
      rnd = Integer.parseInt(m_RandomSeedText.getText().trim());
    } catch (Exception ex) {
      m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Fourteenth"));
      rnd = 1;
    }
    inst.randomize(new Random(rnd));
        }
        int trainSize = (int) Math.round(inst.numInstances() * percent / 100);
        int testSize = inst.numInstances() - trainSize;
        Instances train = new Instances(inst, 0, trainSize);
        Instances test = new Instances(inst, trainSize, testSize);
        m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Fifteenth") + trainSize+ Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Sixteenth"));
        Classifier current = null;
        try {
    current = Classifier.makeCopy(template);
        } catch (Exception ex) {
    m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_LogMessage_Text_Sixth") + ex.getMessage());
        }
        current.buildClassifier(train);
        eval = new Evaluation(train, costMatrix);
        m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Seventeenth"));
      
        if (outputPredictionsText) {
    printPredictionsHeader(outBuff, inst, Messages.getInstance().getString("ClassifierPanel_StartClassifier_PrintPredictionsHeader_Text_First"));
        }
    
        for (int jj=0;jj<test.numInstances();jj++) {
    processClassifierPrediction(test.instance(jj), current,
              eval, predInstances, plotShape,
              plotSize);
    if (outputPredictionsText) {
        outBuff.append(predictionText(current, test.instance(jj), jj+1));
    }
    if ((jj % 100) == 0) {
      m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Eighteenth")
              +jj + Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Nineteenth"));
    }
        }
        if (outputPredictionsText) {
    outBuff.append("\n");
        }
        outBuff.append(Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_ThirtySixth"));
        break;
   
        case 4: // Test on user split
        m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_StartClassifier_Log_StatusMessage_Text_Twentyth"));
        eval = new Evaluation(inst, costMatrix);
       
        if (outputPredictionsText) {
    printPredictionsHeader(outBuff, inst, Messages.getInstance().getString("ClassifierPanel_StartClassifier_PrintPredictionsHeader_Text_Second"));
        }

        Instance instance;
        int jj = 0;
        while (source.hasMoreElements(userTestStructure)) {
    instance = source.nextElement(userTestStructure);
    processClassifierPrediction(instance, classifier,
        eval, predInstances, plotShape,
        plotSize);
    if (outputPredictionsText) {
      outBuff.append(predictionText(classifier, instance, jj+1));
View Full Code Here

          public void run() {
            // Copy the current state of things
            m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Log_StatusMessage_Text_First"));

            StringBuffer outBuff = m_History.getNamedBuffer(name);
            DataSource source = null;
            Instances userTestStructure = null;
            // additional vis info (either shape type or point size)
            FastVector plotShape = new FastVector();
            FastVector plotSize = new FastVector();
            Instances predInstances = null;

            CostMatrix costMatrix = null;
            if (m_EvalWRTCostsBut.isSelected()) {
              costMatrix = new CostMatrix((CostMatrix) m_CostMatrixEditor
                                          .getValue());
            }   
            boolean outputConfusion = m_OutputConfusionBut.isSelected();
            boolean outputPerClass = m_OutputPerClassBut.isSelected();
            boolean outputSummary = true;
            boolean outputEntropy = m_OutputEntropyBut.isSelected();
            boolean saveVis = m_StorePredictionsBut.isSelected();
            boolean outputPredictionsText =
              m_OutputPredictionsTextBut.isSelected();
            String grph = null;   
            Evaluation eval = null;

            try {

              boolean incrementalLoader = (m_TestLoader instanceof IncrementalConverter);
              if (m_TestLoader != null && m_TestLoader.getStructure() != null) {
                m_TestLoader.reset();
                source = new DataSource(m_TestLoader);
                userTestStructure = source.getStructure();
              }
              // Check the test instance compatibility
              if (source == null) {
                throw new Exception(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Exception_Text_First"));
              }
              if (trainHeader != null) {
                if (trainHeader.classIndex() >
                    userTestStructure.numAttributes()-1)
                  throw new Exception(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Exception_Text_Second"));
                userTestStructure.setClassIndex(trainHeader.classIndex());
                if (!trainHeader.equalHeaders(userTestStructure)) {
                  throw new Exception(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Exception_Text_Third"));
                }
              } else {
          if (classifier instanceof PMMLClassifier) {
            // set the class based on information in the mining schema
            Instances miningSchemaStructure =
              ((PMMLClassifier)classifier).getMiningSchema().getMiningSchemaAsInstances();
            String className = miningSchemaStructure.classAttribute().name();
            Attribute classMatch = userTestStructure.attribute(className);
            if (classMatch == null) {
              throw new Exception(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Exception_Text_Fourth")
            + className + Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Exception_Text_Fifth"));
            }
            userTestStructure.setClass(classMatch);
          } else {
            userTestStructure.
              setClassIndex(userTestStructure.numAttributes()-1);
          }
              }
              if (m_Log instanceof TaskLogger) {
                ((TaskLogger)m_Log).taskStarted();
              }
              m_Log.statusMessage(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Log_StatusMessage_Text_Second"));
              m_Log.logMessage(Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Log_LogMessage_Text_First") + name
                               + Messages.getInstance().getString("ClassifierPanel_ReEvaluateModel_Log_LogMessage_Text_Second"));
              eval = new Evaluation(userTestStructure, costMatrix);
              eval.useNoPriors();
     
              // set up the structure of the plottable instances for
              // visualization if selected
              if (saveVis) {
                predInstances = setUpVisualizableInstances(userTestStructure);
                predInstances.setClassIndex(userTestStructure.classIndex()+1);
              }
     
              outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_Twelveth"));
              outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_Thirteenth"))
              outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_Fourteenth")
                             + userTestStructure.relationName() + '\n');
              if (incrementalLoader)
          outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_Sixteenth"));
              else
          outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_Seventeenth") + source.getDataSet().numInstances() + "\n");
              outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_Nineteenth")
            + userTestStructure.numAttributes() + "\n\n");
              if (trainHeader == null &&
                  !(classifier instanceof
                      weka.classifiers.pmml.consumer.PMMLClassifier)) {

                outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_TwentyFirst"));

              }

              if (outputPredictionsText) {
                outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_TwentySecond"));
                outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_TwentyThird"));
                if (userTestStructure.classAttribute().isNominal()) {
                  outBuff.append(Messages.getInstance().getString("ClassifierPanel_SaveClassifier_OutBuffer_Text_TwentyFourth"));
                }
                outBuff.append("\n");
              }

        Instance instance;
        int jj = 0;
        while (source.hasMoreElements(userTestStructure)) {
    instance = source.nextElement(userTestStructure);
    processClassifierPrediction(instance, classifier,
        eval, predInstances, plotShape,
        plotSize);
    if (outputPredictionsText) {
      outBuff.append(predictionText(classifier, instance, jj+1));
View Full Code Here

   * @throws Exception  if something goes wrong
   */
  public static void main(String[] args) throws Exception {
    String     tmpStr;
    String    filename;
    DataSource     source;
    Instances     data;
    int     classIndex;
    Capabilities   cap;
    Iterator    iter;

    if (args.length == 0) {
      System.out.println(
    "\nUsage: " + Capabilities.class.getName()
    + " -file <dataset> [-c <class index>]\n");
      return;
    }
   
    // get parameters
    tmpStr = Utils.getOption("file", args);
    if (tmpStr.length() == 0)
      throw new Exception("No file provided with option '-file'!");
    else
      filename = tmpStr;

    tmpStr = Utils.getOption("c", args);
    if (tmpStr.length() != 0) {
      if (tmpStr.equals("first"))
  classIndex = 0;
      else if (tmpStr.equals("last"))
  classIndex = -2// last
      else
  classIndex = Integer.parseInt(tmpStr) - 1;
    }
    else {
      classIndex = -3// not set
    }
   
    // load data
    source = new DataSource(filename);
    if (classIndex == -3)
      data = source.getDataSet();
    else if (classIndex == -2)
      data = source.getDataSet(source.getStructure().numAttributes() - 1);
    else
      data = source.getDataSet(classIndex);

    // determine and print capabilities
    cap = forInstances(data);
    System.out.println("File: " + filename);
    System.out.println("Class index: " + ((data.classIndex() == -1) ? "not set" : "" + (data.classIndex() + 1)));
View Full Code Here

  public static void filterFile(Filter filter, String [] options)
    throws Exception {

    boolean debug = false;
    Instances data = null;
    DataSource input = null;
    PrintWriter output = null;
    boolean helpRequest;
    String sourceCode = "";

    try {
       helpRequest = Utils.getFlag('h', options);

      if (Utils.getFlag('d', options)) {
  debug = true;
      }
      String infileName = Utils.getOption('i', options);
      String outfileName = Utils.getOption('o', options);
      String classIndex = Utils.getOption('c', options);
      if (filter instanceof Sourcable)
  sourceCode = Utils.getOption('z', options);
     
      if (filter instanceof OptionHandler) {
  ((OptionHandler)filter).setOptions(options);
      }

      Utils.checkForRemainingOptions(options);
      if (helpRequest) {
  throw new Exception("Help requested.\n");
      }
      if (infileName.length() != 0) {
  input = new DataSource(infileName);
      } else {
  input = new DataSource(System.in);
      }
      if (outfileName.length() != 0) {
  output = new PrintWriter(new FileOutputStream(outfileName));
      } else {
  output = new PrintWriter(System.out);
      }

      data = input.getStructure();
      if (classIndex.length() != 0) {
  if (classIndex.equals("first")) {
    data.setClassIndex(0);
  } else if (classIndex.equals("last")) {
    data.setClassIndex(data.numAttributes() - 1);
  } else {
    data.setClassIndex(Integer.parseInt(classIndex) - 1);
  }
      }
    } catch (Exception ex) {
      String filterOptions = "";
      // Output the error and also the valid options
      if (filter instanceof OptionHandler) {
  filterOptions += "\nFilter options:\n\n";
  Enumeration enu = ((OptionHandler)filter).listOptions();
  while (enu.hasMoreElements()) {
    Option option = (Option) enu.nextElement();
    filterOptions += option.synopsis() + '\n'
      + option.description() + "\n";
  }
      }

      String genericOptions = "\nGeneral options:\n\n"
  + "-h\n"
  + "\tGet help on available options.\n"
  + "\t(use -b -h for help on batch mode.)\n"
  + "-i <file>\n"
  + "\tThe name of the file containing input instances.\n"
  + "\tIf not supplied then instances will be read from stdin.\n"
  + "-o <file>\n"
  + "\tThe name of the file output instances will be written to.\n"
  + "\tIf not supplied then instances will be written to stdout.\n"
  + "-c <class index>\n"
  + "\tThe number of the attribute to use as the class.\n"
  + "\t\"first\" and \"last\" are also valid entries.\n"
  + "\tIf not supplied then no class is assigned.\n";

      if (filter instanceof Sourcable) {
  genericOptions +=
    "-z <class name>\n"
    + "\tOutputs the source code representing the trained filter.\n";
      }
     
      throw new Exception('\n' + ex.getMessage()
        + filterOptions+genericOptions);
    }
   
    if (debug) {
      System.err.println("Setting input format");
    }
    boolean printedHeader = false;
    if (filter.setInputFormat(data)) {
      if (debug) {
  System.err.println("Getting output format");
      }
      output.println(filter.getOutputFormat().toString());
      printedHeader = true;
    }
   
    // Pass all the instances to the filter
    Instance inst;
    while (input.hasMoreElements(data)) {
      inst = input.nextElement(data);
      if (debug) {
  System.err.println("Input instance to filter");
      }
      if (filter.input(inst)) {
  if (debug) {
View Full Code Here

  public static void batchFilterFile(Filter filter, String [] options)
    throws Exception {

    Instances firstData = null;
    Instances secondData = null;
    DataSource firstInput = null;
    DataSource secondInput = null;
    PrintWriter firstOutput = null;
    PrintWriter secondOutput = null;
    boolean helpRequest;
    String sourceCode = "";

    try {
      helpRequest = Utils.getFlag('h', options);

      String fileName = Utils.getOption('i', options);
      if (fileName.length() != 0) {
  firstInput = new DataSource(fileName);
      } else {
  throw new Exception("No first input file given.\n");
      }

      fileName = Utils.getOption('r', options);
      if (fileName.length() != 0) {
  secondInput = new DataSource(fileName);
      } else {
  throw new Exception("No second input file given.\n");
      }

      fileName = Utils.getOption('o', options);
      if (fileName.length() != 0) {
  firstOutput = new PrintWriter(new FileOutputStream(fileName));
      } else {
  firstOutput = new PrintWriter(System.out);
      }
     
      fileName = Utils.getOption('s', options);
      if (fileName.length() != 0) {
  secondOutput = new PrintWriter(new FileOutputStream(fileName));
      } else {
  secondOutput = new PrintWriter(System.out);
      }
      String classIndex = Utils.getOption('c', options);
      if (filter instanceof Sourcable)
  sourceCode = Utils.getOption('z', options);

      if (filter instanceof OptionHandler) {
  ((OptionHandler)filter).setOptions(options);
      }
      Utils.checkForRemainingOptions(options);
     
      if (helpRequest) {
  throw new Exception("Help requested.\n");
      }
      firstData = firstInput.getStructure();
      secondData = secondInput.getStructure();
      if (!secondData.equalHeaders(firstData)) {
  throw new Exception("Input file formats differ.\n");
      }
      if (classIndex.length() != 0) {
  if (classIndex.equals("first")) {
    firstData.setClassIndex(0);
    secondData.setClassIndex(0);
  } else if (classIndex.equals("last")) {
    firstData.setClassIndex(firstData.numAttributes() - 1);
    secondData.setClassIndex(secondData.numAttributes() - 1);
  } else {
    firstData.setClassIndex(Integer.parseInt(classIndex) - 1);
    secondData.setClassIndex(Integer.parseInt(classIndex) - 1);
  }
      }
    } catch (Exception ex) {
      String filterOptions = "";
      // Output the error and also the valid options
      if (filter instanceof OptionHandler) {
  filterOptions += "\nFilter options:\n\n";
  Enumeration enu = ((OptionHandler)filter).listOptions();
  while (enu.hasMoreElements()) {
    Option option = (Option) enu.nextElement();
    filterOptions += option.synopsis() + '\n'
      + option.description() + "\n";
  }
      }

      String genericOptions = "\nGeneral options:\n\n"
  + "-h\n"
  + "\tGet help on available options.\n"
  + "-i <filename>\n"
  + "\tThe file containing first input instances.\n"
  + "-o <filename>\n"
  + "\tThe file first output instances will be written to.\n"
  + "-r <filename>\n"
  + "\tThe file containing second input instances.\n"
  + "-s <filename>\n"
  + "\tThe file second output instances will be written to.\n"
  + "-c <class index>\n"
  + "\tThe number of the attribute to use as the class.\n"
  + "\t\"first\" and \"last\" are also valid entries.\n"
  + "\tIf not supplied then no class is assigned.\n";

      if (filter instanceof Sourcable) {
  genericOptions +=
    "-z <class name>\n"
    + "\tOutputs the source code representing the trained filter.\n";
      }
     
      throw new Exception('\n' + ex.getMessage()
        + filterOptions+genericOptions);
    }
    boolean printedHeader = false;
    if (filter.setInputFormat(firstData)) {
      firstOutput.println(filter.getOutputFormat().toString());
      printedHeader = true;
    }
   
    // Pass all the instances to the filter
    Instance inst;
    while (firstInput.hasMoreElements(firstData)) {
      inst = firstInput.nextElement(firstData);
      if (filter.input(inst)) {
  if (!printedHeader) {
    throw new Error("Filter didn't return true from setInputFormat() "
        + "earlier!");
  }
  firstOutput.println(filter.output().toString());
      }
    }
   
    // Say that input has finished, and print any pending output instances
    if (filter.batchFinished()) {
      if (!printedHeader) {
  firstOutput.println(filter.getOutputFormat().toString());
      }
      while (filter.numPendingOutput() > 0) {
  firstOutput.println(filter.output().toString());
      }
    }
   
    if (firstOutput != null) {
      firstOutput.close();
    }   
    printedHeader = false;
    if (filter.isOutputFormatDefined()) {
      secondOutput.println(filter.getOutputFormat().toString());
      printedHeader = true;
    }
    // Pass all the second instances to the filter
    while (secondInput.hasMoreElements(secondData)) {
      inst = secondInput.nextElement(secondData);
      if (filter.input(inst)) {
  if (!printedHeader) {
    throw new Error("Filter didn't return true from"
        + " isOutputFormatDefined() earlier!");
  }
View Full Code Here

  public static String evaluate(Associator associator, String[] optionsthrows Exception {

    String trainFileString = "";
    String graphFileName = "";
    AssociatorEvaluation eval;
    DataSource loader;

    // help?
    if (Utils.getFlag('h', options))
      throw new Exception("\nHelp requested.\n" + makeOptionString(associator));
   
    try {
      // general options
      trainFileString = Utils.getOption('t', options);
      if (trainFileString.length() == 0)
  throw new Exception("No training file given!");
      loader = new DataSource(trainFileString);

      graphFileName = Utils.getOption('g', options);

      // associator specific options
      if (associator instanceof OptionHandler) {
        ((OptionHandler) associator).setOptions(options);
      }
     
      // left-over options?
      Utils.checkForRemainingOptions(options);
    }
    catch (Exception e) {
      throw new Exception(
    "\nWeka exception: "
    + e.getMessage() + "\n"
    + makeOptionString(associator));
    }
   
    // load file and build associations
    eval = new AssociatorEvaluation();
    String results = eval.evaluate(associator, new Instances(loader.getDataSet()));
   
   
    File file = new File("C:\\ProgramFiles\\apache-tomcat-6.0.18\\pippo.txt");
    logger.debug("Saving results on file [" + file + "]");
    BufferedWriter w = new BufferedWriter(new FileWriter(graphFileName));
View Full Code Here

   * @throws Exception  if something goes wrong
   */
  public static void main(String[] args) throws Exception {
    String     tmpStr;
    String    filename;
    DataSource     source;
    Instances     data;
    int     classIndex;
    Capabilities   cap;
    Iterator    iter;

    if (args.length == 0) {
      System.out.println(
    "\nUsage: " + Capabilities.class.getName()
    + " -file <dataset> [-c <class index>]\n");
      return;
    }
   
    // get parameters
    tmpStr = Utils.getOption("file", args);
    if (tmpStr.length() == 0)
      throw new Exception("No file provided with option '-file'!");
    else
      filename = tmpStr;

    tmpStr = Utils.getOption("c", args);
    if (tmpStr.length() != 0) {
      if (tmpStr.equals("first"))
  classIndex = 0;
      else if (tmpStr.equals("last"))
  classIndex = -2// last
      else
  classIndex = Integer.parseInt(tmpStr) - 1;
    }
    else {
      classIndex = -3// not set
    }
   
    // load data
    source = new DataSource(filename);
    if (classIndex == -3)
      data = source.getDataSet();
    else if (classIndex == -2)
      data = source.getDataSet(source.getStructure().numAttributes() - 1);
    else
      data = source.getDataSet(classIndex);

    // determine and print capabilities
    cap = forInstances(data);
    System.out.println("File: " + filename);
    System.out.println("Class index: " + ((data.classIndex() == -1) ? "not set" : "" + (data.classIndex() + 1)));
View Full Code Here

    try {
      Instances i;
      // read from stdin and print statistics
      if (args.length == 0) {
  DataSource source = new DataSource(System.in);
  i = source.getDataSet();
  System.out.println(i.toSummaryString());
      }
      // read file and print statistics
      else if ((args.length == 1) && (!args[0].equals("-h")) && (!args[0].equals("help"))) {
  DataSource source = new DataSource(args[0]);
  i = source.getDataSet();
  System.out.println(i.toSummaryString());
      }
      // read two files, merge them and print result to stdout
      else if ((args.length == 3) && (args[0].toLowerCase().equals("merge"))) {
  DataSource source1 = new DataSource(args[1]);
  DataSource source2 = new DataSource(args[2]);
  i = Instances.mergeInstances(source1.getDataSet(), source2.getDataSet());
  System.out.println(i);
      }
      // read two files, append them and print result to stdout
      else if ((args.length == 3) && (args[0].toLowerCase().equals("append"))) {
  DataSource source1 = new DataSource(args[1]);
  DataSource source2 = new DataSource(args[2]);
  if (!source1.getStructure().equalHeaders(source2.getStructure()))
    throw new Exception("The two datasets have different headers!");
  Instances structure = source1.getStructure();
  System.out.println(source1.getStructure());
  while (source1.hasMoreElements(structure))
    System.out.println(source1.nextElement(structure));
  structure = source2.getStructure();
  while (source2.hasMoreElements(structure))
    System.out.println(source2.nextElement(structure));
      }
      // read file and seed value, randomize data and print result to stdout
      else if ((args.length == 3) && (args[0].toLowerCase().equals("randomize"))) {
  DataSource source = new DataSource(args[2]);
  i = source.getDataSet();
  i.randomize(new Random(Integer.parseInt(args[1])));
  System.out.println(i);
      }
      // wrong parameters
      else {
View Full Code Here

    int cc = clusterer.numberOfClusters();
    double[] instanceStats = new double[cc];
    int unclusteredInstances = 0;

    if (fileName.length() != 0) {
      DataSource source = new DataSource(fileName);
      Instances structure = source.getStructure();
      Instance inst;
      while (source.hasMoreElements(structure)) {
  inst = source.nextElement(structure);
  try {
    cnum = clusterer.clusterInstance(inst);

    if (clusterer instanceof DensityBasedClusterer) {
      loglk += ((DensityBasedClusterer)clusterer).
View Full Code Here

TOP

Related Classes of weka.core.converters.ConverterUtils.DataSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.