/unit/test_wxagg.py

https://github.com/friedrichromstedt/matplotlib-grayscale · Python · 174 lines · 89 code · 37 blank · 48 comment · 7 complexity · 0cc31d11ff95b3f326101f887756d8a0 MD5 · raw file

  1. #!/usr/bin/env pythonw
  2. # Name: test_wxagg.py
  3. # Purpose: exercises the agg to wx.Image and wx.Bitmap conversion functions
  4. # Author: Ken McIvor <mcivor@iit.edu>
  5. #
  6. # Copyright 2005 Illinois Institute of Technology
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be
  17. # included in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. # IN NO EVENT SHALL ILLINOIS INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY
  23. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. #
  27. # Except as contained in this notice, the name of Illinois Institute
  28. # of Technology shall not be used in advertising or otherwise to promote
  29. # the sale, use or other dealings in this Software without prior written
  30. # authorization from Illinois Institute of Technology.
  31. import wx
  32. import time
  33. import matplotlib
  34. matplotlib.use('Agg')
  35. from matplotlib.figure import Figure
  36. from matplotlib.transforms import Bbox, Point, Value
  37. from matplotlib.backends.backend_agg import FigureCanvasAgg
  38. from matplotlib.backends.backend_wxagg import _py_convert_agg_to_wx_image, \
  39. _py_convert_agg_to_wx_bitmap
  40. import matplotlib.backends._wxagg as wxagg
  41. ####################
  42. # Test Configuration
  43. ####################
  44. # Simple tests -- write PNG images of the plots
  45. TEST_PY = 0
  46. TEST_EXT = 0
  47. # Timing tests -- print time per plot
  48. TIME_PY = 1
  49. TIME_EXT = 1
  50. #################
  51. # Test Parameters
  52. #################
  53. # Bounding box to use in testing
  54. ll_x = 320
  55. ll_y = 240
  56. ur_x = 640
  57. ur_y = 480
  58. BBOX = Bbox(Point(Value(ll_x), Value(ll_y)),
  59. Point(Value(ur_x), Value(ur_y)))
  60. # Number of iterations for timing
  61. NITERS = 25
  62. ###############################################################################
  63. #
  64. # Testing framework
  65. #
  66. def time_loop(function, args):
  67. i = 0
  68. start = time.time()
  69. while i < NITERS:
  70. function(*args)
  71. i += 1
  72. return (time.time() - start)/NITERS
  73. def make_figure():
  74. figure = Figure((6.4, 4.8), 100, frameon=False)
  75. canvas = FigureCanvasAgg(figure)
  76. return figure, canvas
  77. def plot_sin(figure):
  78. from pylab import arange, sin, pi
  79. t = arange(0.0, 2.0, 0.01)
  80. s = sin(2*pi*t)
  81. axes = figure.gca()
  82. axes.plot(t, s, linewidth=1.0)
  83. axes.set_title('title')
  84. def main():
  85. app = wx.PySimpleApp()
  86. figure, canvas = make_figure()
  87. bbox = None
  88. plot_sin(figure)
  89. canvas.draw()
  90. agg = canvas.get_renderer()
  91. if 0:
  92. print 'll.x =', BBOX.ll().x().get()
  93. print 'll.y =', BBOX.ll().y().get()
  94. print 'ur.x =', BBOX.ur().x().get()
  95. print 'ur.y =', BBOX.ur().y().get()
  96. # test the pure python implementation
  97. if TEST_PY:
  98. i_py = _py_convert_agg_to_wx_image( agg, None)
  99. b_py = _py_convert_agg_to_wx_bitmap(agg, None)
  100. i_py_b = _py_convert_agg_to_wx_image( agg, BBOX)
  101. b_py_b = _py_convert_agg_to_wx_bitmap(agg, BBOX)
  102. i_py.SaveFile( 'a_py_img.png', wx.BITMAP_TYPE_PNG)
  103. b_py.SaveFile( 'a_py_bmp.png', wx.BITMAP_TYPE_PNG)
  104. i_py_b.SaveFile('b_py_img.png', wx.BITMAP_TYPE_PNG)
  105. b_py_b.SaveFile('b_py_bmp.png', wx.BITMAP_TYPE_PNG)
  106. # test the C++ implementation
  107. if TEST_EXT:
  108. i_ext = wxagg.convert_agg_to_wx_image( agg, None)
  109. b_ext = wxagg.convert_agg_to_wx_bitmap(agg, None)
  110. i_ext_b = wxagg.convert_agg_to_wx_image( agg, BBOX)
  111. b_ext_b = wxagg.convert_agg_to_wx_bitmap(agg, BBOX)
  112. i_ext.SaveFile( 'a_ext_img.png', wx.BITMAP_TYPE_PNG)
  113. b_ext.SaveFile( 'a_ext_bmp.png', wx.BITMAP_TYPE_PNG)
  114. i_ext_b.SaveFile('b_ext_img.png', wx.BITMAP_TYPE_PNG)
  115. b_ext_b.SaveFile('b_ext_bmp.png', wx.BITMAP_TYPE_PNG)
  116. # time the pure python implementation
  117. if TIME_PY:
  118. t = time_loop(_py_convert_agg_to_wx_image, (agg,None))
  119. print 'Python agg2img: %.4f seconds (%.1f HZ)' % (t, 1/t)
  120. t = time_loop(_py_convert_agg_to_wx_bitmap, (agg,None))
  121. print 'Python agg2bmp: %.4f seconds (%.1f HZ)' % (t, 1/t)
  122. t = time_loop(_py_convert_agg_to_wx_image, (agg,BBOX))
  123. print 'Python agg2img w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t)
  124. t = time_loop(_py_convert_agg_to_wx_bitmap, (agg,BBOX))
  125. print 'Python agg2bmp w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t)
  126. # time the C++ implementation
  127. if TIME_EXT:
  128. t = time_loop(wxagg.convert_agg_to_wx_image, (agg,None))
  129. print '_wxagg agg2img: %.4f seconds (%.1f HZ)' % (t, 1/t)
  130. t = time_loop(wxagg.convert_agg_to_wx_bitmap, (agg,None))
  131. print '_wxagg agg2bmp: %.4f seconds (%.1f HZ)' % (t, 1/t)
  132. t = time_loop(wxagg.convert_agg_to_wx_image, (agg,BBOX))
  133. print '_wxagg agg2img w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t)
  134. t = time_loop(wxagg.convert_agg_to_wx_bitmap, (agg,BBOX))
  135. print '_wxagg agg2bmp w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t)
  136. if __name__ == '__main__':
  137. main()