jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def fit_GMM(self, data, n_components, means_init=None, precisions_init=None):
        gmm = mixture.GaussianMixture(n_components=n_components, covariance_type='full',
                              means_init=means_init, precisions_init=precisions_init,
                              tol=1e-6, reg_covar=1e-6, verbose=2)
        gmm.fit(data)
        return gmm
//  [python]
def fit_GMM(self, data, n_components, means_init=None, precisions_init=None):
        gmm = GaussianMixture(n_components=n_components, covariance_type='full',
                              means_init=means_init, precisions_init=precisions_init,
                              tol=1e-6, reg_covar=1e-6, verbose=2)
        gmm.fit(data)
        return gmm
//  [python]
def fit(self, data, target):
        data = np.asarray(data)
        target = np.asarray(target).astype(self.dtype)
#         print "gmmcl fit sh ", data.shape
        un = np.unique(target)
        for label in un:
            # cli = sklearn.mixture.GMM(**self.clspars)
            if label in self.cls:
                cli = self.cls[label]
            else:
                cli = sklearn.mixture.GMM(**self.clspars)
            dtl = data[target.reshape(-1) == label]
            cli.fit(dtl)
            self.cls[label] = cli

        pass
//  [python]
def fit(self, data, target):
        data = np.asarray(data)
        target = np.asarray(target).astype(self.dtype)
#         print "gmmcl fit sh ", data.shape
        un = np.unique(target)
        for label in un:
            # cli = sklearn.mixture.GMM(**self.clspars)
            if label in self.cls:
                cli = self.cls[label]
            else:
                cli = sklearn.mixture.GaussianMixture(**self.clspars)
            dtl = data[target.reshape(-1) == label]
            cli.fit(dtl)
            self.cls[label] = cli

        pass
//  [python]
def _gmm_single(x, cv, components, num_models, random_seed=None, return_all=True):
    """
    Runs GMM several times and saves value with best BIC score

    Args:
        x (np.ndarray):             Data
        cv (list of str):           Covariance, must be 'spherical', 'tied', 'diag', or 'full'
        components (list of ints):  Number of output clusters
        num_models (int):           Number of models to run. Only one is returned
        random_seed (int):          Random seed
        return_all (bool):          Whether or not to return all models.

        Returns:
        gmm_list OR best_gmm:           List of class identity or classes for best model
        gmm_labels OR best_gmm_labels:  Label list for all models or labels for best model
        gmm_proba OR best_gmm_proba:    Probability list of class belonging or probability for best model
    """
    if return_all is True:
        gmm_list = []
        gmm_labels = []
        gmm_proba = []
    lowest_bic = np.infty
    bic_temp = 0
    if random_seed is None:
        rng = np.random.RandomState(seed=42)
    else:
        seed = random_seed
    for n in range(num_models):
        if random_seed is None:
            seed = rng.randint(5000)
        for j in range(len(components)):
            for cv_type in cv:
                gmm = mixture.GaussianMixture(
                    n_components=components[j],
                    covariance_type=cv_type,
                    random_state=seed,
                )
                labels = gmm.fit_predict(x)
                bic_temp = gmm.bic(x)

                if return_all is True:
                    gmm_list.append(gmm)
                    gmm_labels.append(labels)
                    gmm_proba.append(gmm.predict_proba(x))

                elif return_all is False:
                    if bic_temp < lowest_bic:
                        lowest_bic = bic_temp
                        best_gmm = gmm
                        best_gmm_labels = labels
                        best_gmm_proba = gmm.predict_proba(x)

    if return_all is True:
        return gmm_list, gmm_labels, gmm_proba
    return best_gmm, best_gmm_labels, best_gmm_proba
//  [python]
def _gmm_single(x, cv, components, num_models, random_seed=None, return_all=True):
    """
    Runs GMM several times and saves value with best BIC score

    Args:
        x (np.ndarray):             Data
        cv (list of str):           Covariance, must be 'spherical', 'tied', 'diag', or 'full'
        components (list of ints):  Number of output clusters
        num_models (int):           Number of models to run. Only one is returned
        random_seed (int):          Random seed
        return_all (bool):          Whether or not to return all models.

        Returns:
        gmm_list OR best_gmm:           List of class identity or classes for best model
        gmm_labels OR best_gmm_labels:  Label list for all models or labels for best model
        gmm_proba OR best_gmm_proba:    Probability list of class belonging or probability for best model
    """
    if return_all is True:
        gmm_list = []
        gmm_labels = []
        gmm_proba = []
    lowest_bic = np.infty
    bic_temp = 0
    if random_seed is None:
        rng = np.random.RandomState(seed=42)
    else:
        seed = random_seed
    for n in range(num_models):
        if random_seed is None:
            seed = rng.randint(5000)
        for j in range(len(components)):
            for cv_type in cv:
                gmm = GaussianMixture(
                    n_components=components[j],
                    covariance_type=cv_type,
                    random_state=seed,
                )
                labels = gmm.fit_predict(x)
                bic_temp = gmm.bic(x)

                if return_all is True:
                    gmm_list.append(gmm)
                    gmm_labels.append(labels)
                    gmm_proba.append(gmm.predict_proba(x))

                elif return_all is False:
                    if bic_temp < lowest_bic:
                        lowest_bic = bic_temp
                        best_gmm = gmm
                        best_gmm_labels = labels
                        best_gmm_proba = gmm.predict_proba(x)

    if return_all is True:
        return gmm_list, gmm_labels, gmm_proba
    return best_gmm, best_gmm_labels, best_gmm_proba
//  [python]
def __init__(
        self,
        arrival_min: float = 0.0,
        arrival_max: float = 24.0,
        duration_min: float = 0.0833,
        duration_max: float = 48.0,
        energy_min: float = 0.5,
        energy_max: float = 150.0,
        pretrained_model: Optional[GaussianMixture] = None,
        **kwargs,
    ):
        super().__init__(
            arrival_min, arrival_max, duration_min, duration_max, energy_min, energy_max
        )
        if pretrained_model is None:
            self.gmm = GaussianMixture(**kwargs)
        else:
            self.gmm = pretrained_model
//  [python]
def GMM_cluster(self, embeddings: np.ndarray, threshold: float, random_state: int = 0):
        """
        Cluster embeddings using a Gaussian Mixture Model (GMM) based on a probability threshold.

        Parameters:
        - embeddings: The input embeddings as a numpy array.
        - threshold: The probability threshold for assigning an embedding to a cluster.
        - random_state: Seed for reproducibility.

        Returns:
        - A tuple containing the cluster labels and the number of clusters determined.
        """
        n_clusters = self.get_optimal_clusters(embeddings)
        gm = mixture.GaussianMixture(n_components=n_clusters, random_state=random_state)
        gm.fit(embeddings)
        probs = gm.predict_proba(embeddings)
        labels = [np.where(prob > threshold)[0] for prob in probs]
        return labels, n_clusters
//  [python]
def GMM_cluster(self, embeddings: np.ndarray, threshold: float, random_state: int = 0):
        """
        Cluster embeddings using a Gaussian Mixture Model (GMM) based on a probability threshold.

        Parameters:
        - embeddings: The input embeddings as a numpy array.
        - threshold: The probability threshold for assigning an embedding to a cluster.
        - random_state: Seed for reproducibility.

        Returns:
        - A tuple containing the cluster labels and the number of clusters determined.
        """
        n_clusters = self.get_optimal_clusters(embeddings)
        gm = GaussianMixture(n_components=n_clusters, random_state=random_state)
        gm.fit(embeddings)
        probs = gm.predict_proba(embeddings)
        labels = [np.where(prob > threshold)[0] for prob in probs]
        return labels, n_clusters
//  [python]
def __init__(self, n_components, n_features, device, covariance_type="full"):
        super().__init__()

        self.n_components = n_components
        self.n_features = n_features
        self.device = device
        self.covariance_type = covariance_type
        
        self.gmm = mixture.GaussianMixture(n_components=n_components, covariance_type=covariance_type, n_init=10)
        self._init_params()
//  [python]
def __init__(self, n_components, n_features, device, covariance_type="full"):
        super().__init__()

        self.n_components = n_components
        self.n_features = n_features
        self.device = device
        self.covariance_type = covariance_type
        
        self.gmm = skGaussianMixture(n_components=n_components, covariance_type=covariance_type, n_init=10)
        self._init_params()
//  [python]
def eval_train(epoch,model):
    model.eval()
    num_samples = args.num_batches*args.batch_size
    losses = torch.zeros(num_samples)
    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\t' %(batch_idx)) 
            # sys.stdout.flush()
            
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    losses = losses.reshape(-1,1)
    gmm = mixture.GaussianMixture(n_components=2,max_iter=10,reg_covar=5e-4,tol=1e-2)
    gmm.fit(losses)
    prob = gmm.predict_proba(losses) 
    prob = prob[:,gmm.means_.argmin()]       
    return prob,paths
//  [python]
def eval_train(epoch,model):
    model.eval()
    num_samples = args.num_batches*args.batch_size
    losses = torch.zeros(num_samples)
    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\t' %(batch_idx)) 
            # sys.stdout.flush()
            
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    losses = losses.reshape(-1,1)
    gmm = GaussianMixture(n_components=2,max_iter=10,reg_covar=5e-4,tol=1e-2)
    gmm.fit(losses)
    prob = gmm.predict_proba(losses) 
    prob = prob[:,gmm.means_.argmin()]       
    return prob,paths
//  [python]
def get_optimal_clusters(
        self, embeddings: np.ndarray, max_clusters: int = 50, random_state: int = RANDOM_SEED
    ) -> int:
        """
        Determine the optimal number of clusters using the Bayesian Information Criterion (BIC) with a Gaussian Mixture Model.

        Parameters:
        - embeddings: The input embeddings as a numpy array.
        - max_clusters: The maximum number of clusters to consider.
        - random_state: Seed for reproducibility.

        Returns:
        - An integer representing the optimal number of clusters found.
        """
        max_clusters = min(max_clusters, len(embeddings))
        n_clusters = np.arange(1, max_clusters)
        bics = []
        for n in n_clusters:
            gm = mixture.GaussianMixture(n_components=n, random_state=random_state)
            gm.fit(embeddings)
            bics.append(gm.bic(embeddings))
        return n_clusters[np.argmin(bics)]
//  [python]
def get_optimal_clusters(
        self, embeddings: np.ndarray, max_clusters: int = 50, random_state: int = RANDOM_SEED
    ) -> int:
        """
        Determine the optimal number of clusters using the Bayesian Information Criterion (BIC) with a Gaussian Mixture Model.

        Parameters:
        - embeddings: The input embeddings as a numpy array.
        - max_clusters: The maximum number of clusters to consider.
        - random_state: Seed for reproducibility.

        Returns:
        - An integer representing the optimal number of clusters found.
        """
        max_clusters = min(max_clusters, len(embeddings))
        n_clusters = np.arange(1, max_clusters)
        bics = []
        for n in n_clusters:
            gm = GaussianMixture(n_components=n, random_state=random_state)
            gm.fit(embeddings)
            bics.append(gm.bic(embeddings))
        return n_clusters[np.argmin(bics)]
//  [python]
def main():
    X, Y = get_data(10000)
    print("Number of data points:", len(Y))

    model = mixture.GaussianMixture(n_components=10)
    model.fit(X)
    M = model.means_
    R = model.predict_proba(X)

    print("Purity:", purity(Y, R)) # max is 1, higher is better
    print("DBI:", DBI(X, M, R))
//  [python]
def main():
    X, Y = get_data(10000)
    print("Number of data points:", len(Y))

    model = GaussianMixture(n_components=10)
    model.fit(X)
    M = model.means_
    R = model.predict_proba(X)

    print("Purity:", purity(Y, R)) # max is 1, higher is better
    print("DBI:", DBI(X, M, R))
//  [python]
def cluster_gmm(
    z,
    K: int,
    on_data: bool = True,
    random_state: Union[int, np.random.RandomState, None] = None,
    **kwargs,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Cluster z by a K-component full covariance Gaussian mixture model

    Inputs:
        z (Ndata x zdim np.array): Latent encodings
        K (int): Number of clusters
        on_data (bool): Compute cluster center as nearest point on the data manifold
        random_state (int or None): Random seed used for GMM clustering
        **kwargs: Additional keyword arguments passed to sklearn.mixture.GaussianMixture

    Returns:
        np.array (Ndata,) of cluster labels
        np.array (K x zdim) of cluster centers
    """
    clf = mixture.GaussianMixture(
        n_components=K, covariance_type="full", random_state=random_state, **kwargs
    )
    labels = clf.fit_predict(z)
    centers = clf.means_
    if on_data:
        centers, centers_ind = get_nearest_point(z, centers)
    return labels, centers
//  [python]
def cluster_gmm(
    z,
    K: int,
    on_data: bool = True,
    random_state: Union[int, np.random.RandomState, None] = None,
    **kwargs,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Cluster z by a K-component full covariance Gaussian mixture model

    Inputs:
        z (Ndata x zdim np.array): Latent encodings
        K (int): Number of clusters
        on_data (bool): Compute cluster center as nearest point on the data manifold
        random_state (int or None): Random seed used for GMM clustering
        **kwargs: Additional keyword arguments passed to sklearn.mixture.GaussianMixture

    Returns:
        np.array (Ndata,) of cluster labels
        np.array (K x zdim) of cluster centers
    """
    clf = GaussianMixture(
        n_components=K, covariance_type="full", random_state=random_state, **kwargs
    )
    labels = clf.fit_predict(z)
    centers = clf.means_
    if on_data:
        centers, centers_ind = get_nearest_point(z, centers)
    return labels, centers
//  [python]
def train_model_gmm(train_dir):
    """
    :param train_dir: derivative order, default order is 2
    :return: gmm_models
    """
    gmm_models = []
    # iterate through the training directory
    for digit in os.listdir(train_dir):
        # get the directory of digit
        digit_dir = os.path.join(train_dir, digit)
        # get the digit label
        label = digit_dir[digit_dir.rfind('/') + 1:]
        # start training
        X = np.array([])
        train_files = [x for x in os.listdir(digit_dir) if x.endswith('.wav')]
        for file_name in train_files:
            file_path = os.path.join(digit_dir, file_name)
            # get mfcc feature and ignore the warning
            with warnings.catch_warnings():
                warnings.simplefilter('ignore')
                features_mfcc = mfcc(file_path)
            # append mfcc to X
            if len(X) == 0:
                X = features_mfcc
            else:
                X = np.append(X, features_mfcc, axis=0)

        # get the gmm model
        model = mixture.GMM(n_components=2, covariance_type='diag')
        # fit gmm model
        np.seterr(all='ignore')
        model.fit(X)
        gmm_models.append((model, label))
    return gmm_models
//  [python]
def train_model_gmm(train_dir):
    """
    :param train_dir: derivative order, default order is 2
    :return: gmm_models
    """
    gmm_models = []
    # iterate through the training directory
    for digit in os.listdir(train_dir):
        # get the directory of digit
        digit_dir = os.path.join(train_dir, digit)
        # get the digit label
        label = digit_dir[digit_dir.rfind('/') + 1:]
        # start training
        X = np.array([])
        train_files = [x for x in os.listdir(digit_dir) if x.endswith('.wav')]
        for file_name in train_files:
            file_path = os.path.join(digit_dir, file_name)
            # get mfcc feature and ignore the warning
            with warnings.catch_warnings():
                warnings.simplefilter('ignore')
                features_mfcc = mfcc(file_path)
            # append mfcc to X
            if len(X) == 0:
                X = features_mfcc
            else:
                X = np.append(X, features_mfcc, axis=0)

        # get the gmm model
        model = GaussianMixture(n_components=2, covariance_type='diag')
        # fit gmm model
        np.seterr(all='ignore')
        model.fit(X)
        gmm_models.append((model, label))
    return gmm_models
//  [python]
def init_gmm_params(self, Dataloader):
        """This function will initialize the parameters for PoE model.

        Parameters
        ----------
        Dataloader : torch.utils.data.DataLoader
            Dataloader for the whole dataset.

        Returns
        -------
        None.

        """
        gmm = mixture.GaussianMixture(n_components=self.n_centroids, covariance_type='diag')

        latent_z = self._out_Batch(Dataloader, out='Z')
        gmm.fit(latent_z)

        self.mu_c.data.copy_(torch.from_numpy(gmm.means_.T.astype(np.float32)))
        self.var_c.data.copy_(torch.from_numpy(gmm.covariances_.T.astype(np.float32)))
//  [python]
def init_gmm_params(self, Dataloader):
        """This function will initialize the parameters for PoE model.

        Parameters
        ----------
        Dataloader : torch.utils.data.DataLoader
            Dataloader for the whole dataset.

        Returns
        -------
        None.

        """
        gmm = GaussianMixture(n_components=self.n_centroids, covariance_type='diag')

        latent_z = self._out_Batch(Dataloader, out='Z')
        gmm.fit(latent_z)

        self.mu_c.data.copy_(torch.from_numpy(gmm.means_.T.astype(np.float32)))
        self.var_c.data.copy_(torch.from_numpy(gmm.covariances_.T.astype(np.float32)))
//  [python]
def mnplotsdistancethreshold(dists, method='bimodal', returnmodel=False):
    distsloc = np.array(dists).reshape(-1, 1)
    if method == 'bimodal':
        GM = s.GMM(n_components=2)
        GM.fit(distsloc)
        if len(dists) == 1:
            labels = [1]
        else:
            labels = GM.predict(distsloc)
            labels = np.nonzero(labels == labels[0])[0]
        if returnmodel:
            return (labels, GM)
        else:
            return labels
    elif method == 'largestgap' or method == 'largest_gap':
        if len(dists) == 1:
            labels = [1]
        else:
            gaps = np.subtract(dists[1:], dists[0:-1])
            wgaps = np.multiply(gaps, np.arange(len(gaps), 0, -1))  # weight gaps (higher weight for first clusters)
            labels = np.arange(0, np.argmax(wgaps)+1)
        return labels
    else:
        raise ValueError('Invalid method submitted to mnplotsdistancethreshold. '
                         'Use either ''bimodal'' or ''largestgap''')
//  [python]
def mnplotsdistancethreshold(dists, method='bimodal', returnmodel=False):
    distsloc = np.array(dists).reshape(-1, 1)
    if method == 'bimodal':
        GM = skmix.GaussianMixture(n_components=2)
        GM.fit(distsloc)
        if len(dists) == 1:
            labels = [1]
        else:
            labels = GM.predict(distsloc)
            labels = np.nonzero(labels == labels[0])[0]
        if returnmodel:
            return (labels, GM)
        else:
            return labels
    elif method == 'largestgap' or method == 'largest_gap':
        if len(dists) == 1:
            labels = [1]
        else:
            gaps = np.subtract(dists[1:], dists[0:-1])
            wgaps = np.multiply(gaps, np.arange(len(gaps), 0, -1))  # weight gaps (higher weight for first clusters)
            labels = np.arange(0, np.argmax(wgaps)+1)
        return labels
    else:
        raise ValueError('Invalid method submitted to mnplotsdistancethreshold. '
                         'Use either ''bimodal'' or ''largestgap''')
//  [python]
def gen_gmm():
    import sklearn.mixture

    np.random.seed(0)
    gmm = sklearn.mixture.GaussianMixture(
        n_components=10,
        covariance_type="full",
        random_state=0,
    )
    gmm.fit(mixed_data)
    x = np.arange(19,25,.1)
    y = np.arange(500,2100,10)
    X,Y = np.meshgrid(x,y,indexing="ij")
    Z = np.zeros((len(x),len(y)))
    for i in range(len(x)):
        for j in range(len(y)):
            Z[i,j]=gmm.score_samples([[X[i,j],Y[i,j]]])[0]
    plt.figure()
    plt.contour(X,Y,Z,levels=np.arange(-15,-5,.5))
    plt.colorbar()
    plt.title("GMM, N=10")
    plt.xlabel("Temperature")
    plt.ylabel("CO2")
    plt.savefig("GMMContour.pdf")
//  [python]
def fit_best_gmm(X, random_state, max_components=2, cv_types=['spherical', 'tied', 'diag', 'full']):
   # Loop over multiple cv_types and n_components and for each fit a GMM
    # calculate the BIC and retain the lowest
    lowest_bic = np.infty
    bic = []
    n_components_range = range(1, max_components+1)
    for cv_type in cv_types:
        for n_components in n_components_range:
        # Fit a Gaussian mixture with EM
            gmm = mixture.GaussianMixture(n_components=n_components, covariance_type=cv_type, random_state=random_state)
            gmm.fit(X)
            bic.append(gmm.bic(X))
            if bic[-1] < lowest_bic:
                lowest_bic = bic[-1]
                best_gmm = gmm
                best_gmm_type = cv_type
                best_gmm_ncomponents = n_components
    return((best_gmm, best_gmm_type, best_gmm_ncomponents))
//  [python]
def fit_best_gmm(X, random_state, max_components=2, cv_types=['spherical', 'tied', 'diag', 'full']):
   # Loop over multiple cv_types and n_components and for each fit a GMM
    # calculate the BIC and retain the lowest
    lowest_bic = np.infty
    bic = []
    n_components_range = range(1, max_components+1)
    for cv_type in cv_types:
        for n_components in n_components_range:
        # Fit a Gaussian mixture with EM
            gmm = GaussianMixture(n_components=n_components, covariance_type=cv_type, random_state=random_state)
            gmm.fit(X)
            bic.append(gmm.bic(X))
            if bic[-1] < lowest_bic:
                lowest_bic = bic[-1]
                best_gmm = gmm
                best_gmm_type = cv_type
                best_gmm_ncomponents = n_components
    return((best_gmm, best_gmm_type, best_gmm_ncomponents))
//  [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 cloud of n points
    :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' (expectation-maximization) 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 cloud of n points
    :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' (expectation-maximization) 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 gmm_compute(x: np.array, n_components: int, cv_type: str) -> list:
    """Fit a Gaussian Mixture Model to the provided data and returns evaluation metrics.

    Args:
        x (numpy.ndarray): Data matrix to train the model
        n_components (int): Number of Gaussian components to use
        cv_type (str): Covariance matrix type to use. Must be one of "spherical", "tied", "diag", "full".

    Returns:
        - gmm_eval (list): model and associated BIC for downstream selection.

    """
    gmm = mixture.GMM(
        n_components=n_components,
        covariance_type=cv_type,
        n_iter=100000,
        init_params="kmeans",
    )
    gmm.fit(x)
    gmm_eval = [gmm, gmm.bic(x)]

    return gmm_eval
//  [python]
def gmm_compute(x: np.array, n_components: int, cv_type: str) -> list:
    """Fit a Gaussian Mixture Model to the provided data and returns evaluation metrics.

    Args:
        x (numpy.ndarray): Data matrix to train the model
        n_components (int): Number of Gaussian components to use
        cv_type (str): Covariance matrix type to use. Must be one of "spherical", "tied", "diag", "full".

    Returns:
        - gmm_eval (list): model and associated BIC for downstream selection.

    """
    gmm = mixture.GaussianMixture(
        n_components=n_components,
        covariance_type=cv_type,
        max_iter=100000,
        init_params="kmeans",
    )
    gmm.fit(x)
    gmm_eval = [gmm, gmm.bic(x)]

    return gmm_eval
//  [python]
def make_gmm(n_components, max_iter=150, random_state=None):
    """fn: wrapper for sklearn's Gaussian Mixture to create gmm object"""
    if not random_state:
        random_state = 7
    model_kwds = dict(
        n_components=n_components,
        max_iter=max_iter,
        n_init=100,
        init_params="random",
        random_state=random_state,
    )

    gmm = s.GaussianMixture(**model_kwds)
    return gmm
//  [python]
def make_gmm(n_components, max_iter=150, random_state=None):
    """fn: wrapper for sklearn's Gaussian Mixture to create gmm object"""
    if not random_state:
        random_state = 7
    model_kwds = dict(
        n_components=n_components,
        max_iter=max_iter,
        n_init=100,
        init_params="random",
        random_state=random_state,
    )

    gmm = mix.GaussianMixture(**model_kwds)
    return gmm
//  [python]
def _find_optimal_model(self, y: np.ndarray):
        """Find optimal GMM model.

        Parameters
        ----------
        y: The data to fit the model on (usually parameters).

        Returns
        -------
        best_gmm, best_n_components, bics:
            The best fitted GMM, the number of components, and all BIC values.
        """

        # iterate over numbers of clusters
        bics = {}
        best_gmm = None
        best_n_components = None
        for n_components in range(
            self.n_components_min, self.n_components_max + 1
        ):
            # fit a Gaussian mixture model
            gmm = s.GaussianMixture(
                n_components=n_components, **self.gmm_args
            )
            gmm.fit(y)

            # evaluate BIC as model selection criterion
            bic = gmm.bic(y)
            if len(bics) == 0 or bic < min(bics.values()):
                best_gmm = gmm
                best_n_components = n_components
            bics[n_components] = bic

        return best_gmm, best_n_components, bics
//  [python]
def _find_optimal_model(self, y: np.ndarray):
        """Find optimal GMM model.

        Parameters
        ----------
        y: The data to fit the model on (usually parameters).

        Returns
        -------
        best_gmm, best_n_components, bics:
            The best fitted GMM, the number of components, and all BIC values.
        """

        # iterate over numbers of clusters
        bics = {}
        best_gmm = None
        best_n_components = None
        for n_components in range(
            self.n_components_min, self.n_components_max + 1
        ):
            # fit a Gaussian mixture model
            gmm = skl_mx.GaussianMixture(
                n_components=n_components, **self.gmm_args
            )
            gmm.fit(y)

            # evaluate BIC as model selection criterion
            bic = gmm.bic(y)
            if len(bics) == 0 or bic < min(bics.values()):
                best_gmm = gmm
                best_n_components = n_components
            bics[n_components] = bic

        return best_gmm, best_n_components, bics
//  [python]
def get_GMM(
    model, dataloader, 
    val_loader=None, n_components=None, 
    random_state=12, n_aug=10, **kwargs):
    if 'Equi' in str(type(model)):
        n_aug = 0
    w_aug, traj_aug = model.group._random_data_aug(
        dataloader.dataset.w_data,
        dataloader.dataset.traj_data, 
        n_aug=n_aug, )
    if n_components is not None:
        gmm = mixture.GaussianMixture(n_components=n_components, random_state=random_state)
        z = model.encode(
            traj_aug.to(model.device),
            w_aug.to(model.device)).detach().cpu()
        gmm_labels = gmm.fit_predict(z)
    else:
        gmm, n_comp_best = get_optimized_GMM_latent(
            model, dataloader, random_state=random_state, 
            n_aug=n_aug, val_loader=val_loader, **kwargs)
    return gmm
//  [python]
def get_GMM(
    model, dataloader, 
    val_loader=None, n_components=None, 
    random_state=12, n_aug=10, **kwargs):
    if 'Equi' in str(type(model)):
        n_aug = 0
    w_aug, traj_aug = model.group._random_data_aug(
        dataloader.dataset.w_data,
        dataloader.dataset.traj_data, 
        n_aug=n_aug, )
    if n_components is not None:
        gmm = GaussianMixture(n_components=n_components, random_state=random_state)
        z = model.encode(
            traj_aug.to(model.device),
            w_aug.to(model.device)).detach().cpu()
        gmm_labels = gmm.fit_predict(z)
    else:
        gmm, n_comp_best = get_optimized_GMM_latent(
            model, dataloader, random_state=random_state, 
            n_aug=n_aug, val_loader=val_loader, **kwargs)
    return gmm
//  [python]
def cluster_gmm(self, n_clusts=5,max_pc=5):

        from sklearn.mixture import GMM
        model = GMM(n_components=n_clusts).fit(self._transformed_pca[:,:max_pc])
        self._clusts = np.array(model.predict(self._transformed_pca[:,:max_pc]))
//  [python]
def cluster_gmm(self, n_clusts=5,max_pc=5):

        from sklearn.mixture import GaussianMixture
        model = GaussianMixture(n_components=n_clusts).fit(self._transformed_pca[:,:max_pc])
        self._clusts = np.array(model.predict(self._transformed_pca[:,:max_pc]))
//  [python]
def get_optimized_GMM(
    data_train, data_val=None, 
    split_ratio=0.2, 
    random_state=12, 
    max_n_comp=30, 
    print_prob=True,
    **kwargs):
    if data_val is None:
        data_train, data_val = split_data(data_train, split_ratio)
    else:
        pass
    n_comp_best = 0
    log_prob_max = -np.inf
    gmm_best = None
    for n_comp in range(1, max_n_comp):
        gmm = mixture.GaussianMixture(n_components=n_comp, random_state=random_state)
        gmm_labels = gmm.fit_predict(data_train)
        log_prob = gmm._estimate_log_prob_resp(data_val)[0].sum()
        if log_prob > log_prob_max:
            n_comp_best = n_comp
            log_prob_max = log_prob
            gmm_best = gmm
    if print_prob:
        print(f'n_c = {n_comp_best:2d}, l_p = {int(log_prob_max):5d}, ', end='')
    return gmm_best, n_comp_best
//  [python]
def get_optimized_GMM(
    data_train, data_val=None, 
    split_ratio=0.2, 
    random_state=12, 
    max_n_comp=30, 
    print_prob=True,
    **kwargs):
    if data_val is None:
        data_train, data_val = split_data(data_train, split_ratio)
    else:
        pass
    n_comp_best = 0
    log_prob_max = -np.inf
    gmm_best = None
    for n_comp in range(1, max_n_comp):
        gmm = GaussianMixture(n_components=n_comp, random_state=random_state)
        gmm_labels = gmm.fit_predict(data_train)
        log_prob = gmm._estimate_log_prob_resp(data_val)[0].sum()
        if log_prob > log_prob_max:
            n_comp_best = n_comp
            log_prob_max = log_prob
            gmm_best = gmm
    if print_prob:
        print(f'n_c = {n_comp_best:2d}, l_p = {int(log_prob_max):5d}, ', end='')
    return gmm_best, n_comp_best
//  [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.GMM(n_components=n_gaussians, covariance_type='diag')
    gmm.weights_ = weights
    gmm.covariances_ = covariances
    gmm.means_ = means
    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
    gmm.precisions_cholesky_ = _compute_precision_cholesky(covariances, 'diag')
    return gmm
//  [python]
def theoretical_quantile(data, n_comp):
    model = mixture.GaussianMixture(n_components=n_comp, random_state=0)
    model.fit(data)
    q = []
    x = []
    # step =  ((np.max(model.sample(100000)[0])) - (np.min(model.sample(100000)[0])))/1000
    step = (np.max(data) - np.min(data)) / 1000
    d = np.arange(np.min(data), np.max(data), step)
    for i in d:
        x.append(i)
        q.append(mix_norm_cdf(i, model.weights_, model.means_, model.covariances_))
    return x, q
//  [python]
def theoretical_quantile(data, n_comp):
    model = GaussianMixture(n_components=n_comp, random_state=0)
    model.fit(data)
    q = []
    x = []
    # step =  ((np.max(model.sample(100000)[0])) - (np.min(model.sample(100000)[0])))/1000
    step = (np.max(data) - np.min(data)) / 1000
    d = np.arange(np.min(data), np.max(data), step)
    for i in d:
        x.append(i)
        q.append(mix_norm_cdf(i, model.weights_, model.means_, model.covariances_))
    return x, q
//  [python]
def trainModel(data, clusterNum, covType):
    """
    使用混合高斯训练模型
    """
    model = mixture.GaussianMixture(n_components=clusterNum, covariance_type=covType)
    model.fit(data)
    return model
//  [python]
def trainModel(data, clusterNum, covType):
    """
    使用混合高斯训练模型
    """
    model = GaussianMixture(n_components=clusterNum, covariance_type=covType)
    model.fit(data)
    return model
//  [python]
def lrts_comp(data):
    n = 0
    biggets_p = -1 * np.infty
    comp_biggest = 0
    max_comp = 10
    if len(data) < max_comp:
        max_comp = len(data)
    for i in range(1, max_comp + 1, 1):
        gm1 = mixture.GaussianMixture(n_components=i, random_state=0)
        gm2 = mixture.GaussianMixture(n_components=i + 1, random_state=0)
        gm1.fit(data)
        ll1 = np.mean(gm1.score_samples(data))
        gm2.fit(data)
        ll2 = np.mean(gm2.score_samples(data))
        LR = 2 * (ll2 - ll1)
        p = chi2.sf(LR, 1)
        if p > biggets_p:
            biggets_p = p
            comp_biggest = i
        n = comp_biggest
    return n
//  [python]
def lrts_comp(data):
    n = 0
    biggets_p = -1 * np.infty
    comp_biggest = 0
    max_comp = 10
    if len(data) < max_comp:
        max_comp = len(data)
    for i in range(1, max_comp + 1, 1):
        gm1 = GaussianMixture(n_components=i, random_state=0)
        gm2 = GaussianMixture(n_components=i + 1, random_state=0)
        gm1.fit(data)
        ll1 = np.mean(gm1.score_samples(data))
        gm2.fit(data)
        ll2 = np.mean(gm2.score_samples(data))
        LR = 2 * (ll2 - ll1)
        p = chi2.sf(LR, 1)
        if p > biggets_p:
            biggets_p = p
            comp_biggest = i
        n = comp_biggest
    return n
//  [python]
def gaussian_curve(hist2):
    nmodes = 2
    GMModel = mixture.GaussianMixture(n_components=nmodes, covariance_type='full')
    GMModel.fit(hist2)
    # mu1,mu2- Two mean values relative two Gaussian Models in the curve
    mu1, mu2 = np.round(GMModel.means_, 2)
    v1, v2 = np.round(GMModel.covariances_, 2)
    v11, v22 = (np.sqrt(v1), np.sqrt(v2))
    return mu1, mu2, v11, v22
//  [python]
def gaussian_curve(hist2):
    nmodes = 2
    GMModel = GaussianMixture(n_components=nmodes, covariance_type='full')
    GMModel.fit(hist2)
    # mu1,mu2- Two mean values relative two Gaussian Models in the curve
    mu1, mu2 = np.round(GMModel.means_, 2)
    v1, v2 = np.round(GMModel.covariances_, 2)
    v11, v22 = (np.sqrt(v1), np.sqrt(v2))
    return mu1, mu2, v11, v22
//  [python]
def initialize_gmm(self, dataloader):
        use_cuda = torch.cuda.is_available()
        if use_cuda:
            self.cuda()

        self.eval()
        data = []
        for batch_idx, (inputs, _) in enumerate(dataloader):
            inputs = inputs.view(inputs.size(0), -1).float()
            if use_cuda:
                inputs = inputs.cuda()
            inputs = Variable(inputs)
            z, outputs, mu, logvar = self.forward(inputs)
            data.append(z.data.cpu().numpy())
        data = np.concatenate(data)
        gmm = mixture.GaussianMixture(n_components=self.n_centroids,covariance_type='diag')
        gmm.fit(data)
        self.u_p.data.copy_(torch.from_numpy(gmm.means_.T.astype(np.float32)))
        self.lambda_p.data.copy_(torch.from_numpy(gmm.covariances_.T.astype(np.float32)))
//  [python]
def initialize_gmm(self, dataloader):
        use_cuda = torch.cuda.is_available()
        if use_cuda:
            self.cuda()

        self.eval()
        data = []
        for batch_idx, (inputs, _) in enumerate(dataloader):
            inputs = inputs.view(inputs.size(0), -1).float()
            if use_cuda:
                inputs = inputs.cuda()
            inputs = Variable(inputs)
            z, outputs, mu, logvar = self.forward(inputs)
            data.append(z.data.cpu().numpy())
        data = np.concatenate(data)
        gmm = GaussianMixture(n_components=self.n_centroids,covariance_type='diag')
        gmm.fit(data)
        self.u_p.data.copy_(torch.from_numpy(gmm.means_.T.astype(np.float32)))
        self.lambda_p.data.copy_(torch.from_numpy(gmm.covariances_.T.astype(np.float32)))
//  [python]
def component(data, columns, method):
    n = 1
    max_comp = 10
    x = []
    if data.shape[0] < max_comp:
        max_comp = data.shape[0]
    if len(columns) == 1:
        x = np.transpose([data[columns[0]].values])
    else:
        x = data[columns].values
    if method == "aic":
        lowest_aic = np.infty
        comp_lowest = 0
        for i in range(1, max_comp + 1, 1):
            gm1 = mixture.GaussianMixture(n_components=i, random_state=0)
            gm1.fit(x)
            aic1 = gm1.aic(x)
            if aic1 < lowest_aic:
                lowest_aic = aic1
                comp_lowest = i
            n = comp_lowest

    if method == "bic":
        lowest_bic = np.infty
        comp_lowest = 0
        for i in range(1, max_comp + 1, 1):
            gm1 = mixture.GaussianMixture(n_components=i, random_state=0)
            gm1.fit(x)
            bic1 = gm1.bic(x)
            if bic1 < lowest_bic:
                lowest_bic = bic1
                comp_lowest = i
            n = comp_lowest

    if method == "LRTS":
        n = lrts_comp(x)
    if method == "quantile":
        biggest_p = -1 * np.infty
        comp_biggest = 0
        for i in range(1, max_comp, 1):
            vals, q = theoretical_quantile(x, i)
            dist = sum_dist(x, vals, q)
            p = probability_mix(dist, vals, q)
            if p > biggest_p:
                biggest_p = p
                comp_biggest = i
        n = comp_biggest
    return n
//  [python]
def component(data, columns, method):
    n = 1
    max_comp = 10
    x = []
    if data.shape[0] < max_comp:
        max_comp = data.shape[0]
    if len(columns) == 1:
        x = np.transpose([data[columns[0]].values])
    else:
        x = data[columns].values
    if method == "aic":
        lowest_aic = np.infty
        comp_lowest = 0
        for i in range(1, max_comp + 1, 1):
            gm1 = GaussianMixture(n_components=i, random_state=0)
            gm1.fit(x)
            aic1 = gm1.aic(x)
            if aic1 < lowest_aic:
                lowest_aic = aic1
                comp_lowest = i
            n = comp_lowest

    if method == "bic":
        lowest_bic = np.infty
        comp_lowest = 0
        for i in range(1, max_comp + 1, 1):
            gm1 = GaussianMixture(n_components=i, random_state=0)
            gm1.fit(x)
            bic1 = gm1.bic(x)
            if bic1 < lowest_bic:
                lowest_bic = bic1
                comp_lowest = i
            n = comp_lowest

    if method == "LRTS":
        n = lrts_comp(x)
    if method == "quantile":
        biggest_p = -1 * np.infty
        comp_biggest = 0
        for i in range(1, max_comp, 1):
            vals, q = theoretical_quantile(x, i)
            dist = sum_dist(x, vals, q)
            p = probability_mix(dist, vals, q)
            if p > biggest_p:
                biggest_p = p
                comp_biggest = i
        n = comp_biggest
    return n
//  [python]
def compute_targets(v, vn, n_targets, vc=None):
    knn = NearestNeighbors(n_neighbors=20, algorithm='kd_tree').fit(v)
    _, indices = knn.kneighbors(v)
    vn_neighbors = vn[indices]
    if vc is not None:
        vc_neighbors = vc[indices]

    v_num = v.shape[0]
    normal_weights = np.ones(v_num)
    rgb_weights = np.ones(v_num)
    for i in range(v_num):
        normal_weights[i] = np.linalg.norm(np.cov(vn_neighbors[i].T), 'fro')
        if vc is not None:
            rgb_weights[i] = np.linalg.norm(np.cov(vc_neighbors[i].T), 'fro')

    v_norm = np.sqrt((v ** 2).sum(1))
    unit_v = v / v_norm[..., None]
    k = n_targets
    gmm = mixture.GaussianMixture(n_components=k, covariance_type='full', random_state=0)
    gmm.fit(unit_v)
    pred_label = gmm.predict(unit_v)
    pred_prob = gmm.predict_proba(unit_v).max(1)
    weights_ = pred_prob * (v_norm ** 4) * normal_weights * rgb_weights
    targets = np.zeros([k, 3])
    radius = np.ones([k])
    weights = gmm.weights_
    for i in range(k):
        index = pred_label == i
        w_sum = weights_[index].sum()
        targets[i] = (v[index] * weights_[index][..., None]).sum(0) / w_sum
        radius[i] = np.quantile(np.sqrt(((v[index] - targets[i]) ** 2).sum(1)), 0.3)
        weights[i] = weights[i] * w_sum

    weights = weights / weights.sum()
    return targets, weights, radius, weights_
//  [python]
def compute_targets(v, vn, n_targets, vc=None):
    knn = NearestNeighbors(n_neighbors=20, algorithm='kd_tree').fit(v)
    _, indices = knn.kneighbors(v)
    vn_neighbors = vn[indices]
    if vc is not None:
        vc_neighbors = vc[indices]

    v_num = v.shape[0]
    normal_weights = np.ones(v_num)
    rgb_weights = np.ones(v_num)
    for i in range(v_num):
        normal_weights[i] = np.linalg.norm(np.cov(vn_neighbors[i].T), 'fro')
        if vc is not None:
            rgb_weights[i] = np.linalg.norm(np.cov(vc_neighbors[i].T), 'fro')

    v_norm = np.sqrt((v ** 2).sum(1))
    unit_v = v / v_norm[..., None]
    k = n_targets
    gmm = GaussianMixture(n_components=k, covariance_type='full', random_state=0)
    gmm.fit(unit_v)
    pred_label = gmm.predict(unit_v)
    pred_prob = gmm.predict_proba(unit_v).max(1)
    weights_ = pred_prob * (v_norm ** 4) * normal_weights * rgb_weights
    targets = np.zeros([k, 3])
    radius = np.ones([k])
    weights = gmm.weights_
    for i in range(k):
        index = pred_label == i
        w_sum = weights_[index].sum()
        targets[i] = (v[index] * weights_[index][..., None]).sum(0) / w_sum
        radius[i] = np.quantile(np.sqrt(((v[index] - targets[i]) ** 2).sum(1)), 0.3)
        weights[i] = weights[i] * w_sum

    weights = weights / weights.sum()
    return targets, weights, radius, weights_