private void writeMLArrayToPath(Path fPath, List<MLArray> mlarray) throws IOException { try (OutputStream w = Files.newOutputStream(fPath)) { //using Writable channel to make it work both for 'standard' and shrinkwrap based filesystems ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel wbc = Channels.newChannel(baos); new MatFileWriter(wbc, mlarray); w.write(baos.toByteArray()); } }
public static void writeWp41ContModule1Mat(Path outputfile, double[][] matrix) throws IOException { LOGGER.debug("writeWp41ContModule1Mat - writing wp41 input X matrix data to file {}", outputfile); MLDouble data = Utils.matToMLDouble(matrix, "X"); List<MLArray> mlarray = new ArrayList<>(); mlarray.add((MLArray) data); MatFileWriter writer = new MatFileWriter(); writer.write(outputfile.toFile(), mlarray); LOGGER.debug("writeWp41ContModule1Mat - X matrix data, written."); }
public static void writeWP41BinaryIndependentSamplingInputFile(Path outputfile, double[] marginalExpectations) throws IOException { MLDouble data = Utils.vectToMLDoubleRow(marginalExpectations, "p"); List<MLArray> mlarray = new ArrayList<>(); mlarray.add((MLArray) data); MatFileWriter writer = new MatFileWriter(); writer.write(outputfile.toFile(), mlarray); }
public static void toFile(File file, Matrix matrix, Object... parameters) throws IOException { MatFileWriter writer = new MatFileWriter(); Collection<MLArray> matrixList = new ArrayList<MLArray>(); matrixList.add(new MLDenseDoubleMatrix(matrix).getWrappedObject()); writer.write(file, matrixList); }
public static void writeWP41ContinuousInputMatFile(Path outputfile, double[][] idata, int nsamples, double ir, double k) throws IOException { MLDouble dNsample = new MLDouble("N_Sample", new double[]{nsamples}, 1); MLDouble dIR = new MLDouble("IR", new double[]{ir}, 1); MLDouble dK = new MLDouble("K", new double[]{k}, 1); MLDouble data = Utils.matToMLDouble(idata, "X"); List<MLArray> mlarray = new ArrayList<>(); mlarray.add((MLArray) dNsample); mlarray.add((MLArray) dIR); mlarray.add((MLArray) dK); mlarray.add((MLArray) data); MatFileWriter writer = new MatFileWriter(); writer.write(outputfile.toFile(), mlarray); }
public static void main(String[] args) throws IOException { final MixtureOfGaussians mog = loadMoG(); final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true); for (final String faceFile : FACE_DSIFTS_PCA) { final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFTPCA = loadDSIFTPCA(faceFile); final FloatFV fvec = fisher.aggregate(loadDSIFTPCA); System.out.println(String.format("%s: %s", faceFile, fvec)); System.out.println("Writing..."); final File out = new File(faceFile + ".fisher.mat"); final MLArray data = toMLArray(fvec); new MatFileWriter(out, Arrays.asList(data)); } }
public static void main(String[] args) throws IOException { final MixtureOfGaussians mog = loadMoG(); final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true); for (final String faceFile : FACE_DSIFTS_PCA) { final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFTPCA = loadDSIFTPCA(faceFile); final FloatFV fvec = fisher.aggregate(loadDSIFTPCA); System.out.println(String.format("%s: %s", faceFile, fvec)); System.out.println("Writing..."); final File out = new File(faceFile + ".fisher.mat"); final MLArray data = toMLArray(fvec); new MatFileWriter(out, Arrays.asList(data)); } }
public void writeSamplingNetworkData(SamplingNetworkData mcNetworkSamplingData) throws IOException { Objects.requireNonNull(mcNetworkSamplingData, "sampling network data is null"); LOGGER.debug("Preparing buses mat data"); MLStructure buses = busesDataAsMLStructure(mcNetworkSamplingData.getBusesData()); LOGGER.debug("Preparing generators mat data"); MLStructure generators = generatorsDataAsMLStructure(mcNetworkSamplingData.getGeneratorsData()); LOGGER.debug("Preparing loads mat data"); MLStructure loads = loadsDataAsMLStructure(mcNetworkSamplingData.getLoadsData()); LOGGER.debug("Saving mat data into " + matFile.toString()); List<MLArray> mlarray = new ArrayList<>(); mlarray.add((MLArray) buses); mlarray.add((MLArray) generators); mlarray.add((MLArray) loads); MatFileWriter writer = new MatFileWriter(); writer.write(matFile.toFile(), mlarray); }
public static void main(String[] args) throws IOException { final MixtureOfGaussians mog = loadMoG(new File(GMM_MATLAB_FILE)); final PrincipalComponentAnalysis pca = loadPCA(new File(PCA_MATLAB_FILE)); final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true); for (final String faceFile : FACE_DSIFTS) { final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFT = MemoryLocalFeatureList.read(new File(faceFile), FloatDSIFTKeypoint.class); projectPCA(loadDSIFT, pca); final FloatFV fvec = fisher.aggregate(loadDSIFT); System.out.println(String.format("%s: %s", faceFile, fvec)); System.out.println("Writing..."); final File out = new File(faceFile + ".fisher.mat"); final MLArray data = toMLArray(fvec); new MatFileWriter(out, Arrays.asList(data)); } }
public static void main(String[] args) throws IOException { final MixtureOfGaussians mog = loadMoG(new File(GMM_MATLAB_FILE)); final PrincipalComponentAnalysis pca = loadPCA(new File(PCA_MATLAB_FILE)); final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true); for (final String faceFile : FACE_DSIFTS) { final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFT = MemoryLocalFeatureList.read(new File(faceFile), FloatDSIFTKeypoint.class); projectPCA(loadDSIFT, pca); final FloatFV fvec = fisher.aggregate(loadDSIFT); System.out.println(String.format("%s: %s", faceFile, fvec)); System.out.println("Writing..."); final File out = new File(faceFile + ".fisher.mat"); final MLArray data = toMLArray(fvec); new MatFileWriter(out, Arrays.asList(data)); } }
private void saveFoldParameterLearner(int fold, int j, BilinearSparseOnlineLearner learner) { // save the state final File learnerOut = new File(String.format("%s/fold_%d", currentOutputRoot(), fold), String.format( "learner_%d", j)); final File learnerOutMat = new File(String.format("%s/fold_%d", currentOutputRoot(), fold), String.format( "learner_%d.mat", j)); learnerOut.getParentFile().mkdirs(); try { IOUtils.writeBinary(learnerOut, learner); final Collection<MLArray> data = new ArrayList<MLArray>(); data.add(CFMatrixUtils.toMLArray("u", learner.getU())); data.add(CFMatrixUtils.toMLArray("w", learner.getW())); if (learner.getBias() != null) { data.add(CFMatrixUtils.toMLArray("b", learner.getBias())); } final MatFileWriter writer = new MatFileWriter(learnerOutMat, data); } catch (final IOException e) { throw new RuntimeException(e); } }
/** * Write a CSV timeIndex to a {@link MLCell} writen to a .mat data file * @param path * @throws IOException */ public static void writeToMatlab(String path) throws IOException { Path timeMatPath = new Path(path + "/times/timeIndex.mat"); FileSystem fs = HadoopToolsUtil.getFileSystem(timeMatPath); LinkedHashMap<Long, IndependentPair<Long, Long>> timeIndex = readTimeCountLines(path); MLCell timeCell = new MLCell("times",new int[]{timeIndex.size(),2}); System.out.println("... reading times"); for (Entry<Long, IndependentPair<Long, Long>> ent : timeIndex.entrySet()) { long time = (long)ent.getKey(); int timeCellIndex = (int)(long)ent.getValue().secondObject(); long count = ent.getValue().firstObject(); timeCell.set(new MLDouble(null, new double[][]{new double[]{time}}), timeCellIndex,0); timeCell.set(new MLDouble(null, new double[][]{new double[]{count}}), timeCellIndex,1); } ArrayList<MLArray> list = new ArrayList<MLArray>(); list.add(timeCell); new MatFileWriter(Channels.newChannel(fs.create(timeMatPath)),list ); }
/** * Write a CSV timeIndex to a {@link MLCell} writen to a .mat data file * @param path * @throws IOException */ public static void writeToMatlab(String path) throws IOException { Path timeMatPath = new Path(path + "/times/timeIndex.mat"); FileSystem fs = HadoopToolsUtil.getFileSystem(timeMatPath); LinkedHashMap<Long, IndependentPair<Long, Long>> timeIndex = readTimeCountLines(path); MLCell timeCell = new MLCell("times",new int[]{timeIndex.size(),2}); System.out.println("... reading times"); for (Entry<Long, IndependentPair<Long, Long>> ent : timeIndex.entrySet()) { long time = (long)ent.getKey(); int timeCellIndex = (int)(long)ent.getValue().secondObject(); long count = ent.getValue().firstObject(); timeCell.set(new MLDouble(null, new double[][]{new double[]{time}}), timeCellIndex,0); timeCell.set(new MLDouble(null, new double[][]{new double[]{count}}), timeCellIndex,1); } ArrayList<MLArray> list = new ArrayList<MLArray>(); list.add(timeCell); new MatFileWriter(Channels.newChannel(fs.create(timeMatPath)),list ); }
/** * Write a CSV wordIndex to a {@link MLCell} writen to a .mat data file * * @param path * @throws IOException */ public static void writeToMatlab(String path) throws IOException { final Path wordMatPath = new Path(path + "/words/wordIndex.mat"); final FileSystem fs = HadoopToolsUtil.getFileSystem(wordMatPath); final LinkedHashMap<String, IndependentPair<Long, Long>> wordIndex = readWordCountLines(path); final MLCell wordCell = new MLCell("words", new int[] { wordIndex.size(), 2 }); System.out.println("... reading words"); for (final Entry<String, IndependentPair<Long, Long>> ent : wordIndex.entrySet()) { final String word = ent.getKey(); final int wordCellIndex = (int) (long) ent.getValue().secondObject(); final long count = ent.getValue().firstObject(); wordCell.set(new MLChar(null, word), wordCellIndex, 0); wordCell.set(new MLDouble(null, new double[][] { new double[] { count } }), wordCellIndex, 1); } final ArrayList<MLArray> list = new ArrayList<MLArray>(); list.add(wordCell); new MatFileWriter(Channels.newChannel(fs.create(wordMatPath)), list); }
@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 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); }
@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 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); }
@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 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)); }