in samoa-instances/src/main/java/org/apache/samoa/instances/AvroLoader.java [138:203]
protected Instance readInstanceSparse(GenericRecord record) {
Instance instance = new SparseInstance(1.0, null);
int numAttribute = -1;
ArrayList<Double> attributeValues = new ArrayList<Double>();
List<Integer> indexValues = new ArrayList<Integer>();
for (Attribute attribute : attributes) {
numAttribute++;
Object value = record.get(attribute.name);
boolean isNumeric = attributes.get(numAttribute).isNumeric();
boolean isNominal = attributes.get(numAttribute).isNominal();
/** If value is empty/null iterate to the next attribute. **/
if (value == null)
continue;
if (isNumeric)
{
if (value instanceof Double) {
Double v = (double) value;
//if (Double.isFinite(v))
if (!Double.isNaN(v) && !Double.isInfinite(v))
this.setSparseValue(instance, indexValues, attributeValues, numAttribute, (double) value);
}
else if (value instanceof Long)
this.setSparseValue(instance, indexValues, attributeValues, numAttribute, (long) value);
else if (value instanceof Integer)
this.setSparseValue(instance, indexValues, attributeValues, numAttribute, (int) value);
else
throw new RuntimeException(AVRO_LOADER_INVALID_TYPE_ERROR + " : " + attribute.name);
}
else if (isNominal)
{
double valueAttribute;
if (!(value instanceof EnumSymbol))
throw new RuntimeException(AVRO_LOADER_INVALID_TYPE_ERROR + " : " + attribute.name);
EnumSymbol enumSymbolalue = (EnumSymbol) value;
String stringValue = enumSymbolalue.toString();
if (("?".equals(stringValue)) || (stringValue == null)) {
valueAttribute = Double.NaN;
} else {
valueAttribute = this.instanceInformation.attribute(numAttribute).indexOfValue(stringValue);
}
this.setSparseValue(instance, indexValues, attributeValues, numAttribute, valueAttribute);
}
}
int[] arrayIndexValues = new int[attributeValues.size()];
double[] arrayAttributeValues = new double[attributeValues.size()];
for (int i = 0; i < arrayIndexValues.length; i++) {
arrayIndexValues[i] = indexValues.get(i).intValue();
arrayAttributeValues[i] = attributeValues.get(i).doubleValue();
}
instance.addSparseValues(arrayIndexValues, arrayAttributeValues, this.instanceInformation.numAttributes());
return instance;
}