IMP  2.4.0
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
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 
10 use_rigid_bodies = True
11 bd = 10
12 radius = 10
13 
14 m = IMP.kernel.Model()
18 d.set_radius(radius)
19 
20 # Set up the particle as either a rigid body or a simple ball
21 if use_rigid_bodies:
22  prb = IMP.kernel.Particle(m)
23  prb.set_name("rigid body")
24  d.set_coordinates(IMP.algebra.Vector3D(0, 0, 0))
27  drb.add_member(p)
28  print("initial frame", drb.get_reference_frame())
29  fp = prb
30  drb.set_coordinates_are_optimized(True)
31  refiner = IMP.core.TableRefiner()
32  refiner.add_particle(prb, [p])
33  to_move = drb
34  print([p.get_name() for p in refiner.get_refined(prb)])
35  fp = d
36 else:
37  fp = d
38  to_move = d
39  d.set_coordinates_are_optimized(True)
40  refiner = None
41 
42 
44  IMP.algebra.Vector3D(-bd - radius, -bd - radius, -bd - radius),
45  IMP.algebra.Vector3D(bd + radius, bd + radius, bd + radius))
46 
47 dheader = IMP.em.create_density_header(bb, 1)
48 dheader.set_resolution(1)
49 dmap = IMP.em.SampledDensityMap(dheader)
50 dmap.set_particles([p])
51 
52 dmap.resample()
53 # computes statistic stuff about the map and insert it in the header
54 dmap.calcRMS()
55 IMP.em.write_map(dmap, "map.mrc", IMP.em.MRCReaderWriter())
57 m.add_restraint(rs)
58 # rs.set_weight(.003)
59 
60 # if rigid bodies are used, we need to define a refiner as
61 # FitRestraint doesn't support just passing all the geometry
62 r = IMP.em.FitRestraint([fp], dmap)
63 rs.add_restraint(r)
65 g.set_name("deriv")
66 w = IMP.display.PymolWriter("derivatives.pym")
67 # kind of abusive
68 steps = 4
69 m.set_log_level(IMP.base.SILENT)
70 
72 
73 
74 def try_point(i, j, k):
75  print("trying", i, j, k)
76  vc = IMP.algebra.Vector3D(i, j, k)
77  to_move.set_coordinates(vc)
78  # display the score at this position
80  cg.set_name("score")
81  v = m.evaluate(True)
82  cg.set_color(IMP.display.get_hot_color(v))
83  w.add_geometry(cg)
84  print("score and derivatives", v, to_move.get_derivatives())
85  w.add_geometry(g)
86 
87  opt.optimize(10)
88  print("after", d.get_coordinates())
89  mag = to_move.get_coordinates().get_magnitude()
90 
91  converge_color = IMP.display.get_gray_color(1.0 / (1.0 + mag))
92  # display the distance after optimization at this position
94  sg.set_color(converge_color)
95  sg.set_name("converge")
96  w.add_geometry(sg)
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)
static RigidBody setup_particle(kernel::Model *m, ParticleIndex pi, kernel::ParticleIndexesAdaptor ps)
Definition: rigid_bodies.h:168
Object used to hold a set of restraints.
Simple conjugate gradients optimizer.
Color get_hot_color(double f)
Return the color for f from the hot color map.
void write_map(DensityMap *m, std::string filename)
Write a density map to a file.
A reference frame in 3D.
Class for sampling a density map from particles.
static XYZR setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: XYZR.h:48
A lookup based particle refiner.
Definition: TableRefiner.h:21
DensityHeader create_density_header(const algebra::BoundingBoxD< 3 > &bb, float spacing)
Create a header from a bounding box in 3D.
Basic utilities for handling cryo-electron microscopy 3D density maps.
Class to handle individual model particles.
static Mass setup_particle(kernel::Model *m, ParticleIndex pi, Float mass)
Definition: Mass.h:44
Calculate score based on fit to EM map.
Definition: FitRestraint.h:32
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Color get_gray_color(double f)
Return the a grayscale value for f.
Write a CGO file with the geometry.
Definition: PymolWriter.h:34
Output IMP model data in various file formats.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73