Ejemplo n.º 1
0
def test_generateEllGaussianImage(fitsName):
    n = 100
    data = np.zeros((n,n))
    s0 = 0
    s1 = 1000
    phi = np.pi/4    # 45 degree
    sigma1 = 4.95
    sigma2 = 5.00
    A = (np.cos(phi)/sigma1)**2  + (np.sin(phi)/sigma2)**2
    B = (np.sin(phi)/sigma1)**2  + (np.cos(phi)/sigma2)**2
    C = 2*np.sin(phi)*np.cos(phi)*(1/(sigma1**2)-1/(sigma2**2))
    print C

    x0 = n/2.0
    y0 = n/2.0
    for i in range(n):
        for j in range(n):
            data[i][j] = s0+s1*np.exp(-0.5*(A*(i-x0)**2 + B*(j-y0)**2 +C*(i-x0)*(j-y0)))

    commons.writeFitsImage(data, fitsName )
Ejemplo n.º 2
0
def procedure():
    # initialization:

    s, B, const,  imgList, varList, phi, lambdaS = prepare()

    LM_SIE = models.PTMASS(critRad=6.16, centerX=0.0, centerY=5.0)
    #LM_SIE = models.SIE(critRad=6.16, axisRatio=0.7, pa=0.0, centerX=0.0, centerY=0.0)

    mappingDict = getMappingDict(imgList,varList, LM_SIE, const=const)

    imgPointList = mappingDict.keys()
    srcPointList = mappingDict.values()
    dim = len(imgPointList)
    srcPosition = []
    s = []
    r = np.zeros((1, 2*dim))
    for i in range(dim):
        x,y, bri, _, _ = srcPointList[i]

        srcPosition.append((x,y))
        s.append(bri)
        r[0][i] = bri
    srcMap = commons.pixelizeSource(srcPosition, s, const)


    commons.writeFitsImage(srcMap, "example/outputSrc.fits")
    plt.imshow(srcMap, origin="lower", interpolation="nearest")
    plt.colorbar()
    plt.show()


    #return
    chi2List = []
    #critRadRange = np.arange(5.9, 6.3, 0.02)
    #qRange = np.arange(0.1, 0.9, 0.05)
    #paRange = np.arange(0, 20, 10)
    centerYrange= np.arange(5, 15, 1)
    index = []
    i = 0
    # = []
    #for critRad in critRadRange:
    # r = np.reshape(r, (1,2*const.length))
    # r = scipy.sparse.coo_matrix(np.transpose(r))

    for yshift in centerYrange:

        i = i+1
        #LM_SIE.critRad = critRad
        #LM_SIE.axisRatio = q
        #LM_SIE.pa = pa
        LM_SIE.centerY = yshift
        mappingDict = getMappingDict(imgList,varList, MODEL = LM_SIE, const=const)


        srcPosition, srcPointList, L, normV, C, indexWeightList, d, RTR , HsTHs, imgPointList= construction.getLensOperator(mappingDict, s)

        chi2, res = construction.getChiSquare(M = L, r = r, d =d,  C=C, const=const)
        chi2 = chi2.toarray()[0][0]
        res = res.toarray()

        # image = construction.buildModelImage(imgPointList, res, const)
        # plt.imshow(image, origin="lower", interpolation="nearest")
        # plt.colorbar()
        # plt.show()
        print  yshift,  chi2


        index.append(yshift)
        chi2List.append(chi2)





    plt.plot(index, chi2List, '*-')
    plt.xlabel("Parameter")
    plt.ylabel("Chi2")
    plt.show()

    #critRadRange = np.arange(1.3, 4.0, 0.1)

    # for i in range(1):
    #     mappingDict = getMappingDict(imgList,varList, model='SIE', critRad = 3.0, const=const)
    #     srcPosition, srcPointList, L, normV, C, indexWeightList, d, RTR, HsTHs = construction.getLensOperator(mappingDict, s)
    #     Ds = construction.getMatrixDs(normV,indexWeightList, const)
    #     Dphi =construction.getMatrixDphi(const)
    #
    #
    #     D = scipy.sparse.coo_matrix(Ds)*scipy.sparse.coo_matrix(Dphi)
    #
    #
    #     M =scipy.sparse.hstack([L,D])
    #
    #     r = np.zeros(2*const.length)
    #     for i in range(const.length):
    #         r[i] = s[i]
    #
    #
    #     r =scipy.sparse.vstack(r)
    #     M_t = scipy.sparse.coo_matrix.transpose(M)
    #
    #     C = scipy.sparse.coo_matrix(C)
    #     RTR = scipy.sparse.coo_matrix(RTR)
    #
    #     C_inv = scipy.sparse.linalg.inv(C)
    #     b = M_t*C_inv*d
    #
    #     A = M_t*C_inv*M+RTR
    #     r = scipy.sparse.linalg.spsolve(A, b)
    #
    #     s = np.zeros((const.length,1))
    #     for i in range(const.length):
    #         s[i][0] = r[i]
    #     s_t = scipy.sparse.coo_matrix.transpose(scipy.sparse.coo_matrix(s))
    #     chi2 = construction.getChiSquare(M = M, r = r, d =d,  C=C)
    #     G = chi2 + s_t*scipy.sparse.coo_matrix(HsTHs)*s
    #     print i, chi2, G
    # srcMap = commons.pixelizeSource(srcPosition, s, const)
    # print srcMap
    # commons.writeFitsImage(srcMap, "model_test.fits")
    # plt.imshow(srcMap, origin="lower", interpolation="nearest")
    # plt.show()



    return