IMP  2.1.0
The Integrative Modeling Platform
domino/interactive.py

IMP::domino::DominoSampler supports an interactive mode where by each step of the sampling process is called explicitly and all intermediate results are exposed. This usage mode can be used to help understand how domino behaves as well as to distribute a domino computation to multiple cores or multiple machines. For the latter, it is useful to use the IMP::domino::get_assignments() and IMP::domino::set_assignments() functions to save the states to a file.

1 ## \example domino/interactive.py
2 # IMP::domino::DominoSampler supports an interactive mode where by each step of the sampling process is called explicitly and all intermediate results are exposed. This usage mode can be used to help understand how domino behaves as well as to distribute a domino computation to multiple cores or multiple machines. For the latter, it is useful to use the IMP::domino::get_assignments() and IMP::domino::set_assignments() functions to save the states to a file.
3 #
4 
5 import IMP.domino
6 import IMP.algebra
7 import IMP.container
8 import IMP
9 
10 m = IMP.kernel.Model()
11 
12 # create some particles
13 ps = [IMP.core.XYZ.setup_particle(IMP.kernel.Particle(m)) for x in range(0, 3)]
14 
17  [(ps[i[0]], ps[i[1]]) for i in [(0, 1), (1, 2)]])
18 print [(p[0].get_name(), p[1].get_name()) for p in lpc.get_particle_pairs()]
20 r.set_model(m)
21 r.set_maximum_score(.1)
22 
23 space = IMP.domino.XYZStates(
24  [IMP.algebra.Vector3D(i, 0, 0) for i in range(0, 6)])
25 
27 for p in ps:
28  pst.set_particle_states(p, space)
29 
30 m.set_log_level(IMP.base.SILENT)
31 
32 # make sure to break up the
33 mt = IMP.domino.get_merge_tree([r], pst)
34 try:
35  IMP.show_graphviz(mt)
36 except:
37  print "Unable to display graph using 'dot'"
38 
39 ds = IMP.domino.DominoSampler(m, pst)
40 # use the default setup for filters
41 ds.set_scoring_function([r])
42 ds.set_merge_tree(mt)
43 ds.set_log_level(IMP.base.SILENT)
44 
45 # recurse down the tree getting the assignments and printing them
46 
47 
48 def get_assignments(vertex):
49  on = mt.get_out_neighbors(vertex)
50  if len(on) == 0:
51  # we have a leaf
52  ret = ds.get_vertex_assignments(vertex)
53  else:
54  # recurse on the two children
55  if on[0] > on[1]:
56  # the get_vertex_assignment methods expects the children in sorted
57  # order
58  on = [on[1], on[0]]
59  a0 = get_assignments(on[0])
60  a1 = get_assignments(on[1])
61  ret = ds.get_vertex_assignments(vertex, a0, a1)
62  print mt.get_vertex_name(vertex), [str(r) for r in ret]
63  return ret
64 
65 # the root is the last vetex
66 get_assignments(mt.get_vertices()[-1])
67 
68 
69 schedule = []
70 # we could instead decompose the tree into independent sets of jobs
71 
72 
73 def schedule_job(vertex):
74  # first figure out the maximum phase for my children, then add me to the
75  # next higher phase
76  on = mt.get_out_neighbors(vertex)
77  max_child_time = -1
78  for n in on:
79  max_child_time = max(schedule_job(n), max_child_time)
80  my_time = max_child_time + 1
81  while len(schedule) < my_time + 1:
82  schedule.append([])
83  # add myself to the next free phase
84  schedule[my_time].append(vertex)
85  return my_time
86 
87 schedule_job(mt.get_vertices()[-1])
88 print "The merging can be scheduled as", schedule