-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathmain.js
More file actions
70 lines (57 loc) · 2.48 KB
/
Copy pathmain.js
File metadata and controls
70 lines (57 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// this API Key will only work on Algorithmia's website; get your own key at https://algorithmia.com/user#credentials
var apiKey = 'simRN+PoQevVd6ePYU9t/kEH4MM1'
var svg = d3.select("svg.viz");
var n = 40; // Number of points
var dataPoints = []; // Point data
// Color scale
var colorScale = d3.scale.category10();
var client = Algorithmia.client(apiKey, 'https://api.algorithmia.com/v1/web/algo');
// Generate initial data
window.addEventListener("load", function() {
generateData();
updateD3();
});
// Setup mappings from data to visualization using D3
function updateD3() {
Algorithmia.viz.drawPoints(svg, dataPoints, "points", 4, colorScale);
};
// Generate random data
function generateData() {
var width = $(".viz-container").width();
var height = $(".viz-container").height();
dataPoints.length = 0;
for(var i = 0; i < n; i++) {
randomPoint = [Math.random() * width, Math.random() * height];
dataPoints.push(randomPoint);
}
// Print debug output
var numTopics = document.getElementById("numClusters").value;
var algorithmInput = [dataPoints, numTopics];
// document.getElementById("geodata-out").innerHTML = "<pre>" + JSON.stringify(algorithmInput, null, 2) + "</pre>";
document.getElementById("cluster-in").innerHTML = "<pre>" + JSON.stringify(algorithmInput, null, 2) + "</pre>";
document.getElementById("cluster-out").innerHTML = "";
updateD3();
};
// Cluster the dataPoints using Algorithmia k-means clustering
function cluster() {
document.getElementById("demo-status").innerHTML = "";
// Algorithm input
var numTopics = document.getElementById("numClusters").value;
var algorithmInput = [dataPoints, numTopics];
document.getElementById("cluster-in").innerHTML = "<pre>" + JSON.stringify(algorithmInput, null, 2) + "</pre>";
// Query Algorithmia /kenny/WekaCluster
client.algo("/kenny/WekaCluster").pipe(algorithmInput).then(function(result) {
// Print debug output
if(result.error) {
document.getElementById("cluster-out").innerHTML = '<div class="text-danger">' + (result.error.message || result.error) + '</div>';
document.getElementById("demo-status").innerHTML = '<span class="text-danger">' + (result.error.message || result.error) + '</span>';
return;
}
algorithmOutput = result.result;
document.getElementById("cluster-out").innerHTML = "<pre>" + JSON.stringify(algorithmOutput, null, 2) + "</pre>";
// Save result
dataPoints = algorithmOutput;
// Update visualization
updateD3();
});
};