Skip to content

Fixes to new convolution code - #8035

Merged
astrofrog merged 5 commits into
astropy:masterfrom
astrofrog:convolution-fixes
Oct 31, 2018
Merged

astrofrog merged 5 commits into
astropy:masterfrom
astrofrog:convolution-fixes

Conversation

@astrofrog

Copy link
Copy Markdown
Member

This fixes #8032

If developers updated to the latest master version of astropy and were in develop mode, and did not rebuild extensions, they would get:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/bsipocz/munka/devel/astropy/astropy/convolution/__init__.py", line 10, in <module>
    from .convolve import convolve, convolve_fft, interpolate_replace_nans, convolve_models
  File "/Users/bsipocz/munka/devel/astropy/astropy/convolution/convolve.py", line 28, in <module>
    lib_path = glob.glob(os.path.join(os.path.dirname(__file__), 'lib_convolve*'))[0]
IndexError: list index out of range

so this PR makes the error message cleaner. For anyone curious, you can reproduce this error with:

python setup.py build_ext --inplace
rm astropy/convolution/lib_convolve.cpython-37m-darwin.so 
python -c 'import astropy.convolution'

Secondly it seems that importing faulthandler doesn't always work (see #8032) - so we should just not import it by default (it's easy for developers to import it in test scripts they are using, or in pytest using pytest-faulthandler).

cc @jamienoss

@astrofrog astrofrog added Bug Affects-dev PRs and issues that do not impact an existing Astropy release labels Oct 30, 2018
@astrofrog astrofrog added this to the v3.1 milestone Oct 30, 2018
@astrofrog
astrofrog requested a review from bsipocz October 30, 2018 08:50
@astropy-bot

astropy-bot Bot commented Oct 30, 2018

Copy link
Copy Markdown

Hi there @astrofrog 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃.

Everything looks good from my point of view! 👍

If there are any issues with this message, please report them here.

@astrofrog
astrofrog requested a review from keflavich October 30, 2018 08:50
@pllim

pllim commented Oct 30, 2018

Copy link
Copy Markdown
Member

From a glance, it seems like there is no "slow Python" fallback if C-extension fails to build?

Perhaps a future PR, but is it possible to have that C-extension as optional at all? That is, will some of the convolution functions still run without a C-extension?

Comment thread astropy/convolution/convolve.py Outdated
@jamienoss

Copy link
Copy Markdown
Contributor

@astrofrog Thanks for making the changes.

@pllim

That is, will some of the convolution functions still run without a C-extension?

No, it's always been dependent on the C extension.

Perhaps a future PR, but is it possible to have that C-extension as optional at all?

Does anything else in Astropy use optional extensions like this? Also, have a missed something, why has this PR (and/or #8032) have anything to do with the extension having build issues?

@astrofrog

Copy link
Copy Markdown
Member Author

@jamienoss - the issue wasn't fundamentally related to the convolution changes, it's just that the way the convolution code now looks for the extension means that @bsipocz got a cryptic error about glob rather than simply having an import of the C extension fail.

@keflavich

Copy link
Copy Markdown
Contributor

This is a bit over my head. I don't want to "approve" it because I don't really know what's going on. Can someone else review more usefully?

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@bsipocz bsipocz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess it's OK now.

Comment thread astropy/convolution/convolve.py Outdated
if sys.platform.startswith('win'):
libConvolve = ctypes.windll.LoadLibrary(lib_path)
lib_paths = glob.glob(os.path.join(os.path.dirname(__file__), 'lib_convolve*'))
if len(lib_paths) > 0:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lib_paths is a list, isn't if lib_paths preferred?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both work and I don't think it matters which one we use here.

Comment thread astropy/convolution/convolve.py Outdated
libConvolve = ctypes.windll.LoadLibrary(lib_path)
lib_paths = glob.glob(os.path.join(os.path.dirname(__file__), 'lib_convolve*'))
if len(lib_paths) > 0:
if sys.platform.startswith('win'):

@jamienoss jamienoss Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Think we need something like:

...
#Search for library
lib_paths = glob.glob(os.path.join(root, 'lib_convolve*'))
lib_path =''
for path in lib_paths:
    if not path.endswith('.py'):
        lib_path = path
        break
if not lib_path:
    raise Exception("Compiled convolution code is missing, try re-building astropy")

#Load found library
if sys.platform.startswith('win'):
    libConvolve = ctypes.windll.LoadLibrary(lib_path)
else:
    libConvolve = ctypes.cdll.LoadLibrary(lib_path)
...

@jamienoss jamienoss Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the exclusion soln. might be better than trying to be explicit for different OS lib suffixes. Neither are pleasant. Any thoughts anyone?

@jamienoss jamienoss Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or

...
lib_paths = glob.glob(os.path.join(root, 'lib_convolve*'))

filtered_lib_paths = [path for path in lib_paths if not path.endswith('.py')]
if not filtered_lib_paths:
    raise Exception("Compiled convolution code is missing, try re-building astropy")

# Use the 1st found
lib_path = filtered_lib_paths[0]

#Load found library
if sys.platform.startswith('win'):
    libConvolve = ctypes.windll.LoadLibrary(lib_path)
else:
    libConvolve = ctypes.cdll.LoadLibrary(lib_path)
...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can raise on if len(filtered_lib_paths) > 1?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What about .pyc files? We should make sure we include any kind of similar files. Any other extensions to exclude?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not that endswith can semantically handle *.

@jamienoss jamienoss Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pyc
pyo
pyx
pxd
pyd (checking now...) This is the suffix for the windows built library.

@jamienoss jamienoss Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should it just go straight into the glob, instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, I'm not sure if .pyd would kill the windows usage...?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yep, we can't include .pyd in the exclusion list as this is what the the windows build produces.

@pllim

pllim commented Oct 30, 2018

Copy link
Copy Markdown
Member

Interesting CircleCI failure -- probably not related?

Worker 'gw3' crashed while running 'astropy/io/fits/tests/test_util.py::TestUtils::()::test_ignore_sigint'

@jamienoss

Copy link
Copy Markdown
Contributor

@pllim it's not related.

@jamienoss

jamienoss commented Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Unless... Nope, unrelated.

@saimn

saimn commented Oct 30, 2018 •

Copy link
Copy Markdown
Contributor

Numpy has a function to load libraries with ctypes, taking care of the cross-platform differences (numpy.ctypeslib.load_library). We use it for MPDAF without any problem so far (but also without Windows testing/support).
https://git-cral.univ-lyon1.fr/MUSE/mpdaf/blob/master/lib/mpdaf/tools/ctools.py#L50-57
https://docs.scipy.org/doc/numpy/reference/routines.ctypeslib.html#numpy.ctypeslib.load_library
https://github.com/numpy/numpy/blob/master/numpy/ctypeslib.py#L91

@jamienoss

Copy link
Copy Markdown
Contributor

@saimn That sounds awesome! Thanks for the intel. I'll take it for a quick spin on windows tomorrow, but I imagine it works great, probably by design definition.

It explicitly solves the lib ext issue that we're having here.

libname : str
Name of the library, which can have ‘lib’ as a prefix, but without an extension.

LIBRARY_PATH = os.path.dirname(__file__)

try:
lib_convolve = load_library("lib_convolve", LIBRARY_PATH)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome, nice and neat.

@pllim

pllim commented Oct 31, 2018

Copy link
Copy Markdown
Member

This is okay to merge when you think it is ready. Thanks!

@jamienoss

Copy link
Copy Markdown
Contributor

@astrofrog I opened #8050, just an FYI incase you felt it necessary to add the test to this PR.

@jamienoss

Copy link
Copy Markdown
Contributor

@astrofrog I tested this on windows and it works as it should. Sorry, I wasn't sure what you dev on and it was quick enough for me to test.

@astrofrog

Copy link
Copy Markdown
Member Author

I'll merge this for now, I don't have time to figure out a test, but if you do please open a PR!

@astrofrog
astrofrog merged commit f533845 into astropy:master Oct 31, 2018
@jamienoss

Copy link
Copy Markdown
Contributor

Thanks again @saimn, nicely saved.

bsipocz pushed a commit that referenced this pull request Oct 31, 2018
@jamienoss

jamienoss commented Nov 2, 2018 •

Copy link
Copy Markdown
Contributor

@pllim Would you mind quickly adding the convolution label, please, cheers.

Comment thread astropy/convolution/convolve.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Affects-dev PRs and issues that do not impact an existing Astropy release Bug convolution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

convolution cannot be imported

6 participants