Skip to content

Commit 4de30aa

Browse files
authored
use pie-documents (#476)
Changes: - use `pie-documents` - remove `annotations` and `documents`, but re-export them from `pie-documents` - remove respective tests - remove `metrics` and `statistics` (except `TokenCountCollector`), but re-export them from `pie-documents` - remove respective tests - remove `pandas` dependency (was just used in metrics) - remove `is_contained_in` and `has_overlap` from `utils.span`, but re-export them from `pie-documents` - remove test file with tests that were commented out - lower codecov project target from 70% to 68% This requires pie-documents release [v0.1.0](https://github.com/ArneBinder/pie-documents/releases/tag/v0.1.0). We mark this as **breaking, since documents and annotations are now from another package** which may cause type mismatches. **IMPORTANT: This will be incompatible with `pie-modules` (this is enforced via may pytorch-ie version in latest pie-modules release).**
1 parent 7bc0040 commit 4de30aa

File tree

19 files changed

+226
-2069
lines changed

19 files changed

+226
-2069
lines changed

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ coverage:
22
status:
33
project:
44
default:
5-
target: 70% # TODO: switch back to auto
5+
target: 68% # TODO: switch back to auto
66
threshold: 1% # the leniency in hitting the target
77
patch:
88
default:

poetry.lock

Lines changed: 123 additions & 156 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ dynamic = [ "classifiers" ]
1818
requires-python = ">=3.9,<4.0"
1919
dependencies = [
2020
"pie-core >=0.2.1, <0.4.0",
21+
"pie-documents >=0.1.0, <0.2.0",
2122
"torch >=1.10",
2223
"pytorch-lightning >=2, <3",
2324
"torchmetrics >1, <2",
2425
"transformers >=4.18, <5",
25-
# required for metrics: f1, confusion_matrix, and statsistics
26-
"pandas >=2.0.0, <3",
2726
]
2827

2928
[project.urls]
@@ -44,6 +43,10 @@ classifiers = [
4443
"License :: OSI Approved :: MIT License"
4544
]
4645

46+
[tool.poetry.urls]
47+
"Bug Tracker" = "https://github.com/christophalt/pytorch-ie/issues"
48+
"Changelog" = "https://github.com/christophalt/pytorch-ie/releases"
49+
4750
[tool.poetry.group.dev]
4851
optional = true
4952

src/pytorch_ie/annotations.py

Lines changed: 13 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,13 @@
1-
from dataclasses import dataclass, field
2-
from typing import Any, Optional, Tuple
3-
4-
from pie_core import Annotation
5-
6-
7-
def _post_init_single_label(self):
8-
if not isinstance(self.label, str):
9-
raise ValueError("label must be a single string.")
10-
11-
if not isinstance(self.score, float):
12-
raise ValueError("score must be a single float.")
13-
14-
15-
def _post_init_multi_label(self):
16-
if self.score is None:
17-
score = tuple([1.0] * len(self.label))
18-
object.__setattr__(self, "score", score)
19-
20-
if not isinstance(self.label, tuple):
21-
object.__setattr__(self, "label", tuple(self.label))
22-
23-
if not isinstance(self.score, tuple):
24-
object.__setattr__(self, "score", tuple(self.score))
25-
26-
if len(self.label) != len(self.score):
27-
raise ValueError(
28-
f"Number of labels ({len(self.label)}) and scores ({len(self.score)}) must be equal."
29-
)
30-
31-
32-
def _post_init_multi_span(self):
33-
if isinstance(self.slices, list):
34-
object.__setattr__(self, "slices", tuple(tuple(s) for s in self.slices))
35-
36-
37-
def _post_init_arguments_and_roles(self):
38-
if len(self.arguments) != len(self.roles):
39-
raise ValueError(
40-
f"Number of arguments ({len(self.arguments)}) and roles ({len(self.roles)}) must be equal"
41-
)
42-
if not isinstance(self.arguments, tuple):
43-
object.__setattr__(self, "arguments", tuple(self.arguments))
44-
if not isinstance(self.roles, tuple):
45-
object.__setattr__(self, "roles", tuple(self.roles))
46-
47-
48-
@dataclass(eq=True, frozen=True)
49-
class Label(Annotation):
50-
label: str
51-
score: float = field(default=1.0, compare=False)
52-
53-
def __post_init__(self) -> None:
54-
_post_init_single_label(self)
55-
56-
def resolve(self) -> Any:
57-
return self.label
58-
59-
60-
@dataclass(eq=True, frozen=True)
61-
class MultiLabel(Annotation):
62-
label: Tuple[str, ...]
63-
score: Optional[Tuple[float, ...]] = field(default=None, compare=False)
64-
65-
def __post_init__(self) -> None:
66-
_post_init_multi_label(self)
67-
68-
def resolve(self) -> Any:
69-
return self.label
70-
71-
72-
@dataclass(eq=True, frozen=True)
73-
class Span(Annotation):
74-
start: int
75-
end: int
76-
77-
def __str__(self) -> str:
78-
if not self.is_attached:
79-
return super().__str__()
80-
return str(self.target[self.start : self.end])
81-
82-
def resolve(self) -> Any:
83-
if self.is_attached:
84-
return self.target[self.start : self.end]
85-
else:
86-
raise ValueError(f"{self} is not attached to a target.")
87-
88-
89-
@dataclass(eq=True, frozen=True)
90-
class LabeledSpan(Span):
91-
label: str
92-
score: float = field(default=1.0, compare=False)
93-
94-
def __post_init__(self) -> None:
95-
_post_init_single_label(self)
96-
97-
def resolve(self) -> Any:
98-
return self.label, super().resolve()
99-
100-
101-
@dataclass(eq=True, frozen=True)
102-
class MultiLabeledSpan(Span):
103-
label: Tuple[str, ...]
104-
score: Optional[Tuple[float, ...]] = field(default=None, compare=False)
105-
106-
def __post_init__(self) -> None:
107-
_post_init_multi_label(self)
108-
109-
def resolve(self) -> Any:
110-
return self.label, super().resolve()
111-
112-
113-
@dataclass(eq=True, frozen=True)
114-
class MultiSpan(Annotation):
115-
slices: Tuple[Tuple[int, int], ...]
116-
117-
def __post_init__(self) -> None:
118-
_post_init_multi_span(self)
119-
120-
def __str__(self) -> str:
121-
if not self.is_attached:
122-
return super().__str__()
123-
return str(tuple(self.target[start:end] for start, end in self.slices))
124-
125-
def resolve(self) -> Any:
126-
if self.is_attached:
127-
return tuple(self.target[start:end] for start, end in self.slices)
128-
else:
129-
raise ValueError(f"{self} is not attached to a target.")
130-
131-
132-
@dataclass(eq=True, frozen=True)
133-
class LabeledMultiSpan(MultiSpan):
134-
label: str
135-
score: float = field(default=1.0, compare=False)
136-
137-
def __post_init__(self) -> None:
138-
super().__post_init__()
139-
_post_init_single_label(self)
140-
141-
def resolve(self) -> Any:
142-
return self.label, super().resolve()
143-
144-
145-
@dataclass(eq=True, frozen=True)
146-
class BinaryRelation(Annotation):
147-
head: Annotation
148-
tail: Annotation
149-
label: str
150-
score: float = field(default=1.0, compare=False)
151-
152-
def __post_init__(self) -> None:
153-
_post_init_single_label(self)
154-
155-
def resolve(self) -> Any:
156-
return self.label, (self.head.resolve(), self.tail.resolve())
157-
158-
159-
@dataclass(eq=True, frozen=True)
160-
class MultiLabeledBinaryRelation(Annotation):
161-
head: Annotation
162-
tail: Annotation
163-
label: Tuple[str, ...]
164-
score: Optional[Tuple[float, ...]] = field(default=None, compare=False)
165-
166-
def __post_init__(self) -> None:
167-
_post_init_multi_label(self)
168-
169-
def resolve(self) -> Any:
170-
return self.label, (self.head.resolve(), self.tail.resolve())
171-
172-
173-
@dataclass(eq=True, frozen=True)
174-
class NaryRelation(Annotation):
175-
arguments: Tuple[Annotation, ...]
176-
roles: Tuple[str, ...]
177-
label: str
178-
score: float = field(default=1.0, compare=False)
179-
180-
def __post_init__(self) -> None:
181-
_post_init_arguments_and_roles(self)
182-
_post_init_single_label(self)
183-
184-
def resolve(self) -> Any:
185-
return (
186-
self.label,
187-
tuple((role, arg.resolve()) for arg, role in zip(self.arguments, self.roles)),
188-
)
1+
# backward compatibility
2+
from pie_documents.annotations import (
3+
BinaryRelation,
4+
Label,
5+
LabeledMultiSpan,
6+
LabeledSpan,
7+
MultiLabel,
8+
MultiLabeledBinaryRelation,
9+
MultiLabeledSpan,
10+
MultiSpan,
11+
NaryRelation,
12+
Span,
13+
)

0 commit comments

Comments
 (0)