utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """
  2. Utils functions of ipfml package (array normalization)
  3. """
  4. import numpy as np
  5. from scipy.integrate import simps
  6. def normalize_arr(arr):
  7. """Normalize data of 1D array shape
  8. Args:
  9. arr: array data of 1D shape
  10. Returns:
  11. Normalized 1D array
  12. Example:
  13. >>> from ipfml import utils
  14. >>> import numpy as np
  15. >>> arr = np.arange(11)
  16. >>> arr_normalized = utils.normalize_arr(arr)
  17. >>> arr_normalized[1]
  18. 0.1
  19. """
  20. output_arr = []
  21. max_value = max(arr)
  22. min_value = min(arr)
  23. for v in arr:
  24. output_arr.append((v - min_value) / (max_value - min_value))
  25. return output_arr
  26. def normalize_arr_with_range(arr, min, max):
  27. '''Normalize data of 1D array shape
  28. Args:
  29. arr: array data of 1D shape
  30. Returns:
  31. Normalized 1D Numpy array
  32. Example:
  33. >>> from ipfml import utils
  34. >>> import numpy as np
  35. >>> arr = np.arange(11)
  36. >>> arr_normalized = utils.normalize_arr_with_range(arr, 0, 20)
  37. >>> arr_normalized[1]
  38. 0.05
  39. '''
  40. output_arr = []
  41. for v in arr:
  42. output_arr.append((v - min) / (max - min))
  43. return output_arr
  44. def normalize_2D_arr(arr):
  45. """Return array normalize from its min and max values
  46. Args:
  47. arr: 2D Numpy array
  48. Returns:
  49. Normalized 2D Numpy array
  50. Example:
  51. >>> from PIL import Image
  52. >>> from ipfml import utils, processing
  53. >>> img = Image.open('./images/test_img.png')
  54. >>> img_mscn = processing.rgb_to_mscn(img)
  55. >>> img_normalized = utils.normalize_2D_arr(img_mscn)
  56. >>> img_normalized.shape
  57. (200, 200)
  58. """
  59. # getting min and max value from 2D array
  60. max_value = arr.max(axis=1).max()
  61. min_value = arr.min(axis=1).min()
  62. # normalize each row
  63. output_array = []
  64. width, height = arr.shape
  65. for row_index in range(0, height):
  66. values = arr[row_index, :]
  67. output_array.append(
  68. normalize_arr_with_range(values, min_value, max_value))
  69. return np.asarray(output_array)
  70. def integral_area_trapz(y_values, dx):
  71. """Returns area under curves from provided data points using Trapezium rule
  72. Args:
  73. points: array of point coordinates
  74. dx: number of unit for x axis
  75. Returns:
  76. Area under curves obtained from these points
  77. Example:
  78. >>> from ipfml import utils
  79. >>> import numpy as np
  80. >>> y_values = np.array([5, 20, 4, 18, 19, 18, 7, 4])
  81. >>> area = utils.integral_area_trapz(y_values, dx=5)
  82. >>> area
  83. 452.5
  84. """
  85. return np.trapz(y_values, dx=dx)
  86. def integral_area_simps(y_values, dx):
  87. """Returns area under curves from provided data points using Simpsons rule
  88. Args:
  89. points: array of point coordinates
  90. dx: number of unit for x axis
  91. Returns:
  92. Area under curves obtained from these points
  93. Example:
  94. >>> from ipfml import utils
  95. >>> import numpy as np
  96. >>> y_values = np.array([5, 20, 4, 18, 19, 18, 7, 4])
  97. >>> area = utils.integral_area_simps(y_values, dx=5)
  98. >>> area
  99. 460.0
  100. """
  101. return simps(y_values, dx=dx)