1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#/usr/bin/env python
from cogent.draw.multivariate_plot import (plot_ordination,
map_colors, text_points, scatter_points, plot_points, arrows)
from cogent.util.unit_test import TestCase, main
import os, pylab
from tempfile import mkstemp
from numpy import asarray, c_
from pdb import set_trace
__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Rob Knight"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"
######
# util class
class TestCasePlot(TestCase):
Debug = True
def fig(self, fname=None):
if self.Debug:
pylab.show()
else:
if not fname:
fd, fname = mkstemp(prefix='PlotTest_', suffix='.png')
pylab.savefig(fname)
pylab.clf()
os.remove(fname)
def p(self, obj):
if self.Debug:
print obj
class TestI(TestCasePlot):
def setUp(self):
self.points = [(0, 0), (1,1), (1,2), (2, 2)]
#adjust canvas
#pylab.xlim(-0.5, 2.5)
#pylab.ylim(-0.5, 2.5)
class FunctionsTests(TestI):
def test_map_colors(self):
#default
self.assertEqual(map_colors(range(3)),
['#000080', '#7dff7a', '#800000'])
#alternative cmap
self.assertEqual(map_colors(range(3), cmap='hot'),
['#0b0000', '#ff5c00', '#ffffff'])
#return tuples
self.assertFloatEqual(map_colors(range(3), mode='tuples'),
[(0.0, 0.0, 0.5), (0.490196, 1.0, 0.477546), (0.5, 0.0, 0.0)])
def test_text_points(self):
#same text for all the points
text_points(self.points, 'X')
self.fig()
def test_text_points_diff_texts(self):
#diff text for all the points
text_points(self.points, ['A', 'B', 'C', 'A'])
self.fig()
def test_plot_points(self):
plot_points(self.points, label='X')
self.fig()
class scatter_points_tests(TestI):
def test_basic(self):
scatter_points(self.points, label='X')
self.fig()
def test_color_list(self):
scatter_points(self.points, c=['k', 'red', '#00FF00', (0, 0, 1)],
s=[100, 200, 300, 400], marker=['o', 's', 'd', 'h'])
pylab.xlim(-0.5, 2.5)
pylab.ylim(-0.5, 2.5)
self.fig()
def test_color_shades(self):
#self.Debug = True
scatter_points(self.points, c=[1, 2, 3, 4],
s=[100, 200, 300, 400], marker=['o', 's', 'd', 'h'])
pylab.xlim(-0.5, 2.5)
pylab.ylim(-0.5, 2.5)
self.fig()
class arrows_tests(TestI):
def test_arrows(self):
points = self.points
arrows([[0,0]]*len(points), points)
self.fig()
class plot_ordination_tests(TestCasePlot):
def setUp(self):
self.points = points_3d
self.keys = ['eigvals', 'samples', 'species', 'centroids', 'biplot']
self.values = [
[0.3, 0.1, 0, -0.5], #eigvals
self.points, #samples
self.points + 0.2, #species
[(0, 1, 0), (2, -2, 0)], #centroids
[(0.5, 0.5, 1), (-1, 1.5, 1)], #biplot
]
def test_basic(self):
res = dict(zip(self.keys[:2], self.values))
plot_ordination(res)
self.fig()
def test_species(self):
res = dict(zip(self.keys[:3], self.values))
plot_ordination(res)
self.fig()
def test_centroids(self):
res = dict(zip(self.keys[:4], self.values))
plot_ordination(res)
self.fig()
def test_biplot(self):
res = dict(zip(self.keys, self.values))
plot_ordination(res,
species_kw={'label': 'sp'}, biplot_kw={'label':['b1', 'b2']},
samples_kw={'label': 'sa'})
self.fig()
def test_choices(self):
res = dict(zip(self.keys, self.values))
plot_ordination(res, choices=[2,3])
self.fig()
def test_axis_names(self):
#self.Debug = True
res = dict(zip(self.keys[:2], self.values))
plot_ordination(res, axis_names=['CCA1', 'CA1', 'CA2'],
constrained_names='CCA')
self.fig()
#####
# test data
points = asarray([(0,0), (-1, 1), (1, -2), (2, 3)], float)
points_3d = c_[points, [[3]]*len(points)]
if __name__ == '__main__':
main()
|