@Override public void map(Text key, BytesWritable value, Context context) throws IOException, InterruptedException { final byte[] values = value.getBytes(); final byte[] points = new byte[value.getLength()]; System.arraycopy(values, 0, points, 0, points.length); final int cluster = assigner.assign(points); context.write(new IntWritable(cluster), new BytesWritable(points)); if (new Random().nextDouble() < randomFallbackChance) { context.write(new IntWritable(k + 1), new BytesWritable(points)); } } }
@Override public void assignDistance(DATATYPE[] data, int[] indices, float[] distances) { internalAssigner.assignDistance(data, indices, distances); for (int i = 0; i < data.length; i++) { if (!allow(distances[i])) { distances[i] = Float.NaN; indices[i] = -1; } } }
@Override public int size() { return this.internalAssigner.size(); }
@Override public int numDimensions() { return this.internalAssigner.numDimensions(); } }
/** * Perform clustering on the given data. * * @param data * the data. * * @return the generated clusters. */ public int[][] performClustering(List<T> data) { @SuppressWarnings("unchecked") T[] d = (T[]) Array.newInstance(data.get(0).getClass(), data.size()); d = data.toArray(d); final FeatureVectorCentroidsResult<T> clusters = this.cluster(d); return new IndexClusters(clusters.defaultHardAssigner().assign(d)).clusters(); }
@Override public IntFloatPair assignDistance(DATATYPE data) { final IntFloatPair res = internalAssigner.assignDistance(data); if (!allow(res.second)) { res.second = Float.NaN; res.first = -1; } return res; }
/** * Utility method to construct a list of quantised local features (local * features with visual word assignments) from a list of features and a * {@link HardAssigner}. * * @param assigner * the assigner to apply to the feature vectors to create the * visual word identifiers * @param features * the features to process * @return a list of features with visual word assignments */ public static <L extends Location, T> List<QuantisedLocalFeature<L>> computeQuantisedFeatures( HardAssigner<T, ?, ?> assigner, List<? extends LocalFeature<L, ? extends ArrayFeatureVector<T>>> features) { final List<QuantisedLocalFeature<L>> out = new ArrayList<QuantisedLocalFeature<L>>(features.size()); for (final LocalFeature<L, ? extends ArrayFeatureVector<T>> f : features) { final int idx = assigner.assign(f.getFeatureVector().values); out.add(new QuantisedLocalFeature<L>(f.getLocation(), idx)); } return out; }
/** * Aggregate the given features into a vector. * * @param features * the features to aggregate * @return the aggregated vector */ public SparseIntFV aggregateVectorsRaw(List<T> features) { final SparseIntFV fv = new SparseIntFV(this.assigner.size()); for (final T f : features) { final int idx = assigner.assign(f); fv.values.increment(idx, 1); } return fv; } }
IntFloatPair best = assigner.assignDistance(data[i]);
/** * Utility method to construct a list of quantised local features (local * features with visual word assignments) from a list of features and a * {@link HardAssigner}. * * @param assigner * the assigner to apply to the feature vectors to create the * visual word identifiers * @param features * the features to process * @return a list of features with visual word assignments */ public static <L extends Location, T> List<QuantisedLocalFeature<L>> computeQuantisedFeatures( HardAssigner<T, ?, ?> assigner, List<? extends LocalFeature<L, ? extends ArrayFeatureVector<T>>> features) { final List<QuantisedLocalFeature<L>> out = new ArrayList<QuantisedLocalFeature<L>>(features.size()); for (final LocalFeature<L, ? extends ArrayFeatureVector<T>> f : features) { final int idx = assigner.assign(f.getFeatureVector().values); out.add(new QuantisedLocalFeature<L>(f.getLocation(), idx)); } return out; }
/** * Aggregate the given features into a vector. * * @param features * the features to aggregate * @return the aggregated vector */ public SparseIntFV aggregateVectorsRaw(List<T> features) { final SparseIntFV fv = new SparseIntFV(this.assigner.size()); for (final T f : features) { final int idx = assigner.assign(f); fv.values.increment(idx, 1); } return fv; } }
IntDoublePair best = assigner.assignDistance(data[i]);
@Override public int[][] assign(long[][] data) { int[][] assignments = new int[data.length][result.getDepth()]; for (int i = 0; i < data.length; i++) { Node node = result.getRoot(); int d = 0; while (node != null) { HardAssigner<long[], double[], IntDoublePair> assigner = assigners.get(node.result); if (assigner == null) { assigner = new ExactLongAssigner(node.result); assigners.put(node.result, assigner); } int best = assigner.assign(data[i]); assignments[i][d] = best; ++d; if (node.children == null) break; node = node.children[best]; } } return assignments; }
@Override public SparseIntFV aggregate(List<? extends LocalFeature<?, ? extends ArrayFeatureVector<T>>> features) { final SparseIntFV fv = new SparseIntFV(this.assigner.size()); for (final LocalFeature<?, ? extends ArrayFeatureVector<T>> f : features) { final int idx = assigner.assign(f.getFeatureVector().values); fv.values.increment(idx, 1); } return fv; }
IntFloatPair best = assigner.assignDistance(data[i]);
@Override public int[][] assign(byte[][] data) { int[][] assignments = new int[data.length][result.getDepth()]; for (int i = 0; i < data.length; i++) { Node node = result.getRoot(); int d = 0; while (node != null) { HardAssigner<byte[], float[], IntFloatPair> assigner = assigners.get(node.result); if (assigner == null) { assigner = new ExactByteAssigner(node.result); assigners.put(node.result, assigner); } int best = assigner.assign(data[i]); assignments[i][d] = best; ++d; if (node.children == null) break; node = node.children[best]; } } return assignments; }
@Override public SparseIntFV aggregate(List<? extends LocalFeature<?, ? extends ArrayFeatureVector<T>>> features) { final SparseIntFV fv = new SparseIntFV(this.assigner.size()); for (final LocalFeature<?, ? extends ArrayFeatureVector<T>> f : features) { final int idx = assigner.assign(f.getFeatureVector().values); fv.values.increment(idx, 1); } return fv; }
IntDoublePair best = assigner.assignDistance(data[i]);