แก้ coding style ภาค 2

This commit is contained in:
Pitchaya Boonsarngsuk
2018-03-22 16:12:25 +00:00
parent 2256af7448
commit 0cdd927444
12 changed files with 160 additions and 130 deletions

View File

@@ -1,5 +1,5 @@
/* eslint-disable block-scoped-var */
import constant from "./constant";
import constant from './constant';
/**
* Set the node id accessor to the specified i.
@@ -7,7 +7,7 @@ import constant from "./constant";
* @param {accessor} i - id accessor.
* @return {accessor} - node id accessor.
*/
function index(d, i) {
function index (d, i) {
return i;
}
@@ -15,7 +15,7 @@ function index(d, i) {
* t-SNE implementation in D3 by using the code existing in tsnejs
* (https://github.com/karpathy/tsnejs) to compute the solution.
*/
export default function() {
export default function () {
var id = index,
distance = constant(300),
nodes,
@@ -33,7 +33,7 @@ export default function() {
* Make a step in t-SNE algorithm and set the velocities for the nodes
* to accumulate the values from solution.
*/
function force() {
function force () {
// Make a step at each iteration.
step();
var solution = getSolution();
@@ -49,7 +49,7 @@ export default function() {
* Calculates the random number from Gaussian distribution.
* @return {number} random number.
*/
function gaussRandom() {
function gaussRandom () {
let u = 2 * Math.random() - 1;
let v = 2 * Math.random() - 1;
let r = u * u + v * v;
@@ -63,11 +63,11 @@ export default function() {
* Return the normalized number.
* @return {number} normalized random number from Gaussian distribution.
*/
function randomN() {
function randomN () {
return gaussRandom() * 1e-4;
}
function sign(x) {
function sign (x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
@@ -76,7 +76,7 @@ export default function() {
* @param {number} n - length of array.
* @return {Float64Array} - array of zeros with length n.
*/
function zeros(n) {
function zeros (n) {
if (typeof n === 'undefined' || isNaN(n)) {
return [];
}
@@ -90,7 +90,7 @@ export default function() {
* @param {number} d - columns.
* @return {array} - 2d array
*/
function random2d(n, d) {
function random2d (n, d) {
var x = [];
for (var i = 0; i < n; i++) {
var y = [];
@@ -109,7 +109,7 @@ export default function() {
* @param {number} tol - limit for entropy difference.
* @return {2d array} - 2d matrix containing probabilities.
*/
function d2p(data, perplexity, tol) {
function d2p (data, perplexity, tol) {
N = Math.floor(data.length);
var Htarget = Math.log(perplexity); // target entropy of distribution.
var P1 = zeros(N * N); // temporary probability matrix.
@@ -161,7 +161,6 @@ export default function() {
} else {
beta = (beta + betamax) / 2;
}
} else {
// Converse case. Make distrubtion less peaky.
betamax = beta;
@@ -183,7 +182,6 @@ export default function() {
for (j = 0; j < N; j++) {
P1[i * N + j] = prow[j];
}
}
// Symmetrize P and normalize it to sum to 1 over all ij
@@ -200,7 +198,7 @@ export default function() {
/**
* Initialize a starting (random) solution.
*/
function initSolution() {
function initSolution () {
Y = random2d(N, dim);
// Step gains to accelerate progress in unchanging directions.
gains = random2d(N, dim, 1.0);
@@ -212,7 +210,7 @@ export default function() {
/**
* @return {2d array} the solution.
*/
function getSolution() {
function getSolution () {
return Y;
}
@@ -220,7 +218,7 @@ export default function() {
* Do a single step (iteration) for the layout.
* @return {number} the current cost.
*/
function step() {
function step () {
iteration += 1;
var cg = costGrad(Y); // Evaluate gradient.
@@ -269,8 +267,7 @@ export default function() {
* @param {2d array} Y - the current solution to evaluate.
* @return {object} that contains a cost and a gradient.
*/
function costGrad(Y) {
function costGrad (Y) {
var pmul = iteration < 100 ? 4 : 1;
// Compute current Q distribution, unnormalized first.
@@ -326,7 +323,7 @@ export default function() {
* the better layout.
* @return {number} - stress of the layout.
*/
function getStress() {
function getStress () {
var totalDiffSq = 0,
totalHighDistSq = 0;
for (var i = 0, source, target, realDist, highDist; i < nodes.length; i++) {
@@ -345,7 +342,7 @@ export default function() {
// API for initializing the algorithm, setting parameters and querying
// metrics.
force.initialize = function(_) {
force.initialize = function (_) {
nodes = _;
N = nodes.length;
// Initialize the probability matrix.
@@ -353,23 +350,23 @@ export default function() {
initSolution();
};
force.id = function(_) {
force.id = function (_) {
return arguments.length ? (id = _, force) : id;
};
force.distance = function(_) {
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), force) : distance;
force.distance = function (_) {
return arguments.length ? (distance = typeof _ === 'function' ? _ : constant(+_), force) : distance;
};
force.stress = function() {
force.stress = function () {
return getStress();
};
force.learningRate = function(_) {
force.learningRate = function (_) {
return arguments.length ? (learningRate = +_, force) : learningRate;
};
force.perplexity = function(_) {
force.perplexity = function (_) {
return arguments.length ? (perplexity = +_, force) : perplexity;
};