Refactor: Extract functions

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-19 09:29:14 +00:00
parent 1d711f828d
commit feec778c62
12 changed files with 351 additions and 163 deletions

View File

@@ -0,0 +1,43 @@
/**
* Initialize the hybrid layout algorithm and start simulation.
*/
function startHybridSimulation() {
console.log("startHybridSimulation");
springForce = false;
d3.selectAll(".nodes").remove();
simulation.stop();
p1 = performance.now();
configuration = {
iteration: ITERATIONS,
neighbourSize: NEIGHBOUR_SIZE,
sampleSize: SAMPLE_SIZE,
distanceRange: SELECTED_DISTANCE * MULTIPLIER,
fullIterations: FULL_ITERATIONS,
fullNeighbourSize: FULL_NEIGHBOUR_SIZE,
fullSampleSize: FULL_SAMPLE_SIZE,
fullDistanceRange: FULL_SELECTED_DISTANCE * MULTIPLIER,
distanceFn: function (s, t) {return distanceFunction(s, t, props, norm) * MULTIPLIER;},
pivots: PIVOTS,
numPivots: NUM_PIVOTS
};
console.log(configuration);
hybridSimulation = d3.hybridSimulation(nodes, configuration);
let sample = hybridSimulation.sample();
let remainder = hybridSimulation.remainder();
addNodesToDOM(sample);
hybridSimulation
.on("sampleTick", ticked)
.on("fullTick", ticked)
.on("startFull", startedFull)
.on("end", ended);
function startedFull() {
console.log("startedFull");
d3.selectAll(".nodes").remove();
addNodesToDOM(nodes);
}
}

View File

@@ -0,0 +1,40 @@
/**
* Initialize the link force algorithm and start simulation.
*/
function startLinkSimulation() {
console.log("startLinkSimulation")
springForce = false;
simulation.stop();
p1 = performance.now();
let links = [];
// Initialize link array.
nodes = simulation.nodes();
for (i = 0; i < nodes.length; i++) {
for (j = 0; j < nodes.length; j++) {
if (i !== j) {
links.push({
source: nodes[i],
target: nodes[j],
});
}
}
}
// Add the links to the simulation.
simulation.force(forceName, d3.forceLink().links(links));
simulation
.alphaDecay(1 - Math.pow(0.001, 1 / ITERATIONS))
.force(forceName)
// The distance function that will be used to calculate distances
// between nodes.
.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm) * MULTIPLIER;
})
// Set the parameter for the algorithm (optional).
.strength(1);
// Restart the simulation.
simulation.alpha(1).restart();
}

View File

@@ -0,0 +1,29 @@
/**
* Initialize the Chalmers' 1996 algorithm and start simulation.
*/
function startNeighbourSamplingSimulation() {
console.log("startNeighbourSamplingSimulation");
springForce = true;
simulation.stop();
p1 = performance.now();
simulation
.alphaDecay(1 - Math.pow(0.001, 1 / ITERATIONS))
.force(forceName, d3.forceNeighbourSamplingDistance()
// Set the parameters for the algorithm (optional).
.neighbourSize(NEIGHBOUR_SIZE)
.sampleSize(SAMPLE_SIZE)
// .freeness(0.5)
.distanceRange(SELECTED_DISTANCE * MULTIPLIER)
// The distance function that will be used to calculate distances
// between nodes.
.distance(function (s, t) {
return distanceFunction(s, t, props, norm) * MULTIPLIER;
})
.stableVelocity(1.2 * MULTIPLIER)
.stableVeloHandler( function(){simulation.stop(); ended();} )
);
// Restart the simulation.
console.log(simulation.force(forceName).neighbourSize(), simulation.force(forceName).sampleSize());
simulation.alpha(1).restart();
}

View File

@@ -0,0 +1,43 @@
/**
* Initialize the t-SNE algorithm and start simulation.
*/
function starttSNE() {
springForce = false;
simulation.stop();
p1 = performance.now();
simulation
.alphaDecay(1 - Math.pow(0.001, 1 / ITERATIONS))
.force(forceName, d3.tSNE()
// Set the parameter for the algorithm (optional).
.perplexity(PERPLEXITY)
.learningRate(LEARNING_RATE)
// The distance function that will be used to calculate distances
// between nodes.
.distance(function (s, t) {
return distanceFunction(s, t, props, norm) * MULTIPLIER;
}));
// Restart the simulation.
console.log(simulation.force(forceName).perplexity(), simulation.force(forceName).learningRate());
simulation.alpha(1).restart();
}
/**
* Initialize the Barnes-Hut algorithm and start simulation.
*/
function startBarnesHutSimulation() {
springForce = false;
simulation.stop();
p1 = performance.now();
simulation
.alphaDecay(1 - Math.pow(0.001, 1 / ITERATIONS))
.force(forceName, d3.forceBarnesHut()
// The distance function that will be used to calculate distances
// between nodes.
.distance(function (s, t) {
return distanceFunction(s, t, props, norm) * MULTIPLIER;
}));
// Restart the simulation.
simulation.alpha(1).restart();
}