hippke / tls

Transit Least Squares: An optimized transit-fitting algorithm to search for periodic transits of small planets
MIT License
48 stars 25 forks source link

Bug running multiprocessing. #98

Closed bl06 closed 2 years ago

bl06 commented 2 years ago

Hi Michael

After successful installation on MacOS, I simply try to run the tutorial 1 and it fails while running on multiprocess. See the message below. It seems the " if name == 'main':" is missing on the main program ?

Cheers Lionel.

    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

Creating model cache for 31 durations Searching 480 data points, 715 periods from 0.601 to 5.0 days Using all 4 CPU threads 0%| | 0/715 periods | 00:00<?

hippke commented 2 years ago

Hey, sorry you are having trouble. This is the first bug report with respect to multiprocessing, and many people use it, so it appears that it's got something to do with your machine. Can you please:

bl06 commented 2 years ago

I have old MacOs (intel core i7, Catalina) and Python 3.8.12

I am not sure where to specify the use_threads=1. is it in the calling : model = transitleastsquares(time, flux, use_threads=1) ? if so then the code crashes : TypeError: init() got an unexpected keyword argument 'use_threads'

hippke commented 2 years ago

Please try:

import numpy
import batman
from transitleastsquares import transitleastsquares

if __name__ == "__main__":
    print("Starting test: synthetic...", end="")

    numpy.random.seed(seed=0)  # reproducibility
    # Create test data
    start = 48
    days = 365.25 * 3
    samples_per_day = 12  # 48
    samples = int(days * samples_per_day)  # 48
    t = numpy.linspace(start, start + days, samples)

    # Use batman to create transits
    ma = batman.TransitParams()
    ma.t0 = (
        start + 20
    )  # time of inferior conjunction; first transit is X days after start
    ma.per = 365.25  # orbital period
    ma.rp = 6371 / 696342  # 6371 planet radius (in units of stellar radii)
    ma.a = 217  # semi-major axis (in units of stellar radii)
    ma.inc = 90  # orbital inclination (in degrees)
    ma.ecc = 0  # eccentricity
    ma.w = 90  # longitude of periastron (in degrees)
    ma.u = [0.5]  # limb darkening coefficients
    ma.limb_dark = "linear"  # limb darkening model
    m = batman.TransitModel(ma, t)  # initializes model
    original_flux = m.light_curve(ma)  # calculates light curve

    # Create noise and merge with flux
    ppm = 5
    stdev = 10 ** -6 * ppm
    noise = numpy.random.normal(0, stdev, int(samples))
    y = original_flux + noise
    y[1] = numpy.nan
    model = transitleastsquares(t, y)
    results = model.power(
        period_min=360,
        period_max=370,
        transit_depth_min=10 * 10 ** -6,
        oversampling_factor=5,
        duration_grid_step=1.02, 
        verbose=True,
        use_threads=1
    )

    numpy.testing.assert_almost_equal(results.chi2_min, 8831.654060613922, decimal=5)
    numpy.testing.assert_almost_equal(
        results.chi2red_min, 0.6719152511118321, decimal=5
    )

    numpy.testing.assert_almost_equal(
        results.period_uncertainty, 0.216212529678387, decimal=5
    )
    numpy.testing.assert_equal(results.per_transit_count[0], 7)
    numpy.testing.assert_equal(len(results.transit_times), 3)
    numpy.testing.assert_almost_equal(results.period, 365.2582192473641, decimal=5)
    numpy.testing.assert_almost_equal(
        results.transit_times[0], 68.00349264912924, decimal=5
    )
bl06 commented 2 years ago

that works

Starting test: synthetic...Transit Least Squares TLS 1.0.30 (17 Nov 2021) Creating model cache for 133 durations Searching 13148 data points, 278 periods from 360.012 to 369.983 days Using 1 of 4 CPU threads 100%|█████████████████████████████████████████████████████████████████████████████████████████████| 278/278 periods | 00:06<00:00 Searching for best T0 for period 365.25822 days 100%|████████████████████████████████████████████████████████████████████████████████████| 13148/13148 [00:03<00:00, 3954.28it/s]

hippke commented 2 years ago

OK great! So at least you can use TLS now. What happens if you just change use_threads=2 in the script?

bl06 commented 2 years ago

You script given above works with use_threads=1 or 2 or 3 or 4 ! however the script in the tutorial 1 runs only if results = model.power(use_threads=1)

bl06 commented 2 years ago

ok. I found it. If I add if name == "main":

at the begging of the code of Tutorial 1, then all the use_treads=1 or 2 or 3 or 4 work.

bl06 commented 2 years ago

Thanks for quick answer and thanks a lot for making this code available !!