ArneBinder commited on
Commit
a8730dc
1 Parent(s): 1e5e4bd

update to pie-datasets 0.3.3

Browse files
Files changed (3) hide show
  1. README.md +28 -0
  2. requirements.txt +2 -0
  3. scidtb_argmin.py +55 -28
README.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PIE Dataset Card for "SciDTB Argmin"
2
+
3
+ This is a [PyTorch-IE](https://github.com/ChristophAlt/pytorch-ie) wrapper for the
4
+ [SciDTB ArgMin Huggingface dataset loading script](https://huggingface.co/datasets/DFKI-SLT/scidtb_argmin).
5
+
6
+ ## Data Schema
7
+
8
+ The document type for this dataset is `SciDTBArgminDocument` which defines the following data fields:
9
+
10
+ - `tokens` (Tuple of string)
11
+ - `id` (str, optional)
12
+ - `metadata` (dictionary, optional)
13
+
14
+ and the following annotation layers:
15
+
16
+ - `units` (annotation type: `LabeledSpan`, target: `tokens`)
17
+ - `relations` (annotation type: `BinaryRelation`, target: `units`)
18
+
19
+ See [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/annotations.py) for the annotation type definitions.
20
+
21
+ ## Document Converters
22
+
23
+ The dataset provides document converters for the following target document types:
24
+
25
+ - `pytorch_ie.documents.TextDocumentWithLabeledSpansAndBinaryRelations`
26
+
27
+ See [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/documents.py) for the document type
28
+ definitions.
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pie-datasets>=0.3.3
2
+ pytorch-ie>=0.29.1
scidtb_argmin.py CHANGED
@@ -3,11 +3,17 @@ import logging
3
  from typing import Any, Callable, Dict, List, Optional, Tuple
4
 
5
  import datasets
6
- import pytorch_ie.data.builder
7
  from pytorch_ie.annotations import BinaryRelation, LabeledSpan
8
  from pytorch_ie.core import AnnotationList, Document, annotation_field
 
 
 
 
9
  from pytorch_ie.utils.span import bio_tags_to_spans
10
 
 
 
 
11
  log = logging.getLogger(__name__)
12
 
13
 
@@ -23,25 +29,27 @@ def labels_and_spans_to_bio_tags(
23
 
24
 
25
  @dataclasses.dataclass
26
- class SciDTBArgminDocument(Document):
27
- tokens: Tuple[str, ...]
28
- id: Optional[str] = None
29
- metadata: Dict[str, Any] = dataclasses.field(default_factory=dict)
30
  units: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
31
  relations: AnnotationList[BinaryRelation] = annotation_field(target="units")
32
 
33
 
 
 
 
 
 
 
34
  def example_to_document(
35
  example: Dict[str, Any],
36
- unit_bio_int2str: Callable[[int], str],
37
- unit_label_int2str: Callable[[int], str],
38
- relation_int2str: Callable[[int], str],
39
  ):
40
-
41
  document = SciDTBArgminDocument(id=example["id"], tokens=tuple(example["data"]["token"]))
42
- bio_tags = unit_bio_int2str(example["data"]["unit-bio"])
43
- unit_labels = unit_label_int2str(example["data"]["unit-label"])
44
- roles = relation_int2str(example["data"]["role"])
45
  tag_sequence = [
46
  f"{bio}-{label}|{role}|{parent_offset}"
47
  for bio, label, role, parent_offset in zip(
@@ -62,7 +70,7 @@ def example_to_document(
62
  ]
63
  document.units.extend(units)
64
 
65
- # TODO: check if direction of the relation is correct (what is the head / tail of the relation?)
66
  relations = []
67
  for idx, parent_offset in enumerate(span_parent_offsets):
68
  if span_roles[idx] != "none":
@@ -79,11 +87,10 @@ def example_to_document(
79
 
80
  def document_to_example(
81
  document: SciDTBArgminDocument,
82
- unit_bio_str2int: Callable[[str], int],
83
- unit_label_str2int: Callable[[str], int],
84
- relation_str2int: Callable[[str], int],
85
  ) -> Dict[str, Any]:
86
-
87
  unit2idx = {unit: idx for idx, unit in enumerate(document.units)}
88
  unit2parent_relation = {relation.head: relation for relation in document.relations}
89
 
@@ -112,19 +119,39 @@ def document_to_example(
112
 
113
  data = {
114
  "token": list(document.tokens),
115
- "unit-bio": unit_bio_str2int(bio_tags),
116
- "unit-label": unit_label_str2int(unit_labels),
117
- "role": relation_str2int(roles),
118
  "parent-offset": [int(idx_str) for idx_str in parent_offsets],
119
  }
120
  result = {"id": document.id, "data": data}
121
  return result
122
 
123
 
124
- class SciDTBArgmin(pytorch_ie.data.builder.GeneratorBasedBuilder):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  DOCUMENT_TYPE = SciDTBArgminDocument
126
 
 
 
 
 
127
  BASE_DATASET_PATH = "DFKI-SLT/scidtb_argmin"
 
128
 
129
  BUILDER_CONFIGS = [datasets.BuilderConfig(name="default")]
130
 
@@ -132,15 +159,15 @@ class SciDTBArgmin(pytorch_ie.data.builder.GeneratorBasedBuilder):
132
 
133
  def _generate_document_kwargs(self, dataset):
134
  return {
135
- "unit_bio_int2str": dataset.features["data"].feature["unit-bio"].int2str,
136
- "unit_label_int2str": dataset.features["data"].feature["unit-label"].int2str,
137
- "relation_int2str": dataset.features["data"].feature["role"].int2str,
138
  }
139
 
140
- def _generate_document(self, example, unit_bio_int2str, unit_label_int2str, relation_int2str):
141
  return example_to_document(
142
  example,
143
- unit_bio_int2str=unit_bio_int2str,
144
- unit_label_int2str=unit_label_int2str,
145
- relation_int2str=relation_int2str,
146
  )
 
3
  from typing import Any, Callable, Dict, List, Optional, Tuple
4
 
5
  import datasets
 
6
  from pytorch_ie.annotations import BinaryRelation, LabeledSpan
7
  from pytorch_ie.core import AnnotationList, Document, annotation_field
8
+ from pytorch_ie.documents import (
9
+ TextDocumentWithLabeledSpansAndBinaryRelations,
10
+ TokenBasedDocument,
11
+ )
12
  from pytorch_ie.utils.span import bio_tags_to_spans
13
 
14
+ from pie_datasets import GeneratorBasedBuilder
15
+ from pie_datasets.document.conversion import token_based_document_to_text_based
16
+
17
  log = logging.getLogger(__name__)
18
 
19
 
 
29
 
30
 
31
  @dataclasses.dataclass
32
+ class SciDTBArgminDocument(TokenBasedDocument):
 
 
 
33
  units: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
34
  relations: AnnotationList[BinaryRelation] = annotation_field(target="units")
35
 
36
 
37
+ @dataclasses.dataclass
38
+ class SimplifiedSciDTBArgminDocument(TokenBasedDocument):
39
+ labeled_spans: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
40
+ binary_relations: AnnotationList[BinaryRelation] = annotation_field(target="labeled_spans")
41
+
42
+
43
  def example_to_document(
44
  example: Dict[str, Any],
45
+ unit_bio: datasets.ClassLabel,
46
+ unit_label: datasets.ClassLabel,
47
+ relation: datasets.ClassLabel,
48
  ):
 
49
  document = SciDTBArgminDocument(id=example["id"], tokens=tuple(example["data"]["token"]))
50
+ bio_tags = unit_bio.int2str(example["data"]["unit-bio"])
51
+ unit_labels = unit_label.int2str(example["data"]["unit-label"])
52
+ roles = relation.int2str(example["data"]["role"])
53
  tag_sequence = [
54
  f"{bio}-{label}|{role}|{parent_offset}"
55
  for bio, label, role, parent_offset in zip(
 
70
  ]
71
  document.units.extend(units)
72
 
73
+ # The relation direction is as in "f{head} {relation_label} {tail}"
74
  relations = []
75
  for idx, parent_offset in enumerate(span_parent_offsets):
76
  if span_roles[idx] != "none":
 
87
 
88
  def document_to_example(
89
  document: SciDTBArgminDocument,
90
+ unit_bio: datasets.ClassLabel,
91
+ unit_label: datasets.ClassLabel,
92
+ relation: datasets.ClassLabel,
93
  ) -> Dict[str, Any]:
 
94
  unit2idx = {unit: idx for idx, unit in enumerate(document.units)}
95
  unit2parent_relation = {relation.head: relation for relation in document.relations}
96
 
 
119
 
120
  data = {
121
  "token": list(document.tokens),
122
+ "unit-bio": unit_bio.str2int(bio_tags),
123
+ "unit-label": unit_label.str2int(unit_labels),
124
+ "role": relation.str2int(roles),
125
  "parent-offset": [int(idx_str) for idx_str in parent_offsets],
126
  }
127
  result = {"id": document.id, "data": data}
128
  return result
129
 
130
 
131
+ def convert_to_text_document_with_labeled_spans_and_binary_relations(
132
+ document: SciDTBArgminDocument,
133
+ ) -> TextDocumentWithLabeledSpansAndBinaryRelations:
134
+ doc_simplified = document.as_type(
135
+ SimplifiedSciDTBArgminDocument,
136
+ field_mapping={"units": "labeled_spans", "relations": "binary_relations"},
137
+ )
138
+ result = token_based_document_to_text_based(
139
+ doc_simplified,
140
+ result_document_type=TextDocumentWithLabeledSpansAndBinaryRelations,
141
+ join_tokens_with=" ",
142
+ )
143
+ return result
144
+
145
+
146
+ class SciDTBArgmin(GeneratorBasedBuilder):
147
  DOCUMENT_TYPE = SciDTBArgminDocument
148
 
149
+ DOCUMENT_CONVERTERS = {
150
+ TextDocumentWithLabeledSpansAndBinaryRelations: convert_to_text_document_with_labeled_spans_and_binary_relations
151
+ }
152
+
153
  BASE_DATASET_PATH = "DFKI-SLT/scidtb_argmin"
154
+ BASE_DATASET_REVISION = "8c02587edcb47ab5b102692bd10bfffd1844a09b"
155
 
156
  BUILDER_CONFIGS = [datasets.BuilderConfig(name="default")]
157
 
 
159
 
160
  def _generate_document_kwargs(self, dataset):
161
  return {
162
+ "unit_bio": dataset.features["data"].feature["unit-bio"],
163
+ "unit_label": dataset.features["data"].feature["unit-label"],
164
+ "relation": dataset.features["data"].feature["role"],
165
  }
166
 
167
+ def _generate_document(self, example, unit_bio, unit_label, relation):
168
  return example_to_document(
169
  example,
170
+ unit_bio=unit_bio,
171
+ unit_label=unit_label,
172
+ relation=relation,
173
  )