-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
101 lines (75 loc) · 2.4 KB
/
common.py
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
import inspect
import os.path
import matplotlib
import matplotlib.ticker
import numpy
import scipy.special
import seaborn
tmax = 100
t = numpy.linspace(0, tmax, 1001)
vi0 = 0.01
sensitivity_parameters = (
('rho', 'Vector birth rate\n$\\rho$ (d$^{-1}$)'),
('mu', 'Vector death rate\n$\\mu$ (d$^{-1}$)'),
('epsilon', 'Encounter rate\n$\\epsilon$ (d$^{-1}$)'),
('phi', 'Proportion feeding\n$\\phi$')
)
parameter_sets_ordered = ('Non-persistent', 'Persistent')
matplotlib.style.use('default')
matplotlib.rc('legend', frameon=False)
seaborn.set_palette(['black', 'darkgray'])
alpha = 0.9
baseline_style = dict(
linestyle = 'dotted',
color = 'black',
linewidth = matplotlib.rcParams['lines.linewidth'] / 2,
alpha = alpha / 2,
zorder = 1
)
def get_scale(param):
if param == 'phi':
return 'logit'
else:
return 'log'
def logitspace(start, stop, *args, **kwargs):
return scipy.special.expit(numpy.linspace(start, stop, *args, **kwargs))
sensitivity_max_abs_mult_change = 3
npoints = 201
def get_dPs(param, value_baseline):
scale = get_scale(param)
if scale in ('linear', 'logit'):
a = 0.1
b = 1 - a
assert a <= value_baseline <= b
if scale == 'linear':
return numpy.linspace(a, b, npoints)
else:
a, b = scipy.special.logit((a, b))
return logitspace(a, b, npoints)
elif scale == 'log':
a = numpy.log10(value_baseline / sensitivity_max_abs_mult_change)
b = numpy.log10(value_baseline * sensitivity_max_abs_mult_change)
return numpy.logspace(a, b, npoints)
else:
raise NotImplementedError('scale = {}'.format(scale))
def style_axis(ax):
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(20))
def _get_filebase():
stack = inspect.stack()
caller = stack[2]
_, basename = os.path.split(caller.filename)
filebase, _ = os.path.splitext(basename)
return filebase
def savefig(fig, append = '', format_ = 'pdf', *args, **kwargs):
filebase = _get_filebase()
outfile = '{}{}.{}'.format(filebase, append, format_)
fig.savefig(outfile, *args, **kwargs)
def load_or_build_data(builder):
filebase = _get_filebase()
savefile = '{}.npy'.format(filebase)
try:
retval = numpy.load(savefile)
except FileNotFoundError:
retval = builder()
numpy.save(savefile, retval)
return retval