You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

base_data_element.py 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. """
  2. Copyright (c) OpenMMLab. All rights reserved.
  3. Modified from
  4. https://github.com/open-mmlab/mmengine/blob/main/mmengine/structures/base_data_element.py # noqa: E501 pylint: disable=line-too-long
  5. """
  6. import copy
  7. from typing import Any, Iterator, Optional, Tuple, Type, Union
  8. import numpy as np
  9. import torch
  10. class BaseDataElement:
  11. """A base data interface that supports Tensor-like and dict-like
  12. operations.
  13. A typical data elements refer to predicted results or ground truth labels
  14. on a task, such as predicted bboxes, instance masks, semantic
  15. segmentation masks, etc. Because groundtruth labels and predicted results
  16. often have similar properties (for example, the predicted bboxes and the
  17. groundtruth bboxes), MMEngine uses the same abstract data interface to
  18. encapsulate predicted results and groundtruth labels, and it is recommended
  19. to use different name conventions to distinguish them, such as using
  20. ``gt_instances`` and ``pred_instances`` to distinguish between labels and
  21. predicted results. Additionally, we distinguish data elements at instance
  22. level, pixel level, and label level. Each of these types has its own
  23. characteristics. Therefore, MMEngine defines the base class
  24. ``BaseDataElement``, and implement ``InstanceData``, ``PixelData``, and
  25. ``LabelData`` inheriting from ``BaseDataElement`` to represent different
  26. types of ground truth labels or predictions.
  27. Another common data element is data example. A data example consists of input
  28. data (such as an image) and its annotations and predictions. In general,
  29. an image can have multiple types of annotations and/or predictions at the
  30. same time (for example, both pixel-level semantic segmentation annotations
  31. and instance-level detection bboxes annotations). All labels and
  32. predictions of a training example are often passed between Dataset, Model,
  33. Visualizer, and Evaluator components. In order to simplify the interface
  34. between components, we can treat them as a large data element and
  35. encapsulate them. Such data elements are generally called XXDataSample in
  36. the OpenMMLab. Therefore, Similar to `nn.Module`, the `BaseDataElement`
  37. allows `BaseDataElement` as its attribute. Such a class generally
  38. encapsulates all the data of a example in the algorithm library, and its
  39. attributes generally are various types of data elements. For example,
  40. MMDetection is assigned by the BaseDataElement to encapsulate all the data
  41. elements of the example labeling and prediction of a example in the
  42. algorithm library.
  43. The attributes in ``BaseDataElement`` are divided into two parts,
  44. the ``metainfo`` and the ``data`` respectively.
  45. - ``metainfo``: Usually contains the
  46. information about the image such as filename,
  47. image_shape, pad_shape, etc. The attributes can be accessed or
  48. modified by dict-like or object-like operations, such as
  49. ``.`` (for data access and modification), ``in``, ``del``,
  50. ``pop(str)``, ``get(str)``, ``metainfo_keys()``,
  51. ``metainfo_values()``, ``metainfo_items()``, ``set_metainfo()`` (for
  52. set or change key-value pairs in metainfo).
  53. - ``data``: Annotations or model predictions are
  54. stored. The attributes can be accessed or modified by
  55. dict-like or object-like operations, such as
  56. ``.``, ``in``, ``del``, ``pop(str)``, ``get(str)``, ``keys()``,
  57. ``values()``, ``items()``. Users can also apply tensor-like
  58. methods to all :obj:`torch.Tensor` in the ``data_fields``,
  59. such as ``.cuda()``, ``.cpu()``, ``.numpy()``, ``.to()``,
  60. ``to_tensor()``, ``.detach()``.
  61. Args:
  62. metainfo (dict, optional): A dict contains the meta information
  63. of single image, such as ``dict(img_shape=(512, 512, 3),
  64. scale_factor=(1, 1, 1, 1))``. Defaults to None.
  65. kwargs (dict, optional): A dict contains annotations of single image or
  66. model predictions. Defaults to None.
  67. Examples:
  68. >>> import torch
  69. >>> from mmengine.structures import BaseDataElement
  70. >>> gt_instances = BaseDataElement()
  71. >>> bboxes = torch.rand((5, 4))
  72. >>> scores = torch.rand((5,))
  73. >>> img_id = 0
  74. >>> img_shape = (800, 1333)
  75. >>> gt_instances = BaseDataElement(
  76. ... metainfo=dict(img_id=img_id, img_shape=img_shape),
  77. ... bboxes=bboxes, scores=scores)
  78. >>> gt_instances = BaseDataElement(
  79. ... metainfo=dict(img_id=img_id, img_shape=(640, 640)))
  80. >>> # new
  81. >>> gt_instances1 = gt_instances.new(
  82. ... metainfo=dict(img_id=1, img_shape=(640, 640)),
  83. ... bboxes=torch.rand((5, 4)),
  84. ... scores=torch.rand((5,)))
  85. >>> gt_instances2 = gt_instances1.new()
  86. >>> # add and process property
  87. >>> gt_instances = BaseDataElement()
  88. >>> gt_instances.set_metainfo(dict(img_id=9, img_shape=(100, 100)))
  89. >>> assert 'img_shape' in gt_instances.metainfo_keys()
  90. >>> assert 'img_shape' in gt_instances
  91. >>> assert 'img_shape' not in gt_instances.keys()
  92. >>> assert 'img_shape' in gt_instances.all_keys()
  93. >>> print(gt_instances.img_shape)
  94. (100, 100)
  95. >>> gt_instances.scores = torch.rand((5,))
  96. >>> assert 'scores' in gt_instances.keys()
  97. >>> assert 'scores' in gt_instances
  98. >>> assert 'scores' in gt_instances.all_keys()
  99. >>> assert 'scores' not in gt_instances.metainfo_keys()
  100. >>> print(gt_instances.scores)
  101. tensor([0.5230, 0.7885, 0.2426, 0.3911, 0.4876])
  102. >>> gt_instances.bboxes = torch.rand((5, 4))
  103. >>> assert 'bboxes' in gt_instances.keys()
  104. >>> assert 'bboxes' in gt_instances
  105. >>> assert 'bboxes' in gt_instances.all_keys()
  106. >>> assert 'bboxes' not in gt_instances.metainfo_keys()
  107. >>> print(gt_instances.bboxes)
  108. tensor([[0.0900, 0.0424, 0.1755, 0.4469],
  109. [0.8648, 0.0592, 0.3484, 0.0913],
  110. [0.5808, 0.1909, 0.6165, 0.7088],
  111. [0.5490, 0.4209, 0.9416, 0.2374],
  112. [0.3652, 0.1218, 0.8805, 0.7523]])
  113. >>> # delete and change property
  114. >>> gt_instances = BaseDataElement(
  115. ... metainfo=dict(img_id=0, img_shape=(640, 640)),
  116. ... bboxes=torch.rand((6, 4)), scores=torch.rand((6,)))
  117. >>> gt_instances.set_metainfo(dict(img_shape=(1280, 1280)))
  118. >>> gt_instances.img_shape # (1280, 1280)
  119. >>> gt_instances.bboxes = gt_instances.bboxes * 2
  120. >>> gt_instances.get('img_shape', None) # (1280, 1280)
  121. >>> gt_instances.get('bboxes', None) # 6x4 tensor
  122. >>> del gt_instances.img_shape
  123. >>> del gt_instances.bboxes
  124. >>> assert 'img_shape' not in gt_instances
  125. >>> assert 'bboxes' not in gt_instances
  126. >>> gt_instances.pop('img_shape', None) # None
  127. >>> gt_instances.pop('bboxes', None) # None
  128. >>> # Tensor-like
  129. >>> cuda_instances = gt_instances.cuda()
  130. >>> cuda_instances = gt_instances.to('cuda:0')
  131. >>> cpu_instances = cuda_instances.cpu()
  132. >>> cpu_instances = cuda_instances.to('cpu')
  133. >>> fp16_instances = cuda_instances.to(
  134. ... device=None, dtype=torch.float16, non_blocking=False,
  135. ... copy=False, memory_format=torch.preserve_format)
  136. >>> cpu_instances = cuda_instances.detach()
  137. >>> np_instances = cpu_instances.numpy()
  138. >>> # print
  139. >>> metainfo = dict(img_shape=(800, 1196, 3))
  140. >>> gt_instances = BaseDataElement(
  141. ... metainfo=metainfo, det_labels=torch.LongTensor([0, 1, 2, 3]))
  142. >>> example = BaseDataElement(metainfo=metainfo,
  143. ... gt_instances=gt_instances)
  144. >>> print(example)
  145. <BaseDataElement(
  146. META INFORMATION
  147. img_shape: (800, 1196, 3)
  148. DATA FIELDS
  149. gt_instances: <BaseDataElement(
  150. META INFORMATION
  151. img_shape: (800, 1196, 3)
  152. DATA FIELDS
  153. det_labels: tensor([0, 1, 2, 3])
  154. ) at 0x7f0ec5eadc70>
  155. ) at 0x7f0fea49e130>
  156. >>> # inheritance
  157. >>> class DetDataSample(BaseDataElement):
  158. ... @property
  159. ... def proposals(self):
  160. ... return self._proposals
  161. ... @proposals.setter
  162. ... def proposals(self, value):
  163. ... self.set_field(value, '_proposals', dtype=BaseDataElement)
  164. ... @proposals.deleter
  165. ... def proposals(self):
  166. ... del self._proposals
  167. ... @property
  168. ... def gt_instances(self):
  169. ... return self._gt_instances
  170. ... @gt_instances.setter
  171. ... def gt_instances(self, value):
  172. ... self.set_field(value, '_gt_instances',
  173. ... dtype=BaseDataElement)
  174. ... @gt_instances.deleter
  175. ... def gt_instances(self):
  176. ... del self._gt_instances
  177. ... @property
  178. ... def pred_instances(self):
  179. ... return self._pred_instances
  180. ... @pred_instances.setter
  181. ... def pred_instances(self, value):
  182. ... self.set_field(value, '_pred_instances',
  183. ... dtype=BaseDataElement)
  184. ... @pred_instances.deleter
  185. ... def pred_instances(self):
  186. ... del self._pred_instances
  187. >>> det_example = DetDataSample()
  188. >>> proposals = BaseDataElement(bboxes=torch.rand((5, 4)))
  189. >>> det_example.proposals = proposals
  190. >>> assert 'proposals' in det_example
  191. >>> assert det_example.proposals == proposals
  192. >>> del det_example.proposals
  193. >>> assert 'proposals' not in det_example
  194. >>> with self.assertRaises(AssertionError):
  195. ... det_example.proposals = torch.rand((5, 4))
  196. """
  197. def __init__(self, *, metainfo: Optional[dict] = None, **kwargs) -> None:
  198. self._metainfo_fields: set = set()
  199. self._data_fields: set = set()
  200. if metainfo is not None:
  201. self.set_metainfo(metainfo=metainfo)
  202. if kwargs:
  203. self.set_data(kwargs)
  204. def set_metainfo(self, metainfo: dict) -> None:
  205. """Set or change key-value pairs in ``metainfo_field`` by parameter
  206. ``metainfo``.
  207. Args:
  208. metainfo (dict): A dict contains the meta information
  209. of image, such as ``img_shape``, ``scale_factor``, etc.
  210. """
  211. assert isinstance(metainfo, dict), f"metainfo should be a ``dict`` but got {type(metainfo)}"
  212. meta = copy.deepcopy(metainfo)
  213. for k, v in meta.items():
  214. self.set_field(name=k, value=v, field_type="metainfo", dtype=None)
  215. def set_data(self, data: dict) -> None:
  216. """Set or change key-value pairs in ``data_field`` by parameter
  217. ``data``.
  218. Args:
  219. data (dict): A dict contains annotations of image or
  220. model predictions.
  221. """
  222. assert isinstance(data, dict), f"data should be a `dict` but got {data}"
  223. for k, v in data.items():
  224. # Use `setattr()` rather than `self.set_field` to allow `set_data`
  225. # to set property method.
  226. setattr(self, k, v)
  227. def update(self, instance: "BaseDataElement") -> None:
  228. """The update() method updates the BaseDataElement with the elements
  229. from another BaseDataElement object.
  230. Args:
  231. instance (BaseDataElement): Another BaseDataElement object for
  232. update the current object.
  233. """
  234. assert isinstance(
  235. instance, BaseDataElement
  236. ), f"instance should be a `BaseDataElement` but got {type(instance)}"
  237. self.set_metainfo(dict(instance.metainfo_items()))
  238. self.set_data(dict(instance.items()))
  239. def new(self, *, metainfo: Optional[dict] = None, **kwargs) -> "BaseDataElement":
  240. """Return a new data element with same type. If ``metainfo`` and
  241. ``data`` are None, the new data element will have same metainfo and
  242. data. If metainfo or data is not None, the new result will overwrite it
  243. with the input value.
  244. Args:
  245. metainfo (dict, optional): A dict contains the meta information
  246. of image, such as ``img_shape``, ``scale_factor``, etc.
  247. Defaults to None.
  248. kwargs (dict): A dict contains annotations of image or
  249. model predictions.
  250. Returns:
  251. BaseDataElement: A new data element with same type.
  252. """
  253. new_data = self.__class__()
  254. if metainfo is not None:
  255. new_data.set_metainfo(metainfo)
  256. else:
  257. new_data.set_metainfo(dict(self.metainfo_items()))
  258. if kwargs:
  259. new_data.set_data(kwargs)
  260. else:
  261. new_data.set_data(dict(self.items()))
  262. return new_data
  263. def clone(self):
  264. """Deep copy the current data element.
  265. Returns:
  266. BaseDataElement: The copy of current data element.
  267. """
  268. clone_data = self.__class__()
  269. clone_data.set_metainfo(dict(self.metainfo_items()))
  270. clone_data.set_data(dict(self.items()))
  271. return clone_data
  272. def keys(self) -> list:
  273. """
  274. Returns:
  275. list: Contains all keys in data_fields.
  276. """
  277. # We assume that the name of the attribute related to property is
  278. # '_' + the name of the property. We use this rule to filter out
  279. # private keys.
  280. # TODO: Use a more robust way to solve this problem
  281. private_keys = {
  282. "_" + key
  283. for key in self._data_fields
  284. if isinstance(getattr(type(self), key, None), property)
  285. }
  286. return list(self._data_fields - private_keys)
  287. def metainfo_keys(self) -> list:
  288. """
  289. Returns:
  290. list: Contains all keys in metainfo_fields.
  291. """
  292. return list(self._metainfo_fields)
  293. def values(self) -> list:
  294. """
  295. Returns:
  296. list: Contains all values in data.
  297. """
  298. return [getattr(self, k) for k in self.keys()]
  299. def metainfo_values(self) -> list:
  300. """
  301. Returns:
  302. list: Contains all values in metainfo.
  303. """
  304. return [getattr(self, k) for k in self.metainfo_keys()]
  305. def all_keys(self) -> list:
  306. """
  307. Returns:
  308. list: Contains all keys in metainfo and data.
  309. """
  310. return self.metainfo_keys() + self.keys()
  311. def all_values(self) -> list:
  312. """
  313. Returns:
  314. list: Contains all values in metainfo and data.
  315. """
  316. return self.metainfo_values() + self.values()
  317. def all_items(self) -> Iterator[Tuple[str, Any]]:
  318. """
  319. Returns:
  320. iterator: An iterator object whose element is (key, value) tuple
  321. pairs for ``metainfo`` and ``data``.
  322. """
  323. for k in self.all_keys():
  324. yield (k, getattr(self, k))
  325. def items(self) -> Iterator[Tuple[str, Any]]:
  326. """
  327. Returns:
  328. iterator: An iterator object whose element is (key, value) tuple
  329. pairs for ``data``.
  330. """
  331. for k in self.keys():
  332. yield (k, getattr(self, k))
  333. def metainfo_items(self) -> Iterator[Tuple[str, Any]]:
  334. """
  335. Returns:
  336. iterator: An iterator object whose element is (key, value) tuple
  337. pairs for ``metainfo``.
  338. """
  339. for k in self.metainfo_keys():
  340. yield (k, getattr(self, k))
  341. @property
  342. def metainfo(self) -> dict:
  343. """dict: A dict contains metainfo of current data element."""
  344. return dict(self.metainfo_items())
  345. def __setattr__(self, name: str, value: Any):
  346. """setattr is only used to set data."""
  347. if name in ("_metainfo_fields", "_data_fields"):
  348. if not hasattr(self, name):
  349. super().__setattr__(name, value)
  350. else:
  351. raise AttributeError(
  352. f"{name} has been used as a " "private attribute, which is immutable."
  353. )
  354. else:
  355. self.set_field(name=name, value=value, field_type="data", dtype=None)
  356. def __delattr__(self, item: str):
  357. """Delete the item in dataelement.
  358. Args:
  359. item (str): The key to delete.
  360. """
  361. if item in ("_metainfo_fields", "_data_fields"):
  362. raise AttributeError(
  363. f"{item} has been used as a " "private attribute, which is immutable."
  364. )
  365. super().__delattr__(item)
  366. if item in self._metainfo_fields:
  367. self._metainfo_fields.remove(item)
  368. elif item in self._data_fields:
  369. self._data_fields.remove(item)
  370. # dict-like methods
  371. __delitem__ = __delattr__
  372. def get(self, key, default=None) -> Any:
  373. """Get property in data and metainfo as the same as python."""
  374. # Use `getattr()` rather than `self.__dict__.get()` to allow getting
  375. # properties.
  376. return getattr(self, key, default)
  377. def pop(self, *args) -> Any:
  378. """Pop property in data and metainfo as the same as python."""
  379. assert len(args) < 3, "``pop`` get more than 2 arguments"
  380. name = args[0]
  381. if name in self._metainfo_fields:
  382. self._metainfo_fields.remove(args[0])
  383. return self.__dict__.pop(*args)
  384. elif name in self._data_fields:
  385. self._data_fields.remove(args[0])
  386. return self.__dict__.pop(*args)
  387. # with default value
  388. elif len(args) == 2:
  389. return args[1]
  390. else:
  391. # don't just use 'self.__dict__.pop(*args)' for only popping key in
  392. # metainfo or data
  393. raise KeyError(f"{args[0]} is not contained in metainfo or data")
  394. def __contains__(self, item: str) -> bool:
  395. """Whether the item is in dataelement.
  396. Args:
  397. item (str): The key to inquire.
  398. """
  399. return item in self._data_fields or item in self._metainfo_fields
  400. def set_field(
  401. self,
  402. value: Any,
  403. name: str,
  404. dtype: Optional[Union[Type, Tuple[Type, ...]]] = None,
  405. field_type: str = "data",
  406. ) -> None:
  407. """Special method for set union field, used as property.setter
  408. functions."""
  409. assert field_type in ["metainfo", "data"]
  410. if dtype is not None:
  411. assert isinstance(value, dtype), f"{value} should be a {dtype} but got {type(value)}"
  412. if field_type == "metainfo":
  413. if name in self._data_fields:
  414. raise AttributeError(
  415. f"Cannot set {name} to be a field of metainfo "
  416. f"because {name} is already a data field"
  417. )
  418. self._metainfo_fields.add(name)
  419. else:
  420. if name in self._metainfo_fields:
  421. raise AttributeError(
  422. f"Cannot set {name} to be a field of data "
  423. f"because {name} is already a metainfo field"
  424. )
  425. self._data_fields.add(name)
  426. super().__setattr__(name, value)
  427. # Tensor-like methods
  428. def to(self, *args, **kwargs) -> "BaseDataElement":
  429. """Apply same name function to all tensors in data_fields."""
  430. new_data = self.new()
  431. for k, v in self.items():
  432. if hasattr(v, "to"):
  433. v = v.to(*args, **kwargs)
  434. data = {k: v}
  435. new_data.set_data(data)
  436. return new_data
  437. # Tensor-like methods
  438. def cpu(self) -> "BaseDataElement":
  439. """Convert all tensors to CPU in data."""
  440. new_data = self.new()
  441. for k, v in self.items():
  442. if isinstance(v, (torch.Tensor, BaseDataElement)):
  443. v = v.cpu()
  444. data = {k: v}
  445. new_data.set_data(data)
  446. return new_data
  447. # Tensor-like methods
  448. def cuda(self) -> "BaseDataElement":
  449. """Convert all tensors to GPU in data."""
  450. new_data = self.new()
  451. for k, v in self.items():
  452. if isinstance(v, (torch.Tensor, BaseDataElement)):
  453. v = v.cuda()
  454. data = {k: v}
  455. new_data.set_data(data)
  456. return new_data
  457. # Tensor-like methods
  458. def npu(self) -> "BaseDataElement":
  459. """Convert all tensors to NPU in data."""
  460. new_data = self.new()
  461. for k, v in self.items():
  462. if isinstance(v, (torch.Tensor, BaseDataElement)):
  463. v = v.npu()
  464. data = {k: v}
  465. new_data.set_data(data)
  466. return new_data
  467. def mlu(self) -> "BaseDataElement":
  468. """Convert all tensors to MLU in data."""
  469. new_data = self.new()
  470. for k, v in self.items():
  471. if isinstance(v, (torch.Tensor, BaseDataElement)):
  472. v = v.mlu()
  473. data = {k: v}
  474. new_data.set_data(data)
  475. return new_data
  476. # Tensor-like methods
  477. def detach(self) -> "BaseDataElement":
  478. """Detach all tensors in data."""
  479. new_data = self.new()
  480. for k, v in self.items():
  481. if isinstance(v, (torch.Tensor, BaseDataElement)):
  482. v = v.detach()
  483. data = {k: v}
  484. new_data.set_data(data)
  485. return new_data
  486. # Tensor-like methods
  487. def numpy(self) -> "BaseDataElement":
  488. """Convert all tensors to np.ndarray in data."""
  489. new_data = self.new()
  490. for k, v in self.items():
  491. if isinstance(v, (torch.Tensor, BaseDataElement)):
  492. v = v.detach().cpu().numpy()
  493. data = {k: v}
  494. new_data.set_data(data)
  495. return new_data
  496. def to_tensor(self) -> "BaseDataElement":
  497. """Convert all np.ndarray to tensor in data."""
  498. new_data = self.new()
  499. for k, v in self.items():
  500. data = {}
  501. if isinstance(v, np.ndarray):
  502. v = torch.from_numpy(v)
  503. data[k] = v
  504. elif isinstance(v, BaseDataElement):
  505. v = v.to_tensor()
  506. data[k] = v
  507. new_data.set_data(data)
  508. return new_data
  509. def to_dict(self) -> dict:
  510. """Convert BaseDataElement to dict."""
  511. return {
  512. k: v.to_dict() if isinstance(v, BaseDataElement) else v for k, v in self.all_items()
  513. }
  514. def __repr__(self) -> str:
  515. """Represent the object."""
  516. def _addindent(s_: str, num_spaces: int) -> str:
  517. """This func is modified from `pytorch` https://github.com/pytorch/
  518. pytorch/blob/b17b2b1cc7b017c3daaeff8cc7ec0f514d42ec37/torch/nn/modu
  519. les/module.py#L29.
  520. Args:
  521. s_ (str): The string to add spaces.
  522. num_spaces (int): The num of space to add.
  523. Returns:
  524. str: The string after add indent.
  525. """
  526. s = s_.split("\n")
  527. # don't do anything for single-line stuff
  528. if len(s) == 1:
  529. return s_
  530. first = s.pop(0)
  531. s = [(num_spaces * " ") + line for line in s]
  532. s = "\n".join(s) # type: ignore
  533. s = first + "\n" + s # type: ignore
  534. return s # type: ignore
  535. def dump(obj: Any) -> str:
  536. """Represent the object.
  537. Args:
  538. obj (Any): The obj to represent.
  539. Returns:
  540. str: The represented str.
  541. """
  542. _repr = ""
  543. if isinstance(obj, dict):
  544. for k, v in obj.items():
  545. _repr += f"\n{k}: {_addindent(dump(v), 4)}"
  546. elif isinstance(obj, BaseDataElement):
  547. _repr += "\n\n META INFORMATION"
  548. metainfo_items = dict(obj.metainfo_items())
  549. _repr += _addindent(dump(metainfo_items), 4)
  550. _repr += "\n\n DATA FIELDS"
  551. items = dict(obj.items())
  552. _repr += _addindent(dump(items), 4)
  553. classname = obj.__class__.__name__
  554. _repr = f"<{classname}({_repr}\n) at {hex(id(obj))}>"
  555. else:
  556. _repr += repr(obj)
  557. return _repr
  558. return dump(self)

An efficient Python toolkit for Abductive Learning (ABL), a novel paradigm that integrates machine learning and logical reasoning in a unified framework.