ProteinMotion
Reference navigation
On this page

CLASS · v0.9.1

DensityMap

An immutable scalar grid with physical coordinates and optional periodic boundaries.

python
from proteinmotion import DensityMap

Constructor

python
DensityMap(
    values,
    *,
    spacing=1.0,
    origin=(0, 0, 0),
    basis=None,
    periodic=False,
    max_voxels=32000000,
)

Parameters

ParameterDefaultDescription
valuesRequiredFinite three-dimensional array in (x, y, z) order, with at least two samples per axis.
spacingkeyword only1.0Positive scalar or (x, y, z) voxel spacing in ångströms for an orthogonal grid.
originkeyword only(0, 0, 0)Physical (x, y, z) origin of the first voxel, in ångströms.
basiskeyword onlyNoneOptional 3×3 matrix whose columns are voxel step vectors in ångströms; replaces spacing.
periodickeyword onlyFalseWrap sampling and crops at cell boundaries for a full periodic grid.
max_voxelskeyword only32000000Maximum number of allocated grid values, default 32 million.

Attributes

NameDescription
valuesRead-only float32 grid in x, y, z order.
originOrigin in ångströms.
basisVoxel step vectors as matrix columns.
meanMean of the source grid, preserved in crops.
stdStandard deviation of the source grid, preserved in crops.
periodicWhether sampling wraps around the grid.

Example and output

This excerpt runs inside a scene’s construct() method. The full example file includes imports, structure loading, and camera setup. Run it from a repository checkout.

Code Full script
python
"""1UBQ electron density from PDBe, with a contour and a moving density slice.

Source: https://www.ebi.ac.uk/pdbe/coordinates/files/1ubq.ccp4
Retrieved 2026-09-17. The supplied map covers a complete crystallographic unit
cell. Cropping extends periodic data across the cell boundary when needed.
Sigma contours use the mean and standard deviation of the original map.
"""

from pathlib import Path

from proteinmotion import (
    ColorLegend,
    ColorScale,
    DensityMap,
    FadeIn,
    FadeOut,
    Protein,
    ProteinScene,
    Text,
)

DATA = Path(__file__).parent / "data"


class DensityMaps(ProteinScene):
    def construct(self):
        protein = Protein.from_file(DATA / "1ubq.cif").ball_and_stick().center()
        protein.set_residue_opacity(0.08)
        helix = protein.select(chain="A", residues=(23, 34))
        helix.set_opacity(1)
        density = DensityMap.from_file(DATA / "1ubq.ccp4")
        local = density.crop(helix, padding=2.5)
        shell = local.isosurface(1.5, opacity=0.28, follow=protein)
        scale = ColorScale(-0.5, 2, colors=("#10243d", "#438ca4", "#f5df93"))
        section = local.slice("z", 0.15, scale=scale, follow=protein, resolution=96)
        self.add(protein, shell)
        self.camera.frame(helix, margin=1.55)
        self.camera.orbit(theta=0.22, phi=0.18)
        self.add(Text("Electron density", position=(0.06, 0.07), font_size=42))
        self.add(Text("1UBQ · helix 23–34 · PDBe map", position=(0.06, 0.13), font_size=25))
        self.play(FadeIn(shell), run_time=1)
        self.play(shell.animate.set_level(2.5), self.camera.animate.orbit(theta=0.25), run_time=2)
        self.play(shell.animate.set_level(1.5), run_time=1.5)
        self.play(FadeIn(section), run_time=1)
        self.add(ColorLegend(scale, title="Map value", position=(0.06, 0.81)))
        self.play(section.animate.set_slice(0.85), run_time=3)
        self.play(FadeOut(section), run_time=1)
        self.wait(0.5)
Output Preview · 10.0 s · 60 fps
Electron density and slices

1UBQ PDBe density: animate the contour and move a slice past helix 23–34.

Methods and properties

NameDescription
from_file()class methodRead MRC/CCP4, optionally gzip-compressed, using Gemmi with map axes and physical coordinates preserved.
boundspropertyEight corners in map coordinates, suitable for framing.
sample()methodTrilinear sampling in Å. Periodic grids wrap; other outside points return NaN.
crop()methodCrop a box around a selected region in its protein’s local coordinates, with padding in ångströms.
isosurface()methodCreate a shaded marching-cubes contour.
slice()methodCreate a trilinearly sampled, colored slice plane.
python
DensityMap.from_file(path, *, origin='auto', max_voxels=32000000)

Read MRC/CCP4, optionally gzip-compressed, using Gemmi with map axes and physical coordinates preserved.

ParameterDefaultDescription
pathRequiredInput or output file path, as specified by the operation.
originkeyword only'auto'auto prefers a nonzero ORIGIN field, otherwise grid starts; header or start forces that convention.
max_voxelskeyword only32000000Maximum surface grid cells; increase this limit for large structures or finer grids.

Returns: Protein

Full crystallographic unit-cell grids are periodic. Cropped maps and EM volumes retain their stored extent. No symmetry expansion is performed.

python
DensityMap.bounds

Eight corners in map coordinates, suitable for framing.

python
DensityMap.sample(points)

Trilinear sampling in Å. Periodic grids wrap; other outside points return NaN.

ParameterDefaultDescription
pointsRequiredCoordinates with shape (points, 3), in model ångströms.
python
DensityMap.crop(region, *, padding=4.0)

Crop a box around a selected region in its protein’s local coordinates, with padding in ångströms.

ParameterDefaultDescription
regionRequiredRegion selection attached to a protein.
paddingkeyword only4.0Extra highlight size in ångströms. None selects a default for the highlight style.

Periodic maps wrap across cell boundaries. Crops preserve the original mean and standard deviation for sigma contours.

python
DensityMap.isosurface(
    level=1.0,
    *,
    units='sigma',
    color='#75d5cb',
    opacity=0.3,
    step_size=1,
    follow=None,
)

Create a shaded marching-cubes contour.

ParameterDefaultDescription
level1.0Contour value in the units chosen for this surface.
unitskeyword only'sigma'sigma for mean + level × standard deviation, or absolute for a stored map value.
colorkeyword only'#75d5cb'Hex color or RGB values. For tint setters, None restores the representation’s base palette.
opacitykeyword only0.3Opacity in [0, 1], from transparent to opaque.
step_sizekeyword only1Positive marching-cubes grid stride. Larger values reduce extraction cost and detail.
followkeyword onlyNoneOptional Protein whose translation, rotation, and scale are applied to this map object. Density does not deform with atom coordinates.
python
DensityMap.slice(
    axis='z',
    position=0.5,
    *,
    scale=None,
    opacity=1.0,
    resolution=128,
    follow=None,
)

Create a trilinearly sampled, colored slice plane.

ParameterDefaultDescription
axis'z'Map grid axis: x, y, or z. In a skew cell these follow the lattice voxel vectors.
position0.5Fractional location along the selected grid axis, from 0 to 1.
scalekeyword onlyNoneColorScale shared by the structure, plots, and legend. None derives limits from finite values.
opacitykeyword only1.0Opacity in [0, 1], from transparent to opaque.
resolutionkeyword only128Samples per edge of the slice plane: integer from 2 to 512.
followkeyword onlyNoneOptional Protein whose translation, rotation, and scale are applied to this map object. Density does not deform with atom coordinates.