From 2186fbb751301ac5f5deef401846bf41dc7bbb90 Mon Sep 17 00:00:00 2001 From: Abhimanyu Susobhanan Date: Mon, 25 Sep 2023 10:45:42 -0500 Subject: [PATCH] to_value --- src/pint/models/astrometry.py | 6 +++--- src/pint/models/binary_ell1.py | 2 +- src/pint/models/dispersion_model.py | 2 +- src/pint/models/fdjump.py | 4 ++-- src/pint/models/frequency_dependent.py | 2 +- src/pint/models/ifunc.py | 2 +- src/pint/models/noise_model.py | 16 ++++++++-------- src/pint/models/parameter.py | 14 +++++++------- src/pint/models/pulsar_binary.py | 2 +- .../stand_alone_psr_binaries/DDK_model.py | 18 +++++++++++------- .../stand_alone_psr_binaries/DD_model.py | 4 ++-- .../stand_alone_psr_binaries/ELL1H_model.py | 4 ++-- .../stand_alone_psr_binaries/ELL1_model.py | 12 ++++++------ .../stand_alone_psr_binaries/binary_generic.py | 8 ++++---- .../stand_alone_psr_binaries/binary_orbits.py | 10 +++++----- src/pint/models/timing_model.py | 2 +- src/pint/models/troposphere_delay.py | 4 ++-- 17 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/pint/models/astrometry.py b/src/pint/models/astrometry.py index 672a8bd45..df9d5fb78 100644 --- a/src/pint/models/astrometry.py +++ b/src/pint/models/astrometry.py @@ -144,7 +144,7 @@ def solar_system_geometric_delay(self, toas, acc_delay=None): if np.any(c): L_hat = self.ssb_to_psb_xyz_ICRS(epoch=tbl["tdbld"][c].astype(np.float64)) re_dot_L = np.sum(tbl["ssb_obs_pos"][c] * L_hat, axis=1) - delay[c] = -re_dot_L.to(ls).value + delay[c] = -re_dot_L.to_value(ls) if self.PX.value != 0.0: L = (1.0 / self.PX.value) * u.kpc # TODO: np.sum currently loses units in some cases... @@ -153,8 +153,8 @@ def solar_system_geometric_delay(self, toas, acc_delay=None): * tbl["ssb_obs_pos"].unit ** 2 ) delay[c] += ( - (0.5 * (re_sqr / L) * (1.0 - re_dot_L**2 / re_sqr)).to(ls).value - ) + 0.5 * (re_sqr / L) * (1.0 - re_dot_L**2 / re_sqr) + ).to_value(ls) return delay * u.second def get_d_delay_quantities(self, toas, include="all"): diff --git a/src/pint/models/binary_ell1.py b/src/pint/models/binary_ell1.py index 8e3c93b4e..29044049d 100644 --- a/src/pint/models/binary_ell1.py +++ b/src/pint/models/binary_ell1.py @@ -262,7 +262,7 @@ def change_binary_epoch(self, new_epoch): tasc_ld = self.TASC.quantity.tdb.mjd_long dt = (new_epoch.tdb.mjd_long - tasc_ld) * u.day d_orbits = dt / PB - PBDOT * dt**2 / (2.0 * PB**2) - n_orbits = np.round(d_orbits.to(u.Unit(""))) + n_orbits = np.round(d_orbits.to(u.dimensionless_unscaled)) if n_orbits == 0: return dt_integer_orbits = PB * n_orbits + PB * PBDOT * n_orbits**2 / 2.0 diff --git a/src/pint/models/dispersion_model.py b/src/pint/models/dispersion_model.py index 6ef81258e..f7eea8bc2 100644 --- a/src/pint/models/dispersion_model.py +++ b/src/pint/models/dispersion_model.py @@ -266,7 +266,7 @@ def d_dm_d_DMs( else: DMEPOCH = self.DMEPOCH.value dt = (toas["tdbld"] - DMEPOCH) * u.day - dt_value = (dt.to(u.yr)).value + dt_value = dt.to_value(u.yr) return taylor_horner(dt_value, dm_terms) * (self.DM.units / par.units) def change_dmepoch(self, new_epoch): diff --git a/src/pint/models/fdjump.py b/src/pint/models/fdjump.py index 9cec3354e..752cceda4 100644 --- a/src/pint/models/fdjump.py +++ b/src/pint/models/fdjump.py @@ -139,9 +139,9 @@ def get_freq_y(self, toas): freq = tbl["freq"] y = ( - np.log(freq.to(u.GHz).value) + np.log(freq.to_value(u.GHz)) if self.FDJUMPLOG.value - else freq.to(u.GHz).value + else freq.to_value(u.GHz) ) non_finite = np.invert(np.isfinite(y)) y[non_finite] = 0.0 diff --git a/src/pint/models/frequency_dependent.py b/src/pint/models/frequency_dependent.py index 3094a8a9a..3b55be380 100644 --- a/src/pint/models/frequency_dependent.py +++ b/src/pint/models/frequency_dependent.py @@ -85,7 +85,7 @@ def FD_delay(self, toas, acc_delay=None): def FD_delay_frequency(self, freq): """Compute the FD delay at an array of frequencies.""" FD_mapping = self.get_prefix_mapping_component("FD") - log_freq = np.log(freq.to(u.GHz).value) + log_freq = np.log(freq.to_value(u.GHz)) non_finite = np.invert(np.isfinite(log_freq)) log_freq[non_finite] = 0.0 FD_coeff = [ diff --git a/src/pint/models/ifunc.py b/src/pint/models/ifunc.py index 91a3b79cb..9ff0ce441 100644 --- a/src/pint/models/ifunc.py +++ b/src/pint/models/ifunc.py @@ -110,7 +110,7 @@ def ifunc_phase(self, toas, delays): # the MJDs(x) and offsets (y) of the interpolation points x, y = np.asarray([t.quantity for t in terms]).T # form barycentered times - ts = toas.table["tdbld"] - delays.to(u.day).value + ts = toas.table["tdbld"] - delays.to_value(u.day) times = np.zeros(len(ts)) # Determine what type of interpolation we are doing. diff --git a/src/pint/models/noise_model.py b/src/pint/models/noise_model.py index 49036fd8a..0654e4149 100644 --- a/src/pint/models/noise_model.py +++ b/src/pint/models/noise_model.py @@ -160,7 +160,7 @@ def scale_toa_sigma(self, toas): return sigma_scaled def sigma_scaled_cov_matrix(self, toas): - scaled_sigma = self.scale_toa_sigma(toas).to(u.s).value ** 2 + scaled_sigma = self.scale_toa_sigma(toas).to_value(u.s) ** 2 return np.diag(scaled_sigma) @@ -337,7 +337,7 @@ def get_noise_basis(self, toas): A quantization matrix maps TOAs to observing epochs. """ tbl = toas.table - t = (tbl["tdbld"].quantity * u.day).to(u.s).value + t = (tbl["tdbld"].quantity * u.day).to_value(u.s) ecorrs = self.get_ecorrs() umats = [] for ec in ecorrs: @@ -363,7 +363,7 @@ def get_noise_weights(self, toas, nweights=None): """ ecorrs = self.get_ecorrs() if nweights is None: - ts = (toas.table["tdbld"].quantity * u.day).to(u.s).value + ts = (toas.table["tdbld"].quantity * u.day).to_value(u.s) nweights = [ get_ecorr_nweights(ts[ec.select_toa_mask(toas)]) for ec in ecorrs ] @@ -371,7 +371,7 @@ def get_noise_weights(self, toas, nweights=None): weights = np.zeros(nc) nctot = 0 for ec, nn in zip(ecorrs, nweights): - weights[nctot : nn + nctot] = ec.quantity.to(u.s).value ** 2 + weights[nctot : nn + nctot] = ec.quantity.to_value(u.s) ** 2 nctot += nn return weights @@ -462,7 +462,7 @@ def get_noise_basis(self, toas): See the documentation for pl_dm_basis_weight_pair function for details.""" tbl = toas.table - t = (tbl["tdbld"].quantity * u.day).to(u.s).value + t = (tbl["tdbld"].quantity * u.day).to_value(u.s) freqs = self._parent.barycentric_radio_freq(toas).to(u.MHz) fref = 1400 * u.MHz D = (fref.value / freqs.value) ** 2 @@ -476,7 +476,7 @@ def get_noise_weights(self, toas): See the documentation for pl_dm_basis_weight_pair for details.""" tbl = toas.table - t = (tbl["tdbld"].quantity * u.day).to(u.s).value + t = (tbl["tdbld"].quantity * u.day).to_value(u.s) amp, gam, nf = self.get_pl_vals() Ffreqs = get_rednoise_freqs(t, nf) return powerlaw(Ffreqs, amp, gam) * Ffreqs[0] @@ -592,7 +592,7 @@ def get_noise_basis(self, toas): See the documentation for pl_rn_basis_weight_pair function for details.""" tbl = toas.table - t = (tbl["tdbld"].quantity * u.day).to(u.s).value + t = (tbl["tdbld"].quantity * u.day).to_value(u.s) nf = self.get_pl_vals()[2] return create_fourier_design_matrix(t, nf) @@ -602,7 +602,7 @@ def get_noise_weights(self, toas): See the documentation for pl_rn_basis_weight_pair for details.""" tbl = toas.table - t = (tbl["tdbld"].quantity * u.day).to(u.s).value + t = (tbl["tdbld"].quantity * u.day).to_value(u.s) amp, gam, nf = self.get_pl_vals() Ffreqs = get_rednoise_freqs(t, nf) return powerlaw(Ffreqs, amp, gam) * Ffreqs[0] diff --git a/src/pint/models/parameter.py b/src/pint/models/parameter.py index 5bf0e9d48..8af5aa1d7 100644 --- a/src/pint/models/parameter.py +++ b/src/pint/models/parameter.py @@ -409,7 +409,7 @@ def _print_uncertainty(self, uncertainty): This converts the :class:`~astropy.units.Quantity` provided to the appropriate units, extracts the value, and converts that to a string. """ - return str(uncertainty.to(self.units).value) + return str(uncertainty.to_value(self.units)) def __repr__(self): out = "{0:16s}{1:20s}".format(f"{self.__class__.__name__}(", self.name) @@ -783,7 +783,7 @@ def _set_uncertainty(self, val): def str_quantity(self, quan): """Quantity as a string (for floating-point values).""" - v = quan.to(self.units).value + v = quan.to_value(self.units) if self._long_double and not isinstance(v, np.longdouble): raise ValueError( "Parameter is supposed to contain long double values but contains a float" @@ -798,9 +798,9 @@ def _get_value(self, quan): return quan elif isinstance(quan, list): # for pairParamters - return [x.to(self.units).value for x in quan] + return [x.to_value(self.units) for x in quan] else: - return quan.to(self.units).value + return quan.to_value(self.units) def as_ufloat(self, units=None): """Return the parameter as a :class:`uncertainties.ufloat` @@ -1606,7 +1606,7 @@ def str_quantity(self, quan): return self.param_comp.str_quantity(quan) def _print_uncertainty(self, uncertainty): - return str(uncertainty.to(self.units).value) + return str(uncertainty.to_value(self.units)) def name_matches(self, name): return self.param_comp.name_matches(name) @@ -2239,8 +2239,8 @@ def str_quantity(self, quan): if len(quan) != 2: raise ValueError(f"Don't know how to print this as a pair: {quan}") - v0 = quan[0].to(self.units).value - v1 = quan[1].to(self.units).value + v0 = quan[0].to_value(self.units) + v1 = quan[1].to_value(self.units) if self._long_double: if not isinstance(v0, np.longdouble): raise TypeError( diff --git a/src/pint/models/pulsar_binary.py b/src/pint/models/pulsar_binary.py index b27dc923c..725abcf21 100644 --- a/src/pint/models/pulsar_binary.py +++ b/src/pint/models/pulsar_binary.py @@ -508,7 +508,7 @@ def change_binary_epoch(self, new_epoch): t0_ld = self.T0.quantity.tdb.mjd_long dt = (new_epoch.tdb.mjd_long - t0_ld) * u.day d_orbits = dt / PB - PBDOT * dt**2 / (2.0 * PB**2) - n_orbits = np.round(d_orbits.to(u.Unit(""))) + n_orbits = np.round(d_orbits.to(u.dimensionless_unscaled)) if n_orbits == 0: return dt_integer_orbits = PB * n_orbits + PB * PBDOT * n_orbits**2 / 2.0 diff --git a/src/pint/models/stand_alone_psr_binaries/DDK_model.py b/src/pint/models/stand_alone_psr_binaries/DDK_model.py index 709033aae..4c94f8119 100644 --- a/src/pint/models/stand_alone_psr_binaries/DDK_model.py +++ b/src/pint/models/stand_alone_psr_binaries/DDK_model.py @@ -175,31 +175,35 @@ def kin(self): def d_SINI_d_KIN(self): # with u.set_enabled_equivalencies(u.dimensionless_angles()): - return np.cos(self.kin()).to(u.Unit("") / self.KIN.unit) + return np.cos(self.kin()).to(u.dimensionless_unscaled / self.KIN.unit) def d_SINI_d_KOM(self): if not self.K96: - return np.cos(self.kin()) * u.Unit("") / self.KOM.unit + return np.cos(self.kin()) * u.dimensionless_unscaled / self.KOM.unit d_si_d_kom = ( (-self.PMLONG_DDK * self.cos_KOM - self.PMLAT_DDK * self.sin_KOM) * self.tt0 * np.cos(self.kin()) ) # with u.set_enabled_equivalencies(u.dimensionless_angles()): - return d_si_d_kom.to(u.Unit("") / self.KOM.unit) + return d_si_d_kom.to(u.dimensionless_unscaled / self.KOM.unit) def d_SINI_d_T0(self): if not self.K96: - return np.ones(len(self.tt0)) * u.Unit("") / self.T0.unit + return np.ones(len(self.tt0)) * u.dimensionless_unscaled / self.T0.unit d_si_d_kom = -(-self.PMLONG_DDK * self.sin_KOM + self.PMLAT_DDK * self.cos_KOM) - return d_si_d_kom.to(u.Unit("") / self.T0.unit) + return d_si_d_kom.to(u.dimensionless_unscaled / self.T0.unit) def d_SINI_d_par(self, par): par_obj = getattr(self, par) try: ko_func = getattr(self, f"d_SINI_d_{par}") except Exception: - ko_func = lambda: np.zeros(len(self.tt0)) * u.Unit("") / par_obj.unit + ko_func = ( + lambda: np.zeros(len(self.tt0)) + * u.dimensionless_unscaled + / par_obj.unit + ) return ko_func() def d_kin_proper_motion_d_KOM(self): @@ -394,7 +398,7 @@ def delta_sini_parallax(self): / PX_kpc * (self.delta_I0() * self.sin_KOM - self.delta_J0() * self.cos_KOM) ) - return delta_sini.to("") + return delta_sini.to(u.dimensionless_unscaled) def delta_a1_parallax(self): """ diff --git a/src/pint/models/stand_alone_psr_binaries/DD_model.py b/src/pint/models/stand_alone_psr_binaries/DD_model.py index e9b82bdc3..e15b7b085 100644 --- a/src/pint/models/stand_alone_psr_binaries/DD_model.py +++ b/src/pint/models/stand_alone_psr_binaries/DD_model.py @@ -149,7 +149,7 @@ def d_omega_d_OMDOT(self): n = 2*pi/PB dOmega/dOMDOT = PB/2*pi*nu """ - PB = (self.pb()).to("second") + PB = (self.pb()).to(u.s) nu = self.nu() return PB / (2 * np.pi * u.rad) * nu @@ -568,7 +568,7 @@ def nhat(self): n = 2*pi/PB # should here be M() """ cosE = np.cos(self.E()) - return 2.0 * np.pi / self.pb().to("second") / (1 - self.ecc() * cosE) + return 2.0 * np.pi / self.pb().to(u.s) / (1 - self.ecc() * cosE) def d_nhat_d_par(self, par): """Derivative. diff --git a/src/pint/models/stand_alone_psr_binaries/ELL1H_model.py b/src/pint/models/stand_alone_psr_binaries/ELL1H_model.py index a976baef1..c2478fbc0 100644 --- a/src/pint/models/stand_alone_psr_binaries/ELL1H_model.py +++ b/src/pint/models/stand_alone_psr_binaries/ELL1H_model.py @@ -46,8 +46,8 @@ def __init__(self): { "H3": 0.0 * u.second, "H4": 0.0 * u.second, - "STIGMA": 0.0 * u.Unit(""), - "NHARMS": 3 * u.Unit(""), + "STIGMA": 0.0 * u.dimensionless_unscaled, + "NHARMS": 3 * u.dimensionless_unscaled, } ) self.binary_params = list(self.param_default_value.keys()) diff --git a/src/pint/models/stand_alone_psr_binaries/ELL1_model.py b/src/pint/models/stand_alone_psr_binaries/ELL1_model.py index b3fd8133d..1191a424b 100644 --- a/src/pint/models/stand_alone_psr_binaries/ELL1_model.py +++ b/src/pint/models/stand_alone_psr_binaries/ELL1_model.py @@ -45,7 +45,7 @@ def ttasc(self): t = self.t if not hasattr(self.t, "unit") or self.t.unit is None: t = self.t * u.day - return (t - self.TASC.value * u.day).to("second") + return (t - self.TASC.value * u.day).to(u.s) def a1(self): """ELL1 model a1 calculation. @@ -103,14 +103,14 @@ def Phi(self): return self.M() def orbits_ELL1(self): - PB = (self.pb()).to("second") + PB = (self.pb()).to(u.s) PBDOT = self.pbdot() ttasc = self.ttasc() return (ttasc / PB - 0.5 * PBDOT * (ttasc / PB) ** 2).decompose() def d_Phi_d_TASC(self): """dPhi/dTASC""" - PB = self.pb().to("second") + PB = self.pb().to(u.s) PBDOT = self.pbdot() ttasc = self.ttasc() return (PBDOT * ttasc / PB - 1.0) * 2 * np.pi * u.rad / PB @@ -157,7 +157,7 @@ def delayI(self): Dre = self.delayR() Drep = self.Drep() Drepp = self.Drepp() - PB = self.pb().to("second") + PB = self.pb().to(u.s) nhat = 2 * np.pi / self.pb() return ( Dre @@ -182,7 +182,7 @@ def d_delayI_d_par(self, par): Dre = self.delayR() Drep = self.Drep() Drepp = self.Drepp() - PB = self.pb().to("second") + PB = self.pb().to(u.s) nhat = 2 * np.pi / self.pb() d_delayI_d_Dre = ( @@ -214,7 +214,7 @@ def ELL1_ecc(self): def ELL1_T0(self): return self.TASC + self.pb() / (2 * np.pi) * ( np.arctan(self.eps1() / self.eps2()) - ).to(u.Unit(""), equivalencies=u.dimensionless_angles()) + ).to(u.dimensionless_unscaled, equivalencies=u.dimensionless_angles()) ############################### def d_delayR_da1(self): diff --git a/src/pint/models/stand_alone_psr_binaries/binary_generic.py b/src/pint/models/stand_alone_psr_binaries/binary_generic.py index c62f174a3..5cad0a48f 100644 --- a/src/pint/models/stand_alone_psr_binaries/binary_generic.py +++ b/src/pint/models/stand_alone_psr_binaries/binary_generic.py @@ -376,7 +376,7 @@ def get_tt0(self, barycentricTOA): T0 = self.T0 if not hasattr(barycentricTOA, "unit") or barycentricTOA.unit is None: barycentricTOA = barycentricTOA * u.day - return (barycentricTOA - T0).to("second") + return (barycentricTOA - T0).to(u.s) def ecc(self): """Calculate eccentricity with EDOT""" @@ -473,7 +473,7 @@ def d_M_d_par(self, par): par_obj = getattr(self, par) result = self.orbits_cls.d_orbits_d_par(par) with u.set_enabled_equivalencies(u.dimensionless_angles()): - result = result.to(u.Unit("") / par_obj.unit) + result = result.to(u.dimensionless_unscaled / par_obj.unit) return result ############################################### @@ -629,7 +629,7 @@ def d_nu_d_par(self, par): return np.zeros(len(self.tt0)) * nu.unit / par_obj.unit def omega(self): - PB = self.pb().to("second") + PB = self.pb().to(u.s) OMDOT = self.OMDOT OM = self.OM return OM + OMDOT * self.tt0 @@ -682,7 +682,7 @@ def pbprime(self): return self.pb() - self.pbdot() * self.tt0 def P(self): - return self.P0 + self.P1 * (self.t - self.PEPOCH).to("second") + return self.P0 + self.P1 * (self.t - self.PEPOCH).to(u.s) def t0(self): return self.t - self.PEPOCH diff --git a/src/pint/models/stand_alone_psr_binaries/binary_orbits.py b/src/pint/models/stand_alone_psr_binaries/binary_orbits.py index 3a1d60bbb..68704321c 100644 --- a/src/pint/models/stand_alone_psr_binaries/binary_orbits.py +++ b/src/pint/models/stand_alone_psr_binaries/binary_orbits.py @@ -97,7 +97,7 @@ def __init__(self, parent, orbit_params=["PB", "PBDOT", "XPBDOT", "T0"]): def orbits(self): """Orbital phase (number of orbits since T0).""" - PB = self.PB.to("second") + PB = self.PB.to(u.s) PBDOT = self.PBDOT XPBDOT = self.XPBDOT return ( @@ -114,14 +114,14 @@ def pbdot_orbit(self): def d_orbits_d_T0(self): """The derivatve of orbits with respect to T0.""" - PB = self.PB.to("second") + PB = self.PB.to(u.s) PBDOT = self.PBDOT XPBDOT = self.XPBDOT return ((PBDOT - XPBDOT) * self.tt0 / PB - 1.0) * 2 * np.pi * u.rad / PB def d_orbits_d_PB(self): """dM/dPB this could be a generic function""" - PB = self.PB.to("second") + PB = self.PB.to(u.s) PBDOT = self.PBDOT XPBDOT = self.XPBDOT return ( @@ -133,12 +133,12 @@ def d_orbits_d_PB(self): def d_orbits_d_PBDOT(self): """dM/dPBDOT this could be a generic function""" - PB = self.PB.to("second") + PB = self.PB.to(u.s) return -np.pi * u.rad * self.tt0**2 / PB**2 def d_orbits_d_XPBDOT(self): """dM/dPBDOT this could be a generic function""" - PB = self.PB.to("second") + PB = self.PB.to(u.s) return -np.pi * u.rad * self.tt0**2 / PB**2 def d_pbprime_d_PB(self): diff --git a/src/pint/models/timing_model.py b/src/pint/models/timing_model.py index b6abe6270..e1d1ba0d3 100644 --- a/src/pint/models/timing_model.py +++ b/src/pint/models/timing_model.py @@ -1470,7 +1470,7 @@ def toa_covariance_matrix(self, toas): """ result = np.zeros((len(toas), len(toas))) if "ScaleToaError" not in self.components: - result += np.diag(toas.table["error"].quantity.to(u.s).value ** 2) + result += np.diag(toas.table["error"].quantity.to_value(u.s) ** 2) for nf in self.covariance_matrix_funcs: result += nf(toas) diff --git a/src/pint/models/troposphere_delay.py b/src/pint/models/troposphere_delay.py index 911f2ad12..9b482ef26 100644 --- a/src/pint/models/troposphere_delay.py +++ b/src/pint/models/troposphere_delay.py @@ -236,7 +236,7 @@ def pressure_from_altitude(self, H): gph = self.EARTH_R * H / (self.EARTH_R + H) # geopotential height if gph > 11 * u.km: log.warning("Pressure approximation invalid for elevations above 11 km") - T = 288.15 - 0.0065 * H.to(u.m).value # temperature lapse + T = 288.15 - 0.0065 * H.to_value(u.m) # temperature lapse return 101.325 * (288.15 / T) ** -5.25575 * u.kPa def zenith_delay(self, lat, H): @@ -314,7 +314,7 @@ def mapping_function(self, alt, lat, H, mjd): # now add in the mapping correction based on height fcorrection = self._herring_map(alt, self.A_HT, self.B_HT, self.C_HT) - return baseMap + (1 / np.sin(alt) - fcorrection) * H.to(u.km).value + return baseMap + (1 / np.sin(alt) - fcorrection) * H.to_value(u.km) def wet_map(self, alt, lat): """This is very similar to the normal mapping function except it uses different