ArneBinder
commited on
Commit
•
1ef2db4
1
Parent(s):
c5a482a
initial release from https://github.com/ArneBinder/pie-datasets/pull/106
Browse files- README.md +50 -0
- drugprot.py +154 -0
- requirements.txt +1 -0
README.md
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# PIE Dataset Card for "DrugProt"
|
2 |
+
|
3 |
+
This is a [PyTorch-IE](https://github.com/ChristophAlt/pytorch-ie) wrapper for the
|
4 |
+
[DrugProt Huggingface dataset loading script](https://huggingface.co/datasets/bigbio/drugprot).
|
5 |
+
|
6 |
+
## Data Schema
|
7 |
+
|
8 |
+
There are two versions of the dataset supported, `drugprot_source` and `drugprot_bigbio_kb`.
|
9 |
+
|
10 |
+
#### `DrugprotDocument` for `drugprot_source`
|
11 |
+
|
12 |
+
defines following fields:
|
13 |
+
|
14 |
+
- `text` (str)
|
15 |
+
- `id` (str, optional)
|
16 |
+
- `metadata` (dictionary, optional)
|
17 |
+
- `title` (str, optional)
|
18 |
+
- `abstract` (str, optional)
|
19 |
+
|
20 |
+
and the following annotation layers:
|
21 |
+
|
22 |
+
- `entities` (annotation type: `LabeledSpan`, target: `text`)
|
23 |
+
- `relations` (annotation type: `BinaryRelation`, target: `entities`)
|
24 |
+
|
25 |
+
#### `DrugprotBigbioDocument` for `drugprot_bigbio_kb`
|
26 |
+
|
27 |
+
defines following fields:
|
28 |
+
|
29 |
+
- `text` (str)
|
30 |
+
- `id` (str, optional)
|
31 |
+
- `metadata` (dictionary, optional)
|
32 |
+
|
33 |
+
and the following annotation layers:
|
34 |
+
|
35 |
+
- `passages` (annotation type: `LabeledSpan`, target: `text`)
|
36 |
+
- `entities` (annotation type: `LabeledSpan`, target: `text`)
|
37 |
+
- `relations` (annotation type: `BinaryRelation`, target: `entities`)
|
38 |
+
|
39 |
+
See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/annotations.py) for the annotation
|
40 |
+
type definitions.
|
41 |
+
|
42 |
+
## Document Converters
|
43 |
+
|
44 |
+
The dataset provides predefined document converters for the following target document types:
|
45 |
+
|
46 |
+
- `pie_modules.documents.TextDocumentWithLabeledSpansAndBinaryRelations` for `DrugprotDocument`
|
47 |
+
- `pie_modules.documents.TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions` for `DrugprotBigbioDocument`
|
48 |
+
|
49 |
+
See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/documents.py) for the document type
|
50 |
+
definitions.
|
drugprot.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
from typing import Any, Dict, Optional, Union
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
from pie_modules.annotations import BinaryRelation, LabeledSpan
|
6 |
+
from pie_modules.documents import (
|
7 |
+
AnnotationLayer,
|
8 |
+
TextBasedDocument,
|
9 |
+
TextDocumentWithLabeledSpansAndBinaryRelations,
|
10 |
+
TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions,
|
11 |
+
annotation_field,
|
12 |
+
)
|
13 |
+
|
14 |
+
from pie_datasets import GeneratorBasedBuilder
|
15 |
+
|
16 |
+
|
17 |
+
@dataclass
|
18 |
+
class DrugprotDocument(TextBasedDocument):
|
19 |
+
title: Optional[str] = None
|
20 |
+
abstract: Optional[str] = None
|
21 |
+
entities: AnnotationLayer[LabeledSpan] = annotation_field(target="text")
|
22 |
+
relations: AnnotationLayer[BinaryRelation] = annotation_field(target="entities")
|
23 |
+
|
24 |
+
|
25 |
+
@dataclass
|
26 |
+
class DrugprotBigbioDocument(TextBasedDocument):
|
27 |
+
passages: AnnotationLayer[LabeledSpan] = annotation_field(target="text")
|
28 |
+
entities: AnnotationLayer[LabeledSpan] = annotation_field(target="text")
|
29 |
+
relations: AnnotationLayer[BinaryRelation] = annotation_field(target="entities")
|
30 |
+
|
31 |
+
|
32 |
+
def example2drugprot(example: Dict[str, Any]) -> DrugprotDocument:
|
33 |
+
metadata = {"entity_ids": []}
|
34 |
+
id2labeled_span: Dict[str, LabeledSpan] = {}
|
35 |
+
|
36 |
+
document = DrugprotDocument(
|
37 |
+
text=example["text"],
|
38 |
+
title=example["title"],
|
39 |
+
abstract=example["abstract"],
|
40 |
+
id=example["document_id"],
|
41 |
+
metadata=metadata,
|
42 |
+
)
|
43 |
+
for span in example["entities"]:
|
44 |
+
labeled_span = LabeledSpan(
|
45 |
+
start=span["offset"][0],
|
46 |
+
end=span["offset"][1],
|
47 |
+
label=span["type"],
|
48 |
+
)
|
49 |
+
document.entities.append(labeled_span)
|
50 |
+
document.metadata["entity_ids"].append(span["id"])
|
51 |
+
id2labeled_span[span["id"]] = labeled_span
|
52 |
+
for relation in example["relations"]:
|
53 |
+
document.relations.append(
|
54 |
+
BinaryRelation(
|
55 |
+
head=id2labeled_span[relation["arg1_id"]],
|
56 |
+
tail=id2labeled_span[relation["arg2_id"]],
|
57 |
+
label=relation["type"],
|
58 |
+
)
|
59 |
+
)
|
60 |
+
return document
|
61 |
+
|
62 |
+
|
63 |
+
def example2drugprot_bigbio(example: Dict[str, Any]) -> DrugprotBigbioDocument:
|
64 |
+
text = " ".join([" ".join(passage["text"]) for passage in example["passages"]])
|
65 |
+
doc_id = example["document_id"]
|
66 |
+
metadata = {"entity_ids": []}
|
67 |
+
id2labeled_span: Dict[str, LabeledSpan] = {}
|
68 |
+
|
69 |
+
document = DrugprotBigbioDocument(
|
70 |
+
text=text,
|
71 |
+
id=doc_id,
|
72 |
+
metadata=metadata,
|
73 |
+
)
|
74 |
+
for passage in example["passages"]:
|
75 |
+
document.passages.append(
|
76 |
+
LabeledSpan(
|
77 |
+
start=passage["offsets"][0][0],
|
78 |
+
end=passage["offsets"][0][1],
|
79 |
+
label=passage["type"],
|
80 |
+
)
|
81 |
+
)
|
82 |
+
# We sort labels and relation to always have an deterministic order for testing purposes.
|
83 |
+
for span in example["entities"]:
|
84 |
+
labeled_span = LabeledSpan(
|
85 |
+
start=span["offsets"][0][0],
|
86 |
+
end=span["offsets"][0][1],
|
87 |
+
label=span["type"],
|
88 |
+
)
|
89 |
+
document.entities.append(labeled_span)
|
90 |
+
document.metadata["entity_ids"].append(span["id"])
|
91 |
+
id2labeled_span[span["id"]] = labeled_span
|
92 |
+
for relation in example["relations"]:
|
93 |
+
document.relations.append(
|
94 |
+
BinaryRelation(
|
95 |
+
head=id2labeled_span[relation["arg1_id"]],
|
96 |
+
tail=id2labeled_span[relation["arg2_id"]],
|
97 |
+
label=relation["type"],
|
98 |
+
)
|
99 |
+
)
|
100 |
+
return document
|
101 |
+
|
102 |
+
|
103 |
+
class Drugprot(GeneratorBasedBuilder):
|
104 |
+
DOCUMENT_TYPES = {
|
105 |
+
"drugprot_source": DrugprotDocument,
|
106 |
+
"drugprot_bigbio_kb": DrugprotBigbioDocument,
|
107 |
+
}
|
108 |
+
|
109 |
+
BASE_DATASET_PATH = "bigbio/drugprot"
|
110 |
+
BASE_DATASET_REVISION = "38ff03d68347aaf694e598c50cb164191f50f61c"
|
111 |
+
|
112 |
+
BUILDER_CONFIGS = [
|
113 |
+
datasets.BuilderConfig(
|
114 |
+
name="drugprot_source",
|
115 |
+
version=datasets.Version("1.0.2"),
|
116 |
+
description="DrugProt source version",
|
117 |
+
),
|
118 |
+
datasets.BuilderConfig(
|
119 |
+
name="drugprot_bigbio_kb",
|
120 |
+
version=datasets.Version("1.0.0"),
|
121 |
+
description="DrugProt BigBio version",
|
122 |
+
),
|
123 |
+
]
|
124 |
+
|
125 |
+
@property
|
126 |
+
def document_converters(self):
|
127 |
+
if self.config.name == "drugprot_source":
|
128 |
+
return {
|
129 |
+
TextDocumentWithLabeledSpansAndBinaryRelations: {
|
130 |
+
"entities": "labeled_spans",
|
131 |
+
"relations": "binary_relations",
|
132 |
+
}
|
133 |
+
}
|
134 |
+
elif self.config.name == "drugprot_bigbio_kb":
|
135 |
+
return {
|
136 |
+
TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions: {
|
137 |
+
"passages": "labeled_partitions",
|
138 |
+
"entities": "labeled_spans",
|
139 |
+
"relations": "binary_relations",
|
140 |
+
}
|
141 |
+
}
|
142 |
+
else:
|
143 |
+
raise ValueError(f"Unknown dataset name: {self.config.name}")
|
144 |
+
|
145 |
+
def _generate_document(
|
146 |
+
self,
|
147 |
+
example: Dict[str, Any],
|
148 |
+
) -> Union[DrugprotDocument, DrugprotBigbioDocument]:
|
149 |
+
if self.config.name == "drugprot_source":
|
150 |
+
return example2drugprot(example)
|
151 |
+
elif self.config.name == "drugprot_bigbio_kb":
|
152 |
+
return example2drugprot_bigbio(example)
|
153 |
+
else:
|
154 |
+
raise ValueError(f"Unknown dataset config name: {self.config.name}")
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
pie-datasets>=0.9.0,<0.10.0
|