Skip to content

Commit

Permalink
make aux graphs more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Apr 18, 2024
1 parent 78c0c84 commit e1ab149
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/inferred_mode/pathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default class PathfinderQueryHandler {
debug(message1);
this.logs.push(new LogEntry('INFO', null, message1).getLog());

const { results: newResultObject, graphs: newAuxGraphs } = this._searchForIntermediates(creativeResponse, dfsGraph, supportGraphsPerNode, kgSrc, kgDst, kgEdge);
const { results: newResultObject, graphs: newAuxGraphs } = this._searchForIntermediates(creativeResponse, dfsGraph, supportGraphsPerNode, kgSrc, kgDst);

creativeResponse.message.results = Object.values(newResultObject).sort((a, b) => (b.analyses[0].score ?? 0) - (a.analyses[0].score ?? 0)).slice(0, this.CREATIVE_LIMIT);
creativeResponse.description = `Query processed successfully, retrieved ${creativeResponse.message.results.length} results.`
Expand All @@ -166,7 +166,11 @@ export default class PathfinderQueryHandler {
for (const eb of Object.values(creativeResponse.message.results[res].analyses[0].edge_bindings)) {
for (const edge of eb) {
const auxGraph = creativeResponse.message.knowledge_graph.edges[edge.id].attributes.find(attr => attr.attribute_type_id === 'biolink:support_graphs')?.value[0];
finalNewAuxGraphs[auxGraph] = { edges: Array.from(newAuxGraphs[auxGraph].edges) };
finalNewAuxGraphs[auxGraph] = { edges: [] };
for (const ed of newAuxGraphs[auxGraph].edges) {
const [st, en] = ed.split('-');
finalNewAuxGraphs[auxGraph].edges.push.apply(finalNewAuxGraphs[auxGraph].edges, dfsGraph[st][en]);
}
}
}
}
Expand All @@ -182,15 +186,18 @@ export default class PathfinderQueryHandler {
return creativeResponse;
}

_searchForIntermediates(creativeResponse: TrapiResponse, dfsGraph: DfsGraph, supportGraphsPerNode: { [node: string]: Set<string> }, kgSrc: string, kgDst: string, kgEdge: string): ResultAuxObject {
_searchForIntermediates(creativeResponse: TrapiResponse, dfsGraph: DfsGraph, supportGraphsPerNode: { [node: string]: Set<string> }, kgSrc: string, kgDst: string): ResultAuxObject {
const span = Telemetry.startSpan({ description: 'pathfinderIntermediateSearch' });

// perform dfs
const stack = [{ node: kgSrc, path: [kgSrc] }];
const newResultObject: ResultObject = {};
const newAuxGraphs: AuxGraphObject = {};
let a= 0;
let b=0;
while (stack.length !== 0) {
const { node, path } = stack.pop()!;
a++;

// continue creating path if we haven't reached end yet
if (node !== kgDst) {
Expand All @@ -206,30 +213,29 @@ export default class PathfinderQueryHandler {
if (path.length <= 2) {
continue;
}

b++;
// loop through all intermediate nodes in path to dest
for (let i = 1; i < path.length - 1; i++) {
const intermediateNode = path[i];
const firstEdges: string[] = [];
const secondEdges: string[] = [];

if (!(`pathfinder-${kgSrc}-${intermediateNode}-${kgDst}` in newResultObject)) {
newAuxGraphs[`pathfinder-${kgSrc}-${intermediateNode}-support`] = { edges: new Set() };
newAuxGraphs[`pathfinder-${intermediateNode}-${kgDst}-support`] = { edges: new Set() };
newAuxGraphs[`pathfinder-${intermediateNode}-support`] = { edges: new Set() };
}

// add "edges" to aux graphs (kg edges will be added later)
for (let j = 0; j < i; j++) {
firstEdges.push.apply(firstEdges, dfsGraph[path[j]][path[j+1]]);
newAuxGraphs[`pathfinder-${kgSrc}-${intermediateNode}-support`].edges.add(`${[path[j]]}-${[path[j+1]]}`);
newAuxGraphs[`pathfinder-${intermediateNode}-support`].edges.add(`${[path[j]]}-${[path[j+1]]}`);
}
for (let j = i; j < path.length - 1; j++) {
secondEdges.push.apply(secondEdges, dfsGraph[path[j]][path[j+1]]);
}
newAuxGraphs[`pathfinder-${intermediateNode}-${kgDst}-support`].edges.add(`${[path[j]]}-${[path[j+1]]}`);
newAuxGraphs[`pathfinder-${intermediateNode}-support`].edges.add(`${[path[j]]}-${[path[j+1]]}`);
}

if (`pathfinder-${kgSrc}-${intermediateNode}-${kgDst}` in newResultObject) {
firstEdges.forEach(edge => {
newAuxGraphs[`pathfinder-${kgSrc}-${intermediateNode}-support`].edges.add(edge);
newAuxGraphs[`pathfinder-${intermediateNode}-support`].edges.add(edge)
});
secondEdges.forEach(edge => {
newAuxGraphs[`pathfinder-${intermediateNode}-${kgDst}-support`].edges.add(edge);
newAuxGraphs[`pathfinder-${intermediateNode}-support`].edges.add(edge);
});
continue;
}
// code below is only for new results
if (`pathfinder-${kgSrc}-${intermediateNode}-${kgDst}` in newResultObject) continue;

// create new edges & aux graph
newResultObject[`pathfinder-${kgSrc}-${intermediateNode}-${kgDst}`] = {
Expand Down Expand Up @@ -290,9 +296,6 @@ export default class PathfinderQueryHandler {
],
attributes: [{ attribute_type_id: 'biolink:support_graphs', value: [`pathfinder-${intermediateNode}-support`] }],
};
newAuxGraphs[`pathfinder-${kgSrc}-${intermediateNode}-support`] = { edges: new Set(firstEdges) };
newAuxGraphs[`pathfinder-${intermediateNode}-${kgDst}-support`] = { edges: new Set(secondEdges) };
newAuxGraphs[`pathfinder-${intermediateNode}-support`] = { edges: new Set([...firstEdges, ...secondEdges]) };

// calculate score
if (supportGraphsPerNode[intermediateNode]?.size > 0) {
Expand Down

0 comments on commit e1ab149

Please sign in to comment.