IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
model_numpy.py
1 ## \example core/model_numpy.py
2 # Demonstration using NumPy to manipulate IMP data.
3 #
4 
5 import IMP.example
6 import sys
7 
8 IMP.setup_from_argv(sys.argv, "model numpy")
9 
10 (m, c) = IMP.example.create_model_and_particles()
11 
12 if not IMP.IMP_KERNEL_HAS_NUMPY:
13  print("IMP was not built with NumPy support.")
14  sys.exit(0)
15 
16 # Get all pairs of particles that are close to each other
18 m.update()
19 
20 # contents is a NumPy 2xN array of the indices of each close particle pair
21 contents = nbl.get_contents()
22 
23 # Get direct access to the coordinates and radii of all particles in the Model.
24 # These are NumPy views, not copies; they share memory with the Model. This
25 # allows us very efficient read-write access to the data, with a caveat: if
26 # the Model memory is reallocated (typically when particles or attributes are
27 # added) any views become invalid, so we should not keep them around long-term.
28 # These arrays are indexed by particle indices.
29 xyz, r = m.get_spheres_numpy()
30 
31 import numpy
32 
33 # Once we have coordinates and particle indices, we can use any NumPy
34 # function on them. Here we calculate the Cartesian distance between each
35 # pair of particles in the close pair container.
36 dists = numpy.linalg.norm(numpy.diff(xyz[contents], axis=1), axis=2).flatten()
37 
38 print("Distribution of center-center particle distances:")
39 print("Min %.2f Max %.2f Mean %.2f" % (dists.min(), dists.max(), dists.mean()))
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Return all close unordered pairs of particles taken from the SingletonContainer.
Example module.