예제 #1
0
  @Override
  public void perform() throws TaskException {

    try {
      Instances[] data = this.readAndCheckData();

      for (int i = 0; i < data.length; ++i) {
        String oldName = data[i].relationName();
        Filter filter = this.initFilter(data[i]);
        data[i] = Filter.useFilter(data[i], filter);
        data[i].setRelationName(oldName); // keep the old relation name
        FileUtils.writeArff(this.output.get(i), data[i]);
      }
    } catch (TaskException e) {
      throw e;
    } catch (Exception e) {
      Logger.getInstance().logStackTrace(e, Logger.V_DEBUG);
      throw new TaskException(TaskException.ERR_IO_ERROR, this.id, e.getMessage());
    }
  }
예제 #2
0
  /**
   * This reads all the input data and checks their headers if they're identical and sets the class
   * attribute if provided in the input parameters.
   *
   * @return all the data read from the individual files
   */
  private Instances[] readAndCheckData() throws Exception {

    Instances[] data = new Instances[this.input.size()];

    for (int i = 0; i < this.input.size(); ++i) {
      data[i] = FileUtils.readArff(this.input.get(i));
    }
    for (int i = 1; i < this.input.size(); ++i) {
      if (!data[i].equalHeaders(data[0])) {
        throw new TaskException(
            TaskException.ERR_INVALID_DATA,
            this.id,
            "Files "
                + this.input.get(i)
                + " and "
                + this.input.get(0)
                + " don't have equal headers.");
      }
    }

    // set class attribute if set in the data
    if (this.hasParameter(GeneralClassifier.CLASS_ARG)) {
      String className = this.parameters.remove(GeneralClassifier.CLASS_ARG);

      for (int i = 0; i < data.length; ++i) {
        Attribute classAttr = data[i].attribute(className);
        if (classAttr == null) {
          throw new TaskException(
              TaskException.ERR_INVALID_DATA,
              this.id,
              "Class attribute " + className + " not found in the input data.");
        }
        data[i].setClass(classAttr);
      }
    }

    return data;
  }