-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding REST API server for prediction (#4)
- Loading branch information
Showing
12 changed files
with
182 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""REST API for CADA using FastAPI.""" | ||
from contextlib import asynccontextmanager | ||
import os | ||
import typing | ||
|
||
from dotenv import load_dotenv | ||
from fastapi import FastAPI, Query | ||
from pydantic import BaseModel | ||
from starlette.responses import Response | ||
|
||
import cada_prio | ||
from cada_prio import predict | ||
|
||
# Load environment | ||
env = os.environ | ||
load_dotenv() | ||
|
||
#: Debug mode | ||
DEBUG = env.get("CADA_DEBUG", "false").lower() in ("true", "1") | ||
#: Path to data / model | ||
PATH_DATA = env.get("CADA_PATH_DATA", "/data/cada") | ||
|
||
#: The CADA models, to be loaded on startup. | ||
GLOBAL_STATIC = {} | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
# Load the models | ||
all_to_hgnc, hgnc_info_by_id = predict.load_hgnc_info(PATH_DATA) | ||
GLOBAL_STATIC["all_to_hgnc"] = all_to_hgnc | ||
GLOBAL_STATIC["hgnc_info_by_id"] = hgnc_info_by_id | ||
graph, model = predict.load_graph_model(PATH_DATA) | ||
GLOBAL_STATIC["graph"] = graph | ||
GLOBAL_STATIC["model"] = model | ||
|
||
yield | ||
|
||
GLOBAL_STATIC.clear() | ||
|
||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
|
||
# Register endpoint for returning CADA version. | ||
@app.get("/version") | ||
async def version(): | ||
return Response(content=cada_prio.__version__) | ||
|
||
|
||
class PredictionResult(BaseModel): | ||
rank: int | ||
score: float | ||
gene_symbol: str | ||
ncbi_gene_id: str | ||
hgnc_id: str | ||
|
||
|
||
# Register endpoint for the prediction | ||
@app.get("/predict") | ||
async def handle_predict( | ||
hpo_terms: typing.Annotated[typing.List[str], Query()], | ||
genes: typing.Annotated[typing.Optional[typing.List[str]], Query()] = [], | ||
): | ||
_, sorted_scores = predict.run_prediction( | ||
hpo_terms, | ||
genes, | ||
GLOBAL_STATIC["all_to_hgnc"], | ||
GLOBAL_STATIC["graph"], | ||
GLOBAL_STATIC["model"], | ||
) | ||
hgnc_info_by_id = GLOBAL_STATIC["hgnc_info_by_id"] | ||
return [ | ||
PredictionResult( | ||
rank=rank, | ||
score=score, | ||
gene_symbol=hgnc_info_by_id[hgnc_id].symbol, | ||
ncbi_gene_id=hgnc_info_by_id[hgnc_id].ncbi_gene_id, | ||
hgnc_id=hgnc_info_by_id[hgnc_id].hgnc_id, | ||
) | ||
for rank, (hgnc_id, score) in enumerate(sorted_scores.items(), start=1) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from cada_prio import predict | ||
|
||
|
||
def test_predict_smoke_test(tmpdir): | ||
predict.run("tests/data/model_smoke", "HP:0008551") | ||
def test_predict_smoke_test(): | ||
predict.run("tests/data/model_smoke", ["HP:0008551"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from starlette.testclient import TestClient | ||
|
||
from cada_prio import rest_server | ||
|
||
|
||
@mock.patch("cada_prio.rest_server.PATH_DATA", "tests/data/model_smoke") | ||
@pytest.mark.asyncio | ||
async def test_version(): | ||
with TestClient(rest_server.app) as client: | ||
response = client.get("/version") | ||
assert response.status_code == 200 | ||
|
||
|
||
@mock.patch("cada_prio.rest_server.PATH_DATA", "tests/data/model_smoke") | ||
@pytest.mark.asyncio | ||
async def test_predict_with_gene(): | ||
with TestClient(rest_server.app) as client: | ||
response = client.get("/predict/?hpo_terms=HP:0008551&hpo=HP:0000007&gene=MKS1") | ||
assert response.status_code == 200 | ||
|
||
|
||
@mock.patch("cada_prio.rest_server.PATH_DATA", "tests/data/model_smoke") | ||
@pytest.mark.asyncio | ||
async def test_predict_without_gene(): | ||
with TestClient(rest_server.app) as client: | ||
response = client.get("/predict/?hpo_terms=HP:0008551&hpo=HP:0000007") | ||
assert response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters