utils.py 6.1 KB

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