benchmate.literature
benchmate.literature.paperinfo
PaperInfo Objects
@dataclass(slots=True)
class PaperInfo()
Dataclass to hold information about a paper, this is constructed inside the Paper class and desined to be compatible with semantic search and embedding distance searches
benchmate.literature.literature
paper_from_response
def paper_from_response(response,
get_references=False,
get_related_papers=False,
get_cited_by=False)
take an openalex response and convert that to a paper object
Arguments:
response: response jsonget_references: whether to return references or notget_related_papers: whether to return related papers or notget_cited_by: whether to return cited papers or not
Returns:
paper object
paper_from_id
def paper_from_id(openalex,
id,
id_type,
get_references=False,
get_related_papers=False,
get_cited_by=False)
for an id type (instead of openalex id) this function will return a paper object, this can be useful when you are trying to
collect information from other sources like a uniprot api call
Arguments:
openalex: openalexid: the actual idid_type: type of the id, pmid, full doi url, pmcid or magidget_references: as the name suggestsget_related_papers: as the name suggestsget_cited_by: as the name suggests
Returns:
a paper object instance, this call paper_from_response under the hood
paper_from_link
def paper_from_link(link,
openalex,
get_references=False,
get_related_papers=False,
get_cited_by=False)
generate a paper object from an openalex link, this is useful for references and related works
Arguments:
link: openalex link
Returns:
a paper object
OpenAlex Objects
@dataclass
class OpenAlex()
just storing basic information about openalex, this includes the link and yourj api key
LitSearch Objects
class LitSearch()
search
def search(openalex,
pos_query,
pos_joiner="and",
neg_query=None,
neg_joiner="or",
fields=["title", "abstract", "doi", "publication_date", "venue"],
sort_by="relevance",
max_results=10000)
search pubmed and arxiv for a query, this is just keyword search no other params are implemented at the moment
Arguments:
query: this is a string that is passed to the search, as long as it is a valid query it will work and other fields can be specifieddatabase: pubmed or arxivresults: what to return, default is paper id PMID and arxiv idmax_results: max number of results to return default 1000
Returns:
paper ids specific to the database
Paper Objects
class Paper()
__init__
def __init__(paper_id)
This class is used to download and process a paper from a given id, it can also be used to process a paper from a file
Arguments:
paper_id: openalex id of the paper
get_json
def get_json(openalex)
for a given paper id query openalex api and get the detailed information
Arguments:
openalex: openalex instance, see above
Returns:
json from openalex
parse_json
def parse_json()
parse the json and extract the most useful information into separate fields
Returns:
paper.info instance filled in for the attrs that are defined explicitly
download
def download(destination)
download the paper pdf to the destination folder
Arguments:
destination: where to download the paper, it must exist, the folder will not be created or checked for existence
Returns:
download the paper pdf to the destination folder
get_references
def get_references(openalex)
get the references of the paper from openalex
Returns:
fill in the paper info references
get_related_works
def get_related_works(openalex)
get the related works of the paper from openalex
Returns:
fill in the paper info related_works
get_cited_by
def get_cited_by(openalex)
get the papers that cite this paper from openalex
Arguments:
cursor: the used does not need to worry about this, it is used for pagination and recursive calls
Returns:
fill in the paper info cited_by
process
def process(processor, extract=True, embed_text=True, embed_images=True)
run the paper processor pipeline
Arguments:
processor: processor class instanceextract: extract text, figures and tables from the pdfembed_text: chunk and embed the paper textembed_images: embed figures and tables
Returns:
filled in paper info, doesnt return anything but fills inplace
benchmate.literature.paper_processor
PaperProcessor Objects
class PaperProcessor()
__init__
def __init__(inference, cache_dir=None, device="gpu")
Init PaperProcessor, this will prepare and download the models if this is the first time you are using it. Make sure that
the cache directory exists and is consistent between run otherwise you will end up with multiple copies of the same models
Arguments:
inference: benchmate.inference.inference instance for embeddingcache_dir: cache dir for storing models, this exclusively for paddle ocr modelsdevice: gpu or cpu, not sure if you are ever going to run them on a tpu but that’s also supported
pipeline
def pipeline(papers, extract=True, embed_text=True, embed_images=True)
run the full paper processing pipeline
Arguments:
papers: list of paper class instanceextract: perform the steps in parseembed_text: semantically chunk and embed the chunks for the full textembed_images: embed the image and their associated text all in one go using a vl model
Returns:
doesnt return anything but fills in the attributes of the paper instances provided One thing to note here, if a paper instance has multiple files, it will go through them one by one and perform all the actions, there is no check for file order, sometimes the main paper is the first one sometimes not, this is especially true if we are downloading the tar file from ncbi, if there is a single oa location in openalex that one is preffered.
benchmate.literature.utils
PaperRelevance Objects
class PaperRelevance()
__call__
def __call__(abstracts, split_n=1, **kneedle_kwargs)
Score all abstracts through semantic + rerank stages.
Arguments:
abstracts: list of abstract strings, arbitrary orderuse_elbow: if True, use kneedle for both stage cutoffs; if False, use top_k_semantic/top_k_rerankkneedle_kwargs: passed through to KneeLocator if use_elbow=True
Returns:
(combined_scores, combined_threshold) combined_scores: list aligned to input abstracts order, 0.0 for non-survivors of stage 1 combined_threshold: minimum combined score to be considered relevant
reconstruct_abstract
def reconstruct_abstract(inverted_index)
reconstruct the abstract of the paper because openalex only returns an inverted index
Arguments:
inverted_index:
extract_pdfs_from_tar
def extract_pdfs_from_tar(file, destination, base_name)
extract all pdf files from a tar.gz file to a destination folder and return the paths to the extracted pdf files
this is there to process pmc tar.gz files
Arguments:
file: downloaded tar.gz filedestination: where to extract the pdf files
Returns:
a list of paths to the extracted pdf files
download_tar
def download_tar(download_link, file)
download the pmc tar file to destination file
Arguments:
download_link: web link to download tar filefile: file to write the tar file into
Returns:
write/download tar file to file