Пример #1
0
def copy_fit(bgm, method='bgm'):
    n_clusters = bgm.n_components
    covartype = bgm.covariance_type
    n_init = bgm.n_init
    max_iter = bgm.max_iter
    tol = bgm.tol
    verbose = True
    if method == 'bgm':
        wcpt = bgm.weight_concentration_prior_type
        reg_covar = bgm.reg_covar
        init_params = bgm.init_params
        tol = bgm.tol
        copy = BGM(n_components=n_clusters,
                   covariance_type=covartype,
                   n_init=n_init,
                   weight_concentration_prior_type=wcpt,
                   init_params=init_params,
                   max_iter=max_iter,
                   verbose=verbose,
                   reg_covar=reg_covar,
                   tol=tol)
        copy.weight_concentration_prior_ = bgm.weight_concentration_prior_
        copy.weight_concentration_ = bgm.weight_concentration_
        copy.mean_precision_prior = bgm.mean_precision_prior
        copy.mean_prior_ = bgm.mean_prior_
        copy.mean_precision_ = bgm.mean_precision_
        copy.covariance_prior_ = bgm.covariance_prior_
        copy.degrees_of_freedom_prior_ = bgm.degrees_of_freedom_prior_
        copy.degrees_of_freedom_ = bgm.degrees_of_freedom_
    if method == 'gmm':
        copy = GMM(n_components=n_clusters,
                   random_state=42,
                   covariance_type=covartype,
                   max_iter=max_iter,
                   n_init=n_init,
                   tol=tol,
                   verbose=verbose)
    copy.means_ = bgm.means_
    copy.covariances_ = bgm.covariances_
    copy.weights_ = bgm.weights_
    copy.precisions_ = bgm.precisions_
    copy.precisions_cholesky_ = bgm.precisions_cholesky_
    copy.converged_ = bgm.converged_
    copy.n_iter_ = bgm.n_iter_
    copy.lower_bound_ = bgm.lower_bound_
    return copy
Пример #2
0
def fit_dirichlet_gmm_to_points(points,
                      n_components,
                      mdl,
                      ps=[],
                      num_iter=100,
                      covariance_type='full',
                      mass_multiplier=1.0):
    """fit a GMM to some points. Will return core::Gaussians.
    if no particles are provided, they will be created

    points:            list of coordinates (python)
    n_components:      number of gaussians to create
    mdl:               IMP Model
    ps:                list of particles to be decorated. if empty, will add
    num_iter:          number of EM iterations
    covariance_type:   covar type for the gaussians. options: 'full', 'diagonal', 'spherical'
    init_centers:      initial coordinates of the GMM
    force_radii:       fix the radii (spheres only)
    force_weight:      fix the weights
    mass_multiplier:   multiply the weights of all the gaussians by this value
    """


    new_sklearn = True
    try:
        from sklearn.mixture import BayesianGaussianMixture
    except ImportError:
        from sklearn.mixture import DPGMM
        new_sklearn = False

    ### create and fit GMM
    print('using dirichlet prior')
    if new_sklearn:
        gmm = BayesianGaussianMixture(
                weight_concentration_prior_type='dirichlet_process',
                n_components=n_components, max_iter=num_iter,
                covariance_type=covariance_type)
    else:
        gmm = DPGMM(n_components=n_components, n_iter=num_iter,
                    covariance_type=covariance_type)

    gmm.fit(points)

    #print('>>> GMM score',gmm.score(points))

    #print gmm.covars_
    #print gmm.weights_
    #print gmm.means_
    ### convert format to core::Gaussian
    if not new_sklearn:
        gmm.precisions_ = gmm.precs_
    for ng in range(n_components):
        invcovar=gmm.precisions_[ng]
        covar=np.linalg.inv(invcovar)
        if covar.size==3:
            covar=np.diag(covar).tolist()
        else:
            covar=covar.tolist()
        center=list(gmm.means_[ng])
        weight=mass_multiplier*gmm.weights_[ng]
        if ng>=len(ps):
            ps.append(IMP.Particle(mdl))
        shape=IMP.algebra.get_gaussian_from_covariance(covar,IMP.algebra.Vector3D(center))
        g=IMP.core.Gaussian.setup_particle(ps[ng],shape)
        IMP.atom.Mass.setup_particle(ps[ng],weight)
        IMP.core.XYZR.setup_particle(ps[ng],sqrt(max(g.get_variances())))