import {takeSampleFrom} from './helpers'; import {placeNearToNearestNeighbour} from './interpCommon'; /** * Perform interpolation where the "parent" node is found by brute-force. * A "parent" of a node to be interpolated is a node whose position in 2D space * is already known and have the least high-dimensional distance to the node in * question. * For each point to be interpolated: * - Phase 1: find the "parent" by comparing high-d distance against every points already plotted on the graph. this is essentially a nearest neighbour finding problem. * - Phase 2 and 3 are passed onto placeNearToNearestNeighbour * @param {list} sampleSet - nodes already plotted on the 2D graph * @param {list} remainderSet - nodes to be interpolated onto the graph * @param {function} distanceFn - f(nodex, nodey) that calculate high-dimensional * distance between 2 nodes * @param {number} endingIts - for phase 3, how many iterations to refine the * placement of each interpolated point */ export default function (sampleSet, remainderSet, distanceFn, endingIts) { let sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample, sampleSubsetDistanceCache = []; // For each datapoint "node" to be interpolated for (let i = remainderSet.length - 1; i >= 0; i--) { let node = remainderSet[i], nearestSample, minDist, sample, dist, index; // For each datapoint "sample" in the sample set for (let j = sampleSet.length - 1; j >= 0; j--) { sample = sampleSet[j]; dist = distanceFn(node, sample); if (nearestSample === undefined || dist < minDist) { minDist = dist; nearestSample = sample; } index = sampleSubset.indexOf(sample); if (index !== -1) { sampleSubsetDistanceCache[index] = dist; } } placeNearToNearestNeighbour(node, nearestSample, minDist, sampleSubset, sampleSubsetDistanceCache, endingIts); } }