IMP  2.0.0
The Integrative Modeling Platform
interactive.py
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.Model()
11 
12 # create some particles
13 ps=[IMP.core.XYZ.setup_particle(IMP.Particle(m)) for x in range(0,3)]
14 
16 lpc= IMP.container.ListPairContainer([(ps[i[0]], ps[i[1]]) for i in [(0,1), (1,2)]])
17 print [(p[0].get_name(), p[1].get_name()) for p in lpc.get_particle_pairs()]
19 r.set_model(m)
20 r.set_maximum_score(.1)
21 
22 space= IMP.domino.XYZStates([IMP.algebra.Vector3D(i, 0, 0) for i in range(0,6)])
23 
25 for p in ps:
26  pst.set_particle_states(p, space)
27 
28 m.set_log_level(IMP.base.SILENT)
29 
30 # make sure to break up the
31 mt= IMP.domino.get_merge_tree([r], pst)
32 try:
33  IMP.show_graphviz(mt)
34 except:
35  print "Unable to display graph using 'dot'"
36 
37 ds= IMP.domino.DominoSampler(m, pst)
38 # use the default setup for filters
39 ds.set_scoring_function([r])
40 ds.set_merge_tree(mt)
41 ds.set_log_level(IMP.base.SILENT)
42 
43 # recurse down the tree getting the assignments and printing them
44 def get_assignments(vertex):
45  on= mt.get_out_neighbors(vertex)
46  if len(on)==0:
47  # we have a leaf
48  ret= ds.get_vertex_assignments(vertex)
49  else:
50  # recurse on the two children
51  if on[0] > on[1]:
52  # the get_vertex_assignment methods expects the children in sorted order
53  on=[on[1], on[0]]
54  a0= get_assignments(on[0])
55  a1= get_assignments(on[1])
56  ret= ds.get_vertex_assignments(vertex, a0, a1)
57  print mt.get_vertex_name(vertex), [str(r) for r in ret]
58  return ret
59 
60 # the root is the last vetex
61 get_assignments(mt.get_vertices()[-1])
62 
63 
64 schedule=[]
65 # we could instead decompose the tree into independent sets of jobs
66 def schedule_job(vertex):
67  # first figure out the maximum phase for my children, then add me to the
68  # next higher phase
69  on= mt.get_out_neighbors(vertex)
70  max_child_time=-1
71  for n in on:
72  max_child_time=max(schedule_job(n), max_child_time)
73  my_time=max_child_time+1
74  while len(schedule) < my_time+1:
75  schedule.append([])
76  # add myself to the next free phase
77  schedule[my_time].append(vertex)
78  return my_time
79 
80 schedule_job(mt.get_vertices()[-1])
81 print "The merging can be scheduled as", schedule