exoplanet-dev / exoplanet

Fast & scalable MCMC for all your exoplanet needs!
https://docs.exoplanet.codes
MIT License
206 stars 52 forks source link

TTVOrbit for StarryLightCurve #49

Closed jiayindong closed 4 years ago

jiayindong commented 5 years ago

Thanks for developing the exoplanet package!

I wonder is the TTVOrbit option on? If so, does the StarryLightCurve work properly on it?

orbit = xo.orbits.TTVOrbit(transit_times=[1,2,3])

Here is what I tried.

orbit = xo.orbits.TTVOrbit(t0=[0],period=[10],ttvs=[1,2,3])
t = np.linspace(0, 20, 100)
u = [0.3, 0.2]
light_curve = xo.StarryLightCurve(u).get_light_curve(orbit=orbit, r=0.1, t=t, texp=0.02).eval()

Here is what I got.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-e5d5e061674b> in <module>
      2 t = np.linspace(0, 20, 100)
      3 u = [0.3, 0.2]
----> 4 light_curve = xo.StarryLightCurve(u).get_light_curve(orbit=orbit, r=0.1, t=t, texp=0.02).eval()

~/.conda/envs/env_WaLEs/lib/python3.7/site-packages/exoplanet-0.1.7.dev0-py3.7.egg/exoplanet/light_curves.py in get_light_curve(self, orbit, r, t, texp, oversample, order, use_in_transit)
     89             transit_model = tt.shape_padleft(tt.zeros_like(r), t.ndim) \
     90                 + tt.shape_padright(tt.zeros_like(t), r.ndim)
---> 91             inds = orbit.in_transit(t, r=r, texp=texp)
     92             t = t[inds]
     93 

~/.conda/envs/env_WaLEs/lib/python3.7/site-packages/exoplanet-0.1.7.dev0-py3.7.egg/exoplanet/orbits/keplerian.py in in_transit(self, t, r, texp)
    614         # Wrap the times into time since transit
    615         hp = 0.5 * self.period
--> 616         dt = tt.mod(self._warp_times(t) - self.t0 + hp, self.period) - hp
    617 
    618         if self.ecc is None:

~/.conda/envs/env_WaLEs/lib/python3.7/site-packages/exoplanet-0.1.7.dev0-py3.7.egg/exoplanet/orbits/ttv.py in _warp_times(self, t)
     78         ind = tt.cast(tt.floor(delta), "int64")
     79         dt = tt.stack([ttv[tt.clip(ind[i], 0, ttv.shape[0]-1)]
---> 80                        for i, ttv in enumerate(self.ttvs)], -1)
     81         return tt.shape_padright(t) + dt

~/.conda/envs/env_WaLEs/lib/python3.7/site-packages/exoplanet-0.1.7.dev0-py3.7.egg/exoplanet/orbits/ttv.py in <listcomp>(.0)
     78         ind = tt.cast(tt.floor(delta), "int64")
     79         dt = tt.stack([ttv[tt.clip(ind[i], 0, ttv.shape[0]-1)]
---> 80                        for i, ttv in enumerate(self.ttvs)], -1)
     81         return tt.shape_padright(t) + dt

~/.conda/envs/env_WaLEs/lib/python3.7/site-packages/theano/tensor/var.py in __getitem__(self, args)
    508         # Check if the number of dimensions isn't too large.
    509         if self.ndim < index_dim_count:
--> 510             raise IndexError('too many indices for array')
    511 
    512         # Convert an Ellipsis if provided into an appropriate number of

IndexError: too many indices for array

I may just misunderstand the functions. Is there any demo code I could check using TTVOrbit on StarryLightCurve?

Thanks a lot!

dfm commented 5 years ago

This functionality has not been carefully tested -- that's also why it's not documented :) -- so use at your own risk!

That being said, the bug here is that the transit_times or ttvs arguments must be lists of arrays (or list of tensors, or list of lists), one for each body. You could change it to transit_times=[[1, 2, 3]] and it should work.

jiayindong commented 5 years ago

Cool, thanks!

gjgilbert commented 5 years ago

@jiayindong @dfm Just a heads up that even using the list-of-arrays workaround, I've been running into issues with TTVOrbit. For some reason, not all the transits are being generated, or are perhaps offset by an unusually large TTV amplitude (see top figure below). The reference planet here is Kepler-18c (P=7.6 days), and should have more or less evenly spaced transits, with a TTV amplitude of ~10 min. As you can see, many transits are missing.

Of course, it's possible I've made a mistake in my implementation somewhere, although everything seems to work fine when I use KeplerianOrbit (bottom figure).

import numpy as np
import matplotlib.pyplot as plt
import exoplanet as exo

def calculate_model_flux(pshape, Rstar, tts, time_stamps, cadences):
    '''
    pshape: array of transit parameters -- [T0, P, rp, zeta, b^2, esinw, ecosw, u1, u2]
    Rstar: stellar radius [solar radii]
    tts: list of transit times
    time_stamps: list of time stamps (one per transit)
    cadences: cadence of each transit
    '''
    # check that vector lengths are consistent
    if len(time_stamps) != len(tts):
        raise ValueError('inconsistent input lengths')
    if len(cadences) != len(tts):
        raise ValueError('inconsistent input lengths')

    # convert pshape parameters to a form more easily used by starry
    T0, P, rp, a, inc, ecc, w, u1, u2 = pshape_to_pstarry(pshape, Rstar)
    b = np.sqrt(pshape[4])

    # setup exo.StarryLightCurve() object
    exoSLC = exo.StarryLightCurve([u1,u2])

    orbit = exo.orbits.TTVOrbit(transit_times=[tts], , a=a, r_star=Rstar)    

    model = exoSLC.get_light_curve(orbit=orbit, r=rp, t=np.hstack(time_stamps)).eval()

    return model

# 'planets' is a list of objects which hold relevant planet parameters, ttv info, etc    
p = planets[0]

tts = p.tts[p.quality]                      # a list of transit times
time_stamps = p.grab_stamps('time')         # a list of 'stamps' cut for 3 transit durations around each tt
flux_stamps = p.grab_stamps('flux')         # a list of correspnding flux values
cadences = p.stamp_cadence[p.quality]       # Kepler cadence of each stamp ('short' or 'long')

model = wings.calculate_model_flux(p.pshape, RSTAR, tts, time_stamps, cadences)

plt.figure(figsize=(18,6))
plt.plot(np.hstack(time_stamps), np.hstack(flux_stamps), 'k.')
plt.plot(np.hstack(time_stamps), model+1, 'orange')
plt.show()

Screen Shot 2019-07-18 at 3 48 47 PM

Screen Shot 2019-07-18 at 3 56 09 PM

dfm commented 5 years ago

Can you send a simple executable example that doesn't have the behavior that you expect and I'll take a look? Thanks!

On Thu, Jul 18, 2019 at 5:11 PM Jiayin Dong notifications@github.com wrote:

Reopened #49 https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_dfm_exoplanet_issues_49&d=DwMCaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=AvKCBU-zyph1qdf6NaatMQ&m=Ww5gFI6EIGxiLCRLoEp28hwySFMZ9G9HM-xHyvfGn-g&s=HbWs5F3btRM92qAdikA5M9mr53zeBeOz8bWwmhUrj1Q&e= .

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_dfm_exoplanet_issues_49-3Femail-5Fsource-3Dnotifications-26email-5Ftoken-3DAACVQSSFXHDMQT6XVNFVRPTQADL6ZA5CNFSM4IEWPNLKYY3PNVWWK3TUL52HS4DFWZEXG43VMVCXMZLOORHG65DJMZUWGYLUNFXW5KTDN5WW2ZLOORPWSZGOSSUAFOA-23event-2D2494038712&d=DwMCaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=AvKCBU-zyph1qdf6NaatMQ&m=Ww5gFI6EIGxiLCRLoEp28hwySFMZ9G9HM-xHyvfGn-g&s=bRtuSOZ42_zgcb6GDudHQXMx_pSfve8INlLTn6ab-uE&e=, or mute the thread https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AACVQSSIAI7QU4TGCES5USDQADL6ZANCNFSM4IEWPNLA&d=DwMCaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=AvKCBU-zyph1qdf6NaatMQ&m=Ww5gFI6EIGxiLCRLoEp28hwySFMZ9G9HM-xHyvfGn-g&s=tfnDSnQhet5kJPx1D9mP1AX2AWjgkxNfqjLL34Np3lY&e= .

-- Dan Foreman-Mackey Associate Research Scientist Flatiron Institute http://dfm.io

gjgilbert commented 5 years ago

I've uploaded my code here: https://github.com/gjgilbert/pterodactyl. Everything should be executable from the jupyter notebook. You'll need to specify a few paths in the first two cells but everything else should run without issue. The behavior in question is in the last few cells under the section "do a model fit" and the relevant function in pterodactyl_wings is "calculate_model_flux"

The lightcurve detrending takes a few minutes, so if you'd prefer me to put together a slimmed-down version of this code let me know and I'll be happy to do it.

dfm commented 5 years ago

Thanks! Yeah - it would be best if you could put together a simple standalone version that isolates the problem. I have a few ideas, but like I said this definitely hasn't been well tested so it'll take some poking!

gjgilbert commented 5 years ago

Okay, a notebook 'dfm_exoplanet_testing' is uploaded here: https://github.com/gjgilbert/pterodactyl, plus three .fits files containing relevant data for the three planets in the Kepler-18 system. All you need to do is specify which file you want to read in (keyword "FITSFILE" in the second cell). FYI, the naming convention I've used sorts the planets by orbital period, which is not necessarily their actual KOI or Kepler ID.

dfm commented 5 years ago

Hi Greg, sorry for the slow response over here! Can you make a little toy example that has the same behavior without all the surrounding infrastructure. Like one single standalone function with no dependencies or files. Then we'll be able to see more clearly where the issue is coming from. Thanks!

gjgilbert commented 5 years ago

No worries about the response time, Dan!

Here's a completely stripped-down notebook that reproduces the issue: https://github.com/gjgilbert/pterodactyl/blob/master/dfm_exoplanet_testing.ipynb

dfm commented 5 years ago

@gjgilbert: I got a chance to look at this and I worked out what was going on. I'd developed the model in the limit where the ttvs were less than the period, but here you have some transits that come more than a period late so that was messing up all the bookkeeping. I just pushed a change that looks a bit better, but there's still an issue when the transit comes >2 "periods" late. In the example you sent, one of your transits is 10 days after the previous one - are you sure that there shouldn't be another transit in there that you're missing? The way the code is set up, you need to provide a transit time for every transit (even the ones that aren't observed). I'll work on making this clearer, but let me know if it's reasonable to expect that some systems will have TTVs larger than the reference period....

dfm commented 5 years ago

Edit: change all occurrences of period to period/2 in the above comment - sorry!