Skip to content

Add residual electron density (Fo²−Fc²) wireframe rendering#10

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/draw-residual-electron-density
Draft

Add residual electron density (Fo²−Fc²) wireframe rendering#10
Copilot wants to merge 3 commits intomainfrom
copilot/draw-residual-electron-density

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 13, 2026

Computes and renders the Fo−Fc difference Fourier map as a wireframe isosurface overlay on the molecule widget. Green = positive residual density, red = negative. Uses gemmi for structure factor calculation and FFT. Density replicates across unit cells when the structure is grown.

New modules

  • density.py — Reads HKL data (SHELX HKLF-4 or embedded CIF _shelx_hkl_file), computes Fc via gemmi.StructureFactorCalculatorX, least-squares scales Fo to Fc, builds difference coefficients (Fo − k·|Fc|) × exp(iφc), and FFTs to a real-space grid via gemmi.ComplexAsuData
  • isosurface.py — Pure-numpy marching cubes producing wireframe line segments (Cartesian) from a 3D density grid

Modified modules

  • molecule2D.py_draw_density_wireframe() renders segments with cumulative rotation tracking; green/red semi-transparent pens
  • loader.pyset_density(), set_density_sigma(), set_hkl_path() API; auto-detects sibling .hkl files; _replicate_density_segments() tiles the wireframe across unit cells matching the grown structure's bounding box
  • viewer_widget.py — "Density (Fo²−Fc²)" checkbox + σ-level spinbox (default ±3σ)

Crystal symmetry handling

When grow mode is active, the loader computes the fractional-coordinate bounding box of all displayed atoms and replicates the unit-cell wireframe by the necessary lattice translations.

Usage

viewer = MoleculeViewerWidget()
viewer.load_file("structure.cif")
# Enable via UI checkbox, or programmatically:
viewer._loader.set_hkl_path("structure.hkl")  # optional, auto-detected
viewer._loader.set_density(True)
viewer._loader.set_density_sigma(3.0)  # ±3σ contour level

Tests

13 new tests covering HKL parsing, reflection merging, density computation (p21c.cif, p31c.cif with both embedded and external HKL), isosurface extraction, and edge cases. All 87 tests pass.

Copilot AI and others added 3 commits April 13, 2026 15:33
- density.py: HKL reading, Fc computation via gemmi, difference map FFT
- isosurface.py: Marching cubes wireframe extraction (pure numpy)
- molecule2D.py: Density wireframe drawing (green=positive, red=negative)
- loader.py: Integrated density loading with auto HKL detection
- viewer_widget.py: Added density toggle and sigma level controls
- __init__.py: Export new modules

Agent-Logs-Url: https://github.com/dkratzert/Fastmolwidget/sessions/a66449fd-1684-48d5-b0d0-55dc851ada89

Co-authored-by: dkratzert <[email protected]>
Replicate density wireframe segments across unit cells when grow mode
is active, so the isosurface coverage matches the expanded structure.

Agent-Logs-Url: https://github.com/dkratzert/Fastmolwidget/sessions/a66449fd-1684-48d5-b0d0-55dc851ada89

Co-authored-by: dkratzert <[email protected]>
- Move gemmi import to top of isosurface.py (PEP 8)
- Rename test variable from 'data' to 'test_data_dir' for clarity

Agent-Logs-Url: https://github.com/dkratzert/Fastmolwidget/sessions/a66449fd-1684-48d5-b0d0-55dc851ada89

Co-authored-by: dkratzert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants