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.

utils.py 4.1 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from itertools import chain
  2. import numpy as np
  3. def flatten(nested_list):
  4. """
  5. Flattens a nested list.
  6. Parameters
  7. ----------
  8. nested_list : list
  9. A list which might contain sublists or tuples.
  10. Returns
  11. -------
  12. list
  13. A flattened version of the input list.
  14. Raises
  15. ------
  16. TypeError
  17. If the input object is not a list.
  18. """
  19. # if not isinstance(nested_list, list):
  20. # raise TypeError("Input must be of type list.")
  21. if isinstance(nested_list, list) and len(nested_list) == 0:
  22. return nested_list
  23. if not isinstance(nested_list, list) or not isinstance(nested_list[0], (list, tuple)):
  24. return nested_list
  25. return list(chain.from_iterable(nested_list))
  26. def reform_list(flattened_list, structured_list):
  27. """
  28. Reform the index based on structured_list structure.
  29. Parameters
  30. ----------
  31. flattened_list : list
  32. A flattened list of predictions.
  33. structured_list : list
  34. A list containing saved predictions, which could be nested lists or tuples.
  35. Returns
  36. -------
  37. list
  38. A reformed list that mimics the structure of structured_list.
  39. """
  40. # if not isinstance(flattened_list, list):
  41. # raise TypeError("Input must be of type list.")
  42. if not isinstance(structured_list[0], (list, tuple)):
  43. return flattened_list
  44. reformed_list = []
  45. idx_start = 0
  46. for elem in structured_list:
  47. idx_end = idx_start + len(elem)
  48. reformed_list.append(flattened_list[idx_start:idx_end])
  49. idx_start = idx_end
  50. return reformed_list
  51. def hamming_dist(pred_pseudo_label, candidates):
  52. """
  53. Compute the Hamming distance between two arrays.
  54. Parameters
  55. ----------
  56. pred_pseudo_label : list
  57. First array to compare.
  58. candidates : list
  59. Second array to compare, expected to have shape (n, m)
  60. where n is the number of rows, m is the length of pred_pseudo_label.
  61. Returns
  62. -------
  63. numpy.ndarray
  64. Hamming distances.
  65. """
  66. pred_pseudo_label = np.array(pred_pseudo_label)
  67. candidates = np.array(candidates)
  68. # Ensuring that pred_pseudo_label is broadcastable to the shape of candidates
  69. pred_pseudo_label = np.expand_dims(pred_pseudo_label, 0)
  70. return np.sum(pred_pseudo_label != candidates, axis=1)
  71. def confidence_dist(pred_prob, candidates):
  72. """
  73. Compute the confidence distance between prediction probabilities and candidates.
  74. Parameters
  75. ----------
  76. pred_prob : list of numpy.ndarray
  77. Prediction probability distributions, each element is an ndarray
  78. representing the probability distribution of a particular prediction.
  79. candidates : list of list of int
  80. Index of candidate labels, each element is a list of indexes being considered
  81. as a candidate correction.
  82. Returns
  83. -------
  84. numpy.ndarray
  85. Confidence distances computed for each candidate.
  86. """
  87. pred_prob = np.clip(pred_prob, 1e-9, 1)
  88. _, cols = np.indices((len(candidates), len(candidates[0])))
  89. return 1 - np.prod(pred_prob[cols, candidates], axis=1)
  90. def to_hashable(x):
  91. """
  92. Convert a nested list to a nested tuple so it is hashable.
  93. Parameters
  94. ----------
  95. x : list or other type
  96. A potentially nested list to convert to a tuple.
  97. Returns
  98. -------
  99. tuple or other type
  100. The input converted to a tuple if it was a list,
  101. otherwise the original input.
  102. """
  103. if isinstance(x, list):
  104. return tuple(to_hashable(item) for item in x)
  105. return x
  106. def restore_from_hashable(x):
  107. """
  108. Convert a nested tuple back to a nested list.
  109. Parameters
  110. ----------
  111. x : tuple or other type
  112. A potentially nested tuple to convert to a list.
  113. Returns
  114. -------
  115. list or other type
  116. The input converted to a list if it was a tuple,
  117. otherwise the original input.
  118. """
  119. if isinstance(x, tuple):
  120. return [restore_from_hashable(item) for item in x]
  121. return x

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