/** * Determines the output format based on the input format and returns this. In case the output * format cannot be returned immediately, i.e., hasImmediateOutputFormat() returns false, then * this method will called from batchFinished() after the call of preprocess(Instances), in which, * e.g., statistics for the actual processing step can be gathered. * * @param inputFormat the input format to base the output format on * @return the output format * @throws Exception in case the determination goes wrong */ protected Instances determineOutputFormat(Instances inputFormat) throws Exception { Instances result; Attribute att; ArrayList<Attribute> atts; int i; m_AttributeIndices.setUpper(inputFormat.numAttributes() - 1); // generate new header atts = new ArrayList<Attribute>(); for (i = 0; i < inputFormat.numAttributes(); i++) { att = inputFormat.attribute(i); if (m_AttributeIndices.isInRange(i)) { if (m_ReplaceAll) atts.add(att.copy(att.name().replaceAll(m_Find, m_Replace))); else atts.add(att.copy(att.name().replaceFirst(m_Find, m_Replace))); } else { atts.add((Attribute) att.copy()); } } result = new Instances(inputFormat.relationName(), atts, 0); result.setClassIndex(inputFormat.classIndex()); return result; }
/** Set the output format. Changes the format of the specified date attribute. */ private void setOutputFormat() { // Create new attributes FastVector newAtts = new FastVector(getInputFormat().numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (j == m_AttIndex.getIndex()) { newAtts.addElement(new Attribute(att.name(), getDateFormat().toPattern())); } else { newAtts.addElement(att.copy()); } } // Create new header Instances newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); m_OutputAttribute = newData.attribute(m_AttIndex.getIndex()); setOutputFormat(newData); }
/** * Set the output format. Swapss the desired nominal attribute values in the header and calls * setOutputFormat(Instances) appropriately. */ private void setOutputFormat() { Instances newData; ArrayList<Attribute> newAtts; ArrayList<String> newVals; // Compute new attributes newAtts = new ArrayList<Attribute>(getInputFormat().numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (j != m_AttIndex.getIndex()) { newAtts.add((Attribute) att.copy()); } else { // Compute list of attribute values newVals = new ArrayList<String>(att.numValues()); for (int i = 0; i < att.numValues(); i++) { if (i == m_FirstIndex.getIndex()) { newVals.add(att.value(m_SecondIndex.getIndex())); } else if (i == m_SecondIndex.getIndex()) { newVals.add(att.value(m_FirstIndex.getIndex())); } else { newVals.add(att.value(i)); } } Attribute newAtt = new Attribute(att.name(), newVals); newAtt.setWeight(att.weight()); newAtts.add(newAtt); } } // Construct new header newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); setOutputFormat(newData); }
/** * Set the output format. Takes the current average class values and m_InputFormat and calls * setOutputFormat(Instances) appropriately. */ private void setOutputFormat() { Instances newData; FastVector newAtts, newVals; boolean firstEndsWithPrime = false, secondEndsWithPrime = false; StringBuffer text = new StringBuffer(); // Compute new attributes newAtts = new FastVector(getInputFormat().numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (j != m_AttIndex.getIndex()) { newAtts.addElement(att.copy()); } else { // Compute new value if (att.value(m_FirstIndex.getIndex()).endsWith("'")) { firstEndsWithPrime = true; } if (att.value(m_SecondIndex.getIndex()).endsWith("'")) { secondEndsWithPrime = true; } if (firstEndsWithPrime || secondEndsWithPrime) { text.append("'"); } if (firstEndsWithPrime) { text.append( ((String) att.value(m_FirstIndex.getIndex())) .substring(1, ((String) att.value(m_FirstIndex.getIndex())).length() - 1)); } else { text.append((String) att.value(m_FirstIndex.getIndex())); } text.append('_'); if (secondEndsWithPrime) { text.append( ((String) att.value(m_SecondIndex.getIndex())) .substring(1, ((String) att.value(m_SecondIndex.getIndex())).length() - 1)); } else { text.append((String) att.value(m_SecondIndex.getIndex())); } if (firstEndsWithPrime || secondEndsWithPrime) { text.append("'"); } // Compute list of attribute values newVals = new FastVector(att.numValues() - 1); for (int i = 0; i < att.numValues(); i++) { if (i == m_FirstIndex.getIndex()) { newVals.addElement(text.toString()); } else if (i != m_SecondIndex.getIndex()) { newVals.addElement(att.value(i)); } } Attribute newAtt = new Attribute(att.name(), newVals); newAtt.setWeight(getInputFormat().attribute(j).weight()); newAtts.addElement(newAtt); } } // Construct new header newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); setOutputFormat(newData); }
/** Set the output format if the class is numeric. */ private void setOutputFormatNumeric() { if (m_Indices == null) { setOutputFormat(null); return; } ArrayList<Attribute> newAtts; int newClassIndex; StringBuffer attributeName; Instances outputFormat; ArrayList<String> vals; // Compute new attributes m_needToTransform = false; for (int i = 0; i < getInputFormat().numAttributes(); i++) { Attribute att = getInputFormat().attribute(i); if (att.isNominal() && (att.numValues() > 2 || m_Numeric || m_TransformAll)) { m_needToTransform = true; break; } } if (!m_needToTransform) { setOutputFormat(getInputFormat()); return; } newClassIndex = getInputFormat().classIndex(); newAtts = new ArrayList<Attribute>(); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if ((!att.isNominal()) || (j == getInputFormat().classIndex())) { newAtts.add((Attribute) att.copy()); } else { if (j < getInputFormat().classIndex()) { newClassIndex += att.numValues() - 2; } // Compute values for new attributes for (int k = 1; k < att.numValues(); k++) { attributeName = new StringBuffer(att.name() + "="); for (int l = k; l < att.numValues(); l++) { if (l > k) { attributeName.append(','); } attributeName.append(att.value(m_Indices[j][l])); } if (m_Numeric) { newAtts.add(new Attribute(attributeName.toString())); } else { vals = new ArrayList<String>(2); vals.add("f"); vals.add("t"); newAtts.add(new Attribute(attributeName.toString(), vals)); } } } } outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0); outputFormat.setClassIndex(newClassIndex); setOutputFormat(outputFormat); }