-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Update documentation for Hunflair2 release #3410
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a0ffa82
Add support of loading of hunflair2 model via Classifier.load()
f3db229
Fix model loading and update main documentation pages
e5b99e9
Add platform check while loading entity mention linking models and sh…
f1d5b03
Update documentation
c8ba30a
Add hint for model import order in __init__ file
cf07676
Revised link on main page
ec08032
Remove sync file
7b3b79d
Fix HunFlair (v1) warnings
bfb5747
Create HUNFLAIR2_TUTORIAL_3_TRAINING_NER.md (WIP)
WangXII c1d7912
Update HUNFLAIR2_TUTORIAL_3_TRAINING_NER.md
WangXII 1f55a7a
Update HUNFLAIR2_TUTORIAL_3_TRAINING_NER.md
WangXII 1fde791
Update HUNFLAIR2_TUTORIAL_3_TRAINING_NER.md
WangXII 67960cd
feat: linking tutorial w/ customizations
7200b10
fix: single entity vs multi-entity tagger
6e00ca9
feat: add new tutorials links
132ed81
chore: fix formatting on file not related to PR
d35fd5d
chore: make mypy happy
587f53c
fix: try remove circular import
76ed90f
chore: revert changes in non-pr related files
09e0bfe
Merge branch 'master' into hunflair2-release
3a9aaa1
Merge branch 'master' into hunflair2-release
alanakbik 848ac61
Merge branch 'master' into hunflair2-release
sg-wbi e3634ea
Add conversion of string to torch device for convenience
alanakbik 189c6e2
Ruff fixes
alanakbik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import inspect | ||
import logging | ||
import os | ||
import platform | ||
import re | ||
import stat | ||
import string | ||
|
@@ -648,6 +649,8 @@ def p(text: str) -> str: | |
emb = emb / torch.norm(emb) | ||
dense_embeddings.append(emb.cpu().numpy()) | ||
sent.clear_embeddings() | ||
|
||
# empty cuda cache if device is a cuda device | ||
if flair.device.type == "cuda": | ||
torch.cuda.empty_cache() | ||
|
||
|
@@ -681,6 +684,11 @@ def embed(self, entity_mentions: List[str]) -> Dict[str, np.ndarray]: | |
emb = emb / torch.norm(emb) | ||
query_embeddings["dense"].append(emb.cpu().numpy()) | ||
sent.clear_embeddings(self.embeddings["dense"].get_names()) | ||
|
||
# Sanity conversion: if flair.device was set as a string, convert to torch.device | ||
if isinstance(flair.device, str): | ||
flair.device = torch.device(flair.device) | ||
|
||
if flair.device.type == "cuda": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sg-wbi Is this really required? |
||
torch.cuda.empty_cache() | ||
|
||
|
@@ -836,9 +844,13 @@ def extract_entities_mentions(self, sentence: Sentence, entity_label_types: Dict | |
if any(label in ["diseases", "genes", "species", "chemical"] for label in sentence.annotation_layers): | ||
if not self._warned_legacy_sequence_tagger: | ||
logger.warning( | ||
"The tagger `Classifier.load('hunflair') is deprecated. Please update to: `Classifier.load('hunflair2')`." | ||
"It appears that the sentences have been annotated with HunFlair (version 1). " | ||
"Consider using HunFlair2 for improved extraction performance: Classifier.load('hunflair2')." | ||
"See https://github.com/flairNLP/flair/blob/master/resources/docs/HUNFLAIR2.md for further " | ||
"information." | ||
) | ||
self._warned_legacy_sequence_tagger = True | ||
|
||
entity_types = {e for sublist in entity_label_types.values() for e in sublist} | ||
entities_mentions = [ | ||
label for label in sentence.get_labels() if normalize_entity_type(label.value) in entity_types | ||
|
@@ -939,6 +951,14 @@ def _fetch_model(model_name: str) -> str: | |
if model_name in hf_model_map: | ||
model_name = hf_model_map[model_name] | ||
|
||
if platform.system() == "Windows": | ||
logger.warning( | ||
"You seem to run your application on a Windows system. Unfortunately, the abbreviation " | ||
"resolution of HunFlair2 is only available on Linux/Mac systems. Therefore, a model " | ||
"without abbreviation resolution is therefore loaded" | ||
) | ||
model_name += "-no-ab3p" | ||
|
||
return hf_download(model_name) | ||
|
||
@classmethod | ||
|
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,137 @@ | ||
# HunFlair2 | ||
|
||
*HunFlair2* is a state-of-the-art named entity tagger and linker for biomedical texts. It comes with | ||
models for genes/proteins, chemicals, diseases, species and cell lines. *HunFlair2* | ||
builds on pretrained domain-specific language models and outperforms other biomedical | ||
NER tools on unseen corpora. | ||
|
||
<b>Content:</b> | ||
[Quick Start](#quick-start) | | ||
[Tool Comparison](#comparison-to-other-biomedical-entity-extraction-tools) | | ||
[Tutorials](#tutorials) | | ||
[Citing HunFlair](#citing-hunflair2) | ||
|
||
## Quick Start | ||
|
||
#### Requirements and Installation | ||
*HunFlair2* is based on Flair 0.13+ and Python 3.8+. If you do not have Python 3.8, install it first. | ||
Then, in your favorite virtual environment, simply do: | ||
``` | ||
pip install flair | ||
``` | ||
|
||
#### Example 1: Biomedical NER | ||
Let's run named entity recognition (NER) over an example sentence. All you need to do is | ||
make a Sentence, load a pre-trained model and use it to predict tags for the sentence: | ||
```python | ||
from flair.data import Sentence | ||
from flair.nn import Classifier | ||
|
||
# make a sentence | ||
sentence = Sentence("Behavioral abnormalities in the Fmr1 KO2 Mouse Model of Fragile X Syndrome") | ||
|
||
# load biomedical NER tagger | ||
tagger = Classifier.load("hunflair2") | ||
|
||
# tag sentence | ||
tagger.predict(sentence) | ||
``` | ||
Done! The Sentence now has entity annotations. Let's print the entities found by the tagger: | ||
```python | ||
for entity in sentence.get_labels(): | ||
print(entity) | ||
``` | ||
This should print: | ||
```console | ||
Span[0:2]: "Behavioral abnormalities" → Disease (1.0) | ||
Span[4:5]: "Fmr1" → Gene (1.0) | ||
Span[6:7]: "Mouse" → Species (1.0) | ||
Span[9:12]: "Fragile X Syndrome" → Disease (1.0) | ||
``` | ||
|
||
#### Example 2: Biomedical NEN | ||
For improved integration and aggregation from multiple different documents linking / normalizing the entities to | ||
standardized ontologies or knowledge bases is required. Let's perform entity normalization by using | ||
specialized models per entity type: | ||
```python | ||
from flair.data import Sentence | ||
from flair.models import EntityMentionLinker | ||
from flair.nn import Classifier | ||
|
||
# make a sentence | ||
sentence = Sentence("Behavioral abnormalities in the Fmr1 KO2 Mouse Model of Fragile X Syndrome") | ||
|
||
# load biomedical NER tagger + predict entities | ||
tagger = Classifier.load("hunflair2") | ||
tagger.predict(sentence) | ||
|
||
# load gene linker and perform normalization | ||
gene_linker = EntityMentionLinker.load("gene-linker") | ||
gene_linker.predict(sentence) | ||
|
||
# load disease linker and perform normalization | ||
disease_linker = EntityMentionLinker.load("disease-linker") | ||
disease_linker.predict(sentence) | ||
|
||
# load species linker and perform normalization | ||
species_linker = EntityMentionLinker.load("species-linker") | ||
species_linker.predict(sentence) | ||
``` | ||
**Note**, the ontologies and knowledge bases used are pre-processed the first time the normalisation is executed, | ||
which might takes a certain amount of time. All further calls are then based on this pre-processing and run | ||
much faster. | ||
|
||
Done! The Sentence now has entity normalizations. Let's print the entity identifiers found by the linkers: | ||
```python | ||
for entity in sentence.get_labels("link"): | ||
print(entity) | ||
``` | ||
This should print: | ||
```console | ||
Span[0:2]: "Behavioral abnormalities" → MESH:D001523/name=Mental Disorders (197.9467010498047) | ||
Span[4:5]: "Fmr1" → 108684022/name=FRAXA (219.9510040283203) | ||
Span[6:7]: "Mouse" → 10090/name=Mus musculus (213.6201934814453) | ||
Span[9:12]: "Fragile X Syndrome" → MESH:D005600/name=Fragile X Syndrome (193.7115020751953) | ||
``` | ||
|
||
## Comparison to other biomedical entity extraction tools | ||
Tools for biomedical entity extraction are typically trained and evaluated on single, rather small gold standard | ||
data sets. However, they are applied "in the wild" to a much larger collection of texts, often varying in | ||
topic, entity distribution, genre (e.g. patents vs. scientific articles) and text type (e.g. abstract | ||
vs. full text), which can lead to severe drops in performance. | ||
|
||
*HunFlair2* outperforms other biomedical entity extraction tools on corpora not used for training of neither | ||
*HunFlair2* or any of the competitor tools. | ||
|
||
| Corpus | Entity Type | BENT | BERN2 | PubTator Central | SciSpacy | HunFlair | | ||
|----------------------------------------------------------------------------------------------|-------------|-------|-------|------------------|----------|-------------| | ||
| [MedMentions](https://github.com/chanzuckerberg/MedMentions) | Chemical | 40.90 | 41.79 | 31.28 | 34.95 | *__51.17__* | | ||
| | Disease | 45.94 | 47.33 | 41.11 | 40.78 | *__57.27__* | | ||
| [tmVar (v3)](https://github.com/ncbi/tmVar3?tab=readme-ov-file) | Gene | 0.54 | 43.96 | *__86.02__* | - | 76.75 | | ||
| [BioID](https://biocreative.bioinformatics.udel.edu/media/store/files/2018/BC6_track1_1.pdf) | Species | 10.35 | 14.35 | *__58.90__* | 37.14 | 49.66 | | ||
||||| | ||
| Average | All | 24.43 | 36.86 | 54.33 | 37.61 | *__58.79__* | | ||
|
||
<sub>All results are F1 scores highlighting end-to-end performance, i.e., named entity recognition and normalization, | ||
using partial matching of predicted text offsets with the original char offsets of the gold standard data. | ||
We allow a shift by max one character.</sub> | ||
|
||
You can find detailed evaluations and discussions in [our paper](https://arxiv.org/abs/2402.12372). | ||
|
||
## Tutorials | ||
We provide a set of quick tutorials to get you started with *HunFlair2*: | ||
* [Tutorial 1: Tagging biomedical named entities](HUNFLAIR2_TUTORIAL_1_TAGGING.md) | ||
* [Tutorial 2: Linking biomedical named entities](HUNFLAIR2_TUTORIAL_2_LINKING.md) | ||
* [Tutorial 3: Training NER models](HUNFLAIR2_TUTORIAL_3_TRAINING_NER.md) | ||
* [Tutorial 4: Customizing linking](HUNFLAIR2_TUTORIAL_4_CUSTOMIZE_LINKING.md) | ||
|
||
## Citing HunFlair2 | ||
Please cite the following paper when using *HunFlair2*: | ||
~~~ | ||
@article{sanger2024hunflair2, | ||
title={HunFlair2 in a cross-corpus evaluation of biomedical named entity recognition and normalization tools}, | ||
author={S{\"a}nger, Mario and Garda, Samuele and Wang, Xing David and Weber-Genzel, Leon and Droop, Pia and Fuchs, Benedikt and Akbik, Alan and Leser, Ulf}, | ||
journal={arXiv preprint arXiv:2402.12372}, | ||
year={2024} | ||
} | ||
~~~ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sg-wbi Is this really required?