Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
181 lines (140 loc) · 4.43 KB
/
Copy pathutils.py
File metadata and controls
181 lines (140 loc) · 4.43 KB
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
"""This module contains various utility functions. The rule of thumb
for inclusion in this module is that the function could conceivably be
used in an entirely unrelated project."""
import os
from math import *
import random
iota = lambda x:x #it is sometimes useful to have the identity
#function lying around
def sort_on(xs, ys, f):
"""Given f: x -> y, sort xs by corresponding ys"""
zs = [(x, index(ys, lambda y: y == f(x))) for x in xs]
return [tup[0] for tup in sorted(zs, key=lambda tup:tup[1])]
def head(xs, p=iota):
"""Take first element of xs, optionally satisfying predicate p"""
filtered_xs = filter(p, xs)
return filtered_xs[0] if filtered_xs else []
def index(xs, p):
"""Return index of first x satisfying p, or None if none do"""
winners = filter(lambda (i, x): p(x), zip(range(len(xs)), xs))
return winners[0][0] if winners else None
def choose2(xs):
"""return list of choose(xs, 2) pairs, retaining ordering on xs"""
return [(x1, x2) for i, x1 in enumerate(xs) for x2 in xs[i+1:]]
def zipWith(f, xs, ys):
"""Return list of form [f(x, y)]"""
return map(lambda (x, y): f(x, y), zip(xs, ys))
def mean(xs):
if xs:
return float(sum(xs))/len(xs)
else:
return None
def geo_mean(xs):
if xs:
return product(xs) ** (1/float(len(xs)))
def verbose_gen(xs,n=None):
for i, x in enumerate(xs):
if not n or i % n == 0:
print i
yield x
def cart_product(xs, ys):
return [(x, y) for x in xs for y in ys]
def normalize(xs):
total = float(sum(xs))
return [x/total for x in xs]
def percentile(p, xs, upper=True):
"""Find the n% percentile cutoff for xs, i.e. smallest y such that
P(x > y) <= n%"""
zs = sorted(xs, reverse=not upper)
n = len(zs)
i = int(n * p)
return zs[i]
def truncate(x,places=2):
return int(x * 10**places)/float(10**places)
def mapmap(f,xxs):
return [map(f,xs) for xs in xxs]
def variance(xs):
return mean([x**2 for x in xs]) - mean(xs)**2
def sd(xs):
print xs
if xs:
return sqrt(mean([x**2 for x in xs]) - mean(xs)**2)
else:
return None
def se(xs):
if xs:
return sd(xs)/sqrt(len(xs))
else:
return None
def fano(xs):
return variance(xs)/mean(xs)
def dictmap(d,xs):
"""Map d over xs"""
return [d[x] for x in xs]
def pearson(x, y):
return scipy.stats.pearsonr(x, y)[0]
def spearman(x, y):
return scipy.stats.spearmanr(x, y)[0]
def l2(x,y):
return sum(zipWith(lambda u,v: (u-v)**2,x,y))
def length(x, y):
return len(x)
def get_name(obj):
return head(dir(),lambda name: eval(name) == obj)
print("loaded utils")
def maxZip(xs,ys):
"""Zip, padding the shorter list with Nones"""
xlen = len(xs)
ylen = len(ys)
return zip(xs + [None for i in range(ylen - xlen)],
ys + [None for i in range(ylen - xlen)])
def transpose(xxs):
"""Transpose a list of the form [[a1,a2...],[b1,b2..]...] into a
list of the form [[a1,b1,...],[a2,b2,...]...]"""
return zip(*xxs)
def group_codons(seq):
return [seq[i*3:(i+1)*3] for i in range(len(seq)/3)]
def product(xs):
return reduce(lambda x,y: x*y,xs)
def reverse(xs):
return xs[::-1]
def org_matches_dir(org, org_dir):
"""Returns whether org_dir is the org_dir of org"""
return all(word.lower() in org_dir.lower() for word in org.split('_'))
def concat(xxs):
return sum(xxs,[])
def plurality(xs):
"""Return the most frequently appearing x in xs"""
return head(head(Counter(xs).most_common()))
def sample(xs,probs):
probs = normalize(probs)
partials = [sum(probs[:i+1]) for i in range(len(probs))]
r = random.random()
i = index(partials,lambda x: x > r)
return xs[i]
def permute(xs):
ys = xs[:]
random.shuffle(ys)
return ys
def base(b,n): #used for 1-based indexing in R
return choose2(range(1,b + 1))[n - 1]
def pairs(xs):
return zip([None] + xs,xs+[None])[1:len(xs)]
def ceiling(x):
if x == floor(x):
return x
else:
return floor(x + 1)
def chop(xs,k):
"""chop xs into k (approximately equal) contiguous sublists"""
n = len(xs)
#n = dk + r; last r items get spread out into last sublists
d = n // k
r = n % k
print r,d
xxs = []
for i in range(k):
xxs.append([xs.pop() for j in range(d + (i < r))])
return xxs
def rollmean(xs,n):
return [mean(xs[i:i+n]) for i in range(len(xs)-n +1)]