@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); if (!(o instanceof List)) { throw new WarpScriptException( getName() + " expects a list of options on top of the stack."); } List<Object> options = (List<Object>) o; o = stack.pop(); if (!(o instanceof Long)) { throw new WarpScriptException( getName() + " expects a maximum latency under the list of options."); } long maxLatency = (long) o; o = stack.pop(); if (!(o instanceof Long)) { throw new WarpScriptException( getName() + " expects a minimum latency under the list of options."); } long minLatency = ((Long) o).longValue(); stack.push(new LatencyFilter(getName(), minLatency, maxLatency, options)); return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object n = stack.pop(); if (!(n instanceof Number)) { throw new WarpScriptException(getName() + " expects a number as parameter."); } stack.setAttribute(WarpScriptStack.ATTRIBUTE_DEBUG_DEPTH, ((Number) n).intValue()); return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); if (!(o instanceof String)) { throw new WarpScriptException(getName() + " operates on a String."); } stack.push(BaseEncoding.base64().encode(Hex.decode(o.toString()))); return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object op2 = stack.pop(); Object op1 = stack.pop(); if (op2 instanceof Boolean && op1 instanceof Boolean) { stack.push(((Boolean) op1).booleanValue() && ((Boolean) op2).booleanValue()); } else { throw new WarpScriptException(getName() + " can only operate on boolean values."); } return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { List<Object> params = ProcessingUtil.parseParams(stack, 1); PGraphics pg = (PGraphics) params.get(0); double saturation = pg.saturation(((Number) params.get(1)).intValue()); stack.push(pg); stack.push(saturation); return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); if (!(o instanceof byte[])) { throw new WarpScriptException(getName() + " expects a byte array on top of the stack."); } try { PickleSerializer serializer = new PickleSerializer(); Object out = serializer.deserializeData((byte[]) o); stack.push(out); } catch (IOException ioe) { throw new WarpScriptException(ioe); } return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); JsonSerializer parser = BOON_SERIALIZER_FACTORY.create(); // // Only allow the serialization of simple lists and maps, otherwise JSON might // expose internals // if (o instanceof List) { for (Object elt : (List) o) { if (!(elt instanceof Number) && !(elt instanceof String) && !(elt instanceof Boolean)) { throw new WarpScriptException( getName() + " can only handle numeric, boolean and string types."); } } } else if (o instanceof Map) { for (Entry<Object, Object> entry : ((Map<Object, Object>) o).entrySet()) { Object elt = entry.getKey(); if (!(elt instanceof Number) && !(elt instanceof String) && !(elt instanceof Boolean)) { throw new WarpScriptException( getName() + " can only handle numeric, boolean and string types."); } elt = entry.getValue(); if (!(elt instanceof Number) && !(elt instanceof String) && !(elt instanceof Boolean)) { throw new WarpScriptException( getName() + " can only handle numeric, boolean and string types."); } } } else { throw new WarpScriptException(getName() + " can only handle simple lists/maps."); } String json = parser.serialize(o).toString(); stack.push(json); return stack; }
@Override protected Map<String, Object> retrieveParameters(WarpScriptStack stack) throws WarpScriptException { Map<String, Object> params = new HashMap<String, Object>(); Object top = stack.pop(); boolean alpha_is_default = false; if (!(top instanceof Double)) { if (!(top instanceof Boolean)) { throw new WarpScriptException( getName() + " expects a significance level (a DOUBLE) or a flag (a BOOLEAN) on top of the stack."); } else { alpha_is_default = true; } } if (!alpha_is_default) { params.put(SIGNIFICANCE_PARAM, ((Number) top).doubleValue()); top = stack.pop(); } else { params.put(SIGNIFICANCE_PARAM, SIGNIFICANCE_DEFAULT); } if (!(top instanceof Boolean)) { throw new WarpScriptException( getName() + " expects a flag (a BOOLEAN) that indicates wether to use modified z-score below the significance level."); } params.put(MODIFIED_PARAM, ((Boolean) top).booleanValue()); return params; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object list = stack.pop(); if (list instanceof Object[]) { stack.push(new WarpScriptStack.Mark()); for (Object o : (Object[]) list) { stack.push(o); } } else if (list instanceof List) { stack.push(new WarpScriptStack.Mark()); for (Object o : (List<Object>) list) { stack.push(o); } } else { stack.push(list); } return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); if (!(o instanceof Number)) { throw new WarpScriptException( getName() + " expects a numeric threshold on top of the stack."); } double threshold = ((Number) o).doubleValue(); o = stack.pop(); if (!(o instanceof List)) { throw new WarpScriptException( getName() + " expects a query number list below the threshold."); } double[] query = new double[((List) o).size()]; int i = 0; for (Object oo : (List) o) { query[i++] = ((Number) oo).doubleValue(); } o = stack.pop(); if (!(o instanceof List)) { throw new WarpScriptException(getName() + " expects a list of numbers below the query list."); } double[] sequence = new double[((List) o).size()]; i = 0; for (Object oo : (List) o) { sequence[i++] = ((Number) oo).doubleValue(); } if (sequence.length <= query.length) { throw new WarpScriptException( getName() + " expects the query list to be shorter than the sequence list."); } double mindist = Double.POSITIVE_INFINITY; List<Integer> bestmatches = new ArrayList<Integer>(); for (i = 0; i <= sequence.length - query.length; i++) { double dist = dtw.compute(query, 0, query.length, sequence, i, query.length, mindist); if (dist < 0) { continue; } if (dist < mindist) { mindist = dist; bestmatches.clear(); } bestmatches.add(i); } stack.push(bestmatches); stack.push(mindist); return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); if (!(o instanceof String)) { throw new WarpScriptException(getName() + " operates on a String."); } String bin = o.toString(); if (bin.length() % 8 != 0) { bin = "0000000".substring(7 - (bin.length() % 8)) + bin; } char[] chars = UnsafeString.getChars(bin); StringBuilder sb = new StringBuilder(); byte curbyte = 0; byte[] bytes = new byte[bin.length() / 8]; int byteidx = 0; for (int i = 0; i < bin.length(); i += 4) { curbyte <<= 4; String nibble = new String(chars, 0, 4); if ("0000".equals(nibble)) { curbyte |= 0; } else if ("0001".equals(nibble)) { curbyte |= 0x1; } else if ("0010".equals(nibble)) { curbyte |= 0x2; } else if ("0011".equals(nibble)) { curbyte |= 0x3; } else if ("0100".equals(nibble)) { curbyte |= 0x4; } else if ("0101".equals(nibble)) { curbyte |= 0x5; } else if ("0110".equals(nibble)) { curbyte |= 0x6; } else if ("0111".equals(nibble)) { curbyte |= 0x7; } else if ("1000".equals(nibble)) { curbyte |= 0x8; } else if ("1001".equals(nibble)) { curbyte |= 0x9; } else if ("1010".equals(nibble)) { curbyte |= 0xA; } else if ("1011".equals(nibble)) { curbyte |= 0xB; } else if ("1100".equals(nibble)) { curbyte |= 0xC; } else if ("1101".equals(nibble)) { curbyte |= 0xD; } else if ("1110".equals(nibble)) { curbyte |= 0xE; } else if ("1111".equals(nibble)) { curbyte |= 0xF; } if (i > 0 && 0 == i % 8) { bytes[byteidx++] = curbyte; curbyte = 0; } } stack.push(bytes); return stack; }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object threshold = stack.pop(); stack.push(new FilterLastLE(getName(), threshold)); return stack; }