21
21
the EstimateProbability class.
24
from __future__ import with_statement
24
from __future__ import with_statement, division
25
25
from cogent.util import parallel
26
26
from cogent.maths import optimisers
27
from cogent.util import progress_display as UI
30
__author__ = "Gavin Huttley and Andrew Butterfield"
31
__author__ = "Gavin Huttley, Andrew Butterfield and Peter Maxwell"
31
32
__copyright__ = "Copyright 2007-2009, The Cogent Project"
32
33
__credits__ = ["Gavin Huttley","Andrew Butterfield", "Matthew Wakefield",
33
34
"Edward Lang", "Peter Maxwell"]
34
35
__license__ = "GPL"
36
37
__maintainer__ = "Gavin Huttley"
37
38
__email__ = "gavin.huttley@anu.edu.au"
38
39
__status__ = "Production"
52
53
def setSeed(self, seed):
55
def run(self, show_progress=False, **opt_args):
57
def run(self, ui, **opt_args):
56
58
# Sets self.observed and self.results (a list _numreplicates long) to
57
59
# whatever is returned from self.simplify([LF result from each PC]).
58
60
# self.simplify() is used as the entire LF result might not be picklable
59
61
# for MPI. Subclass must provide self.alignment and
60
62
# self.parameter_controllers
61
alignment_randomness = random.Random()
62
alignment_randomness.seed(self.seed)
63
parallel.sync_random(alignment_randomness)
65
opt_args['show_progress'] = show_progress
66
63
if 'random_series' not in opt_args and not opt_args.get('local', None):
67
64
opt_args['random_series'] = random.Random()
71
for pc in self.parameter_controllers:
72
pc.setAlignment(self.alignment)
73
lf = pc.optimise(return_calculator=True, **opt_args)
74
starting_points.append(lf)
75
self._observed.append(lf)
76
self.observed = self.simplify(*self.parameter_controllers)
78
# making the simulated alignment on every node keeps alignment_randomness in sync
79
# across all cpus. This is only a good way of doing it so long as making the alignment
80
# is much faster than optimising the likelihood function.
81
simaligns = [self.parameter_controllers[0].simulateAlignment(
82
random_series=alignment_randomness) for i in range(self._numreplicates)]
84
def _one_replicate(simalign):
85
for (pc, starting_point) in zip(self.parameter_controllers, starting_points):
86
with pc.real_par_controller.updatesPostponed():
87
# using a calculator as a memo object to rest the parameter values
88
pc.real_par_controller.updateFromCalculator(starting_point)
89
# but alignment is different each time
90
pc.setAlignment(simalign)
91
# pc needs to be told that it can't use all the CPUs it did initially
92
# which seems less than ideal (it should be able to discover that itself)
93
pc.real_par_controller.setupParallelContext()
94
pc.optimise(**opt_args)
95
return self.simplify(*self.parameter_controllers)
97
self.results = parallel.map(_one_replicate, simaligns, show_progress=False)#opt shows anyway
66
null_pc = self.parameter_controllers[0]
67
pcs = len(self.parameter_controllers)
71
model_label = ['null', 'alt ']
73
model_label = ['null'] + ['alt%s'%i for i in range(1,pcs)]
76
def each_model(alignment, ui):
78
pc.setAlignment(alignment)
79
return pc.optimise(return_calculator=True, **opt_args)
80
memos = ui.eager_map(one_model, self.parameter_controllers,
82
concise_result = self.simplify(*self.parameter_controllers)
83
return (memos, concise_result)
85
#optimisations = pcs * (self._numreplicates + 1)
86
init_work = pcs / (self._numreplicates + pcs)
87
ui.display('Original data', 0.0, init_work)
88
(starting_points, self.observed) = each_model(self.alignment)
90
ui.display('Randomness', init_work, 0.0)
91
alignment_random_state = random.Random(self.seed).getstate()
93
comm = parallel.getCommunicator()
94
alignment_random_state = comm.bcast(alignment_random_state, 0)
97
for (pc, start_point) in zip(self.parameter_controllers, starting_points):
98
# using a calculator as a memo object to reset the params
99
pc.updateFromCalculator(start_point)
100
aln_rnd = random.Random(0)
101
aln_rnd.setstate(alignment_random_state)
102
aln_rnd.jumpahead(i*10**9)
103
simalign = null_pc.simulateAlignment(random_series=aln_rnd)
104
(dummy, result) = each_model(simalign)
107
ui.display('Bootstrap', init_work)
108
self.results = ui.eager_map(
109
one_replicate, range(self._numreplicates), noun='replicate',
100
113
class EstimateProbability(ParametricBootstrapCore):
101
114
# 2 parameter controllers, LR