Example #1
0
def main():
    # Number of trials.
    n = 10

    # Other parameters.
    baseline = 90.  # days
    minutes_per_day = 24. * 60.
    signal_to_noise = 10000.
    nsamples = np.ceil(baseline * minutes_per_day)
    segsize, mindur, maxdur, nbins = (2., 0.01, 0.5, 1000)

    # To make it deterministic, seed the PRNG.
    np.random.seed(4)

    period = np.random.uniform(segsize, 30.)  # days
    duration = np.random.uniform(1. / 24., 5. / 24.)  # days
    depth = np.random.uniform(-0.01, -0.5)
    phase = 0.5

    # Create logarithmically spaced numbers of lightcurves.
    num_lightcurves = np.logspace(0., 3., n).astype('int64')

    # Allocate memory to store results.
    cython_times = np.zeros((n, ), dtype='float64')

    for i in xrange(n):
        # Print a status update message.
        print 'BENCHMARK: # lightcurves =', num_lightcurves[
            i], '/', i + 1, 'of', n, '...'

        for j in xrange(num_lightcurves[i]):
            # Simulate the lightcurve.
            time, flux, fluxerr, _, _, _ = simulate_box_lightcurve(
                period, duration, depth, phase, signal_to_noise, nsamples,
                baseline)

            cython_start = clock()
            out_cython = bls_pulse_cython(time,
                                          flux,
                                          fluxerr,
                                          nbins,
                                          segsize,
                                          mindur,
                                          maxdur,
                                          detrend_order=0,
                                          direction=0)
            cython_end = clock()

            cython_times[i] += cython_end - cython_start

    np.savez('unittests/benchmark2.npz',
             num_lightcurves=num_lightcurves,
             cython_times=cython_times)
    __generate_figure()
Example #2
0
def main():
    # Number of trials.
    n = 10

    # Other parameters.
    baseline = 90.      # days
    minutes_per_day = 24. * 60.
    signal_to_noise = 10000.
    nsamples = np.ceil(baseline * minutes_per_day)
    segsize, mindur, maxdur, nbins = (2., 0.01, 0.5, 1000)

    # To make it deterministic, seed the PRNG.
    np.random.seed(4)

    period = np.random.uniform(segsize, 30.)            # days
    duration = np.random.uniform(1. / 24., 5. / 24.)    # days
    depth = np.random.uniform(-0.01, -0.5)
    phase = 0.5

    # Create logarithmically spaced numbers of lightcurves.
    num_lightcurves = np.logspace(0., 3., n).astype('int64')

    # Allocate memory to store results.
    cython_times = np.zeros((n,), dtype='float64')

    for i in xrange(n):
        # Print a status update message.
        print 'BENCHMARK: # lightcurves =', num_lightcurves[i], '/', i + 1, 'of', n, '...'

        for j in xrange(num_lightcurves[i]):
            # Simulate the lightcurve.
            time, flux, fluxerr, _, _, _ = simulate_box_lightcurve(period,
                duration, depth, phase, signal_to_noise, nsamples, baseline)

            cython_start = clock()
            out_cython = bls_pulse_cython(time, flux, fluxerr, nbins, segsize, mindur, maxdur,
                detrend_order=0, direction=0)
            cython_end = clock()

            cython_times[i] += cython_end - cython_start

    np.savez('unittests/benchmark2.npz', num_lightcurves=num_lightcurves, cython_times=cython_times)
    __generate_figure()
Example #3
0
def main(err_on_fail=True, allow_straddling=True, ofile=None, mode='python'):
    if ofile is not None:
        ofile.write('#\tMeas. mid.\tAct. mid.\tMeas. dpth.\tAct. dpth.\tMeas. '
            'dur.\tAct. dur\n')

    # Define absolute/relative tolerances.
    midtime_atol = 0.1
    duration_rtol = 0.1
    depth_rtol = 0.1

    # How many lightcurves to simulate.
    n = 10

    # Other parameters.
    minutes_per_day = 24. * 60.
    signal_to_noise, baseline = (1.e5, 90.)
    nsamples = np.ceil(baseline * minutes_per_day)
    segsize, mindur, maxdur, nbins = (2., 0.01, 0.5, 1000)

    # To make it deterministic, seed the PRNG.
    np.random.seed(4)

    period_list = np.random.uniform(segsize, 30., size=n)           # days
    duration_list = np.random.uniform(1. / 24., 5. / 24., size=n)   # days
    depth_list = np.random.uniform(-0.01, -0.5, size=n)
    phase_list = np.random.uniform(0., 1., size=n)

    for i, p, du, dp, ph in zip(np.arange(n), period_list, duration_list,
    depth_list, phase_list):
        # Print a status update message.
        print 'TEST_BLS_PULSE: Test case', i + 1, 'of', n, '...'

        # Simulate the lightcurve.
        time, flux, fluxerr, duration, depth, midtime = \
            simulate_box_lightcurve(p, du, dp, ph, signal_to_noise, nsamples,
                baseline)

        if mode == 'python':
            out = bls_pulse_python(time, flux, fluxerr, nbins, segsize, mindur,
                maxdur, detrend_order=0, direction=0)
        elif mode == 'vec':
            out = bls_pulse_vec(time, flux, fluxerr, nbins, segsize, mindur,
                maxdur, detrend_order=0, direction=0)
        elif mode == 'cython':
            dtime, dflux, dfluxerr, dsamples, segstart, segend = \
                bin_and_detrend(time, flux, fluxerr, nbins, segsize,
                    detrend_order=0)
            out = bls_pulse_cython(dtime, dflux, dfluxerr, dsamples, nbins,
                segsize, mindur, maxdur, direction=0)
        else:
            raise ValueError('Invalid test mode: %s' % mode)

        bls_du = out['duration'].ravel()
        bls_dp = out['depth'].ravel()
        bls_mid = out['midtime'].ravel()

        for j in xrange(len(midtime)):
            ndx = np.nanargmin(np.absolute(midtime[j] - bls_mid))

            if ofile:
                ofile.write('%d\t%f\t%f\t%f\t%f\t%f\t%f\n' % (j, bls_mid[ndx],
                    midtime[j], bls_dp[ndx], depth[j], bls_du[ndx],
                    duration[j]))

            if is_straddling(midtime[j], duration[j], segsize, time):
                print '    Transit %02d.....PASS (straddling)' % j
                continue

            try:
                diff_midtime = np.absolute(midtime[j] - bls_mid[ndx])
                diff_depth = np.absolute(depth[j] - bls_dp[ndx])
                diff_duration = np.absolute(duration[j] - bls_du[ndx])

                errstring = '    Transit %02d.....FAIL\n' % j

                if diff_midtime > midtime_atol:
                    errstring += 'MIDTIME: Expected ' + str(midtime[j]) + \
                        ', measured ' + str(bls_mid[ndx]) + ', diff. ' + \
                        str(diff_midtime) + ', allowed diff. ' + \
                        str(midtime_atol)
                    print errstring
                    raise RuntimeError
                #elif abs(diff_depth / depth[j]) > depth_rtol:
                #    errstring += 'DEPTH: Expected ' + str(depth[j]) + \
                #        ', measured ' + str(bls_dp[ndx]) + ', rel. diff. ' + \
                #        str(diff_depth / depth[j] * 100) + \
                #        '%, allowed rel. diff. ' + str(depth_rtol * 100) + '%'
                #    print errstring
                #    raise RuntimeError
                elif abs(diff_duration / duration[j]) > duration_rtol:
                    errstring += 'DURATION: Expected ' + str(duration[j]) + \
                        ', measured ' + str(bls_du[ndx]) + ', rel. diff. ' + \
                        str(diff_duration / duration[j] * 100) + \
                        '%, allowed diff. ' + str(duration_rtol * 100) + '%'
                    print errstring
                    raise RuntimeError
                else:
                    # All values within relative or absolute tolerances.
                    print '    Transit %02d.....PASS' % j
            except RuntimeError:
                if err_on_fail:
                    sys.exit(1)
Example #4
0
def main():
    # Number of baselines to try.
    n = 10

    # Other parameters.
    minutes_per_day = 24. * 60.
    signal_to_noise = 10000.
    segsize, mindur, maxdur, nbins = (2., 0.01, 0.5, 1000)

    # To make it deterministic, seed the PRNG.
    np.random.seed(4)

    duration = np.random.uniform(1. / 24., 5. / 24.)  # days
    depth = np.random.uniform(-0.01, -0.5)
    phase = 0.5

    # Create logarithmically spaced baselines.
    baselines = np.logspace(1., 3., n)

    # Allocate memory to store results.
    python_times = np.empty_like(baselines)
    vec_times = np.empty_like(baselines)
    cython_times = np.empty_like(baselines)

    for i in xrange(n):
        # Print a status update message.
        print 'BENCHMARK: Baseline =', baselines[i], '/', i + 1, 'of', n, '...'

        # Simulate the lightcurve.
        nsamples = np.ceil(baselines[i] * minutes_per_day)
        period = np.random.uniform(segsize, baselines[i])  # days
        time, flux, fluxerr, _, _, _ = simulate_box_lightcurve(
            period, duration, depth, phase, signal_to_noise, nsamples,
            baselines[i])

        sys.stdout.write('  Running Python test...')
        sys.stdout.flush()
        python_start = clock()
        out_python = bls_pulse_python(time,
                                      flux,
                                      fluxerr,
                                      nbins,
                                      segsize,
                                      mindur,
                                      maxdur,
                                      detrend_order=0,
                                      direction=0)
        python_end = clock()
        sys.stdout.write(' done.\n')
        sys.stdout.flush()

        sys.stdout.write('  Running vectorized test...')
        sys.stdout.flush()
        vec_start = clock()
        out_vec = bls_pulse_vec(time,
                                flux,
                                fluxerr,
                                nbins,
                                segsize,
                                mindur,
                                maxdur,
                                detrend_order=0,
                                direction=0)
        vec_end = clock()
        sys.stdout.write(' done.\n')
        sys.stdout.flush()

        sys.stdout.write('  Running Cython test...')
        sys.stdout.flush()
        cython_start = clock()
        out_cython = bls_pulse_cython(time,
                                      flux,
                                      fluxerr,
                                      nbins,
                                      segsize,
                                      mindur,
                                      maxdur,
                                      detrend_order=0,
                                      direction=0)
        cython_end = clock()
        sys.stdout.write(' done.\n')
        sys.stdout.flush()

        python_times[i] = python_end - python_start
        vec_times[i] = vec_end - vec_start
        cython_times[i] = cython_end - cython_start

    np.savez('unittests/benchmark.npz',
             baselines=baselines,
             python_times=python_times,
             vec_times=vec_times,
             cython_times=cython_times)
    __generate_figure()
Example #5
0
def main():
    # Number of baselines to try.
    n = 10

    # Other parameters.
    minutes_per_day = 24. * 60.
    signal_to_noise = 10000.
    segsize, mindur, maxdur, nbins = (2., 0.01, 0.5, 1000)

    # To make it deterministic, seed the PRNG.
    np.random.seed(4)

    duration = np.random.uniform(1. / 24., 5. / 24.)    # days
    depth = np.random.uniform(-0.01, -0.5)
    phase = 0.5

    # Create logarithmically spaced baselines.
    baselines = np.logspace(1., 3., n)

    # Allocate memory to store results.
    python_times = np.empty_like(baselines)
    vec_times = np.empty_like(baselines)
    cython_times = np.empty_like(baselines)

    for i in xrange(n):
        # Print a status update message.
        print 'BENCHMARK: Baseline =', baselines[i], '/', i + 1, 'of', n, '...'

        # Simulate the lightcurve.
        nsamples = np.ceil(baselines[i] * minutes_per_day)
        period = np.random.uniform(segsize, baselines[i])   # days
        time, flux, fluxerr, _, _, _ = simulate_box_lightcurve(period,
            duration, depth, phase, signal_to_noise, nsamples, baselines[i])

        sys.stdout.write('  Running Python test...')
        sys.stdout.flush()
        python_start = clock()
        out_python = bls_pulse_python(time, flux, fluxerr, nbins, segsize, mindur, maxdur,
            detrend_order=0, direction=0)
        python_end = clock()
        sys.stdout.write(' done.\n')
        sys.stdout.flush()

        sys.stdout.write('  Running vectorized test...')
        sys.stdout.flush()
        vec_start = clock()
        out_vec = bls_pulse_vec(time, flux, fluxerr, nbins, segsize, mindur, maxdur,
            detrend_order=0, direction=0)
        vec_end = clock()
        sys.stdout.write(' done.\n')
        sys.stdout.flush()

        sys.stdout.write('  Running Cython test...')
        sys.stdout.flush()
        cython_start = clock()
        out_cython = bls_pulse_cython(time, flux, fluxerr, nbins, segsize, mindur, maxdur,
            detrend_order=0, direction=0)
        cython_end = clock()
        sys.stdout.write(' done.\n')
        sys.stdout.flush()

        python_times[i] = python_end - python_start
        vec_times[i] = vec_end - vec_start
        cython_times[i] = cython_end - cython_start

    np.savez('unittests/benchmark.npz', baselines=baselines, python_times=python_times,
        vec_times=vec_times, cython_times=cython_times)
    __generate_figure()
Example #6
0
def main(err_on_fail=True, allow_straddling=True, ofile=None, mode='python'):
    if ofile is not None:
        ofile.write(
            '#\tMeas. mid.\tAct. mid.\tMeas. dpth.\tAct. dpth.\tMeas. dur.\tAct. dur\n'
        )

    # Define absolute/relative tolerances.
    midtime_atol = 0.1
    duration_rtol = 0.1
    depth_rtol = 0.1

    # How many lightcurves to simulate.
    n = 10

    # Other parameters.
    minutes_per_day = 24. * 60.
    signal_to_noise, baseline = (10000., 90.)
    nsamples = np.ceil(baseline * minutes_per_day)
    segsize, mindur, maxdur, nbins = (2., 0.01, 0.5, 1000)

    # To make it deterministic, seed the PRNG.
    np.random.seed(4)

    period_list = np.random.uniform(segsize, 30., size=n)  # days
    duration_list = np.random.uniform(1. / 24., 5. / 24., size=n)  # days
    depth_list = np.random.uniform(-0.01, -0.5, size=n)
    phase_list = np.random.uniform(0., 1., size=n)

    for i, p, du, dp, ph in zip(np.arange(n), period_list, duration_list,
                                depth_list, phase_list):
        # Print a status update message.
        print 'TEST_BLS_PULSE: Test case', i + 1, 'of', n, '...'

        # Simulate the lightcurve.
        time, flux, fluxerr, duration, depth, midtime = simulate_box_lightcurve(
            p, du, dp, ph, signal_to_noise, nsamples, baseline)

        if mode == 'python':
            out = bls_pulse_python(time,
                                   flux,
                                   fluxerr,
                                   nbins,
                                   segsize,
                                   mindur,
                                   maxdur,
                                   detrend_order=0,
                                   direction=0)
        elif mode == 'vec':
            out = bls_pulse_vec(time,
                                flux,
                                fluxerr,
                                nbins,
                                segsize,
                                mindur,
                                maxdur,
                                detrend_order=0,
                                direction=0)
        elif mode == 'cython':
            out = bls_pulse_cython(time,
                                   flux,
                                   fluxerr,
                                   nbins,
                                   segsize,
                                   mindur,
                                   maxdur,
                                   detrend_order=0,
                                   direction=0)
        else:
            raise ValueError('Invalid test mode: %s' % mode)

        bls_du = out['duration'].ravel()
        bls_dp = out['depth'].ravel()
        bls_mid = out['midtime'].ravel()

        for j in xrange(len(midtime)):
            ndx = np.nanargmin(np.absolute(midtime[j] - bls_mid))

            if ofile:
                ofile.write('%d\t%f\t%f\t%f\t%f\t%f\t%f\n' %
                            (j, bls_mid[ndx], midtime[j], bls_dp[ndx],
                             depth[j], bls_du[ndx], duration[j]))

            if is_straddling(midtime[j], duration[j], segsize, time):
                print '    Transit %02d.....PASS (straddling)' % j
                continue

            try:
                diff_midtime = np.absolute(midtime[j] - bls_mid[ndx])
                diff_depth = np.absolute(depth[j] - bls_dp[ndx])
                diff_duration = np.absolute(duration[j] - bls_du[ndx])

                errstring = '    Transit %02d.....FAIL\n' % j

                if diff_midtime > midtime_atol:
                    errstring += 'MIDTIME: Expected ' + str(midtime[j]) + ', measured ' + \
                        str(bls_mid[ndx]) + ', diff. ' + str(diff_midtime) + \
                        ', allowed diff. ' + str(midtime_atol)
                    print errstring
                    raise RuntimeError
                elif diff_depth / depth[j] > depth_rtol:
                    errstring += 'DEPTH: Expected ' + str(depth[j]) + ', measured ' + \
                        str(bls_dp[ndx]) + ', rel. diff. ' + \
                        str(diff_depth / depth[j] * 100) + '%, allowed rel. diff. ' + \
                        str(depth_rtol * 100) + '%'
                    print errstring
                    raise RuntimeError
                elif diff_duration / duration[j] > duration_rtol:
                    errstring += 'DURATION: Expected ' + str(duration[j]) + ', measured ' + \
                        str(bls_du[ndx]) + ', rel. diff. ' + \
                        str(diff_duration / duration[j] * 100) + '%, allowed diff. ' + \
                        str(duration_rtol * 100) + '%'
                    print errstring
                    raise RuntimeError
                else:
                    # All values within relative or absolute tolerances.
                    print '    Transit %02d.....PASS' % j
            except RuntimeError:
                if err_on_fail:
                    sys.exit(1)