Пример #1
0
def test_percent_close_to_median():
    """Test feature which finds the percentage of points near the median value."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['percent_close_to_median'])
    amplitude = (max(values) - min(values)) / 2.
    within_buffer = np.abs(values - np.median(values)) < 0.2 * amplitude
    npt.assert_allclose(f['percent_close_to_median'], np.mean(within_buffer))
Пример #2
0
def test_percent_close_to_median():
    """Test feature which finds the percentage of points near the median value."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors,
            ['percent_close_to_median'])
    amplitude = (max(values) - min(values)) / 2.
    within_buffer = np.abs(values - np.median(values)) < 0.2*amplitude
    npt.assert_allclose(f['percent_close_to_median'], np.mean(within_buffer))
Пример #3
0
def test_delta_t_hist():
    """Test histogram of all time lags."""
    times, values, errors = irregular_random(500)
    delta_ts = [pair[1] - pair[0] for pair in itertools.combinations(times, 2)]
    nbins = 50
    bins = np.linspace(0, max(times) - min(times), nbins + 1)
    npt.assert_allclose(cf.delta_t_hist(times, nbins),
                        np.histogram(delta_ts, bins=bins)[0], atol=2)
Пример #4
0
def test_scatter_res_raw():
    """Test feature that measures scatter of Lomb-Scargle residuals."""
    times, values, errors = irregular_random()
    lomb_model = lomb_scargle.lomb_scargle_model(times, values, errors)
    residuals = values - lomb_model['freq_fits'][0]['model']
    resid_mad = np.median(np.abs(residuals - np.median(residuals)))
    value_mad = np.median(np.abs(values - np.median(values)))
    f = generate_features(times, values, errors, ['scatter_res_raw'])
    npt.assert_allclose(f['scatter_res_raw'], resid_mad / value_mad, atol=3e-2)
Пример #5
0
def test_scatter_res_raw():
    """Test feature that measures scatter of Lomb-Scargle residuals."""
    times, values, errors = irregular_random()
    lomb_model = lomb_scargle.lomb_scargle_model(times, values, errors)
    residuals = values - lomb_model['freq_fits'][0]['model']
    resid_mad = np.median(np.abs(residuals - np.median(residuals)))
    value_mad = np.median(np.abs(values - np.median(values)))
    f = generate_features(times, values, errors, ['scatter_res_raw'])
    npt.assert_allclose(f['scatter_res_raw'], resid_mad / value_mad, atol=3e-2)
Пример #6
0
def test_amplitude():
    """Test features related to amplitude/magnitude percentiles."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['amplitude'])
    npt.assert_allclose(f['amplitude'], (max(values) - min(values)) / 2.)

    f = generate_features(times, values, errors, ['percent_amplitude'])
    max_scaled = 10**(-0.4 * max(values))
    min_scaled = 10**(-0.4 * min(values))
    med_scaled = 10**(-0.4 * np.median(values))
    peak_from_median = max(abs((max_scaled - med_scaled) / med_scaled),
                           abs((min_scaled - med_scaled)) / med_scaled)
    npt.assert_allclose(f['percent_amplitude'], peak_from_median, rtol=5e-4)

    f = generate_features(times, values, errors,
                          ['percent_difference_flux_percentile'])
    band_offset = 13.72
    w_m2 = 10**(-0.4 * (values + band_offset) - 3
                )  # 1 erg/s/cm^2 = 10^-3 w/m^2
    npt.assert_allclose(
        f['percent_difference_flux_percentile'],
        np.diff(np.percentile(w_m2, [5, 95])) / np.median(w_m2))

    f = generate_features(times, values, errors,
                          ['flux_percentile_ratio_mid20'])
    npt.assert_allclose(
        f['flux_percentile_ratio_mid20'],
        np.diff(np.percentile(w_m2, [40, 60])) /
        np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors,
                          ['flux_percentile_ratio_mid35'])
    npt.assert_allclose(
        f['flux_percentile_ratio_mid35'],
        np.diff(np.percentile(w_m2, [32.5, 67.5])) /
        np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors,
                          ['flux_percentile_ratio_mid50'])
    npt.assert_allclose(
        f['flux_percentile_ratio_mid50'],
        np.diff(np.percentile(w_m2, [25, 75])) /
        np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors,
                          ['flux_percentile_ratio_mid65'])
    npt.assert_allclose(
        f['flux_percentile_ratio_mid65'],
        np.diff(np.percentile(w_m2, [17.5, 82.5])) /
        np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors,
                          ['flux_percentile_ratio_mid80'])
    npt.assert_allclose(
        f['flux_percentile_ratio_mid80'],
        np.diff(np.percentile(w_m2, [10, 90])) /
        np.diff(np.percentile(w_m2, [5, 95])))
Пример #7
0
def test_delta_t_hist():
    """Test histogram of all time lags."""
    times, values, errors = irregular_random(500)
    delta_ts = [pair[1] - pair[0] for pair in itertools.combinations(times, 2)]
    nbins = 50
    bins = np.linspace(0, max(times) - min(times), nbins + 1)
    npt.assert_allclose(cf.delta_t_hist(times, nbins),
                        np.histogram(delta_ts, bins=bins)[0],
                        atol=2)
Пример #8
0
def test_normalize_hist():
    """Test normalization of histogram."""
    times, values, errors = irregular_random(500)
    delta_ts = [pair[1] - pair[0] for pair in itertools.combinations(times, 2)]
    nbins = 50
    bins = np.linspace(0, max(times) - min(times), nbins + 1)
    nhist = cf.normalize_hist(cf.delta_t_hist(times, nbins),
                              max(times) - min(times))
    npt.assert_allclose(nhist, np.histogram(delta_ts, bins=bins,
                                            density=True)[0], atol=0.01)
Пример #9
0
def test_qso_features():
    """Test features which measure fit of QSO model.

    Reference values are hard-coded values from previous implementation; not sure
    of examples with a closed-form solution.
    """
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors,
                          ['qso_log_chi2_qsonu', 'qso_log_chi2nuNULL_chi2nu'])
    npt.assert_allclose(f['qso_log_chi2_qsonu'], 6.9844064754)
    npt.assert_allclose(f['qso_log_chi2nuNULL_chi2nu'], -0.456526327522)
Пример #10
0
def test_qso_features():
    """Test features which measure fit of QSO model.

    Reference values are hard-coded values from previous implementation; not sure
    of examples with a closed-form solution.
    """
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors,
            ['qso_log_chi2_qsonu', 'qso_log_chi2nuNULL_chi2nu'])
    npt.assert_allclose(f['qso_log_chi2_qsonu'], 6.9844064754)
    npt.assert_allclose(f['qso_log_chi2nuNULL_chi2nu'], -0.456526327522)
Пример #11
0
def test_normalize_hist():
    """Test normalization of histogram."""
    times, values, errors = irregular_random(500)
    delta_ts = [pair[1] - pair[0] for pair in itertools.combinations(times, 2)]
    nbins = 50
    bins = np.linspace(0, max(times) - min(times), nbins + 1)
    nhist = cf.normalize_hist(cf.delta_t_hist(times, nbins),
                              max(times) - min(times))
    npt.assert_allclose(nhist,
                        np.histogram(delta_ts, bins=bins, density=True)[0],
                        atol=0.01)
Пример #12
0
def test_weighted_average():
    """Test weighted average and distance from weighted average features."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['weighted_average'])
    weighted_avg = np.average(values, weights=1. / (errors**2))
    weighted_var = np.average((values - weighted_avg)**2,
                              weights=1. / (errors**2))
    npt.assert_allclose(f['weighted_average'], weighted_avg)

    dists_from_weighted_avg = values - weighted_avg
    stds_from_weighted_avg = (dists_from_weighted_avg / np.sqrt(weighted_var))

    f = generate_features(times, values, errors, ['percent_beyond_1_std'])
    npt.assert_equal(f['percent_beyond_1_std'],
                     np.mean(stds_from_weighted_avg > 1.))
Пример #13
0
def test_weighted_average():
    """Test weighted average and distance from weighted average features."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['weighted_average'])
    weighted_avg = np.average(values, weights=1. / (errors**2))
    weighted_var = np.average((values - weighted_avg)**2,
                              weights=1. / (errors**2))
    npt.assert_allclose(f['weighted_average'], weighted_avg)

    dists_from_weighted_avg = values - weighted_avg
    stds_from_weighted_avg = (dists_from_weighted_avg /
            np.sqrt(weighted_var))

    f = generate_features(times, values, errors,
                                      ['percent_beyond_1_std'])
    npt.assert_equal(f['percent_beyond_1_std'], np.mean(stds_from_weighted_avg > 1.))
Пример #14
0
def test_stetson():
    """Test Stetson variability features."""
    times, values, errors = irregular_random(size=201)
    f = generate_features(times, values, errors, ['stetson_j'])
    # Stetson mean approximately equal to standard mean for large inputs
    dists = np.sqrt(float(len(values)) / (len(values) - 1.)) * (values - np.mean(values)) / 0.1
    npt.assert_allclose(f['stetson_j'],
                        np.mean(np.sign(dists**2-1)*np.sqrt(np.abs(dists**2-1))),
                        rtol=1e-2)
    # Stetson_j should be somewhat close to (scaled) variance for normal data
    npt.assert_allclose(f['stetson_j']*0.1, np.var(values), rtol=2e-1)
    # Hard-coded original value
    npt.assert_allclose(f['stetson_j'], 7.591347175195703)

    f = generate_features(times, values, errors, ['stetson_k'])
    npt.assert_allclose(f['stetson_k'], 1./0.798 * np.mean(np.abs(dists)) / np.sqrt(np.mean(dists**2)), rtol=5e-4)
    # Hard-coded original value
    npt.assert_allclose(f['stetson_k'], 1.0087218792719013)
Пример #15
0
def test_amplitude():
    """Test features related to amplitude/magnitude percentiles."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['amplitude'])
    npt.assert_allclose(f['amplitude'], (max(values) - min(values)) /2.)

    f = generate_features(times, values, errors, ['percent_amplitude'])
    max_scaled = 10**(-0.4 * max(values))
    min_scaled = 10**(-0.4 * min(values))
    med_scaled = 10**(-0.4 * np.median(values))
    peak_from_median = max(abs((max_scaled - med_scaled) / med_scaled),
            abs((min_scaled - med_scaled)) / med_scaled)
    npt.assert_allclose(f['percent_amplitude'], peak_from_median, rtol=5e-4)

    f = generate_features(times, values, errors, ['percent_difference_flux_percentile'])
    band_offset = 13.72
    w_m2 = 10**(-0.4*(values+band_offset)-3)  # 1 erg/s/cm^2 = 10^-3 w/m^2
    npt.assert_allclose(f['percent_difference_flux_percentile'], np.diff(
        np.percentile(w_m2, [5, 95])) / np.median(w_m2))

    f = generate_features(times, values, errors, ['flux_percentile_ratio_mid20'])
    npt.assert_allclose(f['flux_percentile_ratio_mid20'],
                            np.diff(np.percentile(w_m2, [40, 60])) /
                            np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors, ['flux_percentile_ratio_mid35'])
    npt.assert_allclose(f['flux_percentile_ratio_mid35'],
                            np.diff(np.percentile(w_m2, [32.5, 67.5])) /
                            np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors, ['flux_percentile_ratio_mid50'])
    npt.assert_allclose(f['flux_percentile_ratio_mid50'],
                            np.diff(np.percentile(w_m2, [25, 75])) /
                            np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors, ['flux_percentile_ratio_mid65'])
    npt.assert_allclose(f['flux_percentile_ratio_mid65'],
                            np.diff(np.percentile(w_m2, [17.5, 82.5])) /
                            np.diff(np.percentile(w_m2, [5, 95])))

    f = generate_features(times, values, errors, ['flux_percentile_ratio_mid80'])
    npt.assert_allclose(f['flux_percentile_ratio_mid80'],
                            np.diff(np.percentile(w_m2, [10, 90])) /
                            np.diff(np.percentile(w_m2, [5, 95])))
Пример #16
0
def test_stetson():
    """Test Stetson variability features."""
    times, values, errors = irregular_random(size=201)
    f = generate_features(times, values, errors, ['stetson_j'])
    # Stetson mean approximately equal to standard mean for large inputs
    dists = np.sqrt(float(len(values)) /
                    (len(values) - 1.)) * (values - np.mean(values)) / 0.1
    npt.assert_allclose(
        f['stetson_j'],
        np.mean(np.sign(dists**2 - 1) * np.sqrt(np.abs(dists**2 - 1))),
        rtol=1e-2)
    # Stetson_j should be somewhat close to (scaled) variance for normal data
    npt.assert_allclose(f['stetson_j'] * 0.1, np.var(values), rtol=2e-1)
    # Hard-coded original value
    npt.assert_allclose(f['stetson_j'], 7.591347175195703)

    f = generate_features(times, values, errors, ['stetson_k'])
    npt.assert_allclose(f['stetson_k'],
                        1. / 0.798 * np.mean(np.abs(dists)) /
                        np.sqrt(np.mean(dists**2)),
                        rtol=5e-4)
    # Hard-coded original value
    npt.assert_allclose(f['stetson_k'], 1.0087218792719013)
Пример #17
0
def test_std():
    """Test standard deviation feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['std'])
    npt.assert_allclose(f['std'], np.std(values))
Пример #18
0
def test_skew():
    """Test statistical skew feature."""
    from scipy import stats
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['skew'])
    npt.assert_allclose(f['skew'], stats.skew(values))
Пример #19
0
def test_max_slope():
    """Test maximum slope feature, which finds the INDEX of the largest slope."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['max_slope'])
    slopes = np.diff(values) / np.diff(times)
    npt.assert_allclose(f['max_slope'], np.max(np.abs(slopes)))
Пример #20
0
def test_min():
    """Test minimum value feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['minimum'])
    npt.assert_equal(f['minimum'], min(values))
Пример #21
0
def test_median():
    """Test median value feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['median'])
    npt.assert_allclose(f['median'], np.median(values))
Пример #22
0
def test_median_absolute_deviation():
    """Test median absolute deviation (from the median) feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['median_absolute_deviation'])
    npt.assert_allclose(f['median_absolute_deviation'],
                        np.median(np.abs(values - np.median(values))))
Пример #23
0
def test_median_absolute_deviation():
    """Test median absolute deviation (from the median) feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['median_absolute_deviation'])
    npt.assert_allclose(f['median_absolute_deviation'], np.median(np.abs(values -
        np.median(values))))
Пример #24
0
def test_max_slope():
    """Test maximum slope feature, which finds the INDEX of the largest slope."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['max_slope'])
    slopes = np.diff(values) / np.diff(times)
    npt.assert_allclose(f['max_slope'], np.max(np.abs(slopes)))
Пример #25
0
def test_median():
    """Test median value feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['median'])
    npt.assert_allclose(f['median'], np.median(values))
Пример #26
0
def test_min():
    """Test minimum value feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['minimum'])
    npt.assert_equal(f['minimum'], min(values))
Пример #27
0
def test_skew():
    """Test statistical skew feature."""
    from scipy import stats
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['skew'])
    npt.assert_allclose(f['skew'], stats.skew(values))
Пример #28
0
def test_std():
    """Test standard deviation feature."""
    times, values, errors = irregular_random()
    f = generate_features(times, values, errors, ['std'])
    npt.assert_allclose(f['std'], np.std(values))