|
| 1 | +let dbscanModel; |
| 2 | +const width = 640; |
| 3 | +const height = 480; |
| 4 | +const colDict = { |
| 5 | + 0: "skyblue", |
| 6 | + 1: "coral", |
| 7 | + 2: "olive", |
| 8 | + 3: "tan", |
| 9 | + 4: "grey", |
| 10 | +}; |
| 11 | + |
| 12 | +// ----- Initialize the example: ------ // |
| 13 | +function init() { |
| 14 | + // make all those nice buttons |
| 15 | + createButtons(); |
| 16 | + // start with 3 cluster |
| 17 | + make(1.55); |
| 18 | +} |
| 19 | +init(); |
| 20 | + |
| 21 | +// STEP 1: |
| 22 | +// create all the buttons |
| 23 | +function createButtons() { |
| 24 | + addClusterButton(1.55); |
| 25 | + addClusterButton(1.56); |
| 26 | + addClusterButton(2); |
| 27 | + addClusterButton("circle"); |
| 28 | + addClusterButton("moon"); |
| 29 | +} |
| 30 | + |
| 31 | +// STEP 2: |
| 32 | +// create the model |
| 33 | +function make(eps) { |
| 34 | + const options = { |
| 35 | + eps: eps, |
| 36 | + minPts: 3, |
| 37 | + }; |
| 38 | + // if moon or circle data, set the options to 0.1 and 0.16 eps |
| 39 | + if (eps === "moon") { |
| 40 | + options.eps = 0.1; |
| 41 | + } else if (eps === "circle") { |
| 42 | + options.eps = 0.16; |
| 43 | + } |
| 44 | + console.log(eps, options.eps); |
| 45 | + |
| 46 | + // get the path to the data in our data folder dynamically |
| 47 | + const dataPath = `data/gaussian2d_${eps}clusters.csv`; |
| 48 | + // create a new dbscan clustering each time on make() |
| 49 | + dbscanModel = ml5.dbscan(dataPath, options, modelReady); |
| 50 | +} |
| 51 | + |
| 52 | +// Step 3: |
| 53 | +// when the model is ready, make the chart |
| 54 | +function modelReady() { |
| 55 | + console.log(dbscanModel.dataset); |
| 56 | + makeChart(); |
| 57 | +} |
| 58 | + |
| 59 | +// Step 4: |
| 60 | +// use the fancy d3 to make magic |
| 61 | +function makeChart() { |
| 62 | + const { dataset } = dbscanModel; |
| 63 | + // clear the chart each time |
| 64 | + // less efficient, but simple |
| 65 | + d3.select("svg").remove(); |
| 66 | + |
| 67 | + // reappend the svg to the chart area |
| 68 | + const svg = d3 |
| 69 | + .select("#chart") |
| 70 | + .append("svg") |
| 71 | + .attr("width", width) |
| 72 | + .attr("height", height); |
| 73 | + |
| 74 | + // d[0] is for the x value in the array |
| 75 | + const xScale = d3 |
| 76 | + .scaleLinear() |
| 77 | + .domain(d3.extent(dataset, d => d[0])) |
| 78 | + .range([10, width - 100]); |
| 79 | + |
| 80 | + // d[1] is for the y value in the array |
| 81 | + const yScale = d3 |
| 82 | + .scaleLinear() |
| 83 | + .domain(d3.extent(dataset, d => d[1])) |
| 84 | + .range([height - 50, 20]); |
| 85 | + |
| 86 | + svg |
| 87 | + .selectAll("circle") |
| 88 | + .data(dataset) |
| 89 | + .enter() |
| 90 | + .append("circle") |
| 91 | + .attr("cx", d => xScale(d[0])) |
| 92 | + .attr("cy", d => yScale(d[1])) |
| 93 | + .attr("r", 6) |
| 94 | + .attr("fill", "black"); |
| 95 | + |
| 96 | + d3.select("svg") |
| 97 | + .selectAll("circle") |
| 98 | + .transition() |
| 99 | + .attr("fill", (d, i) => colDict[dataset[i].clusterid]); |
| 100 | +} |
| 101 | + |
| 102 | +// adds the buttons for the respective cluster data |
| 103 | +// we could also use d3.append() and d3.select() here :) |
| 104 | +function addClusterButton(eps, minPts = 3) { |
| 105 | + const btn = document.createElement("BUTTON"); |
| 106 | + btn.innerText = `cluster: ${eps} & minPts: ${minPts}`; |
| 107 | + |
| 108 | + btn.addEventListener("click", function(e) { |
| 109 | + make(eps); |
| 110 | + }); |
| 111 | + |
| 112 | + document.querySelector("#buttons").appendChild(btn); |
| 113 | + |
| 114 | + return btn; |
| 115 | +} |
0 commit comments