Skip to content

Instantly share code, notes, and snippets.

@jaor
Last active June 28, 2019 15:31
Show Gist options
  • Save jaor/585a76f48f8ae6545030bf4ebaeb1af6 to your computer and use it in GitHub Desktop.
Save jaor/585a76f48f8ae6545030bf4ebaeb1af6 to your computer and use it in GitHub Desktop.
Anomalies above/below threshold
{
"name": "Anomalies above threshold",
"kind": "script",
"description": "Creates an anomaly detector and a dataset with all the anomalies above or below a given threshold",
"source_code": "script.whizzml",
"inputs":[
{
"name": "dataset",
"type": "dataset-id",
"description": "Anomaly to use"
},
{
"name": "threshold",
"type": "number",
"description": "Anomaly score threshold (0-100)"
},
{
"name": "above",
"type": "boolean",
"default": false,
"description": "If true, keep anomalies above threshold, below otherwise"
}
],
"outputs":[
{
"name": "anomaly",
"type": "anomaly-id",
"description": "The anomaly detector"
},
{
"name": "dataset",
"type": "dataset-id",
"description": "Dataset with instances with a score above or below `threshold`"
}]
}
(define (batch-threshold dataset threshold above?)
(when (not (<= 0 threshold 100))
(raise "Threshold must be in the range [0, 100]"))
(let (anomaly (create-anomaly dataset)
ba (create-batchanomalyscore anomaly
dataset
{"output_dataset" true "all_fields" true})
ds (resource-property (wait ba) "output_dataset_resource")
th (/ threshold 100)
cmp (if above? ">" "<")
fltr (flatline "({cmp} (f \"score\") {th})")
fds (wait (create-dataset ds {"lisp_filter" fltr})))
(delete* [ba ds])
[anomaly fds]))
(define [anomaly dataset] (batch-threshold dataset threshold above))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment