Package org.apache.sysds.runtime.util
Class DataConverter
java.lang.Object
org.apache.sysds.runtime.util.DataConverter
This class provides methods to read and write matrix blocks from to HDFS using different data formats.
Those functionalities are used especially for CP read/write and exporting in-memory matrices to HDFS
(before executing MR jobs).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.apache.commons.math3.linear.Array2DRowRealMatrixHelper method that converts SystemDS matrix variable (varname) into a Array2DRowRealMatrix format, which is useful in invoking Apache CommonsMath.static org.apache.commons.math3.linear.BlockRealMatrixstatic boolean[]static DenseBlockstatic DenseBlockconvertToDenseBlock(MatrixBlock mb, boolean deep) static double[][]Creates a two-dimensional double matrix of the input matrix block.static double[]static double[]convertToDoubleVector(MatrixBlock mb, boolean deep) static double[]convertToDoubleVector(MatrixBlock mb, boolean deep, boolean allowNull) static FrameBlockconvertToFrameBlock(String[][] data) Converts a two dimensions string array into a frame block of value type string.static FrameBlockconvertToFrameBlock(String[][] data, Types.ValueType[] schema) static FrameBlockconvertToFrameBlock(String[][] data, Types.ValueType[] schema, String[] colnames) static FrameBlockConverts a matrix block into a frame block of value type double.static FrameBlockconvertToFrameBlock(MatrixBlock mb, int k) Converts a matrix block into a frame block of value type double.static FrameBlockConverts a matrix block into a frame block of value type given.static FrameBlockconvertToFrameBlock(MatrixBlock mb, Types.ValueType[] schema) Converts a matrix block into a frame block of with the given schemastatic FrameBlockconvertToFrameBlock(MatrixBlock mb, Types.ValueType[] schema, int k) Converts a matrix block into a frame block of with the given schemastatic FrameBlockconvertToFrameBlock(MatrixBlock mb, Types.ValueType vt, int k) Converts a matrix block into a frame block of a given value type.static int[]static long[]static MatrixBlockconvertToMatrixBlock(double[][] data) Creates a dense Matrix Block and copies the given double matrix into it.static MatrixBlockconvertToMatrixBlock(double[] data, boolean columnVector) Creates a dense Matrix Block and copies the given double vector into it.static MatrixBlockconvertToMatrixBlock(int[][] data) Converts an Integer matrix to an MatrixBlockstatic MatrixBlockstatic MatrixBlockconvertToMatrixBlock(HashMap<MatrixIndexes, Double> map, int rlen, int clen) NOTE: this method also ensures the specified matrix dimensionsstatic MatrixBlockconvertToMatrixBlock(org.apache.commons.math3.linear.RealMatrix rm) static MatrixBlockconvertToMatrixBlock(FrameBlock frame) Converts a frame block with arbitrary schema into a matrix block.static MatrixBlockstatic MatrixBlockconvertToMatrixBlock(CTableMap map, int rlen, int clen) NOTE: this method also ensures the specified matrix dimensionsstatic MatrixBlock[]convertToMatrixBlockPartitions(MatrixBlock mb, boolean colwise) static String[][]convertToStringFrame(FrameBlock frame) Converts a frame block with arbitrary schema into a two dimensional string array.static TensorBlockconvertToTensorBlock(MatrixBlock mb, Types.ValueType vt, boolean toBasicTensor) static int[]static voidcopyToDoubleVector(MatrixBlock mb, double[] dest, int destPos) static int[]getTensorDimensions(ExecutionContext ec, CPOperand dims) static MatrixBlockreadMatrixFromHDFS(String dir, Types.FileFormat fmt, long rlen, long clen, int blen) static MatrixBlockreadMatrixFromHDFS(String dir, Types.FileFormat fmt, long rlen, long clen, int blen, long expectedNnz) static MatrixBlockreadMatrixFromHDFS(String dir, Types.FileFormat fmt, long rlen, long clen, int blen, long expectedNnz, FileFormatProperties formatProperties) static MatrixBlockCore method for reading matrices in format textcell, matrixmarket, binarycell, or binaryblock from HDFS into main memory.static TensorBlockreadTensorFromHDFS(String dir, Types.FileFormat fmt, long[] dims, int blen, Types.ValueType[] schema) static BitSettoBitSet(double[] data) static double[]toDouble(float[] data) static double[]toDouble(int[] data) static double[]toDouble(long[] data) static double[]static double[]static float[]toFloat(double[] data) static int[]toInt(double[] data) static long[]toLong(double[] data) static String[]toString(double[] data) static StringtoString(TensorBlock tb) static StringtoString(TensorBlock tb, boolean sparse, String separator, String lineseparator, String leftBorder, String rightBorder, int rowsToPrint, int colsToPrint, int decimal) Returns a string representation of a tensorstatic StringtoString(FrameBlock fb) static StringtoString(FrameBlock fb, boolean sparse, String separator, String lineseparator, int rowsToPrint, int colsToPrint, int decimal) static StringtoString(ListObject list, int rows, int cols, boolean sparse, String separator, String lineSeparator, int rowsToPrint, int colsToPrint, int decimal) static StringtoString(MatrixBlock mb) static StringtoString(MatrixBlock mb, boolean sparse, String separator, String lineseparator, int rowsToPrint, int colsToPrint, int decimal) Returns a string representation of a matrixstatic voidwriteMatrixToHDFS(MatrixBlock mat, String dir, Types.FileFormat fmt, DataCharacteristics dc) static voidwriteMatrixToHDFS(MatrixBlock mat, String dir, Types.FileFormat fmt, DataCharacteristics dc, int replication, FileFormatProperties formatProperties) static voidwriteMatrixToHDFS(MatrixBlock mat, String dir, Types.FileFormat fmt, DataCharacteristics dc, int replication, FileFormatProperties formatProperties, boolean diag) static voidwriteTensorToHDFS(TensorBlock tensor, String dir, Types.FileFormat fmt, DataCharacteristics dc)
-
Constructor Details
-
DataConverter
public DataConverter()
-
-
Method Details
-
writeMatrixToHDFS
public static void writeMatrixToHDFS(MatrixBlock mat, String dir, Types.FileFormat fmt, DataCharacteristics dc) throws IOException - Throws:
IOException
-
writeMatrixToHDFS
public static void writeMatrixToHDFS(MatrixBlock mat, String dir, Types.FileFormat fmt, DataCharacteristics dc, int replication, FileFormatProperties formatProperties) throws IOException - Throws:
IOException
-
writeMatrixToHDFS
public static void writeMatrixToHDFS(MatrixBlock mat, String dir, Types.FileFormat fmt, DataCharacteristics dc, int replication, FileFormatProperties formatProperties, boolean diag) throws IOException - Throws:
IOException
-
writeTensorToHDFS
public static void writeTensorToHDFS(TensorBlock tensor, String dir, Types.FileFormat fmt, DataCharacteristics dc) throws IOException - Throws:
IOException
-
readMatrixFromHDFS
public static MatrixBlock readMatrixFromHDFS(String dir, Types.FileFormat fmt, long rlen, long clen, int blen) throws IOException - Throws:
IOException
-
readMatrixFromHDFS
public static MatrixBlock readMatrixFromHDFS(String dir, Types.FileFormat fmt, long rlen, long clen, int blen, long expectedNnz) throws IOException - Throws:
IOException
-
readMatrixFromHDFS
public static MatrixBlock readMatrixFromHDFS(String dir, Types.FileFormat fmt, long rlen, long clen, int blen, long expectedNnz, FileFormatProperties formatProperties) throws IOException - Throws:
IOException
-
readTensorFromHDFS
public static TensorBlock readTensorFromHDFS(String dir, Types.FileFormat fmt, long[] dims, int blen, Types.ValueType[] schema) throws IOException - Throws:
IOException
-
readMatrixFromHDFS
Core method for reading matrices in format textcell, matrixmarket, binarycell, or binaryblock from HDFS into main memory. For expected dense matrices we directly copy value- or block-at-a-time into the target matrix. In contrast, for sparse matrices, we append (column-value)-pairs and do a final sort if required in order to prevent large reorg overheads and increased memory consumption in case of unordered inputs. DENSE MxN input: * best/average/worst: O(M*N) SPARSE MxN input * best (ordered, or binary block w/ clen<=blen): O(M*N) * average (unordered): O(M*N*log(N)) * worst (descending order per row): O(M * N^2) NOTE: providing an exact estimate of 'expected sparsity' can prevent a full copy of the result matrix block (required for changing sparse->dense, or vice versa)- Parameters:
prop- read properties- Returns:
- matrix block
- Throws:
IOException- if IOException occurs
-
convertToDoubleMatrix
Creates a two-dimensional double matrix of the input matrix block.- Parameters:
mb- matrix block- Returns:
- 2d double array
-
convertToBooleanVector
-
convertVectorToIndexList
-
convertToIntVector
-
convertToLongVector
-
convertToDenseBlock
-
convertToDenseBlock
-
convertToDoubleVector
-
convertToDoubleVector
-
convertToDoubleVector
-
convertToDoubleList
-
convertToMatrixBlock
Creates a dense Matrix Block and copies the given double matrix into it.- Parameters:
data- 2d double array- Returns:
- matrix block
-
convertToMatrixBlock
Converts an Integer matrix to an MatrixBlock- Parameters:
data- Int matrix input that is converted to double MatrixBlock- Returns:
- The matrixBlock constructed.
-
convertToMatrixBlock
Creates a dense Matrix Block and copies the given double vector into it.- Parameters:
data- double arraycolumnVector- if true, create matrix with single column. if false, create matrix with single row- Returns:
- matrix block
-
convertToMatrixBlock
-
convertToMatrixBlock
public static MatrixBlock convertToMatrixBlock(HashMap<MatrixIndexes, Double> map, int rlen, int clen) NOTE: this method also ensures the specified matrix dimensions- Parameters:
map- map of matrix index keys and double valuesrlen- number of rowsclen- number of columns- Returns:
- matrix block
-
convertToMatrixBlock
-
convertToMatrixBlock
NOTE: this method also ensures the specified matrix dimensions- Parameters:
map- ?rlen- number of rowsclen- number of columns- Returns:
- matrix block
-
convertToMatrixBlock
Converts a frame block with arbitrary schema into a matrix block. Since matrix block only supports value type double, we do a best effort conversion of non-double types which might result in errors for non-numerical data.- Parameters:
frame- frame block- Returns:
- matrix block
-
convertToStringFrame
Converts a frame block with arbitrary schema into a two dimensional string array.- Parameters:
frame- frame block- Returns:
- 2d string array
-
convertToFrameBlock
Converts a two dimensions string array into a frame block of value type string. If the given array is null or of length 0, we return an empty frame block.- Parameters:
data- 2d string array- Returns:
- frame block
-
convertToFrameBlock
-
convertToFrameBlock
public static FrameBlock convertToFrameBlock(String[][] data, Types.ValueType[] schema, String[] colnames) -
convertToFrameBlock
Converts a matrix block into a frame block of value type double.- Parameters:
mb- matrix block- Returns:
- frame block of type double
-
convertToFrameBlock
Converts a matrix block into a frame block of value type double.- Parameters:
mb- matrix blockk- parallelization degree- Returns:
- frame block of type double
-
convertToFrameBlock
Converts a matrix block into a frame block of value type given.- Parameters:
mb- matrix blockvt- value type target- Returns:
- frame block of type given
-
convertToFrameBlock
Converts a matrix block into a frame block of a given value type.- Parameters:
mb- matrix blockvt- value typek- parallelization degree- Returns:
- a return frame block with the given schema
-
convertToFrameBlock
Converts a matrix block into a frame block of with the given schema- Parameters:
mb- matrix blockschema- schema- Returns:
- a return frame block with the given schema
-
convertToFrameBlock
Converts a matrix block into a frame block of with the given schema- Parameters:
mb- matrix blockschema- schemak- parallelization degree- Returns:
- a return frame block with the given schema
-
convertToTensorBlock
public static TensorBlock convertToTensorBlock(MatrixBlock mb, Types.ValueType vt, boolean toBasicTensor) -
convertToMatrixBlockPartitions
-
convertToArray2DRowRealMatrix
public static org.apache.commons.math3.linear.Array2DRowRealMatrix convertToArray2DRowRealMatrix(MatrixBlock mb) Helper method that converts SystemDS matrix variable (varname) into a Array2DRowRealMatrix format, which is useful in invoking Apache CommonsMath.- Parameters:
mb- matrix object- Returns:
- matrix as a commons-math3 Array2DRowRealMatrix
-
convertToBlockRealMatrix
public static org.apache.commons.math3.linear.BlockRealMatrix convertToBlockRealMatrix(MatrixBlock mb) -
convertToMatrixBlock
-
copyToDoubleVector
-
toString
-
toString
public static String toString(MatrixBlock mb, boolean sparse, String separator, String lineseparator, int rowsToPrint, int colsToPrint, int decimal) Returns a string representation of a matrix- Parameters:
mb- matrix blocksparse- if true, string will contain a table with row index, col index, value (where value != 0.0) otherwise it will be a rectangular string with all values of the matrix blockseparator- Separator string between each element in a row, or between the columns in sparse formatlineseparator- Separator string between each rowrowsToPrint- maximum number of rows to print, -1 for allcolsToPrint- maximum number of columns to print, -1 for alldecimal- number of decimal places to print, -1 for default- Returns:
- matrix as a string
-
toString
-
toString
public static String toString(TensorBlock tb, boolean sparse, String separator, String lineseparator, String leftBorder, String rightBorder, int rowsToPrint, int colsToPrint, int decimal) Returns a string representation of a tensor- Parameters:
tb- tensor blocksparse- if true, string will contain a table with row index, col index, value (where value != 0.0) otherwise it will be a rectangular string with all values of the tensor blockseparator- Separator string between each element in a row, or between the columns in sparse formatlineseparator- Separator string between each rowleftBorder- Characters placed at the start of a new dimension levelrightBorder- Characters placed at the end of a new dimension levelrowsToPrint- maximum number of rows to print, -1 for allcolsToPrint- maximum number of columns to print, -1 for alldecimal- number of decimal places to print, -1 for default- Returns:
- tensor as a string
-
toString
-
toString
public static String toString(FrameBlock fb, boolean sparse, String separator, String lineseparator, int rowsToPrint, int colsToPrint, int decimal) -
toString
public static String toString(ListObject list, int rows, int cols, boolean sparse, String separator, String lineSeparator, int rowsToPrint, int colsToPrint, int decimal) -
getTensorDimensions
-
toDouble
public static double[] toDouble(float[] data) -
toDouble
public static double[] toDouble(long[] data) -
toDouble
public static double[] toDouble(int[] data) -
toDouble
-
toDouble
-
toFloat
public static float[] toFloat(double[] data) -
toInt
public static int[] toInt(double[] data) -
toLong
public static long[] toLong(double[] data) -
toBitSet
-
toString
-