/maps/build/mayavi/integrationtests/mayavi/test_image_plane_widget.py

https://github.com/fspaolo/code
Python | 153 lines | 86 code | 33 blank | 34 comment | 1 complexity | 04429af2328a414d77e17a14a9344789 MD5 | raw file
  1. """Tests for the ImagePlaneWidget module.
  2. """
  3. # Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
  4. # Copyright (c) 2005, Enthought, Inc.
  5. # License: BSD Style.
  6. # Standard library imports.
  7. from os.path import abspath
  8. from StringIO import StringIO
  9. import copy
  10. import numpy
  11. # Enthought library imports.
  12. from enthought.traits.api import TraitError
  13. # Local imports.
  14. from common import TestCase
  15. class TestImagePlaneWidget(TestCase):
  16. def make_data(self):
  17. """Creates suitable data for the test."""
  18. dims = numpy.array((64, 64, 64), 'i')
  19. # Create some scalars to render.
  20. dx, dy, dz = 10.0/(dims - 1)
  21. x = numpy.reshape(numpy.arange(-5.0, 5.0+dx*0.5, dx, 'f'),
  22. (dims[0], 1, 1))
  23. y = numpy.reshape(numpy.arange(-5.0, 5.0+dy*0.5, dy, 'f'),
  24. (1, dims[1], 1))
  25. z = numpy.reshape(numpy.arange(-5.0, 5.0+dz*0.5, dz, 'f'),
  26. (1, 1, dims[0]))
  27. scalars = numpy.sin(x*y*z)/(x*y*z)
  28. return scalars
  29. def set_view(self, s):
  30. """Sets the view correctly."""
  31. #s.scene.reset_zoom()
  32. s.scene.z_plus_view()
  33. c = s.scene.camera
  34. c.azimuth(30)
  35. c.elevation(30)
  36. s.render()
  37. def check(self):
  38. script = self.script
  39. src = script.engine.current_scene.children[0]
  40. i1, i2, i3 = src.children[0].children[1:]
  41. assert i1.ipw.plane_orientation == 'x_axes'
  42. assert numpy.allclose(i1.ipw.center, (0, 31.5, 31.5))
  43. rng = i1.ipw.reslice_output.point_data.scalars.range
  44. assert numpy.allclose(rng, (-0.2, 1.0), atol=0.1)
  45. assert i2.ipw.plane_orientation == 'y_axes'
  46. assert numpy.allclose(i2.ipw.center, (31.5, 0, 31.5))
  47. rng = i2.ipw.reslice_output.point_data.scalars.range
  48. assert numpy.allclose(rng, (-0.2, 1.0), atol=0.1)
  49. assert i3.ipw.plane_orientation == 'z_axes'
  50. assert numpy.allclose(i3.ipw.center, (31.5, 31.5, 0))
  51. rng = i3.ipw.reslice_output.point_data.scalars.range
  52. assert numpy.allclose(rng, (-0.2, 1.0), atol=0.1)
  53. def test(self):
  54. self.main()
  55. def do(self):
  56. ############################################################
  57. # Imports.
  58. script = self.script
  59. from enthought.mayavi.sources.array_source import ArraySource
  60. from enthought.mayavi.modules.outline import Outline
  61. from enthought.mayavi.modules.image_plane_widget import ImagePlaneWidget
  62. ############################################################
  63. # Create a new scene and set up the visualization.
  64. s = self.new_scene()
  65. d = ArraySource()
  66. sc = self.make_data()
  67. d.scalar_data = sc
  68. script.add_source(d)
  69. # Create an outline for the data.
  70. o = Outline()
  71. script.add_module(o)
  72. # ImagePlaneWidgets for the scalars
  73. ipw = ImagePlaneWidget()
  74. script.add_module(ipw)
  75. ipw_y = ImagePlaneWidget()
  76. script.add_module(ipw_y)
  77. ipw_y.ipw.plane_orientation = 'y_axes'
  78. ipw_z = ImagePlaneWidget()
  79. script.add_module(ipw_z)
  80. ipw_z.ipw.plane_orientation = 'z_axes'
  81. # Set the scene to a suitable view.
  82. self.set_view(s)
  83. self.check()
  84. ############################################################
  85. # Test if saving a visualization and restoring it works.
  86. # Save visualization.
  87. f = StringIO()
  88. f.name = abspath('test.mv2') # We simulate a file.
  89. script.save_visualization(f)
  90. f.seek(0) # So we can read this saved data.
  91. # Remove existing scene.
  92. engine = script.engine
  93. engine.close_scene(s)
  94. # Load visualization
  95. script.load_visualization(f)
  96. s = engine.current_scene
  97. # Set the scene to a suitable view.
  98. self.set_view(s)
  99. self.check()
  100. ############################################################
  101. # Test if the MayaVi2 visualization can be deepcopied.
  102. # Pop the source object.
  103. sources = s.children
  104. s.children = []
  105. # Add it back to see if that works without error.
  106. s.children.extend(sources)
  107. self.set_view(s)
  108. self.check()
  109. # Now deepcopy the source and replace the existing one with
  110. # the copy. This basically simulates cutting/copying the
  111. # object from the UI via the right-click menu on the tree
  112. # view, and pasting the copy back.
  113. sources1 = copy.deepcopy(sources)
  114. s.children[:] = sources
  115. self.set_view(s)
  116. self.check()
  117. # If we have come this far, we are golden!
  118. if __name__ == "__main__":
  119. t = TestImagePlaneWidget()
  120. t.test()