From 8b03f0a36ba5a3aae02f86a804d0433aacc0b3af Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 19 Jun 2022 17:30:02 -0400 Subject: [PATCH] tweak: Interface for findPaths so that it can work with an ordered list. --- packages/utility-tools/src/aggregatePaths.js | 20 ++++++++--------- packages/utility-tools/src/example.js | 23 +++----------------- packages/utility-tools/src/findPaths.js | 17 +++++++++++++++ packages/utility-tools/src/index.js | 5 ++--- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/packages/utility-tools/src/aggregatePaths.js b/packages/utility-tools/src/aggregatePaths.js index b880e8b..09d2286 100644 --- a/packages/utility-tools/src/aggregatePaths.js +++ b/packages/utility-tools/src/aggregatePaths.js @@ -3,7 +3,7 @@ import { run } from "@quri/squiggle-lang"; export async function aggregatePathsThroughMixtureOfDistributions({ pathsArray, - nodes, + orderedList, VERBOSE, }) { let print = (x) => { @@ -12,7 +12,7 @@ export async function aggregatePathsThroughMixtureOfDistributions({ } }; let result = pathsArray.map((paths, i) => { - print(nodes[i].name); + print(orderedList[i].name); let multipliedDistributions = paths.map( (path) => path.multipliedDistributionsInPath ); @@ -60,7 +60,7 @@ export async function aggregatePathsThroughMixtureOfDistributions({ console.groupEnd(); print(""); return { - name: nodes[i].name, + name: orderedList[i].name, meanOfAggregatedDistributions: mean, ninetyPercentileConfidenceIntervalOfAggregatedDistributions: ci90percentAnswer, @@ -84,7 +84,7 @@ export const geomMean = (arr) => { export function aggregatePathsThroughMixtureOfMeans({ pathsArray, - nodes, + orderedList, VERBOSE, }) { let print = (x) => { @@ -94,7 +94,7 @@ export function aggregatePathsThroughMixtureOfMeans({ }; let result = pathsArray.map((paths, i) => { - print(nodes[i].name); + print(orderedList[i].name); let expectedRelativeValues = paths .map((path) => path.expectedRelativeValue) .filter((x) => x != undefined); @@ -112,7 +112,7 @@ export function aggregatePathsThroughMixtureOfMeans({ } } return { - name: nodes[i].name, + name: orderedList[i].name, aggregatedMeans: answer, arrayMeans: expectedRelativeValues, allPositive: hasNegative.length == 0, @@ -123,7 +123,7 @@ export function aggregatePathsThroughMixtureOfMeans({ export async function aggregatePaths({ pathsArray, - nodes, + orderedList, aggregationType, VERBOSE, }) { @@ -134,19 +134,19 @@ export async function aggregatePaths({ console.log("Warning: this may take a long time"); return await aggregatePathsThroughMixtureOfDistributions({ pathsArray, - nodes, + orderedList, VERBOSE, }); } else if (aggregationType == "mean") { return aggregatePathsThroughMixtureOfMeans({ pathsArray, - nodes, + orderedList, VERBOSE, }); } else { return aggregatePathsThroughMixtureOfMeans({ pathsArray, - nodes, + orderedList, VERBOSE, }); } diff --git a/packages/utility-tools/src/example.js b/packages/utility-tools/src/example.js index c5cc202..620403a 100644 --- a/packages/utility-tools/src/example.js +++ b/packages/utility-tools/src/example.js @@ -1,7 +1,7 @@ // IMPORTS import * as fs from "fs"; import { mergeSort } from "./mergeSort.js"; -import { findDistancesFromAllElementsToAllReferencePoints } from "./findPaths.js"; +import { findDistances } from "./findPaths.js"; import { aggregatePaths } from "./aggregatePaths.js"; // DEFS @@ -11,11 +11,6 @@ const outputFilePath = "./output/output.json"; // HELPERS -const findElementPosition = (name, orderedList) => { - let node = orderedList.find((node) => node.name == name); - return node.position; -}; - // MAIN async function main() { // read file @@ -43,25 +38,13 @@ async function main() { console.log(""); // find Paths - let nodes = orderedList.map((element, i) => ({ - ...element, - position: i, - })); - const linksWithPosition = links.map((link) => ({ - ...link, - sourceElementPosition: findElementPosition(link.source, nodes), - targetElementPosition: findElementPosition(link.target, nodes), - })); - let paths = await findDistancesFromAllElementsToAllReferencePoints({ - nodes, - links: linksWithPosition, - }); + let paths = await findDistances({ orderedList, links }); // console.log(JSON.stringify(paths, null, 4)); // Aggregate paths. let aggregatedPaths = await aggregatePaths({ pathsArray: paths, - nodes, + orderedList, aggregationType: "mean", // alternatively: aggregationType: "distribution" VERBOSE: false, }); diff --git a/packages/utility-tools/src/findPaths.js b/packages/utility-tools/src/findPaths.js index 5e957ba..eac5d25 100644 --- a/packages/utility-tools/src/findPaths.js +++ b/packages/utility-tools/src/findPaths.js @@ -312,3 +312,20 @@ export async function findDistancesFromAllElementsToAllReferencePoints({ } return distancesForAllElements; } + +export async function findDistances({ orderedList, links }) { + let nodes = orderedList.map((element, i) => ({ + ...element, + position: i, + })); + const linksWithPosition = links.map((link) => ({ + ...link, + sourceElementPosition: findElementPosition(link.source, nodes), + targetElementPosition: findElementPosition(link.target, nodes), + })); + let result = await findDistancesFromAllElementsToAllReferencePoints({ + nodes, + links: linksWithPosition, + }); + return result; +} diff --git a/packages/utility-tools/src/index.js b/packages/utility-tools/src/index.js index d89e7b7..1d4253e 100644 --- a/packages/utility-tools/src/index.js +++ b/packages/utility-tools/src/index.js @@ -1,6 +1,5 @@ import { mergeSort } from "./mergeSort.js"; -import { findDistancesFromAllElementsToAllReferencePoints } from "./findPaths.js"; +import { findDistances } from "./findPaths.js"; import { aggregatePaths } from "./aggregatePaths.js"; -export const findPaths = findDistancesFromAllElementsToAllReferencePoints; -export { mergeSort, aggregatePaths }; +export { mergeSort, aggregatePaths, findDistances };