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