/usr/lib/python3/dist-packages/ginga/ColorDist.py is in python3-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | #
# ColorDist.py -- Color Distribution algorithms
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
"""
These algorithms are modeled after the ones described for ds9 here:
http://ds9.si.edu/doc/ref/how.html
"""
import math
import numpy
class ColorDistError(Exception):
pass
class ColorDistBase(object):
def __init__(self, hashsize, colorlen=None):
super(ColorDistBase, self).__init__()
self.hashsize = hashsize
if colorlen is None:
colorlen = 256
self.colorlen = colorlen
self.maxhashsize = 1024*1024
# this actually holds the hash array
self.hash = None
self.calc_hash()
def hash_array(self, idx):
# NOTE: data could be assumed to be in the range 0..hashsize-1
# at this point but clip as a precaution
idx = idx.clip(0, self.hashsize-1)
arr = self.hash[idx]
return arr
def get_hash_size(self):
return self.hashsize
def set_hash_size(self, size):
assert (size >= self.colorlen) and (size <= self.maxhashsize), \
ColorDistError("Bad hash size!")
self.hashsize = size
self.calc_hash()
def check_hash(self):
hashlen = len(self.hash)
assert hashlen == self.hashsize, \
ColorDistError("Computed hash table size (%d) != specified size (%d)" % (hashlen, self.hashsize))
def calc_hash(self):
raise ColorDistError("Subclass needs to override this method")
def get_dist_pct(self, pct):
raise ColorDistError("Subclass needs to override this method")
class LinearDist(ColorDistBase):
"""
y = x
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None):
super(LinearDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
val = min(max(float(pct), 0.0), 1.0)
return val
def __str__(self):
return 'linear'
class LogDist(ColorDistBase):
"""
y = log(a*x + 1) / log(a)
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None, exp=1000.0):
self.exp = exp
super(LogDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
base = numpy.log(self.exp * base + 1.0) / numpy.log(self.exp)
base = base.clip(0.0, 1.0)
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
val_inv = (math.exp(pct * math.log(self.exp)) - 1) / self.exp
val = min(max(float(val_inv), 0.0), 1.0)
return val
def __str__(self):
return 'log'
class PowerDist(ColorDistBase):
"""
y = ((a ** x) - 1) / a
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None, exp=1000.0):
self.exp = exp
super(PowerDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
base = (self.exp ** base - 1.0) / self.exp
base = base.clip(0.0, 1.0)
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
val_inv = math.log(self.exp * pct + 1, self.exp)
val = min(max(float(val_inv), 0.0), 1.0)
return val
def __str__(self):
return 'power'
class SqrtDist(ColorDistBase):
"""
y = sqrt(x)
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None):
super(SqrtDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
base = numpy.sqrt(base)
base = base.clip(0.0, 1.0)
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
val_inv = pct ** 2.0
val = min(max(float(val_inv), 0.0), 1.0)
return val
def __str__(self):
return 'sqrt'
class SquaredDist(ColorDistBase):
"""
y = x ** 2
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None):
super(SquaredDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
base = (base ** 2.0)
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
val_inv = math.sqrt(pct)
val = min(max(float(val_inv), 0.0), 1.0)
return val
def __str__(self):
return 'squared'
class AsinhDist(ColorDistBase):
"""
y = asinh(nonlinearity * x) / factor
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None, factor=10.0,
nonlinearity=3.0):
self.factor = factor
self.nonlinearity = nonlinearity
super(AsinhDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
base = numpy.arcsinh(self.factor * base) / self.nonlinearity
base = base.clip(0.0, 1.0)
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
# calculate inverse of dist fn
val_inv = math.sinh(self.nonlinearity * pct) / self.factor
val = min(max(float(val_inv), 0.0), 1.0)
return val
def __str__(self):
return 'asinh'
class SinhDist(ColorDistBase):
"""
y = sinh(factor * x) / nonlinearity
where x in (0..1)
"""
def __init__(self, hashsize, colorlen=None, factor=3.0,
nonlinearity=10.0):
self.factor = factor
self.nonlinearity = nonlinearity
super(SinhDist, self).__init__(hashsize, colorlen=colorlen)
def calc_hash(self):
base = numpy.arange(0.0, float(self.hashsize), 1.0) / self.hashsize
base = numpy.sinh(self.factor * base) / self.nonlinearity
base = base.clip(0.0, 1.0)
# normalize to color range
l = base * (self.colorlen - 1)
self.hash = l.astype(numpy.uint)
self.check_hash()
def get_dist_pct(self, pct):
# calculate inverse of dist fn
val_inv = math.asinh(self.nonlinearity * pct) / self.factor
val = min(max(float(val_inv), 0.0), 1.0)
return val
def __str__(self):
return 'sinh'
class HistogramEqualizationDist(ColorDistBase):
"""
The histogram equalization distribution function distributes colors
based on the frequency of each data value.
"""
def __init__(self, hashsize, colorlen=None):
super(HistogramEqualizationDist, self).__init__(hashsize,
colorlen=colorlen)
def calc_hash(self):
pass
# TODO: this method has a lot more overhead compared to the other
# scaling methods because the hash array must be computed each time
# the data is delivered to hash_array()--in the other scaling
# methods it is precomputed in calc_hash(). Investigate whether
# there is a way to make this more efficient.
#
def hash_array(self, idx):
# NOTE: data could be assumed to be in the range 0..hashsize-1
# at this point but clip as a precaution
idx = idx.clip(0, self.hashsize-1)
#get image histogram
hist, bins = numpy.histogram(idx.flatten(),
self.hashsize, density=False)
cdf = hist.cumsum()
# normalize to color range
l = (cdf - cdf.min()) * (self.colorlen - 1) / (
cdf.max() - cdf.min())
self.hash = l.astype(numpy.uint)
self.check_hash()
arr = self.hash[idx]
return arr
def get_dist_pct(self, pct):
# TODO: this is wrong but we need a way to invert the hash
return pct
def __str__(self):
return 'histeq'
distributions = {
'linear': LinearDist,
'log': LogDist,
'power': PowerDist,
'sqrt': SqrtDist,
'squared': SquaredDist,
'asinh': AsinhDist,
'sinh': SinhDist,
'histeq': HistogramEqualizationDist,
}
def add_dist(name, distClass):
global distributions
distributions[name.lower()] = distClass
def get_dist_names():
a_names = set(distributions.keys())
std_names = ['linear', 'log', 'power', 'sqrt', 'squared', 'asinh', 'sinh',
'histeq']
rest = a_names - set(std_names)
if len(rest) > 0:
std_names = std_names + list(rest)
return std_names
def get_dist(name):
if not name in distributions:
raise ColorDistError("Invalid distribution algorithm '%s'" % (name))
return distributions[name]
#END
|