From 97f345e0bbc92b41e9d8ffb05513c3dfaebd74ec Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Tue, 12 Jul 2022 13:55:03 +0100 Subject: [PATCH 1/9] Initial use of pickle_dir --- conftest.py | 12 +- src/pint/toa.py | 28 ++- tests/datafile/get_tempo_result.py | 4 +- tests/datafile/make_J1713_libstempo.py | 3 +- tests/test_B1855.py | 4 +- tests/test_B1855_9yrs.py | 4 +- tests/test_B1953.py | 6 +- tests/test_Galactic.py | 13 +- tests/test_J0613.py | 4 +- tests/test_TDB_method.py | 2 +- tests/test_absphase.py | 21 +- tests/test_all_component_and_model_builder.py | 26 +-- tests/test_astrometry.py | 2 +- tests/test_astropyobservatory.py | 2 + tests/test_barytoa.py | 4 +- tests/test_binary_generic.py | 12 +- tests/test_clock_file.py | 8 +- tests/test_clockcorr.py | 8 +- tests/test_compare.py | 12 +- tests/test_copy.py | 11 +- tests/test_covariance_matrix.py | 8 +- tests/test_d_phase_d_toa.py | 3 +- tests/test_datafiles.py | 4 +- tests/test_dd.py | 4 +- tests/test_ddk.py | 8 +- tests/test_density.py | 14 +- tests/test_derived_quantities.py | 8 +- tests/test_design_matrix.py | 11 +- tests/test_determinism.py | 2 +- tests/test_dmefac_dmequad.py | 5 +- tests/test_dmx.py | 2 +- tests/test_dmxrange_add_sub.py | 4 +- tests/test_downhill_fitter.py | 2 +- tests/test_early_chime_data.py | 2 +- tests/test_ell1h.py | 13 +- tests/test_event_optimize.py | 2 +- tests/test_event_optimize_MCMCFitter.py | 1 - tests/test_event_optimize_multiple.py | 1 - tests/test_event_toas.py | 10 +- tests/test_eventstats.py | 2 +- tests/test_fake_toas.py | 18 +- tests/test_fd.py | 4 +- tests/test_fermiphase.py | 3 +- tests/test_fitter.py | 8 +- tests/test_fitter_compare.py | 10 +- tests/test_fitter_error_checking.py | 4 +- tests/test_flagging_clustering.py | 24 ++- tests/test_glitch.py | 2 +- tests/test_gls_fitter.py | 2 +- tests/test_grid.py | 4 +- tests/test_infostrings.py | 12 +- tests/test_jump.py | 9 +- tests/test_mask_parameter.py | 8 +- tests/test_model.py | 2 +- tests/test_model_derivatives.py | 4 +- tests/test_model_ifunc.py | 4 +- tests/test_model_manual.py | 11 +- tests/test_model_wave.py | 4 +- tests/test_modelconversions.py | 4 +- tests/test_modelutils.py | 5 +- tests/test_new_observatories.py | 8 +- tests/test_observatory.py | 12 +- tests/test_observatory_envar.py | 7 +- tests/test_observatory_metadata.py | 2 + tests/test_orbit_phase.py | 4 +- tests/test_parametercovariancematrix.py | 6 +- tests/test_parameters.py | 2 +- tests/test_parfile.py | 2 +- tests/test_parfile_writing.py | 4 +- tests/test_parfile_writing_format.py | 5 +- tests/test_phase.py | 9 +- tests/test_phase_commands.py | 7 +- tests/test_photonphase.py | 6 +- tests/test_pickle.py | 7 +- tests/test_piecewise.py | 4 +- tests/test_pintbary.py | 2 +- tests/test_pintempo.py | 4 +- tests/test_pintk.py | 4 +- tests/test_plk_widget.py | 4 +- tests/test_polycos.py | 12 +- tests/test_prefixparaminheritance.py | 2 +- tests/test_priors.py | 6 +- tests/test_process_parfile.py | 5 +- tests/test_pulsar_mjd.py | 4 +- tests/test_pulsar_position.py | 2 +- tests/test_pulse_number.py | 14 +- tests/test_random_models.py | 15 +- tests/test_residuals.py | 2 +- tests/test_satobs.py | 6 +- tests/test_solar_system_body.py | 4 +- tests/test_solar_wind.py | 17 +- tests/test_stigma.py | 13 +- tests/test_templates.py | 2 +- tests/test_tempox_compatibility.py | 5 +- tests/test_tim_writing.py | 5 +- tests/test_times.py | 9 +- tests/test_timing_model.py | 10 +- tests/test_toa.py | 2 +- tests/test_toa_flag.py | 3 +- tests/test_toa_indexing.py | 6 +- tests/test_toa_pickle.py | 49 ++--- tests/test_toa_selection.py | 8 +- tests/test_toa_shuffle.py | 13 +- tests/test_toa_writer.py | 13 +- tests/test_troposphere_model.py | 132 +++++++----- tests/test_utils.py | 26 ++- tests/test_variety_parfiles.py | 12 +- tests/test_widebandTOA_fitting.py | 199 ++++++++++-------- tests/test_wideband_dm_data.py | 161 +++++++------- tests/test_wls_fitter.py | 108 +++++----- tests/test_zima.py | 4 +- tests/utils.py | 5 +- 112 files changed, 760 insertions(+), 647 deletions(-) diff --git a/conftest.py b/conftest.py index c5dabb5ac..bb69ca7d8 100644 --- a/conftest.py +++ b/conftest.py @@ -1,8 +1,12 @@ import os +import shutil +import tempfile +from pathlib import Path + import hypothesis import pytest -from astropy.utils.data import check_download_cache from astropy.config import paths +from astropy.utils.data import check_download_cache # This setup is drawn from Astropy and might not be entirely relevant to us; # in particular we don't have a cron run for slow tests. @@ -42,3 +46,9 @@ def temp_cache(tmpdir): with paths.set_temp_cache(tmpdir): yield None check_download_cache() + + +@pytest.fixture(scope="session") +def pickle_dir(): + with tempfile.TemporaryDirectory(prefix="pytest-pickles-") as d: + yield Path(d) diff --git a/src/pint/toa.py b/src/pint/toa.py index 600536109..5dc1b19eb 100644 --- a/src/pint/toa.py +++ b/src/pint/toa.py @@ -171,7 +171,7 @@ def get_TOAs( tdb_method : str Which method to use for the clock correction to TDB. See :func:`pint.observatory.Observatory.get_TDBs` for details. - picklefilename : str or None + picklefilename : Path or str or None Filename to use for caching loaded file. Defaults to adding ``.pickle.gz`` to the filename of the timfile, if there is one and only one. If no filename is available, or multiple filenames are provided, a specific filename must be provided. @@ -322,7 +322,7 @@ def load_pickle(toafilename, picklefilename=None): toafilename : str Base filename of the TOAs; pickles will be searched for with ".pickle.gz", ".pickle", or just this filename. - picklefilename : str, optional + picklefilename : str or Path, optional Explicit filename to use. Returns @@ -334,11 +334,17 @@ def load_pickle(toafilename, picklefilename=None): IOError If no pickle is found. """ - picklefilenames = ( - [toafilename + ext for ext in (".pickle.gz", ".pickle", "")] - if picklefilename is None - else [picklefilename] - ) + if picklefilename is None: + picklefilenames = [toafilename + ext for ext in (".pickle.gz", ".pickle", "")] + else: + picklefilename = Path(picklefilename) + if picklefilename.is_dir(): + picklefilenames = [ + picklefilename / (Path(toafilename).name + ext) + for ext in (".pickle.gz", ".pickle", "") + ] + else: + picklefilenames = [picklefilename] lf = None for fn in picklefilenames: @@ -372,6 +378,12 @@ def save_pickle(toas, picklefilename=None): """ # Save the PINT version used to create this pickle file toas.pintversion = pint.__version__ + pickle_dir = None + if picklefilename is not None: + picklefilename = Path(picklefilename) + if picklefilename.is_dir(): + pickle_dir = picklefilename + picklefilename = None if picklefilename is not None: pass elif toas.merged: @@ -385,6 +397,8 @@ def save_pickle(toas, picklefilename=None): picklefilename = toas.filename[0] + ".pickle.gz" else: raise ValueError("TOA pickle method needs a (single) filename.") + if pickle_dir is not None: + picklefilename = pickle_dir / Path(picklefilename).name with gzip.open(picklefilename, "wb") as f: pickle.dump(toas, f) diff --git a/tests/datafile/get_tempo_result.py b/tests/datafile/get_tempo_result.py index d2f3602ef..c2998816a 100644 --- a/tests/datafile/get_tempo_result.py +++ b/tests/datafile/get_tempo_result.py @@ -1,6 +1,8 @@ +import argparse + import tempo_utils as t1u + from pint.utils import longdouble2str -import argparse def get_tempo_result(parfile, timfile): diff --git a/tests/datafile/make_J1713_libstempo.py b/tests/datafile/make_J1713_libstempo.py index 0b5845ab8..ae2709959 100644 --- a/tests/datafile/make_J1713_libstempo.py +++ b/tests/datafile/make_J1713_libstempo.py @@ -1,6 +1,7 @@ +import datetime + import libstempo as T import numpy as np -import datetime timJ1713 = "J1713+0747_NANOGrav_11yv0_short.tim" # one file in ICRS, one in ECL diff --git a/tests/test_B1855.py b/tests/test_B1855.py index 20a594c0d..16ba8626c 100644 --- a/tests/test_B1855.py +++ b/tests/test_B1855.py @@ -6,12 +6,12 @@ import astropy.units as u import numpy as np import pytest +import test_derivative_utils as tdu +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa -import test_derivative_utils as tdu from pint.residuals import Residuals -from pinttestdata import datadir class TestB1855(unittest.TestCase): diff --git a/tests/test_B1855_9yrs.py b/tests/test_B1855_9yrs.py index 59549ad16..beee1a4e5 100644 --- a/tests/test_B1855_9yrs.py +++ b/tests/test_B1855_9yrs.py @@ -5,12 +5,12 @@ import astropy.units as u import numpy as np +import test_derivative_utils as tdu +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa -import test_derivative_utils as tdu from pint.residuals import Residuals -from pinttestdata import datadir class TestB1855(unittest.TestCase): diff --git a/tests/test_B1953.py b/tests/test_B1953.py index 9946b5b1b..f8287db49 100644 --- a/tests/test_B1953.py +++ b/tests/test_B1953.py @@ -1,16 +1,16 @@ """Various tests to assess the performance of the B1953+29.""" -from astropy import log import os import unittest import astropy.units as u import numpy as np +import test_derivative_utils as tdu +from astropy import log +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa -import test_derivative_utils as tdu from pint.residuals import Residuals -from pinttestdata import datadir class TestB1953(unittest.TestCase): diff --git a/tests/test_Galactic.py b/tests/test_Galactic.py index 723c3cb3d..1472389da 100644 --- a/tests/test_Galactic.py +++ b/tests/test_Galactic.py @@ -2,19 +2,18 @@ import os import unittest +import astropy.coordinates +import astropy.time import astropy.units as u import numpy as np +import test_derivative_utils as tdu +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa -import test_derivative_utils as tdu -from pint.residuals import Residuals -from pinttestdata import datadir -from pint.pulsar_ecliptic import PulsarEcliptic from pint import utils - -import astropy.coordinates -import astropy.time +from pint.pulsar_ecliptic import PulsarEcliptic +from pint.residuals import Residuals class TestGalactic(unittest.TestCase): diff --git a/tests/test_J0613.py b/tests/test_J0613.py index 2181aeb41..89ccab5ee 100644 --- a/tests/test_J0613.py +++ b/tests/test_J0613.py @@ -5,12 +5,12 @@ import astropy.units as u import numpy as np +import test_derivative_utils as tdu +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa -import test_derivative_utils as tdu from pint.residuals import Residuals -from pinttestdata import datadir class TestJ0613(unittest.TestCase): diff --git a/tests/test_TDB_method.py b/tests/test_TDB_method.py index 927ae72d8..ceeb705fb 100644 --- a/tests/test_TDB_method.py +++ b/tests/test_TDB_method.py @@ -3,9 +3,9 @@ import unittest import numpy as np +from pinttestdata import datadir import pint.toa as toa -from pinttestdata import datadir class TestTDBMethod(unittest.TestCase): diff --git a/tests/test_absphase.py b/tests/test_absphase.py index 053f15c6b..da971a0e4 100644 --- a/tests/test_absphase.py +++ b/tests/test_absphase.py @@ -2,21 +2,22 @@ import os import unittest +import numpy as np +from pinttestdata import datadir + import pint.models import pint.toa -from pinttestdata import datadir parfile = os.path.join(datadir, "NGC6440E.par") timfile = os.path.join(datadir, "zerophase.tim") -class TestAbsPhase(unittest.TestCase): - def test_phase_zero(self): - # Check that model phase is 0.0 for a TOA at exactly the TZRMJD - model = pint.models.get_model(parfile) - toas = pint.toa.get_TOAs(timfile) +def test_phase_zero(pickle_dir): + # Check that model phase is 0.0 for a TOA at exactly the TZRMJD + model = pint.models.get_model(parfile) + toas = pint.toa.get_TOAs(timfile, picklefilename=pickle_dir) - ph = model.phase(toas, abs_phase=True) - # Check that integer and fractional phase values are very close to 0.0 - self.assertAlmostEqual(ph[0].value, 0.0) - self.assertAlmostEqual(ph[1].value, 0.0) + ph = model.phase(toas, abs_phase=True) + # Check that integer and fractional phase values are very close to 0.0 + assert np.isclose(ph[0].value, 0.0) + assert np.isclose(ph[1].value, 0.0) diff --git a/tests/test_all_component_and_model_builder.py b/tests/test_all_component_and_model_builder.py index 1541da8fe..bf021a07c 100644 --- a/tests/test_all_component_and_model_builder.py +++ b/tests/test_all_component_and_model_builder.py @@ -1,25 +1,27 @@ """Test model builder using variance input""" -from collections import defaultdict -import pytest +import copy import io +from collections import defaultdict from glob import glob -import copy from os.path import basename, join -import numpy as np + import astropy.units as u +import numpy as np +import pytest +from pinttestdata import datadir + +from pint.models.model_builder import ComponentConflict, ModelBuilder, get_model +from pint.models.parameter import floatParameter from pint.models.timing_model import ( - TimingModel, - PhaseComponent, - Component, - AllComponents, AliasConflict, + AllComponents, + Component, + PhaseComponent, + TimingModel, UnknownBinaryModel, ) -from pint.models.model_builder import ModelBuilder, ComponentConflict, get_model -from pint.models.parameter import floatParameter -from pint.utils import split_prefixed_name, PrefixError -from pinttestdata import datadir +from pint.utils import PrefixError, split_prefixed_name class SimpleModel(PhaseComponent): diff --git a/tests/test_astrometry.py b/tests/test_astrometry.py index 152cd7c27..4c2e3da4d 100644 --- a/tests/test_astrometry.py +++ b/tests/test_astrometry.py @@ -5,9 +5,9 @@ import numpy as np import pytest from astropy.coordinates import Latitude, Longitude +from pinttestdata import datadir from pint.models import get_model -from pinttestdata import datadir @pytest.fixture diff --git a/tests/test_astropyobservatory.py b/tests/test_astropyobservatory.py index bbcb05f9e..cfa1c6880 100644 --- a/tests/test_astropyobservatory.py +++ b/tests/test_astropyobservatory.py @@ -1,6 +1,8 @@ import logging import unittest + import numpy as np + import pint.observatory diff --git a/tests/test_barytoa.py b/tests/test_barytoa.py index 699c2f470..527fe3199 100644 --- a/tests/test_barytoa.py +++ b/tests/test_barytoa.py @@ -2,13 +2,13 @@ import os import astropy.units as u +from pinttestdata import datadir import pint.fitter import pint.models -from pint.models.model_builder import get_model import pint.residuals import pint.toa -from pinttestdata import datadir +from pint.models.model_builder import get_model def test_barytoa(): diff --git a/tests/test_binary_generic.py b/tests/test_binary_generic.py index 180d89c38..b7cac97e4 100644 --- a/tests/test_binary_generic.py +++ b/tests/test_binary_generic.py @@ -1,17 +1,17 @@ """Tests of PINT generic binary model """ import logging -from os.path import basename, join -from glob import glob import unittest -import pytest +from glob import glob +from os.path import basename, join from warnings import warn -from pint.models.model_builder import get_model -from pint.models.timing_model import MissingParameter, TimingModel, Component -from utils import verify_stand_alone_binary_parameter_updates +import pytest from pinttestdata import datadir +from utils import verify_stand_alone_binary_parameter_updates +from pint.models.model_builder import get_model +from pint.models.timing_model import Component, MissingParameter, TimingModel bad_trouble = ["J1923+2515_NANOGrav_9yv1.gls.par", "J1744-1134.basic.ecliptic.par"] diff --git a/tests/test_clock_file.py b/tests/test_clock_file.py index 481f45414..10a609b4b 100644 --- a/tests/test_clock_file.py +++ b/tests/test_clock_file.py @@ -8,14 +8,12 @@ from numpy.testing import assert_allclose, assert_array_equal from pint.observatory import ( - get_observatory, + ClockCorrectionOutOfRange, bipm_default, + get_observatory, update_clock_files, - ClockCorrectionOutOfRange, -) -from pint.observatory.clock_file import ( - ClockFile, ) +from pint.observatory.clock_file import ClockFile from pint.observatory.topo_obs import export_all_clock_files diff --git a/tests/test_clockcorr.py b/tests/test_clockcorr.py index c07f2fb00..1d2ebb294 100644 --- a/tests/test_clockcorr.py +++ b/tests/test_clockcorr.py @@ -1,12 +1,12 @@ +import os import unittest +from os import path -import pytest -import os import astropy.units as u import numpy -from pinttestdata import datadir -from os import path +import pytest from astropy.time import Time +from pinttestdata import datadir from pint.observatory import Observatory from pint.observatory.clock_file import ClockFile diff --git a/tests/test_compare.py b/tests/test_compare.py index 2048acd7d..3212f65aa 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -1,13 +1,15 @@ -import numpy as np +import io +import os import unittest +from copy import deepcopy as cp + import astropy import astropy.units as u +import numpy as np +from pinttestdata import datadir + import pint import pint.models as mod -import os -import io -from copy import deepcopy as cp -from pinttestdata import datadir class TestCompare(unittest.TestCase): diff --git a/tests/test_copy.py b/tests/test_copy.py index f735f9048..112dd530c 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -1,17 +1,18 @@ """ Test for pint object copying """ -import os -import pytest -import numpy as np import copy +import os import sys import astropy.units as u -from pint.models import get_model +import numpy as np +import pytest +from pinttestdata import datadir + from pint.fitter import WidebandTOAFitter +from pint.models import get_model from pint.toa import get_TOAs -from pinttestdata import datadir os.chdir(datadir) diff --git a/tests/test_covariance_matrix.py b/tests/test_covariance_matrix.py index 81aab55f9..a6bd69d32 100644 --- a/tests/test_covariance_matrix.py +++ b/tests/test_covariance_matrix.py @@ -1,15 +1,15 @@ """ Various of tests for the pint covariance. """ -import pytest import os -import numpy as np import astropy.units as u -from pint.pint_matrix import CovarianceMatrix, combine_covariance_matrix - +import numpy as np +import pytest from pinttestdata import datadir +from pint.pint_matrix import CovarianceMatrix, combine_covariance_matrix + os.chdir(datadir) diff --git a/tests/test_d_phase_d_toa.py b/tests/test_d_phase_d_toa.py index 8cc0fe206..db44f3ae9 100644 --- a/tests/test_d_phase_d_toa.py +++ b/tests/test_d_phase_d_toa.py @@ -8,10 +8,11 @@ except ImportError: from astropy._erfa import DJM0 +from pinttestdata import datadir, testdir + import pint.toa as toa from pint.models import model_builder as mb from pint.polycos import Polycos -from pinttestdata import datadir, testdir class TestD_phase_d_toa(unittest.TestCase): diff --git a/tests/test_datafiles.py b/tests/test_datafiles.py index 4c693fe2a..32e0fe0e4 100644 --- a/tests/test_datafiles.py +++ b/tests/test_datafiles.py @@ -1,8 +1,10 @@ """Test installation of PINT data files""" import os +import tempfile + import pytest + import pint.config -import tempfile @pytest.fixture diff --git a/tests/test_dd.py b/tests/test_dd.py index 3c356051c..cc55756bb 100644 --- a/tests/test_dd.py +++ b/tests/test_dd.py @@ -4,12 +4,12 @@ import astropy.units as u import numpy as np +from pinttestdata import datadir +from utils import verify_stand_alone_binary_parameter_updates import pint.models.model_builder as mb import pint.toa as toa -from utils import verify_stand_alone_binary_parameter_updates from pint.residuals import Residuals -from pinttestdata import datadir class TestDD(unittest.TestCase): diff --git a/tests/test_ddk.py b/tests/test_ddk.py index 31dbb429a..7790f40d6 100644 --- a/tests/test_ddk.py +++ b/tests/test_ddk.py @@ -4,26 +4,26 @@ import os import re import unittest -from io import StringIO import warnings +from io import StringIO import astropy.units as u import numpy as np import pytest import test_derivative_utils as tdu from astropy.time import Time +from loguru import logger as log from pinttestdata import datadir from utils import verify_stand_alone_binary_parameter_updates -from loguru import logger as log +import pint.fitter import pint.models.model_builder as mb -from pint.models import get_model import pint.simulation import pint.toa as toa +from pint.models import get_model from pint.models.parameter import boolParameter from pint.models.timing_model import MissingParameter, TimingModelError from pint.residuals import Residuals -import pint.fitter temp_par_str = """ PSR J1713+0747 diff --git a/tests/test_density.py b/tests/test_density.py index f5461b2c9..37383b787 100644 --- a/tests/test_density.py +++ b/tests/test_density.py @@ -1,13 +1,15 @@ -import numpy as np +import io +from copy import deepcopy + import astropy.units as u -from astropy.time import Time import matplotlib.pyplot as plt -import pint.toa as toa +import numpy as np +import pytest +from astropy.time import Time + import pint.simulation as simulation +import pint.toa as toa from pint.models import get_model -from copy import deepcopy -import io -import pytest @pytest.mark.parametrize("ndays", [7 * u.d, 20 * u.d]) diff --git a/tests/test_derived_quantities.py b/tests/test_derived_quantities.py index cf306e1ae..28e57dc60 100644 --- a/tests/test_derived_quantities.py +++ b/tests/test_derived_quantities.py @@ -28,17 +28,17 @@ from pint.derived_quantities import ( a1sini, companion_mass, + gamma, mass_funct, mass_funct2, + omdot, + omdot_to_mtot, + pbdot, pulsar_age, pulsar_B, pulsar_B_lightcyl, pulsar_edot, pulsar_mass, - omdot, - pbdot, - gamma, - omdot_to_mtot, ) diff --git a/tests/test_design_matrix.py b/tests/test_design_matrix.py index f3bd6ffb3..c45f472aa 100644 --- a/tests/test_design_matrix.py +++ b/tests/test_design_matrix.py @@ -1,17 +1,18 @@ """ Test for pint design matrix""" import os -import pytest + +import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir from pint.models import get_model -from pint.toa import get_TOAs from pint.pint_matrix import ( DesignMatrixMaker, - combine_design_matrices_by_quantity, combine_design_matrices_by_param, + combine_design_matrices_by_quantity, ) -import astropy.units as u -from pinttestdata import datadir +from pint.toa import get_TOAs class TestDesignMatrix: diff --git a/tests/test_determinism.py b/tests/test_determinism.py index 15b807124..26cf1f306 100644 --- a/tests/test_determinism.py +++ b/tests/test_determinism.py @@ -5,6 +5,7 @@ import numpy as np import numpy.random from numpy.testing import assert_array_equal +from pinttestdata import datadir, testdir import pint.fermi_toas as fermi import pint.models @@ -12,7 +13,6 @@ from pint.mcmc_fitter import MCMCFitter, MCMCFitterBinnedTemplate from pint.sampler import EmceeSampler from pint.scripts.event_optimize import marginalize_over_phase, read_gaussfitfile -from pinttestdata import datadir, testdir def test_sampler(): diff --git a/tests/test_dmefac_dmequad.py b/tests/test_dmefac_dmequad.py index c47bf10c1..ae0ce714a 100644 --- a/tests/test_dmefac_dmequad.py +++ b/tests/test_dmefac_dmequad.py @@ -2,13 +2,12 @@ """ from io import StringIO +import astropy.units as u import numpy as np import pytest -import astropy.units as u -from pint.toa import get_TOAs from pint.models.noise_model import ScaleDmError - +from pint.toa import get_TOAs tim = """FORMAT 1 fake 1400 54000 1.0 gbt -pp_dm 57.0 -pp_dme 0.01 -fe Rcvr_800 diff --git a/tests/test_dmx.py b/tests/test_dmx.py index 8e79e1519..ea618caa6 100644 --- a/tests/test_dmx.py +++ b/tests/test_dmx.py @@ -4,11 +4,11 @@ import astropy.units as u import numpy as np +from pinttestdata import datadir import pint.toa as toa from pint import residuals from pint.models import model_builder as mb -from pinttestdata import datadir class TestDMX(unittest.TestCase): diff --git a/tests/test_dmxrange_add_sub.py b/tests/test_dmxrange_add_sub.py index 8e8e8024f..8deded003 100644 --- a/tests/test_dmxrange_add_sub.py +++ b/tests/test_dmxrange_add_sub.py @@ -1,8 +1,10 @@ """Test functions for adding and removing DMX ranges.""" -import pytest import io + import numpy as np +import pytest + from pint.models import get_model from pint.models.dispersion_model import DispersionDM, DispersionDMX from pint.utils import PrefixError diff --git a/tests/test_downhill_fitter.py b/tests/test_downhill_fitter.py index 4435c048f..9ab126bf5 100644 --- a/tests/test_downhill_fitter.py +++ b/tests/test_downhill_fitter.py @@ -13,8 +13,8 @@ import pint.fitter from pint.models import get_model from pint.models.timing_model import MissingTOAs -from pint.toa import merge_TOAs from pint.simulation import make_fake_toas_uniform +from pint.toa import merge_TOAs par_eccentric = """ PSR J1234+5678 diff --git a/tests/test_early_chime_data.py b/tests/test_early_chime_data.py index ccdeb0bed..112bb699a 100644 --- a/tests/test_early_chime_data.py +++ b/tests/test_early_chime_data.py @@ -5,11 +5,11 @@ import astropy.units as u import numpy as np from astropy.tests.helper import assert_quantity_allclose +from pinttestdata import datadir, testdir import pint.models.model_builder as mb import pint.toa as toa from pint.residuals import Residuals -from pinttestdata import datadir, testdir class Test_CHIME_data(unittest.TestCase): diff --git a/tests/test_ell1h.py b/tests/test_ell1h.py index 62f147bf4..e8fba87d0 100644 --- a/tests/test_ell1h.py +++ b/tests/test_ell1h.py @@ -2,22 +2,21 @@ import logging import os import unittest -import pytest +from io import StringIO from warnings import warn import astropy.units as u import numpy as np +import pytest +import test_derivative_utils as tdu +from pinttestdata import datadir +from utils import verify_stand_alone_binary_parameter_updates import pint.fitter as ff +import pint.toa as toa from pint.models import get_model from pint.models.timing_model import TimingModelError -import pint.toa as toa -import test_derivative_utils as tdu -from utils import verify_stand_alone_binary_parameter_updates from pint.residuals import Residuals -from pinttestdata import datadir -from io import StringIO - os.chdir(datadir) diff --git a/tests/test_event_optimize.py b/tests/test_event_optimize.py index a3237fc91..896bf678d 100644 --- a/tests/test_event_optimize.py +++ b/tests/test_event_optimize.py @@ -9,9 +9,9 @@ from pathlib import Path import pytest +from pinttestdata import datadir from pint.scripts import event_optimize -from pinttestdata import datadir def test_result(tmp_path): diff --git a/tests/test_event_optimize_MCMCFitter.py b/tests/test_event_optimize_MCMCFitter.py index ce52ef503..952ccee57 100644 --- a/tests/test_event_optimize_MCMCFitter.py +++ b/tests/test_event_optimize_MCMCFitter.py @@ -3,7 +3,6 @@ # to get the fftfit module. It can be run manually by people who have PRESTO import os import unittest - from io import StringIO from pinttestdata import datadir diff --git a/tests/test_event_optimize_multiple.py b/tests/test_event_optimize_multiple.py index e67640f82..50ef1e6aa 100644 --- a/tests/test_event_optimize_multiple.py +++ b/tests/test_event_optimize_multiple.py @@ -4,7 +4,6 @@ import os import sys import unittest - from io import StringIO from pinttestdata import datadir diff --git a/tests/test_event_toas.py b/tests/test_event_toas.py index 434a1798e..da30a3d0f 100644 --- a/tests/test_event_toas.py +++ b/tests/test_event_toas.py @@ -1,10 +1,14 @@ import os -import pytest -from pint.event_toas import read_mission_info_from_heasoft, create_mission_config -from pint.event_toas import load_fits_TOAs +import pytest from pinttestdata import datadir +from pint.event_toas import ( + create_mission_config, + load_fits_TOAs, + read_mission_info_from_heasoft, +) + def test_xselect_mdb_is_found_headas(monkeypatch, tmp_path): """Test event file reading.""" diff --git a/tests/test_eventstats.py b/tests/test_eventstats.py index 5fe0f1f98..b5bc937eb 100755 --- a/tests/test_eventstats.py +++ b/tests/test_eventstats.py @@ -1,8 +1,8 @@ #!/usr/bin/env python +import numpy as np from numpy.testing import assert_allclose, assert_equal import pint.eventstats as es -import numpy as np def test_sig2sigma(): diff --git a/tests/test_fake_toas.py b/tests/test_fake_toas.py index 3e40b148a..ef8970ce3 100644 --- a/tests/test_fake_toas.py +++ b/tests/test_fake_toas.py @@ -1,15 +1,17 @@ -import astropy.units as u -import pint.simulation -from pint.models.model_builder import get_model, get_model_and_toas -from pint.toa import get_TOAs -import pint.residuals import io -import numpy as np -import tempfile import os +import tempfile + +import astropy.units as u +import numpy as np +from pinttestdata import datadir, testdir + import pint.config +import pint.residuals +import pint.simulation from pint.fitter import GLSFitter -from pinttestdata import datadir, testdir +from pint.models.model_builder import get_model, get_model_and_toas +from pint.toa import get_TOAs def test_noise_addition(): diff --git a/tests/test_fd.py b/tests/test_fd.py index 76cd8d8a9..09acbc86e 100644 --- a/tests/test_fd.py +++ b/tests/test_fd.py @@ -2,15 +2,15 @@ import copy import os import unittest -import pytest import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir import pint.models.model_builder as mb import pint.residuals import pint.toa as toa -from pinttestdata import datadir class TestFD(unittest.TestCase): diff --git a/tests/test_fermiphase.py b/tests/test_fermiphase.py index e395bd389..051f86844 100644 --- a/tests/test_fermiphase.py +++ b/tests/test_fermiphase.py @@ -5,15 +5,14 @@ from pathlib import Path import pytest - from astropy.io import fits +from pinttestdata import datadir import pint.models import pint.scripts.fermiphase as fermiphase import pint.toa as toa from pint.fermi_toas import load_Fermi_TOAs from pint.observatory.satellite_obs import get_satellite_observatory -from pinttestdata import datadir parfile = datadir / "PSRJ0030+0451_psrcat.par" eventfile = ( diff --git a/tests/test_fitter.py b/tests/test_fitter.py index 748d8b903..202456604 100644 --- a/tests/test_fitter.py +++ b/tests/test_fitter.py @@ -2,17 +2,17 @@ import os from copy import deepcopy +import astropy.units as u + # import matplotlib # matplotlib.use('TKAgg') import matplotlib.pyplot as plt import pytest -import astropy.units as u +from pinttestdata import datadir import pint.models as tm -from pint import fitter, toa, simulation -from pinttestdata import datadir import pint.models.parameter as param -from pint import ls +from pint import fitter, ls, simulation, toa from pint.models import get_model, get_model_and_toas diff --git a/tests/test_fitter_compare.py b/tests/test_fitter_compare.py index 5d589591d..2ae8212c0 100644 --- a/tests/test_fitter_compare.py +++ b/tests/test_fitter_compare.py @@ -1,29 +1,29 @@ #! /usr/bin/env python +import copy import os import unittest -from os.path import join from io import StringIO -import copy -import numpy as np +from os.path import join import astropy.units as u +import numpy as np import pytest from pinttestdata import datadir import pint from pint.fitter import ( ConvergenceFailure, - MaxiterReached, DownhillGLSFitter, DownhillWLSFitter, GLSFitter, + MaxiterReached, WidebandDownhillFitter, WidebandTOAFitter, WLSFitter, ) from pint.models.model_builder import get_model -from pint.toa import get_TOAs from pint.simulation import make_fake_toas_uniform +from pint.toa import get_TOAs @pytest.fixture diff --git a/tests/test_fitter_error_checking.py b/tests/test_fitter_error_checking.py index f1ec1e4eb..0f290e31f 100644 --- a/tests/test_fitter_error_checking.py +++ b/tests/test_fitter_error_checking.py @@ -1,10 +1,10 @@ #! /usr/bin/env python import io +import re -import numpy as np import astropy.units as u +import numpy as np import pytest -import re import pint.fitter from pint.models import get_model diff --git a/tests/test_flagging_clustering.py b/tests/test_flagging_clustering.py index fb0b1bff2..82909bfa2 100644 --- a/tests/test_flagging_clustering.py +++ b/tests/test_flagging_clustering.py @@ -1,35 +1,41 @@ """Tests for clustering and flagging""" +import copy import logging import os import unittest -import pytest -import copy import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa +from pint.models import PhaseJump, parameter as p from pint.residuals import Residuals -from pinttestdata import datadir -from pint.models import parameter as p -from pint.models import PhaseJump class SimpleSetup: - def __init__(self, par, tim): + def __init__(self, par, tim, pickle_dir): self.par = par self.tim = tim self.m = mb.get_model(self.par) self.t = toa.get_TOAs( - self.tim, ephem="DE405", planets=False, include_bipm=False + self.tim, + ephem="DE405", + planets=False, + include_bipm=False, + usepickle=True, + picklefilename=pickle_dir, ) @pytest.fixture -def setup_NGC6440E(): +def setup_NGC6440E(pickle_dir): return SimpleSetup( - os.path.join(datadir, "NGC6440E.par"), os.path.join(datadir, "NGC6440E.tim") + os.path.join(datadir, "NGC6440E.par"), + os.path.join(datadir, "NGC6440E.tim"), + pickle_dir, ) diff --git a/tests/test_glitch.py b/tests/test_glitch.py index 0f7943f45..af349a911 100644 --- a/tests/test_glitch.py +++ b/tests/test_glitch.py @@ -5,12 +5,12 @@ import astropy.units as u import numpy as np import pytest +from pinttestdata import datadir import pint.fitter import pint.models import pint.residuals import pint.toa -from pinttestdata import datadir parfile = os.path.join(datadir, "prefixtest.par") timfile = os.path.join(datadir, "prefixtest.tim") diff --git a/tests/test_gls_fitter.py b/tests/test_gls_fitter.py index fa2234877..464e8bd5b 100644 --- a/tests/test_gls_fitter.py +++ b/tests/test_gls_fitter.py @@ -5,11 +5,11 @@ import astropy.units as u import numpy as np +from pinttestdata import datadir import pint.models.model_builder as mb from pint import toa from pint.fitter import GLSFitter -from pinttestdata import datadir class TestGLS(unittest.TestCase): diff --git a/tests/test_grid.py b/tests/test_grid.py index 93a43a272..5717f6ae7 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -3,15 +3,15 @@ import io import multiprocessing import os -import pytest import astropy.units as u import numpy as np +import pytest import pint.config import pint.gridutils import pint.models.parameter as param -from pint.fitter import GLSFitter, WLSFitter, DownhillWLSFitter, DownhillGLSFitter +from pint.fitter import DownhillGLSFitter, DownhillWLSFitter, GLSFitter, WLSFitter from pint.models.model_builder import get_model_and_toas # for multi-core tests, don't use all available CPUs diff --git a/tests/test_infostrings.py b/tests/test_infostrings.py index a6b8ca1b2..c2f35bbde 100644 --- a/tests/test_infostrings.py +++ b/tests/test_infostrings.py @@ -1,20 +1,20 @@ """Tests for adding info strings to parfiles and tim files""" -import logging -import os -import unittest -import pytest import copy import io +import logging +import os import platform +import unittest import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir +import pint import pint.models.model_builder as mb import pint.toa as toa from pint.residuals import Residuals -from pinttestdata import datadir -import pint class SimpleSetup: diff --git a/tests/test_jump.py b/tests/test_jump.py index 3f14fdda4..df49cd826 100644 --- a/tests/test_jump.py +++ b/tests/test_jump.py @@ -1,19 +1,18 @@ """Tests for jump model component """ +import copy import logging import os import unittest -import pytest -import copy import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa +from pint.models import PhaseJump, parameter as p from pint.residuals import Residuals -from pinttestdata import datadir -from pint.models import parameter as p -from pint.models import PhaseJump class SimpleSetup: diff --git a/tests/test_mask_parameter.py b/tests/test_mask_parameter.py index 3c5f200c0..3f18d574c 100644 --- a/tests/test_mask_parameter.py +++ b/tests/test_mask_parameter.py @@ -1,19 +1,19 @@ """Various tests for the maskParameter""" +import copy import os -import pytest from io import StringIO + import astropy import astropy.time as time import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir from pint.models.model_builder import get_model from pint.models.parameter import maskParameter from pint.toa import get_TOAs -from pinttestdata import datadir - -import copy @pytest.fixture diff --git a/tests/test_model.py b/tests/test_model.py index 9c9255957..bcd245302 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -6,11 +6,11 @@ import matplotlib.pyplot as plt import numpy as np from astropy import log +from pinttestdata import datadir import pint.models as tm from pint import toa from pint.residuals import Residuals -from pinttestdata import datadir def test_model(): diff --git a/tests/test_model_derivatives.py b/tests/test_model_derivatives.py index 65c02d109..f3d89a8bd 100644 --- a/tests/test_model_derivatives.py +++ b/tests/test_model_derivatives.py @@ -8,13 +8,13 @@ import numdifftools import numpy as np import pytest +from astropy import units as u from hypothesis import HealthCheck, Verbosity, assume, given, settings from hypothesis.strategies import composite, floats, integers, sampled_from from numpy.testing import assert_allclose -from astropy import units as u -import pint.toa import pint.simulation +import pint.toa from pint.models import get_model diff --git a/tests/test_model_ifunc.py b/tests/test_model_ifunc.py index 1dc11503b..8bddc011e 100644 --- a/tests/test_model_ifunc.py +++ b/tests/test_model_ifunc.py @@ -3,12 +3,12 @@ import unittest import astropy.units as u +from pinttestdata import datadir +import pint.fitter import pint.models import pint.residuals import pint.toa -import pint.fitter -from pinttestdata import datadir # Not included in the test here, but as a sanity check I used this same # ephemeris to phase up Fermi data, and it looks good. diff --git a/tests/test_model_manual.py b/tests/test_model_manual.py index e4017e1c7..15ce9b75f 100644 --- a/tests/test_model_manual.py +++ b/tests/test_model_manual.py @@ -1,22 +1,23 @@ """Test model building and structure for simple models.""" +from collections import defaultdict from glob import glob from os.path import basename, join -from collections import defaultdict -import pytest import astropy.units as u +import pytest +from pinttestdata import datadir + from pint.models.astrometry import AstrometryEquatorial from pint.models.dispersion_model import DispersionDM, DispersionDMX -from pint.models.spindown import Spindown from pint.models.model_builder import get_model +from pint.models.spindown import Spindown from pint.models.timing_model import ( + Component, MissingParameter, TimingModel, - Component, UnknownBinaryModel, ) -from pinttestdata import datadir @pytest.fixture diff --git a/tests/test_model_wave.py b/tests/test_model_wave.py index 39a9ce024..037dc915c 100644 --- a/tests/test_model_wave.py +++ b/tests/test_model_wave.py @@ -1,15 +1,15 @@ #! /usr/bin/env python import os import unittest -import pytest import astropy.units as u +import pytest +from pinttestdata import datadir import pint.fitter import pint.models import pint.residuals import pint.toa -from pinttestdata import datadir # Not included in the test here, but as a sanity check I used this same # ephemeris to phase up Fermi data, and it looks good. diff --git a/tests/test_modelconversions.py b/tests/test_modelconversions.py index b9e10db94..2c0baa410 100644 --- a/tests/test_modelconversions.py +++ b/tests/test_modelconversions.py @@ -1,16 +1,16 @@ import io import os -import pytest import astropy.units as u import numpy as np +import pytest import pint.residuals import pint.simulation from pint.fitter import WLSFitter from pint.models.model_builder import get_model, get_model_and_toas -from pint.toa import get_TOAs from pint.pulsar_ecliptic import OBL, PulsarEcliptic +from pint.toa import get_TOAs modelstring_ECL = """ PSR B1855+09 diff --git a/tests/test_modelutils.py b/tests/test_modelutils.py index ec6d46bad..4be28d0f8 100644 --- a/tests/test_modelutils.py +++ b/tests/test_modelutils.py @@ -4,13 +4,12 @@ import astropy.units as u import numpy as np +from pinttestdata import datadir import pint.toa as toa from pint.models import get_model +from pint.modelutils import model_ecliptic_to_equatorial, model_equatorial_to_ecliptic from pint.residuals import Residuals -from pinttestdata import datadir - -from pint.modelutils import model_equatorial_to_ecliptic, model_ecliptic_to_equatorial class TestEcliptic(unittest.TestCase): diff --git a/tests/test_new_observatories.py b/tests/test_new_observatories.py index bc1c02e91..2fa7bebda 100644 --- a/tests/test_new_observatories.py +++ b/tests/test_new_observatories.py @@ -1,14 +1,14 @@ +import os +import os.path from io import StringIO import numpy as np -import os -import os.path import pytest +from pinttestdata import datadir -from pint.toa import get_TOAs from pint.models import get_model from pint.observatory import get_observatory -from pinttestdata import datadir +from pint.toa import get_TOAs parfile = os.path.join(datadir, "NGC6440E.par") diff --git a/tests/test_observatory.py b/tests/test_observatory.py index 691a0b583..8b788f17b 100644 --- a/tests/test_observatory.py +++ b/tests/test_observatory.py @@ -1,23 +1,23 @@ #!/usr/bin/env python import io import os -from pathlib import Path import unittest +from pathlib import Path import numpy as np import pytest -from pint.pulsar_mjd import Time +from pinttestdata import datadir import pint.observatory import pint.observatory.topo_obs +from pint.observatory import NoClockCorrections, Observatory, get_observatory +from pint.observatory.special_locations import load_special_locations from pint.observatory.topo_obs import ( + TopoObs, load_observatories, load_observatories_from_usual_locations, - TopoObs, ) -from pint.observatory.special_locations import load_special_locations -from pint.observatory import get_observatory, Observatory, NoClockCorrections -from pinttestdata import datadir +from pint.pulsar_mjd import Time @pytest.fixture diff --git a/tests/test_observatory_envar.py b/tests/test_observatory_envar.py index d66cff8b8..7e28f0c7a 100644 --- a/tests/test_observatory_envar.py +++ b/tests/test_observatory_envar.py @@ -1,11 +1,12 @@ +import importlib import os import sys + import pytest -import importlib -import pint.observatory.topo_obs -import pint.observatory.special_locations import pint.observatory +import pint.observatory.special_locations +import pint.observatory.topo_obs @pytest.fixture diff --git a/tests/test_observatory_metadata.py b/tests/test_observatory_metadata.py index 9849fd9e0..8c855570c 100644 --- a/tests/test_observatory_metadata.py +++ b/tests/test_observatory_metadata.py @@ -1,7 +1,9 @@ import logging import os import unittest + import numpy as np + import pint.observatory diff --git a/tests/test_orbit_phase.py b/tests/test_orbit_phase.py index fcd3b53e8..98e01e8bc 100644 --- a/tests/test_orbit_phase.py +++ b/tests/test_orbit_phase.py @@ -5,10 +5,10 @@ import astropy.time as time import astropy.units as u import numpy as np +from pinttestdata import datadir -import pint.toa as t import pint.models as m -from pinttestdata import datadir +import pint.toa as t class TestOrbitPhase(unittest.TestCase): diff --git a/tests/test_parametercovariancematrix.py b/tests/test_parametercovariancematrix.py index 3438a8fd7..b91417e38 100644 --- a/tests/test_parametercovariancematrix.py +++ b/tests/test_parametercovariancematrix.py @@ -1,9 +1,9 @@ -import numpy as np import os import unittest from os.path import join import astropy.units as u +import numpy as np import pytest from pinttestdata import datadir @@ -12,13 +12,13 @@ DownhillGLSFitter, DownhillWLSFitter, GLSFitter, - WLSFitter, WidebandDownhillFitter, WidebandTOAFitter, + WLSFitter, ) from pint.models.model_builder import get_model -from pint.toa import get_TOAs from pint.simulation import make_fake_toas_uniform +from pint.toa import get_TOAs @pytest.fixture diff --git a/tests/test_parameters.py b/tests/test_parameters.py index fc5ffe335..2c4b9a4a6 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -1,8 +1,8 @@ import copy +import io import os import pickle import unittest -import io import astropy.time as time import astropy.units as u diff --git a/tests/test_parfile.py b/tests/test_parfile.py index 325e690cd..7b69d7fef 100644 --- a/tests/test_parfile.py +++ b/tests/test_parfile.py @@ -3,9 +3,9 @@ import tempfile import pytest +from pinttestdata import datadir, testdir import pint.models as tm -from pinttestdata import testdir, datadir parfile = datadir / "J1744-1134.basic.par" diff --git a/tests/test_parfile_writing.py b/tests/test_parfile_writing.py index 95d41f68b..355fed37b 100644 --- a/tests/test_parfile_writing.py +++ b/tests/test_parfile_writing.py @@ -2,16 +2,16 @@ import numbers import os from io import StringIO -import pytest import astropy.units as u import numpy as np +import pytest +from pinttestdata import datadir import pint.models.model_builder as mb import pint.models.parameter as mp import pint.toa as toa from pint.residuals import Residuals -from pinttestdata import datadir def test_parfile_write(tmp_path): diff --git a/tests/test_parfile_writing_format.py b/tests/test_parfile_writing_format.py index 1936cd45f..619a013c4 100644 --- a/tests/test_parfile_writing_format.py +++ b/tests/test_parfile_writing_format.py @@ -1,15 +1,14 @@ import os import unittest -import pytest from io import StringIO import pytest from hypothesis import given from hypothesis.strategies import sampled_from +from pinttestdata import datadir -from pint.models import get_model, get_model_and_toas from pint import fitter -from pinttestdata import datadir +from pint.models import get_model, get_model_and_toas def test_SWM(): diff --git a/tests/test_phase.py b/tests/test_phase.py index 6972bb09c..f8599893a 100644 --- a/tests/test_phase.py +++ b/tests/test_phase.py @@ -1,10 +1,13 @@ # Test for phase.py -import pytest -import numpy as np +import math + import astropy.units as u +import numpy as np +import pytest + from pint.phase import Phase -import math + # modified from @mhvk's test_phase_class.py def assert_equal(first, second): diff --git a/tests/test_phase_commands.py b/tests/test_phase_commands.py index 53947ffda..44a130e2a 100644 --- a/tests/test_phase_commands.py +++ b/tests/test_phase_commands.py @@ -2,12 +2,13 @@ import os import unittest +import astropy.units as u +import numpy as np +from pinttestdata import datadir + import pint.models import pint.toa from pint.residuals import Residuals -from pinttestdata import datadir -import astropy.units as u -import numpy as np parfile = os.path.join(datadir, "NGC6440E_PHASETEST.par") timfile = os.path.join(datadir, "NGC6440E_PHASETEST.tim") diff --git a/tests/test_photonphase.py b/tests/test_photonphase.py index 23b2b65ee..644055289 100644 --- a/tests/test_photonphase.py +++ b/tests/test_photonphase.py @@ -2,12 +2,12 @@ import os import unittest +import numpy as np import pytest +from astropy.io import fits +from pinttestdata import datadir import pint.scripts.photonphase as photonphase -from pinttestdata import datadir -from astropy.io import fits -import numpy as np parfile = datadir / "J1513-5908_PKS_alldata_white.par" eventfile = datadir / "B1509_RXTE_short.fits" diff --git a/tests/test_pickle.py b/tests/test_pickle.py index e6460aea0..2663bdbe7 100644 --- a/tests/test_pickle.py +++ b/tests/test_pickle.py @@ -1,13 +1,14 @@ """Test pickle-ability of timing models""" +import os +import pickle + import astropy.units as u import numpy as np -import os +from pinttestdata import datadir import pint.config import pint.models.parameter as param from pint.models.model_builder import get_model -from pinttestdata import datadir -import pickle def test_pickle_prefixparameter(): diff --git a/tests/test_piecewise.py b/tests/test_piecewise.py index a0bc0be07..cbf86cb58 100644 --- a/tests/test_piecewise.py +++ b/tests/test_piecewise.py @@ -5,14 +5,14 @@ import astropy.units as u import numpy as np import pytest +from pinttestdata import datadir import pint.fitter import pint.models import pint.residuals import pint.toa -from pinttestdata import datadir -from pint.models.timing_model import MissingParameter from pint import fitter, toa +from pint.models.timing_model import MissingParameter parfile = os.path.join(datadir, "piecewise.par") parfile2 = os.path.join(datadir, "piecewise_twocomps.par") diff --git a/tests/test_pintbary.py b/tests/test_pintbary.py index 7def071a0..9fb5f4b47 100644 --- a/tests/test_pintbary.py +++ b/tests/test_pintbary.py @@ -1,9 +1,9 @@ #!/usr/bin/env python import sys import unittest +from io import StringIO import numpy as np -from io import StringIO import pint.scripts.pintbary as pintbary diff --git a/tests/test_pintempo.py b/tests/test_pintempo.py index 1f8963350..8a48bac0d 100644 --- a/tests/test_pintempo.py +++ b/tests/test_pintempo.py @@ -1,12 +1,12 @@ #!/usr/bin/env python import os import sys - from io import StringIO -from pint.scripts import pintempo from pinttestdata import datadir +from pint.scripts import pintempo + parfile = os.path.join(datadir, "NGC6440E.par") timfile = os.path.join(datadir, "NGC6440E.tim") diff --git a/tests/test_pintk.py b/tests/test_pintk.py index 92c1a2a3a..428f161cb 100644 --- a/tests/test_pintk.py +++ b/tests/test_pintk.py @@ -1,13 +1,13 @@ #!/usr/bin/env python import os import unittest +from io import StringIO import numpy as np import pytest -from io import StringIO +from pinttestdata import datadir import pint.scripts.pintk as pintk -from pinttestdata import datadir parfile = os.path.join(datadir, "NGC6440E.par") timfile = os.path.join(datadir, "NGC6440E.tim") diff --git a/tests/test_plk_widget.py b/tests/test_plk_widget.py index 00f6cdb94..ca0817c0e 100644 --- a/tests/test_plk_widget.py +++ b/tests/test_plk_widget.py @@ -1,7 +1,9 @@ +from tkinter import Frame + import astropy.units as u import pytest + from pint.pintk.plk import PlkWidget -from tkinter import Frame @pytest.fixture diff --git a/tests/test_polycos.py b/tests/test_polycos.py index 9dfba6ee2..86d146f35 100644 --- a/tests/test_polycos.py +++ b/tests/test_polycos.py @@ -1,12 +1,14 @@ """Test polycos.""" -import pytest +from pathlib import Path + import numpy as np -from pint.polycos import Polycos -from pint.models import get_model -import pint.toa as toa +import pytest from pinttestdata import datadir -from pathlib import Path + +import pint.toa as toa +from pint.models import get_model +from pint.polycos import Polycos @pytest.fixture diff --git a/tests/test_prefixparaminheritance.py b/tests/test_prefixparaminheritance.py index 29a0019b1..8e6a4bfe8 100644 --- a/tests/test_prefixparaminheritance.py +++ b/tests/test_prefixparaminheritance.py @@ -1,5 +1,5 @@ -import unittest import io +import unittest from pint.models import get_model diff --git a/tests/test_priors.py b/tests/test_priors.py index 8d67e6801..961d1495a 100644 --- a/tests/test_priors.py +++ b/tests/test_priors.py @@ -3,18 +3,18 @@ import unittest import numpy as np +from pinttestdata import datadir from scipy.stats import norm import pint.models # from pint.models.priors import * from pint.models.priors import ( - Prior, - UniformBoundedRV, GaussianBoundedRV, + Prior, RandomInclinationPrior, + UniformBoundedRV, ) -from pinttestdata import datadir class TestPriors(unittest.TestCase): diff --git a/tests/test_process_parfile.py b/tests/test_process_parfile.py index 84fa679dd..984ecfaa1 100644 --- a/tests/test_process_parfile.py +++ b/tests/test_process_parfile.py @@ -3,13 +3,12 @@ """ import os import tempfile - -import pytest from io import StringIO -from pint.models.model_builder import ModelBuilder, parse_parfile, TimingModelError +import pytest from pint.models import get_model +from pint.models.model_builder import ModelBuilder, TimingModelError, parse_parfile base_par = """ PSR J1234+5678 diff --git a/tests/test_pulsar_mjd.py b/tests/test_pulsar_mjd.py index 8c1185e93..4f3d0d453 100644 --- a/tests/test_pulsar_mjd.py +++ b/tests/test_pulsar_mjd.py @@ -3,10 +3,10 @@ import unittest import numpy as np -from pint.pulsar_mjd import Time - from pinttestdata import datadir +from pint.pulsar_mjd import Time + class TestPsrMjd(unittest.TestCase): @classmethod diff --git a/tests/test_pulsar_position.py b/tests/test_pulsar_position.py index c4eb1b6ca..067a0a499 100644 --- a/tests/test_pulsar_position.py +++ b/tests/test_pulsar_position.py @@ -4,9 +4,9 @@ import unittest import numpy as np +from pinttestdata import datadir import pint.models.model_builder as mb -from pinttestdata import datadir class TestPulsarPosition(unittest.TestCase): diff --git a/tests/test_pulse_number.py b/tests/test_pulse_number.py index 74a39ac3d..07130f499 100644 --- a/tests/test_pulse_number.py +++ b/tests/test_pulse_number.py @@ -1,19 +1,19 @@ #!/usr/bin/python -from io import StringIO import os -import pytest from copy import deepcopy +from io import StringIO import numpy as np +import pytest from astropy import units as u from numpy.testing import assert_almost_equal - -from pint.residuals import Residuals from pinttestdata import datadir + import pint.fitter from pint.models import get_model -from pint.toa import get_TOAs +from pint.residuals import Residuals from pint.simulation import make_fake_toas_uniform +from pint.toa import get_TOAs parfile = os.path.join(datadir, "withpn.par") timfile = os.path.join(datadir, "withpn.tim") @@ -25,11 +25,11 @@ def model(): @pytest.fixture -def toas(): +def toas(pickle_dir): # The scope="module" setting ensures the TOAs object will be created # only once for the whole module, which will save time but might # allow accidental modifications done in one test to affect other tests. - return get_TOAs(timfile) + return get_TOAs(timfile, usepickle=True, picklefilename=pickle_dir) @pytest.fixture diff --git a/tests/test_random_models.py b/tests/test_random_models.py index b642d2d16..2e1c84d08 100644 --- a/tests/test_random_models.py +++ b/tests/test_random_models.py @@ -1,21 +1,20 @@ import os from copy import deepcopy +import astropy.units as u + # import matplotlib # matplotlib.use('TKAgg') import matplotlib.pyplot as plt -import pytest import numpy as np -import astropy.units as u +import pytest +from pinttestdata import datadir -from pint.models import get_model, get_model_and_toas -from pint.toa import get_TOAs import pint.fitter -from pint import toa, simulation -from pinttestdata import datadir import pint.models.parameter as param -from pint import ls -from pint import utils +from pint import ls, simulation, toa, utils +from pint.models import get_model, get_model_and_toas +from pint.toa import get_TOAs @pytest.mark.parametrize( diff --git a/tests/test_residuals.py b/tests/test_residuals.py index 8a1e87b1d..aa56b3f2c 100644 --- a/tests/test_residuals.py +++ b/tests/test_residuals.py @@ -16,8 +16,8 @@ from pint.models import get_model from pint.models.dispersion_model import Dispersion from pint.residuals import CombinedResiduals, Residuals, WidebandTOAResiduals -from pint.toa import get_TOAs from pint.simulation import make_fake_toas_uniform +from pint.toa import get_TOAs from pint.utils import weighted_mean os.chdir(datadir) diff --git a/tests/test_satobs.py b/tests/test_satobs.py index 9e957e630..c053ae8f6 100644 --- a/tests/test_satobs.py +++ b/tests/test_satobs.py @@ -1,11 +1,11 @@ import os -import pytest -from astropy.time import Time import numpy as np +import pytest +from astropy.time import Time +from pinttestdata import datadir from pint.observatory.satellite_obs import get_satellite_observatory -from pinttestdata import datadir def test_good_calls(): diff --git a/tests/test_solar_system_body.py b/tests/test_solar_system_body.py index 6ac9a91bd..45a5e9191 100644 --- a/tests/test_solar_system_body.py +++ b/tests/test_solar_system_body.py @@ -1,15 +1,15 @@ #!/usr/bin/env python import os import unittest -import pytest import astropy.time as time import numpy as np +import pytest from astropy.coordinates import solar_system_ephemeris +from pinttestdata import datadir import pint.config from pint.solar_system_ephemerides import objPosVel, objPosVel_wrt_SSB -from pinttestdata import datadir # Hack to support FileNotFoundError in Python 2 try: diff --git a/tests/test_solar_wind.py b/tests/test_solar_wind.py index 6d83553dc..c8db21849 100644 --- a/tests/test_solar_wind.py +++ b/tests/test_solar_wind.py @@ -1,21 +1,22 @@ """ Test for pint solar wind module """ -import os -from io import StringIO -import pytest -import numpy as np -from numpy.testing import assert_allclose import copy +import os import sys +from io import StringIO import astropy.units as u +import numpy as np +import pytest from astropy.time import Time -from pint.models import get_model +from numpy.testing import assert_allclose +from pinttestdata import datadir + from pint.fitter import WidebandTOAFitter -from pint.toa import get_TOAs +from pint.models import get_model from pint.simulation import make_fake_toas_uniform -from pinttestdata import datadir +from pint.toa import get_TOAs os.chdir(datadir) diff --git a/tests/test_stigma.py b/tests/test_stigma.py index 54157e0f3..512da742a 100644 --- a/tests/test_stigma.py +++ b/tests/test_stigma.py @@ -1,19 +1,18 @@ +import io import logging import os -import io +import tempfile import astropy.units as u import numpy as np import pytest -import tempfile - -from pint.models import get_model -import pint.toa as toa -import pint.simulation as simulation import test_derivative_utils as tdu -from pint.residuals import Residuals from pinttestdata import datadir +import pint.simulation as simulation +import pint.toa as toa +from pint.models import get_model +from pint.residuals import Residuals stigma_template = """ PSR FAKE diff --git a/tests/test_templates.py b/tests/test_templates.py index 8dd73ba9d..d95950b0b 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -1,8 +1,8 @@ import logging import numpy as np - from pinttestdata import datadir + from pint.templates import lcfitters, lcprimitives, lctemplate diff --git a/tests/test_tempox_compatibility.py b/tests/test_tempox_compatibility.py index 9ac780925..e4928c93b 100644 --- a/tests/test_tempox_compatibility.py +++ b/tests/test_tempox_compatibility.py @@ -1,11 +1,12 @@ -from io import StringIO import re +from io import StringIO import astropy.units as u import numpy as np import pytest -from pint.models import get_model + import pint.toa +from pint.models import get_model par_basic = """ PSR J1234+5678 diff --git a/tests/test_tim_writing.py b/tests/test_tim_writing.py index 78dd80c69..86bf3cb1e 100644 --- a/tests/test_tim_writing.py +++ b/tests/test_tim_writing.py @@ -1,11 +1,12 @@ -from io import StringIO import re +from io import StringIO import astropy.units as u import numpy as np import pytest from hypothesis import given, settings -from hypothesis.strategies import lists, tuples, one_of, from_regex, just +from hypothesis.strategies import from_regex, just, lists, one_of, tuples + from pint.toa import get_TOAs basic_tim_header = "FORMAT 1\n" diff --git a/tests/test_times.py b/tests/test_times.py index 11f1c0ae9..40f80249e 100644 --- a/tests/test_times.py +++ b/tests/test_times.py @@ -16,7 +16,7 @@ @pytest.mark.xfail( reason="PINT has a newer version of the Arecibo position than TEMPO2" ) -def test_times_against_tempo2(): +def test_times_against_tempo2(pickle_dir): log.setLevel("ERROR") # for nice output info, set the following instead # log.setLevel('INFO') @@ -24,7 +24,12 @@ def test_times_against_tempo2(): ls = u.def_unit("ls", const.c * 1.0 * u.s) log.info("Reading TOAs into PINT") - ts = toa.get_TOAs(datadir + "/testtimes.tim", include_bipm=False, usepickle=False) + ts = toa.get_TOAs( + datadir + "/testtimes.tim", + include_bipm=False, + usepickle=True, + picklefilename=pickle_dir, + ) if log.level < 25: ts.print_summary() ts.table.sort("index") diff --git a/tests/test_timing_model.py b/tests/test_timing_model.py index 808364cc2..12253fb35 100644 --- a/tests/test_timing_model.py +++ b/tests/test_timing_model.py @@ -2,19 +2,19 @@ import os import sys import warnings -from copy import deepcopy from contextlib import redirect_stdout +from copy import deepcopy import astropy.units as u import numpy as np import pytest from hypothesis import given -from hypothesis.strategies import permutations, composite +from hypothesis.strategies import composite, permutations from numpy.testing import assert_allclose -from pint import toa -from pint.observatory import compare_t2_observatories_dat from pinttestdata import datadir +import pint.residuals +from pint import toa from pint.models import ( DEFAULT_ORDER, AstrometryEquatorial, @@ -26,9 +26,9 @@ get_model, parameter as p, ) +from pint.observatory import compare_t2_observatories_dat from pint.simulation import make_fake_toas_uniform from pint.toa import get_TOAs -import pint.residuals @pytest.fixture diff --git a/tests/test_toa.py b/tests/test_toa.py index e4f2b06c4..bf1072f82 100644 --- a/tests/test_toa.py +++ b/tests/test_toa.py @@ -9,8 +9,8 @@ from pint.models import get_model from pint.observatory import get_observatory -from pint.toa import TOA, TOAs from pint.simulation import make_fake_toas_uniform +from pint.toa import TOA, TOAs class TestTOA(unittest.TestCase): diff --git a/tests/test_toa_flag.py b/tests/test_toa_flag.py index 5c60b16b2..706695d8f 100644 --- a/tests/test_toa_flag.py +++ b/tests/test_toa_flag.py @@ -4,9 +4,10 @@ import os import unittest -import pint.toa as toa from pinttestdata import datadir +import pint.toa as toa + class TestToaFlag(unittest.TestCase): """Compare delays from the dd model with tempo and PINT""" diff --git a/tests/test_toa_indexing.py b/tests/test_toa_indexing.py index c06ff0bf5..d98d4d3ed 100644 --- a/tests/test_toa_indexing.py +++ b/tests/test_toa_indexing.py @@ -2,9 +2,9 @@ import numpy as np import pytest -from hypothesis import given, assume -from hypothesis.strategies import slices, integers, booleans, one_of, lists -from hypothesis.extra.numpy import arrays, array_shapes +from hypothesis import assume, given +from hypothesis.extra.numpy import array_shapes, arrays +from hypothesis.strategies import booleans, integers, lists, one_of, slices from pint.toa import get_TOAs diff --git a/tests/test_toa_pickle.py b/tests/test_toa_pickle.py index 5b34af320..c5336fc96 100644 --- a/tests/test_toa_pickle.py +++ b/tests/test_toa_pickle.py @@ -13,8 +13,7 @@ import pint.models import pint.toa -from pint import toa -from pint import simulation +from pint import simulation, toa @pytest.fixture @@ -25,28 +24,6 @@ def temp_tim(tmpdir): return tt, tp -class TestTOAReader(unittest.TestCase): - def setUp(self): - os.chdir(datadir) - # First, read the TOAs from the tim file. - # This should also create the pickle file. - try: - os.remove("test1.tim.pickle.gz") - os.remove("test1.tim.pickle") - except OSError: - pass - tt = toa.get_TOAs("test1.tim", usepickle=False, include_bipm=False) - self.numtoas = tt.ntoas - del tt - # Now read them from the pickle - self.t = toa.get_TOAs("test1.tim", usepickle=True, include_bipm=False) - - def test_pickle(self): - # Initially this just checks that the same number - # of TOAs came out of the pickle as went in. - assert self.t.ntoas == self.numtoas - - def test_pickle_created(temp_tim): tt, tp = temp_tim toa.get_TOAs(tt, usepickle=True) @@ -59,6 +36,13 @@ def test_pickle_works(temp_tim): toa.get_TOAs(tt, usepickle=True) +def test_pickle_recovers_number_of_toas(temp_tim): + tt, tp = temp_tim + t1 = toa.get_TOAs(tt, usepickle=True) + t2 = toa.get_TOAs(tt, usepickle=True) + assert len(t1) == len(t2) + + def test_pickle_used(temp_tim): tt, tp = temp_tim assert not toa.get_TOAs(tt, usepickle=True).was_pickled @@ -126,3 +110,20 @@ def test_pickle_moved(temp_tim): # Should fail since the original file is gone with pytest.raises(FileNotFoundError): toa.get_TOAs(tt, usepickle=True, picklefilename=tp) + + +def test_pickle_dir_works(tmp_path): + a = tmp_path / "a" + a.mkdir() + + b = tmp_path / "b" + b.mkdir() + + shutil.copy(datadir / "test2.tim", a) + toa.get_TOAs(a / "test2.tim", usepickle=True, picklefilename=b) + + assert len(list(a.iterdir())) == 1 + assert len(list(b.iterdir())) == 1 + + t = toa.get_TOAs(a / "test2.tim", usepickle=True, picklefilename=b) + assert t.was_pickled diff --git a/tests/test_toa_selection.py b/tests/test_toa_selection.py index f96424253..444474672 100644 --- a/tests/test_toa_selection.py +++ b/tests/test_toa_selection.py @@ -5,15 +5,15 @@ import astropy.units as u import numpy as np +from pinttestdata import datadir + +import pint.models.model_builder as mb +import pint.toa as toa # import matplotlib # matplotlib.use('TKAgg') # import matplotlib.pyplot as plt -import pint.models.model_builder as mb -import pint.toa as toa -from pinttestdata import datadir - class TestTOAselection(unittest.TestCase): @classmethod diff --git a/tests/test_toa_shuffle.py b/tests/test_toa_shuffle.py index e6ba9be01..33fdb70a1 100644 --- a/tests/test_toa_shuffle.py +++ b/tests/test_toa_shuffle.py @@ -1,22 +1,17 @@ import io import os +import time from copy import deepcopy import numpy as np import pytest -import time - -from hypothesis import given -from hypothesis.strategies import ( - composite, - permutations, -) from astropy import units as u - +from hypothesis import given +from hypothesis.strategies import composite, permutations from pinttestdata import datadir -from pint import simulation, toa import pint.residuals +from pint import simulation, toa from pint.models import get_model, get_model_and_toas shuffletoas = """FORMAT 1 diff --git a/tests/test_toa_writer.py b/tests/test_toa_writer.py index 3a422fb0d..ab0d8743c 100644 --- a/tests/test_toa_writer.py +++ b/tests/test_toa_writer.py @@ -1,14 +1,15 @@ -from io import StringIO import os import os.path +from io import StringIO + +import astropy.units as u +import numpy as np import pytest +from astropy.time import Time +from pinttestdata import datadir +from pint import simulation, toa from pint.models import get_model -from pint import toa, simulation -from pinttestdata import datadir -from astropy.time import Time -import numpy as np -import astropy.units as u def test_roundtrip_bary_toa_Tempo2format(tmpdir): diff --git a/tests/test_troposphere_model.py b/tests/test_troposphere_model.py index 66fc42955..e4d0230d8 100644 --- a/tests/test_troposphere_model.py +++ b/tests/test_troposphere_model.py @@ -1,90 +1,106 @@ #!/usr/bin/env python import os -import unittest +import warnings import astropy.units as u import numpy as np +import pytest +from astropy.coordinates import AltAz, SkyCoord +from astropy.time import Time +from pinttestdata import datadir, testdir + import pint.models as models import pint.observatory import pint.toa as toa -from astropy.coordinates import AltAz, SkyCoord -from astropy.time import Time from pint.observatory import get_observatory -from pinttestdata import testdir, datadir +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass -class TestTroposphereDelay(unittest.TestCase): + s = Setup() - MIN_ALT = 5 # the minimum altitude in degrees for testing the delay model + s.MIN_ALT = 5 # the minimum altitude in degrees for testing the delay model - FLOAT_THRESHOLD = 1e-12 # + s.FLOAT_THRESHOLD = 1e-12 # - def setUp(self): + # parfile = os.path.join(datadir, "J1744-1134.basic.par") + # ngc = os.path.join(datadir, "NGC6440E") - # parfile = os.path.join(datadir, "J1744-1134.basic.par") - # ngc = os.path.join(datadir, "NGC6440E") + s.toas = toa.get_TOAs( + os.path.join(datadir, "NGC6440E.tim"), picklefilename=pickle_dir + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + s.model = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) + s.modelWithTD = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) + s.modelWithTD.CORRECT_TROPOSPHERE.value = True - self.toas = toa.get_TOAs(os.path.join(datadir, "NGC6440E.tim")) - self.model = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) - self.modelWithTD = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) - self.modelWithTD.CORRECT_TROPOSPHERE.value = True + s.toasInvalid = toa.get_TOAs( + os.path.join(datadir, "NGC6440E.tim"), picklefilename=pickle_dir + ) - self.toasInvalid = toa.get_TOAs(os.path.join(datadir, "NGC6440E.tim")) + for i in range(len(s.toasInvalid.table)): + # adjust the timing by half a day to make them invalid + s.toasInvalid.table["mjd"][i] += 0.5 * u.d - for i in range(len(self.toasInvalid.table)): - # adjust the timing by half a day to make them invalid - self.toasInvalid.table["mjd"][i] += 0.5 + s.testAltitudes = np.arange(s.MIN_ALT, 90, 100) * u.deg + s.testHeights = np.array([10, 100, 1000, 5000]) * u.m - self.testAltitudes = np.arange(self.MIN_ALT, 90, 100) * u.deg - self.testHeights = np.array([10, 100, 1000, 5000]) * u.m + s.td = s.modelWithTD.components["TroposphereDelay"] + return s - self.td = self.modelWithTD.components["TroposphereDelay"] - def test_altitudes(self): - # the troposphere delay should strictly decrease with increasing altitude above horizon - delays = self.td.delay_model(self.testAltitudes, 0, 1000 * u.m, 0) +def test_altitudes(setup): + # the troposphere delay should strictly decrease with increasing altitude above horizon + delays = setup.td.delay_model(setup.testAltitudes, 0, 1000 * u.m, 0) - for i in range(1, len(delays)): - assert delays[i] < delays[i - 1] + for i in range(1, len(delays)): + assert delays[i] < delays[i - 1] - def test_heights(self): - # higher elevation observatories should have less troposphere delay - heightDelays = [] # list of the delays at each height - for h in self.testHeights: - heightDelays.append(self.td.delay_model(self.testAltitudes, 0, h, 0)) - for i in range(1, len(self.testHeights)): - print(heightDelays[i], heightDelays[i - 1]) - assert np.all(np.less(heightDelays[i], heightDelays[i - 1])) - def test_model_access(self): - # make sure that the model components are linked correctly to the troposphere delay - assert hasattr(self.model, "CORRECT_TROPOSPHERE") - assert "TroposphereDelay" in self.modelWithTD.components.keys() +def test_heights(setup): + # higher elevation observatories should have less troposphere delay + heightDelays = [] # list of the delays at each height + for h in setup.testHeights: + heightDelays.append(setup.td.delay_model(setup.testAltitudes, 0, h, 0)) + for i in range(1, len(setup.testHeights)): + print(heightDelays[i], heightDelays[i - 1]) + assert np.all(np.less(heightDelays[i], heightDelays[i - 1])) - # the model should have the sky coordinates defined - assert self.td._get_target_skycoord() is not None - def test_invalid_altitudes(self): - assert np.all( - np.less_equal( - np.abs(self.td.troposphere_delay(self.toasInvalid)), - self.FLOAT_THRESHOLD * u.s, - ) +def test_model_access(setup): + # make sure that the model components are linked correctly to the troposphere delay + assert hasattr(setup.model, "CORRECT_TROPOSPHERE") + assert "TroposphereDelay" in setup.modelWithTD.components.keys() + + # the model should have the sky coordinates defined + assert setup.td._get_target_skycoord() is not None + + +def test_invalid_altitudes(setup): + assert np.all( + np.less_equal( + np.abs(setup.td.troposphere_delay(setup.toasInvalid)), + setup.FLOAT_THRESHOLD * u.s, ) + ) + - def test_latitude_index(self): - # the code relies on finding the neighboring latitudes to any site - # for atmospheric constants defined at every 15 degrees - # so I will test the nearest neighbors function +def test_latitude_index(setup): + # the code relies on finding the neighboring latitudes to any site + # for atmospheric constants defined at every 15 degrees + # so I will test the nearest neighbors function - l1 = self.td._find_latitude_index(20 * u.deg) - l2 = self.td._find_latitude_index(-80 * u.deg) - l3 = self.td._find_latitude_index(0 * u.deg) - l4 = self.td._find_latitude_index(-90 * u.deg) + l1 = setup.td._find_latitude_index(20 * u.deg) + l2 = setup.td._find_latitude_index(-80 * u.deg) + l3 = setup.td._find_latitude_index(0 * u.deg) + l4 = setup.td._find_latitude_index(-90 * u.deg) - assert self.td.LAT[l1] <= 20 * u.deg < self.td.LAT[l1 + 1] - assert self.td.LAT[l2] <= 80 * u.deg <= self.td.LAT[l2 + 1] - assert self.td.LAT[l3] <= 0 * u.deg <= self.td.LAT[l3 + 1] - assert self.td.LAT[l4] <= 90 * u.deg <= self.td.LAT[l4 + 1] + assert setup.td.LAT[l1] <= 20 * u.deg < setup.td.LAT[l1 + 1] + assert setup.td.LAT[l2] <= 80 * u.deg <= setup.td.LAT[l2 + 1] + assert setup.td.LAT[l3] <= 0 * u.deg <= setup.td.LAT[l3 + 1] + assert setup.td.LAT[l4] <= 90 * u.deg <= setup.td.LAT[l4 + 1] diff --git a/tests/test_utils.py b/tests/test_utils.py index a8e152e42..06b38add5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -43,10 +43,10 @@ dmxparse, interesting_lines, lines_of, + list_parameters, open_or_use, taylor_horner, taylor_horner_deriv, - list_parameters, ) @@ -602,19 +602,33 @@ def test_time_from_mjd_string_rejects_other_formats(): time_from_mjd_string("58000", format="cxcsec") -def test_dmxparse(): +def test_dmxparse_runs(pickle_dir): """Test for dmxparse function.""" + # FIXME: what does this actually check? m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par")) - t = toa.get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim")) + t = toa.get_TOAs( + os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim"), + usepickle=True, + picklefilename=pickle_dir, + ) f = fitter.GLSFitter(toas=t, model=m) f.fit_toas() - dmx = dmxparse(f, save=False) + dmxparse(f, save=False) + + +def test_dmxparse_runs_wls(pickle_dir): + """Test for dmxparse function.""" # Check exception handling + # FIXME: what does this actually check? m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12_DMX.par")) - t = toa.get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim")) + t = toa.get_TOAs( + os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim"), + usepickle=True, + picklefilename=pickle_dir, + ) f = fitter.WLSFitter(toas=t, model=m) f.fit_toas() - dmx = dmxparse(f, save=False) + dmxparse(f, save=False) def test_pmtot(): diff --git a/tests/test_variety_parfiles.py b/tests/test_variety_parfiles.py index cdc985741..2c768fa9a 100644 --- a/tests/test_variety_parfiles.py +++ b/tests/test_variety_parfiles.py @@ -1,16 +1,16 @@ """Various test for the bad par files""" -import pytest -from io import StringIO import warnings +from io import StringIO +import pytest + +from pint.models.model_builder import get_model from pint.models.timing_model import ( - TimingModelError, - UnknownBinaryModel, MissingBinaryError, MissingParameter, + TimingModelError, + UnknownBinaryModel, ) -from pint.models.model_builder import get_model - base_par = """ PSR J1234+5678 diff --git a/tests/test_widebandTOA_fitting.py b/tests/test_widebandTOA_fitting.py index 6b5af854d..b8c9ea821 100644 --- a/tests/test_widebandTOA_fitting.py +++ b/tests/test_widebandTOA_fitting.py @@ -1,101 +1,114 @@ """ Various of tests for the general data fitter using wideband TOAs. """ +import astropy.units as u +import numpy as np import pytest -import os +from pinttestdata import datadir -import numpy as np -import astropy.units as u +from pint.fitter import WidebandTOAFitter from pint.models import get_model from pint.toa import get_TOAs -from pint.fitter import WidebandTOAFitter -from pinttestdata import datadir -os.chdir(datadir) - - -class TestWidebandTOAFitter: - def setup(self): - self.model = get_model("J1614-2230_NANOGrav_12yv3.wb.gls.par") - self.toas = get_TOAs( - "J1614-2230_NANOGrav_12yv3.wb.tim", ephem="DE436", bipm_version="BIPM2015" - ) - self.fit_data_name = ["toa", "dm"] - self.fit_params_lite = ["F0", "F1", "ELONG", "ELAT", "DMJUMP1", "DMX_0022"] - self.tempo_res = np.genfromtxt( - "J1614-2230_NANOGrav_12yv3.wb.tempo_test", comments="#" - ) - - def test_fitter_init(self): - fitter = WidebandTOAFitter([self.toas], self.model, additional_args={}) - - # test making residuals - assert len(fitter.resids._combined_resids) == 2 * self.toas.ntoas - # test additional args - add_args = {} - add_args["toa"] = {"subtract_mean": False} - fitter2 = WidebandTOAFitter([self.toas], self.model, additional_args=add_args) - - assert fitter2.resids.toa.subtract_mean == False - - def test_fitter_designmatrix(self): - fitter = WidebandTOAFitter([self.toas], self.model, additional_args={}) - fitter.model.free_params = self.fit_params_lite - assert set(fitter.model.free_params) == set(self.fit_params_lite) - # test making design matrix - d_matrix = fitter.get_designmatrix() - assert d_matrix.shape == (2 * self.toas.ntoas, len(self.fit_params_lite) + 1) - assert [lb[0] for lb in d_matrix.labels[0]] == ["toa", "dm"] - assert d_matrix.derivative_params == (["Offset"] + fitter.model.free_params) - - def test_fitting_no_full_cov(self): - fitter = WidebandTOAFitter([self.toas], self.model, additional_args={}) - time_rms_pre = fitter.resids_init.toa.rms_weighted() - dm_rms_pre = fitter.resids_init.dm.rms_weighted() - fitter.fit_toas() - dm_rms_post = fitter.resids.dm.rms_weighted() - - prefit_pint = fitter.resids_init.toa.time_resids - prefit_tempo = self.tempo_res[:, 0] * u.us - diff_prefit = (prefit_pint - prefit_tempo).to(u.ns) - # 50 ns is the difference of PINT tempo precession and nautation model. - assert np.abs(diff_prefit - diff_prefit.mean()).max() < 50 * u.ns - postfit_pint = fitter.resids.toa.time_resids - postfit_tempo = self.tempo_res[:, 1] * u.us - diff_postfit = (postfit_pint - postfit_tempo).to(u.ns) - assert np.abs(diff_postfit - diff_postfit.mean()).max() < 50 * u.ns - assert np.abs(dm_rms_pre - dm_rms_post) < 5e-8 * dm_rms_pre.unit - - def test_fitting_full_cov(self): - fitter2 = WidebandTOAFitter([self.toas], self.model, additional_args={}) - time_rms_pre = fitter2.resids_init.toa.rms_weighted() - dm_rms_pre = fitter2.resids_init.dm.rms_weighted() - fitter2.fit_toas(full_cov=True) - time_rms_post = fitter2.resids.toa.rms_weighted() - dm_rms_post = fitter2.resids.dm.rms_weighted() - - prefit_pint = fitter2.resids_init.toa.time_resids - prefit_tempo = self.tempo_res[:, 0] * u.us - diff_prefit = (prefit_pint - prefit_tempo).to(u.ns) - # 50 ns is the difference of PINT tempo precession and nautation model. - assert np.abs(diff_prefit - diff_prefit.mean()).max() < 50 * u.ns - postfit_pint = fitter2.resids.toa.time_resids - postfit_tempo = self.tempo_res[:, 1] * u.us - diff_postfit = (postfit_pint - postfit_tempo).to(u.ns) - assert np.abs(diff_postfit - diff_postfit.mean()).max() < 50 * u.ns - assert np.abs(dm_rms_pre - dm_rms_post) < 5e-8 * dm_rms_pre.unit - - def test_noise_design_matrix_index(self): - model = get_model("B1855+09_NANOGrav_12yv3.wb.gls.par") - toas = get_TOAs( - "B1855+09_NANOGrav_12yv3.wb.tim", ephem="DE436", bipm_version="BIPM2015" - ) - fitter = WidebandTOAFitter(toas, model, additional_args={}) - fitter.fit_toas(full_cov=False, debug=True) - # Test red noise basis - pl_rd = fitter.model.pl_rn_basis_weight_pair(fitter.toas)[0] - p0, p1 = fitter.resids.pl_red_noise_M_index - pl_rd_backwards = ( - fitter.resids.pl_red_noise_M[0] * fitter.resids.norm[p0:p1][np.newaxis, :] - ) - assert np.all(np.isclose(pl_rd, pl_rd_backwards[0:313, :])) + +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + s.model = get_model(datadir / "J1614-2230_NANOGrav_12yv3.wb.gls.par") + s.toas = get_TOAs( + datadir / "J1614-2230_NANOGrav_12yv3.wb.tim", + ephem="DE436", + bipm_version="BIPM2015", + picklefilename=pickle_dir, + ) + s.fit_data_name = ["toa", "dm"] + s.fit_params_lite = ["F0", "F1", "ELONG", "ELAT", "DMJUMP1", "DMX_0022"] + s.tempo_res = np.genfromtxt( + datadir / "J1614-2230_NANOGrav_12yv3.wb.tempo_test", comments="#" + ) + return s + + +def test_fitter_init(setup): + fitter = WidebandTOAFitter([setup.toas], setup.model, additional_args={}) + + # test making residuals + assert len(fitter.resids._combined_resids) == 2 * setup.toas.ntoas + # test additional args + add_args = {} + add_args["toa"] = {"subtract_mean": False} + fitter2 = WidebandTOAFitter([setup.toas], setup.model, additional_args=add_args) + + assert fitter2.resids.toa.subtract_mean == False + + +def test_fitter_designmatrix(setup): + fitter = WidebandTOAFitter([setup.toas], setup.model, additional_args={}) + fitter.model.free_params = setup.fit_params_lite + assert set(fitter.model.free_params) == set(setup.fit_params_lite) + # test making design matrix + d_matrix = fitter.get_designmatrix() + assert d_matrix.shape == (2 * setup.toas.ntoas, len(setup.fit_params_lite) + 1) + assert [lb[0] for lb in d_matrix.labels[0]] == ["toa", "dm"] + assert d_matrix.derivative_params == (["Offset"] + fitter.model.free_params) + + +def test_fitting_no_full_cov(setup): + fitter = WidebandTOAFitter([setup.toas], setup.model, additional_args={}) + time_rms_pre = fitter.resids_init.toa.rms_weighted() + dm_rms_pre = fitter.resids_init.dm.rms_weighted() + fitter.fit_toas() + dm_rms_post = fitter.resids.dm.rms_weighted() + + prefit_pint = fitter.resids_init.toa.time_resids + prefit_tempo = setup.tempo_res[:, 0] * u.us + diff_prefit = (prefit_pint - prefit_tempo).to(u.ns) + # 50 ns is the difference of PINT tempo precession and nautation model. + assert np.abs(diff_prefit - diff_prefit.mean()).max() < 50 * u.ns + postfit_pint = fitter.resids.toa.time_resids + postfit_tempo = setup.tempo_res[:, 1] * u.us + diff_postfit = (postfit_pint - postfit_tempo).to(u.ns) + assert np.abs(diff_postfit - diff_postfit.mean()).max() < 50 * u.ns + assert np.abs(dm_rms_pre - dm_rms_post) < 5e-8 * dm_rms_pre.unit + + +def test_fitting_full_cov(setup): + fitter2 = WidebandTOAFitter([setup.toas], setup.model, additional_args={}) + time_rms_pre = fitter2.resids_init.toa.rms_weighted() + dm_rms_pre = fitter2.resids_init.dm.rms_weighted() + fitter2.fit_toas(full_cov=True) + time_rms_post = fitter2.resids.toa.rms_weighted() + dm_rms_post = fitter2.resids.dm.rms_weighted() + + prefit_pint = fitter2.resids_init.toa.time_resids + prefit_tempo = setup.tempo_res[:, 0] * u.us + diff_prefit = (prefit_pint - prefit_tempo).to(u.ns) + # 50 ns is the difference of PINT tempo precession and nautation model. + assert np.abs(diff_prefit - diff_prefit.mean()).max() < 50 * u.ns + postfit_pint = fitter2.resids.toa.time_resids + postfit_tempo = setup.tempo_res[:, 1] * u.us + diff_postfit = (postfit_pint - postfit_tempo).to(u.ns) + assert np.abs(diff_postfit - diff_postfit.mean()).max() < 50 * u.ns + assert np.abs(dm_rms_pre - dm_rms_post) < 5e-8 * dm_rms_pre.unit + + +def test_noise_design_matrix_index(pickle_dir): + model = get_model(datadir / "B1855+09_NANOGrav_12yv3.wb.gls.par") + toas = get_TOAs( + datadir / "B1855+09_NANOGrav_12yv3.wb.tim", + ephem="DE436", + bipm_version="BIPM2015", + picklefilename=pickle_dir, + ) + fitter = WidebandTOAFitter(toas, model, additional_args={}) + fitter.fit_toas(full_cov=False, debug=True) + # Test red noise basis + pl_rd = fitter.model.pl_rn_basis_weight_pair(fitter.toas)[0] + p0, p1 = fitter.resids.pl_red_noise_M_index + pl_rd_backwards = ( + fitter.resids.pl_red_noise_M[0] * fitter.resids.norm[p0:p1][np.newaxis, :] + ) + assert np.all(np.isclose(pl_rd, pl_rd_backwards[0:313, :])) diff --git a/tests/test_wideband_dm_data.py b/tests/test_wideband_dm_data.py index f30d8cd27..d3014e181 100644 --- a/tests/test_wideband_dm_data.py +++ b/tests/test_wideband_dm_data.py @@ -9,13 +9,13 @@ import numpy as np import pytest from astropy.time import TimeDelta -from pinttestdata import datadir from numpy.testing import assert_allclose +from pinttestdata import datadir +from pint.fitter import WidebandTOAFitter from pint.models import get_model from pint.residuals import Residuals, WidebandTOAResiduals from pint.toa import get_TOAs -from pint.fitter import WidebandTOAFitter os.chdir(datadir) @@ -87,82 +87,89 @@ def wb_toas_all(wb_model): return toas -class TestDMData: - def setup(self): - self.model = get_model("J1614-2230_NANOGrav_12yv3.wb.gls.par") - self.toas = get_TOAs("J1614-2230_NANOGrav_12yv3.wb.tim") - toa_backends, valid_flags = self.toas.get_flag_value("fe") - self.toa_backends = np.array(toa_backends) - self.dm_jump_params = [ - getattr(self.model, x) - for x in self.model.params - if (x.startswith("DMJUMP")) - ] +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + s.model = get_model(datadir / "J1614-2230_NANOGrav_12yv3.wb.gls.par") + s.toas = get_TOAs( + datadir / "J1614-2230_NANOGrav_12yv3.wb.tim", picklefilename=pickle_dir + ) + toa_backends, valid_flags = s.toas.get_flag_value("fe") + s.toa_backends = np.array(toa_backends) + s.dm_jump_params = [ + getattr(s.model, x) for x in s.model.params if (x.startswith("DMJUMP")) + ] + return s + + +def test_data_reading(setup): + dm_data_raw, valid = setup.toas.get_flag_value("pp_dm") + # For this input, the DM number should be the same with the TOA number. + dm_data = np.array(dm_data_raw)[valid] + assert len(valid) == setup.toas.ntoas + assert len(dm_data) == setup.toas.ntoas + assert dm_data.mean != 0.0 + + +def test_dm_modelcomponent(setup): + assert "DispersionJump" in setup.model.components.keys() + assert "ScaleDmError" in setup.model.components.keys() + assert "SolarWindDispersion" in setup.model.components.keys() + + +def test_dm_jumps(setup): + # First get the toas for jump + all_backends = list(set(setup.toa_backends)) + dm_jump_value = setup.model.jump_dm(setup.toas) + dm_jump_map = {} + for dmj in setup.dm_jump_params: + dm_jump_map[dmj.key_value[0]] = dmj + for be in all_backends: + assert all(dm_jump_value[setup.toa_backends == be] == -dm_jump_map[be].quantity) - def test_data_reading(self): - dm_data_raw, valid = self.toas.get_flag_value("pp_dm") - # For this input, the DM number should be the same with the TOA number. - dm_data = np.array(dm_data_raw)[valid] - assert len(valid) == self.toas.ntoas - assert len(dm_data) == self.toas.ntoas - assert dm_data.mean != 0.0 - - def test_dm_modelcomponent(self): - assert "DispersionJump" in self.model.components.keys() - assert "ScaleDmError" in self.model.components.keys() - assert "SolarWindDispersion" in self.model.components.keys() - - def test_dm_jumps(self): - # First get the toas for jump - all_backends = list(set(self.toa_backends)) - dm_jump_value = self.model.jump_dm(self.toas) - dm_jump_map = {} - for dmj in self.dm_jump_params: - dm_jump_map[dmj.key_value[0]] = dmj - for be in all_backends: - assert all( - dm_jump_value[self.toa_backends == be] == -dm_jump_map[be].quantity - ) - - r = WidebandTOAResiduals( - self.toas, self.model, dm_resid_args=dict(subtract_mean=False) - ) - - model2 = deepcopy(self.model) - for i, be in enumerate(all_backends): - dm_jump_map[be].value += i + 1 - - r2 = WidebandTOAResiduals( - self.toas, model2, dm_resid_args=dict(subtract_mean=False) - ) - - delta_dm = r2.dm.resids_value - r.dm.resids_value - delta_dm_intended = np.zeros_like(delta_dm) - for i, be in enumerate(all_backends): - delta_dm_intended[self.toa_backends == be] = -(i + 1) - assert np.allclose(delta_dm, delta_dm_intended) - - def test_dmjump_derivative(self): - # This test is designe to test the dm jump derivatives. - # First get the toas for jump - for dmj_param in self.dm_jump_params: - # test derivative function in the dmjump component - d_dm_d_dmjump = self.model.d_dm_d_dmjump(self.toas, dmj_param.name) - be = dmj_param.key_value - # The derivative of dm with respect to dm jump is -1 for the jumped - # TOAs/DM data, the others are zero - assert all(d_dm_d_dmjump[self.toa_backends == be] == -1.0 * u.Unit("")) - assert all(d_dm_d_dmjump[self.toa_backends != be] == 0.0 * u.Unit("")) - d_delay_d_dmjump = self.model.d_delay_d_dmjump(self.toas, dmj_param.name) - # The derivative of delay with respect to dm jump is 0. - assert all(d_delay_d_dmjump == 0.0 * (u.s / dmj_param.units)) - # Test the registered functions in the timing model. - # When constructing the design matrixes, the registered function - # will be called. - assert self.model.delay_deriv_funcs[dmj_param.name] == [ - self.model.d_delay_d_dmjump - ] - assert self.model.dm_derivs[dmj_param.name] == [self.model.d_dm_d_dmjump] + r = WidebandTOAResiduals( + setup.toas, setup.model, dm_resid_args=dict(subtract_mean=False) + ) + + model2 = deepcopy(setup.model) + for i, be in enumerate(all_backends): + dm_jump_map[be].value += i + 1 + + r2 = WidebandTOAResiduals( + setup.toas, model2, dm_resid_args=dict(subtract_mean=False) + ) + + delta_dm = r2.dm.resids_value - r.dm.resids_value + delta_dm_intended = np.zeros_like(delta_dm) + for i, be in enumerate(all_backends): + delta_dm_intended[setup.toa_backends == be] = -(i + 1) + assert np.allclose(delta_dm, delta_dm_intended) + + +def test_dmjump_derivative(setup): + # This test is designe to test the dm jump derivatives. + # First get the toas for jump + for dmj_param in setup.dm_jump_params: + # test derivative function in the dmjump component + d_dm_d_dmjump = setup.model.d_dm_d_dmjump(setup.toas, dmj_param.name) + be = dmj_param.key_value + # The derivative of dm with respect to dm jump is -1 for the jumped + # TOAs/DM data, the others are zero + assert all(d_dm_d_dmjump[setup.toa_backends == be] == -1.0 * u.Unit("")) + assert all(d_dm_d_dmjump[setup.toa_backends != be] == 0.0 * u.Unit("")) + d_delay_d_dmjump = setup.model.d_delay_d_dmjump(setup.toas, dmj_param.name) + # The derivative of delay with respect to dm jump is 0. + assert all(d_delay_d_dmjump == 0.0 * (u.s / dmj_param.units)) + # Test the registered functions in the timing model. + # When constructing the design matrixes, the registered function + # will be called. + assert setup.model.delay_deriv_funcs[dmj_param.name] == [ + setup.model.d_delay_d_dmjump + ] + assert setup.model.dm_derivs[dmj_param.name] == [setup.model.d_dm_d_dmjump] def test_wideband_residuals(wb_model, wb_toas): diff --git a/tests/test_wls_fitter.py b/tests/test_wls_fitter.py index 089bf179d..077dbd707 100644 --- a/tests/test_wls_fitter.py +++ b/tests/test_wls_fitter.py @@ -1,60 +1,62 @@ #! /usr/bin/env python import os -import unittest -from pint.models.model_builder import get_model +import pytest +from pinttestdata import datadir + from pint import toa from pint.fitter import WLSFitter -from pinttestdata import datadir +from pint.models.model_builder import get_model + +per_param = { + "A1": 1e-05, + "DECJ": 1e-06, + "DMX_0003": 120, + "ECC": 0.2, + "F0": 1e-12, + "F1": 0.001, + "JUMP3": 10.0, + "M2": 10.0, + "OM": 1e-06, + "PB": 1e-08, + "PMDEC": 0.1, + "PMRA": 0.1, + "PX": 100, + "RAJ": 1e-08, + "SINI": -0.004075, + "T0": 1e-10, +} + + +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + cls = Setup() + cls.par = datadir / "B1855+09_NANOGrav_dfg+12_TAI_FB90.par" + cls.tim = datadir / "B1855+09_NANOGrav_dfg+12.tim" + cls.m = get_model(cls.par) + cls.t = toa.get_TOAs(cls.tim, ephem="DE405", picklefilename=pickle_dir) + cls.f = WLSFitter(cls.t, cls.m) + # set perturb parameter step + return cls + + +def perturb_param(setup, param, h): + par = getattr(setup.f.model, param) + orv = par.value + par.value = (1 + h) * orv + setup.f.model.free_params = [param] + + +@pytest.mark.parametrize("param, delta", list(per_param.items())) +def test_wls_fitter(setup, param, delta): + perturb_param(setup, param, delta) + setup.f.fit_toas() + red_chi2 = setup.f.resids.reduced_chi2 + assert red_chi2 < 2.6 -class Testwls(unittest.TestCase): - """Compare delays from the dd model with tempo and PINT""" - - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.par = "B1855+09_NANOGrav_dfg+12_TAI_FB90.par" - cls.tim = "B1855+09_NANOGrav_dfg+12.tim" - cls.m = get_model(cls.par) - cls.t = toa.get_TOAs(cls.tim, ephem="DE405") - cls.f = WLSFitter(cls.t, cls.m) - # set perturb parameter step - cls.per_param = { - "A1": 1e-05, - "DECJ": 1e-06, - "DMX_0003": 120, - "ECC": 0.2, - "F0": 1e-12, - "F1": 0.001, - "JUMP3": 10.0, - "M2": 10.0, - "OM": 1e-06, - "PB": 1e-08, - "PMDEC": 0.1, - "PMRA": 0.1, - "PX": 100, - "RAJ": 1e-08, - "SINI": -0.004075, - "T0": 1e-10, - } - - def perturb_param(self, param, h): - self.f.reset_model() - par = getattr(self.f.model, param) - orv = par.value - par.value = (1 + h) * orv - self.f.update_resids() - self.f.model.free_params = [param] - - def test_wlf_fitter(self): - for ii, p in enumerate(self.per_param.keys()): - self.perturb_param(p, self.per_param[p]) - self.f.fit_toas() - red_chi2 = self.f.resids.reduced_chi2 - tol = 2.6 - msg = "Fitting parameter " + p + " failed. with red_chi2 " + str(red_chi2) - assert red_chi2 < tol, msg - - def test_has_correlated_errors(self): - assert not self.f.resids.model.has_correlated_errors +def test_has_correlated_errors(setup): + assert not setup.f.resids.model.has_correlated_errors diff --git a/tests/test_zima.py b/tests/test_zima.py index 73594b2ec..e5fafbaf9 100644 --- a/tests/test_zima.py +++ b/tests/test_zima.py @@ -2,12 +2,12 @@ import os import sys import unittest +from io import StringIO import numpy as np -from io import StringIO +from pinttestdata import datadir, testdir import pint.scripts.zima as zima -from pinttestdata import datadir, testdir def test_result(tmp_path): diff --git a/tests/utils.py b/tests/utils.py index 54874af59..cb75eb387 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,10 +1,11 @@ """Utililty functions for the tests""" import logging import os -from io import StringIO import unittest -import pytest import warnings +from io import StringIO + +import pytest def verify_stand_alone_binary_parameter_updates(m): From 237d0a0eabdeda178dfc832ec740f2366344165c Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Tue, 12 Jul 2022 14:05:43 +0100 Subject: [PATCH 2/9] Suppress warning --- tests/test_copy.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_copy.py b/tests/test_copy.py index 00d984db1..9cee5c3b3 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -4,19 +4,18 @@ import copy import os import sys +import warnings import astropy.units as u import numpy as np import pytest +from astropy.table import Table from pinttestdata import datadir -from pint.fitter import WidebandTOAFitter -from pint.models import get_model -from astropy.table import Table -from pint.models import get_model -from pint.fitter import WidebandTOAFitter import pint.fitter import pint.residuals +from pint.fitter import WidebandTOAFitter +from pint.models import get_model from pint.toa import get_TOAs parfile = os.path.join(datadir, "NGC6440E.par") @@ -25,7 +24,10 @@ @pytest.fixture def model(): - return get_model(parfile) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + # suppress T2CMETHOD warning + return get_model(parfile) @pytest.fixture From 2f19357ee3d305e447b438189c480a39c0b93027 Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Tue, 12 Jul 2022 14:14:53 +0100 Subject: [PATCH 3/9] Smarter warning suppression --- tests/test_copy.py | 7 ++++--- tests/test_fitter.py | 2 +- tests/test_model_derivatives.py | 1 - tests/test_toa_flag.py | 6 +++--- tests/test_troposphere_model.py | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_copy.py b/tests/test_copy.py index 9cee5c3b3..0ac30e6c8 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -25,8 +25,7 @@ @pytest.fixture def model(): with warnings.catch_warnings(): - warnings.simplefilter("ignore") - # suppress T2CMETHOD warning + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") return get_model(parfile) @@ -74,7 +73,9 @@ def test_copy_fitter_object(model, toas): def test_copy_wideband_fitter_object(): - model = get_model(os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.gls.par")) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + model = get_model(os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.gls.par")) toas = get_TOAs(os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.tim")) fitter = WidebandTOAFitter([toas], model, additional_args={}) fitter_copy = copy.deepcopy(fitter) diff --git a/tests/test_fitter.py b/tests/test_fitter.py index c260c212b..0d44dab98 100644 --- a/tests/test_fitter.py +++ b/tests/test_fitter.py @@ -1,7 +1,7 @@ #! /usr/bin/env python import os -from io import StringIO from copy import deepcopy +from io import StringIO import astropy.units as u diff --git a/tests/test_model_derivatives.py b/tests/test_model_derivatives.py index f3d89a8bd..3210b6724 100644 --- a/tests/test_model_derivatives.py +++ b/tests/test_model_derivatives.py @@ -152,7 +152,6 @@ def f(value): else: p.value = value * p.units with quiet(): - warnings.simplefilter("ignore") try: dphase = m.phase(toas, abs_phase=False) - phase except ValueError: diff --git a/tests/test_toa_flag.py b/tests/test_toa_flag.py index 700fd9345..ec59e193e 100644 --- a/tests/test_toa_flag.py +++ b/tests/test_toa_flag.py @@ -1,13 +1,13 @@ """Various tests to assess the performance of TOA get_flag_value.""" +import copy +import io import os import unittest -import io + import numpy as np import pytest -import copy - from pinttestdata import datadir import pint.toa as toa diff --git a/tests/test_troposphere_model.py b/tests/test_troposphere_model.py index e4d0230d8..3d3b72a14 100644 --- a/tests/test_troposphere_model.py +++ b/tests/test_troposphere_model.py @@ -34,7 +34,7 @@ class Setup: os.path.join(datadir, "NGC6440E.tim"), picklefilename=pickle_dir ) with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") s.model = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) s.modelWithTD = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) s.modelWithTD.CORRECT_TROPOSPHERE.value = True From cba3e55bb28718d17194d7b7f07255497e3a6e76 Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Tue, 12 Jul 2022 20:15:44 +0100 Subject: [PATCH 4/9] Source fixes revealed by warnings cleanup --- src/pint/models/binary_dd.py | 6 +- src/pint/pint_matrix.py | 6 +- tests/test_TDB_method.py | 24 ++--- tests/test_residuals.py | 173 ++++++++++++++++++++--------------- tests/test_solar_wind.py | 9 -- tests/test_timing_model.py | 13 ++- 6 files changed, 119 insertions(+), 112 deletions(-) diff --git a/src/pint/models/binary_dd.py b/src/pint/models/binary_dd.py index 107ed1750..ce1bb1412 100644 --- a/src/pint/models/binary_dd.py +++ b/src/pint/models/binary_dd.py @@ -84,16 +84,16 @@ def validate(self): # If any *DOT is set, we need T0 for p in ("PBDOT", "OMDOT", "EDOT", "A1DOT"): if getattr(self, p).value is None: - getattr(self, p).set("0") + getattr(self, p).value = 0 getattr(self, p).frozen = True if self.GAMMA.value is None: - self.GAMMA.set("0") + self.GAMMA.value = 0 self.GAMMA.frozen = True # If eccentricity is zero, freeze some parameters to 0 # OM = 0 -> T0 = TASC if self.ECC.value == 0 or self.ECC.value is None: for p in ("ECC", "OM", "OMDOT", "EDOT"): - getattr(self, p).set("0") + getattr(self, p).value = 0 getattr(self, p).frozen = True diff --git a/src/pint/pint_matrix.py b/src/pint/pint_matrix.py index ec3d28b81..dc1158e06 100644 --- a/src/pint/pint_matrix.py +++ b/src/pint/pint_matrix.py @@ -227,7 +227,7 @@ def get_label_along_axis(self, axis, label_name): DEPRECATED - use get_label(..., axis=axis) """ warn( - "This method is deprecated. Use `parameter_covariance_matrix.get_label(..., axis=axis)[0]` instead of `parameter_covariance_matrix.get_label_along_axis(...)", + "This method is deprecated. Use `parameter_covariance_matrix.get_label(..., axis=axis)[0]` instead of `parameter_covariance_matrix.get_label_along_axis(axis, ...)", category=DeprecationWarning, ) label_in_one_axis = self.axis_labels[axis] @@ -615,7 +615,7 @@ def combine_design_matrices_by_param(matrix1, matrix2, padding=0.0): append_offset = matrix1.shape[0] base_matrix = matrix1.matrix for d_quantity in matrix2.derivative_quantity: - quantity_label = matrix2.get_label_along_axis(0, d_quantity) + quantity_label = matrix2.get_label(d_quantity, axis=0)[0] if d_quantity in base_quantity_index.keys(): # Check quantity size d_quantity_size = quantity_label[3] - quantity_label[2] @@ -658,7 +658,7 @@ def combine_design_matrices_by_param(matrix1, matrix2, padding=0.0): new_matrix.fill(padding) # Fill up the new_matrix with matrix2 for quantity, new_idx in new_quantity_index.items(): - old_idx = matrix2.get_label_along_axis(0, d_quantity)[2:4] + old_idx = matrix2.get_label(d_quantity, axis=0)[0][2:4] new_matrix[new_idx[0] : new_idx[1], :] = matrix2.matrix[ old_idx[0] : old_idx[1], : ] diff --git a/tests/test_TDB_method.py b/tests/test_TDB_method.py index ceeb705fb..658fe3fe3 100644 --- a/tests/test_TDB_method.py +++ b/tests/test_TDB_method.py @@ -1,6 +1,5 @@ """tests for different compute TDB method.""" import os -import unittest import numpy as np from pinttestdata import datadir @@ -8,18 +7,11 @@ import pint.toa as toa -class TestTDBMethod(unittest.TestCase): - """Compare delays from the dd model with tempo and PINT""" - - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.tim = "B1855+09_NANOGrav_9yv1.tim" - - def test_astropy_ephem(self): - t_astropy = toa.get_TOAs(self.tim, ephem="DE436t") - t_ephem = toa.get_TOAs(self.tim, ephem="DE436t", tdb_method="ephemeris") - diff = (t_astropy.table["tdbld"] - t_ephem.table["tdbld"]) * 86400.0 - assert np.all(np.abs(diff) < 5e-9), ( - "Test TDB method, 'astropy' vs " "'ephemeris' failed." - ) +def test_astropy_ephem(pickle_dir): + tim = datadir / "B1855+09_NANOGrav_9yv1.tim" + t_astropy = toa.get_TOAs(tim, ephem="DE436t", picklefilename=pickle_dir) + t_ephem = toa.get_TOAs( + tim, ephem="DE436t", tdb_method="ephemeris", picklefilename=pickle_dir + ) + diff = (t_astropy.table["tdbld"] - t_ephem.table["tdbld"]) * 86400.0 + assert np.all(np.abs(diff) < 5e-9) diff --git a/tests/test_residuals.py b/tests/test_residuals.py index aa56b3f2c..9e6623ba4 100644 --- a/tests/test_residuals.py +++ b/tests/test_residuals.py @@ -1,8 +1,7 @@ """ Test for residual class """ -import os -from copy import deepcopy +import warnings from io import StringIO import astropy.units as u @@ -14,13 +13,9 @@ from pint.fitter import GLSFitter, WidebandTOAFitter, WLSFitter from pint.models import get_model -from pint.models.dispersion_model import Dispersion from pint.residuals import CombinedResiduals, Residuals, WidebandTOAResiduals from pint.simulation import make_fake_toas_uniform from pint.toa import get_TOAs -from pint.utils import weighted_mean - -os.chdir(datadir) @pytest.fixture @@ -49,78 +44,108 @@ def wideband_fake(): return toas, model -class TestResidualBuilding: - def setup(self): - self.model = get_model("J1614-2230_NANOGrav_12yv3.wb.gls.par") - # self.toa = make_fake_toas(57000,59000,20,model=self.model) - self.toa = get_TOAs("J1614-2230_NANOGrav_12yv3.wb.tim") +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + # s.toa = make_fake_toas(57000,59000,20,model=s.model) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + s.model = get_model(datadir / "J1614-2230_NANOGrav_12yv3.wb.gls.par") + s.toa = get_TOAs( + datadir / "J1614-2230_NANOGrav_12yv3.wb.tim", picklefilename=pickle_dir + ) + return s - def test_build_phase_residual(self): - phase_res = Residuals(toas=self.toa, model=self.model) - assert len(phase_res.phase_resids) == self.toa.ntoas - # Test no mean subtraction - phase_res_nomean = Residuals( - toas=self.toa, model=self.model, residual_type="toa", subtract_mean=False - ) - assert len(phase_res_nomean.resids) == self.toa.ntoas - assert phase_res.resids.unit == phase_res.unit - phase_res_noweight = Residuals( - toas=self.toa, - model=self.model, - residual_type="toa", - subtract_mean=True, - use_weighted_mean=False, - ) - # assert np.all(phase_res_nomean.resids - - # phase_res_nomean.resids.mean() == phase_res_noweight.resids) +def test_build_phase_residual(setup): + phase_res = Residuals(toas=setup.toa, model=setup.model) + assert len(phase_res.phase_resids) == setup.toa.ntoas - def test_build_dm_residual(self): - dm_res = Residuals(toas=self.toa, model=self.model, residual_type="dm") - assert len(dm_res.resids) == self.toa.ntoas - # Test no mean subtraction - dm_res_nomean = Residuals( - toas=self.toa, model=self.model, residual_type="dm", subtract_mean=True - ) - assert len(dm_res_nomean.resids) == self.toa.ntoas - weight = 1.0 / (dm_res.dm_error**2) - wm = np.average(dm_res.resids, weights=weight) - assert np.all(dm_res.resids - wm == dm_res_nomean.resids) - dm_res_noweight = Residuals( - toas=self.toa, - model=self.model, - residual_type="dm", - subtract_mean=True, - use_weighted_mean=False, - ) - assert np.all(dm_res.resids - dm_res.resids.mean() == dm_res_noweight.resids) - - def test_combined_residuals(self): - phase_res = Residuals(toas=self.toa, model=self.model) - dm_res = Residuals(toas=self.toa, model=self.model, residual_type="dm") - cb_residuals = CombinedResiduals([phase_res, dm_res]) - cb_chi2 = cb_residuals.chi2 - - assert len(cb_residuals._combined_resids) == 2 * self.toa.ntoas - assert cb_residuals.unit["toa"] == u.s - assert cb_residuals.unit["dm"] == u.pc / u.cm**3 - assert cb_chi2 == phase_res.chi2 + dm_res.chi2 - with pytest.raises(AttributeError): - cb_residuals.dof - with pytest.raises(AttributeError): - cb_residuals.residual_objs["toa"].dof - with pytest.raises(AttributeError): - cb_residuals.residual_objs["dm"].dof - with pytest.raises(AttributeError): - cb_residuals.model - - def test_wideband_residuals(self): - wb_res = WidebandTOAResiduals(toas=self.toa, model=self.model) - assert wb_res.dof == 419 - # Make sure the model object are shared by all individual residual class - assert wb_res.model is wb_res.toa.model - assert wb_res.model is wb_res.dm.model +def test_build_phase_residual_nomean(setup): + phase_res = Residuals(toas=setup.toa, model=setup.model) + + # Test no mean subtraction + phase_res_nomean = Residuals( + toas=setup.toa, model=setup.model, residual_type="toa", subtract_mean=False + ) + assert len(phase_res_nomean.resids) == setup.toa.ntoas + assert phase_res.resids.unit == phase_res.unit + + +@pytest.mark.xfail( + reason="Historical test, maybe broken maybe not?", raises=AssertionError +) +def test_build_phase_residual_noweight(setup): + # Test no mean subtraction + phase_res_nomean = Residuals( + toas=setup.toa, model=setup.model, residual_type="toa", subtract_mean=False + ) + + phase_res_noweight = Residuals( + toas=setup.toa, + model=setup.model, + residual_type="toa", + subtract_mean=True, + use_weighted_mean=False, + ) + assert np.all( + phase_res_nomean.resids - phase_res_nomean.resids.mean() + == phase_res_noweight.resids + ) + + +def test_build_dm_residual(setup): + dm_res = Residuals(toas=setup.toa, model=setup.model, residual_type="dm") + assert len(dm_res.resids) == setup.toa.ntoas + + # Test no mean subtraction + dm_res_nomean = Residuals( + toas=setup.toa, model=setup.model, residual_type="dm", subtract_mean=True + ) + assert len(dm_res_nomean.resids) == setup.toa.ntoas + weight = 1.0 / (dm_res.dm_error**2) + wm = np.average(dm_res.resids, weights=weight) + assert np.all(dm_res.resids - wm == dm_res_nomean.resids) + dm_res_noweight = Residuals( + toas=setup.toa, + model=setup.model, + residual_type="dm", + subtract_mean=True, + use_weighted_mean=False, + ) + assert np.all(dm_res.resids - dm_res.resids.mean() == dm_res_noweight.resids) + + +def test_combined_residuals(setup): + phase_res = Residuals(toas=setup.toa, model=setup.model) + dm_res = Residuals(toas=setup.toa, model=setup.model, residual_type="dm") + cb_residuals = CombinedResiduals([phase_res, dm_res]) + cb_chi2 = cb_residuals.chi2 + + assert len(cb_residuals._combined_resids) == 2 * setup.toa.ntoas + assert cb_residuals.unit["toa"] == u.s + assert cb_residuals.unit["dm"] == u.pc / u.cm**3 + assert cb_chi2 == phase_res.chi2 + dm_res.chi2 + with pytest.raises(AttributeError): + cb_residuals.dof + with pytest.raises(AttributeError): + cb_residuals.residual_objs["toa"].dof + with pytest.raises(AttributeError): + cb_residuals.residual_objs["dm"].dof + with pytest.raises(AttributeError): + cb_residuals.model + + +def test_wideband_residuals(setup): + wb_res = WidebandTOAResiduals(toas=setup.toa, model=setup.model) + assert wb_res.dof == 419 + # Make sure the model object are shared by all individual residual class + assert wb_res.model is wb_res.toa.model + assert wb_res.model is wb_res.dm.model def test_residuals_scaled_uncertainties(): diff --git a/tests/test_solar_wind.py b/tests/test_solar_wind.py index c8db21849..711d7632a 100644 --- a/tests/test_solar_wind.py +++ b/tests/test_solar_wind.py @@ -1,24 +1,15 @@ """ Test for pint solar wind module """ -import copy -import os -import sys from io import StringIO -import astropy.units as u import numpy as np import pytest from astropy.time import Time from numpy.testing import assert_allclose -from pinttestdata import datadir -from pint.fitter import WidebandTOAFitter from pint.models import get_model from pint.simulation import make_fake_toas_uniform -from pint.toa import get_TOAs - -os.chdir(datadir) par = """ PSR J1234+5678 diff --git a/tests/test_timing_model.py b/tests/test_timing_model.py index 12253fb35..1c4616bae 100644 --- a/tests/test_timing_model.py +++ b/tests/test_timing_model.py @@ -33,21 +33,20 @@ @pytest.fixture def model_0437(): - return get_model(os.path.join(datadir, "J0437-4715.par")) + return get_model(datadir / "J0437-4715.par") @pytest.fixture -def timfile_jumps(): - os.chdir(datadir) - return get_TOAs("test1.tim") +def timfile_jumps(pickle_dir): + return get_TOAs(datadir / "test1.tim", picklefilename=pickle_dir) @pytest.fixture -def timfile_nojumps(): - return get_TOAs(os.path.join(datadir, "NGC6440E.tim")) +def timfile_nojumps(pickle_dir): + return get_TOAs(datadir / "NGC6440E.tim", picklefilename=pickle_dir) -len_timfile_nojumps = len(get_TOAs(os.path.join(datadir, "NGC6440E.tim"))) +# len_timfile_nojumps = len(get_TOAs(os.path.join(datadir, "NGC6440E.tim"))) class TestModelBuilding: From a614140a1aaf2c31b8c17b4c694c342f3b6c0520 Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Tue, 12 Jul 2022 20:46:36 +0100 Subject: [PATCH 5/9] Downgrade "EFAC has no TOAs to info" --- src/pint/models/noise_model.py | 8 ++++++-- tests/test_random_models.py | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/pint/models/noise_model.py b/src/pint/models/noise_model.py index 6caa0937c..e5b1f255e 100644 --- a/src/pint/models/noise_model.py +++ b/src/pint/models/noise_model.py @@ -160,14 +160,18 @@ def scale_toa_sigma(self, toas): if np.any(mask): sigma_scaled[mask] = np.hypot(sigma_scaled[mask], equad.quantity) else: - warnings.warn(f"EQUAD {equad} has no TOAs") + # Is this really a problem? Was a warning, downgrading to info + log.info(f"EQUAD {equad} has no TOAs") + pass for efac_name in self.EFACs: efac = getattr(self, efac_name) mask = efac.select_toa_mask(toas) if np.any(mask): sigma_scaled[mask] *= efac.quantity else: - warnings.warn(f"EFAC {efac} has no TOAs") + # Is this really a problem? Was a warning, downgrading to info + log.info(f"EFAC {efac} has no TOAs") + pass return sigma_scaled def sigma_scaled_cov_matrix(self, toas): diff --git a/tests/test_random_models.py b/tests/test_random_models.py index 2e1c84d08..9e90f02eb 100644 --- a/tests/test_random_models.py +++ b/tests/test_random_models.py @@ -1,18 +1,11 @@ -import os -from copy import deepcopy +import warnings -import astropy.units as u - -# import matplotlib -# matplotlib.use('TKAgg') -import matplotlib.pyplot as plt import numpy as np import pytest from pinttestdata import datadir import pint.fitter -import pint.models.parameter as param -from pint import ls, simulation, toa, utils +from pint import simulation from pint.models import get_model, get_model_and_toas from pint.toa import get_TOAs @@ -26,11 +19,15 @@ pint.fitter.DownhillGLSFitter, ], ) -def test_random_models(fitter): +def test_random_models(fitter, pickle_dir): # Get model and TOAs - m, t = get_model_and_toas( - os.path.join(datadir, "NGC6440E.par"), os.path.join(datadir, "NGC6440E.tim") - ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + m, t = get_model_and_toas( + datadir / "NGC6440E.par", + datadir / "NGC6440E.tim", + picklefilename=pickle_dir, + ) f = fitter(toas=t, model=m) # Do a 4-parameter fit @@ -59,12 +56,15 @@ def test_random_models(fitter): "fitter", [pint.fitter.WidebandTOAFitter, pint.fitter.WidebandDownhillFitter], ) -def test_random_models_wb(fitter): - model = get_model(os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.gls.par")) +def test_random_models_wb(fitter, pickle_dir): + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + model = get_model(datadir / "J1614-2230_NANOGrav_12yv3.wb.gls.par") toas = get_TOAs( - os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.tim"), + datadir / "J1614-2230_NANOGrav_12yv3.wb.tim", ephem="DE436", bipm_version="BIPM2015", + picklefilename=pickle_dir, ) f = fitter(toas, model) # Do a 4-parameter fit From 371cbf77867b9cc03021275bc1759bb7bcf90d4d Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Wed, 13 Jul 2022 15:27:12 +0100 Subject: [PATCH 6/9] Minor code cleanup arising from test cleanup --- src/pint/polycos.py | 25 ++----- tests/test_fake_toas.py | 4 +- tests/test_fitter.py | 55 +++++++++------ tests/test_grid.py | 125 +++++++++++++--------------------- tests/test_parameters.py | 93 ++++++++++++------------- tests/test_parfile_writing.py | 10 ++- tests/test_phase_commands.py | 23 +++---- tests/test_polycos.py | 52 +++++++++----- 8 files changed, 191 insertions(+), 196 deletions(-) diff --git a/src/pint/polycos.py b/src/pint/polycos.py index 0902baad1..4902eb4e5 100644 --- a/src/pint/polycos.py +++ b/src/pint/polycos.py @@ -502,46 +502,35 @@ def generate_polycos( method="TEMPO", numNodes=20, ): - """ - Generate the polyco data. + """Generate the polyco data. + + This function replaces anything in ``self`` with a freshly + generated polyco table. Parameters ---------- - model : TimingModel + model : pint.models.TimingModel TimingModel to generate the Polycos with parameters setup. - - mjdStart : float / numpy longdouble + mjdStart : float or np.longdouble Start time of polycos in mjd - - mjdEnd : float / numpy longdouble + mjdEnd : float or np.longdouble Ending time of polycos in mjd - obs : str Observatory code - segLength : int Length of polyco segement [minutes] - ncoeff : int Number of coefficents - obsFreq : float Observing frequency [MHz] - maxha : float optional. Default 12.0 Maximum hour angle. Only 12.0 is supported for now. - method : string optional ["TEMPO", "TEMPO2", ...] Default TEMPO Method to generate polycos. Only the TEMPO method is supported for now. - numNodes : int optional. Default 20 Number of nodes for fitting. It cannot be less then the number of coefficents. - - Return - --------- - A polyco table. """ mjdStart = data2longdouble(mjdStart) mjdEnd = data2longdouble(mjdEnd) diff --git a/tests/test_fake_toas.py b/tests/test_fake_toas.py index ef8970ce3..6a70d4d34 100644 --- a/tests/test_fake_toas.py +++ b/tests/test_fake_toas.py @@ -113,7 +113,7 @@ def test_noise_addition_EQUAD(): ) -def test_zima(): +def test_zima(pickle_dir): parfile = pint.config.examplefile("NGC6440E.par") outfile = tempfile.NamedTemporaryFile(suffix="tim") @@ -125,7 +125,7 @@ def test_zima(): os.system(zima_command) - t = get_TOAs(outfile.name) + t = get_TOAs(outfile.name, picklefilename=pickle_dir) r = pint.residuals.Residuals(t, m) # need a generous rtol because of the small statistics assert np.isclose(r.calc_time_resids().std(), 1 * u.us, rtol=0.5) diff --git a/tests/test_fitter.py b/tests/test_fitter.py index 0d44dab98..503b5f347 100644 --- a/tests/test_fitter.py +++ b/tests/test_fitter.py @@ -1,5 +1,6 @@ #! /usr/bin/env python import os +import warnings from copy import deepcopy from io import StringIO @@ -17,9 +18,10 @@ from pint.models import get_model, get_model_and_toas -@pytest.mark.xfail def test_fitter_basic(): - m = tm.get_model(os.path.join(datadir, "NGC6440E.par")) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + m = tm.get_model(datadir / "NGC6440E.par") m.fit_params = ["F0", "F1"] e = 1 * u.us t = simulation.make_fake_toas_uniform(56000, 59000, 16, m, error=e) @@ -30,26 +32,28 @@ def test_fitter_basic(): f_1 = fitter.WLSFitter(toas=t, model=m) f_1.fit_toas() - assert abs(f_1.model.F0 - m.F0) < dF0 + assert abs(f_1.model.F0.quantity - m.F0.quantity) < dF0 m_2 = deepcopy(m) m_2.F0.quantity += 2 * dF0 - assert abs(m_2.F0 - m.F0) > dF0 + assert abs(m_2.F0.quantity - m.F0.quantity) > dF0 f_2 = fitter.WLSFitter(toas=t, model=m_2) f_2.fit_toas() - assert abs(f_2.model.F0 - m.F0) < dF0 + assert abs(f_2.model.F0.quantity - m.F0.quantity) < dF0 @pytest.mark.skipif( "DISPLAY" not in os.environ, reason="Needs an X server, xvfb counts" ) -def test_fitter(): +def test_fitter(pickle_dir): # Get model - m = tm.get_model(os.path.join(datadir, "NGC6440E.par")) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + m = tm.get_model(datadir / "NGC6440E.par") # Get TOAs - t = toa.TOAs(os.path.join(datadir, "NGC6440E.tim")) + t = toa.get_TOAs(datadir / "NGC6440E.tim", picklefilename=pickle_dir) t.apply_clock_corrections(include_bipm=False) t.compute_TDBs() try: @@ -201,12 +205,14 @@ def test_ftest_wb(): assert isinstance(Ftest_dict["ft"], float) or isinstance(Ftest_dict["ft"], bool) -def test_fitsummary_binary(): +def test_fitsummary_binary(pickle_dir): """Test fitter print_summary() when an ELL1 binary is fit""" par = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.gls.par") tim = os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim") - m, t = get_model_and_toas(par, tim) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + m, t = get_model_and_toas(par, tim, picklefilename=pickle_dir) f = fitter.WLSFitter(t, m) f.model.free_params = ["PB", "A1", "SINI"] @@ -214,31 +220,42 @@ def test_fitsummary_binary(): f.print_summary() -def test_fitselector(): +def test_fitselector_wb(pickle_dir): # WB - par = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.gls.par") - tim = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.tim") + par = datadir / "B1855+09_NANOGrav_12yv3.wb.gls.par" + tim = datadir / "B1855+09_NANOGrav_12yv3.wb.tim" - m, t = get_model_and_toas(par, tim) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + m, t = get_model_and_toas(par, tim, picklefilename=pickle_dir) f = fitter.Fitter.auto(t, m, downhill=True) assert isinstance(f, fitter.WidebandDownhillFitter) f = fitter.Fitter.auto(t, m, downhill=False) assert isinstance(f, fitter.WidebandTOAFitter) + +def test_fitselector_gls(pickle_dir): # correlated errors m, t = get_model_and_toas( - os.path.join(datadir, "J0023+0923_NANOGrav_11yv0.gls.par"), - os.path.join(datadir, "J0023+0923_NANOGrav_11yv0.tim"), + datadir / "J0023+0923_NANOGrav_11yv0.gls.par", + datadir / "J0023+0923_NANOGrav_11yv0.tim", + picklefilename=pickle_dir, ) f = fitter.Fitter.auto(t, m, downhill=True) assert isinstance(f, fitter.DownhillGLSFitter) f = fitter.Fitter.auto(t, m, downhill=False) assert isinstance(f, fitter.GLSFitter) + +def test_fitselector_wls(pickle_dir): # uncorrelated errors - m, t = get_model_and_toas( - os.path.join(datadir, "NGC6440E.par"), os.path.join(datadir, "NGC6440E.tim") - ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + m, t = get_model_and_toas( + datadir / "NGC6440E.par", + datadir / "NGC6440E.tim", + picklefilename=pickle_dir, + ) f = fitter.Fitter.auto(t, m, downhill=True) assert isinstance(f, fitter.DownhillWLSFitter) f = fitter.Fitter.auto(t, m, downhill=False) diff --git a/tests/test_grid.py b/tests/test_grid.py index 5717f6ae7..daf0b0f3c 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -1,8 +1,6 @@ -"""Test chi^2 gridding routines""" +"""Test chi^2 gridding routines.""" import concurrent.futures -import io -import multiprocessing -import os +import warnings import astropy.units as u import numpy as np @@ -18,10 +16,17 @@ ncpu = 2 -def test_grid_singleprocessor(): +@pytest.fixture +def m_t(pickle_dir): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + return get_model_and_toas(parfile, timfile, picklefilename=pickle_dir) + + +def test_grid_singleprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -43,10 +48,8 @@ def test_grid_singleprocessor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_extraparams_singleprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_extraparams_singleprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -70,10 +73,8 @@ def test_grid_extraparams_singleprocessor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_multiprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_multiprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -95,10 +96,8 @@ def test_grid_multiprocessor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_oneparam(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_oneparam(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -115,10 +114,8 @@ def test_grid_oneparam(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_oneparam_extraparam(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_oneparam_extraparam(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -137,10 +134,8 @@ def test_grid_oneparam_extraparam(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_oneparam_existingexecutor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_oneparam_existingexecutor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -159,10 +154,8 @@ def test_grid_oneparam_existingexecutor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_3param_singleprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_3param_singleprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -188,10 +181,8 @@ def test_grid_3param_singleprocessor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_3param_multiprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_3param_multiprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -219,10 +210,8 @@ def test_grid_3param_multiprocessor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_derived_singleprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_derived_singleprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -244,10 +233,8 @@ def test_grid_derived_singleprocessor(): assert np.isclose(bestfit, chi2grid_tau.min(), atol=1) -def test_grid_derived_extraparam_singleprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_derived_extraparam_singleprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -270,10 +257,8 @@ def test_grid_derived_extraparam_singleprocessor(): assert np.isclose(bestfit, chi2grid_tau.min(), atol=1) -def test_grid_derived_multiprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_derived_multiprocessor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -291,10 +276,8 @@ def test_grid_derived_multiprocessor(): assert np.isclose(bestfit, chi2grid_tau.min(), atol=1) -def test_grid_derived_existingexecutor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_derived_existingexecutor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -318,10 +301,8 @@ def test_grid_derived_existingexecutor(): assert np.isclose(bestfit, chi2grid_tau.min(), atol=1) -def test_grid_derived_extraparam_existingexecutor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_derived_extraparam_existingexecutor(m_t): + m, t = m_t f = WLSFitter(t, m) f.fit_toas() @@ -346,10 +327,8 @@ def test_grid_derived_extraparam_existingexecutor(): assert np.isclose(bestfit, chi2grid_tau.min(), atol=1) -def test_grid_3param_prefix_singleprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_3param_prefix_singleprocessor(m_t): + m, t = m_t # add a F2 to the model modelcomponent = m.components["Spindown"] @@ -390,10 +369,8 @@ def test_grid_3param_prefix_singleprocessor(): assert np.isclose(bestfit, chi2grid.min()) -def test_grid_3param_prefix_multiprocessor(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_3param_prefix_multiprocessor(m_t): + m, t = m_t # add a F2 to the model modelcomponent = m.components["Spindown"] @@ -439,10 +416,8 @@ def test_grid_3param_prefix_multiprocessor(): @pytest.mark.parametrize( "fitter", [GLSFitter, WLSFitter, DownhillWLSFitter, DownhillGLSFitter] ) -def test_grid_fitters_singleprocessor(fitter): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_fitters_singleprocessor(fitter, m_t): + m, t = m_t f = fitter(t, m) f.fit_toas() bestfit = f.resids.chi2 @@ -461,10 +436,8 @@ def test_grid_fitters_singleprocessor(fitter): @pytest.mark.parametrize( "fitter", [GLSFitter, WLSFitter, DownhillWLSFitter, DownhillGLSFitter] ) -def test_grid_fitters_multiprocessor(fitter): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_grid_fitters_multiprocessor(fitter, m_t): + m, t = m_t f = fitter(t, m) f.fit_toas() bestfit = f.resids.chi2 @@ -480,10 +453,8 @@ def test_grid_fitters_multiprocessor(fitter): assert np.isclose(bestfit, chi2grid.min()) -def test_tuple_fit(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_tuple_fit(m_t): + m, t = m_t f = WLSFitter(t, m) # find the best-fit @@ -512,10 +483,8 @@ def test_tuple_fit(): assert np.isclose(extra["DM"][3], f.model.DM.quantity) -def test_derived_tuple_fit(): - parfile = pint.config.examplefile("NGC6440E.par") - timfile = pint.config.examplefile("NGC6440E.tim") - m, t = get_model_and_toas(parfile, timfile) +def test_derived_tuple_fit(m_t): + m, t = m_t f = WLSFitter(t, m) # find the best-fit diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 2c4b9a4a6..bed0aae41 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -3,6 +3,7 @@ import os import pickle import unittest +import warnings import astropy.time as time import astropy.units as u @@ -141,9 +142,10 @@ def test_units_consistent(): class TestParameters(unittest.TestCase): @classmethod def setUpClass(cls): - os.chdir(datadir) - cls.m = get_model("B1855+09_NANOGrav_dfg+12_modified.par") - cls.mp = get_model("prefixtest.par") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + cls.m = get_model(datadir / "B1855+09_NANOGrav_dfg+12_modified.par") + cls.mp = get_model(datadir / "prefixtest.par") def test_RAJ(self): """Check whether the value and units of RAJ parameter are ok""" @@ -391,46 +393,6 @@ def test_START_FINISH_in_par(self): fitter.model.FINISH.value, fitter.toas.last_MJD.value, places=9 ) - def test_START_FINISH_not_in_par(self): - """ - Check that START/FINISH parameters are added and set up when not - in input file. - """ - # check initialization after fitting for .par file without START/FINISH - m = get_model("NGC6440E.par") - t = get_TOAs("NGC6440E.tim") - - start_postval = 53478.2858714192 # from Tempo2 - finish_postval = 54187.5873241699 # from Tempo2 - - self.assertTrue(hasattr(m, "START")) - self.assertTrue(hasattr(m, "FINISH")) - - # fit toas and compare with expected/Tempo2 (for WLS) values - fitters = [ - pint.fitter.WLSFitter(toas=t, model=m), - pint.fitter.GLSFitter(toas=t, model=m), - ] - for fitter in fitters: - fitter.fit_toas() - self.assertTrue(hasattr(fitter.model, "START")) - self.assertTrue(hasattr(fitter.model, "FINISH")) - self.assertEqual(fitter.model.START.frozen, True) - self.assertEqual(fitter.model.FINISH.frozen, True) - if fitter.method == "weighted_least_square": - self.assertAlmostEqual( - fitter.model.START.value, start_postval, places=9 - ) - self.assertAlmostEqual( - fitter.model.FINISH.value, finish_postval, places=9 - ) - self.assertAlmostEqual( - fitter.model.START.value, fitter.toas.first_MJD.value, places=9 - ) - self.assertAlmostEqual( - fitter.model.FINISH.value, fitter.toas.last_MJD.value, places=9 - ) - def test_START_FINISH_notfrozen(self): """ check that when the START/FINISH parameters @@ -452,6 +414,40 @@ def test_START_FINISH_notfrozen(self): self.assertEqual(m.FINISH.frozen, True) +@pytest.mark.parametrize("fitter_type", [pint.fitter.WLSFitter, pint.fitter.GLSFitter]) +def test_START_FINISH_not_in_par(pickle_dir, fitter_type): + """ + Check that START/FINISH parameters are added and set up when not + in input file. + """ + # check initialization after fitting for .par file without START/FINISH + m = get_model("NGC6440E.par") + t = get_TOAs("NGC6440E.tim", picklefilename=pickle_dir) + + start_postval = 53478.2858714192 # from Tempo2 + finish_postval = 54187.5873241699 # from Tempo2 + + assert hasattr(m, "START") + assert hasattr(m, "FINISH") + + # fit toas and compare with expected/Tempo2 (for WLS) values + fitter = fitter_type(toas=t, model=m) + fitter.fit_toas() + assert hasattr(fitter.model, "START") + assert hasattr(fitter.model, "FINISH") + assert fitter.model.START.frozen + assert fitter.model.FINISH.frozen + if fitter.method == "weighted_least_square": + assert np.isclose(fitter.model.START.value, start_postval, atol=1e-9, rtol=0) + assert np.isclose(fitter.model.FINISH.value, finish_postval, atol=1e-9, rtol=0) + assert np.isclose( + fitter.model.START.value, fitter.toas.first_MJD.value, atol=1e-9, rtol=0 + ) + assert np.isclose( + fitter.model.FINISH.value, fitter.toas.last_MJD.value, atol=1e-9, rtol=0 + ) + + @pytest.mark.parametrize( "type_,str_value,value", [ @@ -589,22 +585,23 @@ def test_parameter_can_be_pickled(p): pickle.dumps(p) -def test_fitter_construction_success_after_remove_param(): +def test_fitter_construction_success_after_remove_param(pickle_dir): """Checks that add_param and remove_param don't require m.setup() to be run prior to constructing a fitter. This addresses issue #1260.""" m = get_model(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par")) - t = get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim")) + t = get_TOAs( + os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim"), picklefilename=pickle_dir + ) FD4 = prefixParameter(parameter_type="float", name="FD4", value=0.0, units=u.s) """Fitter construction used to fail after remove_param without m.setup(). Test this (for both adding and removing, just to be safe):""" m.add_param_from_top(FD4, "FD") - f = pint.fitter.GLSFitter(toas=t, model=m) + pint.fitter.GLSFitter(toas=t, model=m) m.remove_param("FD4") - f = pint.fitter.GLSFitter(toas=t, model=m) + pint.fitter.GLSFitter(toas=t, model=m) def test_correct_number_of_params_and_FD_terms_after_add_or_remove_param(): """Checks that the number of parameters in some component after add_param or remove_param is correct. Similarly checks that len(m.get_prefix_mapping('FD')) (i.e. num_FD_terms) is correct after seeing a bug where the length didn't change after FD param removal (see #1260).""" m = get_model(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par")) - t = get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim")) FD4 = prefixParameter(parameter_type="float", name="FD4", value=0.0, units=u.s) m.add_param_from_top(FD4, "FD") assert len(m.components["FD"].params) == 4 diff --git a/tests/test_parfile_writing.py b/tests/test_parfile_writing.py index 355fed37b..3b05df4f7 100644 --- a/tests/test_parfile_writing.py +++ b/tests/test_parfile_writing.py @@ -14,10 +14,16 @@ from pint.residuals import Residuals -def test_parfile_write(tmp_path): +def test_parfile_write(tmp_path, pickle_dir): parfileB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par") timB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim") - toasB1855 = toa.get_TOAs(timB1855, ephem="DE421", planets=False, include_bipm=False) + toasB1855 = toa.get_TOAs( + timB1855, + ephem="DE421", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) modelB1855 = mb.get_model(parfileB1855) out_parfile = tmp_path / "test_parfile_write.par" # change parameter value diff --git a/tests/test_phase_commands.py b/tests/test_phase_commands.py index 44a130e2a..51d864b86 100644 --- a/tests/test_phase_commands.py +++ b/tests/test_phase_commands.py @@ -14,15 +14,14 @@ timfile = os.path.join(datadir, "NGC6440E_PHASETEST.tim") -class TestAbsPhase(unittest.TestCase): - def test_phase_commands(self): - model = pint.models.get_model(parfile) - toas = pint.toa.get_TOAs(timfile) - # This TOA has PHASE -0.3. Check that - assert np.isclose(float(toas.table[32]["flags"]["phase"]), -0.3) - # This TOA should have PHASE 0.2 and -padd -0.2 - assert np.isclose(float(toas.table["flags"][9]["padd"]), -0.2) - assert np.isclose(float(toas.table["flags"][9]["phase"]), 0.2) - # The end result should be these residuals if the commands are respected - res = Residuals(toas=toas, model=model) - assert (res.rms_weighted() - 1602.0293 * u.us) < 0.1 * u.us +def test_phase_commands(pickle_dir): + model = pint.models.get_model(parfile) + toas = pint.toa.get_TOAs(timfile, picklefile=pickle_dir) + # This TOA has PHASE -0.3. Check that + assert np.isclose(float(toas.table[32]["flags"]["phase"]), -0.3) + # This TOA should have PHASE 0.2 and -padd -0.2 + assert np.isclose(float(toas.table["flags"][9]["padd"]), -0.2) + assert np.isclose(float(toas.table["flags"][9]["phase"]), 0.2) + # The end result should be these residuals if the commands are respected + res = Residuals(toas=toas, model=model) + assert (res.rms_weighted() - 1602.0293 * u.us) < 0.1 * u.us diff --git a/tests/test_polycos.py b/tests/test_polycos.py index 86d146f35..5e5eb2c2f 100644 --- a/tests/test_polycos.py +++ b/tests/test_polycos.py @@ -1,9 +1,13 @@ """Test polycos.""" +import tempfile +import warnings from pathlib import Path import numpy as np import pytest +from hypothesis import given, settings +from hypothesis.strategies import sampled_from from pinttestdata import datadir import pint.toa as toa @@ -13,12 +17,12 @@ @pytest.fixture def polyco_file(): - return Path(datadir) / "B1855_polyco.dat" + return datadir / "B1855_polyco.dat" @pytest.fixture def par_file(): - return Path(datadir) / "B1855+09_polycos.par" + return datadir / "B1855+09_polycos.par" def test_polycos_basic(polyco_file): @@ -52,7 +56,7 @@ def test_find_entry(polyco_file): for t in [54999.8, 55000.8, 55001.8, np.linspace(54999, 55002, 1000)]: with pytest.raises(ValueError): - _ = p.find_entry(t) + p.find_entry(t) def test_read_write_round_trip(tmpdir, polyco_file): @@ -71,26 +75,40 @@ def test_read_write_round_trip(tmpdir, polyco_file): def test_generate_polycos_maxha_error(par_file): - model = get_model(str(par_file)) + model = get_model(par_file) p = Polycos() with pytest.raises(ValueError): p.generate_polycos(model, 55000, 55002, "ao", 144, 12, 400.0, maxha=8) -@pytest.mark.parametrize("obs", ["ao", "gbt", "@", "coe"]) -@pytest.mark.parametrize("obsfreq", [1400.0, 400.0]) -@pytest.mark.parametrize("nspan, ncoeff", [(144, 12), (72, 9)]) -def test_generate_polycos(tmpdir, par_file, obs, obsfreq, nspan, ncoeff): - output_polyco = tmpdir / "B1855_polyco_round_trip_from_par.dat" +# FIXME: try different par files? +@settings(max_examples=2) +@given( + sampled_from(["NGC6440E.par", "B1855+09_polycos.par"]), + sampled_from(["ao", "gbt", "@", "coe"]), + sampled_from([1400.0, 400.0, 99999.0]), + sampled_from([(144, 12), (72, 9)]), +) +def test_generate_polycos_agrees_with_model_phase( + par, + obs, + obsfreq, + nspan_ncoeff, +): + nspan, ncoeff = nspan_ncoeff mjd_start, mjd_end = 55000.0, 55001.0 - - model = get_model(str(par_file)) - - p = Polycos() - p.generate_polycos(model, mjd_start, mjd_end, obs, nspan, ncoeff, obsfreq) - p.write_polyco_file(output_polyco) - q = Polycos() - q.read_polyco_file(output_polyco) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + model = get_model(datadir / par) + with tempfile.TemporaryDirectory() as tmpdir_str: + tmpdir = Path(tmpdir_str) + output_polyco = tmpdir / "B1855_polyco_round_trip_from_par.dat" + + p = Polycos() + p.generate_polycos(model, mjd_start, mjd_end, obs, nspan, ncoeff, obsfreq) + p.write_polyco_file(output_polyco) + q = Polycos() + q.read_polyco_file(output_polyco) mjds = np.linspace(mjd_start, mjd_end, 51) From 0695a4ab725be0e2186ef526ba92a5a1ede42d2f Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Wed, 13 Jul 2022 18:57:58 +0100 Subject: [PATCH 7/9] Test cleanup --- src/pint/models/noise_model.py | 8 +- tests/test_B1855.py | 122 ++++++------ tests/test_B1855_9yrs.py | 114 ++++++----- tests/test_B1953.py | 136 ++++++------- tests/test_J0613.py | 149 +++++++------- tests/test_all_component_and_model_builder.py | 3 - tests/test_barytoa.py | 4 +- tests/test_modelutils.py | 181 ++++++++++-------- tests/test_parfile_writing.py | 5 +- tests/test_phase_commands.py | 2 +- tests/test_utils.py | 8 +- 11 files changed, 383 insertions(+), 349 deletions(-) diff --git a/src/pint/models/noise_model.py b/src/pint/models/noise_model.py index e5b1f255e..25add06e5 100644 --- a/src/pint/models/noise_model.py +++ b/src/pint/models/noise_model.py @@ -160,8 +160,8 @@ def scale_toa_sigma(self, toas): if np.any(mask): sigma_scaled[mask] = np.hypot(sigma_scaled[mask], equad.quantity) else: - # Is this really a problem? Was a warning, downgrading to info - log.info(f"EQUAD {equad} has no TOAs") + # Is this really a problem? + warnings.warn(f"EQUAD {equad} has no TOAs") pass for efac_name in self.EFACs: efac = getattr(self, efac_name) @@ -169,8 +169,8 @@ def scale_toa_sigma(self, toas): if np.any(mask): sigma_scaled[mask] *= efac.quantity else: - # Is this really a problem? Was a warning, downgrading to info - log.info(f"EFAC {efac} has no TOAs") + # Is this really a problem? + warnings.warn(f"EFAC {efac} has no TOAs") pass return sigma_scaled diff --git a/tests/test_B1855.py b/tests/test_B1855.py index 16ba8626c..dc0adf963 100644 --- a/tests/test_B1855.py +++ b/tests/test_B1855.py @@ -1,7 +1,6 @@ """Various tests to assess the performance of the B1855+09.""" import logging -import os -import unittest +import warnings import astropy.units as u import numpy as np @@ -14,66 +13,69 @@ from pint.residuals import Residuals -class TestB1855(unittest.TestCase): - """Compare delays from the dd model with tempo and PINT""" +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.parfileB1855 = "B1855+09_NANOGrav_dfg+12_TAI_FB90.par" - cls.timB1855 = "B1855+09_NANOGrav_dfg+12.tim" - try: - cls.toasB1855 = toa.get_TOAs( - cls.timB1855, ephem="DE405", planets=False, include_bipm=False - ) - except IOError: - pytest.skip("Unable to fetch ephemeris") - cls.modelB1855 = mb.get_model(cls.parfileB1855) - logging.debug("%s" % cls.modelB1855.components) - logging.debug("%s" % cls.modelB1855.params) - # tempo result - cls.ltres = np.genfromtxt( - cls.parfileB1855 + ".tempo2_test", skip_header=1, unpack=True - ) + s = Setup() + s.parfileB1855 = datadir / "B1855+09_NANOGrav_dfg+12_TAI_FB90.par" + s.timB1855 = datadir / "B1855+09_NANOGrav_dfg+12.tim" + s.toasB1855 = toa.get_TOAs( + s.timB1855, + ephem="DE405", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + warnings.filterwarnings("ignore", message=r".*EPHVER.*") + s.modelB1855 = mb.get_model(s.parfileB1855) + logging.debug("%s" % s.modelB1855.components) + logging.debug("%s" % s.modelB1855.params) + # tempo result + s.ltres = np.genfromtxt( + str(s.parfileB1855) + ".tempo2_test", skip_header=1, unpack=True + ) + return s - def test_B1855(self): - pint_resids_us = Residuals( - self.toasB1855, self.modelB1855, use_weighted_mean=False - ).time_resids.to(u.s) - # Due to the gps2utc clock correction. We are at 3e-8 seconds level. - assert np.all( - np.abs(pint_resids_us.value - self.ltres) < 3e-8 - ), "B1855 residuals test failed." - def test_derivative(self): - log = logging.getLogger("TestB1855.derivative_test") - testp = tdu.get_derivative_params(self.modelB1855) - delay = self.modelB1855.delay(self.toasB1855) - for p in testp.keys(): - log.debug("Runing derivative for %s", "d_delay_d_" + p) - ndf = self.modelB1855.d_phase_d_param_num(self.toasB1855, p, testp[p]) - adf = self.modelB1855.d_phase_d_param(self.toasB1855, delay, p) - diff = adf - ndf - if not np.all(diff.value) == 0.0: - mean_der = (adf + ndf) / 2.0 - relative_diff = np.abs(diff) / np.abs(mean_der) - # print "Diff Max is :", np.abs(diff).max() - msg = ( - "Derivative test failed at d_delay_d_%s with max relative difference %lf" - % (p, np.nanmax(relative_diff).value) - ) - if p in ["SINI"]: - tol = 0.7 - else: - tol = 1e-3 - log.debug( - "derivative relative diff for %s, %lf" - % ("d_delay_d_" + p, np.nanmax(relative_diff).value) - ) - assert np.nanmax(relative_diff) < tol, msg - else: - continue +def test_B1855(setup): + pint_resids_us = Residuals( + setup.toasB1855, setup.modelB1855, use_weighted_mean=False + ).time_resids.to(u.s) + # Due to the gps2utc clock correction. We are at 3e-8 seconds level. + assert np.all( + np.abs(pint_resids_us.value - setup.ltres) < 3e-8 + ), "B1855 residuals test failed." -if __name__ == "__main__": - pass +def test_derivative(setup): + log = logging.getLogger("TestB1855.derivative_test") + testp = tdu.get_derivative_params(setup.modelB1855) + delay = setup.modelB1855.delay(setup.toasB1855) + for p in testp.keys(): + log.debug("Runing derivative for %s", "d_delay_d_" + p) + ndf = setup.modelB1855.d_phase_d_param_num(setup.toasB1855, p, testp[p]) + adf = setup.modelB1855.d_phase_d_param(setup.toasB1855, delay, p) + diff = adf - ndf + if not np.all(diff.value) == 0.0: + mean_der = (adf + ndf) / 2.0 + relative_diff = np.abs(diff) / np.abs(mean_der) + # print "Diff Max is :", np.abs(diff).max() + msg = ( + "Derivative test failed at d_delay_d_%s with max relative difference %lf" + % (p, np.nanmax(relative_diff).value) + ) + if p in ["SINI"]: + tol = 0.7 + else: + tol = 1e-3 + log.debug( + "derivative relative diff for %s, %lf" + % ("d_delay_d_" + p, np.nanmax(relative_diff).value) + ) + assert np.nanmax(relative_diff) < tol, msg + else: + continue diff --git a/tests/test_B1855_9yrs.py b/tests/test_B1855_9yrs.py index beee1a4e5..712cec619 100644 --- a/tests/test_B1855_9yrs.py +++ b/tests/test_B1855_9yrs.py @@ -1,10 +1,10 @@ """Various tests to assess the performance of the B1855+09.""" import logging -import os -import unittest +import warnings import astropy.units as u import numpy as np +import pytest import test_derivative_utils as tdu from pinttestdata import datadir @@ -13,57 +13,67 @@ from pint.residuals import Residuals -class TestB1855(unittest.TestCase): - """Compare delays from the dd model with tempo and PINT""" +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.parfileB1855 = "B1855+09_NANOGrav_9yv1.gls.par" - cls.timB1855 = "B1855+09_NANOGrav_9yv1.tim" - cls.toasB1855 = toa.get_TOAs( - cls.timB1855, ephem="DE421", planets=False, include_bipm=False - ) - cls.modelB1855 = mb.get_model(cls.parfileB1855) - # tempo result - cls.ltres = np.genfromtxt( - cls.parfileB1855 + ".tempo2_test", skip_header=1, unpack=True - ) + s = Setup() + s.parfileB1855 = datadir / "B1855+09_NANOGrav_9yv1.gls.par" + s.timB1855 = datadir / "B1855+09_NANOGrav_9yv1.tim" + s.toasB1855 = toa.get_TOAs( + s.timB1855, + ephem="DE421", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + warnings.filterwarnings("ignore", message=r".*EPHVER.*") + s.modelB1855 = mb.get_model(s.parfileB1855) + # tempo result + s.ltres = np.genfromtxt( + str(s.parfileB1855) + ".tempo2_test", skip_header=1, unpack=True + ) + return s - def test_B1855(self): - pint_resids_us = Residuals( - self.toasB1855, self.modelB1855, use_weighted_mean=False - ).time_resids.to(u.s) - # Due to the gps2utc clock correction. We are at 3e-8 seconds level. - assert np.all( - np.abs(pint_resids_us.value - self.ltres) < 3e-8 - ), "B1855 residuals test failed." - def test_derivative(self): - log = logging.getLogger("TestB1855.derivative_test") - testp = tdu.get_derivative_params(self.modelB1855) - delay = self.modelB1855.delay(self.toasB1855) - for p in testp.keys(): - log.debug("Runing derivative for %s", "d_delay_d_" + p) - ndf = self.modelB1855.d_phase_d_param_num(self.toasB1855, p, testp[p]) - adf = self.modelB1855.d_phase_d_param(self.toasB1855, delay, p) - diff = adf - ndf - if not np.all(diff.value) == 0.0: - mean_der = (adf + ndf) / 2.0 - relative_diff = np.abs(diff) / np.abs(mean_der) - # print "Diff Max is :", np.abs(diff).max() - msg = ( - "Derivative test failed at d_delay_d_%s with max relative difference %lf" - % (p, np.nanmax(relative_diff).value) - ) - if p in ["SINI"]: - tol = 0.7 - else: - tol = 1e-3 - log.debug( - "derivative relative diff for %s, %lf" - % ("d_delay_d_" + p, np.nanmax(relative_diff).value) - ) - assert np.nanmax(relative_diff) < tol, msg +def test_B1855(setup): + pint_resids_us = Residuals( + setup.toasB1855, setup.modelB1855, use_weighted_mean=False + ).time_resids.to(u.s) + # Due to the gps2utc clock correction. We are at 3e-8 seconds level. + assert np.all( + np.abs(pint_resids_us.value - setup.ltres) < 3e-8 + ), "B1855 residuals test failed." + + +def test_derivative(setup): + log = logging.getLogger("TestB1855.derivative_test") + testp = tdu.get_derivative_params(setup.modelB1855) + delay = setup.modelB1855.delay(setup.toasB1855) + for p in testp.keys(): + log.debug("Runing derivative for %s", "d_delay_d_" + p) + ndf = setup.modelB1855.d_phase_d_param_num(setup.toasB1855, p, testp[p]) + adf = setup.modelB1855.d_phase_d_param(setup.toasB1855, delay, p) + diff = adf - ndf + if not np.all(diff.value) == 0.0: + mean_der = (adf + ndf) / 2.0 + relative_diff = np.abs(diff) / np.abs(mean_der) + # print "Diff Max is :", np.abs(diff).max() + msg = ( + "Derivative test failed at d_delay_d_%s with max relative difference %lf" + % (p, np.nanmax(relative_diff).value) + ) + if p in ["SINI"]: + tol = 0.7 else: - continue + tol = 1e-3 + log.debug( + "derivative relative diff for %s, %lf" + % ("d_delay_d_" + p, np.nanmax(relative_diff).value) + ) + assert np.nanmax(relative_diff) < tol, msg + else: + continue diff --git a/tests/test_B1953.py b/tests/test_B1953.py index f8287db49..18bd6aeda 100644 --- a/tests/test_B1953.py +++ b/tests/test_B1953.py @@ -1,9 +1,9 @@ """Various tests to assess the performance of the B1953+29.""" -import os -import unittest +import warnings import astropy.units as u import numpy as np +import pytest import test_derivative_utils as tdu from astropy import log from pinttestdata import datadir @@ -13,71 +13,79 @@ from pint.residuals import Residuals -class TestB1953(unittest.TestCase): - """Compare delays from the dd model with tempo and PINT""" +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.parfileB1953 = "B1953+29_NANOGrav_dfg+12_TAI_FB90.par" - cls.timB1953 = "B1953+29_NANOGrav_dfg+12.tim" - cls.toasB1953 = toa.get_TOAs( - cls.timB1953, ephem="DE405", planets=False, include_bipm=False - ) - cls.modelB1953 = mb.get_model(cls.parfileB1953) - # tempo result - cls.ltres, cls.ltbindelay = np.genfromtxt( - cls.parfileB1953 + ".tempo2_test", skip_header=1, unpack=True - ) - print(cls.ltres) + s = Setup() - def test_B1953_binary_delay(self): - # Calculate delays with PINT - pint_binary_delay = self.modelB1953.binarymodel_delay(self.toasB1953, None) - assert np.all( - np.abs(pint_binary_delay.value + self.ltbindelay) < 1e-8 - ), "B1953 binary delay test failed." + s.parfileB1953 = datadir / "B1953+29_NANOGrav_dfg+12_TAI_FB90.par" + s.timB1953 = datadir / "B1953+29_NANOGrav_dfg+12.tim" + s.toasB1953 = toa.get_TOAs( + s.timB1953, + ephem="DE405", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + warnings.filterwarnings("ignore", message=r".*EPHVER.*") + s.modelB1953 = mb.get_model(s.parfileB1953) + # tempo result + s.ltres, s.ltbindelay = np.genfromtxt( + str(s.parfileB1953) + ".tempo2_test", skip_header=1, unpack=True + ) + print(s.ltres) + return s - def test_B1953(self): - pint_resids_us = Residuals( - self.toasB1953, self.modelB1953, use_weighted_mean=False - ).time_resids.to(u.s) - # Due to the gps2utc clock correction. We are at 3e-8 seconds level. - assert np.all( - np.abs(pint_resids_us.value - self.ltres) < 3e-8 - ), "B1953 residuals test failed." - def test_derivative(self): - log.setLevel("DEBUG") - testp = tdu.get_derivative_params(self.modelB1953) - delay = self.modelB1953.delay(self.toasB1953) - for p in testp.keys(): - log.debug("Runing derivative for %s".format("d_delay_d_" + p)) - ndf = self.modelB1953.d_phase_d_param_num(self.toasB1953, p, testp[p]) - adf = self.modelB1953.d_phase_d_param(self.toasB1953, delay, p) - diff = adf - ndf - if not np.all(diff.value) == 0.0: - mean_der = (adf + ndf) / 2.0 - relative_diff = np.abs(diff) / np.abs(mean_der) - # print "Diff Max is :", np.abs(diff).max() - msg = ( - "Derivative test failed at d_delay_d_%s with max relative difference %lf" - % (p, np.nanmax(relative_diff).value) - ) - if p in ["ECC", "EDOT"]: - tol = 20 - elif p in ["PMDEC"]: - tol = 5e-3 - else: - tol = 1e-3 - log.debug( - "derivative relative diff for %s, %lf" - % ("d_delay_d_" + p, np.nanmax(relative_diff).value) - ) - assert np.nanmax(relative_diff) < tol, msg - else: - continue +def test_B1953_binary_delay(setup): + # Calculate delays with PINT + pint_binary_delay = setup.modelB1953.binarymodel_delay(setup.toasB1953, None) + assert np.all( + np.abs(pint_binary_delay.value + setup.ltbindelay) < 1e-8 + ), "B1953 binary delay test failed." + +def test_B1953(setup): + pint_resids_us = Residuals( + setup.toasB1953, setup.modelB1953, use_weighted_mean=False + ).time_resids.to(u.s) + # Due to the gps2utc clock correction. We are at 3e-8 seconds level. + assert np.all( + np.abs(pint_resids_us.value - setup.ltres) < 3e-8 + ), "B1953 residuals test failed." -if __name__ == "__main__": - pass + +def test_derivative(setup): + log.setLevel("DEBUG") + testp = tdu.get_derivative_params(setup.modelB1953) + delay = setup.modelB1953.delay(setup.toasB1953) + for p in testp.keys(): + log.debug("Runing derivative for {}".format("d_delay_d_" + p)) + ndf = setup.modelB1953.d_phase_d_param_num(setup.toasB1953, p, testp[p]) + adf = setup.modelB1953.d_phase_d_param(setup.toasB1953, delay, p) + diff = adf - ndf + if not np.all(diff.value) == 0.0: + mean_der = (adf + ndf) / 2.0 + relative_diff = np.abs(diff) / np.abs(mean_der) + # print "Diff Max is :", np.abs(diff).max() + msg = ( + "Derivative test failed at d_delay_d_%s with max relative difference %lf" + % (p, np.nanmax(relative_diff).value) + ) + if p in ["ECC", "EDOT"]: + tol = 20 + elif p in ["PMDEC"]: + tol = 5e-3 + else: + tol = 1e-3 + log.debug( + "derivative relative diff for %s, %lf" + % ("d_delay_d_" + p, np.nanmax(relative_diff).value) + ) + assert np.nanmax(relative_diff) < tol, msg + else: + continue diff --git a/tests/test_J0613.py b/tests/test_J0613.py index 89ccab5ee..a526ebbc6 100644 --- a/tests/test_J0613.py +++ b/tests/test_J0613.py @@ -1,10 +1,10 @@ """Various tests to assess the performance of the J0623-0200.""" import logging -import os -import unittest +import warnings import astropy.units as u import numpy as np +import pytest import test_derivative_utils as tdu from pinttestdata import datadir @@ -13,78 +13,85 @@ from pint.residuals import Residuals -class TestJ0613(unittest.TestCase): - """Compare delays from the ELL1 model with tempo and PINT""" +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.parfileJ0613 = "J0613-0200_NANOGrav_dfg+12_TAI_FB90.par" - cls.timJ0613 = "J0613-0200_NANOGrav_dfg+12.tim" - cls.toasJ0613 = toa.get_TOAs( - cls.timJ0613, ephem="DE405", planets=False, include_bipm=False - ) - cls.modelJ0613 = mb.get_model(cls.parfileJ0613) - # tempo result - cls.ltres, cls.ltbindelay = np.genfromtxt( - cls.parfileJ0613 + ".tempo2_test", skip_header=1, unpack=True - ) - print(cls.ltres) + s = Setup() + s.parfileJ0613 = datadir / "J0613-0200_NANOGrav_dfg+12_TAI_FB90.par" + s.timJ0613 = datadir / "J0613-0200_NANOGrav_dfg+12.tim" + s.toasJ0613 = toa.get_TOAs( + s.timJ0613, + ephem="DE405", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + warnings.filterwarnings("ignore", message=r".*EPHVER.*") + s.modelJ0613 = mb.get_model(s.parfileJ0613) + # tempo result + s.ltres, s.ltbindelay = np.genfromtxt( + str(s.parfileJ0613) + ".tempo2_test", skip_header=1, unpack=True + ) + print(s.ltres) + return s - def test_J0613_binary_delay(self): - # Calculate delays with PINT - pint_binary_delay = self.modelJ0613.binarymodel_delay(self.toasJ0613, None) - assert np.all( - np.abs(pint_binary_delay.value + self.ltbindelay) < 1e-8 - ), "J0613 binary delay test failed." - def test_J0613(self): - pint_resids_us = Residuals( - self.toasJ0613, self.modelJ0613, use_weighted_mean=False - ).time_resids.to(u.s) - # Due to the gps2utc clock correction. We are at 3e-8 seconds level. - assert np.all( - np.abs(pint_resids_us.value - self.ltres) < 3e-8 - ), "J0613 residuals test failed." +def test_J0613_binary_delay(setup): + # Calculate delays with PINT + pint_binary_delay = setup.modelJ0613.binarymodel_delay(setup.toasJ0613, None) + assert np.all( + np.abs(pint_binary_delay.value + setup.ltbindelay) < 1e-8 + ), "J0613 binary delay test failed." - def test_derivative(self): - log = logging.getLogger("TestJ0613.derivative_test") - self.modelJ0613.PBDOT.value = 0.0 # For test PBDOT - self.modelJ0613.EPS1DOT.value = 0.0 - self.modelJ0613.EPS2DOT.value = 0.0 - self.modelJ0613.A1DOT.value = 0.0 - testp = tdu.get_derivative_params(self.modelJ0613) - delay = self.modelJ0613.delay(self.toasJ0613) - # Change parameter test step - testp["EPS1"] = 1 - testp["EPS2"] = 1 - testp["PMDEC"] = 1 - testp["PMRA"] = 1 - for p in testp.keys(): - log.debug("Runing derivative for %s", "d_delay_d_" + p) - ndf = self.modelJ0613.d_phase_d_param_num(self.toasJ0613, p, testp[p]) - adf = self.modelJ0613.d_phase_d_param(self.toasJ0613, delay, p) - diff = adf - ndf - if not np.all(diff.value) == 0.0: - mean_der = (adf + ndf) / 2.0 - relative_diff = np.abs(diff) / np.abs(mean_der) - # print "Diff Max is :", np.abs(diff).max() - msg = ( - "Derivative test failed at d_delay_d_%s with max relative difference %lf" - % (p, np.nanmax(relative_diff).value) - ) - if p in ["EPS1DOT", "EPS1"]: - tol = 0.05 - else: - tol = 1e-3 - log.debug( - "derivative relative diff for %s, %lf" - % ("d_delay_d_" + p, np.nanmax(relative_diff).value) - ) - assert np.nanmax(relative_diff) < tol, msg - else: - continue + +def test_J0613(setup): + pint_resids_us = Residuals( + setup.toasJ0613, setup.modelJ0613, use_weighted_mean=False + ).time_resids.to(u.s) + # Due to the gps2utc clock correction. We are at 3e-8 seconds level. + assert np.all( + np.abs(pint_resids_us.value - setup.ltres) < 3e-8 + ), "J0613 residuals test failed." -if __name__ == "__main__": - pass +def test_derivative(setup): + log = logging.getLogger("TestJ0613.derivative_test") + setup.modelJ0613.PBDOT.value = 0.0 # For test PBDOT + setup.modelJ0613.EPS1DOT.value = 0.0 + setup.modelJ0613.EPS2DOT.value = 0.0 + setup.modelJ0613.A1DOT.value = 0.0 + testp = tdu.get_derivative_params(setup.modelJ0613) + delay = setup.modelJ0613.delay(setup.toasJ0613) + # Change parameter test step + testp["EPS1"] = 1 + testp["EPS2"] = 1 + testp["PMDEC"] = 1 + testp["PMRA"] = 1 + for p in testp.keys(): + log.debug("Runing derivative for %s", "d_delay_d_" + p) + ndf = setup.modelJ0613.d_phase_d_param_num(setup.toasJ0613, p, testp[p]) + adf = setup.modelJ0613.d_phase_d_param(setup.toasJ0613, delay, p) + diff = adf - ndf + if not np.all(diff.value) == 0.0: + mean_der = (adf + ndf) / 2.0 + relative_diff = np.abs(diff) / np.abs(mean_der) + # print "Diff Max is :", np.abs(diff).max() + msg = ( + "Derivative test failed at d_delay_d_%s with max relative difference %lf" + % (p, np.nanmax(relative_diff).value) + ) + if p in ["EPS1DOT", "EPS1"]: + tol = 0.05 + else: + tol = 1e-3 + log.debug( + "derivative relative diff for %s, %lf" + % ("d_delay_d_" + p, np.nanmax(relative_diff).value) + ) + assert np.nanmax(relative_diff) < tol, msg + else: + continue diff --git a/tests/test_all_component_and_model_builder.py b/tests/test_all_component_and_model_builder.py index bf021a07c..dc0b2e043 100644 --- a/tests/test_all_component_and_model_builder.py +++ b/tests/test_all_component_and_model_builder.py @@ -2,11 +2,9 @@ import copy import io -from collections import defaultdict from glob import glob from os.path import basename, join -import astropy.units as u import numpy as np import pytest from pinttestdata import datadir @@ -19,7 +17,6 @@ Component, PhaseComponent, TimingModel, - UnknownBinaryModel, ) from pint.utils import PrefixError, split_prefixed_name diff --git a/tests/test_barytoa.py b/tests/test_barytoa.py index 527fe3199..f007d7fd5 100644 --- a/tests/test_barytoa.py +++ b/tests/test_barytoa.py @@ -11,13 +11,13 @@ from pint.models.model_builder import get_model -def test_barytoa(): +def test_barytoa(pickle_dir): os.chdir(datadir) # This par file has a very simple model in it m = get_model("slug.par") # This .tim file has TOAs at the barycenter, and at infinite frequency - t = pint.toa.get_TOAs("slug.tim") + t = pint.toa.get_TOAs("slug.tim", picklefilename=pickle_dir) rs = pint.residuals.Residuals(t, m).time_resids diff --git a/tests/test_modelutils.py b/tests/test_modelutils.py index 4be28d0f8..9db598cbb 100644 --- a/tests/test_modelutils.py +++ b/tests/test_modelutils.py @@ -1,9 +1,10 @@ import logging import os -import unittest +import warnings import astropy.units as u import numpy as np +import pytest from pinttestdata import datadir import pint.toa as toa @@ -12,87 +13,97 @@ from pint.residuals import Residuals -class TestEcliptic(unittest.TestCase): - """Test conversion from equatorial <-> ecliptic coordinates, and compare residuals.""" - - @classmethod - def setUpClass(cls): - # J0613 is in equatorial - cls.parfileJ0613 = os.path.join( - datadir, "J0613-0200_NANOGrav_dfg+12_TAI_FB90.par" - ) - cls.timJ0613 = os.path.join(datadir, "J0613-0200_NANOGrav_dfg+12.tim") - cls.toasJ0613 = toa.get_TOAs( - cls.timJ0613, ephem="DE405", planets=False, include_bipm=False - ) - cls.modelJ0613 = get_model(cls.parfileJ0613) - - # B1855+09 is in ecliptic - cls.parfileB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par") - cls.timB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim") - cls.toasB1855 = toa.get_TOAs( - cls.timB1855, ephem="DE421", planets=False, include_bipm=False - ) - cls.modelB1855 = get_model(cls.parfileB1855) - - cls.log = logging.getLogger("TestEcliptic") - - def test_to_ecliptic(self): - # determine residuals with base (equatorial) model - pint_resids = Residuals( - self.toasJ0613, self.modelJ0613, use_weighted_mean=False - ).time_resids.to(u.s) - - # convert model to ecliptic coordinates - ECLmodelJ0613 = model_equatorial_to_ecliptic(self.modelJ0613) - assert ECLmodelJ0613 is not None, "Creation of ecliptic model failed" - assert ( - "AstrometryEcliptic" in ECLmodelJ0613.components - ), "Creation of ecliptic model failed" - assert not ( - "AstrometryEquatorial" in ECLmodelJ0613.components - ), "Equatorial model still present" - self.log.debug("Ecliptic model created") - - # determine residuals with new (ecliptic) model - ECLpint_resids = Residuals( - self.toasJ0613, ECLmodelJ0613, use_weighted_mean=False - ).time_resids.to(u.s) - self.log.debug(np.abs(pint_resids - ECLpint_resids)) - msg = ( - "Residual comparison to ecliptic model failed with max relative difference %e s" - % np.nanmax(np.abs(pint_resids - ECLpint_resids)).value - ) - assert np.all(np.abs(pint_resids - ECLpint_resids) < 1e-10 * u.s), msg - - def test_to_equatorial(self): - # determine residuals with base (ecliptic) model - pint_resids = Residuals( - self.toasB1855, self.modelB1855, use_weighted_mean=False - ).time_resids.to(u.s) - - # convert model to ecliptic coordinates - EQUmodelB1855 = model_ecliptic_to_equatorial(self.modelB1855) - assert EQUmodelB1855 is not None, "Creation of equatorial model failed" - assert ( - "AstrometryEquatorial" in EQUmodelB1855.components - ), "Creation of equatorial model failed" - assert not ( - "AstrometryEcliptic" in EQUmodelB1855.components - ), "Ecliptic model still present" - self.log.debug("Equatorial model created") - - # determine residuals with new (equatorial) model - EQUpint_resids = Residuals( - self.toasB1855, EQUmodelB1855, use_weighted_mean=False - ).time_resids.to(u.s) - self.log.debug(np.abs(pint_resids - EQUpint_resids)) - msg = ( - "Residual comparison to ecliptic model failed with max relative difference %e s" - % np.nanmax(np.abs(pint_resids - EQUpint_resids)).value - ) - assert np.all(np.abs(pint_resids - EQUpint_resids) < 1e-10 * u.s), msg - - -if __name__ == "__main__": - pass +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + + # J0613 is in equatorial + s.parfileJ0613 = datadir / "J0613-0200_NANOGrav_dfg+12_TAI_FB90.par" + s.timJ0613 = datadir / "J0613-0200_NANOGrav_dfg+12.tim" + s.toasJ0613 = toa.get_TOAs( + s.timJ0613, + ephem="DE405", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + warnings.filterwarnings("ignore", message=r".*EPHVER.*") + s.modelJ0613 = get_model(s.parfileJ0613) + + # B1855+09 is in ecliptic + s.parfileB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par") + s.timB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim") + s.toasB1855 = toa.get_TOAs( + s.timB1855, + ephem="DE421", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + s.modelB1855 = get_model(s.parfileB1855) + + s.log = logging.getLogger("TestEcliptic") + return s + + +def test_to_ecliptic(setup): + # determine residuals with base (equatorial) model + pint_resids = Residuals( + setup.toasJ0613, setup.modelJ0613, use_weighted_mean=False + ).time_resids.to(u.s) + + # convert model to ecliptic coordinates + ECLmodelJ0613 = model_equatorial_to_ecliptic(setup.modelJ0613) + assert ECLmodelJ0613 is not None, "Creation of ecliptic model failed" + assert ( + "AstrometryEcliptic" in ECLmodelJ0613.components + ), "Creation of ecliptic model failed" + assert not ( + "AstrometryEquatorial" in ECLmodelJ0613.components + ), "Equatorial model still present" + setup.log.debug("Ecliptic model created") + + # determine residuals with new (ecliptic) model + ECLpint_resids = Residuals( + setup.toasJ0613, ECLmodelJ0613, use_weighted_mean=False + ).time_resids.to(u.s) + setup.log.debug(np.abs(pint_resids - ECLpint_resids)) + msg = ( + "Residual comparison to ecliptic model failed with max relative difference %e s" + % np.nanmax(np.abs(pint_resids - ECLpint_resids)).value + ) + assert np.all(np.abs(pint_resids - ECLpint_resids) < 1e-10 * u.s), msg + + +def test_to_equatorial(setup): + # determine residuals with base (ecliptic) model + pint_resids = Residuals( + setup.toasB1855, setup.modelB1855, use_weighted_mean=False + ).time_resids.to(u.s) + + # convert model to ecliptic coordinates + EQUmodelB1855 = model_ecliptic_to_equatorial(setup.modelB1855) + assert EQUmodelB1855 is not None, "Creation of equatorial model failed" + assert ( + "AstrometryEquatorial" in EQUmodelB1855.components + ), "Creation of equatorial model failed" + assert not ( + "AstrometryEcliptic" in EQUmodelB1855.components + ), "Ecliptic model still present" + setup.log.debug("Equatorial model created") + + # determine residuals with new (equatorial) model + EQUpint_resids = Residuals( + setup.toasB1855, EQUmodelB1855, use_weighted_mean=False + ).time_resids.to(u.s) + setup.log.debug(np.abs(pint_resids - EQUpint_resids)) + msg = ( + "Residual comparison to ecliptic model failed with max relative difference %e s" + % np.nanmax(np.abs(pint_resids - EQUpint_resids)).value + ) + assert np.all(np.abs(pint_resids - EQUpint_resids) < 1e-10 * u.s), msg diff --git a/tests/test_parfile_writing.py b/tests/test_parfile_writing.py index 3b05df4f7..b7a015c7e 100644 --- a/tests/test_parfile_writing.py +++ b/tests/test_parfile_writing.py @@ -1,6 +1,5 @@ """Various tests to assess the performance of parfile writing.""" import numbers -import os from io import StringIO import astropy.units as u @@ -15,8 +14,8 @@ def test_parfile_write(tmp_path, pickle_dir): - parfileB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par") - timB1855 = os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim") + parfileB1855 = datadir / "B1855+09_NANOGrav_9yv1.gls.par" + timB1855 = datadir / "B1855+09_NANOGrav_9yv1.tim" toasB1855 = toa.get_TOAs( timB1855, ephem="DE421", diff --git a/tests/test_phase_commands.py b/tests/test_phase_commands.py index 51d864b86..14b02f39b 100644 --- a/tests/test_phase_commands.py +++ b/tests/test_phase_commands.py @@ -16,7 +16,7 @@ def test_phase_commands(pickle_dir): model = pint.models.get_model(parfile) - toas = pint.toa.get_TOAs(timfile, picklefile=pickle_dir) + toas = pint.toa.get_TOAs(timfile, picklefilename=pickle_dir) # This TOA has PHASE -0.3. Check that assert np.isclose(float(toas.table[32]["flags"]["phase"]), -0.3) # This TOA should have PHASE 0.2 and -padd -0.2 diff --git a/tests/test_utils.py b/tests/test_utils.py index 06b38add5..c9bfafbe6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -605,9 +605,9 @@ def test_time_from_mjd_string_rejects_other_formats(): def test_dmxparse_runs(pickle_dir): """Test for dmxparse function.""" # FIXME: what does this actually check? - m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par")) + m = tm.get_model(datadir / "B1855+09_NANOGrav_9yv1.gls.par") t = toa.get_TOAs( - os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim"), + datadir / "B1855+09_NANOGrav_9yv1.tim", usepickle=True, picklefilename=pickle_dir, ) @@ -620,9 +620,9 @@ def test_dmxparse_runs_wls(pickle_dir): """Test for dmxparse function.""" # Check exception handling # FIXME: what does this actually check? - m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12_DMX.par")) + m = tm.get_model(datadir / "B1855+09_NANOGrav_dfg+12_DMX.par") t = toa.get_TOAs( - os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim"), + datadir / "B1855+09_NANOGrav_dfg+12.tim", usepickle=True, picklefilename=pickle_dir, ) From 357379532bba4491b9eb15a3dded20eec5cecb7a Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Wed, 13 Jul 2022 20:45:16 +0100 Subject: [PATCH 8/9] Further test cleanup --- src/pint/toa.py | 10 +- tests/test_dd.py | 72 +++++------ tests/test_ddk.py | 288 +++++++++++++++++++++----------------------- tests/test_ell1h.py | 18 ++- 4 files changed, 195 insertions(+), 193 deletions(-) diff --git a/src/pint/toa.py b/src/pint/toa.py index 3a991e48b..6968f9320 100644 --- a/src/pint/toa.py +++ b/src/pint/toa.py @@ -110,7 +110,7 @@ def get_TOAs( include_gps=None, planets=None, model=None, - usepickle=False, + usepickle=None, tdb_method="default", picklefilename=None, limits="warn", @@ -166,8 +166,10 @@ def get_TOAs( If a valid timing model is passed, model commands (such as BIPM version, planet shapiro delay, and solar system ephemeris) that affect TOA loading are applied. - usepickle : bool - Whether to try to use pickle-based caching of loaded clock-corrected TOAs objects. + usepickle : bool or None + Whether to try to use pickle-based caching of loaded clock-corrected + TOAs objects. If None, setting picklefilename will trigger use of + pickles; to suppress this supply False. tdb_method : str Which method to use for the clock correction to TDB. See :func:`pint.observatory.Observatory.get_TDBs` for details. @@ -222,6 +224,8 @@ def get_TOAs( updatepickle = False recalc = False + if usepickle is None: + usepickle = picklefilename is not None if usepickle: try: t = load_pickle(timfile, picklefilename=picklefilename) diff --git a/tests/test_dd.py b/tests/test_dd.py index cc55756bb..5c4c83c7e 100644 --- a/tests/test_dd.py +++ b/tests/test_dd.py @@ -4,6 +4,7 @@ import astropy.units as u import numpy as np +import pytest from pinttestdata import datadir from utils import verify_stand_alone_binary_parameter_updates @@ -12,39 +13,38 @@ from pint.residuals import Residuals -class TestDD(unittest.TestCase): - """Compare delays from the dd model with libstempo and PINT""" - - @classmethod - def setUpClass(cls): - os.chdir(datadir) - cls.parfileB1855 = "B1855+09_NANOGrav_dfg+12_modified_DD.par" - cls.timB1855 = "B1855+09_NANOGrav_dfg+12.tim" - cls.toasB1855 = toa.get_TOAs( - cls.timB1855, ephem="DE405", planets=False, include_bipm=False - ) - cls.modelB1855 = mb.get_model(cls.parfileB1855) - # libstempo result - cls.ltres, cls.ltbindelay = np.genfromtxt( - cls.parfileB1855 + ".tempo_test", unpack=True - ) - - def test_J1855_binary_delay(self): - # Calculate delays with PINT - pint_binary_delay = self.modelB1855.binarymodel_delay(self.toasB1855, None) - assert np.all( - np.abs(pint_binary_delay.value + self.ltbindelay) < 1e-11 - ), "DD B1855 TEST FAILED" - - # TODO: PINT can still incresase the precision by adding more components - def test_B1855(self): - pint_resids_us = Residuals( - self.toasB1855, self.modelB1855, use_weighted_mean=False - ).time_resids.to(u.s) - assert np.all( - np.abs(pint_resids_us.value - self.ltres) < 1e-7 - ), "DD B1855 TEST FAILED" - - -if __name__ == "__main__": - pass +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + s.parfileB1855 = datadir / "B1855+09_NANOGrav_dfg+12_modified_DD.par" + s.timB1855 = datadir / "B1855+09_NANOGrav_dfg+12.tim" + s.toasB1855 = toa.get_TOAs( + s.timB1855, + ephem="DE405", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + s.modelB1855 = mb.get_model(s.parfileB1855) + # libstempo result + s.ltres, s.ltbindelay = np.genfromtxt( + str(s.parfileB1855) + ".tempo_test", unpack=True + ) + return s + + +def test_J1855_binary_delay(setup): + # Calculate delays with PINT + pint_binary_delay = setup.modelB1855.binarymodel_delay(setup.toasB1855, None) + assert np.all(np.abs(pint_binary_delay.value + setup.ltbindelay) < 1e-11) + + +# TODO: PINT can still incresase the precision by adding more components +def test_B1855(setup): + pint_resids_us = Residuals( + setup.toasB1855, setup.modelB1855, use_weighted_mean=False + ).time_resids.to(u.s) + assert np.all(np.abs(pint_resids_us.value - setup.ltres) < 1e-7) diff --git a/tests/test_ddk.py b/tests/test_ddk.py index 7790f40d6..7d550abbe 100644 --- a/tests/test_ddk.py +++ b/tests/test_ddk.py @@ -46,154 +46,146 @@ """ -class TestDDK(unittest.TestCase): - """Compare delays from the ddk model with libstempo and PINT""" - - @classmethod - def setUpClass(cls): - cls.parfileJ1713 = "J1713+0747_NANOGrav_11yv0_short.gls.par" - cls.ICRSparfileJ1713 = "J1713+0747_NANOGrav_11yv0_short.gls.ICRS.par" - cls.timJ1713 = "J1713+0747_NANOGrav_11yv0_short.tim" - cls.toasJ1713 = toa.get_TOAs( - os.path.join(datadir, cls.timJ1713), ephem="DE421", planets=False - ) - index = cls.toasJ1713.table["index"] - cls.ECLmodelJ1713 = mb.get_model(os.path.join(datadir, cls.parfileJ1713)) - cls.ICRSmodelJ1713 = mb.get_model(os.path.join(datadir, cls.ICRSparfileJ1713)) - # libstempo result - # calculate using: datafile/make_J1713_libstempo.py - # make sure to sort to agree with the grouped TOAs - ( - cls.ECLltt, - cls.ECLltdt, - cls.ECLltf, - cls.ECLltres, - cls.ECLltbindelay, - ) = np.genfromtxt( - os.path.join(datadir, cls.parfileJ1713 + ".libstempo"), unpack=True - )[ - :, index - ] - ( - cls.ICRSltt, - cls.ICRSltdt, - cls.ICRSltf, - cls.ICRSltres, - cls.ICRSltbindelay, - ) = np.genfromtxt( - os.path.join(datadir, cls.ICRSparfileJ1713 + ".libstempo"), unpack=True - )[ - :, index - ] - - def test_J1713_ECL_binary_delay(self): - # Calculate delays with PINT - pint_binary_delay = self.ECLmodelJ1713.binarymodel_delay(self.toasJ1713, None) - print(f"{np.abs(pint_binary_delay.value + self.ECLltbindelay).max()}") - assert np.all(np.abs(pint_binary_delay.value + self.ECLltbindelay) < 5e-6), ( - "DDK J1713 ECL BINARY DELAY TEST FAILED: max difference is %e" - % np.abs(pint_binary_delay.value + self.ECLltbindelay).max() - ) - - def test_J1713_ICRS_binary_delay(self): - # Calculate delays with PINT - pint_binary_delay = self.ICRSmodelJ1713.binarymodel_delay(self.toasJ1713, None) - print(f"{np.abs(pint_binary_delay.value + self.ECLltbindelay).max()}") - assert np.all(np.abs(pint_binary_delay.value + self.ECLltbindelay) < 6e-6), ( - "DDK J1713 ICRS BINARY DELAY TEST FAILED: max difference is %e" - % np.abs(pint_binary_delay.value + self.ICRSltbindelay).max() - ) - - def test_J1713_ECL(self): - pint_resids_us = Residuals( - self.toasJ1713, self.ECLmodelJ1713, use_weighted_mean=False - ).time_resids.to(u.s) - diff = pint_resids_us.value - self.ECLltres - print("Max diff %e" % np.abs(diff - diff.mean()).max()) - assert np.all(np.abs(diff - diff.mean()) < 2e-8), ( - "DDK J1713 ECL RESIDUAL TEST FAILED: max difference is %e" - % np.abs(diff - diff.mean()).max() - ) - - def test_J1713_ICRS(self): - pint_resids_us = Residuals( - self.toasJ1713, self.ICRSmodelJ1713, use_weighted_mean=False - ).time_resids.to(u.s) - diff = pint_resids_us.value - self.ICRSltres - print("Max diff %e" % np.abs(diff - diff.mean()).max()) - assert np.all(np.abs(diff - diff.mean()) < 2e-8), ( - "DDK J1713 ICRS RESIDUAL TEST FAILED: max difference is %e" - % np.abs(diff - diff.mean()).max() - ) - - def test_change_px(self): - self.ECLmodelJ1713.update_binary_object(toas=self.toasJ1713) - assert ( - self.ECLmodelJ1713.binary_instance.PX.value == self.ECLmodelJ1713.PX.value - ) - bdelay0 = self.ECLmodelJ1713.binary_instance.binary_delay() - b_time0 = self.ECLmodelJ1713.binary_instance.t - # Change PX value - self.ECLmodelJ1713.PX.value = 0.1 - self.ECLmodelJ1713.update_binary_object(None) - b_time1 = self.ECLmodelJ1713.binary_instance.t - assert self.ECLmodelJ1713.binary_instance.PX.value == 0.1 - # The stand alone binary model's input time should not change - assert np.all(b_time0 == b_time1) - # Check if the time residual changed - bdelay1 = self.ECLmodelJ1713.binary_instance.binary_delay() - diff = bdelay0 - bdelay1 - assert np.all(diff != 0) - - def test_J1713_deriv(self): - testp = tdu.get_derivative_params(self.ECLmodelJ1713) - delay = self.ECLmodelJ1713.delay(self.toasJ1713) - for p in testp.keys(): - # Only check the binary parameters - if p not in self.ECLmodelJ1713.binary_instance.binary_params: - continue - if p in ["PX", "PMRA", "PMDEC"]: - continue - par = getattr(self.ECLmodelJ1713, p) - if isinstance(par, boolParameter): - continue - print("Runing derivative for %s" % ("d_phase_d_" + p)) - ndf = self.ECLmodelJ1713.d_phase_d_param_num(self.toasJ1713, p, testp[p]) - adf = self.ECLmodelJ1713.d_phase_d_param(self.toasJ1713, delay, p) - diff = adf - ndf - if not np.all(diff.value) == 0.0: - mean_der = (adf + ndf) / 2.0 - relative_diff = np.abs(diff) / np.abs(mean_der) - # print "Diff Max is :", np.abs(diff).max() - msg = ( - "Derivative test failed at d_phase_d_%s with max relative difference %lf" - % (p, np.nanmax(relative_diff).value) - ) - if p in ["SINI", "KIN"]: - tol = 0.7 - elif p in ["KOM"]: - tol = 0.04 - else: - tol = 1e-3 - print( - "derivative relative diff for %s, %lf" - % ("d_phase_d_" + p, np.nanmax(relative_diff).value) - ) - assert np.nanmax(relative_diff) < tol, msg +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + + s.parfileJ1713 = datadir / "J1713+0747_NANOGrav_11yv0_short.gls.par" + s.ICRSparfileJ1713 = datadir / "J1713+0747_NANOGrav_11yv0_short.gls.ICRS.par" + s.timJ1713 = datadir / "J1713+0747_NANOGrav_11yv0_short.tim" + s.toasJ1713 = toa.get_TOAs( + datadir / s.timJ1713, ephem="DE421", planets=False, picklefilename=pickle_dir + ) + index = s.toasJ1713.table["index"] + s.ECLmodelJ1713 = mb.get_model(datadir / s.parfileJ1713) + s.ICRSmodelJ1713 = mb.get_model(datadir / s.ICRSparfileJ1713) + # libstempo result + # calculate using: datafile/make_J1713_libstempo.py + # make sure to sort to agree with the grouped TOAs + (s.ECLltt, s.ECLltdt, s.ECLltf, s.ECLltres, s.ECLltbindelay,) = np.genfromtxt( + str(datadir / s.parfileJ1713) + ".libstempo", unpack=True + )[:, index] + (s.ICRSltt, s.ICRSltdt, s.ICRSltf, s.ICRSltres, s.ICRSltbindelay,) = np.genfromtxt( + str(datadir / s.ICRSparfileJ1713) + ".libstempo", unpack=True + )[:, index] + return s + + +def test_J1713_ECL_binary_delay(setup): + # Calculate delays with PINT + pint_binary_delay = setup.ECLmodelJ1713.binarymodel_delay(setup.toasJ1713, None) + print(f"{np.abs(pint_binary_delay.value + setup.ECLltbindelay).max()}") + assert np.all(np.abs(pint_binary_delay.value + setup.ECLltbindelay) < 5e-6), ( + "DDK J1713 ECL BINARY DELAY TEST FAILED: max difference is %e" + % np.abs(pint_binary_delay.value + setup.ECLltbindelay).max() + ) + + +def test_J1713_ICRS_binary_delay(setup): + # Calculate delays with PINT + pint_binary_delay = setup.ICRSmodelJ1713.binarymodel_delay(setup.toasJ1713, None) + print(f"{np.abs(pint_binary_delay.value + setup.ECLltbindelay).max()}") + assert np.all(np.abs(pint_binary_delay.value + setup.ECLltbindelay) < 6e-6), ( + "DDK J1713 ICRS BINARY DELAY TEST FAILED: max difference is %e" + % np.abs(pint_binary_delay.value + setup.ICRSltbindelay).max() + ) + + +def test_J1713_ECL(setup): + pint_resids_us = Residuals( + setup.toasJ1713, setup.ECLmodelJ1713, use_weighted_mean=False + ).time_resids.to(u.s) + diff = pint_resids_us.value - setup.ECLltres + print("Max diff %e" % np.abs(diff - diff.mean()).max()) + assert np.all(np.abs(diff - diff.mean()) < 2e-8), ( + "DDK J1713 ECL RESIDUAL TEST FAILED: max difference is %e" + % np.abs(diff - diff.mean()).max() + ) + + +def test_J1713_ICRS(setup): + pint_resids_us = Residuals( + setup.toasJ1713, setup.ICRSmodelJ1713, use_weighted_mean=False + ).time_resids.to(u.s) + diff = pint_resids_us.value - setup.ICRSltres + print("Max diff %e" % np.abs(diff - diff.mean()).max()) + assert np.all(np.abs(diff - diff.mean()) < 2e-8), ( + "DDK J1713 ICRS RESIDUAL TEST FAILED: max difference is %e" + % np.abs(diff - diff.mean()).max() + ) + + +def test_change_px(setup): + setup.ECLmodelJ1713.update_binary_object(toas=setup.toasJ1713) + assert setup.ECLmodelJ1713.binary_instance.PX.value == setup.ECLmodelJ1713.PX.value + bdelay0 = setup.ECLmodelJ1713.binary_instance.binary_delay() + b_time0 = setup.ECLmodelJ1713.binary_instance.t + # Change PX value + setup.ECLmodelJ1713.PX.value = 0.1 + setup.ECLmodelJ1713.update_binary_object(None) + b_time1 = setup.ECLmodelJ1713.binary_instance.t + assert setup.ECLmodelJ1713.binary_instance.PX.value == 0.1 + # The stand alone binary model's input time should not change + assert np.all(b_time0 == b_time1) + # Check if the time residual changed + bdelay1 = setup.ECLmodelJ1713.binary_instance.binary_delay() + diff = bdelay0 - bdelay1 + assert np.all(diff != 0) + + +def test_J1713_deriv(setup): + testp = tdu.get_derivative_params(setup.ECLmodelJ1713) + delay = setup.ECLmodelJ1713.delay(setup.toasJ1713) + for p in testp.keys(): + # Only check the binary parameters + if p not in setup.ECLmodelJ1713.binary_instance.binary_params: + continue + if p in ["PX", "PMRA", "PMDEC"]: + continue + par = getattr(setup.ECLmodelJ1713, p) + if isinstance(par, boolParameter): + continue + print("Runing derivative for %s" % ("d_phase_d_" + p)) + ndf = setup.ECLmodelJ1713.d_phase_d_param_num(setup.toasJ1713, p, testp[p]) + adf = setup.ECLmodelJ1713.d_phase_d_param(setup.toasJ1713, delay, p) + diff = adf - ndf + if not np.all(diff.value) == 0.0: + mean_der = (adf + ndf) / 2.0 + relative_diff = np.abs(diff) / np.abs(mean_der) + # print "Diff Max is :", np.abs(diff).max() + msg = ( + "Derivative test failed at d_phase_d_%s with max relative difference %lf" + % (p, np.nanmax(relative_diff).value) + ) + if p in ["SINI", "KIN"]: + tol = 0.7 + elif p in ["KOM"]: + tol = 0.04 else: - continue + tol = 1e-3 + print( + "derivative relative diff for %s, %lf" + % ("d_phase_d_" + p, np.nanmax(relative_diff).value) + ) + assert np.nanmax(relative_diff) < tol, msg + else: + continue + - def test_K96(self): - modelJ1713 = copy.deepcopy(self.ECLmodelJ1713) - log = logging.getLogger("TestJ1713 Switch of K96") - modelJ1713.K96.value = False - res = Residuals( - self.toasJ1713, modelJ1713, use_weighted_mean=False - ).time_resids.to(u.s) - delay = self.ECLmodelJ1713.delay(self.toasJ1713) - testp = tdu.get_derivative_params(modelJ1713) - for p in testp.keys(): - self.ECLmodelJ1713.d_phase_d_param(self.toasJ1713, delay, p) +def test_K96(setup): + modelJ1713 = copy.deepcopy(setup.ECLmodelJ1713) + log = logging.getLogger("TestJ1713 Switch of K96") + modelJ1713.K96.value = False + res = Residuals( + setup.toasJ1713, modelJ1713, use_weighted_mean=False + ).time_resids.to(u.s) + delay = setup.ECLmodelJ1713.delay(setup.toasJ1713) + testp = tdu.get_derivative_params(modelJ1713) + for p in testp.keys(): + setup.ECLmodelJ1713.d_phase_d_param(setup.toasJ1713, delay, p) def test_ddk_ECL_ICRS(): @@ -283,7 +275,3 @@ def test_A1dot_warning(): UserWarning, match=r"Using A1DOT with a DDK model is not advised." ): m = mb.get_model(StringIO(temp_par_str + "\nA1DOT 2\n")) - - -if __name__ == "__main__": - pass diff --git a/tests/test_ell1h.py b/tests/test_ell1h.py index e8fba87d0..0ee0637bc 100644 --- a/tests/test_ell1h.py +++ b/tests/test_ell1h.py @@ -44,13 +44,23 @@ @pytest.fixture(scope="module") -def toasJ0613(): - return toa.get_TOAs("J0613-0200_NANOGrav_9yv1.tim", ephem="DE421", planets=False) +def toasJ0613(pickle_dir): + return toa.get_TOAs( + "J0613-0200_NANOGrav_9yv1.tim", + ephem="DE421", + planets=False, + picklefilename=pickle_dir, + ) @pytest.fixture(scope="module") -def toasJ1853(): - return toa.get_TOAs("J1853+1303_NANOGrav_11yv0.tim", ephem="DE421", planets=False) +def toasJ1853(pickle_dir): + return toa.get_TOAs( + "J1853+1303_NANOGrav_11yv0.tim", + ephem="DE421", + planets=False, + picklefilename=pickle_dir, + ) @pytest.fixture From 5553ebae8c78de874daf7eaca994d9051f074d3e Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Fri, 15 Jul 2022 19:14:58 +0100 Subject: [PATCH 9/9] Further cleanup --- src/pint/toa.py | 2 +- tests/test_barytoa.py | 5 +- tests/test_copy.py | 6 +- tests/test_covariance_matrix.py | 2 - tests/test_d_phase_d_toa.py | 70 +++++------ tests/test_design_matrix.py | 209 +++++++++++++++++--------------- tests/test_early_chime_data.py | 71 ++++++----- tests/test_ell1h.py | 22 ++-- tests/test_tim_writing.py | 1 + tests/test_timing_model.py | 2 +- 10 files changed, 206 insertions(+), 184 deletions(-) diff --git a/src/pint/toa.py b/src/pint/toa.py index 6968f9320..4514a9eb8 100644 --- a/src/pint/toa.py +++ b/src/pint/toa.py @@ -398,7 +398,7 @@ def save_pickle(toas, picklefilename=None): if isinstance(toas.filename, (str, Path)): picklefilename = str(toas.filename) + ".pickle.gz" else: - picklefilename = toas.filename[0] + ".pickle.gz" + picklefilename = str(toas.filename[0]) + ".pickle.gz" else: raise ValueError("TOA pickle method needs a (single) filename.") if pickle_dir is not None: diff --git a/tests/test_barytoa.py b/tests/test_barytoa.py index f007d7fd5..63b3fe209 100644 --- a/tests/test_barytoa.py +++ b/tests/test_barytoa.py @@ -12,12 +12,11 @@ def test_barytoa(pickle_dir): - os.chdir(datadir) # This par file has a very simple model in it - m = get_model("slug.par") + m = get_model(datadir / "slug.par") # This .tim file has TOAs at the barycenter, and at infinite frequency - t = pint.toa.get_TOAs("slug.tim", picklefilename=pickle_dir) + t = pint.toa.get_TOAs(datadir / "slug.tim", picklefilename=pickle_dir) rs = pint.residuals.Residuals(t, m).time_resids diff --git a/tests/test_copy.py b/tests/test_copy.py index 0ac30e6c8..1a3cb7842 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -72,11 +72,11 @@ def test_copy_fitter_object(model, toas): assert fitter is not fitter_copy -def test_copy_wideband_fitter_object(): +def test_copy_wideband_fitter_object(pickle_dir): with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") - model = get_model(os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.gls.par")) - toas = get_TOAs(os.path.join(datadir, "J1614-2230_NANOGrav_12yv3.wb.tim")) + model = get_model(datadir / "J1614-2230_NANOGrav_12yv3.wb.gls.par") + toas = get_TOAs(datadir / "J1614-2230_NANOGrav_12yv3.wb.tim") fitter = WidebandTOAFitter([toas], model, additional_args={}) fitter_copy = copy.deepcopy(fitter) diff --git a/tests/test_covariance_matrix.py b/tests/test_covariance_matrix.py index a6bd69d32..a1e20f586 100644 --- a/tests/test_covariance_matrix.py +++ b/tests/test_covariance_matrix.py @@ -10,8 +10,6 @@ from pint.pint_matrix import CovarianceMatrix, combine_covariance_matrix -os.chdir(datadir) - class TestCovarianceMatrix: """Test for covariance matrix""" diff --git a/tests/test_d_phase_d_toa.py b/tests/test_d_phase_d_toa.py index db44f3ae9..e7c24239c 100644 --- a/tests/test_d_phase_d_toa.py +++ b/tests/test_d_phase_d_toa.py @@ -1,47 +1,49 @@ -import os -import unittest - import numpy as np +import pytest try: from erfa import DJM0 except ImportError: from astropy._erfa import DJM0 -from pinttestdata import datadir, testdir +from pinttestdata import datadir import pint.toa as toa from pint.models import model_builder as mb from pint.polycos import Polycos -class TestD_phase_d_toa(unittest.TestCase): - @classmethod - def setUpClass(self): - os.chdir(datadir) - self.parfileB1855 = "B1855+09_polycos.par" - self.timB1855 = "B1855_polyco.tim" - self.toasB1855 = toa.get_TOAs( - self.timB1855, ephem="DE405", planets=False, include_bipm=False - ) - self.modelB1855 = mb.get_model(self.parfileB1855) - # Read tempo style polycos. - self.plc = Polycos() - self.plc.read_polyco_file("B1855_polyco.dat", "tempo") - - def testD_phase_d_toa(self): - pint_d_phase_d_toa = self.modelB1855.d_phase_d_toa(self.toasB1855) - mjd = np.array( - [ - np.longdouble(t.jd1 - DJM0) + np.longdouble(t.jd2) - for t in self.toasB1855.table["mjd"] - ] - ) - tempo_d_phase_d_toa = self.plc.eval_spin_freq(mjd) - diff = pint_d_phase_d_toa.value - tempo_d_phase_d_toa - relative_diff = diff / tempo_d_phase_d_toa - assert np.all(np.abs(relative_diff) < 1e-7), "d_phase_d_toa test failed." - - -if __name__ == "__main__": - pass +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + s.parfileB1855 = datadir / "B1855+09_polycos.par" + s.timB1855 = datadir / "B1855_polyco.tim" + s.toasB1855 = toa.get_TOAs( + s.timB1855, + ephem="DE405", + planets=False, + include_bipm=False, + picklefilename=pickle_dir, + ) + s.modelB1855 = mb.get_model(s.parfileB1855) + # Read tempo style polycos. + s.plc = Polycos() + s.plc.read_polyco_file(datadir / "B1855_polyco.dat", "tempo") + return s + + +def testD_phase_d_toa(setup): + pint_d_phase_d_toa = setup.modelB1855.d_phase_d_toa(setup.toasB1855) + mjd = np.array( + [ + np.longdouble(t.jd1 - DJM0) + np.longdouble(t.jd2) + for t in setup.toasB1855["mjd"] + ] + ) + tempo_d_phase_d_toa = setup.plc.eval_spin_freq(mjd) + diff = pint_d_phase_d_toa.value - tempo_d_phase_d_toa + relative_diff = diff / tempo_d_phase_d_toa + assert np.all(np.abs(relative_diff) < 1e-7), "d_phase_d_toa test failed." diff --git a/tests/test_design_matrix.py b/tests/test_design_matrix.py index c45f472aa..fc0b5a911 100644 --- a/tests/test_design_matrix.py +++ b/tests/test_design_matrix.py @@ -1,5 +1,5 @@ """ Test for pint design matrix""" -import os +import warnings import astropy.units as u import numpy as np @@ -15,100 +15,113 @@ from pint.toa import get_TOAs -class TestDesignMatrix: - def setup(self): - os.chdir(datadir) - self.par_file = "J1614-2230_NANOGrav_12yv3.wb.gls.par" - self.tim_file = "J1614-2230_NANOGrav_12yv3.wb.tim" - self.model = get_model(self.par_file) - self.toas = get_TOAs(self.tim_file) - self.default_test_param = [] - for p in self.model.params: - if not getattr(self.model, p).frozen: - self.default_test_param.append(p) - self.test_param_lite = ["F0", "ELONG", "ELAT", "DMX_0023", "JUMP1", "DMJUMP2"] - self.phase_designmatrix_maker = DesignMatrixMaker("phase", u.Unit("")) - self.toa_designmatrix_maker = DesignMatrixMaker("toa", u.s) - self.dm_designmatrix_maker = DesignMatrixMaker("dm", u.pc / u.cm**3) - self.noise_designmatrix_maker = DesignMatrixMaker("toa_noise", u.s) - - def test_make_phase_designmatrix(self): - phase_designmatrix = self.phase_designmatrix_maker( - self.toas, self.model, self.test_param_lite - ) - - assert phase_designmatrix.ndim == 2 - assert phase_designmatrix.shape == ( - self.toas.ntoas, - len(self.test_param_lite) + 1, - ) - - # Test labels - labels = phase_designmatrix.labels - assert len(labels) == 2 - assert len(labels[0]) == 1 - assert len(labels[1]) == len(self.test_param_lite) + 1 - assert [l[0] for l in labels[1]] == ["Offset"] + self.test_param_lite - - def test_make_dm_designmatrix(self): - test_param = ["DMX_0001", "DMX_0010", "DMJUMP1"] - phase_designmatrix = self.dm_designmatrix_maker( - self.toas, self.model, test_param - ) - - def test_combine_designmatrix_quantity(self): - phase_designmatrix = self.phase_designmatrix_maker( - self.toas, self.model, self.test_param_lite - ) - dm_designmatrix = self.dm_designmatrix_maker( - self.toas, self.model, self.test_param_lite, offset=True, offset_padding=0.0 - ) - - combined = combine_design_matrices_by_quantity( - [phase_designmatrix, dm_designmatrix] - ) - # dim1 includes parameter lite and offset - assert combined.shape == (2 * self.toas.ntoas, len(self.test_param_lite) + 1) - assert len(combined.get_axis_labels(0)) == 2 - dim0_labels = [x[0] for x in combined.get_axis_labels(0)] - assert dim0_labels == ["phase", "dm"] - dim1_labels = [x[0] for x in combined.get_axis_labels(1)] - assert dim1_labels == ["Offset"] + self.test_param_lite - - def test_toa_noise_designmatrix(self): - toas = get_TOAs("B1855+09_NANOGrav_9yv1.tim") - model = get_model("B1855+09_NANOGrav_9yv1.gls.par") - noise_designmatrix = self.noise_designmatrix_maker(toas, model) - assert noise_designmatrix.shape[0] == toas.ntoas - assert noise_designmatrix.derivative_quantity == ["toa"] - assert noise_designmatrix.derivative_params == ["toa_noise_params"] - - def test_combine_designmatrix_all(self): - toas = get_TOAs("B1855+09_NANOGrav_12yv3.wb.tim") - model = get_model("B1855+09_NANOGrav_12yv3.wb.gls.par") - noise_designmatrix = self.noise_designmatrix_maker(toas, model) - - toa_designmatrix = self.toa_designmatrix_maker( - toas, model, self.test_param_lite - ) - dm_designmatrix = self.dm_designmatrix_maker( - toas, model, self.test_param_lite, offset=True, offset_padding=0.0 - ) - combined_quantity = combine_design_matrices_by_quantity( - [toa_designmatrix, dm_designmatrix] - ) - combined_param = combine_design_matrices_by_param( - combined_quantity, noise_designmatrix - ) - - assert combined_param.shape == ( - toa_designmatrix.shape[0] + dm_designmatrix.shape[0], - toa_designmatrix.shape[1] + noise_designmatrix.shape[1], - ) - - assert np.all( - combined_param.matrix[ - toas.ntoas : toas.ntoas * 2, toa_designmatrix.shape[1] : : - ] - == 0.0 - ) +@pytest.fixture +def setup(pickle_dir): + class Setup: + pass + + s = Setup() + s.par_file = datadir / "J1614-2230_NANOGrav_12yv3.wb.gls.par" + s.tim_file = datadir / "J1614-2230_NANOGrav_12yv3.wb.tim" + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + s.model = get_model(s.par_file) + s.toas = get_TOAs(s.tim_file, picklefilename=pickle_dir) + s.default_test_param = [] + for p in s.model.params: + if not getattr(s.model, p).frozen: + s.default_test_param.append(p) + s.test_param_lite = ["F0", "ELONG", "ELAT", "DMX_0023", "JUMP1", "DMJUMP2"] + s.phase_designmatrix_maker = DesignMatrixMaker("phase", u.Unit("")) + s.toa_designmatrix_maker = DesignMatrixMaker("toa", u.s) + s.dm_designmatrix_maker = DesignMatrixMaker("dm", u.pc / u.cm**3) + s.noise_designmatrix_maker = DesignMatrixMaker("toa_noise", u.s) + return s + + +def test_make_phase_designmatrix(setup): + phase_designmatrix = setup.phase_designmatrix_maker( + setup.toas, setup.model, setup.test_param_lite + ) + + assert phase_designmatrix.ndim == 2 + assert phase_designmatrix.shape == ( + setup.toas.ntoas, + len(setup.test_param_lite) + 1, + ) + + # Test labels + labels = phase_designmatrix.labels + assert len(labels) == 2 + assert len(labels[0]) == 1 + assert len(labels[1]) == len(setup.test_param_lite) + 1 + assert [l[0] for l in labels[1]] == ["Offset"] + setup.test_param_lite + + +def test_make_dm_designmatrix(setup): + test_param = ["DMX_0001", "DMX_0010", "DMJUMP1"] + phase_designmatrix = setup.dm_designmatrix_maker( + setup.toas, setup.model, test_param + ) + + +def test_combine_designmatrix_quantity(setup): + phase_designmatrix = setup.phase_designmatrix_maker( + setup.toas, setup.model, setup.test_param_lite + ) + dm_designmatrix = setup.dm_designmatrix_maker( + setup.toas, setup.model, setup.test_param_lite, offset=True, offset_padding=0.0 + ) + + combined = combine_design_matrices_by_quantity( + [phase_designmatrix, dm_designmatrix] + ) + # dim1 includes parameter lite and offset + assert combined.shape == (2 * setup.toas.ntoas, len(setup.test_param_lite) + 1) + assert len(combined.get_axis_labels(0)) == 2 + dim0_labels = [x[0] for x in combined.get_axis_labels(0)] + assert dim0_labels == ["phase", "dm"] + dim1_labels = [x[0] for x in combined.get_axis_labels(1)] + assert dim1_labels == ["Offset"] + setup.test_param_lite + + +def test_toa_noise_designmatrix(setup, pickle_dir): + toas = get_TOAs(datadir / "B1855+09_NANOGrav_9yv1.tim", picklefilename=pickle_dir) + model = get_model(datadir / "B1855+09_NANOGrav_9yv1.gls.par") + noise_designmatrix = setup.noise_designmatrix_maker(toas, model) + assert noise_designmatrix.shape[0] == toas.ntoas + assert noise_designmatrix.derivative_quantity == ["toa"] + assert noise_designmatrix.derivative_params == ["toa_noise_params"] + + +def test_combine_designmatrix_all(setup, pickle_dir): + toas = get_TOAs( + datadir / "B1855+09_NANOGrav_12yv3.wb.tim", picklefilename=pickle_dir + ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + model = get_model(datadir / "B1855+09_NANOGrav_12yv3.wb.gls.par") + noise_designmatrix = setup.noise_designmatrix_maker(toas, model) + + toa_designmatrix = setup.toa_designmatrix_maker(toas, model, setup.test_param_lite) + dm_designmatrix = setup.dm_designmatrix_maker( + toas, model, setup.test_param_lite, offset=True, offset_padding=0.0 + ) + combined_quantity = combine_design_matrices_by_quantity( + [toa_designmatrix, dm_designmatrix] + ) + combined_param = combine_design_matrices_by_param( + combined_quantity, noise_designmatrix + ) + + assert combined_param.shape == ( + toa_designmatrix.shape[0] + dm_designmatrix.shape[0], + toa_designmatrix.shape[1] + noise_designmatrix.shape[1], + ) + + assert np.all( + combined_param.matrix[ + toas.ntoas : toas.ntoas * 2, toa_designmatrix.shape[1] : : + ] + == 0.0 + ) diff --git a/tests/test_early_chime_data.py b/tests/test_early_chime_data.py index 112bb699a..078794862 100644 --- a/tests/test_early_chime_data.py +++ b/tests/test_early_chime_data.py @@ -1,37 +1,54 @@ """Various tests to assess the performance of early CHIME data.""" -import os -import unittest +import warnings import astropy.units as u -import numpy as np +import pytest from astropy.tests.helper import assert_quantity_allclose -from pinttestdata import datadir, testdir +from pinttestdata import datadir import pint.models.model_builder as mb import pint.toa as toa from pint.residuals import Residuals -class Test_CHIME_data(unittest.TestCase): - """Compare delays from the dd model with tempo and PINT""" - - @classmethod - def setUpClass(self): - os.chdir(datadir) - self.parfile = "B1937+21.basic.par" - self.tim = "B1937+21.CHIME.CHIME.NG.N.tim" - - def test_toa_read(self): - toas = toa.get_TOAs(self.tim, ephem="DE436", planets=False, include_bipm=True) - assert toas.ntoas == 848, "CHIME TOAs did not read correctly." - assert list(set(toas.get_obss())) == [ - "chime" - ], "CHIME did not recognized by observatory module." - - def test_residuals(self): - model = mb.get_model(self.parfile) - toas = toa.get_TOAs(self.tim, ephem="DE436", planets=False, include_bipm=True) - r = Residuals(toas, model) - # Comment out the following test for now, since the new residual - # code makes it fail, and it probably shouldn't -- SMR - # assert_quantity_allclose(r.time_resids.to(u.us), 0*u.us, atol=800*u.us, rtol=0) +@pytest.fixture +def setup(): + class Setup: + pass + + s = Setup() + s.parfile = datadir / "B1937+21.basic.par" + s.tim = datadir / "B1937+21.CHIME.CHIME.NG.N.tim" + return s + + +def test_toa_read(setup, pickle_dir): + toas = toa.get_TOAs( + setup.tim, + ephem="DE436", + planets=False, + include_bipm=True, + picklefilename=pickle_dir, + ) + assert toas.ntoas == 848, "CHIME TOAs did not read correctly." + assert set(toas.get_obss()) == {"chime"} + + +@pytest.mark.xfail( + reason="SMR says: the new residual code makes it fail, and it probably shouldn't" +) +def test_residuals(setup, pickle_dir): + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=r".*T2CMETHOD.*") + model = mb.get_model(setup.parfile) + toas = toa.get_TOAs( + setup.tim, + ephem="DE436", + planets=False, + include_bipm=True, + picklefilename=pickle_dir, + ) + r = Residuals(toas, model) + # Comment out the following test for now, since the new residual + # code makes it fail, and it probably shouldn't -- SMR + assert_quantity_allclose(r.time_resids.to(u.us), 0 * u.us, atol=800 * u.us, rtol=0) diff --git a/tests/test_ell1h.py b/tests/test_ell1h.py index 0ee0637bc..05d43af76 100644 --- a/tests/test_ell1h.py +++ b/tests/test_ell1h.py @@ -1,26 +1,18 @@ """Tests of ELL1H model """ import logging -import os -import unittest from io import StringIO -from warnings import warn import astropy.units as u import numpy as np import pytest import test_derivative_utils as tdu from pinttestdata import datadir -from utils import verify_stand_alone_binary_parameter_updates import pint.fitter as ff import pint.toa as toa from pint.models import get_model -from pint.models.timing_model import TimingModelError from pint.residuals import Residuals -os.chdir(datadir) - - simple_par = """ PSR J0613-0200 LAMBDA 93.7990065496191 1 0.0000000158550 @@ -46,7 +38,7 @@ @pytest.fixture(scope="module") def toasJ0613(pickle_dir): return toa.get_TOAs( - "J0613-0200_NANOGrav_9yv1.tim", + datadir / "J0613-0200_NANOGrav_9yv1.tim", ephem="DE421", planets=False, picklefilename=pickle_dir, @@ -56,7 +48,7 @@ def toasJ0613(pickle_dir): @pytest.fixture(scope="module") def toasJ1853(pickle_dir): return toa.get_TOAs( - "J1853+1303_NANOGrav_11yv0.tim", + datadir / "J1853+1303_NANOGrav_11yv0.tim", ephem="DE421", planets=False, picklefilename=pickle_dir, @@ -65,23 +57,23 @@ def toasJ1853(pickle_dir): @pytest.fixture def modelJ0613(): - return get_model("J0613-0200_NANOGrav_9yv1_ELL1H.gls.par") + return get_model(datadir / "J0613-0200_NANOGrav_9yv1_ELL1H.gls.par") @pytest.fixture def modelJ1853(): - return get_model("J1853+1303_NANOGrav_11yv0.gls.par") + return get_model(datadir / "J1853+1303_NANOGrav_11yv0.gls.par") @pytest.fixture() def modelJ0613_STIG(): - return get_model("J0613-0200_NANOGrav_9yv1_ELL1H_STIG.gls.par") + return get_model(datadir / "J0613-0200_NANOGrav_9yv1_ELL1H_STIG.gls.par") @pytest.fixture() def tempo2_res(): - parfileJ1853 = "J1853+1303_NANOGrav_11yv0.gls.par" - return np.genfromtxt(parfileJ1853 + ".tempo2_test", skip_header=1, unpack=True) + parfileJ1853 = datadir / "J1853+1303_NANOGrav_11yv0.gls.par" + return np.genfromtxt(str(parfileJ1853) + ".tempo2_test", skip_header=1, unpack=True) def test_J1853(toasJ1853, modelJ1853, tempo2_res): diff --git a/tests/test_tim_writing.py b/tests/test_tim_writing.py index 86bf3cb1e..743797fff 100644 --- a/tests/test_tim_writing.py +++ b/tests/test_tim_writing.py @@ -126,6 +126,7 @@ def test_barycenter(k): "-obs", "-clkcorr", "-to", # FIXME: used in clock corrections? What does this do? + "-pn", # pulse number has special interpretation } ), from_regex(re.compile(r"[ \t]+", re.ASCII), fullmatch=True), diff --git a/tests/test_timing_model.py b/tests/test_timing_model.py index 1c4616bae..990fee2ca 100644 --- a/tests/test_timing_model.py +++ b/tests/test_timing_model.py @@ -51,7 +51,7 @@ def timfile_nojumps(pickle_dir): class TestModelBuilding: def setup(self): - self.parfile = os.path.join(datadir, "J0437-4715.par") + self.parfile = datadir / "J0437-4715.par" def test_from_par(self): tm = get_model(self.parfile)