forked from argriffing/xgcode
/
20111202a.py
207 lines (188 loc) · 6.39 KB
/
20111202a.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
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
"""
Plot expected log likelihood ratios over time.
"""
from StringIO import StringIO
import numpy as np
import scipy
from scipy import linalg
import Form
import FormOut
from MatrixUtil import ndot
import mrate
import ctmcmi
import RUtil
UNIFORM = 'uniform'
ONE_INC = 'one_big'
TWO_INC = 'two_big'
ONE_DEC = 'one_small'
TWO_DEC = 'two_small'
BALANCED = 'balanced'
HALPERN_BRUNO = 'halpern_bruno'
g_mode_to_color = {
UNIFORM : 'black',
ONE_INC : 'red',
TWO_INC : 'orange',
ONE_DEC : 'green',
TWO_DEC : 'blue',
}
g_ordered_modes = (
UNIFORM,
ONE_INC,
TWO_INC,
ONE_DEC,
TWO_DEC,
)
TOPLEFT = 'topleft'
BOTTOMLEFT = 'bottomleft'
TOPRIGHT = 'topright'
BOTTOMRIGHT = 'bottomright'
def get_form():
"""
@return: the body of a form
"""
# define the form objects
form_objects = [
Form.Integer('nstates', 'number of states', 4, low=2, high=9),
Form.Float('sel_surr',
'unitless deviation from uniformity (between 0 and 1)',
'0.95', low_inclusive=0, high_exclusive=1),
Form.FloatInterval(
't_low', 't_high', 'divtime interval',
'0', '0.8', low_inclusive=0, low_width_exclusive=0),
Form.RadioGroup('selection', 'selection approximation', [
Form.RadioItem(BALANCED, 'WAG-like f=1/2'),
Form.RadioItem(HALPERN_BRUNO, 'Halpern-Bruno', True)]),
Form.CheckGroup('distribution', 'stationary distribution', [
Form.CheckItem(UNIFORM, UNIFORM, True),
Form.CheckItem(ONE_INC, ONE_INC.replace('_', ' '), True),
Form.CheckItem(TWO_INC, TWO_INC.replace('_', ' '), True),
Form.CheckItem(ONE_DEC, ONE_DEC.replace('_', ' '), True),
Form.CheckItem(TWO_DEC, TWO_DEC.replace('_', ' '))]),
Form.RadioGroup('legend_placement', 'plot legend location', [
Form.RadioItem(TOPLEFT, 'top left'),
Form.RadioItem(BOTTOMLEFT, 'bottom left'),
Form.RadioItem(TOPRIGHT, 'top right', True),
Form.RadioItem(BOTTOMRIGHT, 'bottom right')]),
Form.ImageFormat()]
return form_objects
def get_form_out():
return FormOut.Image('plot')
def get_distn_uniform(n, t):
v = np.ones(n, dtype=float)
return v / np.sum(v)
def get_distn_one_inc(n, t):
v = (1-t) * np.ones(n, dtype=float)
v[0] = 1
return v / np.sum(v)
def get_distn_two_inc(n, t):
v = (1-t) * np.ones(n, dtype=float)
v[0] = 1
v[1] = 1
return v / np.sum(v)
def get_distn_one_dec(n, t):
v = np.ones(n, dtype=float)
v[0] -= t
return v / np.sum(v)
def get_distn_two_dec(n, t):
v = np.ones(n, dtype=float)
v[0] -= t
v[1] -= t
return v / np.sum(v)
def make_table(args, distn_modes):
"""
Make outputs to pass to RUtil.get_table_string.
@param args: user args
@param distn_modes: ordered distribution modes
@return: matrix, headers
"""
# define some variables
t_low = args.t_low
t_high = args.t_high
if t_high <= t_low:
raise ValueError('low time must be smaller than high time')
ntimes = 100
incr = (t_high - t_low) / (ntimes - 1)
n = args.nstates
# define some tables
distn_mode_to_f = {
UNIFORM : get_distn_uniform,
ONE_INC : get_distn_one_inc,
TWO_INC : get_distn_two_inc,
ONE_DEC : get_distn_one_dec,
TWO_DEC : get_distn_two_dec}
selection_mode_to_f = {
BALANCED : mrate.to_gtr_balanced,
HALPERN_BRUNO : mrate.to_gtr_halpern_bruno}
# define the selection modes and calculators
selection_f = selection_mode_to_f[args.selection]
distn_fs = [distn_mode_to_f[m] for m in distn_modes]
# define the headers
headers = ['t'] + [s.replace('_', '.') for s in distn_modes]
# define the numbers in the table
S = np.ones((n, n), dtype=float)
S -= np.diag(np.sum(S, axis=1))
arr = []
for i in range(ntimes):
t = t_low + i * incr
row = [t]
for distn_f in distn_fs:
v = distn_f(n, args.sel_surr)
R = selection_f(S, v)
expected_log_ll_ratio = ctmcmi.get_expected_ll_ratio(R, t)
row.append(expected_log_ll_ratio)
arr.append(row)
return np.array(arr), headers
def get_response_content(fs):
distn_modes = [x for x in g_ordered_modes if x in fs.distribution]
if not distn_modes:
raise ValueError('no distribution mode was specified')
colors = [g_mode_to_color[m] for m in distn_modes]
arr, headers = make_table(fs, distn_modes)
distn_headers = headers[1:]
# Get the largest value in the array,
# skipping the first column.
arrmax = np.max(arr[:,1:])
# write the R script body
out = StringIO()
ylim = RUtil.mk_call_str('c', 0, arrmax + 0.1)
sel_str = {
BALANCED : 'f=1/2',
HALPERN_BRUNO : 'Halpern-Bruno',
}[fs.selection]
print >> out, RUtil.mk_call_str(
'plot',
'my.table$t',
'my.table$%s' % distn_headers[0],
type='"n"',
ylim=ylim,
xlab='"time"',
ylab='"expected log L-ratio"',
main='"Effect of selection (%s) on log L-ratio for %d states"' % (sel_str, fs.nstates),
)
for c, header in zip(colors, distn_headers):
print >> out, RUtil.mk_call_str(
'lines',
'my.table$t',
'my.table$%s' % header,
col='"%s"' % c,
)
mode_names = [s.replace('_', ' ') for s in distn_modes]
legend_name_str = 'c(' + ', '.join('"%s"' % s for s in mode_names) + ')'
legend_col_str = 'c(' + ', '.join('"%s"' % s for s in colors) + ')'
legend_lty_str = 'c(' + ', '.join(['1']*len(distn_modes)) + ')'
print >> out, RUtil.mk_call_str(
'legend',
'"%s"' % fs.legend_placement,
legend_name_str,
col=legend_col_str,
lty=legend_lty_str,
)
script_body = out.getvalue()
# create the R plot image
table_string = RUtil.get_table_string(arr, headers)
device_name = Form.g_imageformat_to_r_function[fs.imageformat]
retcode, r_out, r_err, image_data = RUtil.run_plotter(
table_string, script_body, device_name)
if retcode:
raise RUtil.RError(r_err)
return image_data