All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.TKnudsen.ComplexDataObject.data.features.FeatureContainer Maven / Gradle / Ivy

Go to download

A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.

There is a newer version: 0.2.13
Show newest version
package com.github.TKnudsen.ComplexDataObject.data.features;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.github.TKnudsen.ComplexDataObject.data.interfaces.IKeyValueProvider;

/**
 * 

* Title: FeatureContainer *

* *

* Description: Stores and manages collections of Feature Vectors. A * FeatureSchema manages the features of the collection. *

* *

* Copyright: Copyright (c) 2016-2017 *

* * @author Juergen Bernard * @version 1.03 */ public class FeatureContainer, T extends AbstractFeatureVector> implements Iterable { private Map featureVectorMap = new HashMap(); protected Map> featureValues = new HashMap>(); protected FeatureSchema featureSchema; public FeatureContainer(FeatureSchema featureSchema) { this.featureSchema = featureSchema; } public FeatureContainer(Map featureVectorMap) { this.featureVectorMap = featureVectorMap; featureSchema = new FeatureSchema(); for (Long ID : featureVectorMap.keySet()) extendDataSchema(featureVectorMap.get(ID)); } public FeatureContainer(Iterable objects) { featureSchema = new FeatureSchema(); for (T object : objects) { featureVectorMap.put(object.getID(), object); extendDataSchema(object); } } private void extendDataSchema(T object) { for (String feature : object.getFeatureKeySet()) if (!featureSchema.contains(feature)) featureSchema.add(feature, object.getFeature(feature).getFeatureValue().getClass(), object.getFeature(feature).getFeatureType()); } /** * Introduces or updates a new feature. * * @param featureName * the feature name * @param type * the expected data type. * @param defaultValue * the default value in case the feature is missing from a data * object. * @return the data schema instance for call-chaining. */ public FeatureSchema addFeature(F feature) { featureValues = new HashMap>(); featureSchema.add(feature.getFeatureName(), feature.getFeatureValue().getClass(), feature.getFeatureType()); Iterator objectIterator = iterator(); while (objectIterator.hasNext()) { T next = objectIterator.next(); if (next.getFeature(feature.getFeatureName()) == null) next.addFeature(feature.getFeatureName(), null, feature.getFeatureType()); } return featureSchema; } /** * Introduces or updates a feature. * * @param featureName * the feature name * @param type * the expected data type. * @param defaultValue * the default value in case the feature is missing from a data * object. * @return the data schema instance for call-chaining. */ public FeatureSchema addFeature(String featureName, Class featureClass, FeatureType featureType) { featureValues = new HashMap>(); featureSchema.add(featureName, featureClass, featureType); Iterator objectIterator = iterator(); while (objectIterator.hasNext()) { T next = objectIterator.next(); if (next.getFeature(featureName) == null) next.addFeature(featureName, null, featureType); } return featureSchema; } /** * Remove functionality. For test purposes. Maybe this functionality will be * removed sometime. * * @param featureVector * @return */ public boolean remove(T featureVector) { if (featureVector == null) return false; long id = featureVector.getID(); if (!featureVectorMap.containsKey(id)) return false; for (String featureName : featureValues.keySet()) { if (featureValues.get(featureName) != null) featureValues.get(featureName).remove(id); } featureVectorMap.remove(id); return true; } /** * Removes a feature from the container and the set of objects. * * @param featureName * the feature name. * @return the data schema instance for call-chaining. */ public FeatureSchema remove(String featureName) { Iterator iterator = iterator(); while (iterator.hasNext()) { T o = iterator.next(); o.removeFeature(featureName); } return featureSchema.remove(featureName); } @Override public Iterator iterator() { return featureVectorMap.values().iterator(); } public Boolean isNumeric(String featureName) { if (Number.class.isAssignableFrom(featureSchema.getType(featureName))) return true; return false; } public Boolean isBoolean(String feature) { if (Boolean.class.isAssignableFrom(featureSchema.getType(feature))) return true; return false; } public Collection getFeatureNames() { return featureSchema.getFeatureNames(); } public Map getFeatureValues(String featureName) { if (featureValues.get(featureName) == null) { calculateEntities(featureName); } return featureValues.get(featureName); } public boolean contains(T featureVector) { if (featureVectorMap.containsKey(featureVector.getID())) return true; return false; } private void calculateEntities(String featureName) { Map ent = new HashMap(); Iterator iterator = iterator(); while (iterator.hasNext()) { T o = iterator.next(); if (o instanceof IKeyValueProvider) ent.put(o.getID(), o.getFeature(featureName)); } this.featureValues.put(featureName, ent); } @Override public String toString() { if (featureSchema == null) return super.toString(); return featureSchema.toString(); } public int size() { if (featureVectorMap == null) return 0; return featureVectorMap.size(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy