@Test public void testSetData() throws IOException { File f = File.createTempFile("matrix", ".m", getTestTempDir()); f.deleteOnExit(); Random gen = RandomUtils.getRandom(); Matrix m0 = new SparseRowMatrix(10, 21); for (MatrixSlice row : m0) { int len = (int) Math.ceil(-15 * Math.log(1 - gen.nextDouble())); for (int i = 0; i < len; i++) { row.vector().set(gen.nextInt(21), 1); } } FileBasedSparseBinaryMatrix.writeMatrix(f, m0); FileBasedSparseBinaryMatrix m = new FileBasedSparseBinaryMatrix(10, 21); m.setData(f); for (MatrixSlice row : m) { Vector diff = row.vector().minus(m0.viewRow(row.index())); double error = diff.norm(1); if (error > 1.0e-14) { System.out.printf("%s\n", diff); } assertEquals(0, error, 1.0e-14); } }
public void setData(File f) throws IOException { List<ByteBuffer> buffers = Lists.newArrayList(); FileChannel input = new FileInputStream(f).getChannel(); buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length()))); data.add(buffers.get(0).asIntBuffer()); Preconditions.checkArgument(buffers.get(0).getInt() == MAGIC_NUMBER_V0, "Wrong type of file"); int rows = buffers.get(0).getInt(); int cols = buffers.get(0).getInt(); Preconditions.checkArgument(rows == rowSize()); Preconditions.checkArgument(cols == columnSize()); rowOffset = new int[rows]; rowSize = new int[rows]; bufferIndex = new int[rows]; int offset = 12 + 4 * rows; for (int i = 0; i < rows; i++) { int size = buffers.get(0).getInt(); int buffer = 0; while (buffer < buffers.size()) { if (offset + size * 4 <= buffers.get(buffer).limit()) { break; } else { offset -= buffers.get(buffer).capacity(); } } if (buffer == buffers.size()) { buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length() - offset))); data.add(buffers.get(buffer).asIntBuffer()); } rowOffset[i] = offset / 4; rowSize[i] = size; bufferIndex[i] = buffer; offset += size * 4; } }
FileBasedSparseBinaryMatrix extends AbstractMatrix { public void setData(File f) throws IOException { List<ByteBuffer> buffers = Lists.newArrayList(); FileChannel input = new FileInputStream(f).getChannel(); buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length()))); data.add(buffers.get(0).asIntBuffer()); Preconditions.checkArgument(buffers.get(0).getInt() == MAGIC_NUMBER_V0, "Wrong type of file"); int rows = buffers.get(0).getInt(); int cols = buffers.get(0).getInt(); Preconditions.checkArgument(rows == rowSize()); Preconditions.checkArgument(cols == columnSize()); rowOffset = new int[rows]; rowSize = new int[rows]; bufferIndex = new int[rows]; int offset = 12 + 4 * rows; for (int i = 0; i < rows; i++) { int size = buffers.get(0).getInt(); int buffer = 0; while (buffer < buffers.size()) { if (offset + size * 4 <= buffers.get(buffer).limit()) { break; } else { offset -= buffers.get(buffer).capacity(); } } if (buffer == buffers.size()) { buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length() - offset))); data.add(buffers.get(buffer).asIntBuffer()); } rowOffset[i] = offset / 4; rowSize[i] = size; bufferIndex[i] = buffer; offset += size * 4; } } }
FileBasedSparseBinaryMatrix extends AbstractMatrix { public void setData(File f) throws IOException { List<ByteBuffer> buffers = Lists.newArrayList(); FileChannel input = new FileInputStream(f).getChannel(); buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length()))); data.add(buffers.get(0).asIntBuffer()); Preconditions.checkArgument(buffers.get(0).getInt() == MAGIC_NUMBER_V0, "Wrong type of file"); int rows = buffers.get(0).getInt(); int cols = buffers.get(0).getInt(); Preconditions.checkArgument(rows == rowSize()); Preconditions.checkArgument(cols == columnSize()); rowOffset = new int[rows]; rowSize = new int[rows]; bufferIndex = new int[rows]; int offset = 12 + 4 * rows; for (int i = 0; i < rows; i++) { int size = buffers.get(0).getInt(); int buffer = 0; while (buffer < buffers.size()) { if (offset + size * 4 <= buffers.get(buffer).limit()) { break; } else { offset -= buffers.get(buffer).capacity(); } } if (buffer == buffers.size()) { buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length() - offset))); data.add(buffers.get(buffer).asIntBuffer()); } rowOffset[i] = offset / 4; rowSize[i] = size; bufferIndex[i] = buffer; offset += size * 4; } } FileBasedSparseBinaryMatrix(int rows, int columns); }
FileBasedSparseBinaryMatrix extends AbstractMatrix { public void setData(File f) throws IOException { List<ByteBuffer> buffers = Lists.newArrayList(); FileChannel input = new FileInputStream(f).getChannel(); buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length()))); data.add(buffers.get(0).asIntBuffer()); Preconditions.checkArgument(buffers.get(0).getInt() == MAGIC_NUMBER_V0, "Wrong type of file"); int rows = buffers.get(0).getInt(); int cols = buffers.get(0).getInt(); Preconditions.checkArgument(rows == rowSize()); Preconditions.checkArgument(cols == columnSize()); rowOffset = new int[rows]; rowSize = new int[rows]; bufferIndex = new int[rows]; int offset = 12 + 4 * rows; for (int i = 0; i < rows; i++) { int size = buffers.get(0).getInt(); int buffer = 0; while (buffer < buffers.size()) { if (offset + size * 4 <= buffers.get(buffer).limit()) { break; } else { offset -= buffers.get(buffer).capacity(); } } if (buffer == buffers.size()) { buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length() - offset))); data.add(buffers.get(buffer).asIntBuffer()); } rowOffset[i] = offset / 4; rowSize[i] = size; bufferIndex[i] = buffer; offset += size * 4; } } FileBasedSparseBinaryMatrix(int rows, int columns); void setData(File f); static void writeMatrix(File f, Matrix m); @Override Matrix assignColumn(int column, Vector other); @Override Matrix assignRow(int row, Vector other); @Override double getQuick(int rowIndex, int columnIndex); @Override Matrix like(); @Override Matrix like(int rows, int columns); @Override void setQuick(int row, int column, double value); @Override Matrix viewPart(int[] offset, int[] size); @Override Vector viewRow(int rowIndex); }
FileBasedSparseBinaryMatrix extends AbstractMatrix { public void setData(File f) throws IOException { List<ByteBuffer> buffers = Lists.newArrayList(); FileChannel input = new FileInputStream(f).getChannel(); buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length()))); data.add(buffers.get(0).asIntBuffer()); Preconditions.checkArgument(buffers.get(0).getInt() == MAGIC_NUMBER_V0, "Wrong type of file"); int rows = buffers.get(0).getInt(); int cols = buffers.get(0).getInt(); Preconditions.checkArgument(rows == rowSize()); Preconditions.checkArgument(cols == columnSize()); rowOffset = new int[rows]; rowSize = new int[rows]; bufferIndex = new int[rows]; int offset = 12 + 4 * rows; for (int i = 0; i < rows; i++) { int size = buffers.get(0).getInt(); int buffer = 0; while (buffer < buffers.size()) { if (offset + size * 4 <= buffers.get(buffer).limit()) { break; } else { offset -= buffers.get(buffer).capacity(); } } if (buffer == buffers.size()) { buffers.add(input.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(Integer.MAX_VALUE, f.length() - offset))); data.add(buffers.get(buffer).asIntBuffer()); } rowOffset[i] = offset / 4; rowSize[i] = size; bufferIndex[i] = buffer; offset += size * 4; } } FileBasedSparseBinaryMatrix(int rows, int columns); void setData(File f); static void writeMatrix(File f, Matrix m); @Override Matrix assignColumn(int column, Vector other); @Override Matrix assignRow(int row, Vector other); @Override double getQuick(int rowIndex, int columnIndex); @Override Matrix like(); @Override Matrix like(int rows, int columns); @Override void setQuick(int row, int column, double value); @Override Matrix viewPart(int[] offset, int[] size); @Override Vector viewRow(int rowIndex);  }