utils.py 5.7 KB

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