IMP  2.0.1
The Integrative Modeling Platform
analyze_convergence.py
1 ## \example em/analyze_convergence.py
2 ## Analyze the convergence of the IMP.em.FitRestraint. The script build a simple model and then displays the derivatives, em score and how well conjugate gradients converges under various displacements of the model.
3 
4 import IMP.display
5 import IMP.em
6 
7 use_rigid_bodies=True
8 bd= 10
9 radius=10
10 
11 m= IMP.Model()
12 p= IMP.Particle(m)
15 d.set_radius(radius)
16 
17 # Set up the particle as either a rigid body or a simple ball
18 if use_rigid_bodies:
19  prb= IMP.Particle(m)
20  prb.set_name("rigid body")
21  d.set_coordinates(IMP.algebra.Vector3D(0,0,0))
23  drb.add_member(p)
24  print "initial frame", drb.get_reference_frame()
25  fp= prb
26  drb.set_coordinates_are_optimized(True)
27  refiner= IMP.core.TableRefiner()
28  refiner.add_particle(prb, [p])
29  to_move= drb
30  print [p.get_name() for p in refiner.get_refined(prb)]
31  fp=d
32 else:
33  fp= d
34  to_move=d
35  d.set_coordinates_are_optimized(True)
36  refiner=None
37 
38 
39 bb= IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(-bd-radius, -bd-radius, -bd-radius),
40  IMP.algebra.Vector3D( bd+radius, bd+radius, bd+radius))
41 
42 dheader = IMP.em.create_density_header(bb,1)
43 dheader.set_resolution(1)
44 dmap = IMP.em.SampledDensityMap(dheader)
45 dmap.set_particles([p])
46 
47 dmap.resample()
48 # computes statistic stuff about the map and insert it in the header
49 dmap.calcRMS()
51 rs= IMP.RestraintSet()
52 m.add_restraint(rs)
53 #rs.set_weight(.003)
54 
55 # if rigid bodies are used, we need to define a refiner as
56 # FitRestraint doesn't support just passing all the geometry
57 r= IMP.em.FitRestraint([fp], dmap)
58 rs.add_restraint(r)
60 g.set_name("deriv")
61 w= IMP.display.PymolWriter("derivatives.pym")
62 # kind of abusive
63 steps=4
64 m.set_log_level(IMP.base.SILENT)
65 
67 
68 def try_point(i, j, k):
69  print "trying", i,j,k
70  vc=IMP.algebra.Vector3D(i,j,k)
71  to_move.set_coordinates(vc)
72  # display the score at this position
74  cg.set_name("score")
75  v=m.evaluate(True)
76  cg.set_color(IMP.display.get_hot_color(v))
77  w.add_geometry(cg)
78  print "score and derivatives", v, to_move.get_derivatives()
79  w.add_geometry(g)
80 
81  opt.optimize(10)
82  print "after", d.get_coordinates()
83  mag= to_move.get_coordinates().get_magnitude()
84 
85  converge_color= IMP.display.get_grey_color(1.0/(1.0+mag))
86  # display the distance after optimization at this position
88  sg.set_color(converge_color)
89  sg.set_name("converge")
90  w.add_geometry(sg)
91 
92 try_point(-bd,-bd,-bd)
93 
94 # For a more informative (but much slower) test, use the following instead:
95 #for i in range(-bd, bd+1, 2*bd/steps):
96 # for j in range(-bd, bd+1, 2*bd/steps):
97 # for k in range(-bd, bd+1, 2*bd/steps):
98 # try_point(i, j, k)