Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_vector_addition(self):
phase1 = Phase([0, 2, 2, 2], [0, 0.3, 0.3, 0])
phase2 = Phase([0, 1, 1, 1], [0, 0.1, 0.2, -0.5])
phasesum = phase1 + phase2
assert isinstance(phasesum, Phase)
assert_equal(phasesum.int, u.Quantity([0, 3, 4, 3]))
assert_equal(phasesum.frac, u.Quantity([0, 0.4, -0.5, -0.5]))
def test_commutative_scalar_addition(self):
phase1 = Phase(2, 0.5)
phase2 = Phase(1, 0.3)
sum1 = phase1 + phase2
sum2 = phase2 + phase1
assert_equal(sum1.int, sum2.int)
assert_equal(sum1.frac, sum2.frac)
def test_vector_addition_with_scalar(self):
vecphase = Phase([0, 2, 2, 2], [0, 0.3, 0.3, 0])
scalarphase = Phase(1, 0.1)
sum1 = vecphase + scalarphase
assert isinstance(sum1, Phase)
assert_equal(sum1.int, u.Quantity([1, 3, 3, 3]))
assert_equal(sum1.frac, u.Quantity([0.1, 0.4, 0.4, 0.1]))
# check commutivity
sum2 = scalarphase + vecphase
assert isinstance(sum2, Phase)
assert_equal(sum1.int, sum2.int)
assert_equal(sum1.frac, sum2.frac)
modelphase = (
self.model.phase(self.toas, abs_phase=True) + delta_pulse_numbers
)
# First assign each TOA to the correct relative pulse number, including
# and delta_pulse_numbers (from PHASE lines or adding phase jumps in GUI)
residualphase = modelphase - Phase(pulse_num, np.zeros_like(pulse_num))
# This converts from a Phase object to a np.float128
full = residualphase.int + residualphase.frac
# If not tracking then do the usual nearest pulse number calculation
else:
# Compute model phase
modelphase = self.model.phase(self.toas) + delta_pulse_numbers
# Here it subtracts the first phase, so making the first TOA be the
# reference. Not sure this is a good idea.
if self.subtract_mean:
modelphase -= Phase(modelphase.int[0], modelphase.frac[0])
# Here we discard the integer portion of the residual and replace it with 0
# This is effectively selecting the nearst pulse to compute the residual to.
residualphase = Phase(np.zeros_like(modelphase.frac), modelphase.frac)
# This converts from a Phase object to a np.float128
full = residualphase.int + residualphase.frac
# If we are using pulse numbers, do we really want to subtract any kind of mean?
if not self.subtract_mean:
return full
if not self.use_weighted_mean:
mean = full.mean()
else:
# Errs for weighted sum. Units don't matter since they will
# cancel out in the weighted sum.
if np.any(self.toas.get_errors() == 0):
raise ValueError(
pn_act = rs.int
addPhase = pn_act - (pulse_num + addpn)
rs = rs.frac
rs += addPhase
if not weighted_mean:
rs -= rs.mean()
else:
w = 1.0 / (np.array(self.toas.get_errors())**2)
wm = (rs*w).sum() / w.sum()
rs -= wm
return rs
if not weighted_mean:
rs -= Phase(0.0,rs.frac.mean())
else:
# Errs for weighted sum. Units don't matter since they will
# cancel out in the weighted sum.
if np.any(self.toas.get_errors() == 0):
raise ValueError('TOA errors are zero - cannot calculate residuals')
w = 1.0/(np.array(self.toas.get_errors())**2)
wm = (rs.frac*w).sum() / w.sum()
rs -= Phase(0.0,wm)
return rs.frac
def phase(self, toas, abs_phase=False):
"""Return the model-predicted pulse phase for the given TOAs."""
# First compute the delays to "pulsar time"
delay = self.delay(toas)
phase = Phase(np.zeros(toas.ntoas), np.zeros(toas.ntoas))
# Then compute the relevant pulse phases
for pf in self.phase_funcs:
phase += Phase(pf(toas, delay))
# If the absolute phase flag is on, use the TZR parameters to compute
# the absolute phase.
if abs_phase:
if "AbsPhase" not in list(self.components.keys()):
# if no absolute phase (TZRMJD), add the component to the model and calculate it
from pint.models import absolute_phase
self.add_component(absolute_phase.AbsPhase())
self.make_TZR_toa(
toas
) # TODO:needs timfile to get all toas, but model doesn't have access to timfile. different place for this?
tz_toa = self.get_TZR_toa(toas)
tz_delay = self.delay(tz_toa)
tz_phase = Phase(np.zeros(len(toas.table)), np.zeros(len(toas.table)))
for pf in self.phase_funcs:
def evalabsphase(self, t):
"""Return the phase at time t, computed with this polyco entry"""
dt = (data2longdouble(t) - self.tmid.value) * data2longdouble(1440.0)
# Compute polynomial by factoring out the dt's
phase = Phase(
self.coeffs[self.ncoeff - 1]
) # Compute phase using two long double
for i in range(self.ncoeff - 2, -1, -1):
pI = Phase(dt * phase.int)
pF = Phase(dt * phase.frac)
c = Phase(self.coeffs[i])
phase = pI + pF + c
# Add DC term
phase += self.rphase + Phase(dt * 60.0 * self.f0)
return phase
else:
w = 1.0 / (np.array(self.toas.get_errors())**2)
wm = (rs*w).sum() / w.sum()
rs -= wm
return rs
if not weighted_mean:
rs -= Phase(0.0,rs.frac.mean())
else:
# Errs for weighted sum. Units don't matter since they will
# cancel out in the weighted sum.
if np.any(self.toas.get_errors() == 0):
raise ValueError('TOA errors are zero - cannot calculate residuals')
w = 1.0/(np.array(self.toas.get_errors())**2)
wm = (rs.frac*w).sum() / w.sum()
rs -= Phase(0.0,wm)
return rs.frac
# If the absolute phase flag is on, use the TZR parameters to compute
# the absolute phase.
if abs_phase:
if "AbsPhase" not in list(self.components.keys()):
# if no absolute phase (TZRMJD), add the component to the model and calculate it
from pint.models import absolute_phase
self.add_component(absolute_phase.AbsPhase())
self.make_TZR_toa(
toas
) # TODO:needs timfile to get all toas, but model doesn't have access to timfile. different place for this?
tz_toa = self.get_TZR_toa(toas)
tz_delay = self.delay(tz_toa)
tz_phase = Phase(np.zeros(len(toas.table)), np.zeros(len(toas.table)))
for pf in self.phase_funcs:
tz_phase += Phase(pf(tz_toa, tz_delay))
return phase - tz_phase
else:
return phase