utils.py 6.0 KB

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