jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def GM(self):
        try:
            nclusters = self.kw['nclusters']
            if not self.dataframe is None:
                self.cluster_result = GaussianMixture(n_components=nclusters).fit_predict(self.dataframe.values)
                self.cluster_plot_data, ok = self.t_sne(2)
                return True
            else:
                self.error_msg = 'Data is null.'
                return False
        except Exception as e:
            self.error_msg = str(e)
            return False
//  [python]
def fit_gmm_multiple_components(input_loss):
    gmm = mixture.GMM(n_components=20,max_iter=10,tol=1e-2,reg_covar=5e-4)
    gmm.fit(input_loss)
    components_open = []
    components_clean = []
    components_closed = []
    for n in range(gmm.n_components):
        if (gmm.means_[n] > .3) & (gmm.means_[n] < .7):
            components_open.extend([n])
        elif (gmm.means_[n] < .3):
            components_clean.extend([n])
        else:
            components_closed.extend([n])
    prob = gmm.predict_proba(input_loss)
    # transform this probability into a 3-component probability
    prob_clean = np.sum(prob[:,components_clean],axis=1)
    prob_closed = np.sum(prob[:,components_closed],axis=1)
    prob_open = np.sum(prob[:,components_open], axis=1)
    return prob_clean, prob_open, prob_closed
//  [python]
def fit_gmm_multiple_components(input_loss):
    gmm = GaussianMixture(n_components=20,max_iter=10,tol=1e-2,reg_covar=5e-4)
    gmm.fit(input_loss)
    components_open = []
    components_clean = []
    components_closed = []
    for n in range(gmm.n_components):
        if (gmm.means_[n] > .3) & (gmm.means_[n] < .7):
            components_open.extend([n])
        elif (gmm.means_[n] < .3):
            components_clean.extend([n])
        else:
            components_closed.extend([n])
    prob = gmm.predict_proba(input_loss)
    # transform this probability into a 3-component probability
    prob_clean = np.sum(prob[:,components_clean],axis=1)
    prob_closed = np.sum(prob[:,components_closed],axis=1)
    prob_open = np.sum(prob[:,components_open], axis=1)
    return prob_clean, prob_open, prob_closed
//  [python]
def __init__(self, samples: Sequence[float], gmm_k: int = 2, seed: int = None):
        self._model = mixture.GaussianMixture(
            n_components=min(len(samples), gmm_k),
            covariance_type="full",
            random_state=seed,
        )
        self._model.fit(samples)
//  [python]
def __init__(self, samples: Sequence[float], gmm_k: int = 2, seed: int = None):
        self._model = GaussianMixture(
            n_components=min(len(samples), gmm_k),
            covariance_type="full",
            random_state=seed,
        )
        self._model.fit(samples)
//  [python]
def gmm(X, k):
    """calculates a GMM from a dataset"""
    gauss = mixture.GaussianMixture(n_components=k).fit(X)
    pi = gauss.weights_
    m = gauss.means_
    S = gauss.covariances_
    clss = gauss.predict(X)
    bic = gauss.bic(X)
    return pi, m, S, clss, bic
//  [python]
def gmm(X, k):
    """calculates a GMM from a dataset"""
    gauss = sklearn.mixture.GaussianMixture(n_components=k).fit(X)
    pi = gauss.weights_
    m = gauss.means_
    S = gauss.covariances_
    clss = gauss.predict(X)
    bic = gauss.bic(X)
    return pi, m, S, clss, bic
//  [python]
def perform_gmm(args):
    data = np.load(args.data_path)
    gm = mixture.GaussianMixture(n_components=args.n_components, random_state=0,
                         verbose=2,
                         covariance_type='full',).fit(data)
    pred = gm.predict(data)
    np.save(args.target_path, pred)
//  [python]
def perform_gmm(args):
    data = np.load(args.data_path)
    gm = GaussianMixture(n_components=args.n_components, random_state=0,
                         verbose=2,
                         covariance_type='full',).fit(data)
    pred = gm.predict(data)
    np.save(args.target_path, pred)
//  [python]
def init_gmm_params(self, dataloader, device='cpu'):
        """
        Init SCALE model with GMM model parameters
        """
        gmm = mixture.GaussianMixture(n_components=self.n_centroids, covariance_type='diag')
        z = self.encodeBatch(dataloader, device)
        gmm.fit(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, device='cpu'):
        """
        Init SCALE model with GMM model parameters
        """
        gmm = GaussianMixture(n_components=self.n_centroids, covariance_type='diag')
        z = self.encodeBatch(dataloader, device)
        gmm.fit(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 fit(self, X, y=None):
        """Fit detector. y is ignored in unsupervised methods.

        Parameters
        ----------
        X : numpy array of shape (n_samples, n_features)
            The input samples.

        y : Ignored
            Not used, present for API consistency by convention.

        sample_weight : array-like, shape (n_samples,)
            Per-sample weights. Rescale C per sample. Higher weights
            force the classifier to put more emphasis on these points.

        Returns
        -------
        self : object
            Fitted estimator.
        """
        # validate inputs X and y (optional)
        X = check_array(X)
        self._set_n_classes(y)

        self.detector_ = mixture.GaussianMixture(
            n_components=self.n_components,
            covariance_type=self.covariance_type,
            tol=self.tol,
            reg_covar=self.reg_covar,
            max_iter=self.max_iter,
            n_init=self.n_init,
            init_params=self.init_params,
            weights_init=self.weights_init,
            means_init=self.means_init,
            precisions_init=self.precisions_init,
            random_state=self.random_state,
            warm_start=self.warm_start,
        )

        self.detector_.fit(X=X, y=y)

        # invert decision_scores_. Outliers comes with higher outlier scores
        self.decision_scores_ = invert_order(self.detector_.score_samples(X))
        self._process_decision_scores()

        return self
//  [python]
def fit(self, X, y=None):
        """Fit detector. y is ignored in unsupervised methods.

        Parameters
        ----------
        X : numpy array of shape (n_samples, n_features)
            The input samples.

        y : Ignored
            Not used, present for API consistency by convention.

        sample_weight : array-like, shape (n_samples,)
            Per-sample weights. Rescale C per sample. Higher weights
            force the classifier to put more emphasis on these points.

        Returns
        -------
        self : object
            Fitted estimator.
        """
        # validate inputs X and y (optional)
        X = check_array(X)
        self._set_n_classes(y)

        self.detector_ = GaussianMixture(
            n_components=self.n_components,
            covariance_type=self.covariance_type,
            tol=self.tol,
            reg_covar=self.reg_covar,
            max_iter=self.max_iter,
            n_init=self.n_init,
            init_params=self.init_params,
            weights_init=self.weights_init,
            means_init=self.means_init,
            precisions_init=self.precisions_init,
            random_state=self.random_state,
            warm_start=self.warm_start,
        )

        self.detector_.fit(X=X, y=y)

        # invert decision_scores_. Outliers comes with higher outlier scores
        self.decision_scores_ = invert_order(self.detector_.score_samples(X))
        self._process_decision_scores()

        return self
//  [python]
def _estimate_GMM(self, x, n_component_lim=[1, 3]):
        """
        Find the GMM that best fit the data in x using Bayesian information criterion.
        """
        min_comp = n_component_lim[0]
        max_comp = n_component_lim[1]

        lowest_BIC = np.inf

        counter = 0
        for i_comp in range(min_comp, max_comp + 1):
            GMM = mixture.GaussianMixture(i_comp)
            GMM.fit(x)
            BIC = GMM.bic(x)
            if BIC < lowest_BIC:
                lowest_BIC = BIC
                best_GMM = mixture.GaussianMixture(i_comp)
                best_GMM.weights_ = GMM.weights_
                best_GMM.means_ = GMM.means_
                best_GMM.covariances_ = GMM.covariances_

            counter += 1

        return best_GMM
//  [python]
def _estimate_GMM(self, x, n_component_lim=[1, 3]):
        """
        Find the GMM that best fit the data in x using Bayesian information criterion.
        """
        min_comp = n_component_lim[0]
        max_comp = n_component_lim[1]

        lowest_BIC = np.inf

        counter = 0
        for i_comp in range(min_comp, max_comp + 1):
            GMM = GaussianMixture(i_comp)
            GMM.fit(x)
            BIC = GMM.bic(x)
            if BIC < lowest_BIC:
                lowest_BIC = BIC
                best_GMM = GaussianMixture(i_comp)
                best_GMM.weights_ = GMM.weights_
                best_GMM.means_ = GMM.means_
                best_GMM.covariances_ = GMM.covariances_

            counter += 1

        return best_GMM
//  [python]
def gmm(self, x, max_num_clusters=None):
        X = np.array([[i] for i in x])
        if max_num_clusters == None:
            max_num_clusters = self.max_num_clusters
        N = np.arange(1, max_num_clusters + 1)
        models = [None for i in range(len(N))]
        AICs = []
        BICs = []
        if len(x) == 1:
            return []
        if len(set(x)) == 1:
            return [x]
        for i in range(len(N)):
            models[i] = mixture.GMM(N[i], init_params="k-means++").fit(X)
        AIC = [m.aic(X) for m in models]
        BIC = [m.bic(X) for m in models]
        M_best = models[np.argmin(AIC)]
        labels = M_best.predict(X)
        
        nclusters = M_best.n_components
        clusters = [[] for i in range(M_best.n_components)]
        for label, i in zip(labels, X):
            clusters[label].append(i[0])
            
        # sort clusters
        for cluster in clusters:
            cluster.sort()
        clusters.sort(key = lambda c:c[0])
        for cluster in clusters:
            m = np.mean(cluster)
            s = np.std(cluster)

        merged_clusters = self.merge_gmm_clusters(clusters)

        return [c for c in merged_clusters if len(c) >= self.min_pts]
//  [python]
def gmm(self, x, max_num_clusters=None):
        X = np.array([[i] for i in x])
        if max_num_clusters == None:
            max_num_clusters = self.max_num_clusters
        N = np.arange(1, max_num_clusters + 1)
        models = [None for i in range(len(N))]
        AICs = []
        BICs = []
        if len(x) == 1:
            return []
        if len(set(x)) == 1:
            return [x]
        for i in range(len(N)):
            models[i] = GaussianMixture(N[i], init_params="k-means++").fit(X)
        AIC = [m.aic(X) for m in models]
        BIC = [m.bic(X) for m in models]
        M_best = models[np.argmin(AIC)]
        labels = M_best.predict(X)
        
        nclusters = M_best.n_components
        clusters = [[] for i in range(M_best.n_components)]
        for label, i in zip(labels, X):
            clusters[label].append(i[0])
            
        # sort clusters
        for cluster in clusters:
            cluster.sort()
        clusters.sort(key = lambda c:c[0])
        for cluster in clusters:
            m = np.mean(cluster)
            s = np.std(cluster)

        merged_clusters = self.merge_gmm_clusters(clusters)

        return [c for c in merged_clusters if len(c) >= self.min_pts]
//  [python]
def cluster_GMM(self):
        # Initalize a GMM object and use it for clustering.
        clf = mixture.GaussianMixture(
            n_components=self.num_clusters,
            covariance_type="tied",
            init_params="kmeans",
            max_iter=50
        )

        # Get cluster assignments.
        clf.fit(self.word_vectors)
        idx = clf.predict(self.word_vectors)
        print("Clustering Done...")

        # Get probabilities of cluster assignments.
        idx_proba = clf.predict_proba(self.word_vectors)

        # Dump cluster assignments and probability of cluster assignments.
        pickle.dump(idx, open(self.pname1, "wb"))
        print("Cluster Assignments Saved...")
        pickle.dump(idx_proba, open(self.pname2, "wb"))
        print("Probabilities of Cluster Assignments saved...")

        return (idx, idx_proba)
//  [python]
def cluster_GMM(self):
        # Initalize a GMM object and use it for clustering.
        clf = GaussianMixture(
            n_components=self.num_clusters,
            covariance_type="tied",
            init_params="kmeans",
            max_iter=50
        )

        # Get cluster assignments.
        clf.fit(self.word_vectors)
        idx = clf.predict(self.word_vectors)
        print("Clustering Done...")

        # Get probabilities of cluster assignments.
        idx_proba = clf.predict_proba(self.word_vectors)

        # Dump cluster assignments and probability of cluster assignments.
        pickle.dump(idx, open(self.pname1, "wb"))
        print("Cluster Assignments Saved...")
        pickle.dump(idx_proba, open(self.pname2, "wb"))
        print("Probabilities of Cluster Assignments saved...")

        return (idx, idx_proba)
//  [python]
def _fit_gaussianmixture(self, df, target, config):
        config_cp = copy.deepcopy(config)
        del config_cp['method']
        encoder = mixture.GaussianMixture(**config_cp).fit(df[target])
        name = "_".join(target) + "_gaussianmixture"
        self.trans_ls.append(('gaussianmixture', name, target, encoder))
//  [python]
def _fit_gaussianmixture(self, df, target, config):
        config_cp = copy.deepcopy(config)
        del config_cp['method']
        encoder = GaussianMixture(**config_cp).fit(df[target])
        name = "_".join(target) + "_gaussianmixture"
        self.trans_ls.append(('gaussianmixture', name, target, encoder))
//  [python]
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))

y_pred = mixture.GaussianMixture(n_components=3).fit_predict(X_aniso)
//  [python]
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))

y_pred = GaussianMixture(n_components=3).fit_predict(X_aniso)
//  [python]
def GMM(eigvec):
    gmm = mixture.GaussianMixture(n_components=2, max_iter=300)
    gmm.fit(eigvec)
    partition = gmm.predict(eigvec)
    return partition
//  [python]
def GMM(eigvec):
    gmm = GaussianMixture(n_components=2, max_iter=300)
    gmm.fit(eigvec)
    partition = gmm.predict(eigvec)
    return partition
//  [python]
def GMM_model(vocab_embeddings, vocab,  topics, rerank, rand):
    GMM = mixture.GaussianMixture(n_components=topics, random_state=rand).fit(vocab_embeddings)
    indices = []
    for i in range(GMM.n_components):
        density = scipy.stats.multivariate_normal(cov=GMM.covariances_[i], mean=GMM.means_[i]).logpdf(vocab_embeddings)
        topk_vals = density.argsort()[-1*len(density):][::-1].astype(int)
        if rerank:
            indices.append(find_top_k_words(100, topk_vals, vocab))
        else:
            indices.append(find_top_k_words(10, topk_vals, vocab))

    return GMM.predict(vocab_embeddings), indices
//  [python]
def GMM_model(vocab_embeddings, vocab,  topics, rerank, rand):
    GMM = GaussianMixture(n_components=topics, random_state=rand).fit(vocab_embeddings)
    indices = []
    for i in range(GMM.n_components):
        density = scipy.stats.multivariate_normal(cov=GMM.covariances_[i], mean=GMM.means_[i]).logpdf(vocab_embeddings)
        topk_vals = density.argsort()[-1*len(density):][::-1].astype(int)
        if rerank:
            indices.append(find_top_k_words(100, topk_vals, vocab))
        else:
            indices.append(find_top_k_words(10, topk_vals, vocab))

    return GMM.predict(vocab_embeddings), indices
//  [python]
def _fit_gmm(X, n_components):
        mixture = mixture.GaussianMixture(n_components=n_components, n_init=1).fit(X)
        aic = mixture.aic(X)
        return mixture, aic
//  [python]
def _fit_gmm(X, n_components):
        mixture = GaussianMixture(n_components=n_components, n_init=1).fit(X)
        aic = mixture.aic(X)
        return mixture, aic
//  [python]
def Frame_to_SklearnMixture(self):
        clf = mixture.GaussianMixture(n_components=self.mix_num, covariance_type='diag')
        #为均值方差赋值
        clf.fit(np.random.rand(self.mix_num, 3))  # Now it thinks it is trained
        #下面这句话有更简单表达,把size(3,1)变成size（3，）只需reshape(3)即可
        for i in range(clf.weights_.shape[0]):    
            clf.weights_[i] = self.weights[i]   # mixture weights (n_components,) 
        clf.weights_ = clf.weights_/sum(clf.weights_)
        # print(type(clf.weights_),clf.weights_.shape)
        clf.means_ = self.means          # mixture means (n_components, 2) 
        clf.covariances_ = self.covariances  # mixture cov (n_components, 2, 2
        return clf
//  [python]
def Frame_to_SklearnMixture(self):
        clf = GaussianMixture(n_components=self.mix_num, covariance_type='diag')
        #为均值方差赋值
        clf.fit(np.random.rand(self.mix_num, 3))  # Now it thinks it is trained
        #下面这句话有更简单表达,把size(3,1)变成size（3，）只需reshape(3)即可
        for i in range(clf.weights_.shape[0]):    
            clf.weights_[i] = self.weights[i]   # mixture weights (n_components,) 
        clf.weights_ = clf.weights_/sum(clf.weights_)
        # print(type(clf.weights_),clf.weights_.shape)
        clf.means_ = self.means          # mixture means (n_components, 2) 
        clf.covariances_ = self.covariances  # mixture cov (n_components, 2, 2
        return clf
//  [python]
def train_model():

	source   = "C:\\Users\\Vaibhav\\Desktop\\SpeakerIdentification\\training_set\\"   
	dest = "C:\\Users\\Vaibhav\\Desktop\\SpeakerIdentification\\trained_models\\"
	train_file = "C:\\Users\\Vaibhav\\Desktop\\SpeakerIdentification\\training_set_addition.txt"        
	file_paths = open(train_file,'r')
	count = 1
	features = np.asarray(())
	for path in file_paths:    
	    path = path.strip()   
	    print(path)

	    sr,audio = read(source + path)
	    print(sr)
	    vector   = extract_features(audio,sr)
	    
	    if features.size == 0:
	        features = vector
	    else:
	        features = np.vstack((features, vector))

	    if count == 5:    
	        gmm = mixture.GaussianMixture(n_components = 6, max_iter = 200, covariance_type='diag',n_init = 3)
	        gmm.fit(features)
	        
	        # dumping the trained gaussian model
	        picklefile = path.split("-")[0]+".gmm"
	        pickle.dump(gmm,open(dest + picklefile,'wb'))
	        print('+ modeling completed for speaker:',picklefile," with data point = ",features.shape)   
	        features = np.asarray(())
	        count = 0
	    count = count + 1
//  [python]
def train_model():

	source   = "C:\\Users\\Vaibhav\\Desktop\\SpeakerIdentification\\training_set\\"   
	dest = "C:\\Users\\Vaibhav\\Desktop\\SpeakerIdentification\\trained_models\\"
	train_file = "C:\\Users\\Vaibhav\\Desktop\\SpeakerIdentification\\training_set_addition.txt"        
	file_paths = open(train_file,'r')
	count = 1
	features = np.asarray(())
	for path in file_paths:    
	    path = path.strip()   
	    print(path)

	    sr,audio = read(source + path)
	    print(sr)
	    vector   = extract_features(audio,sr)
	    
	    if features.size == 0:
	        features = vector
	    else:
	        features = np.vstack((features, vector))

	    if count == 5:    
	        gmm = GaussianMixture(n_components = 6, max_iter = 200, covariance_type='diag',n_init = 3)
	        gmm.fit(features)
	        
	        # dumping the trained gaussian model
	        picklefile = path.split("-")[0]+".gmm"
	        pickle.dump(gmm,open(dest + picklefile,'wb'))
	        print('+ modeling completed for speaker:',picklefile," with data point = ",features.shape)   
	        features = np.asarray(())
	        count = 0
	    count = count + 1
//  [python]
def run_gmm(dataset_names: List, number_of_clusters: List, random_state=1):
    """
    Run the Gaussian Mixture Model (GMM) clustering algorithm on multiple datasets and collect and save the results.

    Args:
        dataset_names (list): A list of dataset names.
        number_of_clusters (list): A list of the number of clusters to use for each dataset.
        random_state (int): An optional random seed for reproducibility. Default is 1.

    Returns:
        None
    """
    results = {}  # Initialize an empty dictionary to store the results

    # Loop through each dataset and corresponding number of clusters
    for dataset_name, n_clusters in zip(dataset_names, number_of_clusters):
        # as it was discovered for some datasets ("jm1", "KDDTest+", etc.) GaussianMixture throws an error
        try:
            start = timer()  # Record the start time of model evaluation
            X, true_labels = get_data(
                dataset_name
            )  # Load the dataset and extract true labels
            model = mixture.GaussianMixture(
                n_components=n_clusters, random_state=random_state
            ).fit(
                X
            )  # Train the GMM model
            pred_labels = model.predict(X)  # Predict the cluster labels
            error_results = metrics.calculate_metrics(
                true_labels, pred_labels
            )  # Calculate error metrics
            results[dataset_name] = load_save.format_results(
                error_results, timer() - start
            )  # Collect and store results
        except Exception as e:
            print(e)  # Print any exception that occurs during model evaluation

    filename = f"./../results/gaussian_mixture_results.json"  # Define the filename to save the results
    load_save.save_json(results, filename)
//  [python]
def run_gmm(dataset_names: List, number_of_clusters: List, random_state=1):
    """
    Run the Gaussian Mixture Model (GMM) clustering algorithm on multiple datasets and collect and save the results.

    Args:
        dataset_names (list): A list of dataset names.
        number_of_clusters (list): A list of the number of clusters to use for each dataset.
        random_state (int): An optional random seed for reproducibility. Default is 1.

    Returns:
        None
    """
    results = {}  # Initialize an empty dictionary to store the results

    # Loop through each dataset and corresponding number of clusters
    for dataset_name, n_clusters in zip(dataset_names, number_of_clusters):
        # as it was discovered for some datasets ("jm1", "KDDTest+", etc.) GaussianMixture throws an error
        try:
            start = timer()  # Record the start time of model evaluation
            X, true_labels = get_data(
                dataset_name
            )  # Load the dataset and extract true labels
            model = GaussianMixture(
                n_components=n_clusters, random_state=random_state
            ).fit(
                X
            )  # Train the GMM model
            pred_labels = model.predict(X)  # Predict the cluster labels
            error_results = metrics.calculate_metrics(
                true_labels, pred_labels
            )  # Calculate error metrics
            results[dataset_name] = load_save.format_results(
                error_results, timer() - start
            )  # Collect and store results
        except Exception as e:
            print(e)  # Print any exception that occurs during model evaluation

    filename = f"./../results/gaussian_mixture_results.json"  # Define the filename to save the results
    load_save.save_json(results, filename)
//  [python]
def _initialize_model(self, model):
        """
        Util to initialize a classifier model for the Picker object
        based on configuration values.
        """

        logger.info(f"Classifier model desired = {model}")
        if model == "gaussian_mixture":
            from sklearn.mixture import GaussianMixture

            self.model = GaussianMixture(n_components=2)
        elif model == "gaussian_naive_bayes":
            from sklearn.naive_bayes import GaussianNB

            self.model = GaussianNB()
        elif model == "xgboost":
            try:
                import xgboost as xgb
            except ModuleNotFoundError as e:
                logger.error(
                    "`xgboost` import failed. xgboost is an optional package, ensure it is installed."
                )
                raise e

            self.model = xgb.XGBClassifier(
                objective="binary:hinge",
                learning_rate=0.1,
                max_depth=6,
                n_estimators=10,
                method="gpu_hist",
            )
        elif model == "thunder_svm":
            try:
                import thundersvm
            except ModuleNotFoundError as e:
                logger.error(
                    "`thundersvm` import failed. thundersvm is an optional package, ensure it is installed."
                )
                raise e

            self.model = thundersvm.SVC(
                kernel=self.model_opts.get("svm_kernel", "rbf"),
                gamma=float(self.model_opts.get("svm_gamma", 0.5)),
            )
        else:
            logger.info("Using SVM Classifier")

            self.model = svm.SVC(
                C=1,
                kernel=self.model_opts.get("svm_kernel", "rbf"),
                gamma=float(self.model_opts.get("svm_gamma", 0.5)),
                class_weight="balanced",
            )
//  [python]
def get_gmm(dist, n_components):
    # fit a **ground truth** label distribution
    all_labels = dist.sample([10000, ])     # assume sufficient samples
    if len(all_labels.shape) == 1:
        all_labels = all_labels.unsqueeze(-1)
    gmm = mixture.GaussianMixture(n_components=n_components).fit(all_labels)
    gmm_dict = {'means': gmm.means_, 'weights': gmm.weights_, 'variances': gmm.covariances_}
    return gmm_dict
//  [python]
def get_gmm(dist, n_components):
    # fit a **ground truth** label distribution
    all_labels = dist.sample([10000, ])     # assume sufficient samples
    if len(all_labels.shape) == 1:
        all_labels = all_labels.unsqueeze(-1)
    gmm = GaussianMixture(n_components=n_components).fit(all_labels)
    gmm_dict = {'means': gmm.means_, 'weights': gmm.weights_, 'variances': gmm.covariances_}
    return gmm_dict
//  [python]
def build_model(self):
        self.model_obj = mixture.GaussianMixture(n_components=self.param.n_state, covariance_type=self.param.covariance_type)
//  [python]
def build_model(self):
        self.model_obj = GaussianMixture(n_components=self.param.n_state, covariance_type=self.param.covariance_type)
//  [python]
def evaluate(
        self,
        X_gt: DataLoader,
        X_syn: DataLoader,
    ) -> Dict:
        scores = []

        for component in [1, 5, 10]:
            gmm = mixture.GaussianMixture(n_components=component, covariance_type="diag")
            gmm.fit(X_gt.dataframe())

            scores.append(gmm.score(X_syn.dataframe()))  # Higher is better

        scores_np = np.asarray(scores)
        scores_np = (scores_np - np.min(scores_np)) / (
            np.max(scores_np) - np.min(scores_np)
        )  # transform scores to [0, 1]
        scores_np = 1 - scores_np  # invert scores - lower is better

        return {self._reduction: self.reduction()(scores_np)}
//  [python]
def evaluate(
        self,
        X_gt: DataLoader,
        X_syn: DataLoader,
    ) -> Dict:
        scores = []

        for component in [1, 5, 10]:
            gmm = GaussianMixture(n_components=component, covariance_type="diag")
            gmm.fit(X_gt.dataframe())

            scores.append(gmm.score(X_syn.dataframe()))  # Higher is better

        scores_np = np.asarray(scores)
        scores_np = (scores_np - np.min(scores_np)) / (
            np.max(scores_np) - np.min(scores_np)
        )  # transform scores to [0, 1]
        scores_np = 1 - scores_np  # invert scores - lower is better

        return {self._reduction: self.reduction()(scores_np)}
//  [python]
def compute_gmm(data, x_min, x_max, ax):
    
    # select number components based on Bayesian Information Criterion (BIC)
    
    best_bic = np.inf  # initializing with a high value for minimization
    best_n_components = -1

    for n_components in range(1, 3 + 1):
        gmm = mixture.GaussianMixture(n_components=n_components, random_state=0)
        gmm.fit(data)
        bic = gmm.bic(data)  # calculate BIC

        if bic < best_bic:
            best_bic = bic
            best_n_components = n_components
    
    # fit a Gaussian mixture model to histogram data using best_n_components
    gmm = mixture.GaussianMixture(n_components=best_n_components, random_state=0)
    gmm.fit(data)

    # generate points (along the DNA intensity histogram X range) for plotting the GMM 
    x = np.linspace(x_min, x_max, 100)
    log_prob = gmm.score_samples(x.reshape(-1, 1))
    pdf = np.exp(log_prob)

    # plot overall GMM fit
    # ax.plot(x, pdf, c='k', lw=3, alpha=0.75, label='GMM fit')

    # plot individual GMM components
    peak_maxs = []
    for i in range(gmm.n_components):
        pdf = (
            (gmm.weights_[i] * 
             norm.pdf(x, gmm.means_[i, 0], np.sqrt(gmm.covariances_[i, 0, 0])))
        )
        ax.plot(x, pdf, '--', lw=3, alpha=0.75, label=f'GMM {i+1}')
        peak_maxs.append(pdf.max())
    
    ax.set_ylim(0, None)  # ensuring GMM y-axis starts at 0
    
    leg = ax.legend(prop={'size': 7})
    for legobj in leg.legendHandles:
        legobj.set_linewidth(5.0)

    ax.grid(False)

    # find the GMM component with the tallest peak
    comp_index = np.argmax(peak_maxs)

    # find index of GMM component with mean closest to the mean of the overall histogram 
    # comp_index = np.argmin(np.abs(gmm.means_ - np.mean(data)))
    
    comp_mean = gmm.means_[comp_index, 0]
    comp_std = np.sqrt(gmm.covariances_[comp_index, 0, 0])
    comp_lower_percentile = norm.ppf(0.005, comp_mean, comp_std)
    comp_upper_percentile = norm.ppf(0.995, comp_mean, comp_std)

    return comp_lower_percentile, comp_upper_percentile
//  [python]
def compute_gmm(data, x_min, x_max, ax):
    
    # select number components based on Bayesian Information Criterion (BIC)
    
    best_bic = np.inf  # initializing with a high value for minimization
    best_n_components = -1

    for n_components in range(1, 3 + 1):
        gmm = GaussianMixture(n_components=n_components, random_state=0)
        gmm.fit(data)
        bic = gmm.bic(data)  # calculate BIC

        if bic < best_bic:
            best_bic = bic
            best_n_components = n_components
    
    # fit a Gaussian mixture model to histogram data using best_n_components
    gmm = GaussianMixture(n_components=best_n_components, random_state=0)
    gmm.fit(data)

    # generate points (along the DNA intensity histogram X range) for plotting the GMM 
    x = np.linspace(x_min, x_max, 100)
    log_prob = gmm.score_samples(x.reshape(-1, 1))
    pdf = np.exp(log_prob)

    # plot overall GMM fit
    # ax.plot(x, pdf, c='k', lw=3, alpha=0.75, label='GMM fit')

    # plot individual GMM components
    peak_maxs = []
    for i in range(gmm.n_components):
        pdf = (
            (gmm.weights_[i] * 
             norm.pdf(x, gmm.means_[i, 0], np.sqrt(gmm.covariances_[i, 0, 0])))
        )
        ax.plot(x, pdf, '--', lw=3, alpha=0.75, label=f'GMM {i+1}')
        peak_maxs.append(pdf.max())
    
    ax.set_ylim(0, None)  # ensuring GMM y-axis starts at 0
    
    leg = ax.legend(prop={'size': 7})
    for legobj in leg.legendHandles:
        legobj.set_linewidth(5.0)

    ax.grid(False)

    # find the GMM component with the tallest peak
    comp_index = np.argmax(peak_maxs)

    # find index of GMM component with mean closest to the mean of the overall histogram 
    # comp_index = np.argmin(np.abs(gmm.means_ - np.mean(data)))
    
    comp_mean = gmm.means_[comp_index, 0]
    comp_std = np.sqrt(gmm.covariances_[comp_index, 0, 0])
    comp_lower_percentile = norm.ppf(0.005, comp_mean, comp_std)
    comp_upper_percentile = norm.ppf(0.995, comp_mean, comp_std)

    return comp_lower_percentile, comp_upper_percentile
//  [python]
def __init__(self, covariance_type, **kwargs):
        
        '''Does Incremental Task mapping by GMM clustering + Dict mapper'''


        self.nC = [0]
        self.w = None
        self.w_std = None ## store the inverse, for prediction. Cholensky cov 
        self.mixtures = None
        self.covariance_type = covariance_type
        
        self.LLClustering = mixture.GaussianMixture(n_components=1,covariance_type=self.covariance_type, **kwargs)
//  [python]
def __init__(self, covariance_type, **kwargs):
        
        '''Does Incremental Task mapping by GMM clustering + Dict mapper'''


        self.nC = [0]
        self.w = None
        self.w_std = None ## store the inverse, for prediction. Cholensky cov 
        self.mixtures = None
        self.covariance_type = covariance_type
        
        self.LLClustering = GaussianMixture(n_components=1,covariance_type=self.covariance_type, **kwargs)
//  [python]
def getGausianMixture(self, ncomp, cvType, ninit, *dsl):
		"""
		finds gaussian mixture parameters
		
		Parameters
			ncomp : num of gaussian componenets
			cvType : co variance type
			ninit: num of intializations
			dsl: list of data set name or list or numpy array
		"""
		self.__printBanner("getting gaussian mixture parameters", *dsl)
		assertInList(cvType, ["full", "tied", "diag", "spherical"], "invalid covariance type")
		dmat = self.__stackData(*dsl)
		
		gm = mixture.GaussianMixture(n_components=ncomp,  covariance_type=cvType, n_init=ninit)
		gm.fit(dmat)
		weights = gm.weights_
		means = gm.means_
		covars = gm.covariances_
		converged = gm.converged_
		niter = gm.n_iter_
		aic = gm.aic(dmat)
		result = self.__printResult("weights", weights, "mean", means, "covariance", covars, "converged", converged, "num iterations", niter, "aic", aic)
		return result
//  [python]
def getGausianMixture(self, ncomp, cvType, ninit, *dsl):
		"""
		finds gaussian mixture parameters
		
		Parameters
			ncomp : num of gaussian componenets
			cvType : co variance type
			ninit: num of intializations
			dsl: list of data set name or list or numpy array
		"""
		self.__printBanner("getting gaussian mixture parameters", *dsl)
		assertInList(cvType, ["full", "tied", "diag", "spherical"], "invalid covariance type")
		dmat = self.__stackData(*dsl)
		
		gm = GaussianMixture(n_components=ncomp,  covariance_type=cvType, n_init=ninit)
		gm.fit(dmat)
		weights = gm.weights_
		means = gm.means_
		covars = gm.covariances_
		converged = gm.converged_
		niter = gm.n_iter_
		aic = gm.aic(dmat)
		result = self.__printResult("weights", weights, "mean", means, "covariance", covars, "converged", converged, "num iterations", niter, "aic", aic)
		return result
//  [python]
def fit_GMM(self, data, n_components, means_init=None):
        print('=' * 40)
        print('Fit GMM in train data for test...')
        np.random.seed(self.args.seed)
        gmm = mixture.GaussianMixture(n_components=n_components, covariance_type='full',
                              means_init=means_init, reg_covar=1e-3, verbose=2)
        gmm.fit(data)
        print('Finish GMM fit.')
        print('=' * 40)
        return gmm
//  [python]
def fit_GMM(self, data, n_components, means_init=None):
        print('=' * 40)
        print('Fit GMM in train data for test...')
        np.random.seed(self.args.seed)
        gmm = GaussianMixture(n_components=n_components, covariance_type='full',
                              means_init=means_init, reg_covar=1e-3, verbose=2)
        gmm.fit(data)
        print('Finish GMM fit.')
        print('=' * 40)
        return gmm
//  [python]
def fit_task(self, features_pertask, n_cluster, **kwargs):
                
        GMM_clustering = mixture.GaussianMixture(n_components=n_cluster,covariance_type=self.covariance_type, **kwargs)
        
        GMM_clustering.fit(features_pertask)
                
        prototypes = GMM_clustering.means_ 
        covariances = GMM_clustering.precisions_cholesky_
        counts = (GMM_clustering.weights_)*features_pertask.shape[0]
        
        bic_scores = GMM_clustering.bic(features_pertask)
        
        return prototypes, covariances, counts, bic_scores
//  [python]
def fit_task(self, features_pertask, n_cluster, **kwargs):
                
        GMM_clustering = GaussianMixture(n_components=n_cluster,covariance_type=self.covariance_type, **kwargs)
        
        GMM_clustering.fit(features_pertask)
                
        prototypes = GMM_clustering.means_ 
        covariances = GMM_clustering.precisions_cholesky_
        counts = (GMM_clustering.weights_)*features_pertask.shape[0]
        
        bic_scores = GMM_clustering.bic(features_pertask)
        
        return prototypes, covariances, counts, bic_scores
//  [python]
def create_classifier(self):
        classifier = mixture.GaussianMixture(n_components=2)
        return classifier
//  [python]
def create_classifier(self):
        classifier = GaussianMixture(n_components=2)
        return classifier
//  [python]
def fit(self, ts):
        self.model = mixture.GaussianMixture(n_components=self.n_clusters, max_iter=200, n_init=self.n_init).fit(ts)

        self.param_dict = {'centroids': self.model.means_,
                           'covariances': self.model.covariances_,
                           'n_centroids': self.n_clusters,
                           'frame_generator': True}
        self.is_fitted = True
//  [python]
def fit(self, ts):
        self.model = GaussianMixture(n_components=self.n_clusters, max_iter=200, n_init=self.n_init).fit(ts)

        self.param_dict = {'centroids': self.model.means_,
                           'covariances': self.model.covariances_,
                           'n_centroids': self.n_clusters,
                           'frame_generator': True}
        self.is_fitted = True
//  [python]
def get_Gussanmixure_weight(clip_model, template, classnames, context_number):
    torch.manual_seed(10)
    context_weights = get_context_weights(clip_model, template, classnames)
    device = context_weights.device
    # from sklearn.cluster import KMeans
    from sklearn.mixture import GMM
    gmm = GMM(n_components=5, random_state=0).fit(context_weights.cpu().numpy())
    context_weights = gmm.sample(context_number)[0]
    context_weights = torch.from_numpy(context_weights).to(device)
    # context_weights = torch.from_numpy(kmeans.cluster_centers_).to(device)
    return context_weights
//  [python]
def get_Gussanmixure_weight(clip_model, template, classnames, context_number):
    torch.manual_seed(10)
    context_weights = get_context_weights(clip_model, template, classnames)
    device = context_weights.device
    # from sklearn.cluster import KMeans
    from sklearn.mixture import GaussianMixture
    gmm = GaussianMixture(n_components=5, random_state=0).fit(context_weights.cpu().numpy())
    context_weights = gmm.sample(context_number)[0]
    context_weights = torch.from_numpy(context_weights).to(device)
    # context_weights = torch.from_numpy(kmeans.cluster_centers_).to(device)
    return context_weights
//  [python]
lowest_bic = np.infty
bic = []
n_components_range = range(1, 7)
cv_types = ['spherical', 'tied', 'diag', 'full']
for cv_type in cv_types:
    for n_components in n_components_range:
        gmm = mixture.GaussianMixture(n_components=n_components,covariance_type=cv_type)
        gmm.fit(x_all)
        bic.append(gmm.aic(x_all))
        if bic[-1] < lowest_bic:
            lowest_bic = bic[-1]
            best_gmm = gmm
//  [python]
lowest_bic = np.infty
bic = []
n_components_range = range(1, 7)
cv_types = ['spherical', 'tied', 'diag', 'full']
for cv_type in cv_types:
    for n_components in n_components_range:
        gmm = GaussianMixture(n_components=n_components,covariance_type=cv_type)
        gmm.fit(x_all)
        bic.append(gmm.aic(x_all))
        if bic[-1] < lowest_bic:
            lowest_bic = bic[-1]
            best_gmm = gmm
//  [python]
NAMESPACE = 'hsc'

data_names = [
    'data/hsc/hsc_mars',
    'data/hsc/hsc_ss2',
]

# Computes the probability that the corrected SS2 dataset
# comes from the original SS2 distribution or from the same
# distribution as the corrected MARS-Seq dataset.
if __name__ == '__main__':
    # Load data.
    datasets, genes_list, n_cells = load_names(data_names, verbose=False)
    datasets, genes = merge_datasets(datasets, genes_list, verbose=False)
    datasets, genes = process_data(datasets, genes)
    datasets = [ normalize(ds, axis=1) for ds in datasets ]
    
    # Fit initial mixture models.
    gm_ss2 = (sklearn.mixture.GMM(n_components=3, n_init=3)
              .fit(datasets[1]))

    # Do batch correction.
    datasets = assemble(
        datasets,
        verbose=False, knn=KNN, sigma=SIGMA, approx=APPROX
    )
    datasets = [ normalize(ds, axis=1) for ds in datasets ]
    
    # Fit mixture models to other dataset.
    gm_mars_corrected = (
        sklearn.mixture.GMM(n_components=3, n_init=3)
        .fit(datasets[0])
    )

    # Natural log likelihoods.
    ll_ss2 = gm_ss2.score(datasets[1])
    ll_mars_corrected = gm_mars_corrected.score(datasets[1])

    # Natural log of the likelihood ratio.
    print(ll_ss2 - max(ll_ss2, ll_mars_corrected))
//  [python]
NAMESPACE = 'hsc'

data_names = [
    'data/hsc/hsc_mars',
    'data/hsc/hsc_ss2',
]

# Computes the probability that the corrected SS2 dataset
# comes from the original SS2 distribution or from the same
# distribution as the corrected MARS-Seq dataset.
if __name__ == '__main__':
    # Load data.
    datasets, genes_list, n_cells = load_names(data_names, verbose=False)
    datasets, genes = merge_datasets(datasets, genes_list, verbose=False)
    datasets, genes = process_data(datasets, genes)
    datasets = [ normalize(ds, axis=1) for ds in datasets ]
    
    # Fit initial mixture models.
    gm_ss2 = (GaussianMixture(n_components=3, n_init=3)
              .fit(datasets[1]))

    # Do batch correction.
    datasets = assemble(
        datasets,
        verbose=False, knn=KNN, sigma=SIGMA, approx=APPROX
    )
    datasets = [ normalize(ds, axis=1) for ds in datasets ]
    
    # Fit mixture models to other dataset.
    gm_mars_corrected = (
        GaussianMixture(n_components=3, n_init=3)
        .fit(datasets[0])
    )

    # Natural log likelihoods.
    ll_ss2 = gm_ss2.score(datasets[1])
    ll_mars_corrected = gm_mars_corrected.score(datasets[1])

    # Natural log of the likelihood ratio.
    print(ll_ss2 - max(ll_ss2, ll_mars_corrected))
//  [python]
def train_gmm(x):
    print(type(x),x)
    global df
    for i in range(1,8):
        array = df.loc[df['Label']==x].iloc[:,:-1]
        gmm = mixture.GaussianMixture(n_components=2**i,covariance_type='diag').fit(array)
        file = ''.join(['D:\Mtech\Sem4\ASR\Project\Model\\',str(2**i),'M_GMM\\',x,'_', str(2**i) ,'.pkl'])
        print(file)
        with open(file, 'wb') as f:
            pickle.dump(gmm, f)
//  [python]
def train_gmm(x):
    print(type(x),x)
    global df
    for i in range(1,8):
        array = df.loc[df['Label']==x].iloc[:,:-1]
        gmm = GaussianMixture(n_components=2**i,covariance_type='diag').fit(array)
        file = ''.join(['D:\Mtech\Sem4\ASR\Project\Model\\',str(2**i),'M_GMM\\',x,'_', str(2**i) ,'.pkl'])
        print(file)
        with open(file, 'wb') as f:
            pickle.dump(gmm, f)
//  [python]
def CBIG_pMFM_fitmodel_simulated():
    '''
        This function is to implement the FCD distribution fit
        for simulated data
        Returns:        None
        '''

    FCD_data_raw = sio.loadmat(
        '../output/step5_STDFCD_results/STD_FCD_simulated_rundata.mat')
    FCD_mean_all = FCD_data_raw['FCD_sim_allrun']
    file_num = FCD_mean_all.shape[0]

    output_path = '../output/step6_SWSTD_state/'
    if not os.path.isdir(output_path):
        os.makedirs(output_path)

    bic_all = np.zeros((file_num, 3))
    mid_p = np.zeros((file_num, 1))

    for i in range(file_num):
        FCD_mean = np.expand_dims(FCD_mean_all[i, :], 1)

        gm1 = mixture.GaussianMixture(n_components=1, n_init=1)
        gm1.fit(FCD_mean)

        gm2 = mixture.GaussianMixture(n_components=2, n_init=10)
        gm2.fit(FCD_mean)

        bic1 = gm1.bic(FCD_mean)
        bic2 = gm2.bic(FCD_mean)

        bic_all[i, 0] = bic1
        bic_all[i, 1] = bic2
        bic_all[i, 2] = np.argmin(bic_all[i, 0:2])

        if bic_all[i, 2] == 1:
            mu1 = gm2.means_[0]
            sigma1 = gm2.covariances_[0, 0, 0]
            mu2 = gm2.means_[1]
            sigma2 = gm2.covariances_[1, 0, 0]
            delta = gm2.weights_[0]

            a = 1 / sigma1**2 - 1 / sigma2**2
            b = 2 * (mu2 / sigma2**2 - mu1 / sigma1**2)
            c = mu1**2 / sigma1**2 - mu2**2 / sigma2**2 - 2 * np.log(
                delta / (1 - delta) * sigma2 / sigma1)

            mid_p1 = (-b + np.sqrt(b**2 - 4 * a * c)) / 2 / a
            mid_p2 = (-b - np.sqrt(b**2 - 4 * a * c)) / 2 / a
            if mu1 < mid_p1 < mu2 or mu1 > mid_p1 > mu2:
                mid_p[i, 0] = mid_p1
            else:
                mid_p[i, 0] = mid_p2

        print(i)

    threshold = dict()
    threshold['th_all_sim'] = mid_p
    threshold['bic_all_sim'] = bic_all
    sio.savemat(output_path + 'FCD_threshold_simulated.mat', threshold)
//  [python]
def CBIG_pMFM_fitmodel_simulated():
    '''
        This function is to implement the FCD distribution fit
        for simulated data
        Returns:        None
        '''

    FCD_data_raw = sio.loadmat(
        '../output/step5_STDFCD_results/STD_FCD_simulated_rundata.mat')
    FCD_mean_all = FCD_data_raw['FCD_sim_allrun']
    file_num = FCD_mean_all.shape[0]

    output_path = '../output/step6_SWSTD_state/'
    if not os.path.isdir(output_path):
        os.makedirs(output_path)

    bic_all = np.zeros((file_num, 3))
    mid_p = np.zeros((file_num, 1))

    for i in range(file_num):
        FCD_mean = np.expand_dims(FCD_mean_all[i, :], 1)

        gm1 = GaussianMixture(n_components=1, n_init=1)
        gm1.fit(FCD_mean)

        gm2 = GaussianMixture(n_components=2, n_init=10)
        gm2.fit(FCD_mean)

        bic1 = gm1.bic(FCD_mean)
        bic2 = gm2.bic(FCD_mean)

        bic_all[i, 0] = bic1
        bic_all[i, 1] = bic2
        bic_all[i, 2] = np.argmin(bic_all[i, 0:2])

        if bic_all[i, 2] == 1:
            mu1 = gm2.means_[0]
            sigma1 = gm2.covariances_[0, 0, 0]
            mu2 = gm2.means_[1]
            sigma2 = gm2.covariances_[1, 0, 0]
            delta = gm2.weights_[0]

            a = 1 / sigma1**2 - 1 / sigma2**2
            b = 2 * (mu2 / sigma2**2 - mu1 / sigma1**2)
            c = mu1**2 / sigma1**2 - mu2**2 / sigma2**2 - 2 * np.log(
                delta / (1 - delta) * sigma2 / sigma1)

            mid_p1 = (-b + np.sqrt(b**2 - 4 * a * c)) / 2 / a
            mid_p2 = (-b - np.sqrt(b**2 - 4 * a * c)) / 2 / a
            if mu1 < mid_p1 < mu2 or mu1 > mid_p1 > mu2:
                mid_p[i, 0] = mid_p1
            else:
                mid_p[i, 0] = mid_p2

        print(i)

    threshold = dict()
    threshold['th_all_sim'] = mid_p
    threshold['bic_all_sim'] = bic_all
    sio.savemat(output_path + 'FCD_threshold_simulated.mat', threshold)