colormap.pyx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # New matplotlib colormaps by Nathaniel J. Smith, Stefan van der Walt,
  2. # and (in the case of viridis) Eric Firing.
  3. #
  4. # This file and the colormaps in it are released under the CC0 license /
  5. # public domain dedication. We would appreciate credit if you use or
  6. # redistribute these colormaps, but do not impose any legal restrictions.
  7. #
  8. # To the extent possible under law, the persons who associated CC0 with
  9. # mpl-colormaps have waived all copyright and related or neighboring rights
  10. # to mpl-colormaps.
  11. #
  12. # You should have received a copy of the CC0 legalcode along with this
  13. # work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  14. __all__ = ['apply_colormap', 'apply_colormap_single']
  15. cdef float _magma_data[16][3]
  16. _magma_data[0][:] = [0.001462, 0.000466, 0.013866]
  17. _magma_data[1][:] = [0.039608, 0.031090, 0.133515]
  18. _magma_data[2][:] = [0.113094, 0.065492, 0.276784]
  19. _magma_data[3][:] = [0.211718, 0.061992, 0.418647]
  20. _magma_data[4][:] = [0.316654, 0.071690, 0.485380]
  21. _magma_data[5][:] = [0.414709, 0.110431, 0.504662]
  22. _magma_data[6][:] = [0.512831, 0.148179, 0.507648]
  23. _magma_data[7][:] = [0.613617, 0.181811, 0.498536]
  24. _magma_data[8][:] = [0.716387, 0.214982, 0.475290]
  25. _magma_data[9][:] = [0.816914, 0.255895, 0.436461]
  26. _magma_data[10][:] = [0.904281, 0.319610, 0.388137]
  27. _magma_data[11][:] = [0.960949, 0.418323, 0.359630]
  28. _magma_data[12][:] = [0.986700, 0.535582, 0.382210]
  29. _magma_data[13][:] = [0.996096, 0.653659, 0.446213]
  30. _magma_data[14][:] = [0.996898, 0.769591, 0.534892]
  31. _magma_data[15][:] = [0.992440, 0.884330, 0.640099]
  32. cdef float _inferno_data[16][3]
  33. _inferno_data[0][:] = [0.001462, 0.000466, 0.013866]
  34. _inferno_data[1][:] = [0.042253, 0.028139, 0.141141]
  35. _inferno_data[2][:] = [0.129285, 0.047293, 0.290788]
  36. _inferno_data[3][:] = [0.238273, 0.036621, 0.396353]
  37. _inferno_data[4][:] = [0.341500, 0.062325, 0.429425]
  38. _inferno_data[5][:] = [0.441207, 0.099338, 0.431594]
  39. _inferno_data[6][:] = [0.540920, 0.134729, 0.415123]
  40. _inferno_data[7][:] = [0.640135, 0.171438, 0.381065]
  41. _inferno_data[8][:] = [0.735683, 0.215906, 0.330245]
  42. _inferno_data[9][:] = [0.822386, 0.275197, 0.266085]
  43. _inferno_data[10][:] = [0.894305, 0.353399, 0.193584]
  44. _inferno_data[11][:] = [0.946965, 0.449191, 0.115272]
  45. _inferno_data[12][:] = [0.978422, 0.557937, 0.034931]
  46. _inferno_data[13][:] = [0.987874, 0.675267, 0.065257]
  47. _inferno_data[14][:] = [0.974638, 0.797692, 0.206332]
  48. _inferno_data[15][:] = [0.947594, 0.917399, 0.410665]
  49. cdef float _plasma_data[16][3]
  50. _plasma_data[0][:] = [0.050383, 0.029803, 0.527975]
  51. _plasma_data[1][:] = [0.193374, 0.018354, 0.590330]
  52. _plasma_data[2][:] = [0.299855, 0.009561, 0.631624]
  53. _plasma_data[3][:] = [0.399411, 0.000859, 0.656133]
  54. _plasma_data[4][:] = [0.494877, 0.011990, 0.657865]
  55. _plasma_data[5][:] = [0.584391, 0.068579, 0.632812]
  56. _plasma_data[6][:] = [0.665129, 0.138566, 0.585582]
  57. _plasma_data[7][:] = [0.736019, 0.209439, 0.527908]
  58. _plasma_data[8][:] = [0.798216, 0.280197, 0.469538]
  59. _plasma_data[9][:] = [0.853319, 0.351553, 0.413734]
  60. _plasma_data[10][:] = [0.901807, 0.425087, 0.359688]
  61. _plasma_data[11][:] = [0.942598, 0.502639, 0.305816]
  62. _plasma_data[12][:] = [0.973416, 0.585761, 0.251540]
  63. _plasma_data[13][:] = [0.991365, 0.675355, 0.198453]
  64. _plasma_data[14][:] = [0.993033, 0.771720, 0.154808]
  65. _plasma_data[15][:] = [0.974443, 0.874622, 0.144061]
  66. cdef float _viridis_data[16][3]
  67. _viridis_data[0][:] = [0.267004, 0.004874, 0.329415]
  68. _viridis_data[1][:] = [0.282327, 0.094955, 0.417331]
  69. _viridis_data[2][:] = [0.278826, 0.175490, 0.483397]
  70. _viridis_data[3][:] = [0.258965, 0.251537, 0.524736]
  71. _viridis_data[4][:] = [0.229739, 0.322361, 0.545706]
  72. _viridis_data[5][:] = [0.199430, 0.387607, 0.554642]
  73. _viridis_data[6][:] = [0.172719, 0.448791, 0.557885]
  74. _viridis_data[7][:] = [0.149039, 0.508051, 0.557250]
  75. _viridis_data[8][:] = [0.127568, 0.566949, 0.550556]
  76. _viridis_data[9][:] = [0.120638, 0.625828, 0.533488]
  77. _viridis_data[10][:] = [0.157851, 0.683765, 0.501686]
  78. _viridis_data[11][:] = [0.246070, 0.738910, 0.452024]
  79. _viridis_data[12][:] = [0.369214, 0.788888, 0.382914]
  80. _viridis_data[13][:] = [0.515992, 0.831158, 0.294279]
  81. _viridis_data[14][:] = [0.678489, 0.863742, 0.189503]
  82. _viridis_data[15][:] = [0.845561, 0.887322, 0.099702]
  83. import numpy as np
  84. cimport cython
  85. cmaps = {}
  86. for (name, data) in (
  87. ('magma', _magma_data),
  88. ('inferno', _inferno_data),
  89. ('plasma', _plasma_data),
  90. ('viridis', _viridis_data)
  91. ):
  92. cmaps[name] = np.array(data, dtype=np.float32)*255
  93. magma = cmaps['magma']
  94. inferno = cmaps['inferno']
  95. plasma = cmaps['plasma']
  96. viridis = cmaps['viridis']
  97. @cython.boundscheck(False)
  98. @cython.wraparound(False)
  99. cdef map_many(int[:, :] idxs, float[:, :, :] dst, float[:,:] cmap):
  100. cdef int height = idxs.shape[0]
  101. cdef int width = idxs.shape[1]
  102. for i in range(height):
  103. for j in range(width):
  104. dst[i][j][0] = cmap[idxs[i][j]][0]
  105. dst[i][j][1] = cmap[idxs[i][j]][1]
  106. dst[i][j][2] = cmap[idxs[i][j]][2]
  107. def apply_colormap(input_: np.ndarray, colormap: str, min_: float=None, max_: float=None):
  108. if max_ is None:
  109. max_ = np.max(input_)
  110. if min_ is None:
  111. min_ = np.min(input_)
  112. range_ = max_ - min_
  113. if range_ == 0:
  114. idxs = np.zeros((input_.shape[0], input_.shape[1]), dtype=np.int32)
  115. else:
  116. normalized = 15*(input_ - min_) / range_
  117. idxs = normalized.astype(np.int32)
  118. np.clip(idxs, 0, 15, out=idxs)
  119. colors = np.zeros(shape=(input_.shape[0], input_.shape[1], 3), dtype=np.float32)
  120. map_many(idxs, colors, cmaps[colormap])
  121. return colors
  122. def apply_colormap_single(input_: np.ndarray, colormap: str, min_: float=None, max_: float=None):
  123. range_ = max_ - min_
  124. if range_ == 0:
  125. idx = 0
  126. else:
  127. idx = int(15*(input_ - min_) / range_)
  128. idx = np.clip(idx, 0, 15)
  129. color = tuple(cmaps[colormap][idx])
  130. return color