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

vectorize HierarchicalHealpixMap evluate #33

Merged
merged 3 commits into from
Jul 6, 2023

Conversation

henrysky
Copy link
Contributor

@henrysky henrysky commented Jul 5, 2023

This PR adds a vectorized version to evaluate HierarchicalHealpixMap.

I have tested the performance of evaluating Combined19 on the whole APOGEE DR17. Simple for loop took 6h56m41s while this PR vectorized function only took 26s. So we can have a speedup of ~1000 times!!

To maintain compatibility, evaluating a single l and b will fall back to the original code while evaluating array input of l and b will use the vectorized code.

I have check the consistency of the result on the whole APOGEE DR17 by np.all(np.isclose(ebv_original, ebv_vectorized, equal_nan=True)) and the code to generate the comparison result is as follow

import numpy as np
from mwdust import Combined19
from astropy.io import fits
from astroNN.apogee import allstar
from galpy.util.coords import radec_to_lb

allstar_f = fits.getdata(allstar(dr=17))

lb = radec_to_lb(allstar_f['RA'], allstar_f['DEC'], degree=True)
ls, bs = lb[:, 0], lb[:, 1]

c19map = Combined19()
ebv = np.zeros_like(ls) * np.nan
for idx, (l, b, d) in enumerate(zip(ls, bs, 1 / allstar_f['GAIAEDR3_PARALLAX'])):
    try:
        ebv[idx] = c19map(l, b, d)
    except:
        pass
np.save("ebv_original.npy", ebv)

ebv_original.zip

import numpy as np
from mwdust import Combined19
from astropy.io import fits
from astroNN.apogee import allstar
from galpy.util.coords import radec_to_lb

allstar_f = fits.getdata(allstar(dr=17))

lb = radec_to_lb(allstar_f['RA'], allstar_f['DEC'], degree=True)
ls, bs = lb[:, 0], lb[:, 1]

c19map = Combined19()
# you don't need to call _evaluate_vectorize directly, but here I will do that to clearly show which code is used
ebv = c19map._evaluate_vectorize(ls, bs, 1 / allstar_f['GAIAEDR3_PARALLAX'])
np.save("ebv_vectorized.npy", ebv)

ebv_vectorized.zip

@jobovy
Copy link
Owner

jobovy commented Jul 5, 2023

I have tested the performance of evaluating Combined19 on the whole APOGEE DR17. Simple for loop took 6h56m41s while this PR vectorized function only took 26s. So we can have a speedup of ~1000 times!!

🤗

This looks great, I'll try to give it a test run later today, but the code looks good to me. One question though: I don't think this code currently allows you to ask for multiple distance moduli at each (l,b), would that be useful to add? I think the code currently allows you to do an array of distance moduli at a single (l,b). Looks like allowing multiple distance moduli at each (l,b) would not be too hard, just need slightly different logic in setting up the results array in the vectorized function I think.

Also, what is the reason not just use the vectorized function for single (l,b) calls? If it works, I think it would be better not to duplicate the code. Or is it much slower to use the vectorized function for a single object?

@jobovy
Copy link
Owner

jobovy commented Jul 5, 2023

Also would be good to add a test.

@henrysky
Copy link
Contributor Author

henrysky commented Jul 5, 2023

I have just added test as well as using the vectorized evaluation even with a single (l, b) as well as single (l,b) with multiple distance.

Also, what is the reason not just use the vectorized function for single (l,b) calls? If it works, I think it would be better not to duplicate the code. Or is it much slower to use the vectorized function for a single object?

I have tested the performance for a single (l, b) with four different distance, it is about 40ms with the original code and about 130ms with the vectorized code. So the difference in time is very small even it is technically a few times slower for a single (l, b).

@jobovy jobovy merged commit 2ae9ed1 into jobovy:main Jul 6, 2023
27 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.

None yet

2 participants