pycs.spl package

Submodules

pycs.spl.multiopt module

Functions to optimize time shifts and microlensing between lcs, using spline fits.

By default the functions don’t touch the ML and sourcespline knots, it’s up to you to enable BOK iterations. And check the results by eye …

Typical order of operations :

Put smooth microlensing on the curves Fit a source to the curve without microlensing opt_magshift (does not use the source) opt_ml (does not change the source) Fit a new source, to all the curves Put a less smooth microlensing on them opt_ml_source

pycs.spl.multiopt.opt_magshift(lcs, sourcespline=None, verbose=False, trace=False)

If you don’t give any sourcespline, this is a dirty rough magshift optimization, using the median mag level (without microlensing), once for all. We don’t touch the magshift of the first curve.

If you do give a sourcespline, I’ll optimize the magshift of each curve one by one to match the spline. New : I even touch the magshift of the first curve.

We use the l1-norm of the residues, not a usual r2, to avoid local minima. Important ! This is done with the nosquare=True option to the call of r2 !

pycs.spl.multiopt.opt_source(lcs, sourcespline, dpmethod='extadj', bokit=0, bokmethod='BF', verbose=True, trace=False)

Just the source spline, without touching the ML of the lcs. At each call, I update the sourcespline with the merged lcs. The internal knots of the sourcespline stay where they are, only the external ones are ajusted.

pycs.spl.multiopt.opt_fluxshift(lcs, sourcespline, verbose=True, trace=False)

Optimizes the flux shift and the magshift of the lcs (not the first one) to get the best fit to the “sourcespline”. Does not touch the microlensing, nor the spline. So this is a building block to be used iteratively with the other optimizers. Especially of the sourcespline, as we fit here even on regions not well constrained by the spline !

The spline should typically well fit to the first curve.

pycs.spl.multiopt.opt_ml(lcs, sourcespline, bokit=0, bokmethod='BF', splflat=False, verbose=True, trace=False)

Optimizes the microlensing of the lcs (one curve after the other) so that they fit to the spline. I work with both polynomial and spline microlensing. NO YOU DONT !!! For spline micorlensing, I can do BOK iterations to move the knots.

Note

Does not touch the sourcespline at all !

But this it what makes the problem linear (except for the BOK iterations) for both splines and polynomial ML, and thus fast !

Parameters for spline ML :

Parameters:
  • bokit
  • bokeps
  • boktests
  • bokwindow
  • splflat

Parameters for poly ML :

None ! We just to a linear weighted least squares on each season ! So for poly ML, all the params above are not used at all.

We do not return anything. Returning a r2 would make no sense, as we do not touch the sourcepline !

pycs.spl.multiopt.redistribflux(lc1, lc2, sourcespline, verbose=True, maxfrac=0.2)

Redistributes flux between lc1 and lc2 (assuming these curves suffer form flux sharing), so to minimize the r2 with respect to the sourcespline. I do not touch the sourcespline, but I do modify your curves in an irreversible way !

Parameters:
  • lc1 – a lightcurve
  • lc2 – another lightcurve
  • sourcespline – the spline that the curves should try to fit to
pycs.spl.multiopt.opt_ts_powell(lcs, sourcespline, optml=True, movefirst=False, verbose=True, trace=False)

If you use this again, implement mlsplflat for optml, might be important !

Optimize the timeshifts of all four lightcurves AND the spline coeffs (but not the knots) so to get the best possible fit.

If optml is True, I will optimize the ml for every trial delays (and then optimize the source again). For this I will always start from the initial settings, I don’t cumulate the ML - source optimizations. This is required as ML - source optimizations are degenarate. If optml is false, this would not be needed, as the spline fitting is unique anyway.

Note that even if I don’t modify the sourcespline knots, I do modify the sourcespline datapoints as the time shifts move !

Improvement ideas : see if better to not optimize the first curves time shift ? Don’t try to optimize the timeshifts at the same time ? Do one after the other, with respect to the first curve ? Special function that takes a spline fit to the first curve as argument, to do this ?

-> Yes, see opt_ts_indi below !

pycs.spl.multiopt.comb(*sequences)

http://code.activestate.com/recipes/502199/ combinations of multiple sequences so you don’t have to write nested for loops

>>> from pprint import pprint as pp
>>> pp(comb(['Guido','Larry'], ['knows','loves'], ['Phyton','Purl']))
[['Guido', 'knows', 'Phyton'],
['Guido', 'knows', 'Purl'],
['Guido', 'loves', 'Phyton'],
['Guido', 'loves', 'Purl'],
['Larry', 'knows', 'Phyton'],
['Larry', 'knows', 'Purl'],
['Larry', 'loves', 'Phyton'],
['Larry', 'loves', 'Purl']]
>>> 
pycs.spl.multiopt.opt_ts_brute(lcs, sourcespline, movefirst=True, optml=False, r=2, step=1.0, verbose=True, trace=False)

If you use this again, implement mlsplflat, might be important.

Given the current delays, I will explore a hypercube (r steps in each direction on each axis) of possible time shift combinations. And choose the shifts that gave the smallest chi2.

For each trial shift, I optimize the sourcespline to fit all curves, and optionally also the ML of every curve.

This is very slow, not really used.

pycs.spl.multiopt.opt_ts_indi(lcs, sourcespline, method='fmin', crit='r2', optml=False, mlsplflat=False, brutestep=1.0, bruter=5, verbose=True, trace=False)

We shift the curves one by one so that they match to the spline, using fmin or brute force. A bit special : I do not touch the spline at all ! Hence I return no r2. Idea is to have a fast ts optimization building block.

The spline should be a shape common to the joined lcs. No need to work on copies, as we do not change the ML or spline iteratively, but only the ML -> nothing can go wrong.

Parma brutestep:
 step size, in days
Parameters:bruter – radius in number of steps

pycs.spl.topopt module

Higher level wrappers around the multiopt optimizers. These attempt to be a as multi-puropose as possible, but please understand that there is no general “optimal” way to optimize a spline fit. Depening on your data, custom optimizers might be needed. That’s what we did for the TDC.

In principle we do not define the microlensing inside of the optimizer, we really just optimize it.

The functions must return a final optimal spline. This spline object also contains its final r2, saved into the pkl, and used for plots etc.

pycs.spl.topopt.opt_rough(lcs, nit=5, shifttime=True, crit='r2', knotstep=100, stabext=300.0, stabgap=20.0, stabstep=4.0, stabmagerr=-2.0, verbose=True)

Getting close to the good delays, as fast as possible : no BOK (i.e. knot positions are not free), only brute force without optml. Indeed with optml this tends to be unstable.

Note

This function is here to correct for shift errors in the order of 20 days, hence its name.

Parameters:
  • nit – a number of iterations (ok to leave default : 5)
  • splstep – the step (in days) between the knots of the spline. 100 is default. Lower this to eg 50 if your curve has very short term intrinsic variations, increase it if your curve is very noisy.
pycs.spl.topopt.opt_fine(lcs, spline=None, nit=10, shifttime=True, crit='r2', knotstep=20, stabext=300.0, stabgap=20.0, stabstep=4.0, stabmagerr=-2.0, bokeps=10, boktests=10, bokwindow=None, redistribflux=False, splflat=True, verbose=True, trace=False, tracedir='opt_fine')

Fine approach, we assume that the timeshifts are within 2 days, and ML is optimized.

Watch out for splflat, a bit dangerous all this … Default is True for the iterations, and we release the splines only at the end. If you put it to False, spline are left free at any stage. This should be fine if you leave one curve without ML.

Parameters:spline – If this is None, I will fit my own spline, but then I expect that your lcs already overlap (i.e., ML is set) ! If you give me a spline, I will use it (in this case I disregard your splstep setting !)
pycs.spl.topopt.opt_fine2(lcs, knotstepfact=1.0, maxit=10, minchange=1.0, verbose=True)

Version 2 Generalisation of pycs.tdc.splotp.spl3, but for a random number of lcs. Assumes a really good initial time shift, does not care about inital magshift. Also, contrary to what was done for tdc.splopt, we do NOT add ML in here, but use whatever is there!

Parameters:
  • maxit – maximum number of iterations
  • minchange – minimum decrease in percent of the r2. I stop if the decrease gets smaller.

Module contents

Simultaneous spline fit technique. This subpackage contains functions to optimize lightcurves (their shifts and ML) so that they fit to a common spline.

  • multiopt contains “builing blocks” for such optimizers, and
  • topopt assembles these building blocks into big wrappers.