utils.py 6.0 KB

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