tweak: Interface for findPaths

so that it can work with an ordered list.
This commit is contained in:
NunoSempere 2022-06-19 17:30:02 -04:00
parent dc7cfbe0c5
commit 8b03f0a36b
4 changed files with 32 additions and 33 deletions

View File

@ -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,
});
}

View File

@ -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,
});

View File

@ -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;
}

View File

@ -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 };