Add residual electron density (Fo²−Fc²) wireframe rendering#10
Draft
Add residual electron density (Fo²−Fc²) wireframe rendering#10
Conversation
- 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]>
Copilot created this pull request from a session on behalf of
dkratzert
April 13, 2026 17:28
View session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 viagemmi.StructureFactorCalculatorX, least-squares scales Fo to Fc, builds difference coefficients(Fo − k·|Fc|) × exp(iφc), and FFTs to a real-space grid viagemmi.ComplexAsuDataisosurface.py— Pure-numpy marching cubes producing wireframe line segments (Cartesian) from a 3D density gridModified modules
molecule2D.py—_draw_density_wireframe()renders segments with cumulative rotation tracking; green/red semi-transparent pensloader.py—set_density(),set_density_sigma(),set_hkl_path()API; auto-detects sibling.hklfiles;_replicate_density_segments()tiles the wireframe across unit cells matching the grown structure's bounding boxviewer_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
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.