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