Skip to content

Instantly share code, notes, and snippets.

@jaor
Last active November 3, 2020 04:33
Show Gist options
  • Save jaor/5b48c322f7a14f663300f52d2b528df1 to your computer and use it in GitHub Desktop.
Save jaor/5b48c322f7a14f663300f52d2b528df1 to your computer and use it in GitHub Desktop.
Use directory as label field
{
"name": "path-to-label",
"kind": "script",
"description": "Extracts the last component of paths as the label",
"source_code": "script.whizzml",
"imports":[
],
"inputs":[
{
"name": "source",
"type": "source-id",
"default": "",
"description": "The composite image source, or empty to use dataset"
},
{
"name": "dataset",
"type": "dataset-id",
"default": "",
"description": "The images dataset, or empty to use source"
},
{
"name": "path-field",
"type": "string",
"default": "filename",
"description": "Name of the field containing image paths"
}
],
"outputs":[
{
"name": "result",
"type": "dataset-id",
"description": "The resulting labeled dataset"
}]
}
(define (flatexp field-name)
(flatline "(replace (f {{field-name}}) \"([^/]+/)*([^/]+)/[^/]+\" \"$2\")"))
(define (add-label dataset field-name)
(log-info "Adding label using field " field-name "...")
(wait (create-dataset dataset
{"new_fields" [{"name" "label"
"field" (flatexp field-name)}]
"refresh_objective" true})))
(define (do-it source-id path-name)
(log-info "Closing input source...")
(wait (update source-id {"closed" true}))
(log-info "Creating dataset...")
(let (ds (wait (create-dataset source-id))
res (wait (add-label ds path-name)))
(delete ds)
res))
(define result
(if (empty? source)
(add-label dataset path-field)
(do-it source path-field)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment