Published
Edited
Mar 11, 2021
1 fork
Insert cell
md`# Draco`
Insert cell
raw_data_url = 'https://localhost:4443/time-series/freqa.json'
Insert cell
prep_data_url = 'https://localhost:4443/time-series/freq_prep.json'
Insert cell
raw_freqs = FileAttachment("freqa.json").json()
Insert cell
freqs = raw_freqs.map(r => ({ id: r.recordid, ...r.fields }))
Insert cell
import { Select } from "@observablehq/inputs"
Insert cell
JSON.stringify(freqs)
Insert cell
asp_prog = createDracoProgram(freqs) //`data("${prep_data_url}").\n` +
Insert cell
computed_spec = {
var spec = solution.specs[0];

// Ensures that the spec embeds the data rather than fetch from a URLkj
delete spec.data.url;
spec.data.values = freqs;
return spec;
}
Insert cell
short_computed_spec = {
var spec = solution.specs[0];

// Ensures that the spec embeds the data rather than fetch from a URLkj
delete spec.data.url;
spec.data.values = freqs.slice(250);
return spec;
}
Insert cell
Insert cell
Insert cell
embed(computed_spec)
Insert cell
Insert cell
JSON.stringify(computed_spec)
Insert cell
JSON.stringify(short_computed_spec)
Insert cell
solution = solver.solve(asp_prog)
Insert cell
solver = {
let s = await new draco.default('https://unpkg.com/wasm-clingo@0.2.0').init();
s.schema = schema;
return s;
}
Insert cell
schema = {
return draco.data2schema(freqs);
}
Insert cell
feature_labels = Object.entries(schema.stats).map(([key, value]) => {
return { label: key + '/' + value.type + '/' + value.distinct, key: key };
})
Insert cell
selected_features
Insert cell
selected_features_ids = selected_features.map(r => r.key)
Insert cell
not_binned_features_ids = not_binned_features.map(r => r.key)
Insert cell
asp = draco.schema2asp(schema)
Insert cell
createDracoProgram = function(data) {
var data_def = asp.join("\n");
console.log("data definitions", data_def);
var query_constraints = "";
var c = 0;
for (var stat in schema.stats) {
if (selected_features_ids.includes(stat)) {
query_constraints += '\nencoding(e' + c + ').';
query_constraints += '\n:- not field(e' + c + ',"' + stat + '").';
}
if (not_binned_features_ids.includes(stat)) {
query_constraints += '\n:- bin(e' + c + ',_).';
}
c += 1;
}
console.log("query constraints", query_constraints);
console.log("final asp", data_def + query_constraints);
return data_def + query_constraints;
}
Insert cell
draco = require('draco-vis')
Insert cell
d3 = require('d3')
Insert cell
vgdata = require('datalib')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more