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

Numpy2 #532

Merged
merged 4 commits into from
Jul 9, 2024
Merged

Numpy2 #532

merged 4 commits into from
Jul 9, 2024

Conversation

lang-m
Copy link
Member

@lang-m lang-m commented Jul 3, 2024

User description


PR Type

Enhancement, Bug fix


Description

  • Updated datetime usage to handle deprecation of utcnow() in Python 3.12.
  • Added .item() to convert xarray and numpy values to Python built-in types.
  • Converted mesh.n, region.pmin, and region.pmax to lists using tolist() method in Jinja2 templates.

Changes walkthrough 📝

Relevant files
Bug fix
hdf5.py
Update datetime usage to handle deprecation in Python 3.12

discretisedfield/io/hdf5.py

  • Updated utcnow to now with datetime.UTC to handle deprecation in
    Python 3.12.
  • +1/-1     
    Enhancement
    hv.py
    Ensure compatibility with xarray in resampling function   

    discretisedfield/plotting/hv.py

  • Added .item() to convert xarray to Python built-in type before using
    linspace.
  • +4/-1     
    tools.py
    Convert numpy values to Python types in count_bps function

    discretisedfield/tools/tools.py

    • Added .item() to convert numpy values to Python built-in types.
    +4/-3     
    mesh.jinja2
    Convert mesh.n to list in mesh template                                   

    discretisedfield/html/templates/mesh.jinja2

    • Converted mesh.n to list using tolist() method.
    +1/-1     
    region.jinja2
    Convert region.pmin and region.pmax to list in region template

    discretisedfield/html/templates/region.jinja2

  • Converted region.pmin and region.pmax to list using tolist() method.
  • +2/-2     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    github-actions bot commented Jul 3, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Possible Bug:
    Ensure that the use of datetime.datetime.now(datetime.UTC) is correctly implemented. The correct usage should be datetime.datetime.now(datetime.timezone.utc).

    Data Conversion:
    Verify that the use of .item() for converting numpy and xarray data types to Python built-in types does not lead to loss of precision or other unintended side effects, especially in large datasets.

    Data Structure Conversion:
    Ensure that the conversion of numpy arrays to lists using .tolist() in the Jinja2 templates does not impact performance or expected behavior, particularly with large arrays.

    Copy link
    Contributor

    github-actions bot commented Jul 3, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Use the recommended datetime.timezone.utc for timezone-aware datetime objects

    Replace datetime.UTC with datetime.timezone.utc for compatibility with Python 3.9 and
    later versions, which deprecate datetime.UTC.

    discretisedfield/io/hdf5.py [76]

    -utc_now = datetime.datetime.now(datetime.UTC).isoformat(timespec="seconds")
    +utc_now = datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds")
     
    Suggestion importance[1-10]: 10

    Why: The suggestion correctly identifies the deprecation of datetime.UTC and provides a compatible alternative, improving code compatibility with Python 3.9 and later versions.

    10
    Possible bug
    Add error handling for potential None values in min/max calculations

    Add error handling for cases where array[dim].min() or array[dim].max() might return NaN
    or None, which would cause item() to fail.

    discretisedfield/plotting/hv.py [740]

    -dim: np.linspace(array[dim].min().item(), array[dim].max().item(), ni)
    +dim: np.linspace(array[dim].min().item() if array[dim].min() is not None else 0, array[dim].max().item() if array[dim].max() is not None else 0, ni)
     
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential bug by adding error handling for None values, which could cause the item() method to fail, thus improving the robustness of the code.

    8
    Add checks to ensure region.pmin and region.pmax are iterable before converting to list

    Ensure that region.pmin and region.pmax are always lists or arrays before calling tolist()
    to avoid potential errors if the properties are not iterable.

    discretisedfield/html/templates/region.jinja2 [3-4]

    -<li>pmin = {{ region.pmin.tolist() }}</li>
    -<li>pmax = {{ region.pmax.tolist() }}</li>
    +<li>pmin = {{ region.pmin.tolist() if isinstance(region.pmin, (list, np.ndarray)) else [region.pmin] }}</li>
    +<li>pmax = {{ region.pmax.tolist() if isinstance(region.pmax, (list, np.ndarray)) else [region.pmax] }}</li>
     
    Suggestion importance[1-10]: 7

    Why: The suggestion improves code robustness by ensuring region.pmin and region.pmax are iterable before calling tolist(), preventing potential runtime errors.

    7

    @lang-m lang-m mentioned this pull request Jul 3, 2024
    36 tasks
    Deprecation of utcnow() in Python 3.12
    @lang-m lang-m requested a review from samjrholt July 9, 2024 09:21
    @lang-m lang-m merged commit 7093240 into master Jul 9, 2024
    8 checks passed
    @lang-m lang-m deleted the numpy2 branch July 9, 2024 10:00
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants