use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.
the class SpatialDistance method getSimilar.
/**
* Returns similar <i>objects</i> to the feature data contained in the given segment container.
*/
@Override
public List<ScoreElement> getSimilar(SegmentContainer sc, ReadableQueryConfig qc) {
final QueryConfig mqc = QueryConfig.clone(qc);
if (sc instanceof ParameterisedLocationQueryTermContainer) {
if (((ParameterisedLocationQueryTermContainer) sc).getParameter().isPresent()) {
final String param = ((ParameterisedLocationQueryTermContainer) sc).getParameter().get();
mqc.setCorrespondenceFunctionIfEmpty(CorrespondenceFunction.hyperbolic(parseAndEvaluateHalfSimilarityDistance(param)));
}
}
return this.extractFeature(sc).map(ReadableFloatVector::toArray).map(array -> this.getSimilar(array, new ReadableQueryConfig(mqc))).orElse(Collections.emptyList());
}
use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.
the class KMeansPP method kMeansPP.
public static <T extends ReadableFloatVector> KMenasResult<T> kMeansPP(List<T> elements, FloatVector helper, int k, double minDist, long randomSeed) {
// init centers
ArrayList<FloatVector> centers = getSeeds(elements, k, randomSeed);
// init clusters
ArrayList<ArrayList<T>> points = new ArrayList<ArrayList<T>>(k);
for (int i = 0; i < k; ++i) {
points.add(new ArrayList<T>(elements.size() / 2));
}
int iter = 0, maxIter = 50;
double dist = 0;
do {
for (int i = 0; i < k; ++i) {
points.get(i).clear();
}
for (T element : elements) {
int j = 0;
double min = ReadableFloatVector.getEuclideanDistance(element, centers.get(0));
for (int i = 1; i < centers.size(); ++i) {
double d = ReadableFloatVector.getEuclideanDistance(element, centers.get(i));
if (d < min) {
min = d;
j = i;
}
}
points.get(j).add(element);
}
dist = 0;
for (int i = 0; i < centers.size(); ++i) {
helper = ColorUtils.getAvg(points.get(i), helper);
FloatVector center = centers.get(i);
dist += ReadableFloatVector.getEuclideanDistance(helper, center);
for (int j = 0; j < center.getElementCount(); ++j) {
center.setElement(j, helper.getElement(j));
}
centers.set(i, center);
}
++iter;
} while (dist > minDist && iter < maxIter);
KMenasResult<T> result = new KMenasResult<T>(k);
result.setCenters(centers);
result.setPoints(points);
result.sort();
return result;
}
use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.
the class CottontailWriter method persist.
@Override
public boolean persist(List<PersistentTuple> tuples) {
long start = System.currentTimeMillis();
int size = tuples.size();
final long txId = this.cottontail.client.begin();
try {
BatchInsert insert = new BatchInsert().into(this.fqn).columns(this.names).txId(txId);
while (!tuples.isEmpty()) {
final PersistentTuple tuple = tuples.remove(0);
final Object[] values = tuple.getElements().stream().map(o -> {
if (o instanceof ReadableFloatVector) {
return ReadableFloatVector.toArray((ReadableFloatVector) o);
} else {
return o;
}
}).toArray();
insert.append(values);
if (insert.size() >= Constants.MAX_PAGE_SIZE_BYTES) {
LOGGER.trace("Inserting msg of size {} into {}", insert.size(), this.fqn);
this.cottontail.client.insert(insert);
insert = new BatchInsert().into(this.fqn).columns(this.names);
}
}
if (insert.getBuilder().getInsertsCount() > 0) {
LOGGER.trace("Inserting msg of size {} into {}", insert.size(), this.fqn);
this.cottontail.client.insert(insert);
}
this.cottontail.client.commit(txId);
long stop = System.currentTimeMillis();
LOGGER.trace("Completed insert of {} elements in {} ms", size, stop - start);
return true;
} catch (StatusRuntimeException e) {
this.cottontail.client.rollback(txId);
return false;
}
}
use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.
the class JsonFileWriter method getPersistentRepresentation.
@Override
public JsonObject getPersistentRepresentation(PersistentTuple tuple) {
int nameIndex = 0;
JsonObject _return = new JsonObject();
for (Object o : tuple.getElements()) {
if (o instanceof float[]) {
_return.add(names[nameIndex++], toArray((float[]) o));
} else if (o instanceof ReadableFloatVector) {
_return.add(names[nameIndex++], toArray(ReadableFloatVector.toArray((ReadableFloatVector) o)));
} else if (o instanceof int[]) {
_return.add(names[nameIndex++], toArray((int[]) o));
} else if (o instanceof boolean[]) {
_return.add(names[nameIndex++], toArray((boolean[]) o));
} else if (o instanceof Integer) {
_return.add(names[nameIndex++], new JsonPrimitive((int) o));
} else if (o instanceof Float) {
_return.add(names[nameIndex++], new JsonPrimitive((float) o));
} else if (o instanceof Long) {
_return.add(names[nameIndex++], new JsonPrimitive((long) o));
} else if (o instanceof Double) {
_return.add(names[nameIndex++], new JsonPrimitive((double) o));
} else if (o instanceof Boolean) {
_return.add(names[nameIndex++], new JsonPrimitive((boolean) o));
} else {
_return.add(names[nameIndex++], new JsonPrimitive(o.toString()));
}
}
return _return;
}
use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.
the class CottontailWriter method getPersistentRepresentation.
@Override
public Insert getPersistentRepresentation(PersistentTuple tuple) {
final Insert insert = new Insert(this.fqn);
int index = 0;
for (Object o : tuple.getElements()) {
if (o instanceof ReadableFloatVector) {
insert.value(this.names[index++], ReadableFloatVector.toArray((ReadableFloatVector) o));
} else {
insert.value(this.names[index++], o);
}
}
return insert;
}
Aggregations