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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations