Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ncbi tests #86

Merged
merged 22 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions semantic_search/ncbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from pathlib import Path
from typing import Any, Dict, List, Generator
import logging
import json
import time

import requests
from Bio import Medline
from dotenv import load_dotenv
from pydantic import BaseSettings
from fastapi import HTTPException

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,11 +72,11 @@ def _parse_medline(text: str) -> List[dict]:
return medline_records


def _get_eutil_records(eutil: str, id: List[str], **opts) -> List[Dict[Any, Any]]:
def _get_eutil_records(eutil: str, _id: List[str], **opts) -> List[Dict[Any, Any]]:
"""Call one of the NCBI EUTILITIES and returns data as Python objects."""
eutils_params = {
"db": "pubmed",
"id": ",".join(id),
"id": ",".join(_id),
"retstart": 0,
"retmode": "xml",
"api_key": settings.ncbi_eutils_api_key,
Expand All @@ -99,8 +99,7 @@ def _medline_to_docs(records: List[Dict[str, str]]) -> List[Dict[str, str]]:
docs = []
for record in records:
if "PMID" not in record:
logging.warn(f"No PMID for {json.dumps(record)}")
continue
raise HTTPException(status_code=422, detail=record["id:"][-1])
pmid = record["PMID"]
abstract = record["AB"] if "AB" in record else ""
title = record["TI"] if "TI" in record else ""
Expand All @@ -117,10 +116,10 @@ def uids_to_docs(uids: List[str]) -> Generator[List[Dict[str, str]], None, None]
for i in range(num_queries):
lower = i * MAX_EFETCH_RETMAX
upper = min([lower + MAX_EFETCH_RETMAX, num_uids])
id = uids[lower:upper]
_id = uids[lower:upper]
try:
start_time = time.time()
eutil_response = _get_eutil_records("efetch", id, rettype="medline", retmode="text")
eutil_response = _get_eutil_records("efetch", _id, rettype="medline", retmode="text")
duration = time.time() - start_time
logging.info(
f"Retrieved docs {lower} through {upper - 1} of {num_uids - 1} in {duration}s"
Expand Down
Loading