📄 capabilities.java
字号:
/** * Generates the message for, e.g., an exception. Adds the classname before the * actual message and returns that string. * * @param msg the actual content of the message, e.g., exception * @return the new message */ protected String createMessage(String msg) { String result; result = ""; if (getOwner() != null) result = getOwner().getClass().getName(); else result = "<anonymous>"; result += ": " + msg; return result; } /** * Test the given attribute, whether it can be processed by the handler, * given its capabilities. The method assumes that the specified attribute * is not the class attribute. * * @param att the attribute to test * @return true if all the tests succeeded * @see #test(Attribute, boolean) */ public boolean test(Attribute att) { return test(att, false); } /** * Test the given attribute, whether it can be processed by the handler, * given its capabilities. * * @param att the attribute to test * @param isClass whether this attribute is the class attribute * @return true if all the tests succeeded * @see #m_AttributeTest */ public boolean test(Attribute att, boolean isClass) { boolean result; Capability cap; Capability capBinary; Capability capUnary; Capability capEmpty; String errorStr; result = true; // shall we test the data? if (!m_AttributeTest) return result; // for exception if (isClass) errorStr = "class"; else errorStr = "attributes"; switch (att.type()) { case Attribute.NOMINAL: if (isClass) { cap = Capability.NOMINAL_CLASS; capBinary = Capability.BINARY_CLASS; capUnary = Capability.UNARY_CLASS; capEmpty = Capability.EMPTY_NOMINAL_CLASS; } else { cap = Capability.NOMINAL_ATTRIBUTES; capBinary = Capability.BINARY_ATTRIBUTES; capUnary = Capability.UNARY_ATTRIBUTES; capEmpty = Capability.EMPTY_NOMINAL_ATTRIBUTES; } if (handles(cap) && (att.numValues() > 2)) break; else if (handles(capBinary) && (att.numValues() == 2)) break; else if (handles(capUnary) && (att.numValues() == 1)) break; else if (handles(capEmpty) && (att.numValues() == 0)) break; if (att.numValues() == 0) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle empty nominal " + errorStr + "!")); result = false; } if (att.numValues() == 1) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle unary " + errorStr + "!")); result = false; } else if (att.numValues() == 2) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle binary " + errorStr + "!")); result = false; } else { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle nominal " + errorStr + "!")); result = false; } break; case Attribute.NUMERIC: if (isClass) cap = Capability.NUMERIC_CLASS; else cap = Capability.NUMERIC_ATTRIBUTES; if (!handles(cap)) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle numeric " + errorStr + "!")); result = false; } break; case Attribute.DATE: if (isClass) cap = Capability.DATE_CLASS; else cap = Capability.DATE_ATTRIBUTES; if (!handles(cap)) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle date " + errorStr + "!")); result = false; } break; case Attribute.STRING: if (isClass) cap = Capability.STRING_CLASS; else cap = Capability.STRING_ATTRIBUTES; if (!handles(cap)) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle string " + errorStr + "!")); result = false; } break; case Attribute.RELATIONAL: if (isClass) cap = Capability.RELATIONAL_CLASS; else cap = Capability.RELATIONAL_ATTRIBUTES; if (!handles(cap)) { m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle relational " + errorStr + "!")); result = false; } // attributes in the relation of this attribute must be tested // separately with a different Capabilites object break; default: m_FailReason = new UnsupportedAttributeTypeException( createMessage("Cannot handle unknown attribute type '" + att.type() + "'!")); result = false; } return result; } /** * Tests the given data, whether it can be processed by the handler, * given its capabilities. Classifiers implementing the * <code>MultiInstanceCapabilitiesHandler</code> interface are checked * automatically for their multi-instance Capabilities (if no bags, then * only the bag-structure, otherwise only the first bag). * * @param data the data to test * @return true if all the tests succeeded * @see #test(Instances, int, int) */ public boolean test(Instances data) { return test(data, 0, data.numAttributes() - 1); } /** * Tests a certain range of attributes of the given data, whether it can be * processed by the handler, given its capabilities. Classifiers * implementing the <code>MultiInstanceCapabilitiesHandler</code> interface * are checked automatically for their multi-instance Capabilities (if no * bags, then only the bag-structure, otherwise only the first bag). * * @param data the data to test * @param fromIndex the range of attributes - start (incl.) * @param toIndex the range of attributes - end (incl.) * @return true if all the tests succeeded * @see MultiInstanceCapabilitiesHandler * @see #m_InstancesTest * @see #m_MissingValuesTest * @see #m_MissingClassValuesTest * @see #m_MinimumNumberInstancesTest */ public boolean test(Instances data, int fromIndex, int toIndex) { int i; int n; int m; Attribute att; Instance inst; boolean testClass; Capabilities cap; boolean missing; Iterator iter; // shall we test the data? if (!m_InstancesTest) return true; // no Capabilities? -> warning if ( (m_Capabilities.size() == 0) || ((m_Capabilities.size() == 1) && handles(Capability.NO_CLASS)) ) System.err.println(createMessage("No capabilities set!")); // any attributes? if (toIndex - fromIndex < 0) { m_FailReason = new WekaException( createMessage("No attributes!")); return false; } // do wee need to test the class attribute, i.e., is the class attribute // within the range of attributes? testClass = (data.classIndex() > -1) && (data.classIndex() >= fromIndex) && (data.classIndex() <= toIndex); // attributes for (i = fromIndex; i <= toIndex; i++) { att = data.attribute(i); // class is handled separately if (i == data.classIndex()) continue; // check attribute types if (!test(att)) return false; } // class if (!handles(Capability.NO_CLASS) && (data.classIndex() == -1)) { m_FailReason = new UnassignedClassException( createMessage("Class attribute not set!")); return false; } // special case: no class attribute can be handled if (handles(Capability.NO_CLASS) && (data.classIndex() > -1)) { cap = getClassCapabilities(); cap.disable(Capability.NO_CLASS); iter = cap.capabilities(); if (!iter.hasNext()) { m_FailReason = new WekaException( createMessage("Cannot handle any class attribute!")); return false; } } if (testClass && !handles(Capability.NO_CLASS)) { att = data.classAttribute(); if (!test(att, true)) return false; // special handling of RELATIONAL class // TODO: store additional Capabilities for this case // missing class labels if (m_MissingClassValuesTest) { if (!handles(Capability.MISSING_CLASS_VALUES)) { for (i = 0; i < data.numInstances(); i++) { if (data.instance(i).classIsMissing()) { m_FailReason = new WekaException( createMessage("Cannot handle missing class values!")); return false; } } } else { if (m_MinimumNumberInstancesTest) { int hasClass = 0; for (i = 0; i < data.numInstances(); i++) { if (!data.instance(i).classIsMissing()) hasClass++; } // not enough instances with class labels? if (hasClass < getMinimumNumberInstances()) { m_FailReason = new WekaException( createMessage("Not enough training instances with class labels (required: " + getMinimumNumberInstances() + ", provided: " + hasClass + ")!")); return false; } } } } } // missing values if (m_MissingValuesTest) { if (!handles(Capability.MISSING_VALUES)) { missing = false; for (i = 0; i < data.numInstances(); i++) { inst = data.instance(i); if (inst instanceof SparseInstance) { for (m = 0; m < inst.numValues(); m++) { n = inst.index(m); // out of scope? if (n < fromIndex) continue; if (n > toIndex) break; // skip class if (n == inst.classIndex()) continue; if (inst.isMissing(n)) { missing = true; break; } } } else { for (n = fromIndex; n <= toIndex; n++) { // skip class if (n == inst.classIndex()) continue; if (inst.isMissing(n)) { missing = true; break; } } } if (missing) { m_FailReason = new NoSupportForMissingValuesException( createMessage("Cannot handle missing values!")); return false; } } } } // instances if (m_MinimumNumberInstancesTest) { if (data.numInstances() < getMinimumNumberInstances()) { m_FailReason = new WekaException( createMessage("Not enough training instances (required: " + getMinimumNumberInstances() + ", provided: " + data.numInstances() + ")!")); return false; } } // Multi-Instance? -> check structure (regardless of attribute range!) if (handles(Capability.ONLY_MULTIINSTANCE)) { // number of attributes? if (data.numAttributes() != 3) { m_FailReason = new WekaException( createMessage("Incorrect Multi-Instance format, must be 'bag-id, bag, class'!")); return false; } // type of attributes and position of class? if ( !data.attribute(0).isNominal() || !data.attribute(1).isRelationValued() || (data.classIndex() != data.numAttributes() - 1) ) { m_FailReason = new WekaException( createMessage("Incorrect Multi-Instance format, must be 'NOMINAL att, RELATIONAL att, CLASS att'!")); return false; } // check data immediately if (getOwner() instanceof MultiInstanceCapabilitiesHandler) { MultiInstanceCapabilitiesHandler handler = (MultiInstanceCapabilitiesHandler) getOwner(); cap = handler.getMultiInstanceCapabilities(); boolean result; if (data.numInstances() > 0) result = cap.test(data.attribute(1).relation(0)); else result = cap.test(data.attribute(1).relation());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -