IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
analyze_convergence.py
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
6import IMP.display
7import IMP.em
8import sys
9
10IMP.setup_from_argv(sys.argv, "analyze convergence")
11
12use_rigid_bodies = True
13bd = 10
14radius = 10
15
16m = IMP.Model()
17p = IMP.Particle(m)
20d.set_radius(radius)
21
22# Set up the particle as either a rigid body or a simple ball
23if 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
35else:
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
45dheader = IMP.em.create_density_header(bb, 1)
46dheader.set_resolution(1)
47dmap = IMP.em.SampledDensityMap(dheader)
48dmap.set_particles([p])
49
50dmap.resample()
51# computes statistic stuff about the map and insert it in the header
52dmap.calcRMS()
54rs = 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
59r = IMP.em.FitRestraint([fp], dmap)
62g.set_name("deriv")
63w = IMP.display.PymolWriter("derivatives.pym")
64# kind of abusive
65steps = 4
66m.set_log_level(IMP.SILENT)
67
69opt.set_scoring_function(sf)
70
71
72def 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
97try_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)
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Class to handle individual particles of a Model object.
Definition Particle.h:45
Object used to hold a set of restraints.
A reference frame in 3D.
static Mass setup_particle(Model *m, ParticleIndex pi, Float mass)
Definition Mass.h:48
Simple conjugate gradients optimizer.
Create a scoring function on a list of restraints.
static RigidBody setup_particle(Model *m, ParticleIndex pi, ParticleIndexesAdaptor ps)
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition XYZR.h:48
Write a CGO file with the geometry.
Definition PymolWriter.h:34
void write_map(DensityMap *m, std::string filename, MapReaderWriter *writer)
Write a density map to a file using the given writer.
Calculate score based on fit to EM map.
Class for sampling a density map from particles.
VectorD< 3 > Vector3D
Definition VectorD.h:407
Output IMP model data in various file formats.
Color get_gray_color(double f)
Return the a grayscale value for f.
Color get_hot_color(double f)
Return the color for f from the hot color map.
Basic utilities for handling cryo-electron microscopy 3D density maps.
DensityHeader create_density_header(const algebra::BoundingBoxD< 3 > &bb, float spacing)
Create a header from a bounding box in 3D.
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.