Optimized cube file parsing in from_cube for improved speed #4331
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.
Summary
Major Changes:
This PR enhances the
from_cube
function inio.common.VolumetricData
to significantly improve performance. When processing large.cube
files, the original implementation took minutes to read and set Pymatgen objects. The optimized version incorporates several key improvements: file reading is now handled withreadlines()
instead of multiplereadline()
calls, reducing I/O operations. Voxel data parsing has been rewritten to use NumPy vectorized parsing instead of loops, making numerical processing faster. Atom site parsing has been improved by replacing the loop-basedreadline()
approach with list comprehensions. Additionally, volumetric data parsing now leveragesnp.fromstring()
instead of converting lists to NumPy arrays.Performance Improvements
The Figures below illustrate the execution time and memory usage improvements:
Figure 1: Performance comparison for large
.cube
files (GB-scale).Figure 2: Performance comparison for small
.cube
files (MB-scale).To quantify improvements, I benchmarked 13
.cube
files of various structures, volumetric data, and sizes. The optimizedfrom_cube()
is 55-65% faster in parsing while maintaining comparable memory usage.Interestingly, preloading the entire file did not significantly impact RAM usage. While the optimized version appears to use slightly more RAM for smaller files (~20-100 MB difference), this is negligible given the short allocation time.
For File 3, a high memory usage difference was observed across multiple runs. While I am unsure of the exact cause, it might be related to NumPy's internal memory management and temporary storage allocation when parsing large files.
To-Do
Simple upgrade completed.
Note: All tested
.cube
files were generated from GCMC, probability plots (aka density plots). The improvements should be applicable to DFT-generated.cube
files all the same (electron densities, molecular orbitals).Checklist
[Y] Google format doc strings added. Check with
ruff
.Formatting & linting checks passed (
pre-commit run --all-files
results below):[Y] Type annotations included. Check with
mypy
.Since only optimizations were made, type annotations remain unchanged from the original function.
[Y] Tests added for new features/fixes.
No new tests were added specifically, but I verified that the changes did not break anything by running pytest. The results were identical to those obtained on a fresh fork:
I also developed a local
pytest
to validate that the optimizedfrom_cube()
generates identical results as the original.Tested parameters:
All tests passed across 13 benchmark
.cube
files.This is my first contribution, so let me know if I'm missing anything!