Skip to content

Commit

Permalink
Merge pull request #203 from biothings/fix-839
Browse files Browse the repository at this point in the history
Inferred-mode response merging: use more specific criteria for direct-edge handling
  • Loading branch information
tokebe authored Aug 6, 2024
2 parents 633d6a7 + f4e63f7 commit e859586
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,28 @@ export default class InferredQueryHandler {
const resultID = `${resultCreativeSubjectID}-${resultCreativeObjectID}`;

// Direct edge answers stand on their own, not as an inferred edge.
if (Object.keys(result.node_bindings).length == 2) {
const boundEdgeID = Object.values(result.analyses[0].edge_bindings)[0][0].id;
const boundEdgeID = Object.values(result.analyses[0].edge_bindings)[0][0].id;
const boundEdge = combinedResponse.message.knowledge_graph.edges[boundEdgeID];
const specialHandling = [
Object.keys(result.node_bindings).length === 2, // Direct edge
// Predicate matches or is descendant
qEdge.predicates.some(
(predicate) =>
predicate === boundEdge.predicate ||
biolink.getDescendantPredicates(predicate).includes(boundEdge.predicate),
),
// All query qualifiers (if any) are accounted for (more is fine)
qEdge.qualifier_constraints.some(({ qualifier_set }) => {
return qualifier_set.every((queryQualifier) =>
boundEdge.qualifiers.some(
(qualifier) =>
queryQualifier.qualifier_type_id === qualifier.qualifier_type_id &&
queryQualifier.qualifier_value === qualifier.qualifier_value,
),
);
}),
].every((test) => test);
if (specialHandling) {
translatedResult.analyses[0].edge_bindings = { [qEdgeID]: [{ id: boundEdgeID, attributes: [] }] };
} else {
// Create an aux graph using the result and associate it with an inferred Edge
Expand All @@ -336,8 +356,8 @@ export default class InferredQueryHandler {
],
attributes: [
{ attribute_type_id: 'biolink:support_graphs', value: [] },
{ attribute_type_id: 'biolink:knowledge_level', value: "prediction" },
{ attribute_type_id: 'biolink:agent_type', value: "computational_model" },
{ attribute_type_id: 'biolink:knowledge_level', value: 'prediction' },
{ attribute_type_id: 'biolink:agent_type', value: 'computational_model' },
],
};
}
Expand All @@ -364,7 +384,7 @@ export default class InferredQueryHandler {
},
[] as string[],
),
attributes: []
attributes: [],
};
}

Expand Down Expand Up @@ -397,9 +417,9 @@ export default class InferredQueryHandler {
if (typeof combinedResponse.message.results[resultID].analyses[0].score !== 'undefined') {
combinedResponse.message.results[resultID].analyses[0].score = resScore
? scaled_sigmoid(
inverse_scaled_sigmoid(combinedResponse.message.results[resultID].analyses[0].score) +
inverse_scaled_sigmoid(resScore),
)
inverse_scaled_sigmoid(combinedResponse.message.results[resultID].analyses[0].score) +
inverse_scaled_sigmoid(resScore),
)
: combinedResponse.message.results[resultID].analyses[0].score;
} else {
combinedResponse.message.results[resultID].analyses[0].score = resScore;
Expand Down Expand Up @@ -568,11 +588,9 @@ export default class InferredQueryHandler {
const message = [
`Addition of ${creativeLimitHit} results from Template ${i + 1}`,
Object.keys(combinedResponse.message.results).length === this.CREATIVE_LIMIT ? ' meets ' : ' exceeds ',
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${
Object.keys(combinedResponse.message.results).length
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${Object.keys(combinedResponse.message.results).length
} merged). `,
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${
subQueries.length - (i + 1)
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${subQueries.length - (i + 1)
} `,
subQueries.length - (i + 1) === 1 ? `template.` : `templates.`,
].join('');
Expand All @@ -597,9 +615,8 @@ export default class InferredQueryHandler {
const total =
Object.values(mergedResultsCount).reduce((sum, count) => sum + count, 0) +
Object.keys(mergedResultsCount).length;
const message = `Merging Summary: (${total}) inferred-template results were merged into (${
Object.keys(mergedResultsCount).length
}) final results, reducing result count by (${total - Object.keys(mergedResultsCount).length})`;
const message = `Merging Summary: (${total}) inferred-template results were merged into (${Object.keys(mergedResultsCount).length
}) final results, reducing result count by (${total - Object.keys(mergedResultsCount).length})`;
debug(message);
combinedResponse.logs.push(new LogEntry('INFO', null, message).getLog());
}
Expand Down

0 comments on commit e859586

Please sign in to comment.