Package Bio :: Package Graphics :: Module ColorSpiral
[hide private]
[frames] | no frames]

Source Code for Module Bio.Graphics.ColorSpiral

  1  # This code is part of the Biopython distribution and governed by its 
  2  # license.  Please see the LICENSE file that should have been included 
  3  # as part of this package. 
  4  # 
  5   
  6  """Generate RGB colours suitable for distinguishing categorical data. 
  7   
  8  This module provides a class that implements a spiral 'path' through HSV 
  9  colour space, permitting the selection of a number of points along that path, 
 10  and returning the output in RGB colour space, suitable for use with ReportLab 
 11  and other graphics packages. 
 12   
 13  This approach to colour choice was inspired by Bang Wong's Points of View 
 14  article: Color Coding, in Nature Methods _7_ 573 (doi:10.1038/nmeth0810-573). 
 15   
 16  The module also provides helper functions that return a list for colours, or 
 17  a dictionary of colours (if passed an iterable containing the names of 
 18  categories to be coloured). 
 19  """ 
 20   
 21  # standard library 
 22  import colorsys    # colour format conversions 
 23  from math import log, exp, floor, pi 
 24  import random      # for jitter values 
 25   
 26   
27 -class ColorSpiral(object):
28 """Implement a spiral path through HSV colour space. 29 30 This class provides functions for sampling points along a logarithmic 31 spiral path through HSV colour space. 32 33 The spiral is described by r = a * exp(b * t) where r is the distance 34 from the axis of the HSV cylinder to the current point in the spiral, 35 and t is the angle through which the spiral has turned to reach the 36 current point. a and b are (positive, real) parameters that control the 37 shape of the spiral. 38 39 a: the starting direction of the spiral 40 b: the number of revolutions about the axis made by the spiral 41 42 We permit the spiral to move along the cylinder ('in V-space') between 43 v_init and v_final, to give a gradation in V (essentially, brightness), 44 along the path, where v_init, v_final are in [0,1]. 45 46 A brightness 'jitter' may also be provided as an absolute value in 47 V-space, to aid in distinguishing consecutive colour points on the 48 path. 49 """
50 - def __init__(self, a=1, b=0.33, v_init=0.85, v_final=0.5, 51 jitter=0.05):
52 """Initialise a logarithmic spiral path through HSV colour space 53 54 Arguments: 55 56 o a - Parameter a for the spiral, controls the initial spiral 57 direction. a > 0 58 59 o b - parameter b for the spiral, controls the rate at which the 60 spiral revolves around the axis. b > 0 61 62 o v_init - initial value of V (brightness) for the spiral. 63 v_init in [0,1] 64 65 o v_final - final value of V (brightness) for the spiral 66 v_final in [0,1] 67 68 o jitter - the degree of V (brightness) jitter to add to each 69 selected colour. The amount of jitter will be selected 70 from a uniform random distribution [-jitter, jitter], 71 and V will be maintained in [0,1]. 72 """ 73 # Initialise attributes 74 self.a = a 75 self.b = b 76 self.v_init = v_init 77 self.v_final = v_final 78 self.jitter = jitter
79
80 - def get_colors(self, k, offset=0.1):
81 """Generate k different RBG colours evenly-space on the spiral. 82 83 A generator returning the RGB colour space values for k 84 evenly-spaced points along the defined spiral in HSV space. 85 86 Arguments: 87 88 o k - the number of points to return 89 90 o offset - how far along the spiral path to start. 91 """ 92 # We use the offset to skip a number of similar colours near to HSV axis 93 assert offset > 0 and offset < 1, "offset must be in (0,1)" 94 v_rate = (self._v_final - self._v_init) / float(k) 95 # Generator for colours: we have divided the arc length into sections 96 # of equal length, and step along them 97 for n in range(1, k+1): 98 # For each value of n, t indicates the angle through which the 99 # spiral has turned, to this point 100 t = (1./self._b) * (log(n + (k * offset)) - 101 log((1 + offset) * k * self._a)) 102 # Put 0 <= h <= 2*pi, where h is the angular part of the polar 103 # co-ordinates for this point on the spiral 104 h = t 105 while h < 0: 106 h += 2 * pi 107 h = (h - (floor(h/(2 * pi)) * pi)) 108 # Now put h in [0, 1] for colorsys conversion 109 h = h / (2 * pi) 110 # r is the radial distance of this point from the centre 111 r = self._a * exp(self._b * t) 112 # v is the brightness of this point, linearly interpolated 113 # from self._v_init to self._v_final. Jitter size is sampled from 114 # a uniform distribution 115 if self._jitter: 116 jitter = random.random() * 2 * self._jitter - self._jitter 117 else: 118 jitter = 0 119 v = self._v_init + (n * v_rate + jitter) 120 # We have arranged the arithmetic such that 0 <= r <= 1, so 121 # we can use this value directly as s in HSV 122 yield colorsys.hsv_to_rgb(h, r, max(0, min(v, 1)))
123
124 - def _get_a(self):
125 return self._a
126
127 - def _set_a(self, value):
128 self._a = max(0, value)
129
130 - def _get_b(self):
131 return self._b
132
133 - def _set_b(self, value):
134 self._b = max(0, value)
135
136 - def _get_v_init(self):
137 return self._v_init
138
139 - def _set_v_init(self, value):
140 self._v_init = max(0, min(1, value))
141
142 - def _get_v_final(self):
143 return self._v_final
144
145 - def _set_v_final(self, value):
146 self._v_final = max(0, min(1, value))
147
148 - def _get_jitter(self):
149 return self._jitter
150
151 - def _set_jitter(self, value):
152 self._jitter = max(0, min(1, value))
153 154 a = property(_get_a, _set_a, 155 doc="Parameter controlling initial spiral direction (a > 0)") 156 b = property(_get_b, _set_b, 157 doc="Parameter controlling rate spiral revolves around axis (b > 0)") 158 v_init = property(_get_v_init, _set_v_init, 159 doc="Initial value of V (brightness) for the spiral (range 0 to 1)") 160 v_final = property(_get_v_final, _set_v_final, 161 doc="Final value of V (brightness) for the spiral (range 0 to 1)") 162 jitter = property(_get_jitter, _set_jitter, 163 doc="Degree of V (brightness) jitter to add to each color (range 0 to 1)")
164 165 166 # Convenience functions for those who don't want to bother with a 167 # ColorSpiral object
168 -def get_colors(k, **kwargs):
169 """Returns k colours selected by the ColorSpiral object, as a generator. 170 171 Arguments: 172 173 o k - the number of colours to return 174 175 o **kwargs - pass-through arguments to the ColorSpiral object 176 """ 177 cs = ColorSpiral(**kwargs) 178 return cs.get_colors(k)
179 180
181 -def get_color_dict(l, **kwargs):
182 """Returns a dictionary of colours using the provided values as keys. 183 184 Returns a dictionary, keyed by the members of iterable l, with a 185 colour assigned to each member. 186 187 Arguments: 188 189 o l - an iterable representing classes to be coloured 190 191 o **kwargs - pass-through arguments to the ColorSpiral object 192 """ 193 cs = ColorSpiral(**kwargs) 194 colors = cs.get_colors(len(l)) 195 dict = {} 196 for item in l: 197 dict[item] = next(colors) 198 return dict
199