Back to index

python-biopython  1.60
test_GraphicsBitmaps.py
Go to the documentation of this file.
00001 # Copyright 2001 by Brad Chapman.  All rights reserved.
00002 # Copyright 2009 by Peter Cock.  All rights reserved.
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 
00007 """Test for bitmap output via ReportLab (requires an extra dependency).
00008 
00009 The primary purpose of this is to flag if renderPM is missing, but also
00010 to check Bio.Graphics can make a bitmap (e.g. PNG).
00011 
00012 The example itself is essentially a repeat from test_GraphicsGeneral.py.
00013 """
00014 
00015 import os
00016 import random
00017 import unittest
00018 
00019 from Bio import MissingExternalDependencyError
00020 from Bio import MissingPythonDependencyError
00021 
00022 try:
00023     # Skip the test if reportlab is not installed
00024     import reportlab as r
00025     del r
00026 except:
00027     raise MissingPythonDependencyError(
00028         "Install ReportLab if you want to use Bio.Graphics.")
00029 try:
00030     # Skip the test if reportlab is not installed
00031     from reportlab.graphics import renderPM
00032 except:
00033     raise MissingPythonDependencyError(\
00034         "Install ReportLab's renderPM module if you want to create "
00035         "bitmaps with Bio.Graphics.")
00036 try:
00037     # Skip the test if PIL is not installed
00038     import Image as i
00039     del i
00040 except:
00041     raise MissingPythonDependencyError(\
00042         "Install PIL (Python Imaging Library) if you want to create "
00043         "bitmaps with Bio.Graphics.")
00044 
00045 from reportlab.graphics.renderPM import RenderPMError
00046 
00047 # the stuff we're testing
00048 from Bio.Graphics.Comparative import ComparativeScatterPlot
00049 
00050 # We're not really using the unittest framework, because we need to
00051 # raise the dependency error BEFORE the invidual tests in order that
00052 # this be skipped by run_tests.py
00053 
00054 def real_test():
00055     min_two_d_lists = 1
00056     max_two_d_lists = 7
00057 
00058     min_num_points = 1
00059     max_num_points = 500
00060 
00061     min_point_num = 0
00062     max_point_num = 200
00063 
00064     plot_info = []
00065     num_two_d_lists = random.randrange(min_two_d_lists, max_two_d_lists)
00066 
00067     for two_d_list in range(num_two_d_lists):
00068         cur_list = []
00069         num_points = random.randrange(min_num_points, max_num_points)
00070         for point in range(num_points):
00071             x_point = random.randrange(min_point_num, max_point_num)
00072             y_point = random.randrange(min_point_num, max_point_num)
00073             cur_list.append((x_point, y_point))
00074         plot_info.append(cur_list)
00075     
00076     compare_plot = ComparativeScatterPlot("png")
00077     compare_plot.display_info = plot_info
00078 
00079     output_file = os.path.join(os.getcwd(), "Graphics", "scatter_test.png")
00080     try:
00081         compare_plot.draw_to_file(output_file, "Testing Scatter Plots")
00082     # there is a bug in reportlab which occasionally generates an
00083     # error here.
00084     except IndexError:
00085         pass
00086     except IOError, err:
00087         if "encoder zip not available" in str(err):
00088             raise MissingExternalDependencyError(
00089                 "Check zip encoder installed for PIL and ReportLab renderPM")
00090         else:
00091             raise err
00092     except RenderPMError, err :
00093         if str(err).startswith("Can't setFont(") :
00094             #TODO - can we raise the error BEFORE the unit test function
00095             #is run? That way it can be skipped in run_tests.py
00096             raise MissingExternalDependencyError(\
00097                 "Check the fonts needed by ReportLab if you want "
00098                 "bitmaps from Bio.Graphics\n" + str(err))
00099         else :
00100             raise err
00101     
00102     return True
00103 
00104 #Run the actual test BEFORE the unittest stuff gets called
00105 real_test()
00106                
00107 class ComparativeTest(unittest.TestCase):
00108     """Do tests for modules involved with comparing data."""
00109     def test_simple_scatter_plot(self):
00110         """Test creation of a simple PNG scatter plot."""
00111         #Dummy method to show up via run_tests.py
00112         pass
00113 
00114 if __name__ == "__main__":
00115     runner = unittest.TextTestRunner(verbosity = 2)
00116     unittest.main(testRunner=runner)