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

Simplify and future-proof numba import in spa.py #1944

Merged
merged 8 commits into from
Mar 13, 2024

Conversation

kandersolar
Copy link
Member

@kandersolar kandersolar commented Jan 4, 2024

  • [ ] Closes #xxxx
  • I am familiar with the contributing guidelines
  • Tests added
  • Updates entries in docs/sphinx/source/reference for API changes.
  • Adds description and name entries in the appropriate "what's new" file in docs/sphinx/source/whatsnew for all changes. Includes link to the GitHub Issue with :issue:`num` or this Pull Request with :pull:`num`. Includes contributor name and/or GitHub username (link with :ghuser:`user`).
  • New code is fully documented. Includes numpydoc compliant docstrings, examples, and comments where necessary.
  • Pull request is nearly complete and ready for detailed review.
  • Maintainer: Appropriate GitHub Labels (including remote-data) and Milestone are assigned to the Pull Request and linked Issue.

spa.py currently inspects numba's version string to determine whether it meets the required minimum of 0.17.0:

try:
from numba import jit, __version__
except ImportError:
warnings.warn('Could not import numba, falling back to numpy ' +
'calculation')
jcompile = nocompile
USE_NUMBA = False
else:
major, minor = __version__.split('.')[:2]
if int(major + minor) >= 17:
# need at least numba >= 0.17.0
jcompile = jit
USE_NUMBA = True
else:
warnings.warn('Numba version must be >= 0.17.0, falling back to ' +
'numpy')
jcompile = nocompile
USE_NUMBA = False

However, the way it performs the check isn't quite correct. Consider what will happen when numba.__version__ eventually ticks over from 0.x.y to 1.0.0. That code on line 32 will compare int("1" + "0") with 17 and incorrectly decide that the installed version of numba isn't new enough.

Rather than fix the version check, I think it makes sense to just remove that check in spa.py altogether in favor of specifying the minimum in setup.py as usual. Even that is probably unnecessarily cautious; numba 0.17.0 is ancient and I think it would be safe to assume that any version of numba that pvlib might encounter today would be much more recent than that, but no harm in setting the minimum anyway.

@kandersolar kandersolar added this to the v0.10.4 milestone Jan 4, 2024
@kandersolar kandersolar marked this pull request as ready for review January 4, 2024 22:54
@kandersolar
Copy link
Member Author

To-do: make the same change to has_numba in conftest.py

Copy link
Member

@AdamRJensen AdamRJensen left a comment

Choose a reason for hiding this comment

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

I'm all for simplifying things!

LGTM

@@ -21,23 +21,15 @@ def nocompile(*args, **kwargs):

if os.getenv('PVLIB_USE_NUMBA', '0') != '0':
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if os.getenv('PVLIB_USE_NUMBA', '0') != '0':
if os.getenv('PVLIB_USE_NUMBA') is not None:

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this change would require some corresponding changes in the part of solarposition.py that controls how spa.py is imported (by setting this environment variable to '0' or '1'), so I'm inclined to leave this as-is for now. It will be addressed someday with #1060.

Copy link
Member

Choose a reason for hiding this comment

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

I didn't realize that it could be defined as zero (I just assumed it would be undefined). Fine with me to just leave it.

jcompile = nocompile
USE_NUMBA = False
jcompile = jit
USE_NUMBA = True
else:
jcompile = nocompile
USE_NUMBA = False
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for inconsistent usage of upper vs. lower case, e.g., USE_NUMBA vs. has_numba?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I see is is that USE_NUMBA is a module-level variable used inside a function far away from where it is defined while has_numba is used once in the same scope immediately after its definition. But of course it's just style :)

@AdamRJensen AdamRJensen merged commit 92c0e5b into pvlib:main Mar 13, 2024
33 of 34 checks passed
@AdamRJensen
Copy link
Member

Thanks @kandersolar for your continuous commitment to keeping pvlib in good shape!

@kandersolar kandersolar deleted the numba-simplify branch March 13, 2024 13:41
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.

2 participants