MLStructure mlStructure = (MLStructure) mfr.getMLArray(varName); xhtml.startElement("ul"); xhtml.newline();
MatFileReader matfilereader = new MatFileReader("test_batch.mat"); double[][] mlArrayDouble = ((MLDouble) matfilereader.getMLArray("data")).getArray();
public static void main(String[] args) throws IOException { final Map<Integer, String> database = readDatabase(); final File outbase = new File("/Users/jon/Data/lfw/matlab-fvs/"); for (int i = 1; i <= 128; i++) { final File chunk = new File( "/Users/jon/Downloads/data/lfw_aligned/SIFT_1pix_PCA64_GMM512/features/poolfv/1/", String.format( "feat_%d-v6.mat", i)); System.out.println(chunk); final MatFileReader reader = new MatFileReader(chunk); final MLSingle feats = (MLSingle) reader.getMLArray("chunk"); final MLDouble index = (MLDouble) reader.getMLArray("index"); for (int j = 0; j < index.getN(); j++) { final int id = (int) (double) index.get(0, j); final File outfile = new File(outbase, database.get(id).replace(".jpg", ".bin")); outfile.getParentFile().mkdirs(); final float[] vec = new float[feats.getM()]; for (int k = 0; k < feats.getM(); k++) { vec[k] = feats.get(k, j); } final FloatFV fv = new FloatFV(vec); IOUtils.writeBinary(outfile, fv); } } }
public static void main(String[] args) throws IOException { final Map<Integer, String> database = readDatabase(); final File outbase = new File("/Users/jon/Data/lfw/matlab-fvs/"); for (int i = 1; i <= 128; i++) { final File chunk = new File( "/Users/jon/Downloads/data/lfw_aligned/SIFT_1pix_PCA64_GMM512/features/poolfv/1/", String.format( "feat_%d-v6.mat", i)); System.out.println(chunk); final MatFileReader reader = new MatFileReader(chunk); final MLSingle feats = (MLSingle) reader.getMLArray("chunk"); final MLDouble index = (MLDouble) reader.getMLArray("index"); for (int j = 0; j < index.getN(); j++) { final int id = (int) (double) index.get(0, j); final File outfile = new File(outbase, database.get(id).replace(".jpg", ".bin")); outfile.getParentFile().mkdirs(); final float[] vec = new float[feats.getM()]; for (int k = 0; k < feats.getM(); k++) { vec[k] = feats.get(k, j); } final FloatFV fv = new FloatFV(vec); IOUtils.writeBinary(outfile, fv); } } }
@Test public void testStructureFromMatlabCreatedFile() throws IOException { // array name File file = getTestFile("simplestruct.mat"); MatFileReader reader = new MatFileReader(file); MLArray mlArray = reader.getMLArray("structure"); List<MLArray> towrite = Arrays.asList(mlArray); new MatFileWriter(getTempFile("simplestructcopy.mat"), towrite); reader = new MatFileReader(getTempFile("simplestructcopy.mat")); reader.getMLArray("structure"); }
@Test public void testInt64() throws Exception { String fileName = "int64.mat"; String arrName = "arr"; MatFileReader mfr; MLArray src; Long max = Long.parseLong("9223372036854775807"); Long min = Long.parseLong("-9223372036854775808"); // read array form file mfr = new MatFileReader(getTestFile(fileName)); assertEquals("Test min. value from file:" + fileName + " array: " + arrName, min, ((MLInt64) mfr.getMLArray("arr")).get(0, 0)); assertEquals("Test max. value from file:" + fileName + " array: " + arrName, max, ((MLInt64) mfr.getMLArray("arr")).get(0, 1)); src = mfr.getMLArray("arr"); // write fileName = "int64out.mat"; ArrayList<MLArray> towrite = new ArrayList<MLArray>(); towrite.add(mfr.getMLArray(arrName)); new MatFileWriter(getTempFile(fileName), towrite); // read again mfr = new MatFileReader(getTempFile(fileName)); assertEquals("Test min. value from file:" + fileName + " array: " + arrName, min, ((MLInt64) mfr.getMLArray(arrName)).get(0, 0)); assertEquals("Test max. value from file:" + fileName + " array: " + arrName, max, ((MLInt64) mfr.getMLArray(arrName)).get(0, 1)); assertEquals("Test if array retrieved from " + fileName + " equals source array", src, mfr.getMLArray(arrName)); }
@Test public void testSparseFromMatlabCreatedFile() throws IOException { // array name File file = getTestFile("sparse.mat"); MatFileReader reader = new MatFileReader(file); MLArray mlArray = reader.getMLArray("spa"); List<MLArray> towrite = Arrays.asList(mlArray); new MatFileWriter(getTempFile("sparsecopy.mat"), towrite); reader = new MatFileReader(getTempFile("sparsecopy.mat")); MLArray mlArrayRetrieved = reader.getMLArray("spa"); assertEquals(mlArray, mlArrayRetrieved); }
@Test public void testUInt64() throws Exception { String fileName = "uint64.mat"; String arrName = "arr"; MatFileReader mfr; MLArray src; Long max = Long.MAX_VALUE; Long min = Long.parseLong("0"); // read array form file mfr = new MatFileReader(getTestFile(fileName)); assertEquals("Test min. value from file:" + fileName + " array: " + arrName, min, ((MLUInt64) mfr.getMLArray("arr")).get(0, 0)); assertEquals("Test max. value from file:" + fileName + " array: " + arrName, max, ((MLUInt64) mfr.getMLArray("arr")).get(0, 1)); src = mfr.getMLArray("arr"); // write fileName = "uint64out.mat"; ArrayList<MLArray> towrite = new ArrayList<MLArray>(); towrite.add(mfr.getMLArray(arrName)); new MatFileWriter(getTempFile(fileName), towrite); // read again mfr = new MatFileReader(getTempFile(fileName)); assertEquals("Test min. value from file:" + fileName + " array: " + arrName, min, ((MLUInt64) mfr.getMLArray(arrName)).get(0, 0)); assertEquals("Test max. value from file:" + fileName + " array: " + arrName, max, ((MLUInt64) mfr.getMLArray(arrName)).get(0, 1)); assertEquals("Test if array retrieved from " + fileName + " equals source array", src, mfr.getMLArray(arrName)); }
private void parseAnnotations(FileObject annotationFile) throws IOException { if (!annotationFile.exists()) { return; } final MatFileReader reader = new MatFileReader(annotationFile.getContent().getInputStream()); final MLDouble boxes = (MLDouble) reader.getMLArray("box_coord"); this.bounds = new Rectangle( (float) (double) boxes.getReal(2) - 1, (float) (double) boxes.getReal(0) - 1, (float) (boxes.getReal(3) - boxes.getReal(2)) - 1, (float) (boxes.getReal(1) - boxes.getReal(0)) - 1); final double[][] contourData = ((MLDouble) reader.getMLArray("obj_contour")).getArray(); this.contour = new Polygon(); for (int i = 0; i < contourData[0].length; i++) { contour.points.add( new Point2dImpl((float) contourData[0][i] + bounds.x - 1, (float) contourData[1][i] + bounds.y - 1) ); } contour.close(); }
private void parseAnnotations(FileObject annotationFile) throws IOException { if (!annotationFile.exists()) { return; } final MatFileReader reader = new MatFileReader(annotationFile.getContent().getInputStream()); final MLDouble boxes = (MLDouble) reader.getMLArray("box_coord"); this.bounds = new Rectangle( (float) (double) boxes.getReal(2) - 1, (float) (double) boxes.getReal(0) - 1, (float) (boxes.getReal(3) - boxes.getReal(2)) - 1, (float) (boxes.getReal(1) - boxes.getReal(0)) - 1); final double[][] contourData = ((MLDouble) reader.getMLArray("obj_contour")).getArray(); this.contour = new Polygon(); for (int i = 0; i < contourData[0].length; i++) { contour.points.add( new Point2dImpl((float) contourData[0][i] + bounds.x - 1, (float) contourData[1][i] + bounds.y - 1) ); } contour.close(); }
@Test public void testCellFromMatlabCreatedFile() throws IOException { // array name File file = getTestFile("cell.mat"); MatFileReader reader = new MatFileReader(file); MLArray mlArray = reader.getMLArray("cel"); List<MLArray> towrite = Arrays.asList(mlArray); new MatFileWriter(getTempFile("cellcopy.mat"), towrite); reader = new MatFileReader(getTempFile("cellcopy.mat")); MLArray mlArrayRetrieved = reader.getMLArray("cel"); assertEquals(mlArray.contentToString(), mlArrayRetrieved.contentToString()); }
@Test public void testInt32() throws IOException { // array name String name = "a"; // test column-packed vector int[] src = new int[]{1, 2, 3, 4}; MLInt32 mlDouble = new MLInt32(name, src, 1); // read array form file MatFileReader mfr = new MatFileReader(getTestFile("int32.mat")); MLArray mlArrayRetrived = mfr.getMLArray(name); // test if MLArray objects are equal assertEquals("Test if value red from file equals value stored", mlDouble, mlArrayRetrived); }
@Test public void testBenchmarkDouble() throws Exception { final String fileName = "bb.mat"; final String name = "bigdouble"; final int SIZE = 1000; MLDouble mlDouble = new MLDouble(name, new int[]{SIZE, SIZE}); for (int i = 0; i < SIZE * SIZE; i++) { mlDouble.set((double) i, i); } // write array to file ArrayList<MLArray> list = new ArrayList<MLArray>(); list.add(mlDouble); // write arrays to file new MatFileWriter(getTempFile(fileName), list); // read array form file MatFileReader mfr = new MatFileReader(getTempFile(fileName)); MLArray mlArrayRetrived = mfr.getMLArray(name); // test if MLArray objects are equal assertEquals("Test if value red from file equals value stored", mlDouble, mlArrayRetrived); }
/** * * <pre> * <code> * >> x = NaN; * >> save('nan', 'x'); * </code> * </pre> * * @throws IOException */ @Test public void testReadingNaN() throws IOException { final String fileName = "nan.mat"; // read array form file MatFileReader mfr = new MatFileReader(getTestFile(fileName)); assertEquals("Test if value red from file equals NaN", Double.NaN, (double) ((MLDouble) mfr.getMLArray("x")).get(0, 0), 0.001); }
@Test public void testBenchmarkUInt8() throws Exception { final String fileName = "bigbyte.mat"; final String name = "bigbyte"; final int SIZE = 1024; MLUInt8 mluint8 = new MLUInt8(name, new int[]{SIZE, SIZE}); // write array to file ArrayList<MLArray> list = new ArrayList<MLArray>(); list.add(mluint8); // write arrays to file new MatFileWriter(getTempFile(fileName), list); // read array form file MatFileReader mfr = new MatFileReader(getTempFile(fileName)); MLArray mlArrayRetrived = mfr.getMLArray(name); for (int i = 0; i < mlArrayRetrived.getSize(); i++) { ((MLNumericArray<?>) mlArrayRetrived).get(i); } // test if MLArray objects are equal assertEquals("Test if value red from file equals value stored", mluint8, mlArrayRetrived); }
/** * Regression bug * * @throws Exception */ @Test public void testDoubleFromMatlabCreatedFile() throws Exception { // array name String name = "arr"; // file name in which array will be stored String fileName = "matnativedouble.mat"; // test column-packed vector double[] src = new double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; MLDouble mlDouble = new MLDouble(name, src, 3); // read array form file MatFileReader mfr = new MatFileReader(getTestFile(fileName)); MLArray mlArrayRetrived = mfr.getMLArray(name); // test if MLArray objects are equal assertEquals("Test if value red from file equals value stored", mlDouble, mlArrayRetrived); }
/** * Regression bug. * * <pre> * <code> * Matlab code: * >> arr = [1.1, 4.4; 2.2, 5.5; 3.3, 6.6]; * >> save('matnativedouble2', arr); * </code> * </pre> * * @throws IOException */ @Test public void testDoubleFromMatlabCreatedFile2() throws IOException { // array name String name = "arr"; // file name in which array will be stored String fileName = "matnativedouble2.mat"; // test column-packed vector double[] src = new double[]{1.1, 2.2, 3.3, 4.4, 5.5, 6.6}; MLDouble mlDouble = new MLDouble(name, src, 3); // read array form file MatFileReader mfr = new MatFileReader(getTestFile(fileName)); MLArray mlArrayRetrived = mfr.getMLArray(name); // test if MLArray objects are equal assertEquals("Test if value red from file equals value stored", mlDouble, mlArrayRetrived); }
@Test public void testMLCharUnicode() throws Exception { // array name String name = "chararr"; // file name in which array will be storred String fileName = "mlcharUTF.mat"; // create MLChar array of a name "chararr" containig one // string value "dummy" MLChar mlChar = new MLChar(name, new String[]{"\u017C\u00F3\u0142w", "\u017C\u00F3\u0142i"}); MatFileWriter writer = new MatFileWriter(); writer.write(getTempFile(fileName), Arrays.asList((MLArray) mlChar)); MatFileReader reader = new MatFileReader(getTempFile(fileName)); MLChar mlChar2 = (MLChar) reader.getMLArray(name); assertEquals("\u017C\u00F3\u0142w", mlChar.getString(0)); assertEquals("\u017C\u00F3\u0142w", mlChar2.getString(0)); assertEquals("\u017C\u00F3\u0142i", mlChar2.getString(1)); }
@Test public void testMultipleDimArrayRealWIndicesFromMatlabCreatedFile() throws IOException { int ndims = 5; int[] dims = new int[]{2, 3, 4, 5, 6}; File file = getTestFile("multiDimMatrix.mat"); MatFileReader reader = new MatFileReader(file); MLDouble mlArray = (MLDouble) reader.getMLArray("in"); int testNDims = mlArray.getNDimensions(); Assert.assertEquals(ndims, testNDims); int[] testDims = mlArray.getDimensions(); for (int i = 0; i < ndims; i++) { Assert.assertEquals(dims[i], testDims[i]); } Double expectedVal = 0.0; for (int i = 0; i < dims[4]; i++) { for (int j = 0; j < dims[3]; j++) { for (int k = 0; k < dims[2]; k++) { for (int l = 0; l < dims[1]; l++) { for (int m = 0; m < dims[0]; m++, expectedVal += 1.0) { Double actual = mlArray.get(m, l, k, j, i); Assert.assertEquals(expectedVal, actual); } } } } } }
@Test public void testMultipleDimArrayGetFromMatlabCreatedFile() throws IOException { int ndims = 5; int[] dims = new int[]{2, 3, 4, 5, 6}; File file = getTestFile("multiDimMatrix.mat"); MatFileReader reader = new MatFileReader(file); MLDouble mlArray = (MLDouble) reader.getMLArray("in"); int testNDims = mlArray.getNDimensions(); Assert.assertEquals(ndims, testNDims); int[] testDims = mlArray.getDimensions(); for (int i = 0; i < ndims; i++) { Assert.assertEquals(dims[i], testDims[i]); } Double expectedVal = 0.0; for (int i = 0; i < dims[4]; i++) { for (int j = 0; j < dims[3]; j++) { for (int k = 0; k < dims[2]; k++) { for (int l = 0; l < dims[1]; l++) { for (int m = 0; m < dims[0]; m++, expectedVal += 1.0) { Double actual = mlArray.get(m, l, k, j, i); Assert.assertEquals(expectedVal, actual); } } } } } }