IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
em/numpy_data.py

Accessing density map data directly using NumPy.

1 ## \example em/numpy_data.py
2 # Accessing density map data directly using NumPy.
3 
4 import IMP.em
5 import IMP.core
6 import IMP.atom
7 import sys
8 
9 IMP.setup_from_argv(sys.argv, "numpy data")
10 
11 if not IMP.IMP_KERNEL_HAS_NUMPY:
12  print("IMP was not built with NumPy support.")
13  sys.exit(0)
14 
15 dmap = IMP.em.read_map(
17 
18 # We can get the actual voxel data as a 3D NumPy array
19 # This is a view of the data in `dmap`, not a copy, so is efficient
20 # (however, it should not be used after the `dmap` object itself is destroyed).
21 m = dmap.get_data()
22 print(m.shape)
23 
24 # We can use any NumPy method to query the data
25 print(m.min(), m.mean(), m.max())
26 
27 # Count all voxels above some threshold
28 print(m[m >= 0.1].size)
29 
30 # We can also change the data; here we do the equivalent of
31 # IMP.em.get_threshold_map() in place, i.e. clear any voxel that is
32 # below the threshold
33 m[m < 0.1] = 0.0
34 print(m.min(), m.mean(), m.max())