-
Notifications
You must be signed in to change notification settings - Fork 1
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
Refactor: add unittest for spillage/api.py #96
Conversation
📝 Walkthrough📝 WalkthroughWalkthroughThe pull request introduces significant updates to the Changes
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 5
🧹 Outside diff range and nitpick comments (1)
SIAB/spillage/api.py (1)
600-600
: Simplifyget
method by omitting defaultNone
When using
dict.get()
, the default return value isNone
if the key is not found. Explicitly specifyingNone
is redundant.Consider applying this diff to simplify the code:
- siab_settings.get("spill_coefs", None)) + siab_settings.get("spill_coefs"))🧰 Tools
🪛 Ruff
600-600: Use
siab_settings.get("spill_coefs")
instead ofsiab_settings.get("spill_coefs", None)
Replace
siab_settings.get("spill_coefs", None)
withsiab_settings.get("spill_coefs")
(SIM910)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- SIAB/spillage/api.py (13 hunks)
🧰 Additional context used
🪛 Ruff
SIAB/spillage/api.py
122-122: f-string without any placeholders
Remove extraneous
f
prefix(F541)
600-600: Use
siab_settings.get("spill_coefs")
instead ofsiab_settings.get("spill_coefs", None)
Replace
siab_settings.get("spill_coefs", None)
withsiab_settings.get("spill_coefs")
(SIM910)
783-783: Ambiguous variable name:
l
(E741)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (8)
SIAB/spillage/api.py (8)
Line range hint
36-65
: LGTM! Consider enhancing error handling.The changes to
_coef_gen
function look good. The removal of thevalue
parameter simplifies the function, and the docstring has been updated accordingly.Consider using a more specific exception for the
lmax
check:- if not isinstance(lmax, int): - raise ValueError("Expected lmax to be an integer") + if not isinstance(lmax, int) or lmax < 0: + raise ValueError("Expected lmax to be a non-negative integer")This change would catch potential issues with negative
lmax
values early.
66-95
: Great addition! Consider enhancing type hinting.The new
_band_indexing
function is a valuable addition, providing flexibility in handling band indexes for different geometries. The implementation is concise and efficient.Consider adding type hints to improve code readability and maintainability:
-def _band_indexing(nbands, indexes, folders): +def _band_indexing(nbands: Union[int, List[int]], indexes: List[int], folders: List[List[str]]) -> Union[int, List[range]]:Also, add the necessary import at the top of the file:
from typing import Union, ListThis change would make the function signature more explicit about the expected input and output types.
Line range hint
97-164
: Good updates! Consider enhancing the deprecation warning.The changes to
_coef_opt_jy
function look good. The updated parameter handling and the renaming of_initguess
to_make_guess
improve code clarity.Consider using the
warnings
module for the deprecation warning:+import warnings def _coef_opt_jy(rcut, orbparams, folders, options, nthreads, spill_coefs = None): # ... (existing code) if spill_coefs: - print("ORBGEN: For jy, spill_coefs is deprecated.", flush = True) + warnings.warn("ORBGEN: For jy, spill_coefs is deprecated and will be removed in a future version.", DeprecationWarning, stacklevel=2)This change provides a more standard way of handling deprecations and allows users to control the visibility of deprecation warnings.
Line range hint
165-228
: Good updates! Consider enhancing parameter handling.The changes to
_coef_opt_pw
function are consistent with the updates in_coef_opt_jy
and improve the handling ofspill_coefs
.Consider using a tuple instead of a list for the default
spill_coefs
value, as it's immutable:- spill_coefs = [0.0, 1.0] if spill_coefs is None else spill_coefs + spill_coefs = (0.0, 1.0) if spill_coefs is None else tuple(spill_coefs)This change ensures that the default value cannot be accidentally modified and makes it clear that the order and number of elements are fixed.
Line range hint
229-258
: Good refactoring! Consider enhancing type hinting.The renaming of
_initguess
to_make_guess
and the updates to handle the newnzeta
structure are good improvements. The implementation is concise and efficient.Consider adding more specific type hints to improve code readability:
-def _make_guess(nzeta, folder, jy = True, diagnosis = True): +def _make_guess(nzeta: List[List[int]], folder: str, jy: bool = True, diagnosis: bool = True) -> Dict[str, Any]:Also, add the necessary imports at the top of the file:
from typing import List, Dict, AnyThis change would make the function signature more explicit about the expected input and output types.
Line range hint
259-310
: LGTM! Consider minor formatting improvements.The changes to
_do_onion_opt
function are mostly cosmetic and do not affect the functionality. The variable renaming improves clarity slightly.Consider adjusting the formatting of the long print statement for better readability:
- print(f"""ORBGEN: optimization on level {iorb + 1} (with # of zeta functions for each l: {nzeta_}), - based on orbital ({nzeta_inner})""", flush = True) + print(f"""ORBGEN: optimization on level {iorb + 1} \ +(with # of zeta functions for each l: {nzeta_}), \ +based on orbital ({nzeta_inner})""", flush=True)This change improves the readability of the multi-line f-string and maintains consistent indentation.
311-344
: Good updates! Consider enhancing type hinting.The changes to
_coef_opt
function improve clarity and flexibility by using more explicit parameter passing. The updated implementation correctly uses the new parameters.Consider adding type hints to improve code readability and maintainability:
-def _coef_opt(rcut, orbparams, folders, maxiter, nthreads, jy, spill_coefs = None): +def _coef_opt(rcut: float, orbparams: List[Dict[str, Any]], folders: List[List[str]], + maxiter: int, nthreads: int, jy: bool, spill_coefs: Optional[List[float]] = None) -> List[List[List[float]]]:Also, add the necessary imports at the top of the file:
from typing import List, Dict, Any, OptionalThis change would make the function signature more explicit about the expected input and output types.
775-811
: Great addition of test cases! Consider minor consistency improvements.The new test methods
test_coef_gen
,test_band_indexing
, andtest_make_guess
provide good coverage for the new and modified functions. They check various scenarios and edge cases, which is crucial for maintaining code quality.For consistency, consider using
self.assertEqual
instead ofself.assertSetEqual
in thetest_make_guess
method:- self.assertSetEqual(guess["nzeta"], [3, 3, 2]) + self.assertEqual(guess["nzeta"], [3, 3, 2])This change maintains consistency with the assertion style used in other test methods.
🧰 Tools
🪛 Ruff
783-783: Ambiguous variable name:
l
(E741)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- SIAB/spillage/api.py (13 hunks)
🧰 Additional context used
📓 Learnings (1)
SIAB/spillage/api.py (3)
Learnt from: kirk0830 PR: kirk0830/ABACUS-ORBGEN#96 File: SIAB/spillage/api.py:783-783 Timestamp: 2024-09-29T07:56:50.811Z Learning: When `l` is used as a variable for angular momentum, do not flag it as ambiguous.
Learnt from: kirk0830 PR: kirk0830/ABACUS-ORBGEN#91 File: SIAB/spillage/api.py:179-179 Timestamp: 2024-09-26T05:29:36.499Z Learning: In this codebase related to quantum physics and chemistry, using `l` as a variable name for angular momentum is widely accepted and conventional, so recommendations to avoid using `l` are unnecessary.
Learnt from: kirk0830 PR: kirk0830/ABACUS-ORBGEN#96 File: SIAB/spillage/api.py:594-594 Timestamp: 2024-09-29T07:57:45.713Z Learning: In cases where there is no valid information to retrieve the element (e.g., when 'folders' is empty or improperly structured), the program should raise an error to inform the user.
🪛 Ruff
SIAB/spillage/api.py
600-600: Use
siab_settings.get("spill_coefs")
instead ofsiab_settings.get("spill_coefs", None)
Replace
siab_settings.get("spill_coefs", None)
withsiab_settings.get("spill_coefs")
(SIM910)
783-783: Ambiguous variable name:
l
(E741)
🔇 Additional comments (1)
SIAB/spillage/api.py (1)
Line range hint
1-1137
: Overall, excellent improvements to the codebase!The changes in this file significantly enhance the structure and functionality of the orbital optimization process. The new functions (
_band_indexing
) and modifications to existing ones (_coef_gen
,_coef_opt_jy
,_coef_opt_pw
,_make_guess
,_coef_opt
) improve flexibility and clarity. The added test cases provide good coverage for these changes, which is crucial for maintaining code quality.The refactoring of parameter handling, particularly the shift from
siab_settings
to more explicit parameters, improves the overall design and makes the code more maintainable. The consistent use of type hinting throughout the file would further enhance readability and catch potential type-related issues early.Keep up the great work!
Summary by CodeRabbit
New Features
Bug Fixes
Tests