use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.
the class ScalingTest method generateSpecFile.
// ----------------------------
private void generateSpecFile(int cols, String specFile) throws IOException, Exception {
final String NAME = "name";
final String METHOD = "method";
final String SCALE_METHOD_Z = "z-score";
final String SCALE_METHOD_M = "mean-subtraction";
JSONObject outputSpec = new JSONObject();
JSONArray scaleSpec = new JSONArray();
for (int colID = 1; colID <= cols; colID++) {
JSONObject obj = new JSONObject();
obj.put(NAME, "V" + colID);
if (colID <= cols / 2)
obj.put(METHOD, SCALE_METHOD_M);
else
obj.put(METHOD, SCALE_METHOD_Z);
scaleSpec.add(obj);
}
outputSpec.put(TfUtils.TXMETHOD_SCALE, scaleSpec);
FileSystem fs = IOUtilFunctions.getFileSystem(specFile);
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fs.create(new Path(specFile), true)))) {
out.write(outputSpec.toString());
}
}
use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.
the class Connection method readDoubleMatrix.
// //////////////////////////////////////////
// Read matrices
// //////////////////////////////////////////
/**
* Reads an input matrix in arbitrary format from HDFS into a dense double array.
* NOTE: this call currently only supports default configurations for CSV.
*
* @param fname the filename of the input matrix
* @return matrix as a two-dimensional double array
* @throws IOException if IOException occurs
*/
public double[][] readDoubleMatrix(String fname) throws IOException {
try {
// read json meta data
String fnamemtd = DataExpression.getMTDFileName(fname);
JSONObject jmtd = new DataExpression().readMetadataFile(fnamemtd, false);
// parse json meta data
long rows = jmtd.getLong(DataExpression.READROWPARAM);
long cols = jmtd.getLong(DataExpression.READCOLPARAM);
int brlen = jmtd.containsKey(DataExpression.ROWBLOCKCOUNTPARAM) ? jmtd.getInt(DataExpression.ROWBLOCKCOUNTPARAM) : -1;
int bclen = jmtd.containsKey(DataExpression.COLUMNBLOCKCOUNTPARAM) ? jmtd.getInt(DataExpression.COLUMNBLOCKCOUNTPARAM) : -1;
long nnz = jmtd.containsKey(DataExpression.READNUMNONZEROPARAM) ? jmtd.getLong(DataExpression.READNUMNONZEROPARAM) : -1;
String format = jmtd.getString(DataExpression.FORMAT_TYPE);
InputInfo iinfo = InputInfo.stringExternalToInputInfo(format);
// read matrix file
return readDoubleMatrix(fname, iinfo, rows, cols, brlen, bclen, nnz);
} catch (Exception ex) {
throw new IOException(ex);
}
}
use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.
the class Connection method readStringFrame.
// //////////////////////////////////////////
// Read frames
// //////////////////////////////////////////
/**
* Reads an input frame in arbitrary format from HDFS into a dense string array.
* NOTE: this call currently only supports default configurations for CSV.
*
* @param fname the filename of the input frame
* @return frame as a two-dimensional string array
* @throws IOException if IOException occurs
*/
public String[][] readStringFrame(String fname) throws IOException {
try {
// read json meta data
String fnamemtd = DataExpression.getMTDFileName(fname);
JSONObject jmtd = new DataExpression().readMetadataFile(fnamemtd, false);
// parse json meta data
long rows = jmtd.getLong(DataExpression.READROWPARAM);
long cols = jmtd.getLong(DataExpression.READCOLPARAM);
String format = jmtd.getString(DataExpression.FORMAT_TYPE);
InputInfo iinfo = InputInfo.stringExternalToInputInfo(format);
// read frame file
return readStringFrame(fname, iinfo, rows, cols);
} catch (Exception ex) {
throw new IOException(ex);
}
}
use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.
the class Connection method convertToMatrix.
/**
* Converts an input stream of a string matrix in csv or textcell format
* into a matrix block. The meta data string is the SystemML generated
* .mtd file including the number of rows and columns.
*
* @param input InputStream to a string matrix in csv or textcell format
* @param meta string representing SystemML matrix metadata in JSON format
* @return matrix as a matrix block
* @throws IOException if IOException occurs
*/
public MatrixBlock convertToMatrix(InputStream input, String meta) throws IOException {
try {
// parse json meta data
JSONObject jmtd = new JSONObject(meta);
int rows = jmtd.getInt(DataExpression.READROWPARAM);
int cols = jmtd.getInt(DataExpression.READCOLPARAM);
String format = jmtd.getString(DataExpression.FORMAT_TYPE);
// parse the input matrix
return convertToMatrix(input, rows, cols, format);
} catch (Exception ex) {
throw new IOException(ex);
}
}
use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.
the class Connection method convertToFrame.
/**
* Converts an input stream of a string frame in csv or textcell format
* into a frame block. The meta data string is the SystemML generated
* .mtd file including the number of rows and columns.
*
* @param input InputStream to a string frame in csv or textcell format
* @param meta string representing SystemML frame metadata in JSON format
* @return frame as a frame block
* @throws IOException if IOException occurs
*/
public FrameBlock convertToFrame(InputStream input, String meta) throws IOException {
try {
// parse json meta data
JSONObject jmtd = new JSONObject(meta);
int rows = jmtd.getInt(DataExpression.READROWPARAM);
int cols = jmtd.getInt(DataExpression.READCOLPARAM);
String format = jmtd.getString(DataExpression.FORMAT_TYPE);
// parse the input frame
return convertToFrame(input, rows, cols, format);
} catch (Exception ex) {
throw new IOException(ex);
}
}
Aggregations