jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def add_user(name,path):

    #Voice authentication
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    CHUNK = 1024
    RECORD_SECONDS = 5

    path = "./"+ path
    #source = "./voice_database"


    #os.mkdir(source)



    dest =  "./gmm_models/"
    #count = 1
    #max_count = len(os.listdir(source))

    #for path in os.listdir(source):
        #path = os.path.join(source, path)


    features = np.array([])

    # reading audio files of speaker
    (sr, audio) = read(path)

    # extract 40 dimensional MFCC & delta MFCC features
    vector   = extract_features(audio,sr)

    if features.size == 0:
        features = vector
    else:
        features = np.vstack((features, vector))

    # when features of 3 files of speaker are concatenated, then do model training
    #if count == max_count:
    gmm = GaussianMixture(n_components = 16, n_iter = 200, covariance_type='diag',n_init = 3)
    gmm.fit(features)


    # saving the trained gaussian model
    pickle.dump(gmm, open(dest + name + '.gmm', 'wb'))
    #json.dump(gmm, a)
    print(name + ' added successfully')

    features = np.asarray(())
    return gmm,dest + name + '.gmm'
//  [python]
def fit(self, **kwargs):
        '''
        First, a Doc2Vec model is trained and clustering of the documents is done by means of GMM.
        :param kwargs: additional arguments which should be passed to Doc2Vec and GMM.
        :param save: if you want to save the trained model set save=True.
        :param filename: name of the saved model.
        :return: Doc2Vec model and GMM clusters.
        '''
        # generate doc2vec model
        self.model = gensim.models.Doc2Vec(self.x_docs, **{key: value for key, value in kwargs.items()
                                                           if
                                                           key in inspect.getfullargspec(gensim.models.Doc2Vec).args or
                                                           key in inspect.getfullargspec(
                                                               gensim.models.base_any2vec.BaseAny2VecModel).args or
                                                           key in inspect.getfullargspec(
                                                               gensim.models.base_any2vec.BaseWordEmbeddingsModel).args}
                                           )

        print('Start clustering..')
        self.gmm = mixture.GMM(**{key: value for key, value in kwargs.items()
                                              if key in inspect.getfullargspec(mixture.GMM).args})
        

        self.doc_vectors = np.array(self.model.docvecs.vectors_docs)
        self.cluster_memberships = self.gmm.fit_predict(self.doc_vectors)
        print('Finished clustering.')
        self.BIC = self.gmm.bic(self.doc_vectors)
        self.cluster_center = self.gmm.means_
        print('BIC: {}'.format(self.BIC))

        self.get_document_topics()
        self.top_topic_center_words = pd.DataFrame(
            [self.most_similar_words_per_topic(topic, 200) for topic in range(self.gmm.n_components)])
        self.topic_similarities = pd.DataFrame(np.argsort(cosine_similarity(self.cluster_center))[:, ::-1]).T
//  [python]
def fit(self, **kwargs):
        '''
        First, a Doc2Vec model is trained and clustering of the documents is done by means of GMM.
        :param kwargs: additional arguments which should be passed to Doc2Vec and GMM.
        :param save: if you want to save the trained model set save=True.
        :param filename: name of the saved model.
        :return: Doc2Vec model and GMM clusters.
        '''
        # generate doc2vec model
        self.model = gensim.models.Doc2Vec(self.x_docs, **{key: value for key, value in kwargs.items()
                                                           if
                                                           key in inspect.getfullargspec(gensim.models.Doc2Vec).args or
                                                           key in inspect.getfullargspec(
                                                               gensim.models.base_any2vec.BaseAny2VecModel).args or
                                                           key in inspect.getfullargspec(
                                                               gensim.models.base_any2vec.BaseWordEmbeddingsModel).args}
                                           )

        print('Start clustering..')
        self.gmm = mixture.GaussianMixture(**{key: value for key, value in kwargs.items()
                                              if key in inspect.getfullargspec(mixture.GaussianMixture).args})
        

        self.doc_vectors = np.array(self.model.docvecs.vectors_docs)
        self.cluster_memberships = self.gmm.fit_predict(self.doc_vectors)
        print('Finished clustering.')
        self.BIC = self.gmm.bic(self.doc_vectors)
        self.cluster_center = self.gmm.means_
        print('BIC: {}'.format(self.BIC))

        self.get_document_topics()
        self.top_topic_center_words = pd.DataFrame(
            [self.most_similar_words_per_topic(topic, 200) for topic in range(self.gmm.n_components)])
        self.topic_similarities = pd.DataFrame(np.argsort(cosine_similarity(self.cluster_center))[:, ::-1]).T
//  [python]
def qa_plot_MoG(Xall,ncomp=2, src='STAR',nsamp=10000,outdir='.',extra=False,append=''): 
    '''Build a color-color plot.  Show the data on the left-hand panel and random draws from 
    the MoGs on the right-hand panel.'''
    if src == 'STAR':
        mog_file = 'legacypipe/data/star_colors_mog.fits'
        xrange = xyrange['x_%s' % src.lower()]
        yrange = xyrange['y_%s' % src.lower()]
        xlab='r - z'
        ylab='g - r'
    elif src == 'ELG':
        mog_file = 'legacypipe/data/elg_colors_mog.fits'
        xrange = xyrange['x_%s' % src.lower()]
        yrange = xyrange['y_%s' % src.lower()]
        xlab='r - z'
        ylab='g - r'
    elif src == 'LRG':
        mog_file = 'legacypipe/data/lrg_colors_mog.fits'
        xrange = xyrange['x_%s' % src.lower()]
        yrange = xyrange['y_%s' % src.lower()]
        xlab='r - z'
        ylab='r - w1'
    else: raise ValueError('src=%s not supported' % src)
    # Build MoG
    if ncomp is None:
        mog = priors._GaussianMixtureModel.load(mog_file)
    else:
        from sklearn.mixture import GMM
        mog = GMM(n_components=ncomp, covariance_type="full").fit(Xall)
    samp = mog.sample(n_samples=nsamp)
    #if extra: 
    #    # Higher accuracy sampling, but more time consuming and negligible improvment
    #    samp= mog.sample_full_pdf(nsamp)
    fig, ax = plt.subplots(1, 3, sharey=True,figsize=(12, 4))
    ax[0].plot(Xall[:,0],Xall[:,1], 'o', c='b', markersize=3)
    priors.add_MoG_curves(ax[1], mog.means_, mog.covars_, mog.weights_)
    ax[2].plot(samp[:,0], samp[:,1], 'o', c='b', markersize=3)
    # Add ts box
    if src != 'STAR':
        ts= priors.TSBox(src=src)
        for i in [0,2]: 
            ts.add_ts_box(ax[i], xlim=xrange,ylim=yrange)
    for i,title in zip(range(3),['Data','Gaussian Mixture','%d Draws' % nsamp]):
        xlab1=ax[i].set_xlabel(xlab)
        ax[i].set_xlim(xrange)
        ax[i].set_ylim(yrange)
        ti=ax[i].set_title(title)
    ylab1=ax[0].set_ylabel(ylab)
    fig.subplots_adjust(wspace=0) #, hspace=0.1)
    for i in range(2):
        priors.rm_last_ticklabel(ax[i])
    name= os.path.join(outdir,'qa-mog-sample-%s%s.png' % (src,append))
    print('Writing {}'.format(name))
    plt.savefig(name, bbox_extra_artists=[xlab1,ylab1,ti], bbox_inches='tight',dpi=150)
    plt.close()
//  [python]
def qa_plot_MoG(Xall,ncomp=2, src='STAR',nsamp=10000,outdir='.',extra=False,append=''): 
    '''Build a color-color plot.  Show the data on the left-hand panel and random draws from 
    the MoGs on the right-hand panel.'''
    if src == 'STAR':
        mog_file = 'legacypipe/data/star_colors_mog.fits'
        xrange = xyrange['x_%s' % src.lower()]
        yrange = xyrange['y_%s' % src.lower()]
        xlab='r - z'
        ylab='g - r'
    elif src == 'ELG':
        mog_file = 'legacypipe/data/elg_colors_mog.fits'
        xrange = xyrange['x_%s' % src.lower()]
        yrange = xyrange['y_%s' % src.lower()]
        xlab='r - z'
        ylab='g - r'
    elif src == 'LRG':
        mog_file = 'legacypipe/data/lrg_colors_mog.fits'
        xrange = xyrange['x_%s' % src.lower()]
        yrange = xyrange['y_%s' % src.lower()]
        xlab='r - z'
        ylab='r - w1'
    else: raise ValueError('src=%s not supported' % src)
    # Build MoG
    if ncomp is None:
        mog = priors._GaussianMixtureModel.load(mog_file)
    else:
        from sklearn.mixture import GaussianMixture
        mog = GaussianMixture(n_components=ncomp, covariance_type="full").fit(Xall)
    samp = mog.sample(n_samples=nsamp)[0]
    #if extra: 
    #    # Higher accuracy sampling, but more time consuming and negligible improvment
    #    samp= mog.sample_full_pdf(nsamp)
    fig, ax = plt.subplots(1, 3, sharey=True,figsize=(12, 4))
    ax[0].plot(Xall[:,0],Xall[:,1], 'o', c='b', markersize=3)
    priors.add_MoG_curves(ax[1], mog.means_, mog.covariances_, mog.weights_)
    ax[2].plot(samp[:,0], samp[:,1], 'o', c='b', markersize=3)
    # Add ts box
    if src != 'STAR':
        ts= priors.TSBox(src=src)
        for i in [0,2]: 
            ts.add_ts_box(ax[i], xlim=xrange,ylim=yrange)
    for i,title in zip(range(3),['Data','Gaussian Mixture','%d Draws' % nsamp]):
        xlab1=ax[i].set_xlabel(xlab)
        ax[i].set_xlim(xrange)
        ax[i].set_ylim(yrange)
        ti=ax[i].set_title(title)
    ylab1=ax[0].set_ylabel(ylab)
    fig.subplots_adjust(wspace=0) #, hspace=0.1)
    for i in range(2):
        priors.rm_last_ticklabel(ax[i])
    name= os.path.join(outdir,'qa-mog-sample-%s%s.png' % (src,append))
    print('Writing {}'.format(name))
    plt.savefig(name, bbox_extra_artists=[xlab1,ylab1,ti], bbox_inches='tight',dpi=150)
    plt.close()
//  [python]
def get_2d_grid_gmm(subdivisions=[5, 5], variance=0.04):
    """
    Compute the weight, mean and covariance of a 2D gmm placed on a 2D grid

    :param subdivisions: 2 element list of number of subdivisions of the 2D space in each axes to form the grid
    :param variance: scalar for spherical gmm.p
    :return gmm: gmm: instance of sklearn GaussianMixture (GMM) object Gauassian mixture model
    """
    # n_gaussians = reduce(lambda x, y: x*y,subdivisions)
    n_gaussians = np.prod(np.array(subdivisions))
    step = [1.0/(subdivisions[0]),  1.0/(subdivisions[1])]

    means = np.mgrid[step[0]-1: 1.0-step[0]: complex(0, subdivisions[0]),
            step[1]-1: 1.0-step[1]: complex(0, subdivisions[1])]
    means = np.reshape(means, [2,-1]).T
    covariances = variance*np.ones_like(means)
    weights = (1.0/n_gaussians)*np.ones(n_gaussians)
    gmm = mixture.GMM(n_components=n_gaussians, covariance_type='diag')
    gmm.weights_ = weights
    gmm.covariances_ = covariances
    gmm.means_ = means
    from sklearn.mixture.gmm import _compute_precision_cholesky
    gmm.precisions_cholesky_ = _compute_precision_cholesky(covariances, 'diag')
    return gmm
//  [python]
def get_2d_grid_gmm(subdivisions=[5, 5], variance=0.04):
    """
    Compute the weight, mean and covariance of a 2D gmm placed on a 2D grid

    :param subdivisions: 2 element list of number of subdivisions of the 2D space in each axes to form the grid
    :param variance: scalar for spherical gmm.p
    :return gmm: gmm: instance of sklearn GaussianMixture (GMM) object Gauassian mixture model
    """
    # n_gaussians = reduce(lambda x, y: x*y,subdivisions)
    n_gaussians = np.prod(np.array(subdivisions))
    step = [1.0/(subdivisions[0]),  1.0/(subdivisions[1])]

    means = np.mgrid[step[0]-1: 1.0-step[0]: complex(0, subdivisions[0]),
            step[1]-1: 1.0-step[1]: complex(0, subdivisions[1])]
    means = np.reshape(means, [2,-1]).T
    covariances = variance*np.ones_like(means)
    weights = (1.0/n_gaussians)*np.ones(n_gaussians)
    gmm = GaussianMixture(n_components=n_gaussians, covariance_type='diag')
    gmm.weights_ = weights
    gmm.covariances_ = covariances
    gmm.means_ = means
    from sklearn.mixture.gaussian_mixture import _compute_precision_cholesky
    gmm.precisions_cholesky_ = _compute_precision_cholesky(covariances, 'diag')
    return gmm
//  [python]
def get_3d_grid_gmm(subdivisions=[5,5,5], variance=0.04):
    """
    Compute the weight, mean and covariance of a gmm placed on a 3D grid
    :param subdivisions: 2 element list of number of subdivisions of the 3D space in each axes to form the grid
    :param variance: scalar for spherical gmm.p
    :return gmm: gmm: instance of sklearn GaussianMixture (GMM) object Gauassian mixture model
    """
    # n_gaussians = reduce(lambda x, y: x*y,subdivisions)
    n_gaussians = np.prod(np.array(subdivisions))
    step = [1.0/(subdivisions[0]),  1.0/(subdivisions[1]),  1.0/(subdivisions[2])]

    means = np.mgrid[ step[0]-1: 1.0-step[0]: complex(0, subdivisions[0]),
                      step[1]-1: 1.0-step[1]: complex(0, subdivisions[1]),
                      step[2]-1: 1.0-step[2]: complex(0, subdivisions[2])]
    means = np.reshape(means, [3, -1]).T
    covariances = variance*np.ones_like(means)
    weights = (1.0/n_gaussians)*np.ones(n_gaussians)
    gmm = mixture.GaussianMixture(n_components=n_gaussians, covariance_type='diag')
    gmm.weights_ = weights
    gmm.covariances_ = covariances
    gmm.means_ = means
    from sklearn.mixture.gaussian_mixture import _compute_precision_cholesky
    gmm.precisions_cholesky_ = _compute_precision_cholesky(covariances, 'diag')
    return gmm
//  [python]
def get_3d_grid_gmm(subdivisions=[5,5,5], variance=0.04):
    """
    Compute the weight, mean and covariance of a gmm placed on a 3D grid
    :param subdivisions: 2 element list of number of subdivisions of the 3D space in each axes to form the grid
    :param variance: scalar for spherical gmm.p
    :return gmm: gmm: instance of sklearn GaussianMixture (GMM) object Gauassian mixture model
    """
    # n_gaussians = reduce(lambda x, y: x*y,subdivisions)
    n_gaussians = np.prod(np.array(subdivisions))
    step = [1.0/(subdivisions[0]),  1.0/(subdivisions[1]),  1.0/(subdivisions[2])]

    means = np.mgrid[ step[0]-1: 1.0-step[0]: complex(0, subdivisions[0]),
                      step[1]-1: 1.0-step[1]: complex(0, subdivisions[1]),
                      step[2]-1: 1.0-step[2]: complex(0, subdivisions[2])]
    means = np.reshape(means, [3, -1]).T
    covariances = variance*np.ones_like(means)
    weights = (1.0/n_gaussians)*np.ones(n_gaussians)
    gmm = GaussianMixture(n_components=n_gaussians, covariance_type='diag')
    gmm.weights_ = weights
    gmm.covariances_ = covariances
    gmm.means_ = means
    from sklearn.mixture.gaussian_mixture import _compute_precision_cholesky
    gmm.precisions_cholesky_ = _compute_precision_cholesky(covariances, 'diag')
    return gmm
//  [python]
n_gaussians = means.shape[0]

points = []
for i in range(len(means)):
    x = np.random.multivariate_normal(means[i], covs[i], N )
    points.append(x)

points = np.concatenate(points)

gmm = mixture.GaussianMixture(n_components=n_gaussians, covariance_type='diag')
//  [python]
n_gaussians = means.shape[0]

points = []
for i in range(len(means)):
    x = np.random.multivariate_normal(means[i], covs[i], N )
    points.append(x)

points = np.concatenate(points)

gmm = GaussianMixture(n_components=n_gaussians, covariance_type='diag')
//  [python]
def run_method(self, x, n_clusters):
        kmeans = mixture.GaussianMixture(n_clusters=n_clusters)
        I = kmeans.fit_predict(x)
        return I
//  [python]
def run_method(self, x, n_clusters):
        kmeans = GaussianMixture(n_clusters=n_clusters)
        I = kmeans.fit_predict(x)
        return I
//  [python]
def select_topic_number(self, metric = ['BIC','Silohuette'], iterations = 10):
        from matplotlib.patches import Ellipse
        from sklearn.mixture import GaussianMixture as GMM
        from sklearn import metrics
        from matplotlib import rcParams
        rcParams['figure.figsize'] = 16, 8
        n_clusters = np.arange(2, self.gmm.n_components)
        if metric == 'Silohuette':
            sils = []
            sils_err = []
            iterations = interations
            for n in n_clusters:
                tmp_sil = []
                for _ in range(iterations):
                    gmm = GMM(n, n_init=2).fit(self.doc_vectors)
                    labels = gmm.predict(self.doc_vectors)
                    sil = metrics.silhouette_score(self.doc_vectors, labels, metric='euclidean')
                    tmp_sil.append(sil)
                val = np.mean(self.SelBest(np.array(tmp_sil), int(iterations / 5)))
                err = np.std(tmp_sil)
                sils.append(val)
                sils_err.append(err)
            plt.errorbar(n_clusters, sils, yerr=sils_err)
            plt.title("Silhouette Scores", fontsize=20)
            plt.xticks(n_clusters)
            plt.xlabel("N. of clusters")
            plt.ylabel("Score")
        elif metric == 'BIC':
            bics = []
            bics_err = []
            iterations = iterations
            for n in n_clusters:
                tmp_bic = []
                for _ in range(iterations):
                    gmm = GMM(n, n_init=2).fit(self.doc_vectors)

                    tmp_bic.append(gmm.bic(self.doc_vectors))
                val = np.mean(self.SelBest(np.array(tmp_bic), int(iterations / 5)))
                err = np.std(tmp_bic)
                bics.append(val)
                bics_err.append(err)
            print('The minimal BIC score is reached for ', n_clusters[bics.index(min(bics))])
            print("Check where the BIC curve's change in slope is big. For this reason, take a look at the gradient values.")
            plt.errorbar(n_clusters, np.gradient(bics), yerr=bics_err, label='BIC')
            plt.title("Gradient of BIC Scores", fontsize=20)
            plt.xticks(n_clusters)
            plt.xlabel("N. of clusters")
            plt.ylabel("grad(BIC)")
            plt.legend()
//  [python]
def set_superpixel_assignment(self):
        """Fit gaussian mixture model to features and get assignment."""
        from sklearn.mixture import GMM

        mmodel = GMM(n_components=self.cd.n_gaussian_components)
        self.spixel_labels = mmodel.fit_predict(self.fdata.values) + 1
//  [python]
def set_superpixel_assignment(self):
        """Fit gaussian mixture model to features and get assignment."""
        from sklearn.mixture import GaussianMixture

        mmodel = GaussianMixture(n_components=self.cd.n_gaussian_components)
        self.spixel_labels = mmodel.fit_predict(self.fdata.values) + 1
//  [python]
def fit(self, all_features):
    # Fit sklearn GMM to obtain parameters
    print("Fitting GMM Model, Params: num_components:{}, covariance_type: {}, reg_covar: {}".format(self.n_components,
                                                                                                    self.covariance_type,
                                                                                                    self.reg_covar))
    gmm = mixture.GaussianMixture(
      n_components=self.n_components,
      covariance_type=self.covariance_type,
      reg_covar=self.reg_covar,
    )
    gmm.fit(all_features)

    # Convert params to torch and save them
    cov = gmm.covariances_
    if self.covariance_type == 'tied':
      # covariance for each component is the same
      cov = np.tile(np.expand_dims(cov, 0), (self.n_components, 1, 1))
    elif self.covariance_type == 'diag':
      # transform from diagonal vector to matrix
      newcov = np.zeros((self.n_components, cov.shape[-1], cov.shape[-1]))
      for i in range(self.n_components):
        # np.diag only works on 1-dimensional arrays
        newcov[i] = np.diag(cov[i])
      cov = newcov
    cov = torch.as_tensor(cov)

    self.init_gmm(self.n_features, n_components=self.n_components,
                  means=torch.as_tensor(gmm.means_),
                  covariances=cov,
                  weights=torch.as_tensor(gmm.weights_))
//  [python]
def fit(self, all_features):
    # Fit sklearn GMM to obtain parameters
    print("Fitting GMM Model, Params: num_components:{}, covariance_type: {}, reg_covar: {}".format(self.n_components,
                                                                                                    self.covariance_type,
                                                                                                    self.reg_covar))
    gmm = GaussianMixture(
      n_components=self.n_components,
      covariance_type=self.covariance_type,
      reg_covar=self.reg_covar,
    )
    gmm.fit(all_features)

    # Convert params to torch and save them
    cov = gmm.covariances_
    if self.covariance_type == 'tied':
      # covariance for each component is the same
      cov = np.tile(np.expand_dims(cov, 0), (self.n_components, 1, 1))
    elif self.covariance_type == 'diag':
      # transform from diagonal vector to matrix
      newcov = np.zeros((self.n_components, cov.shape[-1], cov.shape[-1]))
      for i in range(self.n_components):
        # np.diag only works on 1-dimensional arrays
        newcov[i] = np.diag(cov[i])
      cov = newcov
    cov = torch.as_tensor(cov)

    self.init_gmm(self.n_features, n_components=self.n_components,
                  means=torch.as_tensor(gmm.means_),
                  covariances=cov,
                  weights=torch.as_tensor(gmm.weights_))
//  [python]
def test_fit_nll(
    num_datapoints: int,
    batch_size: int,
    num_features: int,
    num_components: int,
    covariance_type: CovarianceType,
):
    data, _ = sample_gmm(
        num_datapoints=num_datapoints,
        num_features=num_features,
        num_components=num_components,
        covariance_type=covariance_type,
    )

    # Ours
    estimator = GaussianMixture(
        num_components,
        covariance_type=covariance_type,
        batch_size=batch_size,
        trainer_params=dict(precision=64),
    )
    ours_nll = estimator.fit(data).score(data)

    # Sklearn
    gmm = SklearnGaussianMixture(num_components, covariance_type=covariance_type)
    sklearn_nll = gmm.fit(data.numpy()).score(data.numpy())

    assert math.isclose(ours_nll, -sklearn_nll, rel_tol=0.01, abs_tol=0.01)
//  [python]
def pretrain(self, adj, features, adj_label, y, weight_tensor, norm, epochs, lr, save_path, dataset):
        opti = Adam(self.parameters(), lr=lr)
        epoch_bar = tqdm(range(epochs))
        gmm = mixture.GaussianMixture(n_components = self.nClusters , covariance_type = 'diag')
        for _ in epoch_bar:
            opti.zero_grad()
            _,_, z = self.encode(features, adj)
            x_ = self.decode(z)
            loss = norm*F.binary_cross_entropy(x_.view(-1), adj_label.to_dense().view(-1), weight = weight_tensor)
            loss.backward()
            opti.step()
        gmm.fit_predict(z.detach().numpy())
        self.pi.data = torch.from_numpy(gmm.weights_)
        self.mu_c.data = torch.from_numpy(gmm.means_)
        self.log_sigma2_c.data =  torch.log(torch.from_numpy(gmm.covariances_))
        self.logstd = self.mean
//  [python]
def pretrain(self, adj, features, adj_label, y, weight_tensor, norm, epochs, lr, save_path, dataset):
        opti = Adam(self.parameters(), lr=lr)
        epoch_bar = tqdm(range(epochs))
        gmm = GaussianMixture(n_components = self.nClusters , covariance_type = 'diag')
        for _ in epoch_bar:
            opti.zero_grad()
            _,_, z = self.encode(features, adj)
            x_ = self.decode(z)
            loss = norm*F.binary_cross_entropy(x_.view(-1), adj_label.to_dense().view(-1), weight = weight_tensor)
            loss.backward()
            opti.step()
        gmm.fit_predict(z.detach().numpy())
        self.pi.data = torch.from_numpy(gmm.weights_)
        self.mu_c.data = torch.from_numpy(gmm.means_)
        self.log_sigma2_c.data =  torch.log(torch.from_numpy(gmm.covariances_))
        self.logstd = self.mean
//  [python]
def gaussian_mixture_model(latents, params):
    gm = mixture.GaussianMixture(n_components=4, random_state=0, init_params='kmeans').fit(latents)
    print('Average negative log likelihood:', -1 * gm.score(latents))
    if params['visualize']:
        plot_gmm(gm, latents)
        scores = []
        for i in range(1, 8):
            gm = mixture.GaussianMixture(n_components=i, random_state=0, init_params='kmeans').fit(latents)
            scores.append(-1 * gm.score(latents))
        sns.set_style("darkgrid")
        plt.figure()
        plt.scatter(range(1, 8), scores, color='green')
        plt.plot(range(1, 8), scores)
        plt.savefig(params['folder_dir'] + '/gaussian_mixture_model.png', format='png', dpi=300)
        plt.show()
    return gm
//  [python]
def gaussian_mixture_model(latents, params):
    gm = GaussianMixture(n_components=4, random_state=0, init_params='kmeans').fit(latents)
    print('Average negative log likelihood:', -1 * gm.score(latents))
    if params['visualize']:
        plot_gmm(gm, latents)
        scores = []
        for i in range(1, 8):
            gm = GaussianMixture(n_components=i, random_state=0, init_params='kmeans').fit(latents)
            scores.append(-1 * gm.score(latents))
        sns.set_style("darkgrid")
        plt.figure()
        plt.scatter(range(1, 8), scores, color='green')
        plt.plot(range(1, 8), scores)
        plt.savefig(params['folder_dir'] + '/gaussian_mixture_model.png', format='png', dpi=300)
        plt.show()
    return gm
//  [python]
def fit(self, X, y) -> "GaussianMixtureNB":
        """Fit the `GaussianMixtureNB` estimator using `X` and `y` as training data by fitting a `GaussianMixture` model
        for each class in the target and for each feature in the data, on the subset of `X` where `y == class`.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features )
            The training data.
        y : array-like of shape (n_samples,)
            The target values.

        Returns
        -------
        self : GaussianMixtureNB
            The fitted estimator.
        """
        X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES)
        if X.ndim == 1:
            X = np.expand_dims(X, 1)

        self.gmms_ = {}
        self.classes_ = unique_labels(y)
        self.n_features_in_ = X.shape[1]
        for c in self.classes_:
            subset_x, subset_y = X[y == c], y[y == c]
            self.gmms_[c] = [
                mixture.GaussianMixture(
                    n_components=self.n_components,
                    covariance_type=self.covariance_type,
                    tol=self.tol,
                    reg_covar=self.reg_covar,
                    max_iter=self.max_iter,
                    n_init=self.n_init,
                    init_params=self.init_params,
                    weights_init=self.weights_init,
                    means_init=self.means_init,
                    precisions_init=self.precisions_init,
                    random_state=self.random_state,
                    warm_start=self.warm_start,
                ).fit(subset_x[:, i].reshape(-1, 1), subset_y)
                for i in range(X.shape[1])
            ]
        return self
//  [python]
def fit(self, X, y) -> "GaussianMixtureNB":
        """Fit the `GaussianMixtureNB` estimator using `X` and `y` as training data by fitting a `GaussianMixture` model
        for each class in the target and for each feature in the data, on the subset of `X` where `y == class`.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features )
            The training data.
        y : array-like of shape (n_samples,)
            The target values.

        Returns
        -------
        self : GaussianMixtureNB
            The fitted estimator.
        """
        X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES)
        if X.ndim == 1:
            X = np.expand_dims(X, 1)

        self.gmms_ = {}
        self.classes_ = unique_labels(y)
        self.n_features_in_ = X.shape[1]
        for c in self.classes_:
            subset_x, subset_y = X[y == c], y[y == c]
            self.gmms_[c] = [
                GaussianMixture(
                    n_components=self.n_components,
                    covariance_type=self.covariance_type,
                    tol=self.tol,
                    reg_covar=self.reg_covar,
                    max_iter=self.max_iter,
                    n_init=self.n_init,
                    init_params=self.init_params,
                    weights_init=self.weights_init,
                    means_init=self.means_init,
                    precisions_init=self.precisions_init,
                    random_state=self.random_state,
                    warm_start=self.warm_start,
                ).fit(subset_x[:, i].reshape(-1, 1), subset_y)
                for i in range(X.shape[1])
            ]
        return self
//  [python]
def __init__(self, means=0, sigmas=1, weights=1):
        data = np.array([t for t in np.broadcast(means, sigmas, weights)])

        components = data.shape[0]
        self._gmm = mixture.GaussianMixture(components, covariance_type='spherical')

        self._gmm.means_ = data[:, :1]
        self._gmm.weights_ = data[:, 2] / data[:, 2].sum()
        self._gmm.covariances_ = data[:, 1] ** 2

        self._gmm.precisions_cholesky_ = 1 / np.sqrt(self._gmm.covariances_)

        self._gmm.fit = None
//  [python]
def __init__(self, means=0, sigmas=1, weights=1):
        data = np.array([t for t in np.broadcast(means, sigmas, weights)])

        components = data.shape[0]
        self._gmm = GaussianMixture(components, covariance_type='spherical')

        self._gmm.means_ = data[:, :1]
        self._gmm.weights_ = data[:, 2] / data[:, 2].sum()
        self._gmm.covariances_ = data[:, 1] ** 2

        self._gmm.precisions_cholesky_ = 1 / np.sqrt(self._gmm.covariances_)

        self._gmm.fit = None
//  [python]
def gmm_fit(neurons_fs, n_components, algo='bayes', n_init=50, random_state=None, mean_precision_prior=None,
            weight_concentration_prior_type='dirichlet_process', weight_concentration_prior=None):
    """
    fit a mixture of gaussians to a set of vectors
    :param neurons_fs: list of numpy arrays of shape n or numpy array of shape n x d
    :param n_components: int
    :param algo: 'em' or 'bayes'
    :param n_init: number of random seeds for the inference algorithm
    :param random_state: random seed for the rng to eliminate randomness
    :return: vector of population labels (of shape n), best fitted model
    """
    if isinstance(neurons_fs, list):
        X = np.vstack(neurons_fs).transpose()
    else:
        X = neurons_fs
    if algo == "em":
        model = mixture.GaussianMixture(n_components=n_components, n_init=n_init, random_state=random_state)
    else:
        model = mixture.BayesianGaussianMixture(n_components=n_components, n_init=n_init, random_state=random_state,
                                        init_params='random', mean_precision_prior=mean_precision_prior,
                                        weight_concentration_prior_type=weight_concentration_prior_type,
                                        weight_concentration_prior=weight_concentration_prior)
    model.fit(X)
    z = model.predict(X)
    return z, model
//  [python]
def gmm_fit(neurons_fs, n_components, algo='bayes', n_init=50, random_state=None, mean_precision_prior=None,
            weight_concentration_prior_type='dirichlet_process', weight_concentration_prior=None):
    """
    fit a mixture of gaussians to a set of vectors
    :param neurons_fs: list of numpy arrays of shape n or numpy array of shape n x d
    :param n_components: int
    :param algo: 'em' or 'bayes'
    :param n_init: number of random seeds for the inference algorithm
    :param random_state: random seed for the rng to eliminate randomness
    :return: vector of population labels (of shape n), best fitted model
    """
    if isinstance(neurons_fs, list):
        X = np.vstack(neurons_fs).transpose()
    else:
        X = neurons_fs
    if algo == "em":
        model = GaussianMixture(n_components=n_components, n_init=n_init, random_state=random_state)
    else:
        model = BayesianGaussianMixture(n_components=n_components, n_init=n_init, random_state=random_state,
                                        init_params='random', mean_precision_prior=mean_precision_prior,
                                        weight_concentration_prior_type=weight_concentration_prior_type,
                                        weight_concentration_prior=weight_concentration_prior)
    model.fit(X)
    z = model.predict(X)
    return z, model
//  [python]
def gaussian_mixture(
    n_components: Annotated[int, {"min": 1, "max": 100}] = 1,
    covariance_type: Literal["full", "tied", "diag", "spherical"] = "full",
    tol: str = "1e-3",
    reg_covar: Annotated[float, {"min": 0.0, "max": 100.0}] = 1e-6,
    max_iter: Annotated[int, {"min": 1, "max": 1000}] = 100,
    n_init: Annotated[int, {"min": 1, "max": 100}] = 1,
    init_params: Literal["kmeans", "random"] = "kmeans",
    random_state: str = "",
):
    random_state = _normalize_random_state(random_state)
    tol = float(tol)
    return mixture.GaussianMixture(
        n_components=n_components,
        covariance_type=covariance_type,
        tol=tol,
        reg_covar=reg_covar,
        max_iter=max_iter,
        n_init=n_init,
        init_params=init_params,
        random_state=random_state,
    )
//  [python]
def gaussian_mixture(
    n_components: Annotated[int, {"min": 1, "max": 100}] = 1,
    covariance_type: Literal["full", "tied", "diag", "spherical"] = "full",
    tol: str = "1e-3",
    reg_covar: Annotated[float, {"min": 0.0, "max": 100.0}] = 1e-6,
    max_iter: Annotated[int, {"min": 1, "max": 1000}] = 100,
    n_init: Annotated[int, {"min": 1, "max": 100}] = 1,
    init_params: Literal["kmeans", "random"] = "kmeans",
    random_state: str = "",
):
    random_state = _normalize_random_state(random_state)
    tol = float(tol)
    return GaussianMixture(
        n_components=n_components,
        covariance_type=covariance_type,
        tol=tol,
        reg_covar=reg_covar,
        max_iter=max_iter,
        n_init=n_init,
        init_params=init_params,
        random_state=random_state,
    )
//  [python]
def zero_center_normalize(df, samples, logInput=False, method="median"):
    """
    Transforming input peptide abundance table into log2-scale and centralize to zero.
    Inputs:
        df :        dataframe of peptide abundances
        samples:    column names of selected samples
        logInput:   input abundances are already in log scale
        method:     method for estimating zero point
    Return:
        df:         the dataframe of peptide abundances after normalization
    """
    assert method in (
        "median",
        "average",
        "GMM",
    ), "Zero centering method has to be among median, average or GMM!"

    if not logInput:
        # convert abundances to log2 scale
        df[samples] = df[samples].apply(np.log2)
    if method == "average":
        norm_scale = np.nanmean(df[samples], axis=0)
    elif method == "median":
        norm_scale = np.nanmedian(df[samples], axis=0)
    elif method == "GMM":
        """ two-component Gaussian mixture model """
        from sklearn.mixture import GMM

        gmm = GMM(2)

        norm_scale = []
        for sp in samples:
            v = df[sp].values
            v = v[np.logical_not(np.isnan(v))]
            v = v[np.logical_not(np.isinf(v))]
            try:
                gmm.fit(np.matrix(v).T)
                vmean = gmm.means_[np.argmin(gmm.covariances_)][0]
                norm_scale.append(vmean)
            except:
                norm_scale.append(np.nanmean(v))
        norm_scale = np.array(norm_scale)

        print(
            "Caution!!",
            "Two-component Gaussian mixture model is used to center peptide abundances!",
            "Centring factors are:",
            *[
                "Sample:{}\tGMM:{:.3f}\t Median:{:.3f}".format(s, g, m)
                for s, g, m in zip(
                    samples, norm_scale, np.nanmedian(df[samples], axis=0)
                )
            ],
            "Check if GMM estimated values deviate greatly from median values.",
            "If in doubt, use other metrics (e.g. median) to centre the abundances!!\n",
            sep="\n"
        )
    df[samples] = df[samples] - norm_scale
    return df
//  [python]
def zero_center_normalize(df, samples, logInput=False, method="median"):
    """
    Transforming input peptide abundance table into log2-scale and centralize to zero.
    Inputs:
        df :        dataframe of peptide abundances
        samples:    column names of selected samples
        logInput:   input abundances are already in log scale
        method:     method for estimating zero point
    Return:
        df:         the dataframe of peptide abundances after normalization
    """
    assert method in (
        "median",
        "average",
        "GMM",
    ), "Zero centering method has to be among median, average or GMM!"

    if not logInput:
        # convert abundances to log2 scale
        df[samples] = df[samples].apply(np.log2)
    if method == "average":
        norm_scale = np.nanmean(df[samples], axis=0)
    elif method == "median":
        norm_scale = np.nanmedian(df[samples], axis=0)
    elif method == "GMM":
        """ two-component Gaussian mixture model """
        from sklearn.mixture import GaussianMixture as GMM

        gmm = GMM(2)

        norm_scale = []
        for sp in samples:
            v = df[sp].values
            v = v[np.logical_not(np.isnan(v))]
            v = v[np.logical_not(np.isinf(v))]
            try:
                gmm.fit(np.matrix(v).T)
                vmean = gmm.means_[np.argmin(gmm.covariances_)][0]
                norm_scale.append(vmean)
            except:
                norm_scale.append(np.nanmean(v))
        norm_scale = np.array(norm_scale)

        print(
            "Caution!!",
            "Two-component Gaussian mixture model is used to center peptide abundances!",
            "Centring factors are:",
            *[
                "Sample:{}\tGMM:{:.3f}\t Median:{:.3f}".format(s, g, m)
                for s, g, m in zip(
                    samples, norm_scale, np.nanmedian(df[samples], axis=0)
                )
            ],
            "Check if GMM estimated values deviate greatly from median values.",
            "If in doubt, use other metrics (e.g. median) to centre the abundances!!\n",
            sep="\n"
        )
    df[samples] = df[samples] - norm_scale
    return df
//  [python]
def get_split_rows_GMM(n_clusters=2, pre_proc=None, ohe=False, seed=17, max_iter=100, n_init=2, covariance_type="full"):
    """
    covariance_type can be one of 'spherical', 'diag', 'tied', 'full'
    """

    def split_rows_GMM(local_data, ds_context, scope):
        data = preproc(local_data, ds_context, pre_proc, ohe)

        estimator = mixture.GaussianMixture(
            n_components=n_clusters,
            covariance_type=covariance_type,
            max_iter=max_iter,
            n_init=n_init,
            random_state=seed,
        )

        clusters = estimator.fit(data).predict(data)

        return split_data_by_clusters(local_data, clusters, scope, rows=True)

    return split_rows_GMM
//  [python]
def get_split_rows_GMM(n_clusters=2, pre_proc=None, ohe=False, seed=17, max_iter=100, n_init=2, covariance_type="full"):
    """
    covariance_type can be one of 'spherical', 'diag', 'tied', 'full'
    """

    def split_rows_GMM(local_data, ds_context, scope):
        data = preproc(local_data, ds_context, pre_proc, ohe)

        estimator = GaussianMixture(
            n_components=n_clusters,
            covariance_type=covariance_type,
            max_iter=max_iter,
            n_init=n_init,
            random_state=seed,
        )

        clusters = estimator.fit(data).predict(data)

        return split_data_by_clusters(local_data, clusters, scope, rows=True)

    return split_rows_GMM
//  [python]
def gmm(matpc, range_GMM=None, fixed_nc=None, max_iter=10000,
        n_init=1, tol=1.e-3, reg_covar=1.e-6, verbose=False):


    if range_GMM is not None:
        # Compute the GMM for different number of clusters set by "range_GMM"
        models = [mixture.GaussianMixture(n, covariance_type='full', random_state=0,
                                  max_iter=max_iter, tol=tol,
                                  reg_covar=reg_covar).fit(matpc) for n in range_GMM]
        # Compute the Bayesian information criterion (BIC) for each model
        BICF = [m.bic(matpc) / 1000 for m in models]
        # Determine the best number of clusters using the knee (or elbow)
        kn = KneeLocator(range_GMM, BICF, S=1, curve='convex',
                         direction='decreasing')
        n_clusters = kn.knee
        if n_clusters is None:
            n_clusters = min(range_GMM)
    else:
        models = []
        n_clusters = fixed_nc
        BICF = None

    # Perform clustering for the best number of clusters
    if n_init > 1:
        init_type = "random"
    else:
        init_type = "kmeans"
    gmix = mixture.GaussianMixture(n_components=n_clusters, covariance_type='full',
                           max_iter=max_iter, tol=tol, n_init=n_init,
                           reg_covar=reg_covar, verbose=verbose,
                           verbose_interval=max_iter//10,
                           init_params=init_type)
    gmix.fit(matpc)
    gmixfinPCA = gmix.predict(matpc)
    probs = gmix.predict_proba(matpc)

    return models, n_clusters, gmixfinPCA, probs, BICF
//  [python]
def gmm(matpc, range_GMM=None, fixed_nc=None, max_iter=10000,
        n_init=1, tol=1.e-3, reg_covar=1.e-6, verbose=False):


    if range_GMM is not None:
        # Compute the GMM for different number of clusters set by "range_GMM"
        models = [GaussianMixture(n, covariance_type='full', random_state=0,
                                  max_iter=max_iter, tol=tol,
                                  reg_covar=reg_covar).fit(matpc) for n in range_GMM]
        # Compute the Bayesian information criterion (BIC) for each model
        BICF = [m.bic(matpc) / 1000 for m in models]
        # Determine the best number of clusters using the knee (or elbow)
        kn = KneeLocator(range_GMM, BICF, S=1, curve='convex',
                         direction='decreasing')
        n_clusters = kn.knee
        if n_clusters is None:
            n_clusters = min(range_GMM)
    else:
        models = []
        n_clusters = fixed_nc
        BICF = None

    # Perform clustering for the best number of clusters
    if n_init > 1:
        init_type = "random"
    else:
        init_type = "kmeans"
    gmix = GaussianMixture(n_components=n_clusters, covariance_type='full',
                           max_iter=max_iter, tol=tol, n_init=n_init,
                           reg_covar=reg_covar, verbose=verbose,
                           verbose_interval=max_iter//10,
                           init_params=init_type)
    gmix.fit(matpc)
    gmixfinPCA = gmix.predict(matpc)
    probs = gmix.predict_proba(matpc)

    return models, n_clusters, gmixfinPCA, probs, BICF
//  [python]
def run_self_correction_augmentation_loop(self, n_steps, synth_aug_percent):
        """
        Augmentation loop where we sample and apply self-correction, and augment the gt data
        """
        self.n_steps = n_steps

        gt_data = self.rng.multivariate_normal(self.initial_mean, self.initial_cov, self.n_samples)

        # initialize model by fitting it on A
        gmm: mixture.GaussianMixture = (
            mixture.GaussianMixture(n_components=self.components, tol=1e-9, random_state=1).fit(gt_data)
        )

        means, covs = [gmm.means_[0]], [gmm.covariances_[0]]  # initial means and covariances
        w2s = [self.w2_metric_gaussian(self.initial_mean, self.initial_cov, means[0], covs[0])]

        n_samples_synthetic = int(synth_aug_percent * self.n_samples)

        print(
            f"Running self_correction_augmentation_loop simulation for "
            f"gamma = {(self.projection_strength / (1 - self.projection_strength)):.3f}"
        )

        for i in tqdm(range(n_steps)):
            # get the new parameters computed from the latest iteration of the gaussian mixture model
            mean = gmm.means_[0]
            cov = gmm.covariances_[0]

            # sample synthetic data from Gaussian with new parameters
            synth_data = self.rng.multivariate_normal(mean, cov, n_samples_synthetic)

            # project this synthetic data into a more physically plausible space
            synth_data_projection = self.self_correction(synth_data, step=i)

            # define the augmented dataset
            gt_data_augmented = np.concatenate((gt_data, synth_data_projection), axis=0)

            # re-fit the ground model to the new augmented data
            np.random.shuffle(gt_data_augmented)
            gmm = mixture.GaussianMixture(
                n_components=self.components, warm_start=True, random_state=1
            ).fit(gt_data_augmented)

            means.append(gmm.means_[0])
            covs.append(gmm.covariances_[0])
            w2s.append(self.w2_metric_gaussian(self.target_mean, self.target_cov, means[-1], covs[-1]))

        return w2s
//  [python]
def run_self_correction_augmentation_loop(self, n_steps, synth_aug_percent):
        """
        Augmentation loop where we sample and apply self-correction, and augment the gt data
        """
        self.n_steps = n_steps

        gt_data = self.rng.multivariate_normal(self.initial_mean, self.initial_cov, self.n_samples)

        # initialize model by fitting it on A
        gmm: GaussianMixture = (
            GaussianMixture(n_components=self.components, tol=1e-9, random_state=1).fit(gt_data)
        )

        means, covs = [gmm.means_[0]], [gmm.covariances_[0]]  # initial means and covariances
        w2s = [self.w2_metric_gaussian(self.initial_mean, self.initial_cov, means[0], covs[0])]

        n_samples_synthetic = int(synth_aug_percent * self.n_samples)

        print(
            f"Running self_correction_augmentation_loop simulation for "
            f"gamma = {(self.projection_strength / (1 - self.projection_strength)):.3f}"
        )

        for i in tqdm(range(n_steps)):
            # get the new parameters computed from the latest iteration of the gaussian mixture model
            mean = gmm.means_[0]
            cov = gmm.covariances_[0]

            # sample synthetic data from Gaussian with new parameters
            synth_data = self.rng.multivariate_normal(mean, cov, n_samples_synthetic)

            # project this synthetic data into a more physically plausible space
            synth_data_projection = self.self_correction(synth_data, step=i)

            # define the augmented dataset
            gt_data_augmented = np.concatenate((gt_data, synth_data_projection), axis=0)

            # re-fit the ground model to the new augmented data
            np.random.shuffle(gt_data_augmented)
            gmm = GaussianMixture(
                n_components=self.components, warm_start=True, random_state=1
            ).fit(gt_data_augmented)

            means.append(gmm.means_[0])
            covs.append(gmm.covariances_[0])
            w2s.append(self.w2_metric_gaussian(self.target_mean, self.target_cov, means[-1], covs[-1]))

        return w2s
//  [python]
def __get_model(self, k: int):
        if self.algorithm == "gmm":
            return mixture.GaussianMixture(n_components=k, random_state=self.random_state)
        return KMeans(n_clusters=k, random_state=self.random_state)
//  [python]
def __get_model(self, k: int):
        if self.algorithm == "gmm":
            return GaussianMixture(n_components=k, random_state=self.random_state)
        return KMeans(n_clusters=k, random_state=self.random_state)