<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_gp_coefficients
----------------------------------

Tests for GP signals used with deterministic coefficients.
"""


import logging
import math
import platform
import unittest

import numpy as np

from enterprise.pulsar import Pulsar
from enterprise.signals import (
    deterministic_signals,
    gp_signals,
    parameter,
    selections,
    signal_base,
    utils,
    white_signals,
)
from enterprise.signals.selections import Selection
from tests.enterprise_test_data import datadir

pyv3 = platform.python_version().split(".")[0] == "3"

logging.basicConfig(format="%(levelname)s: %(name)s: %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)


@signal_base.function
def create_quant_matrix(toas, dt=1):
    U, _ = utils.create_quantization_matrix(toas, dt=dt, nmin=1)
    avetoas = np.array([toas[idx.astype(bool)].mean() for idx in U.T])
    # return value slightly different than 1 to get around ECORR columns
    return U * 1.0000001, avetoas


@signal_base.function
def se_kernel(etoas, log10_sigma=-7, log10_lam=np.log10(30 * 86400)):
    tm = np.abs(etoas[None, :] - etoas[:, None])
    d = np.eye(tm.shape[0]) * 10 ** (2 * (log10_sigma - 1.5))
    return 10 ** (2 * log10_sigma) * np.exp(-tm ** 2 / 2 / 10 ** (2 * log10_lam)) + d


class TestGPCoefficients(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        """Setup the Pulsar object."""

        # initialize Pulsar class
        cls.psr = Pulsar(datadir + "/B1855+09_NANOGrav_9yv1.gls.par", datadir + "/B1855+09_NANOGrav_9yv1.tim")

        cls.psr2 = Pulsar(datadir + "/B1937+21_NANOGrav_9yv1.gls.par", datadir + "/B1937+21_NANOGrav_9yv1.tim")

    if not pyv3:
        msg = "GP coefficients compatible only with Python 3. Skipping tests"
        logger.warning(msg)
        pass
    else:

        def test_ephemeris(self):
            """Test physical-ephemeris delay, made three ways: from
            marginalized GP, from coefficient-based GP, from
            deterministic model."""

            ef = white_signals.MeasurementNoise(efac=parameter.Uniform(0.1, 5.0))

            eph = gp_signals.FourierBasisCommonGP_physicalephem(sat_orb_elements=None)

            ephc = gp_signals.FourierBasisCommonGP_physicalephem(sat_orb_elements=None, coefficients=True)

            ephd = deterministic_signals.PhysicalEphemerisSignal(sat_orb_elements=False)

            model = ef + eph
            modelc = ef + ephc
            modeld = ef + ephd

            pta = signal_base.PTA([model(self.psr), model(self.psr2)])
            ptac = signal_base.PTA([modelc(self.psr), modelc(self.psr2)])
            ptad = signal_base.PTA([modeld(self.psr), modeld(self.psr2)])

            cf = 1e-3 * np.random.randn(11)
            cf[0] = 1e-5  # this is more sensitive to linearity

            bs = pta.get_basis()
            da = [np.dot(bs[0], cf), np.dot(bs[1], cf)]

            params = {
                "B1855+09_efac": 1,
                "B1937+21_efac": 1,
                "B1855+09_physicalephem_gp_coefficients": cf,
                "B1937+21_physicalephem_gp_coefficients": cf,
            }
            db = ptac.get_delay(params=params)

            dparams = {
                "B1855+09_efac": 1,
                "B1937+21_efac": 1,<commit_msg>Fix ephemeris GP coefficient test for renamed process<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_gp_coefficients
----------------------------------

Tests for GP signals used with deterministic coefficients.
"""


import logging
import math
import platform
import unittest

import numpy as np

from enterprise.pulsar import Pulsar
from enterprise.signals import (
    deterministic_signals,
    gp_signals,
    parameter,
    selections,
    signal_base,
    utils,
    white_signals,
)
from enterprise.signals.selections import Selection
from tests.enterprise_test_data import datadir

pyv3 = platform.python_version().split(".")[0] == "3"

logging.basicConfig(format="%(levelname)s: %(name)s: %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)


@signal_base.function
def create_quant_matrix(toas, dt=1):
    U, _ = utils.create_quantization_matrix(toas, dt=dt, nmin=1)
    avetoas = np.array([toas[idx.astype(bool)].mean() for idx in U.T])
    # return value slightly different than 1 to get around ECORR columns
    return U * 1.0000001, avetoas


@signal_base.function
def se_kernel(etoas, log10_sigma=-7, log10_lam=np.log10(30 * 86400)):
    tm = np.abs(etoas[None, :] - etoas[:, None])
    d = np.eye(tm.shape[0]) * 10 ** (2 * (log10_sigma - 1.5))
    return 10 ** (2 * log10_sigma) * np.exp(-tm ** 2 / 2 / 10 ** (2 * log10_lam)) + d


class TestGPCoefficients(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        """Setup the Pulsar object."""

        # initialize Pulsar class
        cls.psr = Pulsar(datadir + "/B1855+09_NANOGrav_9yv1.gls.par", datadir + "/B1855+09_NANOGrav_9yv1.tim")

        cls.psr2 = Pulsar(datadir + "/B1937+21_NANOGrav_9yv1.gls.par", datadir + "/B1937+21_NANOGrav_9yv1.tim")

    if not pyv3:
        msg = "GP coefficients compatible only with Python 3. Skipping tests"
        logger.warning(msg)
        pass
    else:
        def test_ephemeris(self):
            """Test physical-ephemeris delay, made three ways: from
            marginalized GP, from coefficient-based GP, from
            deterministic model."""

            ef = white_signals.MeasurementNoise(efac=parameter.Uniform(0.1, 5.0))

            eph = gp_signals.FourierBasisCommonGP_physicalephem(sat_orb_elements=None)

            ephc = gp_signals.FourierBasisCommonGP_physicalephem(sat_orb_elements=None, coefficients=True)

            ephd = deterministic_signals.PhysicalEphemerisSignal(sat_orb_elements=False)

            model = ef + eph
            modelc = ef + ephc
            modeld = ef + ephd

            pta = signal_base.PTA([model(self.psr), model(self.psr2)])
            ptac = signal_base.PTA([modelc(self.psr), modelc(self.psr2)])
            ptad = signal_base.PTA([modeld(self.psr), modeld(self.psr2)])

            cf = 1e-3 * np.random.randn(11)
            cf[0] = 1e-5  # this is more sensitive to linearity

            bs = pta.get_basis()
            da = [np.dot(bs[0], cf), np.dot(bs[1], cf)]

            params = {
                "B1855+09_efac": 1,
                "B1937+21_efac": 1,
                "B1855+09_phys_ephem_gp_coefficients": cf,
                "B1937+21_phys_ephem_gp_coefficients": cf,
            }
            db = ptac.get_delay(params=params)

            dparams = {
                "B1855+09_efac": 1,
                "B1937+21_efac": 1,
{'fraction_non_alphanumeric': 0.1290803398420033, 'fraction_numerical': 0.041734982858846324, 'mean_word_length': 2.613354873451804, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5874807', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2291, 'n_words': 464}