def test_matrix(self):
     Thrf = 25.
     dt = .5
     TT, m_h = getCanoHRF(Thrf, dt)
     m_H = np.array(m_h)
     m = vt.mult(m_H, m_H)
Beispiel #2
0
def Main_vbjde_Python_constrained(graph, Y, Onsets, Thrf, K, TR, beta, dt, scale=1, estimateSigmaH=True, sigmaH=0.1, NitMax=-1, NitMin=1, estimateBeta=False, PLOT=False):
    logger.info("EM started ...")
    np.random.seed(6537546)

    ##########################################################################
    # INITIALIZATIONS
    # Initialize parameters
    if NitMax < 0:
        NitMax = 100
    gamma = 7.5
    gradientStep = 0.005
    MaxItGrad = 120
    #Thresh = 1e-5
    Thresh_FreeEnergy = 1e-5

    # Initialize sizes vectors
    D = int(np.ceil(Thrf / dt))
    M = len(Onsets)
    N = Y.shape[0]
    J = Y.shape[1]
    l = int(np.sqrt(J))
    sigma_epsilone = np.ones(J)

    # Neighbours
    maxNeighbours = max([len(nl) for nl in graph])
    neighboursIndexes = np.zeros((J, maxNeighbours), dtype=np.int32)
    neighboursIndexes -= 1
    for i in xrange(J):
        neighboursIndexes[i, :len(graph[i])] = graph[i]
    # Conditions
    X = OrderedDict([])
    for condition, Ons in Onsets.iteritems():
        X[condition] = vt.compute_mat_X_2(N, TR, D, dt, Ons)
    XX = np.zeros((M, N, D), dtype=np.int32)
    nc = 0
    for condition, Ons in Onsets.iteritems():
        XX[nc, :, :] = X[condition]
        nc += 1
    # Sigma and mu
    mu_M = np.zeros((M, K), dtype=np.float64)
    sigma_M = 0.5 * np.ones((M, K), dtype=np.float64)
    sigma_M0 = 0.5 * np.ones((M, K), dtype=np.float64)
    for k in xrange(1, K):
        mu_M[:, k] = 2.0
    # Covariance matrix
    order = 2
    D2 = vt.buildFiniteDiffMatrix(order, D)
    R = np.dot(D2, D2) / pow(dt, 2 * order)

    Gamma = np.identity(N)

    q_Z = np.zeros((M, K, J), dtype=np.float64)
    # for k in xrange(0,K):
    q_Z[:, 1, :] = 1
    q_Z1 = np.zeros((M, K, J), dtype=np.float64)
    Z_tilde = q_Z.copy()

    Sigma_A = np.zeros((M, M, J), np.float64)
    m_A = np.zeros((J, M), dtype=np.float64)
    TT, m_h = getCanoHRF(Thrf - dt, dt)  # TODO: check
    for j in xrange(0, J):
        Sigma_A[:, :, j] = 0.01 * np.identity(M)
        for m in xrange(0, M):
            for k in xrange(0, K):
                m_A[j, m] += np.random.normal(mu_M[m, k],
                                              np.sqrt(sigma_M[m, k])) * Z_tilde[m, k, j]
    m_H = np.array(m_h).astype(np.float64)
    m_H1 = np.array(m_h)
    Sigma_H = np.ones((D, D), dtype=np.float64)
    Beta = beta * np.ones((M), dtype=np.float64)

    zerosDD = np.zeros((D, D), dtype=np.float64)
    zerosD = np.zeros((D), dtype=np.float64)
    zerosND = np.zeros((N, D), dtype=np.float64)
    zerosMM = np.zeros((M, M), dtype=np.float64)
    zerosJMD = np.zeros((J, M, D), dtype=np.float64)
    zerosK = np.zeros(K)

    P = vt.PolyMat(N, 4, TR)
    zerosP = np.zeros((P.shape[0]), dtype=np.float64)
    L = vt.polyFit(Y, TR, 4, P)
    PL = np.dot(P, L)
    y_tilde = Y - PL
    sigmaH1 = sigmaH

    Crit_H = 1
    Crit_Z = 1
    Crit_A = 1
    cA = []
    cH = []
    cZ = []
    Ndrift = L.shape[0]
    Crit_FreeEnergy = 1

    t1 = time.time()

    ##########################################################################
    # VBJDE num. iter. minimum

    ni = 0

    while ((ni < NitMin) or (((Crit_FreeEnergy > Thresh_FreeEnergy) or ((Crit_H > Thresh) and (Crit_Z > Thresh) and (Crit_A > Thresh))) and (ni < NitMax))):

        logger.info("------------------------------ Iteration n° " +
                    str(ni + 1) + " ------------------------------")

        #####################
        # EXPECTATION
        #####################

        # A
        logger.info("E A step ...")
        Sigma_A, m_A = vt.expectation_A(
            Y, Sigma_H, m_H, m_A, X, Gamma, PL, sigma_M, q_Z, mu_M, D, N, J, M, K, y_tilde, Sigma_A, sigma_epsilone, zerosJMD)
        m_A1 = m_A

        # crit A
        DIFF = np.abs(np.reshape(m_A, (M * J)) - np.reshape(m_A1, (M * J)))
        Crit_A = sum(DIFF) / len(np.where(DIFF != 0))
        cA += [Crit_A]

        # H
        logger.info("E H step ...")
        Sigma_H, m_H = vt.expectation_H(
            Y, Sigma_A, m_A, X, Gamma, PL, D, R, sigmaH, J, N, y_tilde, zerosND, sigma_epsilone, scale, zerosDD, zerosD)
        m_H[0] = 0
        m_H[-1] = 0
        m_H1 = m_H

        # crit H
        Crit_H = np.abs(np.mean(m_H - m_H1) / np.mean(m_H))
        cH += [Crit_H]

        # Z
        logger.info("E Z step ...")
        q_Z, Z_tilde = vt.expectation_Z(
            Sigma_A, m_A, sigma_M, Beta, Z_tilde, mu_M, q_Z, graph, M, J, K, zerosK)

        # crit Z
        DIFF = np.abs(
            np.reshape(q_Z, (M * K * J)) - np.reshape(q_Z1, (M * K * J)))
        Crit_Z = sum(DIFF) / len(np.where(DIFF != 0))
        cZ += [Crit_Z]
        q_Z1 = q_Z

        # Plotting HRF
        if PLOT and ni >= 0:
            import matplotlib.pyplot as plt
            plt.figure(M + 1)
            plt.plot(m_H)
            plt.hold(True)

        ####################
        # MAXIMIZATION
        #####################

        # HRF: Sigma_h
        if estimateSigmaH:
            logger.info("M sigma_H step ...")
            sigmaH = (np.dot(vt.mult(m_H, m_H) + Sigma_H, R)).trace()
            sigmaH /= D

        # (mu,sigma)
        logger.info("M (mu,sigma) step ...")
        mu_M, sigma_M = vt.maximization_mu_sigma(
            mu_M, sigma_M, q_Z, m_A, K, M, Sigma_A)

        # Drift L
        L = vt.maximization_L(Y, m_A, X, m_H, L, P, zerosP)
        PL = np.dot(P, L)
        y_tilde = Y - PL

        # Beta
        if estimateBeta:
            logger.info("estimating beta")
            for m in xrange(0, M):
                Beta[m] = vt.maximization_beta(
                    Beta[m], q_Z, Z_tilde, J, K, m, graph, gamma, neighboursIndexes, maxNeighbours)
            logger.info("End estimating beta")
            # logger.info(Beta)

        # Sigma noise
        logger.info("M sigma noise step ...")
        sigma_epsilone = vt.maximization_sigma_noise(
            Y, X, m_A, m_H, Sigma_H, Sigma_A, PL, sigma_epsilone, M, zerosMM)

        #### Computing Free Energy ####
        """
        if ni > 0:
            FreeEnergy1 = FreeEnergy
        FreeEnergy = Compute_FreeEnergy(y_tilde,m_A,Sigma_A,mu_M,sigma_M,m_H,Sigma_H,R,Det_invR,sigmaH,p_Wtilde,tau1,tau2,q_Z,neighboursIndexes,maxNeighbours,Beta,sigma_epsilone,XX,Gamma,Det_Gamma,XGamma,J,D,M,N,K,S,"CompMod")
        if ni > 0:
            Crit_FreeEnergy = (FreeEnergy1 - FreeEnergy) / FreeEnergy1
        FreeEnergy_Iter += [FreeEnergy]
        cFE += [Crit_FreeEnergy]
        """

        # Update index
        ni += 1

    t2 = time.time()

    ##########################################################################
    # PLOTS and SNR computation

    """FreeEnergyArray = np.zeros((ni),dtype=np.float64)
    for i in xrange(ni):
        FreeEnergyArray[i] = FreeEnergy_Iter[i]

    SUM_q_Z_array = np.zeros((M,ni),dtype=np.float64)
    mu1_array = np.zeros((M,ni),dtype=np.float64)
    h_norm_array = np.zeros((ni),dtype=np.float64)
    for m in xrange(M):
        for i in xrange(ni):
            SUM_q_Z_array[m,i] = SUM_q_Z[m][i]
            mu1_array[m,i] = mu1[m][i]
            h_norm_array[i] = h_norm[i]
    sigma_M = np.sqrt(np.sqrt(sigma_M))
    """

    Norm = np.linalg.norm(m_H)
    m_H /= Norm
    m_A *= Norm
    mu_M *= Norm
    sigma_M *= Norm
    sigma_M = np.sqrt(sigma_M)

    if PLOT and 0:
        import matplotlib.pyplot as plt
        import matplotlib
        font = {'size': 15}
        matplotlib.rc('font', **font)
        plt.savefig('./HRF_Iter_CompMod.png')
        plt.hold(False)
        plt.figure(2)
        plt.plot(cAH[1:-1], 'lightblue')
        plt.hold(True)
        plt.plot(cFE[1:-1], 'm')
        plt.hold(False)
        #plt.legend( ('CA','CH', 'CZ', 'CAH', 'CFE') )
        plt.legend(('CAH', 'CFE'))
        plt.grid(True)
        plt.savefig('./Crit_CompMod.png')
        plt.figure(3)
        plt.plot(FreeEnergyArray)
        plt.grid(True)
        plt.savefig('./FreeEnergy_CompMod.png')

        plt.figure(4)
        for m in xrange(M):
            plt.plot(SUM_q_Z_array[m])
            plt.hold(True)
        plt.hold(False)
        plt.savefig('./Sum_q_Z_Iter_CompMod.png')

        plt.figure(5)
        for m in xrange(M):
            plt.plot(mu1_array[m])
            plt.hold(True)
        plt.hold(False)
        plt.savefig('./mu1_Iter_CompMod.png')

        plt.figure(6)
        plt.plot(h_norm_array)
        plt.savefig('./HRF_Norm_CompMod.png')

        Data_save = xndarray(h_norm_array, ['Iteration'])
        Data_save.save('./HRF_Norm_Comp.nii')

    CompTime = t2 - t1
    cTimeMean = CompTime / ni

    logger.info("Nb iterations to reach criterion: %d", ni)
    logger.info("Computational time = %s min %s s", str(
        int(CompTime // 60)), str(int(CompTime % 60)))
    # print "Computational time = " + str(int( CompTime//60 ) ) + " min " +
    # str(int(CompTime%60)) + " s"
    logger.info('mu_M: %f', mu_M)
    logger.info('sigma_M: %f', sigma_M)
    logger.info("sigma_H = %s" + str(sigmaH))
    logger.info("Beta = %s" + str(Beta))

    StimulusInducedSignal = vt.computeFit(m_H, m_A, X, J, N)
    SNR = 20 * \
        np.log(
            np.linalg.norm(Y) / np.linalg.norm(Y - StimulusInducedSignal - PL))
    SNR /= np.log(10.)
    print 'SNR comp =', SNR
    return m_A, m_H, q_Z, sigma_epsilone, mu_M, sigma_M, Beta, L, PL
def Main_vbjde_Python_constrained(graph, Y, Onsets, Thrf, K, TR, beta, dt, scale=1, estimateSigmaH=True, sigmaH=0.1, NitMax=-1, NitMin=1, estimateBeta=False, PLOT=False):
    logger.info("EM started ...")
    np.random.seed(6537546)

    ##########################################################################
    # INITIALIZATIONS
    # Initialize parameters
    if NitMax < 0:
        NitMax = 100
    gamma = 7.5
    gradientStep = 0.005
    MaxItGrad = 120
    #Thresh = 1e-5
    Thresh_FreeEnergy = 1e-5

    # Initialize sizes vectors
    D = int(np.ceil(Thrf / dt))
    M = len(Onsets)
    N = Y.shape[0]
    J = Y.shape[1]
    l = int(np.sqrt(J))
    sigma_epsilone = np.ones(J)

    # Neighbours
    maxNeighbours = max([len(nl) for nl in graph])
    neighboursIndexes = np.zeros((J, maxNeighbours), dtype=np.int32)
    neighboursIndexes -= 1
    for i in xrange(J):
        neighboursIndexes[i, :len(graph[i])] = graph[i]
    # Conditions
    X = OrderedDict([])
    for condition, Ons in Onsets.iteritems():
        X[condition] = vt.compute_mat_X_2(N, TR, D, dt, Ons)
    XX = np.zeros((M, N, D), dtype=np.int32)
    nc = 0
    for condition, Ons in Onsets.iteritems():
        XX[nc, :, :] = X[condition]
        nc += 1
    # Sigma and mu
    mu_M = np.zeros((M, K), dtype=np.float64)
    sigma_M = 0.5 * np.ones((M, K), dtype=np.float64)
    sigma_M0 = 0.5 * np.ones((M, K), dtype=np.float64)
    for k in xrange(1, K):
        mu_M[:, k] = 2.0
    # Covariance matrix
    order = 2
    D2 = vt.buildFiniteDiffMatrix(order, D)
    R = np.dot(D2, D2) / pow(dt, 2 * order)

    Gamma = np.identity(N)

    q_Z = np.zeros((M, K, J), dtype=np.float64)
    # for k in xrange(0,K):
    q_Z[:, 1, :] = 1
    q_Z1 = np.zeros((M, K, J), dtype=np.float64)
    Z_tilde = q_Z.copy()

    Sigma_A = np.zeros((M, M, J), np.float64)
    m_A = np.zeros((J, M), dtype=np.float64)
    TT, m_h = getCanoHRF(Thrf - dt, dt)  # TODO: check
    for j in xrange(0, J):
        Sigma_A[:, :, j] = 0.01 * np.identity(M)
        for m in xrange(0, M):
            for k in xrange(0, K):
                m_A[j, m] += np.random.normal(mu_M[m, k],
                                              np.sqrt(sigma_M[m, k])) * Z_tilde[m, k, j]
    m_H = np.array(m_h).astype(np.float64)
    m_H1 = np.array(m_h)
    Sigma_H = np.ones((D, D), dtype=np.float64)
    Beta = beta * np.ones((M), dtype=np.float64)

    zerosDD = np.zeros((D, D), dtype=np.float64)
    zerosD = np.zeros((D), dtype=np.float64)
    zerosND = np.zeros((N, D), dtype=np.float64)
    zerosMM = np.zeros((M, M), dtype=np.float64)
    zerosJMD = np.zeros((J, M, D), dtype=np.float64)
    zerosK = np.zeros(K)

    P = vt.PolyMat(N, 4, TR)
    zerosP = np.zeros((P.shape[0]), dtype=np.float64)
    L = vt.polyFit(Y, TR, 4, P)
    PL = np.dot(P, L)
    y_tilde = Y - PL
    sigmaH1 = sigmaH

    Crit_H = 1
    Crit_Z = 1
    Crit_A = 1
    cA = []
    cH = []
    cZ = []
    Ndrift = L.shape[0]
    Crit_FreeEnergy = 1

    t1 = time.time()

    ##########################################################################
    # VBJDE num. iter. minimum

    ni = 0

    while ((ni < NitMin) or (((Crit_FreeEnergy > Thresh_FreeEnergy) or ((Crit_H > Thresh) and (Crit_Z > Thresh) and (Crit_A > Thresh))) and (ni < NitMax))):

        logger.info("------------------------------ Iteration n° " +
                    str(ni + 1) + " ------------------------------")

        #####################
        # EXPECTATION
        #####################

        # A
        logger.info("E A step ...")
        Sigma_A, m_A = vt.expectation_A(
            Y, Sigma_H, m_H, m_A, X, Gamma, PL, sigma_M, q_Z, mu_M, D, N, J, M, K, y_tilde, Sigma_A, sigma_epsilone, zerosJMD)
        m_A1 = m_A

        # crit A
        DIFF = np.abs(np.reshape(m_A, (M * J)) - np.reshape(m_A1, (M * J)))
        Crit_A = sum(DIFF) / len(np.where(DIFF != 0))
        cA += [Crit_A]

        # H
        logger.info("E H step ...")
        Sigma_H, m_H = vt.expectation_H(
            Y, Sigma_A, m_A, X, Gamma, PL, D, R, sigmaH, J, N, y_tilde, zerosND, sigma_epsilone, scale, zerosDD, zerosD)
        m_H[0] = 0
        m_H[-1] = 0
        m_H1 = m_H

        # crit H
        Crit_H = np.abs(np.mean(m_H - m_H1) / np.mean(m_H))
        cH += [Crit_H]

        # Z
        logger.info("E Z step ...")
        q_Z, Z_tilde = vt.expectation_Z(
            Sigma_A, m_A, sigma_M, Beta, Z_tilde, mu_M, q_Z, graph, M, J, K, zerosK)

        # crit Z
        DIFF = np.abs(
            np.reshape(q_Z, (M * K * J)) - np.reshape(q_Z1, (M * K * J)))
        Crit_Z = sum(DIFF) / len(np.where(DIFF != 0))
        cZ += [Crit_Z]
        q_Z1 = q_Z

        # Plotting HRF
        if PLOT and ni >= 0:
            import matplotlib.pyplot as plt
            plt.figure(M + 1)
            plt.plot(m_H)
            plt.hold(True)

        ####################
        # MAXIMIZATION
        #####################

        # HRF: Sigma_h
        if estimateSigmaH:
            logger.info("M sigma_H step ...")
            sigmaH = (np.dot(vt.mult(m_H, m_H) + Sigma_H, R)).trace()
            sigmaH /= D

        # (mu,sigma)
        logger.info("M (mu,sigma) step ...")
        mu_M, sigma_M = vt.maximization_mu_sigma(
            mu_M, sigma_M, q_Z, m_A, K, M, Sigma_A)

        # Drift L
        L = vt.maximization_L(Y, m_A, X, m_H, L, P, zerosP)
        PL = np.dot(P, L)
        y_tilde = Y - PL

        # Beta
        if estimateBeta:
            logger.info("estimating beta")
            for m in xrange(0, M):
                Beta[m] = vt.maximization_beta(
                    Beta[m], q_Z, Z_tilde, J, K, m, graph, gamma, neighboursIndexes, maxNeighbours)
            logger.info("End estimating beta")
            # logger.info(Beta)

        # Sigma noise
        logger.info("M sigma noise step ...")
        sigma_epsilone = vt.maximization_sigma_noise(
            Y, X, m_A, m_H, Sigma_H, Sigma_A, PL, sigma_epsilone, M, zerosMM)

        #### Computing Free Energy ####
        """
        if ni > 0:
            FreeEnergy1 = FreeEnergy
        FreeEnergy = Compute_FreeEnergy(y_tilde,m_A,Sigma_A,mu_M,sigma_M,m_H,Sigma_H,R,Det_invR,sigmaH,p_Wtilde,tau1,tau2,q_Z,neighboursIndexes,maxNeighbours,Beta,sigma_epsilone,XX,Gamma,Det_Gamma,XGamma,J,D,M,N,K,S,"CompMod")
        if ni > 0:
            Crit_FreeEnergy = (FreeEnergy1 - FreeEnergy) / FreeEnergy1
        FreeEnergy_Iter += [FreeEnergy]
        cFE += [Crit_FreeEnergy]
        """

        # Update index
        ni += 1

    t2 = time.time()

    ##########################################################################
    # PLOTS and SNR computation

    """FreeEnergyArray = np.zeros((ni),dtype=np.float64)
    for i in xrange(ni):
        FreeEnergyArray[i] = FreeEnergy_Iter[i]

    SUM_q_Z_array = np.zeros((M,ni),dtype=np.float64)
    mu1_array = np.zeros((M,ni),dtype=np.float64)
    h_norm_array = np.zeros((ni),dtype=np.float64)
    for m in xrange(M):
        for i in xrange(ni):
            SUM_q_Z_array[m,i] = SUM_q_Z[m][i]
            mu1_array[m,i] = mu1[m][i]
            h_norm_array[i] = h_norm[i]
    sigma_M = np.sqrt(np.sqrt(sigma_M))
    """

    Norm = np.linalg.norm(m_H)
    m_H /= Norm
    m_A *= Norm
    mu_M *= Norm
    sigma_M *= Norm
    sigma_M = np.sqrt(sigma_M)

    if PLOT and 0:
        import matplotlib.pyplot as plt
        import matplotlib
        font = {'size': 15}
        matplotlib.rc('font', **font)
        plt.savefig('./HRF_Iter_CompMod.png')
        plt.hold(False)
        plt.figure(2)
        plt.plot(cAH[1:-1], 'lightblue')
        plt.hold(True)
        plt.plot(cFE[1:-1], 'm')
        plt.hold(False)
        #plt.legend( ('CA','CH', 'CZ', 'CAH', 'CFE') )
        plt.legend(('CAH', 'CFE'))
        plt.grid(True)
        plt.savefig('./Crit_CompMod.png')
        plt.figure(3)
        plt.plot(FreeEnergyArray)
        plt.grid(True)
        plt.savefig('./FreeEnergy_CompMod.png')

        plt.figure(4)
        for m in xrange(M):
            plt.plot(SUM_q_Z_array[m])
            plt.hold(True)
        plt.hold(False)
        plt.savefig('./Sum_q_Z_Iter_CompMod.png')

        plt.figure(5)
        for m in xrange(M):
            plt.plot(mu1_array[m])
            plt.hold(True)
        plt.hold(False)
        plt.savefig('./mu1_Iter_CompMod.png')

        plt.figure(6)
        plt.plot(h_norm_array)
        plt.savefig('./HRF_Norm_CompMod.png')

        Data_save = xndarray(h_norm_array, ['Iteration'])
        Data_save.save('./HRF_Norm_Comp.nii')

    CompTime = t2 - t1
    cTimeMean = CompTime / ni

    logger.info("Nb iterations to reach criterion: %d", ni)
    logger.info("Computational time = %s min %s s", str(
        int(CompTime // 60)), str(int(CompTime % 60)))
    # print "Computational time = " + str(int( CompTime//60 ) ) + " min " +
    # str(int(CompTime%60)) + " s"
    logger.info('mu_M: %f', mu_M)
    logger.info('sigma_M: %f', sigma_M)
    logger.info("sigma_H = %s" + str(sigmaH))
    logger.info("Beta = %s" + str(Beta))

    StimulusInducedSignal = vt.computeFit(m_H, m_A, X, J, N)
    SNR = 20 * \
        np.log(
            np.linalg.norm(Y) / np.linalg.norm(Y - StimulusInducedSignal - PL))
    SNR /= np.log(10.)
    logger.info('SNR comp = %f', SNR)
    return m_A, m_H, q_Z, sigma_epsilone, mu_M, sigma_M, Beta, L, PL