annotate test_weblogo.py @ 10:20716450be87

Uploaded
author davidmurphy
date Mon, 30 Jan 2012 21:17:50 -0500
parents c55bdc2fb9fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1 #!/usr/bin/env python
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
2
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
3 # Copyright (c) 2006, The Regents of the University of California, through
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
4 # Lawrence Berkeley National Laboratory (subject to receipt of any required
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
5 # approvals from the U.S. Dept. of Energy). All rights reserved.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
6
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
7 # This software is distributed under the new BSD Open Source License.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
8 # <http://www.opensource.org/licenses/bsd-license.html>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
9 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
10 # Redistribution and use in source and binary forms, with or without
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
11 # modification, are permitted provided that the following conditions are met:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
12 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
13 # (1) Redistributions of source code must retain the above copyright notice,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
14 # this list of conditions and the following disclaimer.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
15 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
16 # (2) Redistributions in binary form must reproduce the above copyright
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
17 # notice, this list of conditions and the following disclaimer in the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
18 # documentation and or other materials provided with the distribution.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
19 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
20 # (3) Neither the name of the University of California, Lawrence Berkeley
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
21 # National Laboratory, U.S. Dept. of Energy nor the names of its contributors
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
22 # may be used to endorse or promote products derived from this software
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
23 # without specific prior written permission.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
24 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
26 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
27 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
28 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
29 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
30 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
31 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
32 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
33 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
34 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
35 # POSSIBILITY OF SUCH DAMAGE.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
36
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
37
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
38 import unittest
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
39
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
40 import weblogolib
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
41 from weblogolib import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
42 from weblogolib import parse_prior, GhostscriptAPI
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
43 from weblogolib.color import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
44 from weblogolib.colorscheme import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
45 from StringIO import StringIO
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
46 import sys
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
47
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
48 from numpy import array, asarray, float64, ones, zeros, int32,all,any, shape
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
49 import numpy as na
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
50
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
51 from corebio import seq_io
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
52 from corebio.seq import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
53
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
54 # python2.3 compatability
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
55 from corebio._future.subprocess import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
56 from corebio._future import resource_stream
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
57
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
58 from corebio.moremath import entropy
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
59 from math import log, sqrt
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
60 codon_alphabetU=['AAA', 'AAC', 'AAG', 'AAU', 'ACA', 'ACC', 'ACG', 'ACU', 'AGA', 'AGC', 'AGG', 'AGU', 'AUA', 'AUC', 'AUG', 'AUU', 'CAA', 'CAC', 'CAG', 'CAU', 'CCA', 'CCC', 'CCG', 'CCU', 'CGA', 'CGC', 'CGG', 'CGU', 'CUA', 'CUC', 'CUG', 'CUU', 'GAA', 'GAC', 'GAG', 'GAU', 'GCA', 'GCC', 'GCG', 'GCU', 'GGA', 'GGC', 'GGG', 'GGU', 'GUA', 'GUC', 'GUG', 'GUU', 'UAA', 'UAC', 'UAG', 'UAU', 'UCA', 'UCC', 'UCG', 'UCU', 'UGA', 'UGC', 'UGG', 'UGU', 'UUA', 'UUC', 'UUG', 'UUU']
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
61 codon_alphabetT=['AAA', 'AAC', 'AAG', 'AAT', 'ACA', 'ACC', 'ACG', 'ACT', 'AGA', 'AGC', 'AGG', 'AGT', 'ATA', 'ATC', 'ATG', 'ATT', 'CAA', 'CAC', 'CAG', 'CAT', 'CCA', 'CCC', 'CCG', 'CCT', 'CGA', 'CGC', 'CGG', 'CGT', 'CTA', 'CTC', 'CTG', 'CTT', 'GAA', 'GAC', 'GAG', 'GAT', 'GCA', 'GCC', 'GCG', 'GCT', 'GGA', 'GGC', 'GGG', 'GGT', 'GTA', 'GTC', 'GTG', 'GTT', 'TAA', 'TAC', 'TAG', 'TAT', 'TCA', 'TCC', 'TCG', 'TCT', 'TGA', 'TGC', 'TGG', 'TGT', 'TTA', 'TTC', 'TTG', 'TTT']
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
62
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
63
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
64 def testdata_stream( name ):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
65 return resource_stream(__name__, 'tests/data/'+name, __file__)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
66
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
67 class test_logoformat(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
68
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
69 def test_options(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
70 options = LogoOptions()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
71
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
72
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
73 class test_ghostscript(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
74 def test_version(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
75 version = GhostscriptAPI().version
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
76
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
77
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
78
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
79 class test_parse_prior(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
80 def assertTrue(self, bool) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
81 self.assertEquals( bool, True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
82
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
83 def test_parse_prior_none(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
84 self.assertEquals( None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
85 parse_prior(None, unambiguous_protein_alphabet ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
86 self.assertEquals( None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
87 parse_prior( 'none', unambiguous_protein_alphabet ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
88 self.assertEquals( None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
89 parse_prior( 'noNe', None) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
90
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
91 def test_parse_prior_equiprobable(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
92 self.assertTrue( all(20.*equiprobable_distribution(20) ==
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
93 parse_prior( 'equiprobable', unambiguous_protein_alphabet ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
94
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
95 self.assertTrue(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
96 all( 1.2* equiprobable_distribution(3)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
97 == parse_prior( ' equiprobablE ', Alphabet('123'), 1.2 ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
98
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
99 def test_parse_prior_percentage(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
100 #print parse_prior( '50%', unambiguous_dna_alphabet, 1. )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
101 self.assertTrue( all( equiprobable_distribution(4)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
102 == parse_prior( '50%', unambiguous_dna_alphabet, 1. ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
103
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
104 self.assertTrue( all( equiprobable_distribution(4)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
105 == parse_prior( ' 50.0 % ', unambiguous_dna_alphabet, 1. ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
106
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
107 self.assertTrue( all( array( (0.3,0.2,0.2,0.3), float64)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
108 == parse_prior( ' 40.0 % ', unambiguous_dna_alphabet, 1. ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
109
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
110 def test_parse_prior_float(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
111 self.assertTrue( all( equiprobable_distribution(4)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
112 == parse_prior( '0.5', unambiguous_dna_alphabet, 1. ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
113
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
114 self.assertTrue( all( equiprobable_distribution(4)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
115 == parse_prior( ' 0.500 ', unambiguous_dna_alphabet, 1. ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
116
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
117 self.assertTrue( all( array( (0.3,0.2,0.2,0.3), float64)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
118 == parse_prior( ' 0.40 ', unambiguous_dna_alphabet, 1. ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
119
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
120 def test_auto(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
121 self.assertTrue( all(4.*equiprobable_distribution(4) ==
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
122 parse_prior( 'auto', unambiguous_dna_alphabet ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
123 self.assertTrue( all(4.*equiprobable_distribution(4) ==
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
124 parse_prior( 'automatic', unambiguous_dna_alphabet ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
125
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
126 def test_weight(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
127 self.assertTrue( all(4.*equiprobable_distribution(4) ==
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
128 parse_prior( 'automatic', unambiguous_dna_alphabet ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
129 self.assertTrue( all(123.123*equiprobable_distribution(4) ==
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
130 parse_prior( 'auto', unambiguous_dna_alphabet , 123.123) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
131
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
132 def test_explicit(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
133 s = "{'A':10, 'C':40, 'G':40, 'T':10}"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
134 p = array( (10, 40, 40,10), float64)*4./100.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
135 self.assertTrue( all(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
136 p == parse_prior( s, unambiguous_dna_alphabet ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
137
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
138
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
139 class test_logooptions(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
140 def test_create(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
141 opt = LogoOptions()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
142 opt.small_fontsize =10
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
143 options = repr(opt)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
144
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
145 opt = LogoOptions(title="sometitle")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
146 assert opt.title == "sometitle"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
147
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
148 class test_logosize(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
149 def test_create(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
150 s = LogoSize(101.0,10.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
151 assert s.stack_width == 101.0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
152 r = repr(s)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
153
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
154
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
155 class test_seqlogo(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
156 # FIXME: The version of python used by Popen may not be the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
157 # same as that used to run this test.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
158 def _exec(self, args, outputtext, returncode =0, stdin=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
159 if not stdin :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
160 stdin = testdata_stream("cap.fa")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
161 args = ["./weblogo"] + args
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
162 p = Popen(args,stdin=stdin,stdout=PIPE, stderr=PIPE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
163 (out, err) = p.communicate()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
164 if returncode ==0 and p.returncode >0 :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
165 print err
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
166 self.assertEquals(returncode, p.returncode)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
167 if returncode == 0 : self.assertEquals( len(err), 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
168
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
169 for item in outputtext :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
170 self.failUnless(item in out)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
171
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
172
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
173
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
174 def test_malformed_options(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
175 self._exec( ["--notarealoption"], [], 2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
176 self._exec( ["extrajunk"], [], 2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
177 self._exec( ["-I"], [], 2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
178
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
179 def test_help_option(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
180 self._exec( ["-h"], ["options"])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
181 self._exec( ["--help"], ["options"])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
182
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
183 def test_version_option(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
184 self._exec( ['--version'], weblogolib.__version__)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
185
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
186
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
187 def test_default_build(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
188 self._exec( [], ["%%Title: Sequence Logo:"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
189
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
190
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
191 # Format options
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
192 def test_width(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
193 self._exec( ['-W','1234'], ["/stack_width 1234"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
194 self._exec( ['--stack-width','1234'], ["/stack_width 1234"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
195
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
196 def test_height(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
197 self._exec( ['-H','1234'], ["/stack_height 1234"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
198 self._exec( ['--stack-height','1234'], ["/stack_height 1234"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
199
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
200
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
201 def test_stacks_per_line(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
202 self._exec( ['-n','7'], ["/stacks_per_line 7 def"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
203 self._exec( ['--stacks-per-line','7'], ["/stacks_per_line 7 def"] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
204
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
205
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
206 def test_title(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
207 self._exec( ['-t', '3456'], ['/logo_title (3456) def',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
208 '/show_title True def'])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
209 self._exec( ['-t', ''], ['/logo_title () def',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
210 '/show_title False def'])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
211 self._exec( ['--title', '3456'], ['/logo_title (3456) def',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
212 '/show_title True def'])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
213
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
214
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
215
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
216
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
217
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
218 class test_which(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
219 def test_which(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
220 tests = (
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
221 (seq_io.read(testdata_stream('cap.fa')), codon_alphabetT),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
222 (seq_io.read(testdata_stream('capu.fa')), codon_alphabetU),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
223
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
224 #(seq_io.read(testdata_stream('cox2.msf')), unambiguous_protein_alphabet),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
225 #(seq_io.read(testdata_stream('Rv3829c.fasta')), unambiguous_protein_alphabet),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
226 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
227 for t in tests :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
228 self.failUnlessEqual(which_alphabet(t[0]), t[1])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
229
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
230
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
231
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
232
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
233 class test_colorscheme(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
234
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
235 def test_colorgroup(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
236 cr = ColorGroup( "ABC", "black", "Because")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
237 self.assertEquals( cr.description, "Because")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
238
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
239 def test_colorscheme(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
240 cs = ColorScheme([
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
241 ColorGroup("G", "orange"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
242 ColorGroup("TU", "red"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
243 ColorGroup("C", "blue"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
244 ColorGroup("A", "green")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
245 ],
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
246 title = "title",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
247 description = "description",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
248 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
249
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
250 self.assertEquals( cs.color('A'), Color.by_name("green"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
251 self.assertEquals( cs.color('X'), cs.default_color)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
252
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
253
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
254
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
255 class test_color(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
256 # 2.3 Python compatibility
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
257 assertTrue = unittest.TestCase.failUnless
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
258 assertFalse = unittest.TestCase.failIf
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
259
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
260 def test_color_names(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
261 names = Color.names()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
262 self.failUnlessEqual( len(names), 147)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
263
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
264 for n in names:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
265 c = Color.by_name(n)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
266 self.assertTrue( c != None )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
267
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
268
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
269 def test_color_components(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
270 white = Color.by_name("white")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
271 self.failUnlessEqual( 1.0, white.red)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
272 self.failUnlessEqual( 1.0, white.green)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
273 self.failUnlessEqual( 1.0, white.blue)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
274
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
275
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
276 c = Color(0.3, 0.4, 0.2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
277 self.failUnlessEqual( 0.3, c.red)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
278 self.failUnlessEqual( 0.4, c.green)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
279 self.failUnlessEqual( 0.2, c.blue)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
280
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
281 c = Color(0,128,0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
282 self.failUnlessEqual( 0.0, c.red)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
283 self.failUnlessEqual( 128./255., c.green)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
284 self.failUnlessEqual( 0.0, c.blue)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
285
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
286
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
287 def test_color_from_rgb(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
288 white = Color.by_name("white")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
289
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
290 self.failUnlessEqual(white, Color(1.,1.,1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
291 self.failUnlessEqual(white, Color(255,255,255) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
292 self.failUnlessEqual(white, Color.from_rgb(1.,1.,1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
293 self.failUnlessEqual(white, Color.from_rgb(255,255,255) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
294
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
295
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
296 def test_color_from_hsl(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
297 red = Color.by_name("red")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
298 lime = Color.by_name("lime")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
299 saddlebrown = Color.by_name("saddlebrown")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
300 darkgreen = Color.by_name("darkgreen")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
301 blue = Color.by_name("blue")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
302 green = Color.by_name("green")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
303
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
304 self.failUnlessEqual(red, Color.from_hsl(0, 1.0,0.5) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
305 self.failUnlessEqual(lime, Color.from_hsl(120, 1.0, 0.5) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
306 self.failUnlessEqual(blue, Color.from_hsl(240, 1.0, 0.5) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
307 self.failUnlessEqual(Color.by_name("gray"), Color.from_hsl(0,0,0.5) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
308
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
309 self.failUnlessEqual(saddlebrown, Color.from_hsl(25, 0.76, 0.31) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
310
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
311 self.failUnlessEqual(darkgreen, Color.from_hsl(120, 1.0, 0.197) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
312
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
313
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
314 def test_color_by_name(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
315 white = Color.by_name("white")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
316 self.failUnlessEqual(white, Color.by_name("white"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
317 self.failUnlessEqual(white, Color.by_name("WHITE"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
318 self.failUnlessEqual(white, Color.by_name(" wHiTe \t\n\t"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
319
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
320
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
321 self.failUnlessEqual(Color(255,255,240), Color.by_name("ivory"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
322 self.failUnlessEqual(Color(70,130,180), Color.by_name("steelblue"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
323
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
324 self.failUnlessEqual(Color(0,128,0), Color.by_name("green"))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
325
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
326
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
327 def test_color_from_invalid_name(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
328 self.failUnlessRaises( ValueError, Color.by_name, "not_a_color")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
329
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
330
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
331 def test_color_clipping(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
332 red = Color.by_name("red")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
333 self.failUnlessEqual(red, Color(255,0,0) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
334 self.failUnlessEqual(red, Color(260,-10,0) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
335 self.failUnlessEqual(red, Color(1.1,-0.,-1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
336
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
337 self.failUnlessEqual( Color(1.0001, 213.0, 1.2).red, 1.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
338 self.failUnlessEqual( Color(-0.001, -2183.0, -1.0).red, 0.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
339 self.failUnlessEqual( Color(1.0001, 213.0, 1.2).green, 1.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
340 self.failUnlessEqual( Color(-0.001, -2183.0, -1.0).green, 0.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
341 self.failUnlessEqual( Color(1.0001, 213.0, 1.2).blue, 1.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
342 self.failUnlessEqual( Color(-0.001, -2183.0, -1.0).blue, 0.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
343
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
344
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
345 def test_color_fail_on_mixed_type(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
346 self.failUnlessRaises( TypeError, Color.from_rgb, 1,1,1.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
347 self.failUnlessRaises( TypeError, Color.from_rgb, 1.0,1,1.0 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
348
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
349 def test_color_red(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
350 # Check Usage comment in Color
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
351 red = Color.by_name("red")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
352 self.failUnlessEqual( red , Color(255,0,0) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
353 self.failUnlessEqual( red, Color(1., 0., 0.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
354
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
355 self.failUnlessEqual( red , Color.from_rgb(1.,0.,0.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
356 self.failUnlessEqual( red , Color.from_rgb(255,0,0) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
357 self.failUnlessEqual( red , Color.from_hsl(0.,1., 0.5) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
358
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
359 self.failUnlessEqual( red , Color.from_string("red") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
360 self.failUnlessEqual( red , Color.from_string("RED") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
361 self.failUnlessEqual( red , Color.from_string("#F00") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
362 self.failUnlessEqual( red , Color.from_string("#FF0000") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
363 self.failUnlessEqual( red , Color.from_string("rgb(255, 0, 0)") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
364 self.failUnlessEqual( red , Color.from_string("rgb(100%, 0%, 0%)") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
365 self.failUnlessEqual( red , Color.from_string("hsl(0, 100%, 50%)") )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
366
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
367
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
368 def test_color_from_string(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
369 purple = Color(128,0,128)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
370 red = Color(255,0,0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
371 skyblue = Color(135,206,235)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
372
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
373 red_strings = ("red",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
374 "ReD",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
375 "RED",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
376 " Red \t",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
377 "#F00",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
378 "#FF0000",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
379 "rgb(255, 0, 0)",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
380 "rgb(100%, 0%, 0%)",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
381 "hsl(0, 100%, 50%)")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
382
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
383 for s in red_strings:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
384 self.failUnlessEqual( red, Color.from_string(s) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
385
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
386 skyblue_strings = ("skyblue",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
387 "SKYBLUE",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
388 " \t\n SkyBlue \t",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
389 "#87ceeb",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
390 "rgb(135,206,235)"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
391 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
392
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
393 for s in skyblue_strings:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
394 self.failUnlessEqual( skyblue, Color.from_string(s) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
395
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
396
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
397
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
398 def test_color_equality(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
399 c1 = Color(123,99,12)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
400 c2 = Color(123,99,12)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
401
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
402 self.failUnlessEqual(c1,c2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
403
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
404
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
405
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
406
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
407
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
408
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
409 class test_Dirichlet(unittest.TestCase) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
410 # 2.3 Python compatibility
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
411 assertTrue = unittest.TestCase.failUnless
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
412 assertFalse = unittest.TestCase.failIf
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
413
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
414
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
415 def test_init(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
416 d = Dirichlet( ( 1,1,1,1,) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
417
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
418
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
419 def test_random(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
420
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
421
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
422 def do_test( alpha, samples = 1000) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
423 ent = zeros( (samples,), float64)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
424 #alpha = ones( ( K,), Float64 ) * A/K
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
425
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
426 #pt = zeros( (len(alpha) ,), Float64)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
427 d = Dirichlet(alpha)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
428 for s in range(samples) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
429 p = d.sample()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
430 #print p
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
431 #pt +=p
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
432 ent[s] = entropy(p)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
433
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
434 #print pt/samples
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
435
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
436 m = mean(ent)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
437 v = var(ent)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
438
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
439 dm = d.mean_entropy()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
440 dv = d.variance_entropy()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
441
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
442 #print alpha, ':', m, v, dm, dv
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
443 error = 4. * sqrt(v/samples)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
444 self.assertTrue( abs(m-dm) < error)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
445 self.assertTrue( abs(v-dv) < error) # dodgy error estimate
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
446
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
447
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
448 do_test( (1., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
449 do_test( (2., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
450 do_test( (3., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
451 do_test( (4., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
452 do_test( (5., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
453 do_test( (6., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
454
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
455 do_test( (1., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
456 do_test( (20., 20.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
457 do_test( (1., 1., 1., 1., 1., 1., 1., 1., 1., 1.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
458 do_test( (.1, .1, .1, .1, .1, .1, .1, .1, .1, .1) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
459 do_test( (.01, .01, .01, .01, .01, .01, .01, .01, .01, .01) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
460 do_test( (2.0, 6.0, 1.0, 1.0) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
461
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
462
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
463 def test_mean(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
464 alpha = ones( ( 10,), float64 ) * 23.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
465 d = Dirichlet(alpha)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
466 m = d.mean()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
467 self.assertAlmostEqual( m[2], 1./10)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
468 self.assertAlmostEqual( sum(m), 1.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
469
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
470 def test_covariance(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
471 alpha = ones( ( 4,), float64 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
472 d = Dirichlet(alpha)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
473 cv = d.covariance()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
474 self.assertEqual( cv.shape, (4,4) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
475 self.assertAlmostEqual( cv[0,0], 1.0 * (1.0 - 1./4.0)/ (4.0 * 5.0) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
476 self.assertAlmostEqual( cv[0,1], - 1 / ( 4. * 4. * 5.) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
477
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
478 def test_mean_x(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
479 alpha = (1.0, 2.0, 3.0, 4.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
480 xx = (2.0, 2.0, 2.0, 2.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
481 m = Dirichlet(alpha).mean_x(xx)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
482 self.assertEquals( m, 2.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
483
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
484 alpha = (1.0, 1.0, 1.0, 1.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
485 xx = (2.0, 3.0, 4.0, 3.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
486 m = Dirichlet(alpha).mean_x(xx)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
487 self.assertEquals( m, 3.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
488
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
489 def test_variance_x(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
490 alpha = (1.0, 1.0, 1.0, 1.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
491 xx = (2.0, 2.0, 2.0, 2.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
492 v = Dirichlet(alpha).variance_x(xx)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
493 self.assertAlmostEquals( v, 0.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
494
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
495 alpha = (1.0, 2.0, 3.0, 4.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
496 xx = (2.0, 0.0, 1.0, 10.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
497 v = Dirichlet(alpha).variance_x(xx)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
498 #print v
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
499 # TODO: Don't actually know if this is correct
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
500
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
501 def test_relative_entropy(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
502 alpha = (2.0, 10.0, 1.0, 1.0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
503 d = Dirichlet(alpha)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
504 pvec = (0.1, 0.2, 0.3, 0.4)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
505
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
506 rent = d.mean_relative_entropy(pvec)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
507 vrent = d.variance_relative_entropy(pvec)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
508 low, high = d.interval_relative_entropy(pvec, 0.95)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
509
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
510 #print
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
511 #print '> ', rent, vrent, low, high
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
512
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
513 # This test can fail randomly, but the precision form a few
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
514 # thousand samples is low. Increasing samples, 1000->2000
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
515 samples = 2000
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
516 sent = zeros( (samples,), float64)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
517
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
518 for s in range(samples) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
519 post = d.sample()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
520 e = -entropy(post)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
521 for k in range(4) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
522 e += - post[k] * log(pvec[k])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
523 sent[s] = e
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
524 sent.sort()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
525 self.assertTrue( abs(sent.mean() - rent) < 4.*sqrt(vrent) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
526 self.assertAlmostEqual( sent.std(), sqrt(vrent), 1 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
527 self.assertTrue( abs(low-sent[ int( samples *0.025)])<0.2 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
528 self.assertTrue( abs(high-sent[ int( samples *0.975)])<0.2 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
529
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
530 #print '>>', mean(sent), var(sent), sent[ int( samples *0.025)] ,sent[ int( samples *0.975)]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
531
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
532
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
533
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
534 def mean( a) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
535 return sum(a)/ len(a)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
536
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
537 def var(a) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
538 return (sum(a*a) /len(a) ) - mean(a)**2
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
539
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
540
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
541
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
542
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
543 if __name__ == '__main__':
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
544 unittest.main()