IMP logo
IMP Reference Guide  2.20.1
The Integrative Modeling Platform
em/analyze_convergence.py

Analyze the convergence of the IMP.em.FitRestraint. The script builds a simple model and then displays the derivatives, em score and how well conjugate gradients converges under various displacements of the model.

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