Esempio n. 1
0
def opt_ml(lc1, lc2, dispersionmethod, plot=False, verbose=False):
    """
	microlensing optimization.
	Sets the eventual microlensings of lc1 and lc2 so to minimize a dispersion.
	Does not shift the curves in any other way.
	
	If plot is True, it displays the steps of the optimization.
	
	@type lc1: lightcurve
	@param lc1: This one will be passed as first argument (i.e. "reference") to the dispersionmethod
	@type lc2: lightcurve
	@param lc2: The light curve that will get interpolated by the dispersionmethod.
	
	@todo: implement a better plot ...
	
	"""

    initparams = ml.multigetfreeparams([lc1, lc2])
    if verbose:
        print "Initial params : ", initparams

    if len(initparams) == 0:
        raise RuntimeError, "There are no free microlensing params to optimize !"

    if plot:
        d2valuelist = []

    def d2(params):
        ml.multisetfreeparams([lc1, lc2], params)

        ret = dispersionmethod(lc1, lc2)["d2"]

        if plot:
            d2valuelist.append(ret)

        return ret

    # http://www.scipy.org/doc/api_docs/SciPy.optimize.optimize.html#fmin_bfgs
    #put disp = 0 to make them silent...

    #optparams = spopt.fmin(d2, initparams)
    #optparams = spopt.fmin_cg(d2, initparams)
    #optparams = spopt.fmin_bfgs(d2, params, epsilon =1.0e-5)
    #optparams = spopt.fmin_bfgs(d2, params, disp=1, retall=0)
    #optparams = spopt.anneal(d2, params)

    #optparams = spopt.fmin_ncg(d2, initparams) # this guy would need a gradient.
    #optparams = spopt.fmin_bfgs(d2, initparams) # precision loss

    # [  2.89270193e-10   4.94366701e-08  -2.63101369e-04  -4.38757115e-03]
    # [  2.95294380e-10   3.64696903e-08  -2.63882092e-04  -5.66560156e-05]

    optparams = spopt.fmin_powell(
        d2, initparams, disp=int(verbose)
    )  # 312 , 2.592636, [  2.89270193e-10   4.94366701e-08  -2.63101369e-04  -4.38757115e-03]
    #optparams = spopt.fmin(d2, initparams) # 150, 2.614217
    #optparams = spopt.fmin(d2, initparams, xtol = 1.0e-10)
    #optparams = spopt.fmin_bfgs(d2, params, epsilon =1.0e-5)

    if verbose:
        print "Optimal params : ", optparams
        print repr(optparams)

    ml.multisetfreeparams([lc1, lc2], optparams)

    if plot:
        plt.semilogy(d2valuelist)
        plt.show()
Esempio n. 2
0
def dispcube(lcs, rawdispersionmethod, verbose=True, timewidth=30, timestep=1.0, optml=False, filename="dispcube.pkl"):
	"""
	3D specplot, calculates the dispersion over a cube of time-delays. And writes the result in a pickle.
	For now this is quick and dirty programming : we want only 4 lightcurves.
	
	This pickle can then be looked at with Mayavi (see example below)
	"""
	
	if len(lcs)!=4:
		raise RuntimeError, "I want 4 lightcurves."
	
	lcsc = [l.copy() for l in lcs]
	
	# We apply microlensing if we do not need it.
	for l in lcsc:
		if optml==False and l.ml != None:
			l.applyml()
	
	# We generate the list of possible couples.
	couplelist = [couple for couple in [[lc1, lc2] for lc1 in lcsc for lc2 in lcsc] if couple[0] != couple[1]]
	#for couple in couplelist:
	#	print couple[0].object, couple[1].object

	#nfree = len(lcs)-1

	
	if optml:
		pass
#		def d2value(params):
#			lc.multisettimedelays(lcsc, params[:nfree])
#			ml.multisetfreeparams(lcsc, params[nfree:])
#		
#			# optimize ml here
#		
#			d2values = np.array([rawdispersionmethod(*couple)["d2"] for couple in couplelist])
#			ret = np.mean(d2values)
#			#print ret
#			return ret
#		
		
	else:
		def d2value(delays):
			lc.multisettimedelays(lcsc, delays)
		
			d2values = np.array([rawdispersionmethod(*couple)["d2"] for couple in couplelist])
			ret = np.mean(d2values)
			#print ret
			return ret
	
	
	initparams = np.concatenate([lc.multigettimedelays(lcsc), ml.multigetfreeparams(lcsc)])
	print "Initial params : ", initparams
	
	timeshifts = np.arange(-(timewidth)*timestep/2.0, (timewidth+1)*timestep/2.0, timestep)
	cubeindexes = np.arange(timewidth + 1)
	
	print "Points to calculate :", len(timeshifts)**3
	d2cube = np.zeros((timewidth+1, timewidth+1, timewidth+1))
	
	xshifts = timeshifts + initparams[0]
	yshifts = timeshifts + initparams[1]
	zshifts = timeshifts + initparams[2]

	
	if optml==False:
		for ix in cubeindexes:
			print "Slice %i of %i" % (ix + 1, timewidth+1)
			for iy in cubeindexes:
				for iz in cubeindexes:
					d2cube[ix, iy, iz] = d2value([xshifts[ix], yshifts[iy], zshifts[iz]])
	
	
	# We do as if we used mgrid... no, this would be ogrid...
	#xshifts = xshifts.reshape(xshifts.size, 1, 1) 
	#yshifts = yshifts.reshape(1, yshifts.size, 1) 
	#zshifts = zshifts.reshape(1, 1, zshifts.size) 
	
	
	beg = -(timewidth)*timestep/2.0
	end = (timewidth+1)*timestep/2.0
	step = timestep
	
	x, y, z = np.mgrid[beg:end:step, beg:end:step, beg:end:step]
	#print x, y, z
	x += initparams[0]
	y += initparams[1]
	z += initparams[2]
	#print x, y, z
	
	util.writepickle({"lcs":lcs, "x":x, "y":y, "z":z, "d2":d2cube}, filename)
Esempio n. 3
0
def dispcube(lcs,
             rawdispersionmethod,
             verbose=True,
             timewidth=30,
             timestep=1.0,
             optml=False,
             filename="dispcube.pkl"):
    """
	3D specplot, calculates the dispersion over a cube of time-delays. And writes the result in a pickle.
	For now this is quick and dirty programming : we want only 4 lightcurves.
	
	This pickle can then be looked at with Mayavi (see example below)
	"""

    if len(lcs) != 4:
        raise RuntimeError, "I want 4 lightcurves."

    lcsc = [l.copy() for l in lcs]

    # We apply microlensing if we do not need it.
    for l in lcsc:
        if optml == False and l.ml != None:
            l.applyml()

    # We generate the list of possible couples.
    couplelist = [
        couple for couple in [[lc1, lc2] for lc1 in lcsc for lc2 in lcsc]
        if couple[0] != couple[1]
    ]
    #for couple in couplelist:
    #	print couple[0].object, couple[1].object

    #nfree = len(lcs)-1

    if optml:
        pass
#		def d2value(params):
#			lc.multisettimedelays(lcsc, params[:nfree])
#			ml.multisetfreeparams(lcsc, params[nfree:])
#
#			# optimize ml here
#
#			d2values = np.array([rawdispersionmethod(*couple)["d2"] for couple in couplelist])
#			ret = np.mean(d2values)
#			#print ret
#			return ret
#

    else:

        def d2value(delays):
            lc.multisettimedelays(lcsc, delays)

            d2values = np.array(
                [rawdispersionmethod(*couple)["d2"] for couple in couplelist])
            ret = np.mean(d2values)
            #print ret
            return ret

    initparams = np.concatenate(
        [lc.multigettimedelays(lcsc),
         ml.multigetfreeparams(lcsc)])
    print "Initial params : ", initparams

    timeshifts = np.arange(-(timewidth) * timestep / 2.0,
                           (timewidth + 1) * timestep / 2.0, timestep)
    cubeindexes = np.arange(timewidth + 1)

    print "Points to calculate :", len(timeshifts)**3
    d2cube = np.zeros((timewidth + 1, timewidth + 1, timewidth + 1))

    xshifts = timeshifts + initparams[0]
    yshifts = timeshifts + initparams[1]
    zshifts = timeshifts + initparams[2]

    if optml == False:
        for ix in cubeindexes:
            print "Slice %i of %i" % (ix + 1, timewidth + 1)
            for iy in cubeindexes:
                for iz in cubeindexes:
                    d2cube[ix, iy, iz] = d2value(
                        [xshifts[ix], yshifts[iy], zshifts[iz]])

    # We do as if we used mgrid... no, this would be ogrid...
    #xshifts = xshifts.reshape(xshifts.size, 1, 1)
    #yshifts = yshifts.reshape(1, yshifts.size, 1)
    #zshifts = zshifts.reshape(1, 1, zshifts.size)

    beg = -(timewidth) * timestep / 2.0
    end = (timewidth + 1) * timestep / 2.0
    step = timestep

    x, y, z = np.mgrid[beg:end:step, beg:end:step, beg:end:step]
    #print x, y, z
    x += initparams[0]
    y += initparams[1]
    z += initparams[2]
    #print x, y, z

    util.writepickle({
        "lcs": lcs,
        "x": x,
        "y": y,
        "z": z,
        "d2": d2cube
    }, filename)
Esempio n. 4
0
def opt_ml(lc1, lc2, dispersionmethod, plot=False, verbose=False):
	"""
	microlensing optimization.
	Sets the eventual microlensings of lc1 and lc2 so to minimize a dispersion.
	Does not shift the curves in any other way.
	
	If plot is True, it displays the steps of the optimization.
	
	@type lc1: lightcurve
	@param lc1: This one will be passed as first argument (i.e. "reference") to the dispersionmethod
	@type lc2: lightcurve
	@param lc2: The light curve that will get interpolated by the dispersionmethod.
	
	@todo: implement a better plot ...
	
	"""

	initparams = ml.multigetfreeparams([lc1, lc2])
	if verbose:
		print "Initial params : ", initparams
		
	if len(initparams) == 0:
		raise RuntimeError, "There are no free microlensing params to optimize !"
		
	if plot:
		d2valuelist = []
	
	def d2(params):
		ml.multisetfreeparams([lc1, lc2], params)
		
		ret = dispersionmethod(lc1, lc2)["d2"]
		
		if plot:
			d2valuelist.append(ret)
		
		return ret
		
	# http://www.scipy.org/doc/api_docs/SciPy.optimize.optimize.html#fmin_bfgs
	#put disp = 0 to make them silent...

	#optparams = spopt.fmin(d2, initparams)
	#optparams = spopt.fmin_cg(d2, initparams)
	#optparams = spopt.fmin_bfgs(d2, params, epsilon =1.0e-5)
	#optparams = spopt.fmin_bfgs(d2, params, disp=1, retall=0)
	#optparams = spopt.anneal(d2, params)
	
	#optparams = spopt.fmin_ncg(d2, initparams) # this guy would need a gradient.
	#optparams = spopt.fmin_bfgs(d2, initparams) # precision loss
	
	
	# [  2.89270193e-10   4.94366701e-08  -2.63101369e-04  -4.38757115e-03]
	# [  2.95294380e-10   3.64696903e-08  -2.63882092e-04  -5.66560156e-05]
	
	
	
	optparams = spopt.fmin_powell(d2, initparams, disp=int(verbose)) # 312 , 2.592636, [  2.89270193e-10   4.94366701e-08  -2.63101369e-04  -4.38757115e-03]
	#optparams = spopt.fmin(d2, initparams) # 150, 2.614217
	#optparams = spopt.fmin(d2, initparams, xtol = 1.0e-10)
	#optparams = spopt.fmin_bfgs(d2, params, epsilon =1.0e-5)
	
	if verbose:
		print "Optimal params : ", optparams
		print repr(optparams)
	
	ml.multisetfreeparams([lc1, lc2], optparams)
	
	if plot:
		plt.semilogy(d2valuelist)
		plt.show()