forked from asyml/forte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_pack.py
670 lines (544 loc) · 21.1 KB
/
base_pack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# Copyright 2019 The Forte Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import gzip
import pickle
import uuid
from abc import abstractmethod
from pathlib import Path
from typing import (
List,
Optional,
Set,
Type,
TypeVar,
Union,
Iterator,
Dict,
Tuple,
Any,
Iterable,
)
import jsonpickle
from forte.common import ProcessExecutionException, EntryNotFoundError
from forte.data.container import EntryContainer
from forte.data.index import BaseIndex
from forte.data.ontology.core import Entry, EntryType, GroupType, LinkType
__all__ = ["BasePack", "BaseMeta", "PackType"]
class BaseMeta:
r"""Basic Meta information for both :class:`~forte.data.data_pack.DataPack`
and :class:`~forte.data.multi_pack.MultiPack`.
Args:
pack_name: An name to identify the data pack, which is helpful in
situation like serialization. It is suggested that the packs should
have different doc ids.
Attributes:
record: Initialized as a dictionary. This is not a required field.
The key of the record should be the entry type and values should
be attributes of the entry type. All the information would be used
for consistency checking purpose if the pipeline is initialized with
`enforce_consistency=True`.
"""
def __init__(self, pack_name: Optional[str] = None):
self.pack_name: Optional[str] = pack_name
self._pack_id: int = uuid.uuid4().int
self.record: Dict[str, Set[str]] = {}
def __getstate__(self):
state = self.__dict__.copy()
return state
def __setstate__(self, state):
"""
Re-obtain the pack manager during deserialization.
Args:
state:
Returns:
"""
self.__dict__.update(state)
@property
def pack_id(self) -> int:
return self._pack_id
class BasePack(EntryContainer[EntryType, LinkType, GroupType]):
r"""The base class of :class:`~forte.data.data_pack.DataPack` and
:class:`~forte.data.multi_pack.MultiPack`.
Args:
pack_name (str, optional): a string name of the pack.
"""
# pylint: disable=too-many-public-methods
def __init__(self, pack_name: Optional[str] = None):
super().__init__()
self.links: List[LinkType] = []
self.groups: List[GroupType] = []
self._meta: BaseMeta = self._init_meta(pack_name)
self._index: BaseIndex = BaseIndex()
self.__control_component: Optional[str] = None
# This Dict maintains a mapping from entry's tid to the Entry object
# itself and the component name associated with the entry.
# The component name is used for tracking the "creator" of this entry.
self._pending_entries: Dict[int, Tuple[Entry, Optional[str]]] = {}
def __getstate__(self):
state = self.__dict__.copy()
state.pop("_index")
state.pop("_pending_entries")
state.pop("_BasePack__control_component")
return state
def __setstate__(self, state):
super().__setstate__(state)
if "meta" in self.__dict__:
self._meta = self.__dict__.pop("meta")
self.__control_component = None
self._pending_entries = {}
@abstractmethod
def _init_meta(self, pack_name: Optional[str] = None) -> BaseMeta:
raise NotImplementedError
def set_meta(self, **kwargs):
for k, v in kwargs.items():
if not hasattr(self._meta, k):
raise AttributeError(f"Meta has no attribute named {k}")
setattr(self._meta, k, v)
@property
def pack_id(self):
return self._meta.pack_id
@abstractmethod
def __iter__(self) -> Iterator[EntryType]:
raise NotImplementedError
def __del__(self):
if len(self._pending_entries) > 0:
raise ProcessExecutionException(
f"There are {len(self._pending_entries)} "
f"entries not added to the index correctly."
)
@property
def pack_name(self):
return self._meta.pack_name
@pack_name.setter
def pack_name(self, pack_name: str):
"""
Update the pack name of this pack.
Args:
pack_name: The new doc id.
Returns:
"""
self._meta.pack_name = pack_name
@classmethod
def _deserialize(
cls,
data_source: Union[Path, str],
serialize_method: str = "jsonpickle",
zip_pack: bool = False,
) -> "PackType":
"""
This function should deserialize a Pack from a string. The
implementation should decide the specific pack type.
Args:
data_source: The data path containing pack data. The content
of the data could be string or bytes depending on the method of
serialization.
serialize_method: The method used to serialize the data, this
should be the same as how serialization is done. The current
options are "jsonpickle" and "pickle". The default method
is "jsonpickle".
zip_pack: Boolean value indicating whether the input source is
zipped.
Returns:
An pack object deserialized from the data.
"""
_open = gzip.open if zip_pack else open
if serialize_method == "jsonpickle":
with _open(data_source, mode="rt") as f: # type: ignore
pack = cls.from_string(f.read())
else:
with _open(data_source, mode="rb") as f: # type: ignore
pack = pickle.load(f)
return pack # type: ignore
@classmethod
def from_string(cls, data_content: str) -> "BasePack":
return jsonpickle.decode(data_content)
@abstractmethod
def delete_entry(self, entry: EntryType):
r"""Remove the entry from the pack.
Args:
entry: The entry to be removed.
Returns:
"""
raise NotImplementedError
def add_entry(
self, entry: Entry, component_name: Optional[str] = None
) -> EntryType:
r"""Add an :class:`~forte.data.ontology.core.Entry` object to the
:class:`BasePack` object. Allow duplicate entries in a pack.
Args:
entry (Entry): An :class:`~forte.data.ontology.core.Entry`
object to be added to the pack.
component_name (str): A name to record that the entry is created by
this component.
Returns:
The input entry itself
"""
# When added to the pack, make a record.
self.record_entry(entry, component_name)
# TODO: Returning the entry itself may not be helpful.
return self._add_entry(entry)
@abstractmethod
def _add_entry(self, entry: Entry) -> EntryType:
r"""Add an :class:`~forte.data.ontology.core.Entry` object to the
:class:`BasePack` object. Allow duplicate entries in a pack.
Args:
entry (Entry): An :class:`~forte.data.ontology.core.Entry`
object to be added to the pack.
Returns:
The input entry itself
"""
raise NotImplementedError
def add_all_remaining_entries(self, component: Optional[str] = None):
"""
Calling this function will add the entries that are not added to the
pack manually.
Args:
component (str): Overwrite the component record with this.
Returns:
"""
for entry, c in list(self._pending_entries.values()):
c_ = component if component else c
self.add_entry(entry, c_)
self._pending_entries.clear()
def to_string(
self,
drop_record: Optional[bool] = False,
json_method: str = "jsonpickle",
indent: Optional[int] = None,
) -> str:
"""
Return the string representation (json encoded) of this method.
Args:
drop_record: Whether to drop the creation records, default is False.
json_method: What method is used to convert data pack to json.
Only supports `json_pickle` for now. Default value is
`json_pickle`.
indent: The indent used for json string.
Returns: String representation of the data pack.
"""
if drop_record:
self._creation_records.clear()
self._field_records.clear()
if json_method == "jsonpickle":
return jsonpickle.encode(self, unpicklable=True, indent=indent)
else:
raise ValueError(f"Unsupported JSON method {json_method}.")
def serialize(
self,
output_path: Union[str, Path],
zip_pack: bool = False,
drop_record: bool = False,
serialize_method: str = "jsonpickle",
indent: Optional[int] = None,
):
r"""
Serializes the data pack to the provided path. The output of this
function depends on the serialization method chosen.
Args:
output_path: The path to write data to.
zip_pack: Whether to compress the result with `gzip`.
drop_record: Whether to drop the creation records, default is False.
serialize_method: The method used to serialize the data. Currently
supports "jsonpickle" (outputs str) and Python's built-in
"pickle" (outputs bytes).
indent: Whether to indent the file if written as JSON.
Returns: Results of serialization.
"""
if zip_pack:
_open = gzip.open
else:
_open = open # type:ignore
if drop_record:
self._creation_records.clear()
self._field_records.clear()
if serialize_method == "pickle":
with _open(output_path, mode="wb") as pickle_out:
pickle.dump(self, pickle_out) # type:ignore
elif serialize_method == "jsonpickle":
with _open(output_path, mode="wt", encoding="utf-8") as json_out:
json_out.write(
self.to_string(drop_record, "jsonpickle", indent=indent)
)
else:
raise NotImplementedError(
f"Unsupported serialization method {serialize_method}"
)
def view(self):
return copy.deepcopy(self)
def set_control_component(self, component: str):
"""
Record the current component that is taking control of this pack.
Args:
component: The component that is going to take control
Returns:
"""
self.__control_component = component
def record_entry(self, entry: Entry, component_name: Optional[str] = None):
c = component_name
if c is None:
# Use the auto-inferred control component.
c = self.__control_component
if c is not None:
try:
self._creation_records[c].add(entry.tid)
except KeyError:
self._creation_records[c] = {entry.tid}
def record_field(self, entry_id: int, field_name: str):
"""
Record who modifies the entry, will be called
in :class:`~forte.data.ontology.core.Entry`
Args:
entry_id: The id of the entry.
field_name: The name of the field modified.
Returns:
"""
c = self.__control_component
if c is not None:
try:
self._field_records[c].add((entry_id, field_name))
except KeyError:
self._field_records[c] = {(entry_id, field_name)}
def on_entry_creation(
self, entry: Entry, component_name: Optional[str] = None
):
"""
Call this when adding a new entry, will be called
in :class:`~forte.data.ontology.core.Entry` when
its `__init__` function is called.
Args:
entry (Entry): The entry to be added.
component_name (str): A name to record that the entry is created by
this component.
Returns:
"""
c = component_name
if c is None:
# Use the auto-inferred control component.
c = self.__control_component
# Record that this entry hasn't been added to the index yet.
self._pending_entries[entry.tid] = entry, c
def regret_creation(self, entry: EntryType):
"""
Will remove the entry from the pending entries internal state of the
pack.
Args:
entry: The entry that we would not add the the pack anymore.
Returns:
"""
self._pending_entries.pop(entry.tid)
# TODO: how to make this return the precise type here?
def get_entry(self, tid: int) -> EntryType:
r"""Look up the entry_index with key ``ptr``. Specific implementation
depends on the actual class."""
entry: EntryType = self._index.get_entry(tid)
if entry is None:
raise KeyError(
f"There is no entry with tid '{tid}'' in this datapack"
)
return entry
@abstractmethod
def get_data(
self, context_type, request, skip_k
) -> Iterator[Dict[str, Any]]:
raise NotImplementedError
@abstractmethod
def get(
self, entry_type: Union[str, Type[EntryType]], **kwargs
) -> Iterator[EntryType]:
"""
Implementation of this method should provide to obtain the entries in
entry ordering. If there are orders defined between the entries, they
should be used first. Otherwise, the insertion order should be
used (FIFO).
Args:
entry_type: The type of the entry to obtain.
Returns:
An iterator of the entries matching the provided arguments.
"""
raise NotImplementedError
def get_single(self, entry_type: Union[str, Type[EntryType]]) -> EntryType:
r"""Take a single entry of type :attr:`entry_type` from this data
pack. This is useful when the target entry type appears only one
time in the :class:`DataPack` for e.g., a Document entry. Or you just
intended to take the first one.
Args:
entry_type: The entry type to be retrieved.
Returns:
A single data entry.
"""
for a in self.get(entry_type):
return a
raise EntryNotFoundError(
f"The entry {entry_type} is not found in the provided pack."
)
def get_ids_by_creator(self, component: str) -> Set[int]:
r"""
Look up the component_index with key `component`. This will return
the entry ids that are created by the `component`
Args:
component: The component (creator) to find ids for.
Returns:
A set of entry ids that are created by the component.
"""
entry_set: Set[int] = self._creation_records[component]
return entry_set
def is_created_by(
self, entry: Entry, components: Union[str, Iterable[str]]
) -> bool:
"""
Check if the entry is created by any of the provided components.
Args:
entry: The entry to check.
components: The list of component names.
Returns (bool):
True if the entry is created by the component, False otherwise.
"""
if isinstance(components, str):
components = [components]
for c in components:
if entry.tid in self._creation_records[c]:
break
else:
# The entry not created by any of these components.
return False
return True
def get_entries_from(self, component: str) -> Set[EntryType]:
"""
Look up all entries from the `component` as a unordered set
Args:
component: The component (creator) to get the entries. It is
normally the full qualified name of the creator class, but it
may also be customized based on the implementation.
Returns:
The set of entry ids that are created by the input component.
"""
return {
self.get_entry(tid) for tid in self.get_ids_by_creator(component)
}
def get_ids_from(self, components: List[str]) -> Set[int]:
"""
Look up entries using a list of components (creators). This will find
each creator iteratively and combine the result.
Args:
components (List[str]): The list of components to find.
Returns:
The list of entry ids that are created from these components.
"""
valid_component_id: Set[int] = set()
for component in components:
valid_component_id |= self.get_ids_by_creator(component)
return valid_component_id
def _expand_to_sub_types(self, entry_type: Type[EntryType]) -> Set[Type]:
"""
Return all the types and the sub types that inherit from the provided
type.
Args:
entry_type: The provided type to search for entry.
Returns:
A set of all the sub-types extending the provided type, including
the input `entry_type` itself.
"""
all_types: Set[Type] = set()
for data_type in self._index.indexed_types():
if issubclass(data_type, entry_type):
all_types.add(data_type)
return all_types
def get_entries_of(
self, entry_type: Type[EntryType], exclude_sub_types=False
) -> Iterator[EntryType]:
"""
Return all entries of this particular type without orders. If you
need to get the annotations based on the entry ordering,
use :meth:`forte.data.base_pack.get`.
Args:
entry_type: The type of the entry you are looking for.
exclude_sub_types (bool): Whether to ignore the inherited sub type
of the provided `entry_type`. Default is True.
Returns:
An iterator of the entries matching the type constraint.
"""
if exclude_sub_types:
for tid in self._index.query_by_type(entry_type):
yield self.get_entry(tid)
else:
for tid in self._index.query_by_type_subtype(entry_type):
yield self.get_entry(tid)
@classmethod
@abstractmethod
def validate_link(cls, entry: EntryType) -> bool:
raise NotImplementedError
@classmethod
@abstractmethod
def validate_group(cls, entry: EntryType) -> bool:
raise NotImplementedError
def get_links_from_node(
self, node: Union[int, EntryType], as_parent: bool
) -> List[LinkType]:
links: List[LinkType] = []
if isinstance(node, Entry):
tid = node.tid
if tid is None:
raise ValueError(
"The requested node has no tid. "
"Have you add this entry into the datapack?"
)
elif isinstance(node, int):
tid = node
else:
raise TypeError(
"Can only get group via entry id (int) or the "
"group object itself (Entry)."
)
if not self._index.link_index_on:
self._index.build_link_index(self.links)
for tid in self._index.link_index(tid, as_parent=as_parent):
entry: EntryType = self.get_entry(tid)
if self.validate_link(entry):
links.append(entry) # type: ignore
return links
def get_links_by_parent(
self, parent: Union[int, EntryType]
) -> List[LinkType]:
return self.get_links_from_node(parent, True)
def get_links_by_child(
self, child: Union[int, EntryType]
) -> List[LinkType]:
return self.get_links_from_node(child, False)
def get_groups_by_member(
self, member: Union[int, EntryType]
) -> Set[GroupType]:
groups: Set[GroupType] = set()
if isinstance(member, Entry):
tid = member.tid
if tid is None:
raise ValueError(
"Argument member has no tid. "
"Have you add this entry into the datapack?"
)
elif isinstance(member, int):
tid = member
else:
raise TypeError(
"Can only get group via entry id (int) or the "
"group object itself (Entry)."
)
if not self._index.group_index_on:
self._index.build_group_index(self.groups)
for tid in self._index.group_index(tid):
entry: EntryType = self.get_entry(tid)
if self.validate_group(entry):
groups.add(entry) # type: ignore
return groups
PackType = TypeVar("PackType", bound=BasePack)