From ab9af27b2cb1206ecbe05d65081d36f55a515ac5 Mon Sep 17 00:00:00 2001 From: Abhimanyu Susobhanan Date: Sat, 23 Sep 2023 19:41:57 -0500 Subject: [PATCH] phase --- CHANGELOG-unreleased.md | 1 + src/pint/phase.py | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG-unreleased.md b/CHANGELOG-unreleased.md index 1f716e8375..51441f198a 100644 --- a/CHANGELOG-unreleased.md +++ b/CHANGELOG-unreleased.md @@ -13,6 +13,7 @@ the released changes. - Moved design matrix normalization code from `pint.fitter` to the new `pint.utils.normalize_designmatrix()` function. - Made `Residuals` independent of `GLSFitter` (GLS chi2 is now computed using the new function `Residuals._calc_gls_chi2()`). - Optionally avoid certain computations in `Astrometry.get_d_delay_quantities()` method. +- Reduced the number of `Quantity` operations during the creation of a `Phase` object. ### Added - Added `WaveX` model as a `DelayComponent` with Fourier amplitudes as fitted parameters - `Parameter.as_latex` method for latex representation of a parameter. diff --git a/src/pint/phase.py b/src/pint/phase.py index b876abe73a..5386807b52 100644 --- a/src/pint/phase.py +++ b/src/pint/phase.py @@ -1,7 +1,7 @@ from collections import namedtuple import astropy.units as u -import numpy +import numpy as np class Phase(namedtuple("Phase", "int frac")): @@ -51,16 +51,12 @@ def __new__(cls, arg1, arg2=None): pulse phase object with arrays of dimensionless :class:`~astropy.units.Quantity` objects as the ``int`` and ``frac`` parts """ - arg1 = ( - arg1.to(u.dimensionless_unscaled) - if hasattr(arg1, "unit") - else u.Quantity(arg1) - ) + # If arg is scalar, convert to an array of length 1 if arg1.shape == (): arg1 = arg1.reshape((1,)) if arg2 is None: - ff, ii = numpy.modf(arg1) + ff, ii = np.modf(arg1) else: arg2 = ( arg2.to(u.dimensionless_unscaled) @@ -69,8 +65,8 @@ def __new__(cls, arg1, arg2=None): ) if arg2.shape == (): arg2 = arg2.reshape((1,)) - arg1S = numpy.modf(arg1) - arg2S = numpy.modf(arg2) + arg1S = np.modf(arg1) + arg2S = np.modf(arg2) # Prior code assumed that fractional part of arg1 was 0 if arg2 was present # @paulray removed that assumption here ff = arg1S[0] + arg2S[0] @@ -83,6 +79,19 @@ def __new__(cls, arg1, arg2=None): index = ff >= 0.5 ff[index] -= 1.0 ii[index] += 1 + + ii = ( + ii.to(u.dimensionless_unscaled) + if isinstance(ii, u.Quantity) + else u.Quantity(ii) + ) + + ff = ( + ff.to(u.dimensionless_unscaled) + if isinstance(ff, u.Quantity) + else u.Quantity(ff) + ) + return super().__new__(cls, ii, ff) def __neg__(self): @@ -91,7 +100,7 @@ def __neg__(self): def __add__(self, other): ff = self.frac + other.frac - ii = numpy.modf(ff)[1] + ii = np.modf(ff)[1] return Phase(self.int + other.int + ii, ff - ii) def __sub__(self, other):