IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
interactive.py
1 ## \example domino/interactive.py
2 # IMP::domino::DominoSampler supports an interactive mode whereby each step
3 # of the sampling process is called explicitly and all intermediate results
4 # are exposed. This usage mode can be used to help understand how domino
5 # behaves as well as to distribute a domino computation to multiple cores
6 # or multiple machines. For the latter, it is useful to use the
7 # IMP::domino::get_assignments() and IMP::domino::set_assignments()
8 # functions to save the states to a file.
9 
10 from __future__ import print_function
11 import IMP.domino
12 import IMP.algebra
13 import IMP.container
14 import IMP
15 import sys
16 
17 IMP.setup_from_argv(sys.argv, "interactive")
18 
19 m = IMP.Model()
20 
21 # create some particles
23  for x in range(0, 3)])
24 
27  m, [(ps[i[0]], ps[i[1]]) for i in [(0, 1), (1, 2)]])
28 print([(m.get_particle_name(p[0]), m.get_particle_name(p[1]))
29  for p in lpc.get_contents()])
31 r.set_maximum_score(.1)
32 
33 space = IMP.domino.XYZStates(
34  [IMP.algebra.Vector3D(i, 0, 0) for i in range(0, 6)])
35 
37 for p in ps:
38  pst.set_particle_states(m.get_particle(p), space)
39 
40 m.set_log_level(IMP.SILENT)
41 
42 # make sure to break up the
43 mt = IMP.domino.get_merge_tree([r], pst)
44 try:
46 except:
47  print("Unable to display graph using 'dot'")
48 
49 ds = IMP.domino.DominoSampler(m, pst)
50 # use the default setup for filters
51 ds.set_restraints([r])
52 ds.set_scoring_function([r])
53 ds.set_merge_tree(mt)
54 ds.set_log_level(IMP.SILENT)
55 
56 # recurse down the tree getting the assignments and printing them
57 
58 
59 def get_assignments(vertex):
60  on = mt.get_out_neighbors(vertex)
61  if len(on) == 0:
62  # we have a leaf
63  ret = ds.get_vertex_assignments(vertex)
64  else:
65  # recurse on the two children
66  if on[0] > on[1]:
67  # the get_vertex_assignment methods expects the children in sorted
68  # order
69  on = [on[1], on[0]]
70  a0 = get_assignments(on[0])
71  a1 = get_assignments(on[1])
72  ret = ds.get_vertex_assignments(vertex, a0, a1)
73  print(mt.get_vertex_name(vertex), [str(r) for r in ret])
74  return ret
75 
76 
77 # the root is the last vertex
78 get_assignments(mt.get_vertices()[-1])
79 
80 schedule = []
81 # we could instead decompose the tree into independent sets of jobs
82 
83 
84 def schedule_job(vertex):
85  # first figure out the maximum phase for my children, then add me to the
86  # next higher phase
87  on = mt.get_out_neighbors(vertex)
88  max_child_time = -1
89  for n in on:
90  max_child_time = max(schedule_job(n), max_child_time)
91  my_time = max_child_time + 1
92  while len(schedule) < my_time + 1:
93  schedule.append([])
94  # add myself to the next free phase
95  schedule[my_time].append(vertex)
96  return my_time
97 
98 
99 schedule_job(mt.get_vertices()[-1])
100 print("The merging can be scheduled as", schedule)
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Various classes to hold sets of particles.
Sample best solutions using Domino.
Definition: DominoSampler.h:32
Score distance between two particle centers using a harmonic function.
static XYZ setup_particle(Model *m, ParticleIndex pi)
Definition: XYZ.h:51
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Store a list of ParticleIndexPairs.
ParticleIndexPairs get_indexes(const ParticlePairsTemp &ps)
Get the indexes from a list of particle pairs.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
MergeTree get_merge_tree(const SubsetGraph &junction_tree)
VectorD< 3 > Vector3D
Definition: VectorD.h:425
Class to handle individual particles of a Model object.
Definition: Particle.h:43
std::string show_graphviz(Graph g)
Applies a PairScore to each Pair in a list.
Divide-and-conquer inferential optimization in discrete space.