Skip to content
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 a bit using new dependencies #288

Merged
merged 20 commits into from
Sep 6, 2023
Merged

Clean up a bit using new dependencies #288

merged 20 commits into from
Sep 6, 2023

Conversation

ajjackson
Copy link
Collaborator

Using some features of Python 3.8+ and newer Numpy

I tried not to go too crazy, there are more places where assignment expressions (i.e. walrus) could be used to save a line but overall flow and clarity don't really benefit.

@codecov-commenter
Copy link

codecov-commenter commented Sep 4, 2023

Codecov Report

Merging #288 (f10d6c4) into master (7c8b90a) will increase coverage by 0.06%.
The diff coverage is 96.26%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the GitHub App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master     #288      +/-   ##
==========================================
+ Coverage   95.79%   95.85%   +0.06%     
==========================================
  Files          28       28              
  Lines        3992     3983       -9     
  Branches      803      803              
==========================================
- Hits         3824     3818       -6     
+ Misses         99       97       -2     
+ Partials       69       68       -1     
Files Changed Coverage Δ
euphonic/util.py 95.79% <81.81%> (+0.01%) ⬆️
euphonic/spectra.py 95.16% <93.33%> (-0.01%) ⬇️
euphonic/broadening.py 82.82% <100.00%> (+0.53%) ⬆️
euphonic/crystal.py 90.82% <100.00%> (-0.33%) ⬇️
euphonic/force_constants.py 99.65% <100.00%> (+<0.01%) ⬆️
euphonic/plot.py 100.00% <100.00%> (ø)
euphonic/powder.py 96.87% <100.00%> (+0.04%) ⬆️
euphonic/qpoint_frequencies.py 99.13% <100.00%> (+<0.01%) ⬆️
euphonic/qpoint_phonon_modes.py 98.83% <100.00%> (+0.01%) ⬆️
euphonic/readers/castep.py 96.04% <100.00%> (+0.88%) ⬆️
... and 2 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2023

Test Results

       22 files  ±0         22 suites  ±0   52m 54s ⏱️ - 2m 7s
  1 049 tests ±0    1 043 ✔️ ±0    6 💤 ±0  0 ±0 
10 430 runs  ±0  10 367 ✔️ ±0  63 💤 ±0  0 ±0 

Results for commit f10d6c4. ± Comparison against base commit 7c8b90a.

♻️ This comment has been updated with latest results.

@ajjackson
Copy link
Collaborator Author

I have used the Literal type annotation everywhere I could find strings being used to switch between a few options.

Another possible tool for this kind of thing is Enum. Python 3.11 introduces the StrEnum which could be especially nice for doing this in a backward-compatible way.

@ajjackson ajjackson marked this pull request as ready for review September 5, 2023 13:01
@ajjackson
Copy link
Collaborator Author

I'm baffled by these codacy errors. Those items are clearly type-hinted as Quantity. They assert as Quantity. mypy doesn't complain about them. Why is Codacy convinced they are numpy arrays?

(mypy doesn't like that they are indexed, though. Which is a bit strange; how does it complain that it doesn't have stubs for pint, but also that it doesn't think Quantity should be indexable?)

@ajjackson
Copy link
Collaborator Author

I think the .pylintrc has to be pushed to master branch before it will be used by Codacy :-(

Using some features of Python 3.8+ and newer Numpy

I tried not to go too crazy, there are more places where assignment
expressions (i.e. walrus) could be used to save a line but overall
flow and clarity don't really benefit.
Newer versions of Pint and Numpy allow a lot more Numpy operations to
"just work" so we can cut a lot of boilerplate conversions.

Unfortunate np.linalg.norm is not one of these yet! For now we'll
leave the casting on that one, but np.square, np.sqrt and np.sum are
ok...
There are lots of places in spectra where only a few specific strings
are allowed. This can be hinted clearly with the Literal type from
Python 3.8+

An even stricter implementation would use Enums, but making this
backward-compatible could be clunky until the Python 3.11 StrEnum is
available.

Importing Quantity from Pint rather than indirectly from Euphonic
seems to make mypy slightly less grumpy.
Codacy complains that at this line

"Instance of ndarray has no 'to' member"

But it should always be a Quantity? Try inserting assertion and see
what happens.

(Overly long line complaint is fair enough...)
Now that Quantities are handled a bit better, it is really only
"needed" in force_constants... And it's not good practice to use
private methods from other modules anyway.

It doesn't hurt to make this casting to Bohr more explicit anyway, it
makes it clearer that this part of the maths is done in atomic units
without Pint wrappers.
@ajjackson
Copy link
Collaborator Author

ajjackson commented Sep 5, 2023

That seems to have fixed it 🎉 . I had to also edit the codacy settings to look for a pylintrc file.

euphonic/broadening.py Outdated Show resolved Hide resolved
euphonic/crystal.py Show resolved Hide resolved
@@ -1097,7 +1099,7 @@ def _dipole_correction_init(crystal: Crystal,
recip_q0 += recip_q0_tmp
else:
break
vol = crystal._cell_volume()
vol = crystal.cell_volume().to('bohr^3').magnitude
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is "magnitude" doing here? Is it a pint thing to return sans unit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, magnitude unwraps it to a bare array. This is why we usually chain it with to so we know what the unit will be and can use the values (relatively) safely.

euphonic/powder.py Show resolved Hide resolved
euphonic/qpoint_phonon_modes.py Outdated Show resolved Hide resolved
euphonic/readers/castep.py Outdated Show resolved Hide resolved
euphonic/readers/castep.py Outdated Show resolved Hide resolved
euphonic/readers/castep.py Show resolved Hide resolved
euphonic/spectra.py Show resolved Hide resolved
euphonic/spectra.py Show resolved Hide resolved
With a little wrapper we can have a nice for-loop which is more
readable.
euphonic/readers/castep.py Outdated Show resolved Hide resolved
Co-authored-by: Jacob Wilkins <46597752+oerc0122@users.noreply.github.com>
@ajjackson ajjackson merged commit a9b31bb into master Sep 6, 2023
7 checks passed
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.

3 participants