Skip to content

Commit

Permalink
Merge pull request #300 from nanograv/master
Browse files Browse the repository at this point in the history
Merge master changes into release branch
  • Loading branch information
vallis authored Oct 19, 2021
2 parents d7bc0f6 + df93437 commit ed135c8
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 54 deletions.
5 changes: 4 additions & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .version import version
try:
from .version import version
except:
version = "unknown"

__version__ = version
38 changes: 26 additions & 12 deletions enterprise/pulsar.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _get_radec_from_ecliptic(self, elong, elat):
decj = np.double(eq.dec)

except TypeError:
msg = "WARNING: Cannot fine sky location coordinates "
msg = "WARNING: Cannot find sky location coordinates "
msg += "for PSR {0}. ".format(self.name)
msg += "Setting values to 0.0"
logger.warning(msg)
Expand Down Expand Up @@ -263,7 +263,7 @@ def backend_flags(self):
if flag in flagnames:
ret[:] = np.where(self._flags[flag] == "", ret, self._flags[flag])

return ret
return ret[self._isort]

@property
def theta(self):
Expand All @@ -277,12 +277,12 @@ def phi(self):

@property
def pos(self):
"""Return unit vector to pulsar."""
"""Return unit vector from SSB to pulsar at fiducial POSEPOCH."""
return self._pos

@property
def pos_t(self):
"""Return unit vector to pulsar as function of time."""
"""Return unit vector from SSB to pulsar as function of time."""
return self._pos_t[self._isort, :]

@property
Expand All @@ -302,11 +302,14 @@ def __init__(self, toas, model, sort=True, drop_pintpsr=True, planets=True):
self._sort = sort
self.planets = planets
self.name = model.PSR.value

if not drop_pintpsr:
self.model = model
self.pint_toas = toas

self._toas = np.array(toas.table["tdbld"], dtype="float64") * 86400
# these are TDB but not barycentered
# self._toas = np.array(toas.table["tdbld"], dtype="float64") * 86400
self._toas = np.array(model.get_barycentric_toas(toas).value, dtype="float64") * 86400
# saving also stoas (e.g., for DMX comparisons)
self._stoas = np.array(toas.get_mjds().value, dtype="float64") * 86400
self._residuals = np.array(resids(toas, model).time_resids.to(u.s), dtype="float64")
Expand Down Expand Up @@ -347,8 +350,11 @@ def __init__(self, toas, model, sort=True, drop_pintpsr=True, planets=True):
self._planetssb = self._get_planetssb(toas, model)
self._sunssb = self._get_sunssb(toas, model)

# TODO: pos_t not currently implemented
self._pos_t = np.zeros((len(self._toas), 3))
which_astrometry = (
"AstrometryEquatorial" if "AstrometryEquatorial" in model.components else "AstrometryEcliptic"
)

self._pos_t = model.components[which_astrometry].ssb_to_psb_xyz_ICRS(model.get_barycentric_toas(toas)).value

self.sort_data()

Expand Down Expand Up @@ -384,22 +390,30 @@ def _get_radec(self, model):

def _get_ssb_lsec(self, toas, obs_planet):
"""Get the planet to SSB vector in lightseconds from Pint table"""
if obs_planet not in toas.table.colnames:
err_msg = f"{obs_planet} is not in toas.table.colnames. Either "
err_msg += "`planet` flag is not True in `toas` or further Pint "
err_msg += "development to add additional planets is needed."
raise ValueError(err_msg)
vec = toas.table[obs_planet] + toas.table["ssb_obs_pos"]
return (vec / const.c).to("s").value

def _get_planetssb(self, toas, model):
planetssb = None
"""
Currently Pint only has position vectors for:
[Earth, Jupiter, Saturn, Uranus, Neptune]
No velocity vectors available
[Mercury, Venus, Mars, Pluto] unavailable pending Pint enhancements.
"""
if self.planets:
planetssb = np.zeros((len(self._toas), 9, 6))
# planetssb[:, 0, :] = self.t2pulsar.mercury_ssb
# planetssb[:, 1, :] = self.t2pulsar.venus_ssb
planetssb = np.empty((len(self._toas), 9, 6))
planetssb[:] = np.nan
planetssb[:, 2, :3] = self._get_ssb_lsec(toas, "obs_earth_pos")
# planetssb[:, 3, :] = self.t2pulsar.mars_ssb
planetssb[:, 4, :3] = self._get_ssb_lsec(toas, "obs_jupiter_pos")
planetssb[:, 5, :3] = self._get_ssb_lsec(toas, "obs_saturn_pos")
planetssb[:, 6, :3] = self._get_ssb_lsec(toas, "obs_uranus_pos")
planetssb[:, 7, :3] = self._get_ssb_lsec(toas, "obs_neptune_pos")
# planetssb[:, 8, :] = self.t2pulsar.pluto_ssb

# if hasattr(model, "ELAT") and hasattr(model, "ELONG"):
# for ii in range(9):
Expand Down
7 changes: 0 additions & 7 deletions enterprise/signals/deterministic_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import numpy as np

from enterprise import pulsar
from enterprise.signals import parameter, selections, signal_base, utils
from enterprise.signals.selections import Selection

Expand Down Expand Up @@ -168,12 +167,6 @@ class PhysicalEphemerisSignal(BaseClass):
signal_id = "phys_ephem_" + name if name else "phys_ephem"

def __init__(self, psr):
# not available for PINT yet
if isinstance(psr, pulsar.PintPulsar):
msg = "Physical Ephemeris model is not compatible with PINT "
msg += "at this time."
raise NotImplementedError(msg)

super(PhysicalEphemerisSignal, self).__init__(psr)

if use_epoch_toas:
Expand Down
17 changes: 10 additions & 7 deletions enterprise/signals/signal_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import scipy.linalg as sl
import scipy.sparse as sps
import six
from sksparse.cholmod import cholesky
from sksparse.cholmod import cholesky, CholmodError

# these are defined in parameter.py, but currently imported
# in various places from signal_base.py
Expand Down Expand Up @@ -215,17 +215,20 @@ def __call__(self, xs, phiinv_method="cliques"):
TNT = self._block_TNT(TNTs)
TNr = self._block_TNr(TNrs)

try:
if self.cholesky_sparse:
if self.cholesky_sparse:
try:
cf = cholesky(TNT + sps.csc_matrix(phiinv)) # cf(Sigma)
expval = cf(TNr)
logdet_sigma = cf.logdet()
else:
except CholmodError: # pragma: no cover
return -np.inf
else:
try:
cf = sl.cho_factor(TNT + phiinv) # cf(Sigma)
expval = sl.cho_solve(cf, TNr)
logdet_sigma = 2 * np.sum(np.log(np.diag(cf[0])))
except:
return -np.inf
except sl.LinAlgError: # pragma: no cover
return -np.inf

loglike += 0.5 * (np.dot(TNr, expval) - logdet_sigma - logdet_phi)
else:
Expand All @@ -239,7 +242,7 @@ def __call__(self, xs, phiinv_method="cliques"):
try:
cf = sl.cho_factor(Sigma)
expval = sl.cho_solve(cf, TNr)
except:
except sl.LinAlgError: # pragma: no cover
return -np.inf

logdet_sigma = np.sum(2 * np.log(np.diag(cf[0])))
Expand Down
Loading

0 comments on commit ed135c8

Please sign in to comment.