-
Notifications
You must be signed in to change notification settings - Fork 871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up util.num.make_symmetric_matrix_from_upper_tri
and util.io_util.micro_pyawk
, fix new ruff
errors
#4192
Open
DanielYang59
wants to merge
7
commits into
materialsproject:master
Choose a base branch
from
DanielYang59:migrate-util-pyawk-change
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+138
−75
Conversation
This file contains 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
DanielYang59
commented
Nov 23, 2024
""" | ||
idx = [0, 3, 4, 1, 5, 2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this helper function is designed to work for 3x3 matrix alone, as such I might just make things simple and reject non 3x3 arrays with ValueError
.
The new implementation gives a ~3 speedup:
Original implementation time: 0.378586 seconds
Simplified implementation time: 0.118705 seconds
Test script (from GPT):
import numpy as np
from timeit import timeit
def original_make_symmetric_matrix_from_upper_tri(val):
"""Original implementation."""
idx = [0, 3, 4, 1, 5, 2]
val = np.array(val)[idx]
mask = ~np.tri(3, k=-1, dtype=bool)
out = np.zeros((3, 3), dtype=val.dtype)
out[mask] = val
out.T[mask] = val
return out
def simplified_make_symmetric_matrix_from_upper_tri(val):
"""Simplified implementation."""
array = np.asarray(val)
if array.shape != (6,):
raise ValueError(f"Expected val of length 6, got {array.shape}")
return np.array([
[array[0], array[3], array[4]],
[array[3], array[1], array[5]],
[array[4], array[5], array[2]],
])
# Test input
val = [1, 2, 3, 4, 5, 6]
# Measure execution time
original_time = timeit(
"original_make_symmetric_matrix_from_upper_tri(val)",
globals=globals(),
number=100000,
)
simplified_time = timeit(
"simplified_make_symmetric_matrix_from_upper_tri(val)",
globals=globals(),
number=100000,
)
# Display results
print(f"Original implementation time: {original_time:.6f} seconds")
print(f"Simplified implementation time: {simplified_time:.6f} seconds")
DanielYang59
commented
Nov 23, 2024
DanielYang59
changed the title
Clean up
Clean up Nov 23, 2024
util.num.make_symmetric_matrix_from_upper_tri
and util.io_util.micro_pyawk
util.num.make_symmetric_matrix_from_upper_tri
and util.io_util.micro_pyawk
, fix new ruff
errors
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.
Simplify/clarify
util.num.make_symmetric_matrix_from_upper_tri
and add testsCaution
make_symmetric_matrix_from_upper_tri
now raiseValueError
for non 3x3 arrayI believe
utils.num.make_symmetric_matrix_from_upper_tri
is designed to work on 3x3 matrix alone (the new index is hard-coded):pymatgen/src/pymatgen/util/num.py
Lines 31 to 32 in 31f1e1f
As such I might greatly simplify the implementation f2c3252 which gives ~3x speedup and much better readability.
Clean up
util.io_util.micro_pyawk
Warning
The
debug/postdebug
is likely debug args, no usage across the entire code bass, schedule them for removal after one-year grace period in case someone really need them.Others
Remove:
pymatgen/src/pymatgen/util/io_utils.py
Lines 98 to 99 in 31f1e1f
I believe this is doing nothing (set umask to 0, but reset it back the original umask immediately)
Fix new
ruff
errors and bumppre-commit
accordingly