/** * Copies string/relational values contained in the instance copied to a new * dataset. The Instance must already be assigned to a dataset. This * dataset and the destination dataset must have the same structure. * * @param instance the Instance containing the string/relational * values to copy. * @param isInput if true the input format and input attribute * locators are used otherwise the output format * and output locators */ protected void copyValues(Instance instance, boolean isInput) { RelationalLocator.copyRelationalValues( instance, (isInput) ? m_InputFormat : m_OutputFormat, (isInput) ? m_InputRelAtts : m_OutputRelAtts); StringLocator.copyStringValues( instance, (isInput) ? m_InputFormat : m_OutputFormat, (isInput) ? m_InputStringAtts : m_OutputStringAtts); }
/** * Sets the format of the input instances. * * @param instanceInfo an Instances object containing the input * instance structure (any instances contained in the object are * ignored - only the structure is required). * @return true if the outputFormat may be collected immediately * @throws Exception if the input format can't be set * successfully */ public boolean setInputFormat(Instances instanceInfo) throws Exception { if (instanceInfo.attribute(1).type()!=Attribute.RELATIONAL) { throw new Exception("Can only handle relational-valued attribute!"); } super.setInputFormat(instanceInfo); m_NumBags = instanceInfo.numInstances(); m_NumInstances = 0; for (int i=0; i<m_NumBags; i++) if (instanceInfo.instance(i).relationalValue(1) == null) { m_NumInstances++; } else { m_NumInstances += instanceInfo.instance(i).relationalValue(1).numInstances(); } Attribute classAttribute = (Attribute) instanceInfo.classAttribute().copy(); Attribute bagIndex = (Attribute) instanceInfo.attribute(0).copy(); /* create a new output format (propositional instance format) */ Instances newData = instanceInfo.attribute(1).relation().stringFreeStructure(); newData.insertAttributeAt(bagIndex, 0); newData.insertAttributeAt(classAttribute, newData.numAttributes()); newData.setClassIndex(newData.numAttributes() - 1); super.setOutputFormat(newData.stringFreeStructure()); m_BagStringAtts = new StringLocator(instanceInfo.attribute(1).relation().stringFreeStructure()); m_BagRelAtts = new RelationalLocator(instanceInfo.attribute(1).relation().stringFreeStructure()); return true; }
/** * Sets the format of the input instances. * * @param instanceInfo an Instances object containing the input * instance structure (any instances contained in the object are * ignored - only the structure is required). * @return true if the outputFormat may be collected immediately * @throws Exception if the input format can't be set * successfully */ public boolean setInputFormat(Instances instanceInfo) throws Exception { if (instanceInfo.attribute(0).type()!= Attribute.NOMINAL) { throw new Exception("The first attribute type of the original propositional instance dataset must be Nominal!"); } super.setInputFormat(instanceInfo); /* create a new output format (multi-instance format) */ Instances newData = instanceInfo.stringFreeStructure(); Attribute attBagIndex = (Attribute) newData.attribute(0).copy(); Attribute attClass = (Attribute) newData.classAttribute().copy(); // remove the bagIndex attribute newData.deleteAttributeAt(0); // remove the class attribute newData.setClassIndex(-1); newData.deleteAttributeAt(newData.numAttributes() - 1); FastVector attInfo = new FastVector(3); attInfo.addElement(attBagIndex); attInfo.addElement(new Attribute("bag", newData)); // relation-valued attribute attInfo.addElement(attClass); Instances data = new Instances("Multi-Instance-Dataset", attInfo, 0); data.setClassIndex(data.numAttributes() - 1); super.setOutputFormat(data.stringFreeStructure()); m_BagStringAtts = new StringLocator(data.attribute(1).relation()); m_BagRelAtts = new RelationalLocator(data.attribute(1).relation()); return true; }
/** * adds a new bag out of the given data and adds it to the output * * @param input the intput dataset * @param output the dataset this bag is added to * @param bagInsts the instances in this bag * @param bagIndex the bagIndex of this bag * @param classValue the associated class value * @param bagWeight the weight of the bag */ protected void addBag( Instances input, Instances output, Instances bagInsts, int bagIndex, double classValue, double bagWeight) { // copy strings/relational values for (int i = 0; i < bagInsts.numInstances(); i++) { RelationalLocator.copyRelationalValues( bagInsts.instance(i), false, input, m_InputRelAtts, bagInsts, m_BagRelAtts); StringLocator.copyStringValues( bagInsts.instance(i), false, input, m_InputStringAtts, bagInsts, m_BagStringAtts); } int value = output.attribute(1).addRelation(bagInsts); Instance newBag = new Instance(output.numAttributes()); newBag.setValue(0, bagIndex); newBag.setValue(2, classValue); newBag.setValue(1, value); if (!m_DoNotWeightBags) { newBag.setWeight(bagWeight); } newBag.setDataset(output); output.add(newBag); }
/** * Convert a single bag over. The converted instances is * added to the end of the output queue. * * @param bag the bag to convert */ private void convertInstance(Instance bag) { Instances data = bag.relationalValue(1); int bagSize = 1; if (data != null) { bagSize = data.numInstances(); } double bagIndex = bag.value(0); double classValue = bag.classValue(); double weight = 0.0; //the proper weight for each instance in a bag if (m_WeightMethod == WEIGHTMETHOD_1) weight = 1.0; else if (m_WeightMethod == WEIGHTMETHOD_INVERSE1) weight = (double) 1.0 / bagSize; else if (m_WeightMethod == WEIGHTMETHOD_INVERSE2) weight=(double) m_NumInstances / (m_NumBags * bagSize); else weight = (double) bag.weight() / bagSize; Instance newInst; Instances outputFormat = getOutputFormat().stringFreeStructure(); for (int i = 0; i < bagSize; i++) { newInst = new Instance(outputFormat.numAttributes()); newInst.setDataset(outputFormat); newInst.setValue(0,bagIndex); if (!bag.classIsMissing()) newInst.setClassValue(classValue); // copy the attribute values to new instance for (int j = 1; j < outputFormat.numAttributes() - 1; j++){ if (data == null) { newInst.setMissing(j); } else { newInst.setValue(j,data.instance(i).value(j - 1)); } } newInst.setWeight(weight); // copy strings/relational values StringLocator.copyStringValues( newInst, false, data, m_BagStringAtts, outputFormat, m_OutputStringAtts); RelationalLocator.copyRelationalValues( newInst, false, data, m_BagRelAtts, outputFormat, m_OutputRelAtts); push(newInst); } }
/** * Copies string/relational values contained in the instance copied to a new * dataset. The Instance must already be assigned to a dataset. This dataset * and the destination dataset must have the same structure. * * @param instance the Instance containing the string/relational values to * copy. * @param isInput if true the input format and input attribute locators are * used otherwise the output format and output locators */ protected void copyValues(Instance instance, boolean isInput) { RelationalLocator.copyRelationalValues(instance, (isInput) ? m_InputFormat : m_OutputFormat, (isInput) ? m_InputRelAtts : m_OutputRelAtts); StringLocator.copyStringValues(instance, (isInput) ? m_InputFormat : m_OutputFormat, (isInput) ? m_InputStringAtts : m_OutputStringAtts); }
/** * Takes string/relational values referenced by an Instance and copies them * from a source dataset to a destination dataset. The instance references are * updated to be valid for the destination dataset. The instance may have the * structure (i.e. number and attribute position) of either dataset (this * affects where references are obtained from). Only works if the number of * string/relational attributes is the same in both indices (implicitly these * string/relational attributes should be semantically same but just with * shifted positions). * * @param instance the instance containing references to strings/ relational * values in the source dataset that will have references updated to * be valid for the destination dataset. * @param instSrcCompat true if the instance structure is the same as the * source, or false if it is the same as the destination (i.e. which * of the string/relational attribute indices contains the correct * locations for this instance). * @param srcDataset the dataset for which the current instance * string/relational value references are valid (after any position * mapping if needed) * @param destDataset the dataset for which the current instance * string/relational value references need to be inserted (after any * position mapping if needed) */ protected void copyValues(Instance instance, boolean instSrcCompat, Instances srcDataset, Instances destDataset) { RelationalLocator.copyRelationalValues(instance, instSrcCompat, srcDataset, m_InputRelAtts, destDataset, m_OutputRelAtts); StringLocator.copyStringValues(instance, instSrcCompat, srcDataset, m_InputStringAtts, getOutputFormat(), m_OutputStringAtts); }
/** * Takes string/relational values referenced by an Instance and copies them * from a source dataset to a destination dataset. The instance references are * updated to be valid for the destination dataset. The instance may have the * structure (i.e. number and attribute position) of either dataset (this * affects where references are obtained from). Only works if the number * of string/relational attributes is the same in both indices (implicitly * these string/relational attributes should be semantically same but just * with shifted positions). * * @param instance the instance containing references to strings/ * relational values in the source dataset that * will have references updated to be valid for * the destination dataset. * @param instSrcCompat true if the instance structure is the same as * the source, or false if it is the same as the * destination (i.e. which of the string/relational * attribute indices contains the correct locations * for this instance). * @param srcDataset the dataset for which the current instance * string/relational value references are valid * (after any position mapping if needed) * @param destDataset the dataset for which the current instance * string/relational value references need to be * inserted (after any position mapping if needed) */ protected void copyValues(Instance instance, boolean instSrcCompat, Instances srcDataset, Instances destDataset) { RelationalLocator.copyRelationalValues( instance, instSrcCompat, srcDataset, m_InputRelAtts, destDataset, m_OutputRelAtts); StringLocator.copyStringValues( instance, instSrcCompat, srcDataset, m_InputStringAtts, getOutputFormat(), m_OutputStringAtts); }