-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
460 lines (403 loc) · 15.7 KB
/
utilities.py
File metadata and controls
460 lines (403 loc) · 15.7 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
"""
.. module:: utilities
:platform: Unix
:synopsis: Utility functions
.. moduleauthor:: Matthias Flor <matthias.c.flor@gmail.com>
"""
import numpy as np
from numpy import sum
import matplotlib.pyplot as plt
import pandas as pd
from pprint import PrettyPrinter
import time, datetime, uuid, sys, pdb
from IPython.core.display import HTML, Javascript, display
try:
from IPython.core.display import clear_output
have_ipython = True
except ImportError:
have_ipython = False
def version_check(installed, required):
if np.size(installed) == 1:
return np.alltrue(installed >= required)
elif installed[0] > required[0]:
return True
elif installed[0] < required[0]:
return False
else:
return version_check(installed[1:], required[1:])
def ProgressBar(endval, indval=None, progress_type='linear'):
if progress_type in ['linear', 'lin']:
return LinearProgressBar(endval)
elif progress_type in ['logarithmic', 'log']:
return LogProgressBar(endval, indval)
class LinearProgressBar:
"""
A simple progress bar that should work reasonably well in an ipython
notebook.
Taken from the pymc package, slightly modified.
"""
def __init__(self, g_max):
self.g_max = g_max
self.prog_bar = '[]'
self.fill_char = '*'
self.width = 40
self.__update_amount(0)
if have_ipython:
self.animate = self.animate_ipython
else:
self.animate = self.animate_noipython
def animate_noipython(self, g, diff=None):
if sys.platform.lower().startswith('win'):
print self, '\r',
else:
print self, chr(27) + '[A'
self.update_iteration(g)
# time.sleep(0.5)
def animate_ipython(self, g, diff=None):
clear_output()
print '\r', self,
sys.stdout.flush()
self.update_iteration(g, diff)
def update_iteration(self, g, diff=None):
self.__update_amount((g / float(self.g_max)) * 100.0)
self.prog_bar += ' gen. %d of %s (max)' % (g, self.g_max)
if diff:
self.prog_bar += ' | %.4g' % diff
def __update_amount(self, new_amount):
percent_done = int(round((new_amount / 100.0) * 100.0))
all_full = self.width - 2
num_hashes = int(round((percent_done / 100.0) * all_full))
self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
pct_string = '%d%%' % percent_done
self.prog_bar = self.prog_bar[0:pct_place] + \
(pct_string + self.prog_bar[pct_place + len(pct_string):])
def __str__(self):
return str(self.prog_bar)
class LogProgressBar:
"""
A simple progress bar that should work reasonably well in an ipython
notebook.
Taken from the pymc package, slightly modified.
"""
def __init__(self, threshold_total, threshold_individual=None):
self.threshold_total = threshold_total
self.log_thresh = (-1)*np.log10(threshold_total)
self.threshold_individual = threshold_individual
self.prog_bar = '[]'
self.fill_char = '*'
self.width = 40
self.__update_amount(0)
if have_ipython:
self.animate = self.animate_ipython
else:
self.animate = self.animate_noipython
def animate_noipython(self, g, diff, ind_diff_max=None):
if sys.platform.lower().startswith('win'):
print self, '\r',
else:
print self, chr(27) + '[A'
self.update_iteration(g, diff)
# time.sleep(0.5)
def animate_ipython(self, g, diff, ind_diff_max=None):
clear_output()
print '\r', self,
sys.stdout.flush()
self.update_iteration(g, diff, ind_diff_max)
def update_iteration(self, g, diff, ind_diff_max=None):
if diff == 0.: # prevent log(0) problem
self.__update_amount(100.)
else:
log_diff = (-1)*np.log10(diff)
self.__update_amount( min(100., (log_diff / float(self.log_thresh)) * 100.0) )
if ind_diff_max is not None:
self.prog_bar += ' %.2g [%-.2g] | %.2g [%-.2g] | %d' % (diff, self.threshold_total, ind_diff_max, self.threshold_individual, g)
else:
self.prog_bar += ' %.2g [%-.2g] | %d' % (diff, self.threshold_total, g)
def __update_amount(self, new_amount):
percent_done = int(round((new_amount / 100.0) * 100.0))
all_full = self.width - 2
num_hashes = int(round((percent_done / 100.0) * all_full))
self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
pct_string = '%d%%' % percent_done
self.prog_bar = self.prog_bar[0:pct_place] + \
(pct_string + self.prog_bar[pct_place + len(pct_string):])
def __str__(self):
return str(self.prog_bar)
def loci2string(loci, alleles):
loci = ['locus'] + loci
alleles = ['alleles'] + [', '.join(row) for row in alleles] # turn list of alleles into a list of strings
w1 = len(max(loci, key=len)) # max locus width
w2 = len(max(alleles, key=len))
ret = "%-*s %-*s\n" % (w1, loci[0], w2, alleles[0]) # header row
ret += '-'*(w1+6+w2)+'\n'
for loc,allele in zip(loci[1:], alleles[1:]):
ret += "%-*s %-*s\n" % (w1, loc, w2, allele)
return ret.rstrip()
def params2string(params):
names = ['parameter'] + sorted(params.keys())
values, descriptions = ['value'], ['description']
for name in names[1:]:
v,d = params[name]
values.append(v)
descriptions.append(d)
values = [str(v) for v in values]
w1 = len(max(names, key=len))
w2 = len(max(values, key=len))
w3 = len(max(descriptions, key=len))
ret = "%-*s %-*s %-*s\n" % (w1, names[0], w2, values[0], w3, descriptions[0])
ret += '-'*(w1+4+w2+6+w3)+'\n'
for name,value,desc in zip(names[1:], values[1:], descriptions[1:]):
ret += "%-*s %-*s %-*s\n" % (w1, name, w2, value, w3, desc)
return ret.rstrip()
def add_preferences(params, prefs):
for pref,vdict in sorted(prefs.items()):
for cue,val in sorted(vdict.items()):
params['pr_{0}_{1}'.format(pref,cue).lower()] = (val, 'rejection probability')
return params
def configure_locals(LOCI, ALLELES, parameters):
config = {}
config['LOCI'] = LOCI
config['ALLELES'] = ALLELES
config['ADICT'] = make_allele_dictionary(LOCI, ALLELES)
#~ config['LABELS'] = panda_index(ALLELES, LOCI) # use this as index for conversion of freqs to pd.Series
config['FSHAPE'] = list_shape(ALLELES) # shape of frequencies
repro_axes = reproduction_axes(LOCI)
config['REPRO_AXES'] = repro_axes # axes for the reproduction step, used for automatic extension of arrays to the correct shape by inserting np.newaxis
config['N_LOCI'] = len(LOCI)
pops = ALLELES[0]
config['POPULATIONS'] = pops # shortcut for faster access to populations
config['N_POPS'] = len(pops) # number of populations within metapopulations
config['REPRO_DIM'] = len(repro_axes)
for name,(value,desc) in parameters.items():
config[name] = value
return config
def timing_report(starttime, generation):
s = 'Simulation run completed.\n'
seconds = time.time()-starttime
hhmmss = str(datetime.timedelta(seconds=int(seconds)))
s += 'Generation: {0}\nElapsed Time (hours:minutes:seconds): {1}\n'.format(generation, hhmmss)
pergen = seconds / generation
#~ hhmmss = str(datetime.timedelta(seconds=int(pergen)))
s += 'Time per generation (seconds): %.2g' % pergen
return s
def list_shape(list2d):
"""
Return the `shape` of a 2-dimensional nested list.
Args:
list2d: nested list
Returns:
out: shape tuple
"""
shape = [0 for i in range(len(list2d))]
for i,l in enumerate(list2d):
shape[i] = len(l)
return tuple(shape)
def extend(arr, dim, pos):
"""
Broadcast array `arr` to new extended dimension `dim`.
This is achieved by inserting the appropriate number of new axes.
The original axes of `arr` become positioned at `pos`. Thus, the
list `pos` must have length equal to `arr.ndim`.
Args:
arr: ndarray
dim: int
pos: int or list of ints
Returns:
out: ndarray
"""
if isinstance(arr, float):
return arr
indexer = [np.newaxis] * dim
if isinstance(pos,int): pos = [pos] # enable passing of a single `int` position
for p in pos:
indexer[p] = slice(None)
return arr[indexer]
def sum_along_axes(arr, axes, squeeze_first=True):
"""
Sum along multiple axes.
Args:
arr: ndarray
Input array.
axes: integer or list of integers
Axes along which `arr` is summed.
Returns:
out: ndarray
Output array. The shape of `out` is identical to the
shape of `arr` along `axes`.
"""
if isinstance(axes,int): axes = [axes] # enable passing of a single int axis
_axes = range(arr.ndim)
for a in axes: _axes.remove(a)
if not squeeze_first:
return np.array( [np.apply_over_axes(sum, arr, _axes).squeeze()] )
return np.apply_over_axes(sum, arr, _axes).squeeze() # apply_over_axes outputs the same dimension as the input
def sum_over_axes(arr, axes):
if isinstance(axes,int): axes = [axes] # enable passing of a single int axis
return np.apply_over_axes(sum, arr, axes).squeeze()
def panda_index(labels, names=None, dtype='|S10'):
"""
Create a pandas.MultiIndex with row names contained in the nested
list `labels` and column names contained in the optional list
`names`.
Args:
labels: nested list of strings
names: list of strings
Example usage:
>>> labels = [['wine','water','beer'], [0.2','0.5'], ['to go','for here']]
>>> names = ['beverage','size','order']
>>> index = make_index(labels,names)
>>> index
"""
if names==None:
names = ['axis{0}'.format(i) for i in range(len(labels))]
else:
assert len(labels)==len(names)
sh = list_shape(labels)
n_axes = len(labels)
n_total = np.prod(sh)
ctile = np.concatenate( ([1],np.cumprod(sh)[:-1]) )
crep = np.concatenate( (np.cumprod(sh[::-1])[:-1][::-1],[1]) )
replabels = np.empty((n_axes,n_total), dtype=dtype)
for i,l in enumerate(labels):
replabels[i] = np.tile( np.repeat(l,crep[i]), ctile[i] )
tuples = zip(*replabels)
return pd.MultiIndex.from_tuples(tuples, names=names)
def myfloat(x, threshold=1e-4, absolute_threshold=1e-10):
import pandas as pd
if x < absolute_threshold: return ' ---'
elif x < threshold: return ' 0.0'
else: return '%.4f' % x
try:
pd.set_option('display.float_format',myfloat)
except:
pd.set_printoptions(precision=5)
class MyPrettyPrinter(PrettyPrinter):
def format(self, object, context, maxlevels, level):
if isinstance(object, float):
return ('%.4f' % object), True, False
else:
return PrettyPrinter.format(self, object, context,
maxlevels, level)
def make_allele_dictionary(loci, alleles):
adict = {} # dictionary for allele name to index conversion
for i,locus in enumerate(alleles):
for allele in locus:
adict[allele] = (i,locus.index(allele))
return adict
def reproduction_axes(loci, who=['female','male','offspring']):
"""
Create a list of reproduction axes names.
Args:
loci: list of strings
names of loci
who: list of strings
Can't really think of anything else than the default
that would make sense here.
Returns:
out: list of strings
"""
if isinstance(who, str):
who = [who]
return [loci[0]] + ["{0}_{1}".format(i, locus) for i in who for locus in loci[1:]]
def nuclear_inheritance(n1, n2=None, r=0.5):
if n2 is not None:
return nuclear_inheritance_at_two_loci(n1=n1, n2=n2, r=r)
else:
return nuclear_inheritance_at_single_locus(n1)
def nuclear_inheritance_at_single_locus(n):
"""Returns an array for the inheritance at a nuclear locus with n alleles."""
ary = np.zeros((n,n,n))
for female in range(n):
for male in range(n):
for offspring in range(n):
if female==male==offspring:
ary[female,male,offspring]=1.
if female!=male:
if (offspring==female) or (offspring==male):
ary[female,male,offspring]=0.5
return ary
def nuclear_inheritance_at_two_loci(n1,n2,r):
ary = np.zeros( (n1,n2, n1,n2, n1,n2), float )
for i in range(n1):
for j in range(n2):
for k in range(n1):
for l in range(n2):
for m in range(n1):
for n in range(n2):
if i==k==m and j==l==n:
ary[i,j,k,l,m,n] = 1.
if i==k==m and j!=l:
if j==n or l==n:
ary[i,j,k,l,m,n] = 0.5
if i!=k and j==l==n:
if i==m or k==m:
ary[i,j,k,l,m,n] = 0.5
if i!=k and j!=l:
if (i==m and j==n) or (k==m and l==n):
ary[i,j,k,l,m,n] = 0.5 * (1-r)
elif (i==m and l==n) or (k==m and j==n):
ary[i,j,k,l,m,n] = 0.5 * r
return ary
def arrdiff(a, b):
"""
Sum over absolute differences between two arrays `a` and `b`.
Args:
a, b: ndarrays
Returns:
out: float
"""
d = np.abs(a-b)
return d, np.sum(d)
def make_reproduction_allele_names(axes, config):
"""
Args:
axes: list of strings
config: dict
Returns:
out: nested list of strings
"""
loci = config['LOCI']
alleles = config['ALLELES']
if axes[0] == 'population':
result = alleles[:1]
axes = axes[1:]
else:
result = []
for ax in axes:
who,locus = ax.split('_') # `who`: 'female', 'male', or 'offspring'
w = who[0] # take first letter, i.e. 'f', 'm', or 'o'
als = alleles[loci.index(locus)]
result.append( ["{0}{1}".format(w,a) for a in als] )
return result
def get_alleles(loci, config):
"""
Args:
loci: list of stings
config: dict
Returns:
out: nested list of ints
"""
return [config['ALLELES'][config['LOCI'].index(locus)] for locus in loci]
def parameters_equal(pdict1, pdict2, verbose=True):
pnames1 = sorted(pdict1.keys())
pnames2 = sorted(pdict2.keys())
if not pnames1 == pnames2:
if verbose:
print 'parameter names not the same\n1: %s\n2: %s' % (str(pnames1), str(pnames2))
return False
for pname in pnames1:
p,d = pdict1[pname]
pval1,pdesc1 = np.array(p), str(d)
p,d = pdict2[pname]
pval2,pdesc2 = np.array(p), str(d)
if not np.allclose(pval1,pval2):
if verbose:
print '`%s` parameter: values not the same\n1: %s\n2: %s' % (pname, str(pval1), str(pval2))
return False
if not pdesc1 == pdesc2:
if verbose:
print '`%s` parameter: descriptions not the same\n1: %s\n2: %s' % (pname, str(pdesc1), str(pdesc2))
return False
return True