Skip to content

Instantly share code, notes, and snippets.

@jaor
Last active January 18, 2021 02:08
Show Gist options
  • Save jaor/f11004d6ccd1ceeeb08dbc0953563d39 to your computer and use it in GitHub Desktop.
Save jaor/f11004d6ccd1ceeeb08dbc0953563d39 to your computer and use it in GitHub Desktop.
table+image -> image composite
{
"name": "table+image->image",
"kind": "script",
"description": "Convert a table+image source to an editable composite",
"source_code": "script.whizzml",
"imports":[
],
"inputs":[
{
"name": "input",
"type": "source-id",
"description": "The table+image to convert"
}],
"outputs":[
{
"name": "output",
"type": "source-id",
"description": "The new image composite"
}]
}
(define (make-res-data source-id)
(log-info "Cloning input data...")
(let (clone (create-source {"origin" source-id})
clone (update clone {"image_analysis" {"enabled" false}})
dataset (wait (create-dataset clone))
table (resource-property clone "table_component")
imgs (filter (lambda (x) (!= table x)) (resource-property clone "sources"))
res (create-source {"sources" imgs
"name" (str "editable "
(resource-property source-id "name"))}))
(delete clone {"delete_all" true})
[(wait res) dataset]))
(define (label-fields dataset)
(let (fds (filter (lambda (f)
(let (o (f "optype"))
(and (!= o "image") (!= o "path"))))
(values (resource-fields dataset))))
(map (lambda (f) {"name" (f "name") "optype" (f "optype")}) fds)))
(define (add-new-fields src ds)
(let (nfds (label-fields ds))
(update src {"new_fields" nfds})
(map (lambda (f) (f "name")) nfds)))
(define (rows-to-values rows names)
(iterate (res [] row rows)
(let (id (head row)
vs (map (lambda (v name)
{"field" name "value" v "components" [id]})
(tail row)
names))
(concat res vs))))
(define (update-row-values src vs)
(when (not (empty? vs))
(update-and-wait (wait src) {"row_values" (take 5 vs)})
(update-row-values src (drop 5 vs))))
(define (add-values offset max-rows sample src names)
(log-progress (+ 0.3 (* 0.65 (- 1 (/ (- max-rows offset) max-rows)))))
(when (< offset max-rows)
(let (rows ((fetch sample {"mode" "linear"
"row_offset" offset
"rows" 100})
["sample" "rows"]
[]))
(when (not (empty? rows))
(log-info "Importing images [" offset " " (+ offset (count rows)) ")")
(update-row-values src (rows-to-values rows names))
(add-values (+ offset (count rows)) max-rows sample src names)))))
(define (add-new-fields-values src ds names)
(let (img-field (head (filter (lambda (f) (= "image" (f "optype")))
(values (resource-fields ds))))
fnames (cons (img-field "name") names)
sample (create-sample {"dataset" ds "input_fields" fnames})
rows (resource-property (wait sample) "rows"))
(log-info "Importing data for " rows " images...")
(add-values 0 rows sample src names)))
(define (table+image->image src-id)
(let ([src ds] (make-res-data src-id)
names (add-new-fields src ds))
(log-info "Importing new fields: " names)
(add-new-fields-values src ds names)
(delete ds)
src))
(define output (table+image->image input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment