org.knowceans.util
Class ArrayIo

java.lang.Object
  extended by org.knowceans.util.ArrayIo

public class ArrayIo
extends java.lang.Object

MatrixIo provides methods to load and save matrices and vectors to file, which automatic compression if the file extension given is ".zip". The standard methods are load/saveBinaryMatrix that load/save a double[][] with the protocol rows,cols,foreach(row): foreach(col): double[row][col] :end :end.

Custom protocols for more complex data can be easily constructed by opening a stream using one of the open*Stream() methods, using the methods of the Data*Stream classes and read/write* methods of this class for matrices and vectors and then closing the stream using the close*Stream methods in this class (provided for symmetry) or the close() methods in the Data*Stream classes.

TODO: The binary methods could be considered for change to a subclass of DataInputStream and DataOutputStream.

Author:
gregor

Constructor Summary
ArrayIo()
           
 
Method Summary
static void closeInputStream(java.io.DataInputStream dis)
          Close the input stream
static void closeOutputStream(java.io.DataOutputStream dos)
          Close the data output, which results in flushing the write buffer and closing the file.
static java.lang.String formatDouble(double d)
           
static double[][] loadAscii(java.lang.String filename)
           
static int[][] loadBinaryIntMatrix(java.lang.String filename)
          Loads an integer matrix from a binary file, optionally a zip file.
static double[][] loadBinaryMatrix(java.lang.String filename)
          Loads a matrix from a binary file, optionally a zip file.
static java.io.DataInputStream openInputStream(java.lang.String filename)
          Opens a data input stream with optional zip compression.
static java.io.DataOutputStream openOutputStream(java.lang.String filename)
          Opens a data output stream with optional zip compression.
static java.lang.String padSpace(java.lang.String s, int length)
           
static java.lang.Object read(java.io.DataInputStream is, java.lang.Object type)
          generic method that calls the methods according to the type of the argument Object
static double[][] readDoubleMatrix(java.io.DataInputStream bw)
          Read matrix from file.
static double[][][] readDoubleTensor(java.io.DataInputStream bw)
           
static double[] readDoubleVector(java.io.DataInputStream bw)
          Read vector from file.
static float[][] readFloatMatrix(java.io.DataInputStream bw)
          Read matrix from file.
static float[] readFloatVector(java.io.DataInputStream bw)
          Read vector from file.
static int[][] readIntMatrix(java.io.DataInputStream bw)
          Read matrix from file.
static int[] readIntVector(java.io.DataInputStream bw)
          Read vector from file.
static void saveAscii(java.lang.String filename, double[][] a)
           
static void saveBinaryIntMatrix(java.lang.String filename, int[][] a)
          Writes integer matrix to binary file.
static void saveBinaryMatrix(java.lang.String filename, double[][] a)
          Writes matrix to binary file.
static void saveShades(java.lang.String filename, double[][] a)
          Save a matrix as shaded values using MayaShades.
static void saveTransposedAscii(java.lang.String filename, double[][] a)
           
static void saveTransposedShades(java.lang.String filename, double[][] a)
          Save a transposed matrix as shaded values using MayaShades.
static void write(java.io.DataOutputStream os, java.lang.Object data)
          generic write method that calls the respective method of the type of data
static void writeDoubleMatrix(java.io.DataOutputStream bw, double[][] matrix)
          Writes a double matrix in the format rows,cols1,a11,a12,a1...,cols2,a21,...
static void writeDoubleTensor(java.io.DataOutputStream bw, double[][][] tensor)
          Writes a double tensor (here = 3d matrix).
static void writeDoubleVector(java.io.DataOutputStream bw, double[] vector)
          Writes a double vector in the format size,v1,v2,...
static void writeFloatMatrix(java.io.DataOutputStream bw, float[][] matrix)
          Writes a float matrix in the format rows,cols,a11,a12,a1...,a21,...
static void writeFloatVector(java.io.DataOutputStream bw, float[] vector)
          Writes a float vector in the format size,v1,v2,...
static void writeIntMatrix(java.io.DataOutputStream bw, int[][] matrix)
          Writes an integer matrix in the format rows,cols1,a11,a12,a1...,cols2,a21,...
static void writeIntVector(java.io.DataOutputStream bw, int[] vector)
          Writes an integer vector in the format size,v1,v2,...
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayIo

public ArrayIo()
Method Detail

loadBinaryIntMatrix

public static int[][] loadBinaryIntMatrix(java.lang.String filename)
Loads an integer matrix from a binary file, optionally a zip file. The method actually reads a float matrix.

Parameters:
filename -
Returns:

loadBinaryMatrix

public static double[][] loadBinaryMatrix(java.lang.String filename)
Loads a matrix from a binary file, optionally a zip file. The method actually reads a float matrix.

Parameters:
filename -
Returns:

saveBinaryIntMatrix

public static void saveBinaryIntMatrix(java.lang.String filename,
                                       int[][] a)
Writes integer matrix to binary file. If the file name ends with zip, the output is zipped. Note: The method actually saves float values.

Parameters:
filename -
a -

saveBinaryMatrix

public static void saveBinaryMatrix(java.lang.String filename,
                                    double[][] a)
Writes matrix to binary file. If the file name ends with zip, the output is zipped. Note: The method actually saves float values.

Parameters:
filename -
a -

read

public static java.lang.Object read(java.io.DataInputStream is,
                                    java.lang.Object type)
                             throws java.io.IOException
generic method that calls the methods according to the type of the argument Object

Parameters:
is -
type -
Returns:
Throws:
java.io.IOException

readIntMatrix

public static int[][] readIntMatrix(java.io.DataInputStream bw)
                             throws java.io.IOException
Read matrix from file.

Parameters:
bw -
Returns:
Throws:
java.io.IOException

readIntVector

public static int[] readIntVector(java.io.DataInputStream bw)
                           throws java.io.IOException
Read vector from file.

Parameters:
bw -
Returns:
Throws:
java.io.IOException

readDoubleTensor

public static double[][][] readDoubleTensor(java.io.DataInputStream bw)
                                     throws java.io.IOException
Throws:
java.io.IOException

readDoubleMatrix

public static double[][] readDoubleMatrix(java.io.DataInputStream bw)
                                   throws java.io.IOException
Read matrix from file.

Parameters:
bw -
Returns:
Throws:
java.io.IOException

readDoubleVector

public static double[] readDoubleVector(java.io.DataInputStream bw)
                                 throws java.io.IOException
Read vector from file.

Parameters:
bw -
Returns:
Throws:
java.io.IOException

readFloatMatrix

public static float[][] readFloatMatrix(java.io.DataInputStream bw)
                                 throws java.io.IOException
Read matrix from file.

Parameters:
bw -
Returns:
Throws:
java.io.IOException

readFloatVector

public static float[] readFloatVector(java.io.DataInputStream bw)
                               throws java.io.IOException
Read vector from file.

Parameters:
bw -
Returns:
Throws:
java.io.IOException

write

public static void write(java.io.DataOutputStream os,
                         java.lang.Object data)
                  throws java.io.IOException
generic write method that calls the respective method of the type of data

Parameters:
writer -
data - object
Throws:
java.io.IOException

writeIntMatrix

public static void writeIntMatrix(java.io.DataOutputStream bw,
                                  int[][] matrix)
                           throws java.io.IOException
Writes an integer matrix in the format rows,cols1,a11,a12,a1...,cols2,a21,... This way, matrices can be stored that have variable row lengths.

Parameters:
bw -
matrix -
Throws:
java.io.IOException

writeIntVector

public static void writeIntVector(java.io.DataOutputStream bw,
                                  int[] vector)
                           throws java.io.IOException
Writes an integer vector in the format size,v1,v2,...

Parameters:
bw -
vector -
Throws:
java.io.IOException

writeDoubleTensor

public static void writeDoubleTensor(java.io.DataOutputStream bw,
                                     double[][][] tensor)
                              throws java.io.IOException
Writes a double tensor (here = 3d matrix).

Parameters:
bw -
tensor -
Throws:
java.io.IOException

writeDoubleMatrix

public static void writeDoubleMatrix(java.io.DataOutputStream bw,
                                     double[][] matrix)
                              throws java.io.IOException
Writes a double matrix in the format rows,cols1,a11,a12,a1...,cols2,a21,...

Parameters:
bw -
matrix -
Throws:
java.io.IOException

writeDoubleVector

public static void writeDoubleVector(java.io.DataOutputStream bw,
                                     double[] vector)
                              throws java.io.IOException
Writes a double vector in the format size,v1,v2,...

Parameters:
bw -
vector -
Throws:
java.io.IOException

writeFloatMatrix

public static void writeFloatMatrix(java.io.DataOutputStream bw,
                                    float[][] matrix)
                             throws java.io.IOException
Writes a float matrix in the format rows,cols,a11,a12,a1...,a21,...

Parameters:
bw -
matrix -
Throws:
java.io.IOException

writeFloatVector

public static void writeFloatVector(java.io.DataOutputStream bw,
                                    float[] vector)
                             throws java.io.IOException
Writes a float vector in the format size,v1,v2,...

Parameters:
bw -
vector -
Throws:
java.io.IOException

padSpace

public static java.lang.String padSpace(java.lang.String s,
                                        int length)

formatDouble

public static java.lang.String formatDouble(double d)
Parameters:
d -
Returns:

loadAscii

public static double[][] loadAscii(java.lang.String filename)

saveAscii

public static void saveAscii(java.lang.String filename,
                             double[][] a)
Parameters:
filename -
a -

saveTransposedAscii

public static void saveTransposedAscii(java.lang.String filename,
                                       double[][] a)
Parameters:
filename -
a - matrix (with equal columns for each row)

openOutputStream

public static java.io.DataOutputStream openOutputStream(java.lang.String filename)
                                                 throws java.io.FileNotFoundException,
                                                        java.io.IOException
Opens a data output stream with optional zip compression. The returned DataOutputStream can be written to and must be closed using closeStream(DataOutputStream dos) or dos.close().

Parameters:
filename -
Returns:
Throws:
java.io.FileNotFoundException
java.io.IOException

closeOutputStream

public static void closeOutputStream(java.io.DataOutputStream dos)
                              throws java.io.IOException
Close the data output, which results in flushing the write buffer and closing the file.

Parameters:
dos -
Throws:
java.io.IOException

openInputStream

public static java.io.DataInputStream openInputStream(java.lang.String filename)
                                               throws java.io.IOException,
                                                      java.io.FileNotFoundException
Opens a data input stream with optional zip compression. The returned DataInputStream can be read from and must be closed using closeStream(DataOutputStream dos) or dos.close().

Parameters:
filename -
Returns:
Throws:
java.io.FileNotFoundException
java.io.IOException

closeInputStream

public static void closeInputStream(java.io.DataInputStream dis)
                             throws java.io.IOException
Close the input stream

Parameters:
dis -
Throws:
java.io.IOException

saveShades

public static void saveShades(java.lang.String filename,
                              double[][] a)
Save a matrix as shaded values using MayaShades. The values are normalised on the maximum.

Parameters:
filename -
a -

saveTransposedShades

public static void saveTransposedShades(java.lang.String filename,
                                        double[][] a)
Save a transposed matrix as shaded values using MayaShades. The values are normalised on the maximum.

Parameters:
filename -
a - matrix with equal cols in each row