dd-core 1.2.6__tar.gz → 1.2.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. {dd_core-1.2.6 → dd_core-1.2.7}/PKG-INFO +1 -1
  2. {dd_core-1.2.6 → dd_core-1.2.7}/pyproject.toml +1 -1
  3. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/__init__.py +1 -1
  4. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/datapoint/__init__.py +1 -1
  5. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/datapoint/image.py +98 -21
  6. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/doc.py +34 -13
  7. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core.egg-info/PKG-INFO +1 -1
  8. {dd_core-1.2.6 → dd_core-1.2.7}/README.md +0 -0
  9. {dd_core-1.2.6 → dd_core-1.2.7}/setup.cfg +0 -0
  10. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/__init__.py +0 -0
  11. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/base.py +0 -0
  12. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/common.py +0 -0
  13. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/custom.py +0 -0
  14. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/custom_serialize.py +0 -0
  15. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/parallel_map.py +0 -0
  16. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/serialize.py +0 -0
  17. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/dataflow/stats.py +0 -0
  18. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/datapoint/annotation.py +0 -0
  19. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/datapoint/box.py +0 -0
  20. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/datapoint/convert.py +0 -0
  21. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/datapoint/view.py +0 -0
  22. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/__init__.py +0 -0
  23. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/cats.py +0 -0
  24. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/cocostruct.py +0 -0
  25. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/d2struct.py +0 -0
  26. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/hfstruct.py +0 -0
  27. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/laylmstruct.py +0 -0
  28. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/maputils.py +0 -0
  29. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/match.py +0 -0
  30. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/misc.py +0 -0
  31. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/nms.py +0 -0
  32. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/pascalstruct.py +0 -0
  33. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/prodigystruct.py +0 -0
  34. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/pubstruct.py +0 -0
  35. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/wandbstruct.py +0 -0
  36. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/mapper/xfundstruct.py +0 -0
  37. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/py.typed +0 -0
  38. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/__init__.py +0 -0
  39. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/concurrency.py +0 -0
  40. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/context.py +0 -0
  41. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/develop.py +0 -0
  42. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/env_info.py +0 -0
  43. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/error.py +0 -0
  44. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/file_utils.py +0 -0
  45. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/fs.py +0 -0
  46. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/identifier.py +0 -0
  47. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/logger.py +0 -0
  48. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/metacfg.py +0 -0
  49. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/object_types.py +0 -0
  50. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/pdf_utils.py +0 -0
  51. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/ptutils.py +0 -0
  52. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/tqdm.py +0 -0
  53. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/transform.py +0 -0
  54. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/types.py +0 -0
  55. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/utils.py +0 -0
  56. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core/utils/viz.py +0 -0
  57. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core.egg-info/SOURCES.txt +0 -0
  58. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core.egg-info/dependency_links.txt +0 -0
  59. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core.egg-info/requires.txt +0 -0
  60. {dd_core-1.2.6 → dd_core-1.2.7}/src/dd_core.egg-info/top_level.txt +0 -0
  61. {dd_core-1.2.6 → dd_core-1.2.7}/tests/test_doc.py +0 -0
  62. {dd_core-1.2.6 → dd_core-1.2.7}/tests/test_session_id_removal_integration.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dd-core
3
- Version: 1.2.6
3
+ Version: 1.2.7
4
4
  Summary: Core data structures, dataflows, mappers and utilities for deepdoctection
5
5
  Author: Dr. Janis Meyer
6
6
  License: Apache License 2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "dd-core"
7
- version = "1.2.6"
7
+ version = "1.2.7"
8
8
  authors = [
9
9
  {name = "Dr. Janis Meyer"}
10
10
  ]
@@ -32,7 +32,7 @@ from typing import TYPE_CHECKING
32
32
 
33
33
  from .utils.file_utils import _LazyModule
34
34
 
35
- __version__ = "1.2.6"
35
+ __version__ = "1.2.7"
36
36
  _IMPORT_STRUCTURE = {
37
37
  "dataflow": [
38
38
  "DataFlowReentrantGuard",
@@ -34,5 +34,5 @@ After all, the point here is not to provide an optimal processing environment.
34
34
  from .annotation import *
35
35
  from .box import *
36
36
  from .convert import *
37
- from .image import Image, ImageFormats, MetaAnnotation
37
+ from .image import Extras, Image, ImageFormats, MetaAnnotation
38
38
  from .view import *
@@ -25,7 +25,7 @@ from collections import defaultdict
25
25
  from dataclasses import dataclass, field
26
26
  from os import environ, fspath
27
27
  from pathlib import Path
28
- from typing import Any, Callable, Optional, Sequence, TypedDict, Union
28
+ from typing import Any, Callable, Literal, Optional, Sequence, TypedDict, Union
29
29
 
30
30
  import numpy as np
31
31
  from lazy_imports import try_import
@@ -183,6 +183,55 @@ class ImageFormats:
183
183
  self._torch_by_device.clear()
184
184
 
185
185
 
186
+ class Extras:
187
+ """Typed transient key-value store for ``Image`` and ``Document``. Never serialized."""
188
+
189
+ def __init__(self) -> None:
190
+ self._schema: dict[str, Literal["str", "list[str]"]] = {}
191
+ self._data: dict[str, Union[str, list[str]]] = {}
192
+
193
+ def set_type(self, key: str, value_type: Literal["str", "list[str]"]) -> None:
194
+ """Register *key* as ``"str"`` (replace on dump) or ``"list[str]"`` (append on dump)."""
195
+ if value_type not in ("str", "list[str]"):
196
+ raise ValueError(f"value_type must be 'str' or 'list[str]', got {value_type!r}")
197
+ if key in self._schema:
198
+ if self._schema[key] != value_type:
199
+ raise ValueError(f"Key {key!r} already registered as {self._schema[key]!r}")
200
+ return
201
+ self._schema[key] = value_type
202
+ if value_type == "list[str]":
203
+ self._data[key] = []
204
+
205
+ def dump(self, key: str, value: str) -> None:
206
+ """Assign (``"str"``) or append (``"list[str]"``) *value* for *key*."""
207
+ if key not in self._schema:
208
+ raise KeyError(f"Key {key!r} not configured. Call configure_extras first.")
209
+ if not isinstance(value, str):
210
+ raise TypeError(f"value must be str, got {type(value).__name__!r}")
211
+ if self._schema[key] == "str":
212
+ self._data[key] = value
213
+ else:
214
+ lst = self._data.setdefault(key, [])
215
+ lst.append(value) # type: ignore[union-attr]
216
+
217
+ def as_dict(self) -> dict[str, Any]:
218
+ """Return a JSON-serializable dict that fully represents this ``Extras`` instance."""
219
+ return {"_schema": dict(self._schema), "_data": dict(self._data)}
220
+
221
+ @classmethod
222
+ def from_dict(cls, data: dict[str, Any]) -> Extras:
223
+ """Reconstruct an ``Extras`` instance from a dict produced by :meth:`as_dict`."""
224
+ obj = cls()
225
+ obj._schema = data["_schema"]
226
+ obj._data = data["_data"]
227
+ return obj
228
+
229
+ def __eq__(self, other: object) -> bool:
230
+ if not isinstance(other, Extras):
231
+ return NotImplemented
232
+ return self._schema == other._schema and self._data == other._data
233
+
234
+
186
235
  class Image(BaseModel):
187
236
  """
188
237
  The image object is the enclosing data class that is used in the core data model to manage, retrieve or store
@@ -251,18 +300,18 @@ class Image(BaseModel):
251
300
  _summary: Optional[CategoryAnnotation] = PrivateAttr(default=None)
252
301
  _image_id: Optional[str] = PrivateAttr(default=None)
253
302
  _pdf_bytes: Optional[bytes] = PrivateAttr(default=None)
254
- _extras: dict[str, str] = PrivateAttr(default_factory=dict)
303
+ _extras: Extras = PrivateAttr(default_factory=Extras)
255
304
 
256
305
  def __init__(self, **data: Any) -> None:
257
306
  """
258
307
  Accept private attrs in kwargs (e.g. `_image_id`, `_summary`, `_bbox`, `_annotation_ids`, `_image`,
259
- `_pdf_bytes`)
308
+ `_pdf_bytes`, `_extras`)
260
309
  Remove them before BaseModel initialization and set the PrivateAttr values afterwards so that
261
- `Image(**inputs)` works when legacy code passes private keys. `_extras` is silently discarded if
262
- present in kwargs and is never restored.
310
+ `Image(**inputs)` works when legacy code passes private keys. `_extras` is restored when a dict
311
+ produced by ``as_dict(add_extras=True)`` is passed; otherwise a fresh ``Extras`` instance is used.
263
312
  """
264
313
  private_keys = ["_image", "_bbox", "_annotation_ids", "_summary", "_image_id", "_pdf_bytes"]
265
- data.pop("_extras", None)
314
+ extras_raw = data.pop("_extras", None)
266
315
  priv: dict[str, Any] = {}
267
316
  for key in private_keys:
268
317
  if key in data:
@@ -283,6 +332,9 @@ class Image(BaseModel):
283
332
  # assign even if value is None to preserve explicit input
284
333
  object.__setattr__(self, key, val)
285
334
 
335
+ if isinstance(extras_raw, dict):
336
+ object.__setattr__(self, "_extras", Extras.from_dict(extras_raw))
337
+
286
338
  @field_validator("embeddings", mode="before")
287
339
  @classmethod
288
340
  def _coerce_embeddings(cls, v: Any) -> dict[str, BoundingBox]:
@@ -439,27 +491,47 @@ class Image(BaseModel):
439
491
  self._pdf_bytes = pdf_bytes
440
492
 
441
493
  @property
442
- def extras(self) -> dict[str, str]:
494
+ def extras(self) -> Extras:
443
495
  """
444
- Transient key-value store for additional messages or metadata attached to this image.
496
+ Typed transient key-value store for additional messages or metadata attached to this image.
445
497
 
446
- The store is never serialized and is silently ignored when a caller passes `_extras` as a
447
- constructor keyword argument.
498
+ Keys must be registered with :meth:`configure_extras` before values can be stored via
499
+ :meth:`dump_extra`. The store is never serialized.
448
500
 
449
501
  Returns:
450
- The `dict` holding the extra entries for this image.
502
+ The :class:`Extras` instance for this image.
451
503
  """
452
504
  return self._extras
453
505
 
454
- @extras.setter
455
- def extras(self, value: dict[str, str]) -> None:
506
+ def configure_extras(self, key: str, value_type: str) -> None:
456
507
  """
457
- Replace the entire extras store.
508
+ Register *key* in the extras store with the given *value_type*.
509
+
510
+ Must be called before :meth:`dump_extra`. Calling again with the same type
511
+ is idempotent; calling with a different type raises ``ValueError``.
512
+
513
+ Args:
514
+ key: The extras key to register.
515
+ value_type: ``"str"`` for single-value assignment, or
516
+ ``"list[str]"`` for append-mode storage.
517
+ """
518
+ self._extras.set_type(key, value_type) # type: ignore[arg-type]
519
+
520
+ def dump_extra(self, key: str, value: str) -> None:
521
+ """
522
+ Store *value* for *key* in the extras store.
523
+
524
+ * ``"str"`` keys: *value* **replaces** the current entry.
525
+ * ``"list[str]"`` keys: *value* is **appended** to the list.
458
526
 
459
527
  Args:
460
- value: A `dict` instance that will replace the current extras store.
528
+ key: A previously configured extras key.
529
+ value: The string value to store.
530
+
531
+ Raises:
532
+ KeyError: If *key* has not been configured yet via :meth:`configure_extras`.
461
533
  """
462
- object.__setattr__(self, "_extras", value)
534
+ self._extras.dump(key, value)
463
535
 
464
536
  @property
465
537
  def width(self) -> BoxCoordinate:
@@ -701,16 +773,21 @@ class Image(BaseModel):
701
773
 
702
774
  return data
703
775
 
704
- def as_dict(self) -> dict[str, Any]:
776
+ def as_dict(self, add_extras: bool = False) -> dict[str, Any]:
705
777
  """
706
- Returns the full image dataclass as dict. Uses the custom `convert.as_dict` to disregard attributes
707
- defined by `remove_keys`.
778
+ Returns the full image dataclass as dict.
779
+
780
+ Args:
781
+ add_extras: When ``True``, the ``_extras`` store is included under the ``"_extras"`` key.
782
+ Defaults to ``False`` so that normal serialization is unchanged.
708
783
 
709
784
  Returns:
710
785
  A custom `dict`.
711
786
  """
712
- # Uses Pydantic core serializer path
713
- return self.model_dump(by_alias=True, exclude_none=False)
787
+ result = self.model_dump(by_alias=True, exclude_none=False)
788
+ if add_extras:
789
+ result["_extras"] = self._extras.as_dict()
790
+ return result
714
791
 
715
792
  def as_json(self) -> str:
716
793
  """
@@ -46,7 +46,7 @@ from .datapoint.annotation import (
46
46
  ReferencePayload,
47
47
  from_json_compatible,
48
48
  )
49
- from .datapoint.image import Image
49
+ from .datapoint.image import Extras, Image
50
50
  from .datapoint.view import ImageAnnotationBaseView, Page
51
51
  from .mapper.maputils import curry
52
52
  from .utils import get_uuid_from_str
@@ -198,7 +198,7 @@ class Document:
198
198
  _images: dict[str, Image] = field(default_factory=dict, init=False, repr=False)
199
199
  _summary: Optional[CategoryAnnotation] = field(default=None, init=False, repr=False)
200
200
  _pdf_bytes: Optional[bytes] = field(default=None, init=False, repr=False)
201
- _extras: dict[str, str] = field(default_factory=dict, init=False, repr=False)
201
+ _extras: Extras = field(default_factory=Extras, init=False, repr=False)
202
202
 
203
203
  def __post_init__(self) -> None:
204
204
  if self.location is None or self.location == "":
@@ -389,26 +389,47 @@ class Document:
389
389
  self._summary = summary_annotation
390
390
 
391
391
  @property
392
- def extras(self) -> dict[str, str]:
392
+ def extras(self) -> Extras:
393
393
  """
394
- Transient key-value store for additional messages or metadata attached to this document.
394
+ Typed transient key-value store for additional messages or metadata attached to this document.
395
395
 
396
- The store is never serialized and is silently ignored when present in constructor kwargs.
396
+ Keys must be registered with :meth:`configure_extras` before values can be stored via
397
+ :meth:`dump_extra`. The store is never serialized.
397
398
 
398
399
  Returns:
399
- The `dict` holding the extra entries for this document.
400
+ The :class:`Extras` instance for this document.
400
401
  """
401
402
  return self._extras
402
403
 
403
- @extras.setter
404
- def extras(self, value: dict[str, str]) -> None:
404
+ def configure_extras(self, key: str, value_type: str) -> None:
405
405
  """
406
- Replace the entire extras store.
406
+ Register *key* in the extras store with the given *value_type*.
407
+
408
+ Must be called before :meth:`dump_extra`. Calling again with the same type
409
+ is idempotent; calling with a different type raises ``ValueError``.
410
+
411
+ Args:
412
+ key: The extras key to register.
413
+ value_type: ``"str"`` for single-value assignment, or
414
+ ``"list[str]"`` for append-mode storage.
415
+ """
416
+ self._extras.set_type(key, value_type) # type: ignore[arg-type]
417
+
418
+ def dump_extra(self, key: str, value: str) -> None:
419
+ """
420
+ Store *value* for *key* in the extras store.
421
+
422
+ * ``"str"`` keys: *value* **replaces** the current entry.
423
+ * ``"list[str]"`` keys: *value* is **appended** to the list.
407
424
 
408
425
  Args:
409
- value: A `dict` instance that will replace the current extras store.
426
+ key: A previously configured extras key.
427
+ value: The string value to store.
428
+
429
+ Raises:
430
+ KeyError: If *key* has not been configured yet via :meth:`configure_extras`.
410
431
  """
411
- self._extras = value
432
+ self._extras.dump(key, value)
412
433
 
413
434
  def get_image(
414
435
  self,
@@ -703,9 +724,9 @@ class Document:
703
724
  path_json = os.fspath(path) + ".json"
704
725
 
705
726
  if extra_file and not dry:
706
- all_ann_ids: list[str] = list(self._extras.get("extra_file", []))
727
+ all_ann_ids: list[str] = list(self._extras._data.get("extra_file", []))
707
728
  for image in self._images.values():
708
- all_ann_ids.extend(image.extras.get("extra_file", []))
729
+ all_ann_ids.extend(image.extras._data.get("extra_file", []))
709
730
 
710
731
  if all_ann_ids:
711
732
  all_export = self.export_annotations(all_ann_ids)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dd-core
3
- Version: 1.2.6
3
+ Version: 1.2.7
4
4
  Summary: Core data structures, dataflows, mappers and utilities for deepdoctection
5
5
  Author: Dr. Janis Meyer
6
6
  License: Apache License 2.0
File without changes
File without changes
File without changes
File without changes
File without changes