PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/unit/test_wxagg.py

https://github.com/tris-sondon/matplotlib
Python | 176 lines | 90 code | 38 blank | 48 comment | 7 complexity | 6d400a3d392fa796cd4a53dcb35c417e 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. from __future__ import print_function
  32. import wx
  33. import time
  34. import matplotlib
  35. matplotlib.use('Agg')
  36. from matplotlib.figure import Figure
  37. from matplotlib.transforms import Bbox, Point, Value
  38. from matplotlib.backends.backend_agg import FigureCanvasAgg
  39. from matplotlib.backends.backend_wxagg import _py_convert_agg_to_wx_image, \
  40. _py_convert_agg_to_wx_bitmap
  41. import matplotlib.backends._wxagg as wxagg
  42. ####################
  43. # Test Configuration
  44. ####################
  45. # Simple tests -- write PNG images of the plots
  46. TEST_PY = 0
  47. TEST_EXT = 0
  48. # Timing tests -- print time per plot
  49. TIME_PY = 1
  50. TIME_EXT = 1
  51. #################
  52. # Test Parameters
  53. #################
  54. # Bounding box to use in testing
  55. ll_x = 320
  56. ll_y = 240
  57. ur_x = 640
  58. ur_y = 480
  59. BBOX = Bbox(Point(Value(ll_x), Value(ll_y)),
  60. Point(Value(ur_x), Value(ur_y)))
  61. # Number of iterations for timing
  62. NITERS = 25
  63. ###############################################################################
  64. #
  65. # Testing framework
  66. #
  67. def time_loop(function, args):
  68. i = 0
  69. start = time.time()
  70. while i < NITERS:
  71. function(*args)
  72. i += 1
  73. return (time.time() - start)/NITERS
  74. def make_figure():
  75. figure = Figure((6.4, 4.8), 100, frameon=False)
  76. canvas = FigureCanvasAgg(figure)
  77. return figure, canvas
  78. def plot_sin(figure):
  79. from pylab import arange, sin, pi
  80. t = arange(0.0, 2.0, 0.01)
  81. s = sin(2*pi*t)
  82. axes = figure.gca()
  83. axes.plot(t, s, linewidth=1.0)
  84. axes.set_title('title')
  85. def main():
  86. app = wx.PySimpleApp()
  87. figure, canvas = make_figure()
  88. bbox = None
  89. plot_sin(figure)
  90. canvas.draw()
  91. agg = canvas.get_renderer()
  92. if 0:
  93. print('ll.x =', BBOX.ll().x().get())
  94. print('ll.y =', BBOX.ll().y().get())
  95. print('ur.x =', BBOX.ur().x().get())
  96. print('ur.y =', BBOX.ur().y().get())
  97. # test the pure python implementation
  98. if TEST_PY:
  99. i_py = _py_convert_agg_to_wx_image( agg, None)
  100. b_py = _py_convert_agg_to_wx_bitmap(agg, None)
  101. i_py_b = _py_convert_agg_to_wx_image( agg, BBOX)
  102. b_py_b = _py_convert_agg_to_wx_bitmap(agg, BBOX)
  103. i_py.SaveFile( 'a_py_img.png', wx.BITMAP_TYPE_PNG)
  104. b_py.SaveFile( 'a_py_bmp.png', wx.BITMAP_TYPE_PNG)
  105. i_py_b.SaveFile('b_py_img.png', wx.BITMAP_TYPE_PNG)
  106. b_py_b.SaveFile('b_py_bmp.png', wx.BITMAP_TYPE_PNG)
  107. # test the C++ implementation
  108. if TEST_EXT:
  109. i_ext = wxagg.convert_agg_to_wx_image( agg, None)
  110. b_ext = wxagg.convert_agg_to_wx_bitmap(agg, None)
  111. i_ext_b = wxagg.convert_agg_to_wx_image( agg, BBOX)
  112. b_ext_b = wxagg.convert_agg_to_wx_bitmap(agg, BBOX)
  113. i_ext.SaveFile( 'a_ext_img.png', wx.BITMAP_TYPE_PNG)
  114. b_ext.SaveFile( 'a_ext_bmp.png', wx.BITMAP_TYPE_PNG)
  115. i_ext_b.SaveFile('b_ext_img.png', wx.BITMAP_TYPE_PNG)
  116. b_ext_b.SaveFile('b_ext_bmp.png', wx.BITMAP_TYPE_PNG)
  117. # time the pure python implementation
  118. if TIME_PY:
  119. t = time_loop(_py_convert_agg_to_wx_image, (agg,None))
  120. print('Python agg2img: %.4f seconds (%.1f HZ)' % (t, 1/t))
  121. t = time_loop(_py_convert_agg_to_wx_bitmap, (agg,None))
  122. print('Python agg2bmp: %.4f seconds (%.1f HZ)' % (t, 1/t))
  123. t = time_loop(_py_convert_agg_to_wx_image, (agg,BBOX))
  124. print('Python agg2img w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t))
  125. t = time_loop(_py_convert_agg_to_wx_bitmap, (agg,BBOX))
  126. print('Python agg2bmp w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t))
  127. # time the C++ implementation
  128. if TIME_EXT:
  129. t = time_loop(wxagg.convert_agg_to_wx_image, (agg,None))
  130. print('_wxagg agg2img: %.4f seconds (%.1f HZ)' % (t, 1/t))
  131. t = time_loop(wxagg.convert_agg_to_wx_bitmap, (agg,None))
  132. print('_wxagg agg2bmp: %.4f seconds (%.1f HZ)' % (t, 1/t))
  133. t = time_loop(wxagg.convert_agg_to_wx_image, (agg,BBOX))
  134. print('_wxagg agg2img w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t))
  135. t = time_loop(wxagg.convert_agg_to_wx_bitmap, (agg,BBOX))
  136. print('_wxagg agg2bmp w/bbox: %.4f seconds (%.1f HZ)' % (t, 1/t))
  137. if __name__ == '__main__':
  138. main()