ClusterQuantiser { public static byte[][] do_getSamples(ClusterQuantiserOptions options) throws IOException { byte[][] data = null; if (options.isSamplesFileMode()) { data = options.getSampleKeypoints(); } else { final List<File> input_files = options.getInputFiles(); final FileType type = options.getFileType(); final int n_input_files = input_files.size(); final List<Header> headers = new ArrayList<Header>(n_input_files); System.err.printf("Reading input %8d / %8d", 0, n_input_files); int totalFeatures = 0; final int[] cumSum = new int[n_input_files + 1]; for (int i = 0; i < n_input_files; i++) { final Header h = type.readHeader(input_files.get(i)); totalFeatures += h.nfeatures; cumSum[i + 1] = totalFeatures; headers.add(h); System.err.printf("\r%8d / %8d", i + 1, n_input_files); } System.err.println(); final int samples = options.getSamples(); if (samples <= 0 || samples > totalFeatures) { System.err .printf("Samples requested %d larger than total samples %d...\n", samples, totalFeatures); data = new byte[totalFeatures][]; for (int i = 0, j = 0; i < n_input_files; i++) { final byte[][] fd = type.readFeatures(input_files.get(i)); for (int k = 0; k < fd.length; k++) { data[j + k] = fd[k]; System.err.printf("\r%8d / %8d", j, totalFeatures); } j += fd.length; } } else { System.err.println("Shuffling and sampling ..."); data = new byte[samples][]; int[] rndIndices = null; if (options.getRandomSeed() == -1) rndIndices = RandomData.getUniqueRandomInts(samples, 0, totalFeatures); else rndIndices = RandomData.getUniqueRandomInts(samples, 0, totalFeatures, new Random(options.getRandomSeed())); System.err.println("Done! Extracting features required"); final TIntArrayList intraFileIndices = new TIntArrayList(); for (int j = 0, s = 0; j < n_input_files; j++) { intraFileIndices.clear(); for (int i = 0; i < samples; i++) { final int idx = rndIndices[i]; if (idx >= cumSum[j] && idx < cumSum[j + 1]) { intraFileIndices.add(idx - cumSum[j]); } } if (intraFileIndices.size() > 0) { final byte[][] f = type.readFeatures(input_files.get(j), intraFileIndices.toArray()); for (int i = 0; i < intraFileIndices.size(); i++, s++) { data[s] = f[i]; System.err .printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%8d / %8d", s + 1, samples); } } } System.err.println(); } if (data != null && options.getSamplesFile() != null) { System.err.println("Writing samples file..."); final FileOutputStream fos = new FileOutputStream( options.getSamplesFile()); final ObjectOutputStream dos = new ObjectOutputStream(fos); dos.writeObject(data); dos.close(); } } return data; }  static SpatialClusters<?> do_create(ClusterQuantiserOptions options); static List<SampleBatch> do_getSampleBatches(ClusterQuantiserOptions options); static byte[][] do_getSamples(ClusterQuantiserOptions options); static void do_info(AbstractClusterQuantiserOptions options); static void do_quant(ClusterQuantiserOptions cqo); static ClusterQuantiserOptions mainOptions(String[] args); static void main(String[] args);  }
@Test public void testSamplesFile() throws CmdLineException, IOException { final File samplesOutFile = folder.newFile("samples-testSamplesFile.out"); samplesOutFile.delete(); String[] args = new String[] { "-t", "LOWE_KEYPOINT_ASCII", "-k", "2", "-d", "1", "-s", "5", "-sf", samplesOutFile .getAbsolutePath(), keyFiles[0], keyFiles[1], keyFiles[2] }; ClusterQuantiserOptions options = new ClusterQuantiserOptions(args); options.prepare(); final byte[][] samples = ClusterQuantiser.do_getSamples(options); args = new String[] { "-ct", "HKMEANS", "-k", "2", "-d", "1", "-c", folder.newFile("HKMEANS-testSamplesFile.voc") .getAbsolutePath(), "-sf", samplesOutFile.getAbsolutePath() }; options = new ClusterQuantiserOptions(args); options.prepare(); options.loadSamplesFile(); final byte[][] gotSamples = options.getSampleKeypoints(); for (int i = 0; i < samples.length; i++) { for (int j = 0; j < samples[i].length; j++) { assertTrue(samples[i][j] == gotSamples[i][j]); } } }