jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def mixture_model(self, data_table, col):

        print('Applying mixture models')
        # Fit a mixture model to our data.
        data = data_table[data_table[col].notnull()][col]
        g = mixture.GaussianMixture(n_components=3, max_iter=100, n_init=1)
        reshaped_data = np.array(data.values.reshape(-1, 1))
        g.fit(reshaped_data)

        # Predict the probabilities
        probs = g.score_samples(reshaped_data)

        # Create the right data frame and concatenate the two.
        data_probs = pd.DataFrame(
            np.power(10, probs), index=data.index, columns=[col+'_mixture'])

        data_table = pd.concat([data_table, data_probs], axis=1)

        return data_table
//  [python]
def mixture_model(self, data_table, col):

        print('Applying mixture models')
        # Fit a mixture model to our data.
        data = data_table[data_table[col].notnull()][col]
        g = GaussianMixture(n_components=3, max_iter=100, n_init=1)
        reshaped_data = np.array(data.values.reshape(-1, 1))
        g.fit(reshaped_data)

        # Predict the probabilities
        probs = g.score_samples(reshaped_data)

        # Create the right data frame and concatenate the two.
        data_probs = pd.DataFrame(
            np.power(10, probs), index=data.index, columns=[col+'_mixture'])

        data_table = pd.concat([data_table, data_probs], axis=1)

        return data_table
//  [python]
def noise_detect(self, cluster_labels, labels, features):
        opt = self.opt

        centers = F.normalize(cluster_labels.T.mm(features), dim=1)
        context_assignments_logits = features.mm(centers.T) / opt.temp
        context_assignments = F.softmax(context_assignments_logits, dim=1)
        losses = - context_assignments[torch.arange(labels.size(0)), labels]
        losses = losses.cpu().numpy()[:, np.newaxis]
        losses = (losses - losses.min()) / (losses.max() - losses.min())
        labels = labels.cpu().numpy()

        from sklearn.mixture import GMM
        confidence = np.zeros((losses.shape[0],))
        if opt.sep_gmm:
            for i in range(self.num_cluster):
                mask = labels == i
                c = losses[mask, :]
                gm = GMM(n_components=2, random_state=0).fit(c)
                pdf = gm.predict_proba(c)
                confidence[mask] = (pdf / pdf.sum(1)[:, np.newaxis])[:, np.argmin(gm.means_)]
        else:
            gm = GMM(n_components=2, random_state=0).fit(losses)
            pdf = gm.predict_proba(losses)
            confidence = (pdf / pdf.sum(1)[:, np.newaxis])[:, np.argmin(gm.means_)]
        confidence = torch.from_numpy(confidence).float().cuda()
        return confidence, context_assignments, centers
//  [python]
def noise_detect(self, cluster_labels, labels, features):
        opt = self.opt

        centers = F.normalize(cluster_labels.T.mm(features), dim=1)
        context_assignments_logits = features.mm(centers.T) / opt.temp
        context_assignments = F.softmax(context_assignments_logits, dim=1)
        losses = - context_assignments[torch.arange(labels.size(0)), labels]
        losses = losses.cpu().numpy()[:, np.newaxis]
        losses = (losses - losses.min()) / (losses.max() - losses.min())
        labels = labels.cpu().numpy()

        from sklearn.mixture import GaussianMixture
        confidence = np.zeros((losses.shape[0],))
        if opt.sep_gmm:
            for i in range(self.num_cluster):
                mask = labels == i
                c = losses[mask, :]
                gm = GaussianMixture(n_components=2, random_state=0).fit(c)
                pdf = gm.predict_proba(c)
                confidence[mask] = (pdf / pdf.sum(1)[:, np.newaxis])[:, np.argmin(gm.means_)]
        else:
            gm = GaussianMixture(n_components=2, random_state=0).fit(losses)
            pdf = gm.predict_proba(losses)
            confidence = (pdf / pdf.sum(1)[:, np.newaxis])[:, np.argmin(gm.means_)]
        confidence = torch.from_numpy(confidence).float().cuda()
        return confidence, context_assignments, centers
//  [python]
def select_K(self):
        def SelBest(arr:list, X:int)->list:
            '''
            returns the set of X configurations with shorter distance
            '''
            dx=np.argsort(arr)[:X]
            return arr[dx]
        try:
            n_clusters=np.arange(2, 25)
            sils = []
            sils_err = []
            iterations = 20
            for n in n_clusters:
                tmp_sil = []
                for _ in range(iterations):
                    gmm = mixture.GaussianMixture(n, n_init = 2, reg_covar=1e-6).fit(self.components)
                    labels = gmm.predict(self.components)
                    sil = silhouette_score(self.components, labels, metric='euclidean')
                    tmp_sil.append(sil)
                val = np.mean(SelBest(np.array(tmp_sil),int(iterations/5)))
                err = np.std(tmp_sil)
                sils.append(val)
                sils_err.append(err)
        except:
            pass
        if len(sils):
            self.optimK = sils.index(max(sils)) + 2
            self.sils_score = sils
            self.model = mixture.GaussianMixture(self.optimK, n_init = 2, reg_covar=1e-6).fit(self.components)
        else:
            assert False, "No optim K, So fail to get GMM"
        return self
//  [python]
def select_K(self):
        def SelBest(arr:list, X:int)->list:
            '''
            returns the set of X configurations with shorter distance
            '''
            dx=np.argsort(arr)[:X]
            return arr[dx]
        try:
            n_clusters=np.arange(2, 25)
            sils = []
            sils_err = []
            iterations = 20
            for n in n_clusters:
                tmp_sil = []
                for _ in range(iterations):
                    gmm = GaussianMixture(n, n_init = 2, reg_covar=1e-6).fit(self.components)
                    labels = gmm.predict(self.components)
                    sil = silhouette_score(self.components, labels, metric='euclidean')
                    tmp_sil.append(sil)
                val = np.mean(SelBest(np.array(tmp_sil),int(iterations/5)))
                err = np.std(tmp_sil)
                sils.append(val)
                sils_err.append(err)
        except:
            pass
        if len(sils):
            self.optimK = sils.index(max(sils)) + 2
            self.sils_score = sils
            self.model = GaussianMixture(self.optimK, n_init = 2, reg_covar=1e-6).fit(self.components)
        else:
            assert False, "No optim K, So fail to get GMM"
        return self
//  [python]
y_pred = mixture.GaussianMixture(n_components=4).fit(X_aniso).predict(X_aniso)
//  [python]
y_pred = KMeans(n_clusters=4, random_state=random_state).fit_predict(X_aniso)

plt.subplot(221)
plt.scatter(X_aniso[:, 0], X_aniso[:, 1], c=y_pred)
plt.title("Anisotropy, Kmeans")

y_pred = GaussianMixture(n_components=4).fit(X_aniso).predict(X_aniso)
//  [python]
def get_gaussianmixture(X, labels, n_clusters, **kwargs):
    model = mixture.GaussianMixture(
        n_components=n_clusters, covariance_type="full", random_state=SEED,
    )
    model.set_params(**kwargs)
    return model.fit(X)
//  [python]
def get_gaussianmixture(X, labels, n_clusters, **kwargs):
    model = GaussianMixture(
        n_components=n_clusters, covariance_type="full", random_state=SEED,
    )
    model.set_params(**kwargs)
    return model.fit(X)
//  [python]
def initialize_hidden_states(self, X):
        
        self.init_states = mixture.GMM(n_components=self.num_states, 
                                           covariance_type='full')
        
        self.init_states.fit(np.concatenate(X).reshape((-1, self.input_dim)))
//  [python]
def initialize_hidden_states(self, X):
        
        self.init_states = GaussianMixture(n_components=self.num_states, 
                                           covariance_type='full')
        
        self.init_states.fit(np.concatenate(X).reshape((-1, self.input_dim)))
//  [python]
def fit_mixture(scores, labels, p_threshold=0.5):
    '''
    Assume the distribution of scores: bimodal gaussian mixture model
    
    return clean labels
    that belongs to the clean cluster by fitting the score distribution to GMM
    '''
    
    clean_labels = []
    indexes = np.array(range(len(scores)))
    probs = {}
    for cls in np.unique(labels):
        cls_index = indexes[labels==cls]
        feats = scores[labels==cls]
        feats_ = np.ravel(feats).astype(np.float).reshape(-1, 1)
        gmm = mixture.GMM(n_components=2, covariance_type='full', tol=1e-6, max_iter=10)
        
        gmm.fit(feats_)
        prob = gmm.predict_proba(feats_)
        prob = prob[:,gmm.means_.argmax()]
        for i in range(len(cls_index)):
            probs[cls_index[i]] = prob[i]
#         weights, means, covars = g.weights_, g.means_, g.covariances_
        
#         # boundary? QDA!
#         a, b = (1/2) * ((1/covars[0]) - (1/covars[1])), -(means[0]/covars[0]) + (means[1]/covars[1])
#         c = (1/2) * ((np.square(means[0])/covars[0]) - (np.square(means[1])/covars[1]))
#         c -= np.log((weights[0])/np.sqrt(2*np.pi*covars[0]))
#         c += np.log((weights[1])/np.sqrt(2*np.pi*covars[1]))
#         d = b**2 - 4*a*c
        
#         bound = estimate_purity(feats, means, covars, weights)
        clean_labels += [cls_index[clean_idx] for clean_idx in range(len(cls_index)) if prob[clean_idx] > p_threshold] 
    
    return np.array(clean_labels, dtype=np.int64), probs
//  [python]
def fit_mixture(scores, labels, p_threshold=0.5):
    '''
    Assume the distribution of scores: bimodal gaussian mixture model
    
    return clean labels
    that belongs to the clean cluster by fitting the score distribution to GMM
    '''
    
    clean_labels = []
    indexes = np.array(range(len(scores)))
    probs = {}
    for cls in np.unique(labels):
        cls_index = indexes[labels==cls]
        feats = scores[labels==cls]
        feats_ = np.ravel(feats).astype(np.float).reshape(-1, 1)
        gmm = GaussianMixture(n_components=2, covariance_type='full', tol=1e-6, max_iter=10)
        
        gmm.fit(feats_)
        prob = gmm.predict_proba(feats_)
        prob = prob[:,gmm.means_.argmax()]
        for i in range(len(cls_index)):
            probs[cls_index[i]] = prob[i]
#         weights, means, covars = g.weights_, g.means_, g.covariances_
        
#         # boundary? QDA!
#         a, b = (1/2) * ((1/covars[0]) - (1/covars[1])), -(means[0]/covars[0]) + (means[1]/covars[1])
#         c = (1/2) * ((np.square(means[0])/covars[0]) - (np.square(means[1])/covars[1]))
#         c -= np.log((weights[0])/np.sqrt(2*np.pi*covars[0]))
#         c += np.log((weights[1])/np.sqrt(2*np.pi*covars[1]))
#         d = b**2 - 4*a*c
        
#         bound = estimate_purity(feats, means, covars, weights)
        clean_labels += [cls_index[clean_idx] for clean_idx in range(len(cls_index)) if prob[clean_idx] > p_threshold] 
    
    return np.array(clean_labels, dtype=np.int64), probs
//  [python]
def __init__(self, gmm, swap=False, diff=False):
        assert gmm.covariance_type == "full"
        # D: static + delta dim
        D = gmm.means_.shape[1] // 2
        self.num_mixtures = gmm.means_.shape[0]
        self.weights = gmm.weights_

        # Split source and target parameters from joint GMM
        self.src_means = gmm.means_[:, :D]
        self.tgt_means = gmm.means_[:, D:]
        self.covarXX = gmm.covariances_[:, :D, :D]
        self.covarXY = gmm.covariances_[:, :D, D:]
        self.covarYX = gmm.covariances_[:, D:, :D]
        self.covarYY = gmm.covariances_[:, D:, D:]

        if diff:
            self.tgt_means = self.tgt_means - self.src_means
            self.covarYY = self.covarXX + self.covarYY - self.covarXY - self.covarYX
            self.covarXY = self.covarXY - self.covarXX
            self.covarYX = self.covarXY.transpose(0, 2, 1)

        # swap src and target parameters
        if swap:
            self.tgt_means, self.src_means = self.src_means, self.tgt_means
            self.covarYY, self.covarXX = self.covarXX, self.covarYY
            self.covarYX, self.covarXY = self.covarXY, self.covarYX

        # p(x), which is used to compute posterior prob. for a given source
        # spectral feature in mapping stage.
        self.px = mixture.GMM(
            n_components=self.num_mixtures, covariance_type="full"
        )
        self.px.means_ = self.src_means
        self.px.covariances_ = self.covarXX
        self.px.weights_ = self.weights
        self.px.precisions_cholesky_ = _compute_precision_cholesky(
            self.px.covariances_, "full"
        )
//  [python]
def __init__(self, gmm, swap=False, diff=False):
        assert gmm.covariance_type == "full"
        # D: static + delta dim
        D = gmm.means_.shape[1] // 2
        self.num_mixtures = gmm.means_.shape[0]
        self.weights = gmm.weights_

        # Split source and target parameters from joint GMM
        self.src_means = gmm.means_[:, :D]
        self.tgt_means = gmm.means_[:, D:]
        self.covarXX = gmm.covariances_[:, :D, :D]
        self.covarXY = gmm.covariances_[:, :D, D:]
        self.covarYX = gmm.covariances_[:, D:, :D]
        self.covarYY = gmm.covariances_[:, D:, D:]

        if diff:
            self.tgt_means = self.tgt_means - self.src_means
            self.covarYY = self.covarXX + self.covarYY - self.covarXY - self.covarYX
            self.covarXY = self.covarXY - self.covarXX
            self.covarYX = self.covarXY.transpose(0, 2, 1)

        # swap src and target parameters
        if swap:
            self.tgt_means, self.src_means = self.src_means, self.tgt_means
            self.covarYY, self.covarXX = self.covarXX, self.covarYY
            self.covarYX, self.covarXY = self.covarXY, self.covarYX

        # p(x), which is used to compute posterior prob. for a given source
        # spectral feature in mapping stage.
        self.px = GaussianMixture(
            n_components=self.num_mixtures, covariance_type="full"
        )
        self.px.means_ = self.src_means
        self.px.covariances_ = self.covarXX
        self.px.weights_ = self.weights
        self.px.precisions_cholesky_ = _compute_precision_cholesky(
            self.px.covariances_, "full"
        )
//  [python]
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=10)

# 模型
model = mixture.GaussianMixture(n_components=3, random_state=5, max_iter=1000)
//  [python]
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=10)

# 模型
model = GaussianMixture(n_components=3, random_state=5, max_iter=1000)
//  [python]
def test_fit(self):
        expected_means = np.array([-55., 0., 7.])
        expected_stds = np.array([3., .5, 1.])

        from sklearn.mixture import GaussianMixture
        gmm = GaussianMixture(n_components=3, means_init=expected_means)
        gmm.means_ = expected_means[..., None]
        gmm.covariances_ = np.array(expected_stds[..., None, None])
        gmm.weights_ = np.array([1/3, 1/3, 1/3])

        obs = gmm.sample(100000 + np.random.randint(-3, 3))[0].squeeze()

        init = deeptime.markov.hmm.init.gaussian.from_data(obs, n_hidden_states=3, reversible=True,
                                                           transition_matrix_kwargs=dict(maxerr=1e-5))
        hmm_est = deeptime.markov.hmm.MaximumLikelihoodHMM(init, lagtime=1)
        hmm = hmm_est.fit(obs).fetch_model()

        np.testing.assert_array_almost_equal(hmm.transition_model.transition_matrix, np.eye(3), decimal=3)
        m = hmm.output_model
        for mean, sigma in zip(m.means, m.sigmas):
            # find the mean closest to this one (order might have changed)
            mean_ix = np.argmin(np.abs(expected_means-mean))
            np.testing.assert_almost_equal(mean, expected_means[mean_ix], decimal=1)
            np.testing.assert_almost_equal(sigma*sigma, expected_stds[mean_ix], decimal=1)
//  [python]
def _gmm(features: pd.DataFrame, random_state: int | None, n_clusters: int = 8) -> np.ndarray:
        km = mixture.GaussianMixture(random_state=random_state, n_components=n_clusters)
        cl = km.fit_predict(features.values)
        return cl
//  [python]
def _gmm(features: pd.DataFrame, random_state: int | None, n_clusters: int = 8) -> np.ndarray:
        km = GaussianMixture(random_state=random_state, n_components=n_clusters)
        cl = km.fit_predict(features.values)
        return cl
//  [python]
def setGMMUBM(self, no_components):
        for i in range(MODEL_SPEAKERS):
            self.GMM.append(mixture.GaussianMixture(n_components= no_components, covariance_type= 'diag'))
            self.UBM.append(mixture.GaussianMixture(n_components= no_components, covariance_type= 'diag'))
//  [python]
def setGMMUBM(self, no_components):
        for i in range(MODEL_SPEAKERS):
            self.GMM.append(GaussianMixture(n_components= no_components, covariance_type= 'diag'))
            self.UBM.append(GaussianMixture(n_components= no_components, covariance_type= 'diag'))
//  [python]
def filterBimodal(X):
    """
    Filter out genes that always have low expression values
    :param X: Dataset matrix (numpy array)
    :return: Tuple: (Filtered X, Iincluded)
    """
    if np.shape(X)[0] < 10:  # Does not work on datasets with less than 10 genes
        return  np.array(X)

    xmax = np.max(X, axis=1)
    xmaxsort = xmax[xmax > 0]  # take out zero values
    xmaxsort = xmaxsort[np.all([xmaxsort > np.percentile(xmaxsort, q=1), xmaxsort < np.percentile(xmaxsort, q=99)],
                               axis=0)]  # take out 1% big & small outliers
    xmaxsort = np.sort(xmaxsort).reshape(-1, 1)
    GM = s.GMM(n_components=2)
    GM.fit(xmaxsort)
    labels = GM.predict(xmaxsort)
    th1 = np.max(xmaxsort[labels == labels[0]])

    Iexcluded = xmax < th1
    Xf = np.array(X)
    Xf[Iexcluded] = 0.0

    return Xf
//  [python]
def filterBimodal(X):
    """
    Filter out genes that always have low expression values
    :param X: Dataset matrix (numpy array)
    :return: Tuple: (Filtered X, Iincluded)
    """
    if np.shape(X)[0] < 10:  # Does not work on datasets with less than 10 genes
        return  np.array(X)

    xmax = np.max(X, axis=1)
    xmaxsort = xmax[xmax > 0]  # take out zero values
    xmaxsort = xmaxsort[np.all([xmaxsort > np.percentile(xmaxsort, q=1), xmaxsort < np.percentile(xmaxsort, q=99)],
                               axis=0)]  # take out 1% big & small outliers
    xmaxsort = np.sort(xmaxsort).reshape(-1, 1)
    GM = skmix.GaussianMixture(n_components=2)
    GM.fit(xmaxsort)
    labels = GM.predict(xmaxsort)
    th1 = np.max(xmaxsort[labels == labels[0]])

    Iexcluded = xmax < th1
    Xf = np.array(X)
    Xf[Iexcluded] = 0.0

    return Xf
//  [python]
def get_initial_means(X, init_params, r):
    # Run a GaussianMixture with max_iter=0 to output the initialization means
    gmm = mixture.GaussianMixture(
        n_components=4, init_params=init_params, tol=1e-9, max_iter=0, random_state=r
    ).fit(X)
    return gmm.means_
//  [python]
def get_initial_means(X, init_params, r):
    # Run a GaussianMixture with max_iter=0 to output the initialization means
    gmm = GaussianMixture(
        n_components=4, init_params=init_params, tol=1e-9, max_iter=0, random_state=r
    ).fit(X)
    return gmm.means_
//  [python]
def __init__(self, kernel='gaussian', bandwidth=.2, alpha=.5):
        super().__init__()
        self.kde = KernelDensity(bandwidth=bandwidth, kernel=kernel)
        self.gmm = mixture.GaussianMixture()
        self.alpha = alpha
        self.bandwidth = bandwidth
        self.kernel = kernel
//  [python]
def __init__(self, kernel='gaussian', bandwidth=.2, alpha=.5):
        super().__init__()
        self.kde = KernelDensity(bandwidth=bandwidth, kernel=kernel)
        self.gmm = GaussianMixture()
        self.alpha = alpha
        self.bandwidth = bandwidth
        self.kernel = kernel
//  [python]
def gmm(z_array, n_clusters):
    '''Initialize clusters using Gaussian mixtures model algorithm.

    Parameters
    ----------
    z_array : array (M,D)
        Latent space data (m_samples, d_features)

    n_clusters : int
        Number of clusters.

    Returns
    -------
    labels : array (M,)
        Sample-wise cluster assignment

    centroids : array (n_clusters, n_features)
        Cluster centroids
    '''
    M = z_array.shape[0]
    # Initialize w/ K-Means
    km = KMeans(
        n_clusters=n_clusters,
        max_iter=1000,
        n_init=100,
        random_state=2009
    )
    km.fit_predict(z_array)
    labels = km.labels_
    centroids = km.cluster_centers_

    labels, counts = np.unique(labels, return_counts=True)

    # Perform EM
    gmm_weights = np.empty(len(labels))
    for i in range(len(labels)):
        gmm_weights[i] = counts[i] / M

    GMM = mixture.GaussianMixture(
        n_components=n_clusters,
        max_iter=1000,
        n_init=1,
        weights_init=gmm_weights,
        means_init=centroids
    )
    np.seterr(under='ignore')
    labels = GMM.fit_predict(z_array)
    centroids = GMM.means_
    return labels, centroids
//  [python]
def gmm(z_array, n_clusters):
    '''Initialize clusters using Gaussian mixtures model algorithm.

    Parameters
    ----------
    z_array : array (M,D)
        Latent space data (m_samples, d_features)

    n_clusters : int
        Number of clusters.

    Returns
    -------
    labels : array (M,)
        Sample-wise cluster assignment

    centroids : array (n_clusters, n_features)
        Cluster centroids
    '''
    M = z_array.shape[0]
    # Initialize w/ K-Means
    km = KMeans(
        n_clusters=n_clusters,
        max_iter=1000,
        n_init=100,
        random_state=2009
    )
    km.fit_predict(z_array)
    labels = km.labels_
    centroids = km.cluster_centers_

    labels, counts = np.unique(labels, return_counts=True)

    # Perform EM
    gmm_weights = np.empty(len(labels))
    for i in range(len(labels)):
        gmm_weights[i] = counts[i] / M

    GMM = GaussianMixture(
        n_components=n_clusters,
        max_iter=1000,
        n_init=1,
        weights_init=gmm_weights,
        means_init=centroids
    )
    np.seterr(under='ignore')
    labels = GMM.fit_predict(z_array)
    centroids = GMM.means_
    return labels, centroids
//  [python]
def gaussian_mixture_split(data: pd.DataFrame, n_components: int,
                           columns: Sequence[Tuple[str, str]] = [
                               ("fret", "eff_app"), ("fret", "stoi_app")],
                           random_seed: int = 0
                           ) -> Tuple[np.ndarray, np.ndarray]:
    """Fit Gaussian mixture model and predict component for each particle

    First, all datapoints are used to fit a Gaussian mixture model. Then each
    particle is assigned the component in which most of its datapoints lie.

    This requires scikit-learn (sklearn).

    Parameters
    ----------
    data
        Single-molecule FRET data
    n_components
        Number of components in the mixture
    columns
        Which columns to fit.
    random_seed
        Seed for the random number generator used to initialize the Gaussian
        mixture model fit.

    Returns
    -------
    Component label for each entry in `data` and mean values for each
    component, one line per component.
    """
    from sklearn.mixture import GMM

    rs = np.random.RandomState(random_seed)
    d = data.loc[:, columns].values
    valid = np.all(np.isfinite(d), axis=1)
    d = d[valid]
    gmm = GMM(n_components=n_components, random_state=rs).fit(d)
    labels = gmm.predict(d)
    mean_sort_idx = np.argsort(gmm.means_[:, 0])[::-1]
    sorter = np.argsort(mean_sort_idx)
    labels = sorter[labels]  # sort according to descending mean
    return labels, gmm.means_[mean_sort_idx]
//  [python]
def gaussian_mixture_split(data: pd.DataFrame, n_components: int,
                           columns: Sequence[Tuple[str, str]] = [
                               ("fret", "eff_app"), ("fret", "stoi_app")],
                           random_seed: int = 0
                           ) -> Tuple[np.ndarray, np.ndarray]:
    """Fit Gaussian mixture model and predict component for each particle

    First, all datapoints are used to fit a Gaussian mixture model. Then each
    particle is assigned the component in which most of its datapoints lie.

    This requires scikit-learn (sklearn).

    Parameters
    ----------
    data
        Single-molecule FRET data
    n_components
        Number of components in the mixture
    columns
        Which columns to fit.
    random_seed
        Seed for the random number generator used to initialize the Gaussian
        mixture model fit.

    Returns
    -------
    Component label for each entry in `data` and mean values for each
    component, one line per component.
    """
    from sklearn.mixture import GaussianMixture

    rs = np.random.RandomState(random_seed)
    d = data.loc[:, columns].values
    valid = np.all(np.isfinite(d), axis=1)
    d = d[valid]
    gmm = GaussianMixture(n_components=n_components, random_state=rs).fit(d)
    labels = gmm.predict(d)
    mean_sort_idx = np.argsort(gmm.means_[:, 0])[::-1]
    sorter = np.argsort(mean_sort_idx)
    labels = sorter[labels]  # sort according to descending mean
    return labels, gmm.means_[mean_sort_idx]
//  [python]
times_init = {}
relative_times = {}

plt.figure(figsize=(4 * len(methods) // 2, 6))
plt.subplots_adjust(
    bottom=0.1, top=0.9, hspace=0.15, wspace=0.05, left=0.05, right=0.95
)

for n, method in enumerate(methods):
    r = np.random.RandomState(seed=1234)
    plt.subplot(2, len(methods) // 2, n + 1)

    start = timer()
    ini = get_initial_means(X, method, r)
    end = timer()
    init_time = end - start

    gmm = mixture.GaussianMixture(
        n_components=4, means_init=ini, tol=1e-9, max_iter=2000, random_state=r
    ).fit(X)

    times_init[method] = init_time
    for i, color in enumerate(colors):
        data = X[gmm.predict(X) == i]
        plt.scatter(data[:, 0], data[:, 1], color=color, marker="x")

    plt.scatter(
        ini[:, 0], ini[:, 1], s=75, marker="D", c="orange", lw=1.5, edgecolors="black"
    )
    relative_times[method] = times_init[method] / times_init[methods[0]]

    plt.xticks(())
    plt.yticks(())
    plt.title(method, loc="left", fontsize=12)
    plt.title(
        "Iter %i | Init Time %.2fx" % (gmm.n_iter_, relative_times[method]),
        loc="right",
        fontsize=10,
    )
//  [python]
times_init = {}
relative_times = {}

plt.figure(figsize=(4 * len(methods) // 2, 6))
plt.subplots_adjust(
    bottom=0.1, top=0.9, hspace=0.15, wspace=0.05, left=0.05, right=0.95
)

for n, method in enumerate(methods):
    r = np.random.RandomState(seed=1234)
    plt.subplot(2, len(methods) // 2, n + 1)

    start = timer()
    ini = get_initial_means(X, method, r)
    end = timer()
    init_time = end - start

    gmm = GaussianMixture(
        n_components=4, means_init=ini, tol=1e-9, max_iter=2000, random_state=r
    ).fit(X)

    times_init[method] = init_time
    for i, color in enumerate(colors):
        data = X[gmm.predict(X) == i]
        plt.scatter(data[:, 0], data[:, 1], color=color, marker="x")

    plt.scatter(
        ini[:, 0], ini[:, 1], s=75, marker="D", c="orange", lw=1.5, edgecolors="black"
    )
    relative_times[method] = times_init[method] / times_init[methods[0]]

    plt.xticks(())
    plt.yticks(())
    plt.title(method, loc="left", fontsize=12)
    plt.title(
        "Iter %i | Init Time %.2fx" % (gmm.n_iter_, relative_times[method]),
        loc="right",
        fontsize=10,
    )
//  [python]
def cluster(X, method="gmm", num=100, vis=False, tol=0.0005):
	# define the model
	# ‘full’: each component has its own general covariance matrix.
	# ‘tied’: all components share the same general covariance matrix.
	# ‘diag’: each component has its own diagonal covariance matrix.
	# ‘spherical’: each component has its own single variance.

	if method == "gmm":
		# 'kmeans', 'k-means++', 'random', 'random_from_data'
		np.random.seed(int(time.time()))
		model = mixture.GaussianMixture(n_components=num, covariance_type="full", init_params="k-means++", max_iter=1000, tol=tol)
	# elif method == "gmgg":
	# 	model = GMM_GMR(num)
	# elif method == "fastgmm":
	# 	model = fast_gmm(nr_mixture = num, min_covar = tol, nr_iteration = 1000, concurrency = 18)
	elif method == "torchgmm":
		model = torch_gmm(num, X.shape[-1], covariance_type="full", init_params="kmeans", max_iter=1000, tol=tol)
	elif method == "km":
		model = KMeans(n_clusters=num, init="k-means++")
	elif method == "ms":
		model = MeanShift(n_jobs=18)
	elif method == "sc":
		model = SpectralClustering(n_clusters=num, n_jobs=18)
	elif method == "db":
		model = DBSCAN(eps=0.17, min_samples=1, n_jobs=18)

	# fit model and predict clusters
	print("start clustering using ", method)
	cluster_inds = np.asarray(model.fit_predict(X))
	print("cluster_inds", X.shape, cluster_inds.shape)
	# score_samples = np.asarray(model.score_samples(X))
	# # scores = np.asarray(model.score(X))
	# cluster_scores = np.asarray(model.predict_proba(X))
	# print("cluster_scores", cluster_scores.shape)
	# max_cluster_scores = np.max(cluster_scores, axis=1)
	# max_cluster_scores_inds = np.argmax(cluster_scores, axis=1)
	# sum_cluster_scores = np.sum(cluster_scores, axis=1)
	# min_inds = np.argsort(max_cluster_scores)
	# # mincluster_inds = np.argmin(cluster_scores, axis=0)
	# print("cluster_scores", max_cluster_scores)
	# print("score_min", max_cluster_scores[min_inds[100]], max_cluster_scores[min_inds[1000]], max_cluster_scores[min_inds[3000]], max_cluster_scores[min_inds[10000]])
	#
	#
	# # print("max_cluster_scores_inds", max_cluster_scores_inds.shape, max_cluster_scores.shape)
	# # max_cluster_mean = scatter_mean(max_cluster_scores_inds, max_cluster_scores, [num])
	# # print("max_cluster_mean", max_cluster_mean)
	# # max_cluster_mean_inds = np.argsort(max_cluster_mean)
	# # print("smallest_cluster_inds", max_cluster_mean_inds)
	# retrieve unique clusters
	clusters = np.unique(cluster_inds)
	# print("finished with ", len(clusters), " clusters", clusters)
	cluster_xyz=np.zeros([len(clusters),3], dtype=np.float32)
	# create scatter plot for samples from each cluster
	counter = 0
	for i in range(len(clusters)):
		row_mask = cluster_inds == clusters[i]
		# print("row_ix", row_mask.shape)
		os.makedirs(path, exist_ok=True)
		if vis:
			np.savetxt(os.path.join(path, "cluster_{:04d}.txt".format(counter)), X[row_mask], delimiter=";")
		cluster_xyz[i] = np.mean(X[row_mask][...,:3], axis=0)
		counter+=1
	# print(np.asarray(model.means_)[...,:3], cluster_xyz)
	return cluster_xyz, cluster_inds, model
//  [python]
def cluster(X, method="gmm", num=100, vis=False, tol=0.0005):
	# define the model
	# ‘full’: each component has its own general covariance matrix.
	# ‘tied’: all components share the same general covariance matrix.
	# ‘diag’: each component has its own diagonal covariance matrix.
	# ‘spherical’: each component has its own single variance.

	if method == "gmm":
		# 'kmeans', 'k-means++', 'random', 'random_from_data'
		np.random.seed(int(time.time()))
		model = GaussianMixture(n_components=num, covariance_type="full", init_params="k-means++", max_iter=1000, tol=tol)
	# elif method == "gmgg":
	# 	model = GMM_GMR(num)
	# elif method == "fastgmm":
	# 	model = fast_gmm(nr_mixture = num, min_covar = tol, nr_iteration = 1000, concurrency = 18)
	elif method == "torchgmm":
		model = torch_gmm(num, X.shape[-1], covariance_type="full", init_params="kmeans", max_iter=1000, tol=tol)
	elif method == "km":
		model = KMeans(n_clusters=num, init="k-means++")
	elif method == "ms":
		model = MeanShift(n_jobs=18)
	elif method == "sc":
		model = SpectralClustering(n_clusters=num, n_jobs=18)
	elif method == "db":
		model = DBSCAN(eps=0.17, min_samples=1, n_jobs=18)

	# fit model and predict clusters
	print("start clustering using ", method)
	cluster_inds = np.asarray(model.fit_predict(X))
	print("cluster_inds", X.shape, cluster_inds.shape)
	# score_samples = np.asarray(model.score_samples(X))
	# # scores = np.asarray(model.score(X))
	# cluster_scores = np.asarray(model.predict_proba(X))
	# print("cluster_scores", cluster_scores.shape)
	# max_cluster_scores = np.max(cluster_scores, axis=1)
	# max_cluster_scores_inds = np.argmax(cluster_scores, axis=1)
	# sum_cluster_scores = np.sum(cluster_scores, axis=1)
	# min_inds = np.argsort(max_cluster_scores)
	# # mincluster_inds = np.argmin(cluster_scores, axis=0)
	# print("cluster_scores", max_cluster_scores)
	# print("score_min", max_cluster_scores[min_inds[100]], max_cluster_scores[min_inds[1000]], max_cluster_scores[min_inds[3000]], max_cluster_scores[min_inds[10000]])
	#
	#
	# # print("max_cluster_scores_inds", max_cluster_scores_inds.shape, max_cluster_scores.shape)
	# # max_cluster_mean = scatter_mean(max_cluster_scores_inds, max_cluster_scores, [num])
	# # print("max_cluster_mean", max_cluster_mean)
	# # max_cluster_mean_inds = np.argsort(max_cluster_mean)
	# # print("smallest_cluster_inds", max_cluster_mean_inds)
	# retrieve unique clusters
	clusters = np.unique(cluster_inds)
	# print("finished with ", len(clusters), " clusters", clusters)
	cluster_xyz=np.zeros([len(clusters),3], dtype=np.float32)
	# create scatter plot for samples from each cluster
	counter = 0
	for i in range(len(clusters)):
		row_mask = cluster_inds == clusters[i]
		# print("row_ix", row_mask.shape)
		os.makedirs(path, exist_ok=True)
		if vis:
			np.savetxt(os.path.join(path, "cluster_{:04d}.txt".format(counter)), X[row_mask], delimiter=";")
		cluster_xyz[i] = np.mean(X[row_mask][...,:3], axis=0)
		counter+=1
	# print(np.asarray(model.means_)[...,:3], cluster_xyz)
	return cluster_xyz, cluster_inds, model
//  [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, rdc_mat=None):
        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,
        )
        center = estimator.means_
        clusters = estimator.fit(data).predict(data)

        return split_data_by_clusters(local_data, clusters, scope, center, 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, rdc_mat=None):
        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,
        )
        center = estimator.means_
        clusters = estimator.fit(data).predict(data)

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

    return split_rows_GMM
//  [python]
def herding_gmm(self, k, num_local_class, i_seed):
        """
        The client run the FedD3 with coreset-based instance.
        :param k: Number of the local distilled images, this need to be integral times of number of local classes
        :param num_local_class: Number of the local classes
        :param i_seed: Index of the used seed
        :return: Distilled images from decentralized dataset in this client
        """
        torch.manual_seed(i_seed)
        random.seed(i_seed)
        np.random.seed(i_seed)
        res = []
        self._train_data['y'] = self._train_data['y'].squeeze()
        self._train_data['x'] = self._train_data['x'].squeeze()
        num_datapoint = int(k / num_local_class)
        cls_set = set()
        for cls in self._train_data['y']:
            cls_set.add(cls.item())

        for cls in cls_set:
            sub_data = []
            indexes = torch.nonzero(self._train_data['y'] == cls)
            indexes = indexes[torch.randperm(indexes.shape[0])]
            for index in indexes:
                sub_data.append(self._train_data['x'][index].numpy().reshape(-1).tolist())

            gm = mixture.GaussianMixture(n_components=int(k / num_local_class), random_state=0).fit(sub_data)
            for x_data in gm.means_:
                k_data_point = [cls, np.array(x_data).reshape(1, 28, 28), k]
                res.append(k_data_point)

        return res
//  [python]
def herding_gmm(self, k, num_local_class, i_seed):
        """
        The client run the FedD3 with coreset-based instance.
        :param k: Number of the local distilled images, this need to be integral times of number of local classes
        :param num_local_class: Number of the local classes
        :param i_seed: Index of the used seed
        :return: Distilled images from decentralized dataset in this client
        """
        torch.manual_seed(i_seed)
        random.seed(i_seed)
        np.random.seed(i_seed)
        res = []
        self._train_data['y'] = self._train_data['y'].squeeze()
        self._train_data['x'] = self._train_data['x'].squeeze()
        num_datapoint = int(k / num_local_class)
        cls_set = set()
        for cls in self._train_data['y']:
            cls_set.add(cls.item())

        for cls in cls_set:
            sub_data = []
            indexes = torch.nonzero(self._train_data['y'] == cls)
            indexes = indexes[torch.randperm(indexes.shape[0])]
            for index in indexes:
                sub_data.append(self._train_data['x'][index].numpy().reshape(-1).tolist())

            gm = GaussianMixture(n_components=int(k / num_local_class), random_state=0).fit(sub_data)
            for x_data in gm.means_:
                k_data_point = [cls, np.array(x_data).reshape(1, 28, 28), k]
                res.append(k_data_point)

        return res
//  [python]
def getGMM(filename):

    nw = 320  #对于16KHz的文件，20ms的采样点个数
    inc = 160
    wave_data=getWaveData(filename)
    winfunc = signal.hann(nw)
    X = enframe(wave_data, nw, inc, winfunc)
    frameNum = X.shape[0]  # 返回矩阵列数，获取帧数

    data=[]
    for oneframe in  X:
        tmpList=list()
        mfccs = librosa.feature.mfcc(y=oneframe, sr=16000, n_mfcc=24)
        # print(mfccs.shape)
        for a in mfccs:
            # print(a.shape)
            tmpList.append(a[0])
        data.append(tmpList)
    data=np.array(data)
    # data.shape : (frames_num, 24)
    print(data.shape)

    # 高斯混合模型 5个样本时，聚类数量为3，效果最好
    # 使用贝叶斯GMM，可避免数量选择
    gmm = mixture.GMM(3, covariance_type='full', random_state=0).fit(data)
    return gmm
//  [python]
def getGMM(filename):

    nw = 320  #对于16KHz的文件，20ms的采样点个数
    inc = 160
    wave_data=getWaveData(filename)
    winfunc = signal.hann(nw)
    X = enframe(wave_data, nw, inc, winfunc)
    frameNum = X.shape[0]  # 返回矩阵列数，获取帧数

    data=[]
    for oneframe in  X:
        tmpList=list()
        mfccs = librosa.feature.mfcc(y=oneframe, sr=16000, n_mfcc=24)
        # print(mfccs.shape)
        for a in mfccs:
            # print(a.shape)
            tmpList.append(a[0])
        data.append(tmpList)
    data=np.array(data)
    # data.shape : (frames_num, 24)
    print(data.shape)

    # 高斯混合模型 5个样本时，聚类数量为3，效果最好
    # 使用贝叶斯GMM，可避免数量选择
    gmm = GaussianMixture(3, covariance_type='full', random_state=0).fit(data)
    return gmm
//  [python]
def eval_train(model, all_loss):
    model.eval()
    num_iter = (len(eval_loader.dataset) // eval_loader.batch_size) + 1
    losses = torch.zeros(len(eval_loader.dataset))
    paths = []
    n = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets, path) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs)
            loss = CE(outputs, targets)
            for b in range(inputs.size(0)):
                losses[n] = loss[b]
                paths.append(path[b])
                n += 1
            sys.stdout.write('\r')
            sys.stdout.write('| Evaluating loss Iter[%3d/%3d]\t' % (batch_idx, num_iter))
            sys.stdout.flush()

    losses = (losses - losses.min()) / (losses.max() - losses.min())
    all_loss.append(losses)

    # fit a two-component GMM to the loss
    input_loss = losses.reshape(-1, 1)
    gmm = mixture.GaussianMixture(n_components=2, max_iter=10, tol=1e-2, reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss)
    prob = prob[:, gmm.means_.argmin()]
    return prob, paths, all_loss
//  [python]
def eval_train(model, all_loss):
    model.eval()
    num_iter = (len(eval_loader.dataset) // eval_loader.batch_size) + 1
    losses = torch.zeros(len(eval_loader.dataset))
    paths = []
    n = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets, path) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs)
            loss = CE(outputs, targets)
            for b in range(inputs.size(0)):
                losses[n] = loss[b]
                paths.append(path[b])
                n += 1
            sys.stdout.write('\r')
            sys.stdout.write('| Evaluating loss Iter[%3d/%3d]\t' % (batch_idx, num_iter))
            sys.stdout.flush()

    losses = (losses - losses.min()) / (losses.max() - losses.min())
    all_loss.append(losses)

    # fit a two-component GMM to the loss
    input_loss = losses.reshape(-1, 1)
    gmm = GaussianMixture(n_components=2, max_iter=10, tol=1e-2, reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss)
    prob = prob[:, gmm.means_.argmin()]
    return prob, paths, all_loss
//  [python]
def fit(self, df, columns_config, unit=None, delta_unit='s'):
        """
        Fits the gausian mixture model for understanding threshold

        :param df: DataFrame with columns responding for
                   Event Name, Event Timestamp and User ID
        :param columns_config: Dictionary that maps to required column names:
                               {'event_name_col': Event Name Column,
                               'event_timestamp_col' Event Timestamp Column,
                               'user_id_col': User ID Column}
        :param unit: type of string for pd.datetime parsing
        :param delta_unit: step in timestamp column (e.g. seconds from `01-01-1970`)
        :return: None
        """

        self.columns_config = columns_config
        self.unit = unit
        self.delta_unit = delta_unit
        df = self.add_time_from_prev_event(df.copy(), unit=self.unit, delta_unit=self.delta_unit)
        time_from_prev = np.log(df.loc[df.from_prev_event > 0, 'from_prev_event'].values)
        X = time_from_prev.reshape(-1, 1)
        if isinstance(self.n_components, int):
            self.model = mixture.GMM(n_components=self.n_components)
            self.model.fit(X)
            self.bics.append(self.model.bic(X))
        else:
            best_bic = np.infty
            best_model = None
            for n in self.n_components:
                model = mixture.GMM(n_components=n)
                model.fit(X)
                curr_bic = model.bic(X)
                self.bics.append(curr_bic)

                if curr_bic < best_bic:
                    best_model = model
                    best_bic = curr_bic

            self.model = best_model
//  [python]
def fit(self, df, columns_config, unit=None, delta_unit='s'):
        """
        Fits the gausian mixture model for understanding threshold

        :param df: DataFrame with columns responding for
                   Event Name, Event Timestamp and User ID
        :param columns_config: Dictionary that maps to required column names:
                               {'event_name_col': Event Name Column,
                               'event_timestamp_col' Event Timestamp Column,
                               'user_id_col': User ID Column}
        :param unit: type of string for pd.datetime parsing
        :param delta_unit: step in timestamp column (e.g. seconds from `01-01-1970`)
        :return: None
        """

        self.columns_config = columns_config
        self.unit = unit
        self.delta_unit = delta_unit
        df = self.add_time_from_prev_event(df.copy(), unit=self.unit, delta_unit=self.delta_unit)
        time_from_prev = np.log(df.loc[df.from_prev_event > 0, 'from_prev_event'].values)
        X = time_from_prev.reshape(-1, 1)
        if isinstance(self.n_components, int):
            self.model = GaussianMixture(n_components=self.n_components)
            self.model.fit(X)
            self.bics.append(self.model.bic(X))
        else:
            best_bic = np.infty
            best_model = None
            for n in self.n_components:
                model = GaussianMixture(n_components=n)
                model.fit(X)
                curr_bic = model.bic(X)
                self.bics.append(curr_bic)

                if curr_bic < best_bic:
                    best_model = model
                    best_bic = curr_bic

            self.model = best_model
//  [python]
def train_using_python(self, input_data, iters=10):
        seed = 6
        if self.components_seeded and self.clf is None:
            self.clf = mixture.GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, init_params='random', random_state=seed, \
                               weights_init=self.components.weights, means_init=self.components.means, n_init=1) #reg_covar=0.01,

        elif not self.components_seeded and self.clf is None:
            self.clf = mixture.GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, init_params='random', n_init=1, random_state=seed) #reg_covar=0.01, 

        self.clf.fit(input_data)
        return self.clf.means_, self.clf.covariances_
//  [python]
def train_using_python(self, input_data, iters=10):
        seed = 6
        if self.components_seeded and self.clf is None:
            self.clf = GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, init_params='random', random_state=seed, \
                               weights_init=self.components.weights, means_init=self.components.means, n_init=1) #reg_covar=0.01,

        elif not self.components_seeded and self.clf is None:
            self.clf = GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, init_params='random', n_init=1, random_state=seed) #reg_covar=0.01, 

        self.clf.fit(input_data)
        return self.clf.means_, self.clf.covariances_
//  [python]
def CAPTCHA_to_data(filename):
    '''
    convert CAPTCHA image to 7 chinese character image data.
    kind of slow because of GMM iteration.
    return a 7 * 40 * 40 array
    '''
    
    width=400
    height=88
    padding=20
    padding_color = 249
    
    captcha = Image.open(filename)

    bg = numpy.full((height+padding*2, width+padding*2), padding_color, dtype='uint8')
    fr = numpy.asarray(captcha.convert('L'))
    bg[padding:padding+height,padding:padding+width] = fr
    
    black_pixel_indexes = numpy.transpose(numpy.nonzero(bg <= 150))
    gmm = sklearn.mixture.GMM(n_components=7, covariance_type='tied', reg_covar=1e2, tol=1e3, n_init=9)
    gmm.fit(black_pixel_indexes)
        
    indexes = gmm.means_.astype(int).tolist()
    new_indexes = []
    for [y, x] in indexes:
        new_indexes.append((y - padding, x - padding))

    data = numpy.empty((0, 40, 40), 'float32')
    full_image = data_to_image(bg)
    
    for [y, x] in new_indexes:
        cim = full_image.crop((x, y, x + padding*2, y + padding*2))
        X = numpy.asarray(cim.convert('L')).astype('float32')
        X[X <= 150] = -1
        # black
        X[X >  150] = 1
        # white
        data = numpy.append(data, X.reshape(1, 40, 40), axis=0)
        

    return data, new_indexes