utils.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. """
  2. Utils functions of ipfml package (normalization, integral...)
  3. """
  4. # main imports
  5. import numpy as np
  6. import math
  7. import sys
  8. # computation imports
  9. from scipy.integrate import simps
  10. def normalize_arr(arr):
  11. """Normalize data of 1D array shape
  12. Args:
  13. arr: array data of 1D shape
  14. Returns:
  15. Normalized 1D array
  16. Example:
  17. >>> from ipfml import utils
  18. >>> import numpy as np
  19. >>> arr = np.arange(5)
  20. >>> arr_normalized = utils.normalize_arr(arr)
  21. >>> arr_normalized[1]
  22. 0.1
  23. """
  24. output_arr = []
  25. sum_value = sum(arr)
  26. for v in arr:
  27. # add of epsilon value in order to avoid Zero Division
  28. output_arr.append(v / sum_value)
  29. return output_arr
  30. def normalize_arr_with_range(arr, min, max):
  31. '''Normalize data of 1D array shape
  32. Args:
  33. arr: array data of 1D shape
  34. Returns:
  35. Normalized 1D Numpy array
  36. Example:
  37. >>> from ipfml import utils
  38. >>> import numpy as np
  39. >>> arr = np.arange(11)
  40. >>> arr_normalized = utils.normalize_arr_with_range(arr, 0, 20)
  41. >>> arr_normalized[1]
  42. 0.05
  43. '''
  44. output_arr = []
  45. for v in arr:
  46. # add of epsilon value in order to avoid Zero Division
  47. output_arr.append((v - min) / (max - min + sys.float_info.epsilon))
  48. return output_arr
  49. def normalize_2D_arr(arr):
  50. """Return array normalize from its min and max values
  51. Args:
  52. arr: 2D Numpy array
  53. Returns:
  54. Normalized 2D Numpy array
  55. Example:
  56. >>> from PIL import Image
  57. >>> from ipfml import utils
  58. >>> from ipfml.processing import transform
  59. >>> img = Image.open('./images/test_img.png')
  60. >>> img_mscn = transform.rgb_to_mscn(img)
  61. >>> img_normalized = utils.normalize_2D_arr(img_mscn)
  62. >>> img_normalized.shape
  63. (200, 200)
  64. """
  65. # getting min and max value from 2D array
  66. max_value = arr.max(axis=1).max()
  67. min_value = arr.min(axis=1).min()
  68. # normalize each row
  69. output_array = []
  70. _, height = arr.shape
  71. for row_index in range(0, height):
  72. values = arr[row_index, :]
  73. output_array.append(
  74. normalize_arr_with_range(values, min_value, max_value))
  75. return np.asarray(output_array)
  76. def integral_area_trapz(y_values, dx):
  77. """Returns area under curves from provided data points using Trapezium rule
  78. Args:
  79. y_values: y values of curve
  80. dx: number of unit for x axis
  81. Returns:
  82. Area under curves obtained from these points
  83. Example:
  84. >>> from ipfml import utils
  85. >>> import numpy as np
  86. >>> y_values = np.array([5, 20, 4, 18, 19, 18, 7, 4])
  87. >>> area = utils.integral_area_trapz(y_values, dx=5)
  88. >>> area
  89. 452.5
  90. """
  91. return np.trapz(y_values, dx=dx)
  92. def integral_area_simps(y_values, dx):
  93. """Returns area under curves from provided data points using Simpsons rule
  94. Args:
  95. y_values: y values of curve
  96. dx: number of unit for x axis
  97. Returns:
  98. Area under curves obtained from these points
  99. Example:
  100. >>> from ipfml import utils
  101. >>> import numpy as np
  102. >>> y_values = np.array([5, 20, 4, 18, 19, 18, 7, 4])
  103. >>> area = utils.integral_area_simps(y_values, dx=5)
  104. >>> area
  105. 460.0
  106. """
  107. return simps(y_values, dx=dx)
  108. def get_indices_of_highest_values(arr, n):
  109. """Returns indices of n highest values from list or 1D numpy array
  110. Args:
  111. arr: List of numpy array
  112. n: number of highest elements wanted
  113. Returns:
  114. `n` indices of highest values
  115. Example:
  116. >>> from ipfml import utils
  117. >>> import numpy as np
  118. >>> arr = np.arange(10)
  119. >>> indices = utils.get_indices_of_highest_values(arr, 2)
  120. >>> indices
  121. array([9, 8])
  122. """
  123. return np.array(arr).argsort()[-n:][::-1]
  124. def get_indices_of_lowest_values(arr, n):
  125. """Returns indices of n highest values from list or 1D numpy array
  126. Args:
  127. arr: List of numpy array
  128. n: number of highest elements wanted
  129. Returns:
  130. `n` indices of highest values
  131. Example:
  132. >>> from ipfml import utils
  133. >>> import numpy as np
  134. >>> arr = np.arange(10)
  135. >>> indices = utils.get_indices_of_lowest_values(arr, 2)
  136. >>> indices
  137. array([0, 1])
  138. """
  139. return np.array(arr).argsort()[::-1][-n:][::-1]
  140. def get_entropy(arr):
  141. """Returns the computed entropy from arr
  142. Args:
  143. arr: numpy array
  144. Returns:
  145. entropy score computed
  146. Example:
  147. >>> from ipfml import utils
  148. >>> import numpy as np
  149. >>> arr = np.arange(10)
  150. >>> entropy = utils.get_entropy(arr)
  151. >>> int(entropy)
  152. 0
  153. """
  154. arr = np.array(arr)
  155. eigen_values = []
  156. sum_eigen_values = (arr * arr).sum()
  157. for val in arr:
  158. eigen_values.append(val * val)
  159. v = []
  160. for val in eigen_values:
  161. v.append(val / sum_eigen_values)
  162. entropy = 0
  163. for val in v:
  164. if val > 0:
  165. entropy += val * math.log(val)
  166. entropy *= -1
  167. entropy /= math.log(len(v))
  168. return entropy
  169. def get_entropy_without_i(arr, i):
  170. """Returns the computed entropy from arr without contribution of i
  171. Args:
  172. arr: numpy array
  173. i: column index
  174. Returns:
  175. entropy score computed
  176. Example:
  177. >>> from ipfml import utils
  178. >>> import numpy as np
  179. >>> arr = np.arange(10)
  180. >>> entropy = utils.get_entropy_without_i(arr, 3)
  181. >>> int(entropy)
  182. 0
  183. """
  184. arr = np.array([v for index, v in enumerate(arr) if index != i])
  185. return get_entropy(arr)
  186. def get_entropy_contribution_of_i(arr, i):
  187. """Returns the entropy contribution i column
  188. Args:
  189. arr: numpy array
  190. i: column index
  191. Returns:
  192. entropy contribution score computed
  193. Example:
  194. >>> from ipfml import utils
  195. >>> import numpy as np
  196. >>> arr = np.arange(10)
  197. >>> entropy = utils.get_entropy_contribution_of_i(arr, 3)
  198. >>> int(entropy)
  199. 0
  200. """
  201. return get_entropy(arr) - get_entropy_without_i(arr, i)