/** Creates an example NominalToString */ public Filter getFilter() { NominalToString f = new NominalToString(); f.setAttributeIndexes("2"); return f; }
/** * Parses a given list of options. * <p/> * * <!-- options-start --> Valid options are: * <p/> * * <pre> * -C <col> * Sets the range of attributes to convert (default last). * </pre> * * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String tmpStr = Utils.getOption('C', options); if (tmpStr.length() != 0) { setAttributeIndexes(tmpStr); } else { setAttributeIndexes("last"); } if (getInputFormat() != null) { setInputFormat(getInputFormat()); } Utils.checkForRemainingOptions(options); }
/** * Input an instance for filtering. The instance is processed and made * available for output immediately. * * @param instance the input instance. * @return true if the filtered instance may now be collected with output(). * @throws IllegalStateException if no input structure has been defined. */ @Override public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (isOutputFormatDefined()) { Instance newInstance = (Instance) instance.copy(); push(newInstance, false); // No need to copy return true; } bufferInput(instance); return false; }
/** * Main method for testing this class. * * @param args should contain arguments to the filter: use -h for help */ public static void main(String[] args) { runFilter(new NominalToString(), args); } }
NominalToString filter1 = new NominalToString(); filter1.setInputFormat(data); data = Filter.useFilter(data, filter1);
/** * Signifies that this batch of input to the filter is finished. If the filter * requires all instances prior to filtering, output() may now be called to * retrieve the filtered instances. * * @return true if there are instances pending output. * @throws IllegalStateException if no input structure has been defined. */ @Override public boolean batchFinished() { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isOutputFormatDefined()) { setOutputFormat(); // Convert pending input instances for (int i = 0; i < getInputFormat().numInstances(); i++) { push((Instance) getInputFormat().instance(i).copy(), false); // No need to copy } } flushInput(); m_NewBatch = true; return (numPendingOutput() != 0); }
/** * returns the configured FilteredClassifier. Since the base classifier is * determined heuristically, derived tests might need to adjust it. * * @return the configured FilteredClassifier */ protected FilteredClassifier getFilteredClassifier() { FilteredClassifier result; result = super.getFilteredClassifier(); ((NominalToString) result.getFilter()).setAttributeIndexes("1"); result.setClassifier(new ZeroR()); return result; }
/** * Set the output format. Takes the current average class values and * m_InputFormat and calls setOutputFormat(Instances) appropriately. */ private void setOutputFormat() { Instances newData; ArrayList<Attribute> newAtts = new ArrayList<Attribute>(getInputFormat() .numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (!att.isNominal() || !m_AttIndex.isInRange(j)) { newAtts.add(att); } else { Attribute newAtt = new Attribute(att.name(), (ArrayList<String>) null); newAtt.setWeight(getInputFormat().attribute(j).weight()); newAtts.add(newAtt); } } // Construct new header newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); setOutputFormat(newData); }
/** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ @Override public String[] getOptions() { Vector<String> result = new Vector<String>(); result.add("-C"); result.add("" + (getAttributeIndexes())); return result.toArray(new String[result.size()]); }
/** * Signifies that this batch of input to the filter is finished. If the filter * requires all instances prior to filtering, output() may now be called to * retrieve the filtered instances. * * @return true if there are instances pending output. * @throws IllegalStateException if no input structure has been defined. */ @Override public boolean batchFinished() { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isOutputFormatDefined()) { setOutputFormat(); // Convert pending input instances for (int i = 0; i < getInputFormat().numInstances(); i++) { push((Instance) getInputFormat().instance(i).copy(), false); // No need to copy } } flushInput(); m_NewBatch = true; return (numPendingOutput() != 0); }
/** * Main method for testing this class. * * @param args should contain arguments to the filter: use -h for help */ public static void main(String[] args) { runFilter(new NominalToString(), args); } }
/** * returns the configured FilteredClassifier. Since the base classifier is * determined heuristically, derived tests might need to adjust it. * * @return the configured FilteredClassifier */ protected FilteredClassifier getFilteredClassifier() { FilteredClassifier result; result = super.getFilteredClassifier(); ((NominalToString) result.getFilter()).setAttributeIndexes("1"); result.setClassifier(new ZeroR()); return result; }
/** * Set the output format. Takes the current average class values and * m_InputFormat and calls setOutputFormat(Instances) appropriately. */ private void setOutputFormat() { Instances newData; ArrayList<Attribute> newAtts = new ArrayList<Attribute>(getInputFormat() .numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (!att.isNominal() || !m_AttIndex.isInRange(j)) { newAtts.add(att); } else { Attribute newAtt = new Attribute(att.name(), (ArrayList<String>) null); newAtt.setWeight(getInputFormat().attribute(j).weight()); newAtts.add(newAtt); } } // Construct new header newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); setOutputFormat(newData); }
/** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ @Override public String[] getOptions() { Vector<String> result = new Vector<String>(); result.add("-C"); result.add("" + (getAttributeIndexes())); return result.toArray(new String[result.size()]); }
/** * Input an instance for filtering. The instance is processed and made * available for output immediately. * * @param instance the input instance. * @return true if the filtered instance may now be collected with output(). * @throws IllegalStateException if no input structure has been defined. */ @Override public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (isOutputFormatDefined()) { Instance newInstance = (Instance) instance.copy(); push(newInstance, false); // No need to copy return true; } bufferInput(instance); return false; }
/** Creates an example NominalToString */ public Filter getFilter() { NominalToString f = new NominalToString(); f.setAttributeIndexes("2"); return f; }
/** * Parses a given list of options. * <p/> * * <!-- options-start --> Valid options are: * <p/> * * <pre> * -C <col> * Sets the range of attributes to convert (default last). * </pre> * * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String tmpStr = Utils.getOption('C', options); if (tmpStr.length() != 0) { setAttributeIndexes(tmpStr); } else { setAttributeIndexes("last"); } if (getInputFormat() != null) { setInputFormat(getInputFormat()); } Utils.checkForRemainingOptions(options); }
/** * tests the filter in conjunction with the FilteredClassifier */ public void testFilteredClassifier() { try { Instances data = getFilteredClassifierData(); for (int i = 0; i < data.numAttributes(); i++) { if (data.classIndex() == i) continue; if (data.attribute(i).isNominal()) { ((NominalToString) m_FilteredClassifier.getFilter()).setAttributeIndexes( "" + (i + 1)); break; } } } catch (Exception e) { fail("Problem setting up test for FilteredClassifier: " + e.toString()); } super.testFilteredClassifier(); }
/** * tests the filter in conjunction with the FilteredClassifier */ public void testFilteredClassifier() { try { Instances data = getFilteredClassifierData(); for (int i = 0; i < data.numAttributes(); i++) { if (data.classIndex() == i) continue; if (data.attribute(i).isNominal()) { ((NominalToString) m_FilteredClassifier.getFilter()).setAttributeIndexes( "" + (i + 1)); break; } } } catch (Exception e) { fail("Problem setting up test for FilteredClassifier: " + e.toString()); } super.testFilteredClassifier(); }
public void testMissing() { ((NominalToString)m_Filter).setAttributeIndexes("5"); Instances result = useFilter(); // Number of attributes and instances shouldn't change assertEquals(m_Instances.numAttributes(), result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); assertEquals("Attribute type should now be STRING", Attribute.STRING, result.attribute(4).type()); assertEquals(4, result.attribute(4).numValues()); for (int i = 0; i < result.numInstances(); i++) { assertTrue("Missing values should be preserved", m_Instances.instance(i).isMissing(4) == result.instance(i).isMissing(4)); } }