From 5f954f48f8ff55c339faed709ebd26b0984abfb2 Mon Sep 17 00:00:00 2001 From: schneefux Date: Thu, 31 Mar 2016 12:22:50 +0200 Subject: move everything into classes --- index.js | 484 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 280 insertions(+), 204 deletions(-) diff --git a/index.js b/index.js index 56b2e5d..e58ddd7 100644 --- a/index.js +++ b/index.js @@ -54,237 +54,313 @@ function makeSlider(labelName, x, y, scale, cb) { /* network construction */ -function drawNodes(nodes, layout, env) { - var cnt = 0; - var biasLayers = 0; - for (layer=0; layer largestLayerSize) largestLayerSize = this.layout.net[layer].size; } - } - return nodes; -} + this.responsiveRadius = d3.min([env.width, env.height]) / d3.max([this.layout.net.layers, largestLayerSize]); + this._updateNodes(env); + }; -function drawLinks(links, weights) { - var cnt = 0; - for (layer=0; layer layout.layers[batch][epoch].max) layout.layers[batch][epoch].max = layerMax; + } } } - } + console.log(layout); // DEBUG - setup(layout, json); -}); - -function setup(layout, data) { - // create empty elements and determine the visualisation's size - var svg = d3.select("#visContainer").append("svg") - .attr("width", "100%") - .attr("height", "100%"); - var width = parseInt(svg.style("width")) || 800, - height = parseInt(svg.style("height")) || 600; - - /*var slideScaler = d3.scale.linear() - .domain([0, 180]) - .range([0, 200]) - .clamp(true); - function slid(val) { - console.log(val); + if (this._finished) this._finished(layout, json); + }; + + return Loader; +})(); + +var Animator = (function() { + function Animator(layout, time, step) { + this.layout = layout; + this.step = step; + this.batch = this.epoch = 0; + this.frameDuration = time / (this.layout.data.batches * this.layout.data.epochs / this.step); + console.log("frame duration:" + this.frameDuration); // DEBUG } - makeSlider("Test", 10, 20, slideScaler, slid);*/ - - var net = createNet(layout); - console.log(layout); - console.log(net); - net.nodes = drawNodes(net.nodes, layout, {"width": width, "height": height}); - net.links = drawLinks(net.links, data[0][0]); - - var link = svg.selectAll(".link") - .data(net.links) - .enter().append("line") - .attr("class", "link") - .style("stroke", "grey") - .style("stroke-width", function(d) { return d.weight * 4; }) // TODO make width always stay between 0-1 - .attr("x1", function(d) { return d.source.x; }) - .attr("y1", function(d) { return d.source.y; }) - .attr("x2", function(d) { return d.target.x; }) - .attr("y2", function(d) { return d.target.y; }); - var node = svg.selectAll(".node") - .data(net.nodes) - .enter().append("circle") - .attr("class", "node") - .attr("cx", function(d) { return d.x; }) - .attr("cy", function(d) { return d.y; }) - .attr("r", function(d) { return d.r; }); - - window.addEventListener("resize", envRedraw, true); - // TODO animation & refresh -} -var maxBatch = 8, maxEpoch = 300; -var epochStep = 50; -var numLayers = 3; -var currBatch = 0, currEpoch = 0; -function animate() { - frameDuration = 20000 / (maxBatch * maxEpoch / epochStep); - envRedraw(cache[currBatch][currEpoch], frameDuration); - currEpoch += epochStep; - if (currEpoch >= maxEpoch) { - currEpoch = 0; - console.log("batch " + currBatch.toString()); - if (++currBatch >= maxBatch) { - currBatch = 0; - return; // stop + Animator.prototype.start = function(callback) { + var self = this; + wrapper = function() { + callback(self.batch, self.epoch, self.frameDuration); + self.animate(); + }; + this.pid = setInterval(wrapper, this.frameDuration); + }; + + Animator.prototype.animate = function() { + this.epoch += this.step; + if (this.epoch >= this.layout.data.epochs) { + this.epoch = 0; + if (++this.batch>= this.layout.data.batches) { + this.batch = 0; + clearInterval(this.pid); // suicide + console.log("finished"); + } } + }; + + return Animator; +})(); + +var Visualizer = (function() { + function Visualizer(svg) { + this.loader = new Loader(); + this.svg = svg; } - setTimeout(animate, frameDuration); -} + + Visualizer.prototype.start = function(url, animationTime, resolution) { + // animationTime in seconds per frame + // resolution in epochs per frame + var self = this; + this.animationTime = animationTime; + this.resolution = resolution; + function wrapper(layout, data) { + self.layout = layout; + self.data = data; + self._setup(); + } + this.loader.load("data/log.json", wrapper); + }; + + Visualizer.prototype._setup = function() { + console.log("setup"); // DEBUG + var self = this; + // create empty elements and determine the visualisation's size + this.svg + .attr("width", "100%") + .attr("height", "100%"); + var width = parseInt(this.svg.style("width")) || 800, + height = parseInt(this.svg.style("height")) || 600; + + /*var slideScaler = d3.scale.linear() + .domain([0, 180]) + .range([0, 200]) + .clamp(true); + function slid(val) { + console.log(val); + } + makeSlider("Test", 10, 20, slideScaler, slid);*/ + + this.network = new Network(this.layout); + this.network.init(); + this.network.resize({"width": width, "height": height}); + this.network.updateWeights(this.data[0][0], this.layout.layers[0][0]); + + function resized() { + width = parseInt(self.svg.style("width")) || 800; + height = parseInt(self.svg.style("height")) || 600; + + self.network.resize({"width": width, "height": height}); + + node + .attr("cx", function(d) { return d.x; }) + .attr("cy", function(d) { return d.y; }) + .attr("r", function(d) { return d.r; }); + link + .attr("x1", function(d) { return d.source.x; }) + .attr("y1", function(d) { return d.source.y; }) + .attr("x2", function(d) { return d.target.x; }) + .attr("y2", function(d) { return d.target.y; }); + } + window.addEventListener("resize", resized, true); + + var link = this.svg.selectAll(".link") + .data(this.network.links) + .enter().append("line") + .attr("class", "link") + .style("stroke", "grey") + .style("stroke-width", function(d) { return d.weight * 10; }) + .attr("x1", function(d) { return d.source.x; }) + .attr("y1", function(d) { return d.source.y; }) + .attr("x2", function(d) { return d.target.x; }) + .attr("y2", function(d) { return d.target.y; }); + var node = this.svg.selectAll(".node") + .data(this.network.nodes) + .enter().append("circle") + .attr("class", "node") + .attr("cx", function(d) { return d.x; }) + .attr("cy", function(d) { return d.y; }) + .attr("r", function(d) { return d.r; }); + + this.animator = new Animator(this.layout, this.animationTime * 1000, this.resolution); + this.animator.start(function(batch, epoch, duration) { + self.network.updateWeights(self.data[batch][epoch], self.layout.layers[batch][epoch]); + + // 10% extra time per frame for the code execution + duration = duration / 1.1; + link + .transition() + .duration(duration) + .style("stroke-width", function(d) { return d.weight * 10; }); + }); + }; + + return Visualizer; +})(); + +var svg = d3.select("#visContainer").append("svg"); +var visualizer = new Visualizer(svg); +visualizer.start("data/log.json", 30, 10); -- cgit v1.3.1