Skip to content

Commit 03862b1

Browse files
authored
Merge pull request #2255 from RTXteam/issue2254
Issue2254
2 parents 72d075d + e36cff3 commit 03862b1

File tree

6 files changed

+91
-46
lines changed

6 files changed

+91
-46
lines changed

code/ARAX/ARAXQuery/ARAX_query.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,10 @@ def main():
10601060
"add_qnode(name=acetaminophen, key=n0)",
10611061
"add_qnode(categories=biolink:Protein, key=n1)",
10621062
"add_qedge(subject=n0, object=n1, key=e0)",
1063-
"expand(edge_key=e0)",
1063+
"expand(edge_key=e0, kp=infores:rtx-kg2)",
10641064
"overlay(action=compute_ngd, virtual_relation_label=N1, subject_qnode_key=n0, object_qnode_key=n1)",
10651065
"resultify(ignore_edge_direction=true)",
1066-
"filter_results(action=limit_number_of_results, max_results=10)",
1066+
"#filter_results(action=limit_number_of_results, max_results=5)",
10671067
"return(message=true, store=true)",
10681068
]}}
10691069

@@ -1838,10 +1838,12 @@ def main():
18381838

18391839

18401840
#### Print out the logging stream
1841-
print(response.show(level=ARAXResponse.DEBUG))
1841+
#if verbose:
1842+
# print(response.show(level=ARAXResponse.DEBUG))
18421843

18431844
#### Print out the message that came back
1844-
print(json.dumps(ast.literal_eval(repr(envelope)), sort_keys=True, indent=2))
1845+
#if verbose:
1846+
# print(json.dumps(ast.literal_eval(repr(envelope)), sort_keys=True, indent=2))
18451847

18461848
#### Other stuff that could be dumped
18471849
#print(json.dumps(message.to_dict(),sort_keys=True,indent=2))
@@ -1858,10 +1860,11 @@ def main():
18581860
#print(f"Essence names in the answers: {[x.essence for x in message.results]}")
18591861
print("Results:")
18601862
for result in message.results:
1861-
confidence = result.confidence
1862-
if confidence is None:
1863-
confidence = 0.0
1864-
print(" -" + '{:6.3f}'.format(confidence) + f"\t{result.essence}")
1863+
analysis = result.analyses[0]
1864+
score = analysis.score
1865+
if score is None:
1866+
score = 0.0
1867+
print(" -" + '{:6.3f}'.format(score) + f"\t{result.essence}")
18651868

18661869
# print the response id at the bottom for convenience too:
18671870
print(f"Returned response id: {envelope.id}")

code/ARAX/ARAXQuery/ARAX_ranker.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,33 @@ def _score_result_graphs_by_networkx_graph_scorer(kg_edge_id_to_edge: Dict[str,
168168
return nx_graph_scorer(result_graphs_nx)
169169

170170

171+
def _break_ties_and_preserve_order(scores):
172+
adjusted_scores = scores.copy()
173+
n = len(scores)
174+
# if there are more than 1,000 scores, apply the fix to the first 1000 scores and ignore the rest
175+
if n > 1000:
176+
n = 1000
177+
178+
for i in range(n):
179+
if i > 0 and adjusted_scores[i] >= adjusted_scores[i - 1]:
180+
# Calculate the decrement such that it makes this score slightly less than the previous,
181+
# maintaining the descending order.
182+
decrement = round(adjusted_scores[i - 1] - adjusted_scores[i], 3) - 0.001
183+
adjusted_scores[i] = adjusted_scores[i - 1] - max(decrement, 0.001)
184+
185+
# Ensure the adjusted score doesn't become lower than the next score
186+
if i < n - 1 and adjusted_scores[i] <= adjusted_scores[i + 1]:
187+
# Adjust the next score to be slightly less than the current score
188+
increment = round(adjusted_scores[i] - adjusted_scores[i + 1], 3) - 0.001
189+
adjusted_scores[i + 1] = adjusted_scores[i] - max(increment, 0.001)
190+
191+
# round all scores to 3 decimal places
192+
adjusted_scores = [round(score, 3) for score in adjusted_scores]
193+
# make sure no scores are below 0
194+
adjusted_scores = [max(score, 0) for score in adjusted_scores]
195+
return adjusted_scores
196+
197+
171198
class ARAXRanker:
172199

173200
# #### Constructor
@@ -657,11 +684,12 @@ def aggregate_scores_dmk(self, response):
657684
[_score_networkx_graphs_by_max_flow,
658685
_score_networkx_graphs_by_longest_path,
659686
_score_networkx_graphs_by_frobenius_norm])))
660-
#print(ranks_list)
661-
#print(float(len(ranks_list)))
687+
688+
662689
result_scores = sum(ranks_list)/float(len(ranks_list))
663690
#print(result_scores)
664691

692+
665693
# Replace Inferred Results Score with Probability score calculated by xDTD model
666694
inferred_qedge_keys = [qedge_key for qedge_key, qedge in message.query_graph.edges.items()
667695
if qedge.knowledge_type == "inferred"]
@@ -699,6 +727,13 @@ def aggregate_scores_dmk(self, response):
699727

700728
# Re-sort the final results
701729
message.results.sort(key=lambda result: result.analyses[0].score, reverse=True)
730+
# break ties and preserve order, round to 3 digits and make sure none are < 0
731+
scores_with_ties = [result.analyses[0].score for result in message.results]
732+
scores_without_ties = _break_ties_and_preserve_order(scores_with_ties)
733+
# reinsert these scores into the results
734+
for result, score in zip(message.results, scores_without_ties):
735+
result.analyses[0].score = score
736+
result.row_data[0] = score
702737
response.debug("Results have been ranked and sorted")
703738

704739

code/ARAX/Examples/ARAX_Example1.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"outputs": [],
2020
"source": [
2121
"# Set the base URL for the ARAX reasoner and its endpoint\n",
22-
"endpoint_url = 'https://arax.transltr.io/api/rtx/v1.4/query'\n",
22+
"endpoint_url = 'https://arax.transltr.io/api/arax/v1.4/query'\n",
2323
"\n",
2424
"# Create a dict of the request, specifying a start previous Message and the list of DSL commands\n",
25-
"query = {\"previous_message_processing_plan\": {\"processing_actions\": [\n",
25+
"query = {\"message\":{}, \"operations\": {\"actions\": [\n",
2626
" \"add_qnode(name=acetaminophen, key=n0)\",\n",
2727
" \"add_qnode(categories=biolink:Protein, key=n1)\",\n",
2828
" \"add_qedge(subject=n0, object=n1, key=e0)\",\n",
@@ -58,9 +58,9 @@
5858
"source": [
5959
"# Unpack respsonse from JSON and display the information log\n",
6060
"response_dict = response_content.json()\n",
61-
"for message in response_dict['log']:\n",
62-
" if message['level'] >= 20:\n",
63-
" print(message['prefix']+message['message'])"
61+
"for message in response_dict['logs']:\n",
62+
" if message['level'] == \"INFO\":\n",
63+
" print(message['level']+\": \"+message['message'])"
6464
]
6565
},
6666
{

code/ARAX/Examples/ARAX_Example2.ipynb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,29 @@
1515
{
1616
"cell_type": "code",
1717
"execution_count": null,
18-
"metadata": {},
18+
"metadata": {
19+
"pycharm": {
20+
"is_executing": true
21+
}
22+
},
1923
"outputs": [],
2024
"source": [
2125
"# Set the base URL for the ARAX reasoner and its endpoint\n",
22-
"endpoint_url = 'https://arax.rtx.ai/api/rtx/v1/query'\n",
26+
"endpoint_url = 'https://arax.transltr.io/api/arax/v1.4/query'\n",
2327
"\n",
2428
"# Create a dict of the request, specifying the list of DSL commands\n",
25-
"query = { \"previous_message_processing_plan\": { \"processing_actions\": [\n",
29+
"query = {\"message\":{}, \"operations\": { \"actions\": [\n",
2630
" \"add_qnode(name=DOID:14330, key=n00)\",\n",
27-
" \"add_qnode(category=biolink:Protein, is_set=true, key=n01)\",\n",
28-
" \"add_qnode(category=biolink:ChemicalSubstance, key=n02)\",\n",
29-
" \"add_qedge(subject=n00, object=n01, id=e00)\",\n",
30-
" \"add_qedge(subject=n01, object=n02, id=e01, predicate=biolink:physically_interacts_with)\",\n",
31-
" \"expand(edge_key=[e00,e01], kp=ARAX/KG1)\",\n",
31+
" \"add_qnode(categories=biolink:Protein, is_set=true, key=n01)\",\n",
32+
" \"add_qnode(categories=biolink:ChemicalEntity, key=n02)\",\n",
33+
" \"add_qedge(subject=n00, object=n01, key=e00)\",\n",
34+
" \"add_qedge(subject=n01, object=n02, key=e01, predicates=biolink:physically_interacts_with)\",\n",
35+
" \"expand(edge_key=[e00,e01], kp=infores:rtx-kg2)\",\n",
3236
" \"overlay(action=compute_jaccard, start_node_key=n00, intermediate_node_key=n01, end_node_key=n02, virtual_relation_label=J1)\",\n",
33-
" \"filter_kg(action=remove_edges_by_attribute, edge_attribute=jaccard_index, direction=below, threshold=.2, remove_connected_nodes=t, qnode_key=n02)\",\n",
34-
" \"filter_kg(action=remove_edges_by_property, edge_property=provided_by, property_value=Pharos)\",\n",
35-
" \"overlay(action=predict_drug_treats_disease, subject_qnode_key=n02, object_qnode_key=n00, virtual_relation_label=P1)\", \n",
37+
" \"filter_kg(action=remove_edges_by_continuous_attribute,edge_attribute=jaccard_index,threshold=0.2,remove_connected_nodes=true,qnode_keys=n02, direction=below)\",\n",
3638
" \"resultify(ignore_edge_direction=true)\",\n",
37-
" \"filter_results(action=sort_by_edge_attribute, edge_attribute=probability_drug_treats, direction=descending, max_results=15)\", \n",
38-
" \"return(message=true, store=true)\",\n",
39+
" \"filter_results(action=limit_number_of_results,max_results=50,prune_kg=true)\",\n",
40+
" \"return(message=true, store=true)\"\n",
3941
" ] } }"
4042
]
4143
},
@@ -64,9 +66,9 @@
6466
"source": [
6567
"# Unpack respsonse from JSON and display the information log\n",
6668
"response_dict = response_content.json()\n",
67-
"for message in response_dict['log']:\n",
68-
" if message['level'] >= 20:\n",
69-
" print(message['prefix']+message['message'])"
69+
"for message in response_dict['logs']:\n",
70+
" if message['level'] == \"INFO\":\n",
71+
" print(message['level']+\": \"+message['message'])"
7072
]
7173
},
7274
{
@@ -80,7 +82,7 @@
8082
" print(f\"Data: {response_dict['id']}\")\n",
8183
" match = re.search(r'(\\d+)$', response_dict['id'])\n",
8284
" if match:\n",
83-
" print(f\"GUI: https://arax.rtx.ai/?m={match.group(1)}\")\n",
85+
" print(f\"GUI: arax.transltr.io/?m={match.group(1)}\")\n",
8486
"else:\n",
8587
" print(\"No id was returned in response\")"
8688
]

code/ARAX/Examples/ARAX_Example3.ipynb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@
1919
"outputs": [],
2020
"source": [
2121
"# Set the base URL for the ARAX reasoner and its endpoint\n",
22-
"endpoint_url = 'https://arax.rtx.ai/api/rtx/v1/query'\n",
22+
"endpoint_url = 'https://arax.transltr.io/api/arax/v1.4/query'\n",
2323
"\n",
2424
"# Create a dict of the request, specifying the list of DSL commands\n",
25-
"query = {\"previous_message_processing_plan\": {\"processing_actions\": [\n",
26-
" \"add_qnode(name=DOID:9406, key=n00)\",\n",
27-
" \"add_qnode(type=biolink:ChemicalSubstance, is_set=true, key=n01)\",\n",
28-
" \"add_qnode(type=biolink:Protein, key=n02)\",\n",
25+
"query = {\"message\":{}, \"operations\": {\"actions\": [\n",
26+
" \"add_qnode(ids=DOID:9406, key=n00)\",\n",
27+
" \"add_qnode(categories=biolink:ChemicalEntity, is_set=true, key=n01)\",\n",
28+
" \"add_qnode(categories=biolink:Protein, key=n02)\",\n",
2929
" \"add_qedge(subject=n00, object=n01, key=e00)\", \n",
3030
" \"add_qedge(subject=n01, object=n02, key=e01)\",\n",
3131
" \"expand(edge_key=[e00,e01])\", \n",
3232
" \"overlay(action=overlay_clinical_info, observed_expected_ratio=true, virtual_relation_label=C1, subject_qnode_key=n00, object_qnode_key=n01)\", \n",
33-
" \"filter_kg(action=remove_edges_by_attribute, edge_attribute=observed_expected_ratio, direction=below, threshold=3, remove_connected_nodes=t, qnode_id=n01)\", \n",
34-
" \"filter_kg(action=remove_orphaned_nodes, node_type=protein)\",\n",
33+
" \"filter_kg(action=remove_edges_by_continuous_attribute, edge_attribute=observed_expected_ratio, \"\n",
34+
" \"direction=below, threshold=3, remove_connected_nodes=t, qnode_keys=n01)\",\n",
35+
" \"filter_kg(action=remove_orphaned_nodes, node_category=biolink:Protein)\",\n",
3536
" \"overlay(action=compute_ngd, virtual_relation_label=N1, subject_qnode_key=n01, object_qnode_key=n02)\", \n",
36-
" \"filter_kg(action=remove_edges_by_attribute, edge_attribute=ngd, direction=above, threshold=0.85, remove_connected_nodes=t, qnode_id=n02)\",\n",
37+
" \"filter_kg(action=remove_edges_by_continuous_attribute, edge_attribute=ngd, direction=above, threshold=0.85, \"\n",
38+
" \"remove_connected_nodes=t, qnode_keys=n02)\",\n",
3739
" \"resultify(ignore_edge_direction=true)\",\n",
3840
" \"return(message=true, store=true)\"\n",
3941
" ]}}"
@@ -51,7 +53,10 @@
5153
"status_code = response_content.status_code\n",
5254
"if status_code != 200:\n",
5355
" print(\"ERROR returned with status \"+str(status_code))\n",
54-
" print(response_content.json())\n",
56+
" #print(response_content.json())\n",
57+
" for x in response_content.json()['logs']:\n",
58+
" if x['level'] == \"ERROR\":\n",
59+
" print(x)\n",
5560
"else:\n",
5661
" print(f\"Response returned with status {status_code}\")"
5762
]
@@ -64,9 +69,9 @@
6469
"source": [
6570
"# Unpack respsonse from JSON and display the information log\n",
6671
"response_dict = response_content.json()\n",
67-
"for message in response_dict['log']:\n",
68-
" if message['level'] >= 20:\n",
69-
" print(message['prefix']+message['message'])"
72+
"for message in response_dict['logs']:\n",
73+
" if message['level'] == \"INFO\":\n",
74+
" print(message['level']+\": \"+message['message'])"
7075
]
7176
},
7277
{
@@ -80,7 +85,7 @@
8085
" print(f\"Data: {response_dict['id']}\")\n",
8186
" match = re.search(r'(\\d+)$', response_dict['id'])\n",
8287
" if match:\n",
83-
" print(f\"GUI: https://arax.rtx.ai/?m={match.group(1)}\")\n",
88+
" print(f\"GUI: https://arax.transltr.io/?m={match.group(1)}\")\n",
8489
"else:\n",
8590
" print(\"No id was returned in response\")"
8691
]

code/ARAX/test/test_ARAX_filter_results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def test_sort_by_score():
190190
assert len(message.results) == 8
191191
result_scores = [x.analyses[0].score for x in message.results]
192192
assert result_scores == sorted(result_scores)
193-
assert max(result_scores) < 1
193+
assert max(result_scores) <= 1
194194

195195
@pytest.mark.external
196196
def test_issue1506():

0 commit comments

Comments
 (0)