-
Notifications
You must be signed in to change notification settings - Fork 371
Move the pycbc array type and some constants away from lalsuite #5217
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
46aaec4
dee754d
510c737
cceda58
ad82185
9e4afd1
8b5de89
d15b515
0c6e6b6
eadf777
402f75c
4f14e4c
6f0dce9
74d3197
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """ | ||
| Constants module for PyCBC. | ||
|
|
||
| This module provides constants from various packages (LAL, astropy, numpy) | ||
| based on a preferred order of availability. This helps minimize dependency bloat | ||
| by relying on smaller, more general packages (astropy/numpy) when the large | ||
| LALSuite dependency is not available. | ||
|
|
||
| The priority order for constant lookup is: LAL > Astropy > NumPy | ||
| """ | ||
|
|
||
| import logging | ||
| import warnings | ||
|
|
||
| # We define a global logger for this module | ||
| logger = logging.getLogger('pycbc.constants') | ||
|
|
||
| # Attempt to import LALSuite | ||
| try: | ||
| import lal | ||
| _LAL_AVAILABLE = True | ||
| logger.debug("LAL constants module found.") | ||
| except ImportError: | ||
| lal = None | ||
| _LAL_AVAILABLE = False | ||
| logger.debug("LAL constants module not found.") | ||
|
|
||
|
|
||
| import numpy as np | ||
| from astropy import ( | ||
| constants as aconstants, | ||
| units as aunits | ||
| ) | ||
| # first, do mappings for constants which the fallback is directly in astropy/numpy | ||
| # All are in SI units | ||
| _FALLBACK_MAPPING = { | ||
| 'C_SI': aconstants.c.value, # Speed of light | ||
| 'G_SI': aconstants.G.value, # Gravitational constant | ||
| 'MSUN_SI': aconstants.M_sun.value, # Mass of the Sun | ||
| 'PC_SI': aconstants.pc.value, # Parsec | ||
| 'REARTH_SI': aconstants.R_earth.value, # Earth equatorial radius | ||
| 'YRJUL_SI': aunits.year.to(aunits.s), # years in seconds | ||
| 'PI': np.pi | ||
| } | ||
|
|
||
| # We need to define some constants from astropy values | ||
| MSUN_SI = _FALLBACK_MAPPING['MSUN_SI'] | ||
| C_SI = _FALLBACK_MAPPING['C_SI'] | ||
| G_SI = _FALLBACK_MAPPING['G_SI'] | ||
| MTSUN_SI = MSUN_SI * G_SI / (C_SI ** 3) | ||
|
|
||
| # Add in these hybrid constants | ||
| _FALLBACK_MAPPING.update({ | ||
| 'MTSUN_SI': MTSUN_SI | ||
| }) | ||
|
|
||
| # Define the Constant Lookup Function | ||
| def get_constant(name): | ||
| """ | ||
| Retrieves a constant value by name from the preferred order of packages. | ||
|
|
||
| The order of preference is: LAL > Astropy > NumPy | ||
|
|
||
| Parameters | ||
| ---------- | ||
| name : str | ||
| The name of the constant to retrieve (e.g., 'C_SI' for the speed of light). | ||
|
|
||
| Returns | ||
| ------- | ||
| float or object | ||
| The value of the constant. | ||
|
|
||
| Raises | ||
| ------ | ||
| NotImplementedError | ||
| If the constant is not found in any of the available packages. | ||
| """ | ||
|
|
||
|
|
||
| if _LAL_AVAILABLE: | ||
| return getattr(lal, name) | ||
|
|
||
| elif name in _FALLBACK_MAPPING: | ||
| return _FALLBACK_MAPPING[name] | ||
|
|
||
| raise NotImplementedError( | ||
| 'Contact the PyCBC team, this should never happen. You are ' | ||
| 'trying to use a LAL constant which doesnt have an astropy/numpy ' | ||
| 'fallback defined.' | ||
| ) | ||
|
|
||
| # Expose the constants as attributes of the module | ||
|
|
||
| C_SI = get_constant('C_SI') | ||
|
||
| G_SI = get_constant('G_SI') | ||
| MTSUN_SI = get_constant('MTSUN_SI') | ||
| MSUN_SI = get_constant('MSUN_SI') | ||
| PC_SI = get_constant('PC_SI') | ||
| REARTH_SI = get_constant('REARTH_SI') | ||
| YRJUL_SI = get_constant('YRJUL_SI') | ||
| PI = get_constant('PI') | ||
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.
@GarethCabournDavies @spxiwh
In fact, almost everything in this mapping is something we should consider just not using lal for. A few of these may require checking (earth_si, I could imagine some specific value was chosen that is not known to different precision). Most of these though don't need to come from lal in any situation I can imagine.
I'd even prefer we don't have fallbacks (at least not without understanding the potential use case a bit more), because that means there could be unexpected behavior if say lal is or is not the source.