Back to index

python-biopython  1.60
test_GraphicsDistribution.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright 2001 by Brad Chapman.  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 """Test the creation of graphics of distribution information.
00007 
00008 Provides tests for the Graphics.Distribution classes which provide the
00009 ability to create graphics to show the distribution of information about
00010 BACs/chromosomes/etc.
00011 """
00012 # standard library
00013 import os
00014 import random
00015 import unittest
00016 
00017 from Bio import MissingExternalDependencyError
00018 try:
00019     import reportlab as r
00020     del r
00021 except:
00022     raise MissingExternalDependencyError(\
00023         "Install reportlab if you want to use Bio.Graphics.")
00024 
00025 # local stuff
00026 from Bio.Graphics.Distribution import BarChartDistribution
00027 from Bio.Graphics.Distribution import LineDistribution
00028 from Bio.Graphics.Distribution import DistributionPage
00029 
00030 
00031 def random_distribution(min = -5.0, max = 5.0, total_items = 50):
00032     """Create a series of random distribution information.
00033     """
00034     num_items = random.randrange(5, total_items)
00035     all_info = []
00036     for item in range(num_items):
00037         new_item = random.uniform(min, max)
00038         all_info.append(new_item)
00039 
00040     return all_info
00041 
00042 class BarChartTest(unittest.TestCase):
00043     """Test display of BarChart distributions on a page.
00044     """
00045     def setUp(self):
00046         self.simple_page = os.path.join(os.getcwd(), "Graphics", "simple_bar.pdf")
00047         self.multi_page = os.path.join(os.getcwd(), "Graphics", "multi_bar.pdf")
00048 
00049         self.num_multi = 5
00050     
00051     def test_simple_page(self):
00052         """Test displaying a page with single distribution.
00053         """
00054         dist_info = []
00055         new_info = random_distribution()
00056         dist_info.append(new_info)
00057         distribution = BarChartDistribution(dist_info)
00058 
00059         dist_page = DistributionPage()
00060         dist_page.distributions.append(distribution)
00061 
00062         dist_page.draw(self.simple_page, "Test Bar Chart")
00063 
00064     def test_multi_page(self):
00065         """Create a page with multiple distributions on it.
00066         """
00067         dist_page = DistributionPage()
00068 
00069         dist_page.number_of_columns = 3
00070         
00071         for multi in range(self.num_multi):
00072             dist_info = []
00073             new_info = random_distribution()
00074             dist_info.append(new_info)
00075 
00076             distribution = BarChartDistribution(dist_info)
00077             distribution.chart_title = "Distribution %s" % (multi + 1)
00078             dist_page.distributions.append(distribution)
00079 
00080         dist_page.draw(self.multi_page, "Test Multi Bar Chart")
00081 
00082 if __name__ == "__main__":
00083     runner = unittest.TextTestRunner(verbosity = 2)
00084     unittest.main(testRunner=runner)