/usr/lib/python2.7/dist-packages/pebl/util.py is in python-pebl 1.0.2-3.
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 | """Miscellaneous utility functions."""
import numpy as N
import math
import os.path
from copy import copy
from collections import deque
def as_list(c):
"""Ensures that the result is a list.
If input is a list/tuple/set, return it.
If it's None, return empty list.
Else, return a list with input as the only element.
"""
if isinstance(c, (list,tuple,set)):
return c
elif c is None:
return []
else:
return [c]
def cond(condition, expr1, expr2):
"""Marked for deletion.. Python2.5 provides this."""
if condition:
return expr1
else:
return expr2
def flatten(seq):
"""Given a nested datastructure, flatten it."""
lst = []
for el in seq:
if type(el) in [list, tuple, set]:
lst.extend(flatten(el))
else:
lst.append(el)
return lst
def normalize(lst):
"""Normalizes a list of numbers (sets sum to 1.0)."""
if not isinstance(lst, N.ndarray):
lst = N.array(lst)
return lst/lst.sum()
def rescale_logvalues(lst):
"""Rescales a list of log values by setting max value to 0.0
This function is necessary when working with list of log values. Without
it, we could have overflows. This is a lot faster than using arbitrary
precision math libraries.
"""
if not isinstance(lst, N.ndarray):
lst = N.array(lst)
return lst - lst.max()
_LogZero = 1.0e-100
_MinLogExp = math.log(_LogZero);
def logadd(x, y):
"""Adds two log values.
Ensures accuracy even when the difference between values is large.
"""
if x < y:
temp = x
x = y
y = temp
z = math.exp(y - x)
logProb = x + math.log(1.0 + z)
if logProb < _MinLogExp:
return _MinLogExp
else:
return logProb
def logsum(lst):
"""Sums a list of log values, ensuring accuracy."""
if not isinstance(lst, N.ndarray):
lst = N.array(lst)
maxval = lst.max()
lst = lst - maxval
return reduce(logadd, lst) + maxval
## from webpy (webpy.org)
def autoassign(self, locals):
"""
Automatically assigns local variables to `self`.
Generally used in `__init__` methods, as in::
def __init__(self, foo, bar, baz=1):
autoassign(self, locals())
"""
for (key, value) in locals.iteritems():
if key == 'self':
continue
setattr(self, key, value)
def unzip(l, *jj):
"""Opposite of zip().
jj is a tuple of list indexes (or keys) to extract or unzip. If not
specified, all items are unzipped.
"""
if jj==():
jj=range(len(l[0]))
rl = [[li[j] for li in l] for j in jj] # a list of lists
if len(rl)==1:
rl=rl[0] #convert list of 1 list to a list
return rl
def nestediter(lst1, lst2):
"""A syntactic shortform for doing nested loops."""
for i in lst1:
for j in lst2:
yield (i,j)
def cartesian_product(list_of_lists):
"""Given n lists (or sets), generate all n-tuple combinations.
>>> list(cartesian_product([[0,1], [0,1,"foo"]]))
[(0, 0), (0, 1), (0, 'foo'), (1, 0), (1, 1), (1, 'foo')]
>>> list(cartesian_product([[0,1], [0,1], [0,1]]))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
"""
head,rest = list_of_lists[0], list_of_lists[1:]
if len(rest) is 0:
for val in head:
yield (val,)
else:
for val in head:
for val2 in cartesian_product(rest):
yield (val,) + val2
def probwheel(items, weights):
"""Randomly select an item from a weighted list of items."""
# convert to numpy array and normalize
weights = normalize(N.array(weights))
# edges for bins
binedges = weights.cumsum()
randval = N.random.random()
for item, edge in zip(items, binedges):
if randval <= edge:
return item
# should never reach here.. but might due to rounding errors.
return items[-1]
def logscale_probwheel(items, logweights):
"""Randomly select an item from a [log] weighted list of items.
Fucntion just rescale logweights and exponentiates before calling
probwheel.
"""
return probwheel(items, N.exp(rescale_logvalues(logweights)))
def entropy_of_list(lst):
"""Given a list of values, generate histogram and calculate the entropy."""
unique_values = N.unique(lst)
unique_counts = N.array([float(len([i for i in lst if i == unique_val])) for unique_val in unique_values])
total = N.sum(unique_counts)
probs = unique_counts/total
# remove probabilities==0 because log(0) = -Inf and causes problems.
# This is ok because p*log(p) == 0*log(0) == 0 so removing these doesn't affect the final sum.
probs = probs[probs>0]
return sum(-probs*N.log(probs))
def edit_distance(network1, network2):
"""Returns the edit distance between two networks.
This is a good (but not the only one) metric for determining similarity
between two networks.
"""
def inverse(edge):
return (edge[1], edge[0])
edges1 = copy(list(network1.edges))
edges2 = copy(list(network2.edges))
# Calculating distance:
# Add 1 to distance for every edge in one network but not in the other,
# EXCEPT, if inverse of edge exists in the other network, distance is
# 1 not 2 (this is because edit operations include add, remove and reverse)
dist = 0
for edge in edges1:
if edge in edges2:
edges2.remove(edge)
elif inverse(edge) in edges2:
dist += 1
edges2.remove(inverse(edge))
else:
dist += 1
# edges2 now contains all edges not in edges1.
dist += len(edges2)
return dist
def levenshtein(a,b):
"""Calculates the Levenshtein distance between *strings* a and b.
from http://hetland.org/coding/python/levenshtein.py
"""
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a,b = b,a
n,m = m,n
current = range(n+1)
for i in range(1,m+1):
previous, current = current, [i]+[0]*n
for j in range(1,n+1):
add, delete = previous[j]+1, current[j-1]+1
change = previous[j-1]
if a[j-1] != b[i-1]:
change = change + 1
current[j] = min(add, delete, change)
return current[n]
def extended_property(func):
"""Function decorator for defining property attributes
The decorated function is expected to return a dictionary
containing one or more of the following pairs:
* fget - function for getting attribute value
* fset - function for setting attribute value
* fdel - function for deleting attribute
"""
return property(doc=func.__doc__, **func())
def lru_cache(maxsize):
'''Decorator applying a least-recently-used cache with the given maximum size.
Arguments to the cached function must be hashable.
Cache performance statistics stored in f.hits and f.misses.
from http://code.activestate.com/recipes/498245/
'''
def decorating_function(f):
cache = {} # mapping of args to results
queue = deque() # order that keys have been accessed
refcount = {} # number of times each key is in the access queue
def wrapper(*args):
# localize variable access (ugly but fast)
_cache=cache; _len=len; _refcount=refcount; _maxsize=maxsize
queue_append=queue.append; queue_popleft = queue.popleft
# get cache entry or compute if not found
try:
result = _cache[args]
wrapper.hits += 1
except KeyError:
result = _cache[args] = f(*args)
wrapper.misses += 1
# record that this key was recently accessed
queue_append(args)
_refcount[args] = _refcount.get(args, 0) + 1
# Purge least recently accessed cache contents
while _len(_cache) > _maxsize:
k = queue_popleft()
_refcount[k] -= 1
if not _refcount[k]:
del _cache[k]
del _refcount[k]
# Periodically compact the queue by duplicate keys
if _len(queue) > _maxsize * 4:
for i in [None] * _len(queue):
k = queue_popleft()
if _refcount[k] == 1:
queue_append(k)
else:
_refcount[k] -= 1
assert len(queue) == len(cache) == len(refcount) == sum(refcount.itervalues())
return result
wrapper.__doc__ = f.__doc__
wrapper.__name__ = f.__name__
wrapper.hits = wrapper.misses = 0
return wrapper
return decorating_function
|