def test_model(data, with_units): t, y, dy, params = data # Compute the model using linear regression A = np.zeros((len(t), 2)) p = params["period"] dt = np.abs((t-params["transit_time"]+0.5*p) % p-0.5*p) m_in = dt < 0.5*params["duration"] A[~m_in, 0] = 1.0 A[m_in, 1] = 1.0 w = np.linalg.solve(np.dot(A.T, A / dy[:, None]**2), np.dot(A.T, y / dy**2)) model_true = np.dot(A, w) if with_units: t = t * u.day y = y * u.mag dy = dy * u.mag model_true = model_true * u.mag # Compute the model using the periodogram pgram = BoxLeastSquares(t, y, dy) model = pgram.model(t, p, params["duration"], params["transit_time"]) # Make sure that the transit mask is consistent with the model transit_mask = pgram.transit_mask(t, p, params["duration"], params["transit_time"]) transit_mask0 = (model - model.max()) < 0.0 assert_allclose(transit_mask, transit_mask0) assert_quantity_allclose(model, model_true)
def test_absolute_times(data, timedelta): # Make sure that we handle absolute times correctly. We also check that # TimeDelta works properly when timedelta is True. # The example data uses relative times t, y, dy, params = data # FIXME: There seems to be a numerical stability issue in that if we run # the algorithm with the same values but offset in time, the transit_time # is not offset by a fixed amount. To avoid this issue in this test, we # make sure the first time is also the smallest so that internally the # values of the relative time should be the same. t[0] = 0. # Add units t = t * u.day y = y * u.mag dy = dy * u.mag # We now construct a set of absolute times but keeping the rest the same. start = Time('2019-05-04T12:34:56') trel = TimeDelta(t) if timedelta else t t = trel + start # and we set up two instances of BoxLeastSquares, one with absolute and one # with relative times. bls1 = BoxLeastSquares(t, y, dy) bls2 = BoxLeastSquares(trel, y, dy) results1 = bls1.autopower(0.16 * u.day) results2 = bls2.autopower(0.16 * u.day) # All the results should match except transit time which should be # absolute instead of relative in the first case. for key in results1: if key == 'transit_time': assert_quantity_allclose((results1[key] - start).to(u.day), results2[key]) elif key == 'objective': assert results1[key] == results2[key] else: assert_allclose(results1[key], results2[key]) # Check that model evaluation works fine model1 = bls1.model(t, 0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) model2 = bls2.model(trel, 0.2 * u.day, 0.05 * u.day, TimeDelta(1 * u.day)) assert_quantity_allclose(model1, model2) # Check model validation with pytest.raises(TypeError) as exc: bls1.model(t, 0.2 * u.day, 0.05 * u.day, 1 * u.day) assert exc.value.args[0] == ('transit_time was provided as a relative time ' 'but the BoxLeastSquares class was initialized ' 'with absolute times.') with pytest.raises(TypeError) as exc: bls1.model(trel, 0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) assert exc.value.args[0] == ('t_model was provided as a relative time ' 'but the BoxLeastSquares class was initialized ' 'with absolute times.') with pytest.raises(TypeError) as exc: bls2.model(trel, 0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) assert exc.value.args[0] == ('transit_time was provided as an absolute time ' 'but the BoxLeastSquares class was initialized ' 'with relative times.') with pytest.raises(TypeError) as exc: bls2.model(t, 0.2 * u.day, 0.05 * u.day, 1 * u.day) assert exc.value.args[0] == ('t_model was provided as an absolute time ' 'but the BoxLeastSquares class was initialized ' 'with relative times.') # Check compute_stats stats1 = bls1.compute_stats(0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) stats2 = bls2.compute_stats(0.2 * u.day, 0.05 * u.day, 1 * u.day) for key in stats1: if key == 'transit_times': assert_quantity_allclose((stats1[key] - start).to(u.day), stats2[key], atol=1e-10 * u.day) elif key.startswith('depth'): for value1, value2 in zip(stats1[key], stats2[key]): assert_quantity_allclose(value1, value2) else: assert_allclose(stats1[key], stats2[key]) # Check compute_stats validation with pytest.raises(TypeError) as exc: bls1.compute_stats(0.2 * u.day, 0.05 * u.day, 1 * u.day) assert exc.value.args[0] == ('transit_time was provided as a relative time ' 'but the BoxLeastSquares class was initialized ' 'with absolute times.') with pytest.raises(TypeError) as exc: bls2.compute_stats(0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) assert exc.value.args[0] == ('transit_time was provided as an absolute time ' 'but the BoxLeastSquares class was initialized ' 'with relative times.') # Check transit_mask mask1 = bls1.transit_mask(t, 0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) mask2 = bls2.transit_mask(trel, 0.2 * u.day, 0.05 * u.day, 1 * u.day) assert_equal(mask1, mask2) # Check transit_mask validation with pytest.raises(TypeError) as exc: bls1.transit_mask(t, 0.2 * u.day, 0.05 * u.day, 1 * u.day) assert exc.value.args[0] == ('transit_time was provided as a relative time ' 'but the BoxLeastSquares class was initialized ' 'with absolute times.') with pytest.raises(TypeError) as exc: bls1.transit_mask(trel, 0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) assert exc.value.args[0] == ('t was provided as a relative time ' 'but the BoxLeastSquares class was initialized ' 'with absolute times.') with pytest.raises(TypeError) as exc: bls2.transit_mask(trel, 0.2 * u.day, 0.05 * u.day, Time('2019-06-04T12:34:56')) assert exc.value.args[0] == ('transit_time was provided as an absolute time ' 'but the BoxLeastSquares class was initialized ' 'with relative times.') with pytest.raises(TypeError) as exc: bls2.transit_mask(t, 0.2 * u.day, 0.05 * u.day, 1 * u.day) assert exc.value.args[0] == ('t was provided as an absolute time ' 'but the BoxLeastSquares class was initialized ' 'with relative times.')