Search in sources :

Example 16 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class TfMetaUtils method parseJsonIDList.

/**
	 * TODO consolidate external and internal json spec definitions
	 * 
	 * @param spec transform specification as json string
	 * @param colnames column names
	 * @param group ?
	 * @return list of column ids
	 * @throws JSONException if JSONException occurs
	 */
public static int[] parseJsonIDList(JSONObject spec, String[] colnames, String group) throws JSONException {
    int[] colList = new int[0];
    boolean ids = spec.containsKey("ids") && spec.getBoolean("ids");
    if (spec.containsKey(group)) {
        //parse attribute-array or plain array of IDs
        JSONArray attrs = null;
        if (spec.get(group) instanceof JSONObject) {
            attrs = (JSONArray) ((JSONObject) spec.get(group)).get(TfUtils.JSON_ATTRS);
            //file-based transform outputs ids w/o id tags
            ids = true;
        } else
            attrs = (JSONArray) spec.get(group);
        //construct ID list array
        colList = new int[attrs.size()];
        for (int i = 0; i < colList.length; i++) {
            colList[i] = ids ? UtilFunctions.toInt(attrs.get(i)) : (ArrayUtils.indexOf(colnames, attrs.get(i)) + 1);
            if (colList[i] <= 0) {
                throw new RuntimeException("Specified column '" + attrs.get(i) + "' does not exist.");
            }
        }
        //ensure ascending order of column IDs
        Arrays.sort(colList);
    }
    return colList;
}
Also used : DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) JSONObject(org.apache.wink.json4j.JSONObject) JSONArray(org.apache.wink.json4j.JSONArray)

Example 17 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class TfMetaUtils method parseRecodeColIDs.

/**
	 * Parses the given json specification and extracts a list of column ids
	 * that are subject to recoding.
	 * 
	 * @param spec transform specification as json string
	 * @param colnames column names
	 * @return list of column ids
	 * @throws IOException if IOException occurs
	 */
@SuppressWarnings("unchecked")
private static List<Integer> parseRecodeColIDs(String spec, String[] colnames) throws IOException {
    if (spec == null)
        throw new IOException("Missing transform specification.");
    List<Integer> specRecodeIDs = null;
    try {
        //parse json transform specification for recode col ids
        JSONObject jSpec = new JSONObject(spec);
        List<Integer> rcIDs = Arrays.asList(ArrayUtils.toObject(TfMetaUtils.parseJsonIDList(jSpec, colnames, TfUtils.TXMETHOD_RECODE)));
        List<Integer> dcIDs = Arrays.asList(ArrayUtils.toObject(TfMetaUtils.parseJsonIDList(jSpec, colnames, TfUtils.TXMETHOD_DUMMYCODE)));
        specRecodeIDs = new ArrayList<Integer>(CollectionUtils.union(rcIDs, dcIDs));
    } catch (Exception ex) {
        throw new IOException(ex);
    }
    return specRecodeIDs;
}
Also used : JSONObject(org.apache.wink.json4j.JSONObject) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) JSONException(org.apache.wink.json4j.JSONException) IOException(java.io.IOException)

Example 18 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class TfMetaUtils method parseJsonObjectIDList.

public static int[] parseJsonObjectIDList(JSONObject spec, String[] colnames, String group) throws JSONException {
    int[] colList = new int[0];
    boolean ids = spec.containsKey("ids") && spec.getBoolean("ids");
    if (spec.containsKey(group) && spec.get(group) instanceof JSONArray) {
        JSONArray colspecs = (JSONArray) spec.get(group);
        colList = new int[colspecs.size()];
        for (int j = 0; j < colspecs.size(); j++) {
            JSONObject colspec = (JSONObject) colspecs.get(j);
            colList[j] = ids ? colspec.getInt("id") : (ArrayUtils.indexOf(colnames, colspec.get("name")) + 1);
            if (colList[j] <= 0) {
                throw new RuntimeException("Specified column '" + colspec.get(ids ? "id" : "name") + "' does not exist.");
            }
        }
        //ensure ascending order of column IDs
        Arrays.sort(colList);
    }
    return colList;
}
Also used : DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) JSONObject(org.apache.wink.json4j.JSONObject) JSONArray(org.apache.wink.json4j.JSONArray)

Example 19 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class EncoderFactory method createEncoder.

@SuppressWarnings("unchecked")
public static Encoder createEncoder(String spec, String[] colnames, ValueType[] schema, FrameBlock meta) throws DMLRuntimeException {
    Encoder encoder = null;
    int clen = schema.length;
    try {
        //parse transform specification
        JSONObject jSpec = new JSONObject(spec);
        List<Encoder> lencoders = new ArrayList<Encoder>();
        //prepare basic id lists (recode, dummycode, pass-through)
        //note: any dummycode column requires recode as preparation
        List<Integer> rcIDs = Arrays.asList(ArrayUtils.toObject(TfMetaUtils.parseJsonIDList(jSpec, colnames, TfUtils.TXMETHOD_RECODE)));
        List<Integer> dcIDs = Arrays.asList(ArrayUtils.toObject(TfMetaUtils.parseJsonIDList(jSpec, colnames, TfUtils.TXMETHOD_DUMMYCODE)));
        rcIDs = new ArrayList<Integer>(CollectionUtils.union(rcIDs, dcIDs));
        List<Integer> binIDs = TfMetaUtils.parseBinningColIDs(jSpec, colnames);
        List<Integer> ptIDs = new ArrayList<Integer>(CollectionUtils.subtract(CollectionUtils.subtract(UtilFunctions.getSequenceList(1, clen, 1), rcIDs), binIDs));
        List<Integer> oIDs = Arrays.asList(ArrayUtils.toObject(TfMetaUtils.parseJsonIDList(jSpec, colnames, TfUtils.TXMETHOD_OMIT)));
        List<Integer> mvIDs = Arrays.asList(ArrayUtils.toObject(TfMetaUtils.parseJsonObjectIDList(jSpec, colnames, TfUtils.TXMETHOD_IMPUTE)));
        //create individual encoders
        if (!rcIDs.isEmpty()) {
            RecodeAgent ra = new RecodeAgent(jSpec, colnames, clen);
            ra.setColList(ArrayUtils.toPrimitive(rcIDs.toArray(new Integer[0])));
            lencoders.add(ra);
        }
        if (!ptIDs.isEmpty())
            lencoders.add(new EncoderPassThrough(ArrayUtils.toPrimitive(ptIDs.toArray(new Integer[0])), clen));
        if (!dcIDs.isEmpty())
            lencoders.add(new DummycodeAgent(jSpec, colnames, schema.length));
        if (!binIDs.isEmpty())
            lencoders.add(new BinAgent(jSpec, colnames, schema.length, true));
        if (!oIDs.isEmpty())
            lencoders.add(new OmitAgent(jSpec, colnames, schema.length));
        if (!mvIDs.isEmpty()) {
            MVImputeAgent ma = new MVImputeAgent(jSpec, colnames, schema.length);
            ma.initRecodeIDList(rcIDs);
            lencoders.add(ma);
        }
        //create composite decoder of all created encoders
        //and initialize meta data (recode, dummy, bin, mv)
        encoder = new EncoderComposite(lencoders);
        if (meta != null)
            encoder.initMetaData(meta);
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
    return encoder;
}
Also used : RecodeAgent(org.apache.sysml.runtime.transform.RecodeAgent) ArrayList(java.util.ArrayList) BinAgent(org.apache.sysml.runtime.transform.BinAgent) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) JSONObject(org.apache.wink.json4j.JSONObject) DummycodeAgent(org.apache.sysml.runtime.transform.DummycodeAgent) OmitAgent(org.apache.sysml.runtime.transform.OmitAgent) MVImputeAgent(org.apache.sysml.runtime.transform.MVImputeAgent)

Example 20 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class AutomatedTestBase method readDMLMetaDataValueType.

public static ValueType readDMLMetaDataValueType(String fileName) {
    try {
        String fname = baseDirectory + OUTPUT_DIR + fileName + ".mtd";
        JSONObject meta = new DataExpression().readMetadataFile(fname, false);
        return ValueType.valueOf(meta.get(DataExpression.VALUETYPEPARAM).toString().toUpperCase());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) JSONObject(org.apache.wink.json4j.JSONObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IOException(java.io.IOException)

Aggregations

JSONObject (org.apache.wink.json4j.JSONObject)23 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)14 IOException (java.io.IOException)12 Path (org.apache.hadoop.fs.Path)8 JSONArray (org.apache.wink.json4j.JSONArray)7 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 FileSystem (org.apache.hadoop.fs.FileSystem)5 DMLException (org.apache.sysml.api.DMLException)4 DataExpression (org.apache.sysml.parser.DataExpression)4 ParseException (org.apache.sysml.parser.ParseException)4 BufferedWriter (java.io.BufferedWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)2 FrameObject (org.apache.sysml.runtime.controlprogram.caching.FrameObject)2 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)2 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)2 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)2 InputInfo (org.apache.sysml.runtime.matrix.data.InputInfo)2