chemparseplot.plot.render#

Structure figures for clusters, periodic cells, and reaction paths.

The backend is fixed by the kind of system. A Lennard-Jones cluster is drawn with solvis. A cell, a reaction path, and any snapshot that contains more than one element are drawn with xyzrender. A one-element snapshot uses the cluster backend, with Cordero radii instead of the Lennard-Jones contact radius.

Added in version 1.14.0.

Module Contents#

Classes#

Frame

One oriented picture: positions are already in the camera frame.

Functions#

_hex_to_rgb

_ruhi_element_colors

__getattr__

covalent_radius

Cordero covalent radius in angstroms.

element_color

Hex body colour for one element.

element_rgb

element_color as an RGB triple in 0..1.

frame_color_overrides

Symbol-to-hex map for one frame. A symbol outside the map raises.

pair_distance

is_covalent_contact

True when the pair falls inside the Jmol/OpenBabel autobond window.

covalent_bonds

Undirected covalent pairs, i < j.

hydrogen_bonds

Kumar O-H…O contacts that are not themselves covalent bonds.

kabsch

Rotation that maps mobile onto target. Each array is (N, 3).

inertia_rotation

Columns are the image x, y, and z axes.

_masses

transferring_hydrogen

Index of the hydrogen that moves farthest between the endpoints.

align_path

Kabsch every frame onto the first, then one inertia rotation for all.

frame_indices

Endpoints, the summit, and the index halfway along each side.

geometric_summit

Frame where the transferring H is equidistant from donor and acceptor.

pack_frames

Centre every frame on the shared bounding box and return its longest side.

_symbols_positions

_as_structures

backend_for

Name of the renderer used for this kind and structure.

_paint

RGB body colours. A missing element raises, as the override map does.

_lj_highlight

_neighbor_highlight

_user_highlight

_molecule_frame

_orthorhombic_edges

_water_molecules

Each oxygen and the two hydrogens covalently bound to it.

_wrap_water

_periodic_hbonds

Kumar contacts. An acceptor outside the cell is stored as a ghost oxygen.

_front_water

The water nearest centre in the image whose oxygen is in view.

_water_frame

_cluster_frame

_path_frames

_single_frames

Return frames and the renderer name.

_import_xyzrender

Import xyzrender via ensure_import, then a normal import.

_import_solvis

Import solvis via ensure_import, then a normal import.

_caption_face

Font file of the theme’s caption face, else matplotlib’s sans-serif.

_caption_px_range

Smallest and largest whole-pixel sizes that print from 5 pt to 7 pt.

_ellipsise

Shorten label so it fits width, ending in an ellipsis.

_fit_caption

The largest size from 7 pt down to 5 pt at which label fits width.

_theme_rgba

_theme_hex

_embedded_dpi

Horizontal dpi stored on the image, or None when the file has none.

_tag_print_dpi

Record a dpi when the raster file did not.

_caption_px

Pixel size of a 7 pt caption on this raster.

_stitch

_hex

_ensure_glx_display

Give VTK an X display when this process has none.

_render_solvis

_render_xyzrender

render_structure

Draw a structure, or a strip of a path, and return an RGBA image.

Data#

API#

chemparseplot.plot.render.log#

‘getLogger(…)’

chemparseplot.plot.render.NATURE_SINGLE_COLUMN_MM#

89.0

chemparseplot.plot.render.PRINT_DPI#

300

chemparseplot.plot.render.MM_PER_INCH#

25.4

chemparseplot.plot.render.CANVAS#

‘round(…)’

chemparseplot.plot.render.LABEL_POINT#

7

chemparseplot.plot.render.CAPTION_FLOOR_POINT#

5

chemparseplot.plot.render.POINTS_PER_INCH#

72

chemparseplot.plot.render.LJ_RMIN_OVER_SIGMA#

None

chemparseplot.plot.render.LJ_SHELL_CUTOFF_OVER_SIGMA#

None

chemparseplot.plot.render.LJ_CONTACT_RADIUS_OVER_SIGMA#

None

chemparseplot.plot.render.HBOND_OO_MAX_A#

3.5

chemparseplot.plot.render.HBOND_BETA_MAX_DEG#

30.0

chemparseplot.plot.render.HYDROGENS_PER_WATER#

2

chemparseplot.plot.render.HYDROGEN_Z#

1

chemparseplot.plot.render.DONOR_AND_ACCEPTOR#

2

chemparseplot.plot.render.BOND_TOLERANCE_A#

0.45

chemparseplot.plot.render.BOND_MIN_A#

0.4

chemparseplot.plot.render.VTK_PARALLEL_SCALE#

0.5

chemparseplot.plot.render.CORAL#

None

chemparseplot.plot.render.TEAL#

None

chemparseplot.plot.render.SKY#

None

chemparseplot.plot.render.BODY_COLORS#

None

chemparseplot.plot.render.SOLVIS_DEPTH_PEELING#

False

chemparseplot.plot.render.SOLVIS_SHADOWS#

False

chemparseplot.plot.render.XYZRENDER_FOG#

False

chemparseplot.plot.render.OUTLINE_POINT#

0.25

chemparseplot.plot.render.KIND_BACKEND#

None

chemparseplot.plot.render.PATH_KINDS#

‘frozenset(…)’

chemparseplot.plot.render._hex_to_rgb(value: str) → numpy.ndarray#
chemparseplot.plot.render.CORAL_RGB#

‘_hex_to_rgb(…)’

chemparseplot.plot.render.TEAL_RGB#

‘_hex_to_rgb(…)’

chemparseplot.plot.render._ruhi_element_colors() → dict[str, str]#
chemparseplot.plot.render.__getattr__(name: str)#
class chemparseplot.plot.render.Frame#

One oriented picture: positions are already in the camera frame.

symbols: list[str]#

None

positions: numpy.ndarray#

None

colors: numpy.ndarray#

None

radii: numpy.ndarray#

None

highlight: numpy.ndarray#

None

bonds: list[tuple[int, int]]#

‘field(…)’

hbonds: list[tuple[int, int]]#

‘field(…)’

label: str = <Multiline-String>#
explicit_bonds: bool#

False

chemparseplot.plot.render.covalent_radius(symbol: str) → float#

Cordero covalent radius in angstroms.

chemparseplot.plot.render.element_color(symbol: str, colors: collections.abc.Mapping[str, str] | None = None) → str#

Hex body colour for one element.

The default map is ELEMENT_COLORS. A symbol outside the map raises.

chemparseplot.plot.render.element_rgb(symbol: str, colors: collections.abc.Mapping[str, str] | None = None) → numpy.ndarray#

element_color as an RGB triple in 0..1.

chemparseplot.plot.render.frame_color_overrides(symbols: list[str], colors: collections.abc.Mapping[str, str] | None = None) → dict[str, str]#

Symbol-to-hex map for one frame. A symbol outside the map raises.

chemparseplot.plot.render.pair_distance(positions: numpy.ndarray, i: int, j: int) → float#
chemparseplot.plot.render.is_covalent_contact(symbols: list[str], positions: numpy.ndarray, i: int, j: int) → bool#

True when the pair falls inside the Jmol/OpenBabel autobond window.

chemparseplot.plot.render.covalent_bonds(symbols: list[str], positions: numpy.ndarray) → list[tuple[int, int]]#

Undirected covalent pairs, i < j.

chemparseplot.plot.render.hydrogen_bonds(symbols: list[str], positions: numpy.ndarray) → list[tuple[int, int]]#

Kumar O-H…O contacts that are not themselves covalent bonds.

There is no periodic boundary. Each pair is (hydrogen, acceptor).

chemparseplot.plot.render.kabsch(mobile: numpy.ndarray, target: numpy.ndarray) → numpy.ndarray#

Rotation that maps mobile onto target. Each array is (N, 3).

chemparseplot.plot.render.inertia_rotation(positions: numpy.ndarray, masses: numpy.ndarray) → numpy.ndarray#

Columns are the image x, y, and z axes.

z is the largest principal moment, so a flat molecule is seen face on.

chemparseplot.plot.render._masses(symbols: list[str]) → numpy.ndarray#
chemparseplot.plot.render.transferring_hydrogen(frames: list[tuple[list[str], numpy.ndarray]]) → int#

Index of the hydrogen that moves farthest between the endpoints.

The last frame is rotated onto the first by the heavy atoms (Z > 1) before the displacement is measured.

chemparseplot.plot.render.align_path(frames: list[tuple[list[str], numpy.ndarray]]) → list[numpy.ndarray]#

Kabsch every frame onto the first, then one inertia rotation for all.

chemparseplot.plot.render.frame_indices(n: int, summit: int) → list[int]#

Endpoints, the summit, and the index halfway along each side.

Duplicates are dropped. The count is fixed by that construction.

chemparseplot.plot.render.geometric_summit(symbols: list[str], positions: list[numpy.ndarray], hydrogen: int) → int#

Frame where the transferring H is equidistant from donor and acceptor.

The donor is the oxygen closest to that H in the first frame. The acceptor is the oxygen closest to it in the last frame.

chemparseplot.plot.render.pack_frames(frames: list[chemparseplot.plot.render.Frame]) → float#

Centre every frame on the shared bounding box and return its longest side.

chemparseplot.plot.render._symbols_positions(atoms) → tuple[list[str], numpy.ndarray]#
chemparseplot.plot.render._as_structures(structure) → list#
chemparseplot.plot.render.backend_for(kind: str, structure) → str#

Name of the renderer used for this kind and structure.

snapshot follows the element count: one element uses the cluster backend, anything else uses the cell backend.

chemparseplot.plot.render._paint(symbols: list[str], highlight: numpy.ndarray, body: numpy.ndarray | None, colors: collections.abc.Mapping[str, str] | None = None) → numpy.ndarray#

RGB body colours. A missing element raises, as the override map does.

body is one colour for every atom, used for a cluster. Otherwise each symbol has to be in the element map.

chemparseplot.plot.render._lj_highlight(positions: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.render._neighbor_highlight(symbols: list[str], positions: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.render._user_highlight(n: int, highlight: list[int] | None, default: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.render._molecule_frame(symbols: list[str], positions: numpy.ndarray, hydrogen: int | None, label: str, highlight: list[int] | None, *, colors: collections.abc.Mapping[str, str] | None = None) → chemparseplot.plot.render.Frame#
chemparseplot.plot.render._orthorhombic_edges(atoms) → numpy.ndarray | None#
chemparseplot.plot.render._water_molecules(symbols: list[str], positions: numpy.ndarray) → list[tuple[int, int, int]] | None#

Each oxygen and the two hydrogens covalently bound to it.

Returns None when the stoichiometry is not water or an oxygen does not have two unused covalent hydrogens.

chemparseplot.plot.render._wrap_water(positions: numpy.ndarray, molecules, edges: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.render._periodic_hbonds(symbols, positions, molecules, edges)#

Kumar contacts. An acceptor outside the cell is stored as a ghost oxygen.

chemparseplot.plot.render._front_water(molecules: list[tuple[int, int, int]], symbols: list[str], positions: numpy.ndarray, radii: numpy.ndarray, centre: numpy.ndarray) → tuple[int, int, int]#

The water nearest centre in the image whose oxygen is in view.

xyzrender draws a larger z nearer the camera. An oxygen is covered when a nearer oxygen, a ghost included, sits closer to it in the image than the sum of their radii. When every water is covered, every water is a candidate.

chemparseplot.plot.render._water_frame(atoms, highlight: list[int] | None, label: str, colors: collections.abc.Mapping[str, str] | None = None) → chemparseplot.plot.render.Frame | None#
chemparseplot.plot.render._cluster_frame(atoms, highlight: list[int] | None, label: str, *, lj: bool) → chemparseplot.plot.render.Frame#
chemparseplot.plot.render._path_frames(structures, kind: str, energies, highlight: list[int] | None, colors: collections.abc.Mapping[str, str] | None = None) → list[chemparseplot.plot.render.Frame]#
chemparseplot.plot.render._single_frames(structures, kind: str, highlight: list[int] | None, colors: collections.abc.Mapping[str, str] | None = None) → tuple[list[chemparseplot.plot.render.Frame], str]#

Return frames and the renderer name.

chemparseplot.plot.render._import_xyzrender()#

Import xyzrender via ensure_import, then a normal import.

chemparseplot.plot.render._import_solvis()#

Import solvis via ensure_import, then a normal import.

chemparseplot.plot.render._caption_face(theme: chemparseplot.plot.theme.PlotTheme) → str#

Font file of the theme’s caption face, else matplotlib’s sans-serif.

chemparseplot.plot.render._caption_px_range(dpi: float) → tuple[int, int]#

Smallest and largest whole-pixel sizes that print from 5 pt to 7 pt.

The smallest rounds up and the largest rounds down, so both stay inside the range.

chemparseplot.plot.render._ellipsise(label: str, font, width: int) → str#

Shorten label so it fits width, ending in an ellipsis.

chemparseplot.plot.render._fit_caption(label: str, width: int, dpi: float, face: str) → tuple[str, object]#

The largest size from 7 pt down to 5 pt at which label fits width.

A label that is wider than width at 5 pt is ellipsised at 5 pt.

chemparseplot.plot.render._theme_rgba(color: str) → tuple[int, int, int, int]#
chemparseplot.plot.render._theme_hex(color: str) → str#
chemparseplot.plot.render._embedded_dpi(image) → float | None#

Horizontal dpi stored on the image, or None when the file has none.

chemparseplot.plot.render._tag_print_dpi(image, layout_px: int)#

Record a dpi when the raster file did not.

The layout panel is layout_px at the print resolution. A writer that emits more pixels and no dpi chunk, which is what xyzrender 0.3.8 does, is printing that same panel at a higher dpi.

chemparseplot.plot.render._caption_px(image) → int#

Pixel size of a 7 pt caption on this raster.

chemparseplot.plot.render._stitch(images, labels: list[str], theme: chemparseplot.plot.theme.PlotTheme) → numpy.ndarray#
chemparseplot.plot.render._hex(rgb: numpy.ndarray) → str#
chemparseplot.plot.render._XVFB_DISPLAY_START#

99

chemparseplot.plot.render._XVFB_DISPLAY_ATTEMPTS#

11

chemparseplot.plot.render._XVFB_READY_SECONDS#

5.0

chemparseplot.plot.render._XVFB_POLL_SECONDS#

0.05

chemparseplot.plot.render._XVFB_SCREEN#

‘1280x1024x24’

chemparseplot.plot.render._XVFB: list[tuple[subprocess.Popen[bytes], str]]#

[]

chemparseplot.plot.render._ensure_glx_display() → None#

Give VTK an X display when this process has none.

The pip wheel of VTK 9.7 draws through GLX. With DISPLAY unset it tries EGL and then OSMesa, finds neither library, and screenshot segfaults. Xvfb is the display the GitHub Ubuntu runners already ship.

chemparseplot.plot.render._render_solvis(frames: list[chemparseplot.plot.render.Frame], span: float, canvas: int, theme: chemparseplot.plot.theme.PlotTheme) → numpy.ndarray#
chemparseplot.plot.render._render_xyzrender(frames: list[chemparseplot.plot.render.Frame], span: float, canvas: int, colors: collections.abc.Mapping[str, str] | None = None, theme: chemparseplot.plot.theme.PlotTheme | None = None) → numpy.ndarray#
chemparseplot.plot.render.render_structure(structure, *, kind: str = 'snapshot', energies=None, highlight: list[int] | None = None, output: str | pathlib.Path | None = None, canvas: int | None = None, element_colors: collections.abc.Mapping[str, str] | None = None, theme: chemparseplot.plot.theme.PlotTheme | None = None) → numpy.ndarray#

Draw a structure, or a strip of a path, and return an RGBA image.

Parameters

structure: An ASE Atoms object, or a sequence of them in path order. kind: cluster (Lennard-Jones, coordinates in units of sigma), cell, instanton, neb, or snapshot. A snapshot with one element is drawn as a cluster of Cordero spheres. Any other snapshot is drawn as a molecule or a cell. energies: One energy per frame. The highest frame is the summit. Without energies, an instanton or NEB summit is the frame where the transferring hydrogen is equidistant from its donor and acceptor. highlight: Zero-based atom indices painted in Ruhi coral. The default for a cluster is the lowest-coordination shell, for a water cell the molecule nearest the cell centre in the image whose oxygen no nearer oxygen covers, and for a path the transferring hydrogen. output: Optional PNG path. canvas: Figure width in pixels. The default is a Nature single column (89 mm) at 300 dpi. Each panel is that width divided by the panel count, so a strip printed at one column keeps the caption inside Nature’s 5 pt to 7 pt range. A caption is set at the largest whole-pixel size in that range at which it fits its panel. A label wider than its panel at 5 pt is ellipsised. xyzrender writes a PNG with more pixels than the panel and no dpi chunk. The caption uses the dpi of that file, or the print resolution times the measured pixel scale when the chunk is absent. An xyzrender panel must be wider than twice the xyzrender padding (40 px in xyzrender 0.3.8), or a ValueError is raised. element_colors: Map from element symbol to hex colour. The default is ELEMENT_COLORS: each element takes the Ruhi colour of the CPK anchor (H, C, N or O) whose Jmol colour is nearest its own. A map must name every element in the figure. theme: Caption face, ground, and ink. The default is the ruhi theme. Every atom is outlined in the ink. A caption_font that is not installed logs a warning naming the face, and the caption falls back to matplotlib’s sans-serif face.

Returns

numpy.ndarray uint8 array of shape (height, width, 4).