示例#1
0
def pseudo_BIC_search(Y,
                      J_range,
                      lambda_range,
                      mu_range,
                      tau_range,
                      theta_bound=1.0,
                      tvw=None,
                      initB='pca',
                      maxK=200,
                      maxN=100,
                      eps=1e-3,
                      eps_gap='auto',
                      step=1.0,
                      eps_jumps=1e-3,
                      callback=None):

    L, S = Y.shape
    J_range = sorted(J_range)
    mu_range = sorted(mu_range)
    lambda_range = sorted(lambda_range)
    tau_range = sorted(tau_range)

    # Parameters dimensions
    m, l, t = len(mu_range), len(lambda_range), len(tau_range)

    # CGHDL params
    params = {
        'theta_bound': theta_bound,
        'tvw': tvw,
        'maxK': maxK,
        'maxN': maxN,
        'eps': eps,
        'eps_gap': eps_gap,
        'step': step
    }

    for J in J_range:
        # For each new J value we start from following B0 and Theta0
        B0 = utils.initB(Y, J)
        #Theta0 = np.ones((J, S)) * theta_bound
        Theta0 = np.zeros((J, S))  # for comparison with original FLLat

        # Evaluation
        for j, i, k in it.product(range(l), range(m), range(t)):

            time = alg.minimize(Y,
                                J,
                                lambda_=lambda_range[j],
                                mu=mu_range[i],
                                tau=tau_range[k],
                                initB=B0,
                                initTheta=Theta0,
                                **params)

            # Callback execution
            if not callback is None:
                callback(time, J, lambda_range[j], mu_range[i], tau_range[k])
示例#2
0
def minimize(Y,
             J,
             lambda_,
             mu,
             tau,
             theta_bound=1.0,
             tvw=None,
             initB='pca',
             initTheta=None,
             maxK=200,
             maxN=100,
             eps=1e-3,
             eps_gap='auto',
             step=1.0):

    L, S = Y.shape

    #### B Initialization
    try:
        assert initB.shape == (L, J)
        B0 = initB.copy()
    except AttributeError:
        assert initB in ['pca', 'rand']
        B0 = utils.initB(Y, J, initB)

    #### Theta Initialization
    # Theta0 = np.zeros((J, S)) # <- default

    # Parameters normalization
    #lambda_ *= (S/float(J))  # l, j -- /S
    #mu *= (S/float(J))       # l, j -- /S
    lambda_ *= 2.0 * (S / float(J))  # l, j -- /S
    mu *= 2.0 * (S / float(J))  # l, j -- /S

    s_time = time.time()
    result = FLLat(
        Y,
        J=J,
        B=B0,
        lam1=lambda_,
        lam2=mu,
        thresh=eps,  # 1e-4 (default)
        maxiter=maxK,  # 100  (default)
        maxiter_B=maxN,  # 1    (  //   )  
        maxiter_T=maxN)  # 1    (  //   )

    return time.time() - s_time
示例#3
0
def minimize(Y,
             J,
             lambda_,
             mu,
             initB='pca',
             initTheta=None,
             maxK=200,
             maxN=100,
             eps=1e-3,
             callback=None):

    L, S = Y.shape

    #### B Initialization
    try:
        assert initB.shape == (L, J)
        B0 = initB.copy()
    except AttributeError:
        assert initB in ['pca', 'rand']
        B0 = utils.initB(Y, J, initB)

    #### Theta Initialization
    try:
        assert initTheta.shape == (J, S)
        Theta0 = initTheta.copy()
    except AttributeError:
        #Theta0 = np.ones((J, S)) * theta_bound
        Theta0 = np.zeros((J, S))  # for comparison with original FLLat

    #### Precision
    p = 1.  # How to justify such a choice?
    eta = 1. / (S * J)
    zeta = 1. / (L * J)

    # Parameters normalization
    lambda_ *= (S / float(J))  # l, j -- /S
    mu *= (S / float(J))  # l, j -- /S

    #### Starting Duality Gap for PHI (with Vi=0, B fixed and Gamma=Theta0)
    C_phi = proxf.c_phi(Y, Theta0, B0, eta)

    #### Starting Duality Gap for PSI (with Vi=0, Theta fixed and Zeta=B0)
    C_psi = proxf.c_psi(Y, Theta0, B0, lambda_, mu, zeta)

    dual_var_phi = None
    dual_var_psi = None
    B, Theta = B0, Theta0

    B_diff = None
    Theta_diff = None
    B_prev = np.empty_like(B)
    Theta_prev = np.empty_like(Theta)

    for k in xrange(int(maxK)):
        B_prev = B.copy()
        Theta_prev = Theta.copy()

        epsk = 1. / ((k + 1)**p)

        # Option Const
        eta = 1.
        zeta = 1.

        # Option DESCR
        #eta = 1. / (k+1)
        #zeta = 1. / (k+1)

        # Option INCR
        #eta = (k+1)
        #zeta = (k+1)

        # Theta update
        Theta, phi_gaps, dual_var_phi = proxf.prox_phi(Theta,
                                                       eta,
                                                       B,
                                                       Y,
                                                       eps=C_phi * epsk,
                                                       maxN=maxN,
                                                       init=dual_var_phi)

        # B update
        B, psi_gaps, dual_var_psi = proxf.prox_psi(B,
                                                   zeta,
                                                   Theta,
                                                   Y,
                                                   mu,
                                                   lambda_,
                                                   eps=C_psi * epsk,
                                                   maxN=maxN,
                                                   init=dual_var_psi)

        # Updating collections
        callback(phi_gaps, psi_gaps, k, J, lambda_, mu)

        B_diff = np.sum((B - B_prev)**2) / np.sum(B_prev**2)
        if k == 0:  #first iteration
            Theta_diff = np.inf  # -> more than eps
        else:
            Theta_diff = np.sum(
                (Theta - Theta_prev)**2) / np.sum(Theta_prev**2)

        # convergence
        #if (B_diff <= eps and Theta_diff <= eps):
        print(B_diff <= eps and Theta_diff <= eps)
        #    break

    return {
        'B': B,
        'Theta': Theta,
        'conv': k,
        'gap_phi': phi_gaps[-1],
        'gap_psi': psi_gaps[-1]
    }
示例#4
0
def minimize(Y,
             J,
             lambda_,
             mu,
             tau,
             theta_bound=1.0,
             tvw=None,
             initB='pca',
             initTheta=None,
             maxK=200,
             maxN=100,
             eps=1e-3,
             eps_gap='auto',
             step=1.0):

    L, S = Y.shape

    #### B Initialization
    try:
        assert initB.shape == (L, J)
        B0 = initB.copy()
    except AttributeError:
        assert initB in ['pca', 'rand']
        B0 = utils.initB(Y, J, initB)

    #### Theta Initialization
    theta_bound = float(theta_bound)
    try:
        assert initTheta.shape == (J, S)
        Theta0 = initTheta.copy()
    except AttributeError:
        #Theta0 = np.ones((J, S)) * theta_bound
        Theta0 = np.zeros((J, S))  # for comparison with original FLLat

    #### Total variation weigths
    if tvw is None:
        w = np.ones((L - 1, 1))
    else:
        w = tvw.reshape(L - 1, 1)

    #### Precision
    p = 1.  # How to justify such a choice?
    eta = 1. / (S * J)
    zeta = 1. / (L * J)

    # Parameters normalization
    tau *= (L / float(J))  # s, j -- /J
    lambda_ *= (S / float(J))  # l, j -- /S
    mu *= (S / float(J))  # l, j -- /S

    #### Starting Duality Gap for PHI (with Vi=0, B fixed and Gamma=Theta0)
    C_phi = proxf.c_phi(Y, Theta0, B0, tau, eta, theta_bound)

    #### Starting Duality Gap for PSI (with Vi=0, Theta fixed and Zeta=B0)
    C_psi = proxf.c_psi(Y, Theta0, B0, lambda_, mu * w, zeta)

    dual_var_phi = None
    dual_var_psi = None
    B, Theta = B0, Theta0

    B_diff = None
    Theta_diff = None
    B_prev = np.empty_like(B)
    Theta_prev = np.empty_like(Theta)

    # Starting timer ----------------------------------------------------------
    s_time = time.time()
    for k in xrange(int(maxK)):
        B_prev = B.copy()
        Theta_prev = Theta.copy()

        if eps_gap == 'auto':
            epsk = {'phi': C_phi / ((k + 1)**p), 'psi': C_psi / ((k + 1)**p)}
        else:
            epsk = {'phi': eps_gap, 'psi': eps_gap}

        if step == 'decr':
            eta = 1. / (k + 1)
            zeta = 1. / (k + 1)
        elif step == 'incr':
            eta = (k + 1)
            zeta = (k + 1)
        else:
            eta = step
            zeta = step

        # Theta update
        Theta, gap_phi, dual_var_phi, n_phi = proxf.prox_phi(Theta,
                                                             eta,
                                                             B,
                                                             Y,
                                                             tau,
                                                             bound=theta_bound,
                                                             eps=epsk['phi'],
                                                             maxN=maxN,
                                                             init=dual_var_phi)

        # B update
        B, gap_psi, dual_var_psi, n_psi = proxf.prox_psi(B,
                                                         zeta,
                                                         Theta,
                                                         Y,
                                                         mu * w,
                                                         lambda_,
                                                         eps=epsk['psi'],
                                                         maxN=maxN,
                                                         init=dual_var_psi)

        B_diff = np.sum((B - B_prev)**2) / np.sum(B_prev**2)
        if k == 0:  #first iteration
            Theta_diff = np.inf  # -> more than eps
        else:
            Theta_diff = np.sum(
                (Theta - Theta_prev)**2) / np.sum(Theta_prev**2)

        # convergence
        if (B_diff <= eps and Theta_diff <= eps):
            break
    return time.time() - s_time
示例#5
0
文件: bic.py 项目: slipguru/PyCGH
def BIC_search(Y,
               J_range,
               lambda_range,
               mu_range,
               tau_range,
               theta_bound=1.0,
               tvw=None,
               maxK=200,
               maxN=100,
               eps=1e-3,
               eps_gap='auto',
               step=1.0,
               eps_jumps=1e-3,
               callback=None):
    """
    This function runs the **E-FLLat** algorithm for all possible combination of parameters and selects the best model (i.e. tuple of parameters) using the Bayesian information criterion (BIC).
    
    Parameters
    ----------
    
    Y : numpy.ndarray
        The matrix representing all aCGH signals. Each column of the matrix represent a singla aCGH.
    
    J_range : array_like
        The list of all possible values for the parameter :math:`J`, representing the number of atoms which will be used for the reconstruction of the matrix.
        
    lambda_range : array_like
        All possible values for the :math:`\lambda` parameter.
    
    mu_range : array_like
        All possible values for the :math:`\mu` parameter.
    
    tau_range : array_like
        All possible values for the :math:`\tau` parameter.
    
    theta_bound : float, optional (default: ``1.0``)
        The value with which each entry of the :math:`\Theta` matrix will be initialized if it is not provided.
    
    tvw : array_like, optional (default: ``None``)
        An array containing the weights for the total variation term (when present).
    
    maxK : int, optional (default: 200)
        The maximum number of external iterations.
    
    maxN : int, optional (default: 100)
        The maximum number of internal iterations.
    
    eps : float, optional (default: 1e-3)
        The value used to stop the external iterations if neither of the two matrices changes more then this threshold.
    
    eps_gap : string, optional (default: ``"auto"``)
        The value used to stop the internal iterations when the solution does not change more than a fixed threshold between two iterations.
    
    step : float, optional (default: 1.0)
        A parameter controlling the step of internal iterations.
    
    eps_jumps : float, optional (default: 1e-3)
        The tolerance when computing the penalty for jumps in atoms.
    
    callback, function, optional (default: ``None``)
        If different from ``None``, the function is called at the end of each iteration and does something with the result obtained.
    """

    import algorithm as alg

    L, S = Y.shape
    J_range = sorted(J_range)
    mu_range = sorted(mu_range)
    lambda_range = sorted(lambda_range)
    tau_range = sorted(tau_range)

    # Parameters dimensions
    m, l, t = len(mu_range), len(lambda_range), len(tau_range)

    # CGHDL params
    params = {
        'theta_bound': theta_bound,
        'tvw': tvw,
        'maxK': maxK,
        'maxN': maxN,
        'eps': eps,
        'eps_gap': eps_gap,
        'step': step
    }

    # Initialization
    BIC_min = np.inf
    B_res, Theta_res = None, None
    J_res, mu_res, lambda_res, tau_res = None, None, None, None

    for J in J_range:
        # For each new J value we start from following B0 and Theta0
        B0 = utils.initB(Y, J)
        #Theta0 = np.ones((J, S)) * theta_bound
        Theta0 = np.zeros((J, S))  # for comparison with original FLLat

        # Evaluation
        for j, i, k in it.product(range(l), range(m), range(t)):

            result = alg.minimize(Y,
                                  J,
                                  lambda_=lambda_range[j],
                                  mu=mu_range[i],
                                  tau=tau_range[k],
                                  initB=B0,
                                  initTheta=Theta0,
                                  **params)
            B = result['B']
            Theta = result['Theta']

            # BIC calculation
            fit = np.sum((Y - np.dot(B, Theta))**2.) / (S * L)
            jumpsB = atoms_jumps(B, eps_jumps)

            # fit                 # complexity
            BIC = (S * L * np.log(fit)) + (jumpsB * np.log(S * L))

            if BIC < BIC_min:
                BIC_min = BIC
                B_res, Theta_res = B, Theta
                J_res, lambda_res, mu_res, tau_res = (J, lambda_range[j],
                                                      mu_range[i],
                                                      tau_range[k])
            # Callback execution
            if not callback is None:
                callback(result, J, lambda_range[j], mu_range[i], tau_range[k],
                         BIC, eps_jumps)

    # Return the best result
    return {
        'B': B_res,
        'Theta': Theta_res,
        'J': J_res,
        'lambda': lambda_res,
        'mu': mu_res,
        'tau': tau_res,
        'BIC': BIC_min
    }
示例#6
0
def minimize(Y,
             J,
             lambda_,
             mu,
             tau,
             theta_bound=1.0,
             tvw=None,
             initB='pca',
             initTheta=None,
             maxK=200,
             maxN=100,
             eps=1e-3,
             eps_gap='auto',
             step=1.0):

    L, S = Y.shape

    #### B Initialization
    try:
        assert initB.shape == (L, J)
        B0 = initB.copy()
    except AttributeError:
        assert initB in ['pca', 'rand']
        B0 = utils.initB(Y, J, initB)

    #### Theta Initialization
    # Theta0 = np.zeros((J, S)) # <- default

    # Parameters normalization
    #lambda_ *= (S/float(J))  # l, j -- /S
    #mu *= (S/float(J))       # l, j -- /S
    lambda_ *= 2.0 * (S / float(J))  # l, j -- /S
    mu *= 2.0 * (S / float(J))  # l, j -- /S

    result = FLLat(
        Y,
        J=J,
        B=B0,
        lam1=lambda_,
        lam2=mu,
        thresh=eps,  # 1e-4 (default)
        maxiter=maxK,  # 100  (default)
        maxiter_B=maxN,  # 1    (  //   )  
        maxiter_T=maxN)  # 1    (  //   )

    B = np.asarray(result.rx2('Beta'))
    Theta = np.asarray(result.rx2('Theta'))
    k = np.asarray(result.rx2('niter'))[0]
    gap_phi = -np.inf  # placeholders
    gap_psi = -np.inf  # --

    return {
        'B': B,
        'Theta': Theta,
        'conv': k,
        'gap_phi': gap_phi,
        'gap_psi': gap_psi,
        'internal_iters': {
            'phi': [-1],
            'psi': [-1]
        },
        'C_phi': -1,
        'C_psi': -1
    }