Public
Edited
Dec 2, 2022
Insert cell
Insert cell
ensemble_params = get_ensemble_params(20)
Insert cell
ensemble = get_ensemble_ellipses_sdfs(300, 300, ensemble_params).map(m => m.map(r => r.map(val => val >= 0 ? 1.0 : 0.0)))
Insert cell
member1 = {
let out = [];
ensemble[2].forEach((r, ridx) => r.forEach((val, cidx) => out.push({r: ridx, c: cidx, value: val, opacity: "y"})))
return out;
}
Insert cell
member2 = {
let out = [];
ensemble[4].forEach((r, ridx) => r.forEach((val, cidx) => out.push({r: ridx, c: cidx, value: val, opacity: "y"})))
return out;
}
Insert cell
Plot.rect(member, {x: "c", y: "r", fill: "value", fillOpacity: 0}).plot()
Insert cell
{
let canvas = DOM.canvas(300, 300);
let ctx = canvas.getContext("2d");
member1.forEach(d => {
ctx.fillStyle = d.value === 1 ? "rgba(255,255,255,0.5)" : "rgba(0,0,0,1.0)";
ctx.fillRect(d.c, d.r, 1, 1);
});
member2.forEach(d => {
ctx.fillStyle = d.value === 1 ? "rgba(255,255,255,0.5)" : "rgba(0,0,0,1.0)";
ctx.fillRect(d.c, d.r, 1, 1);
});
return canvas;
}
Insert cell
Insert cell
function compute_pairwise_distances(shapes){
let dmat = [];
for(let i=0; i<shapes.length; i++){
for(let j=0; j<shapes.length; j++){
dmat.push({i, j, dist: get_dice_coefficient(shapes[i], shapes[j])});
}
}
return dmat;
}
Insert cell
pw_mat = compute_pairwise_distances(ensemble)
Insert cell
{
let num_members = ensemble.length;
let size_bin = 30;
let canvas = DOM.canvas(num_members*size_bin, num_members*size_bin);
let ctx = canvas.getContext("2d");

let color_scale = d3.scaleSequential(d3.extent(pw_mat, d => d.dist), d3.interpolateBrBG);

pw_mat.forEach(d => {
ctx.fillStyle = color_scale(d.dist);
ctx.fillRect(d.i * size_bin, d.j * size_bin, size_bin, size_bin);
});
return canvas;
}
Insert cell
d3 = require("d3")
Insert cell
get_dice_coefficient(ensemble[0], ensemble[1])
Insert cell
function get_dice_coefficient(A, B){
// A and B are two dimensional arrays
let flat_A = flatten(A);
let flat_B = flatten(B);
let intersection = 0;
let union = 0;
if(flat_A.length == flat_B.length){
for(let i=0; i<flat_A.length; i++){
intersection += flat_A[i] === 1 && flat_B[i] === 1 ? 1 : 0;
union += flat_A[i] === 1 || flat_B[i] === 1 ? 2 : 0;
}
return (2*intersection)/union;
}else{
return null;
}
}
Insert cell
A = [[1,1,1],[0,1,0],[0,0,0]]
Insert cell
B = [[1,1,1],[0,1,0],[0,0,0]]
Insert cell
get_dice_coefficient(A, B)
Insert cell
Insert cell
Insert cell
Insert cell
import {get_ensemble_params, get_ensemble_ellipses_sdfs} from "a6d2d5f3cf3ef0a4"
Insert cell
import {flatten, render_image} from "1f9d2894b7a3668f"
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