jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def initialize_GaussianKernel(points, N,covariance_type='diag',reg_covar=1e-1,n_init=10):
    GMM = GaussianMixture(n_components=N, covariance_type=covariance_type, verbose=1,
                          reg_covar=reg_covar,n_init=n_init)
    GMM.fit(points)
    centers = GMM.means_
    covariances = GMM.covariances_
    probas = GMM.weights_
    order = np.argsort(probas)[::-1]
    centers = centers[order]
    covariances = covariances[order]
    probas = probas[order]
    if covariance_type == 'diag':
        widths = np.sqrt(covariances)
    elif covariance_type == 'full':
        sqrt_precision_matrix = np.array(
            [inv_root_matrix(covariance) for covariance in covariances])
    if covariance_type == 'diag':
        return centers.T, widths.T
    elif covariance_type == 'full':
        return centers.T, sqrt_precision_matrix.T
//  [python]
def __init__(self):
        self.gm = mixture.GaussianMixture()
        self.trained = False
//  [python]
def __init__(self):
        self.gm = GaussianMixture()
        self.trained = False
//  [python]
def calc_gmm(self, dim=10, calc_times=100, force=False):
        from sklearn.mixture import GaussianMixture
        logger.info("calculating gmm centers")
        X = self.mus
        gmm_path = self.path_to_save_results/"gmm.pkl"
        if gmm_path.exists():
            logger.info(f"loading {gmm_path}")
            with gmm_path.open("rb") as f:
                best_gmm = pickle.load(f)
                best_aic = best_gmm.aic(X)
        else:
            best_aic = np.inf
            pbar = tqdm(range(calc_times))
            for i in pbar:
                gmm = GaussianMixture(dim, covariance_type="full").fit(X)
                if gmm.aic(X) < best_aic:
                    best_aic = gmm.aic(X)
                    best_gmm = gmm
                pbar.set_description(
                    "[" + "⠸⠴⠦⠇⠋⠙"[i % 6] + "]" + f"{best_aic:.2f}")

            with gmm_path.open("wb") as f:
                pickle.dump(best_gmm, f)

        logger.info(f"best aic : {best_aic}")
        self.gmm = best_gmm
        self.aic = best_aic
        self.gmm_classes = best_gmm.predict(X)
        self.gmm_centers = best_gmm.means_
//  [python]
def _make_gmm(self, ngauss):
        """
        Make a GMM object for sampling
        """
        from sklearn.mixture import GMM

        gmm = GMM(
            n_components=ngauss,
            max_iter=10000,
            reg_covar=1.0e-12,
            covariance_type="full",
            random_state=self.rng,
        )

        return gmm
//  [python]
def _make_gmm(self, ngauss):
        """
        Make a GMM object for sampling
        """
        from sklearn.mixture import GaussianMixture

        gmm = GaussianMixture(
            n_components=ngauss,
            max_iter=10000,
            reg_covar=1.0e-12,
            covariance_type="full",
            random_state=self.rng,
        )

        return gmm
//  [python]
def fit(
        self, data, ngauss, n_iter=5000, min_covar=1.0e-6, doplot=False, **keys
    ):
        """
        data is shape
            [npoints, ndim]
        """
        from sklearn.mixture import GMM

        if len(data.shape) == 1:
            data = data[:, numpy.newaxis]

        print("ngauss:   ", ngauss)
        print("n_iter:   ", n_iter)
        print("min_covar:", min_covar)

        gmm = GMM(
            n_components=ngauss,
            n_iter=n_iter,
            min_covar=min_covar,
            covariance_type="full",
            random_state=self.rng,
        )

        gmm.fit(data)

        if not gmm.converged_:
            print("DID NOT CONVERGE")

        self._gmm = gmm
        self.set_mixture(gmm.weights_, gmm.means_, gmm.covariances_)

        if doplot:
            plt = self.plot(data=data, **keys)
            return plt
//  [python]
def fit(
        self, data, ngauss, n_iter=5000, min_covar=1.0e-6, doplot=False, **keys
    ):
        """
        data is shape
            [npoints, ndim]
        """
        from sklearn.mixture import GaussianMixture

        if len(data.shape) == 1:
            data = data[:, numpy.newaxis]

        print("ngauss:   ", ngauss)
        print("n_iter:   ", n_iter)
        print("min_covar:", min_covar)

        gmm = GaussianMixture(
            n_components=ngauss,
            max_iter=n_iter,
            reg_covar=min_covar,
            covariance_type="full",
            random_state=self.rng,
        )

        gmm.fit(data)

        if not gmm.converged_:
            print("DID NOT CONVERGE")

        self._gmm = gmm
        self.set_mixture(gmm.weights_, gmm.means_, gmm.covariances_)

        if doplot:
            plt = self.plot(data=data, **keys)
            return plt
//  [python]
def __clustering(self, X, y=None):
        """
        The clustering procedure of the Optimal Weighted Clustering Gaussian 
        Process. This function should not be called externally
        """

        self.sizeX = len(X)
        
        if self.cluster_method == 'k-mean':
            clusterer = KMeans(n_clusters=self.n_cluster)
            clusterer.fit(X)
            self.cluster_label = clusterer.labels_
            self.clusterer = clusterer
        elif self.cluster_method == 'tree':
            
            # if (self.min_leaf_size > 0):
            #     self.minsamples = self.min_leaf_size
            #     tree = IncrementalRegressionTree(min_samples_leaf=self.min_leaf_size)
            # else:
            #     self.minsamples = int(len(X)/(self.n_cluster))
            #     tree = IncrementalRegressionTree(min_samples_leaf=self.minsamples)
            
            # tree.fit(X,y)
            # labels = tree.apply(X)
            # clusters = np.unique(labels)
            # k = len(clusters)
            # if self.verbose:
            #     print(("leafs:",k))
            # self.n_cluster = k
            # self.leaf_labels = np.unique(labels)
            # self.cluster_label = labels
            # self.clusterer = tree
            pass
            
        elif self.cluster_method == 'random':
            r = self.n_sample % self.n_cluster
            m = (self.n_sample - r) / self.n_cluster
            self.cluster_label = array(range(self.n_cluster) * m + range(r))
            self.clusterer = None
            shuffle(self.cluster_label)
        elif self.cluster_method == 'GMM':    #GMM from sklearn
            self.clusterer = mixture.GaussianMixture(n_components=self.n_cluster, n_init=10)
            self.clusterer.fit(X)
            self.cluster_labels_proba = self.clusterer.predict_proba(X)
            self.cluster_label = self.clusterer.predict(X)
        elif self.cluster_method == 'fuzzy-c-mean': #Fuzzy C-means from sklearn
            cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(X.T, self.n_cluster, 2, error=0.000005, maxiter=10000, init=None)
            self.clusterer = cntr #save the centers for cmeans_predict
            self.cluster_labels_proba = u.T
            self.cluster_labels_proba = np.array(self.cluster_labels_proba)
            self.cluster_label = np.argmax(u, axis=0)
            self.cluster_label = np.array(self.cluster_label)
//  [python]
def __clustering(self, X, y=None):
        """
        The clustering procedure of the Optimal Weighted Clustering Gaussian 
        Process. This function should not be called externally
        """

        self.sizeX = len(X)
        
        if self.cluster_method == 'k-mean':
            clusterer = KMeans(n_clusters=self.n_cluster)
            clusterer.fit(X)
            self.cluster_label = clusterer.labels_
            self.clusterer = clusterer
        elif self.cluster_method == 'tree':
            
            # if (self.min_leaf_size > 0):
            #     self.minsamples = self.min_leaf_size
            #     tree = IncrementalRegressionTree(min_samples_leaf=self.min_leaf_size)
            # else:
            #     self.minsamples = int(len(X)/(self.n_cluster))
            #     tree = IncrementalRegressionTree(min_samples_leaf=self.minsamples)
            
            # tree.fit(X,y)
            # labels = tree.apply(X)
            # clusters = np.unique(labels)
            # k = len(clusters)
            # if self.verbose:
            #     print(("leafs:",k))
            # self.n_cluster = k
            # self.leaf_labels = np.unique(labels)
            # self.cluster_label = labels
            # self.clusterer = tree
            pass
            
        elif self.cluster_method == 'random':
            r = self.n_sample % self.n_cluster
            m = (self.n_sample - r) / self.n_cluster
            self.cluster_label = array(range(self.n_cluster) * m + range(r))
            self.clusterer = None
            shuffle(self.cluster_label)
        elif self.cluster_method == 'GMM':    #GMM from sklearn
            self.clusterer = GaussianMixture(n_components=self.n_cluster, n_init=10)
            self.clusterer.fit(X)
            self.cluster_labels_proba = self.clusterer.predict_proba(X)
            self.cluster_label = self.clusterer.predict(X)
        elif self.cluster_method == 'fuzzy-c-mean': #Fuzzy C-means from sklearn
            cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(X.T, self.n_cluster, 2, error=0.000005, maxiter=10000, init=None)
            self.clusterer = cntr #save the centers for cmeans_predict
            self.cluster_labels_proba = u.T
            self.cluster_labels_proba = np.array(self.cluster_labels_proba)
            self.cluster_label = np.argmax(u, axis=0)
            self.cluster_label = np.array(self.cluster_label)
//  [python]
def eval_train(epoch, model, eval_loader, criterion, num_batches, batch_size, stats_log):
    model.eval()
    num_samples = num_batches * batch_size + 37497  # add for intersection
    losses = torch.zeros(num_samples)
    paths = []
    n = 0
    with torch.no_grad():
        for batch_idx, (inputs, _, targets, clean_target, path, _) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs)
            loss = criterion(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_noisy = losses[:num_batches * batch_size]
    losses = (losses - losses_noisy.min()) / (losses_noisy.max() - losses_noisy.min())
    losses_noisy = losses[:num_batches * batch_size]
    losses, losses_noisy = losses.reshape(-1, 1), losses_noisy.reshape(-1, 1)
    gmm = mixture.GaussianMixture(n_components=2, max_iter=100, reg_covar=5e-4, tol=1e-2)
    gmm.fit(losses_noisy)

    clean_idx, noisy_idx = gmm.means_.argmin(), gmm.means_.argmax()
    stats_log.write('GMM results: {} with variance {} and weight {}\t'
                    '{} with variance {} and weight {}\n'.format(gmm.means_[clean_idx], gmm.covariances_[clean_idx],
                                                                 gmm.weights_[clean_idx],
                                                                 gmm.means_[noisy_idx], gmm.covariances_[noisy_idx],
                                                                 gmm.weights_[noisy_idx], ))
    stats_log.flush()

    prob = gmm.predict_proba(losses)
    prob = prob[:, clean_idx]
    return prob, paths
//  [python]
def eval_train(epoch, model, eval_loader, criterion, num_batches, batch_size, stats_log):
    model.eval()
    num_samples = num_batches * batch_size + 37497  # add for intersection
    losses = torch.zeros(num_samples)
    paths = []
    n = 0
    with torch.no_grad():
        for batch_idx, (inputs, _, targets, clean_target, path, _) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs)
            loss = criterion(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_noisy = losses[:num_batches * batch_size]
    losses = (losses - losses_noisy.min()) / (losses_noisy.max() - losses_noisy.min())
    losses_noisy = losses[:num_batches * batch_size]
    losses, losses_noisy = losses.reshape(-1, 1), losses_noisy.reshape(-1, 1)
    gmm = GaussianMixture(n_components=2, max_iter=100, reg_covar=5e-4, tol=1e-2)
    gmm.fit(losses_noisy)

    clean_idx, noisy_idx = gmm.means_.argmin(), gmm.means_.argmax()
    stats_log.write('GMM results: {} with variance {} and weight {}\t'
                    '{} with variance {} and weight {}\n'.format(gmm.means_[clean_idx], gmm.covariances_[clean_idx],
                                                                 gmm.weights_[clean_idx],
                                                                 gmm.means_[noisy_idx], gmm.covariances_[noisy_idx],
                                                                 gmm.weights_[noisy_idx], ))
    stats_log.flush()

    prob = gmm.predict_proba(losses)
    prob = prob[:, clean_idx]
    return prob, paths
//  [python]
def build_network(self):
        self._gmm = mixture.GaussianMixture(
            n_components=self.args.gmm_num_cluster, covariance_type='full',
            max_iter=self.args.gmm_max_iteration, random_state=self.args.seed,
            warm_start=True
        )
        self._gmm_weights = {'mean': None, 'cov': None}
        self._gmm_vec_size = self._observation_size * 2 + self._action_size

        # the Normal-inverse-Wishart prior coefficient. Note that this number
        # is used in the repo, but in the paper "End-to-End Training of Deep
        # Visuomotor Policies", it actuall says @m and @n0 should both be 0
        self._NIW_prior = {
            'm': self.args.gmm_prior_strength,
            'n0': (self.args.gmm_batch_size - 2.0 - self._gmm_vec_size) /
            self.args.gmm_batch_size * self.args.gmm_prior_strength
        }
//  [python]
def build_network(self):
        self._gmm = GaussianMixture(
            n_components=self.args.gmm_num_cluster, covariance_type='full',
            max_iter=self.args.gmm_max_iteration, random_state=self.args.seed,
            warm_start=True
        )
        self._gmm_weights = {'mean': None, 'cov': None}
        self._gmm_vec_size = self._observation_size * 2 + self._action_size

        # the Normal-inverse-Wishart prior coefficient. Note that this number
        # is used in the repo, but in the paper "End-to-End Training of Deep
        # Visuomotor Policies", it actuall says @m and @n0 should both be 0
        self._NIW_prior = {
            'm': self.args.gmm_prior_strength,
            'n0': (self.args.gmm_batch_size - 2.0 - self._gmm_vec_size) /
            self.args.gmm_batch_size * self.args.gmm_prior_strength
        }
//  [python]
def _load_gmm(cluster):
    """Load a trained GMM for the decision tree.

    Parameters
    ----------
    cluster : int or str
        The cluster whose GMM should be loaded

    Returns
    -------
    tuple of sklearn.mixtures.GaussianMixture, list
       The trained GMM instance and a list mapping the model components to the corresponding classes.
    """

    props = gmm.GMM[cluster]

    gmm_ = mixture.GaussianMixture(
        n_components=props["n_components"],
        covariance_type=props["covariance_type"],
        tol=props["tol"],
        reg_covar=props["reg_covar"],
        max_iter=props["max_iter"],
        n_init=props["n_init"],
        init_params=props["init_params"],
        weights_init=props["weights_init"],
        means_init=props["means_init"],
        precisions_init=props["precisions_init"],
        random_state=props["random_state"],
        warm_start=props["warm_start"],
        verbose=props["verbose"],
        verbose_interval=props["verbose_interval"],
    )
    gmm_.weights_ = props["weights_"]
    gmm_.means_ = props["means_"]
    gmm_.covariances_ = props["covariances_"]
    gmm_.precisions_ = props["precisions_"]
    gmm_.precisions_cholesky_ = props["precisions_cholesky_"]
    gmm_.n_iter_ = props["n_iter_"]
    gmm_.lower_bound_ = props["lower_bound_"]

    classes = gmm.GMM[cluster]["classes"]

    return gmm_, classes
//  [python]
def _load_gmm(cluster):
    """Load a trained GMM for the decision tree.

    Parameters
    ----------
    cluster : int or str
        The cluster whose GMM should be loaded

    Returns
    -------
    tuple of sklearn.mixtures.GaussianMixture, list
       The trained GMM instance and a list mapping the model components to the corresponding classes.
    """

    props = gmm.GMM[cluster]

    gmm_ = GaussianMixture(
        n_components=props["n_components"],
        covariance_type=props["covariance_type"],
        tol=props["tol"],
        reg_covar=props["reg_covar"],
        max_iter=props["max_iter"],
        n_init=props["n_init"],
        init_params=props["init_params"],
        weights_init=props["weights_init"],
        means_init=props["means_init"],
        precisions_init=props["precisions_init"],
        random_state=props["random_state"],
        warm_start=props["warm_start"],
        verbose=props["verbose"],
        verbose_interval=props["verbose_interval"],
    )
    gmm_.weights_ = props["weights_"]
    gmm_.means_ = props["means_"]
    gmm_.covariances_ = props["covariances_"]
    gmm_.precisions_ = props["precisions_"]
    gmm_.precisions_cholesky_ = props["precisions_cholesky_"]
    gmm_.n_iter_ = props["n_iter_"]
    gmm_.lower_bound_ = props["lower_bound_"]

    classes = gmm.GMM[cluster]["classes"]

    return gmm_, classes
//  [python]
def my_GMM(x):
    n_clusters = range(1, 201)
    scores = []
    for n in n_clusters:
        my_gmm = mixture.GaussianMixture(n_components = n).fit(x)
        scores.append(my_gmm.score(x))
    scores = np.array(scores)
    return np.argmax(scores) + 1
//  [python]
def my_GMM(x):
    n_clusters = range(1, 201)
    scores = []
    for n in n_clusters:
        my_gmm = GaussianMixture(n_components = n).fit(x)
        scores.append(my_gmm.score(x))
    scores = np.array(scores)
    return np.argmax(scores) + 1
//  [python]
def __get_model(self, k: int):
        if self.algorithm == 'gmm':
            return mixture.GaussianMixture(n_components=k, random_state=self.random_state)
        return KMeans(n_clusters=k, random_state=self.random_state)
//  [python]
def __get_model(self, k: int):
        if self.algorithm == 'gmm':
            return GaussianMixture(n_components=k, random_state=self.random_state)
        return KMeans(n_clusters=k, random_state=self.random_state)
//  [python]
def _signal_binarize_mixture(signal, threshold="auto"):
    if threshold == "auto":
        threshold = 0.5

    # fit a Gaussian Mixture Model with two components
    clf = sklearn.mixture.GMM(n_components=2, random_state=333)
    clf = clf.fit(signal.reshape(-1, 1))

    # Get predicted probabilities
    probability = clf.predict_proba(signal.reshape(-1, 1))[
        :, np.argmax(clf.means_)
    ]

    binary = np.zeros(len(signal))
    binary[probability >= threshold] = 1
    return binary
//  [python]
def _signal_binarize_mixture(signal, threshold="auto"):
    if threshold == "auto":
        threshold = 0.5

    # fit a Gaussian Mixture Model with two components
    clf = sklearn.mixture.GaussianMixture(n_components=2, random_state=333)
    clf = clf.fit(signal.reshape(-1, 1))

    # Get predicted probabilities
    probability = clf.predict_proba(signal.reshape(-1, 1))[
        :, np.argmax(clf.means_[:, 0])
    ]

    binary = np.zeros(len(signal))
    binary[probability >= threshold] = 1
    return binary
//  [python]
def fit(self, X):
    assert X.ndim == 2, "Only support input matrix but given: %s" % str(X.shape)
    n_classes = X.shape[1]
    it = tqdm(list(range(n_classes))) if self.verbose else range(n_classes)
    for i in it:
      # ====== normalizing ====== #
      x_train = self.normalize(X[:, i], test_mode=False)
      # ====== GMM ====== #
      try:
        gmm = mixture.GaussianMixture(n_components=self.n_components_per_class,
                              covariance_type='diag',
                              init_params='kmeans',
                              n_init=8,
                              max_iter=120,
                              random_state=self.random_state)
        gmm.fit(x_train[:, np.newaxis])
      except ValueError as e:
        if "ill-defined empirical covariance" in str(e):
          gmm = _DummyGMM()
          gmm.fit(x_train[:, np.newaxis])
        else:
          import traceback
          traceback.print_exc()
          raise e
      # ====== save GMM ====== #
      means_ = gmm.means_.ravel()
      order = np.argsort(means_)
      self._models.append((order, gmm))
//  [python]
def fit(self, X):
    assert X.ndim == 2, "Only support input matrix but given: %s" % str(X.shape)
    n_classes = X.shape[1]
    it = tqdm(list(range(n_classes))) if self.verbose else range(n_classes)
    for i in it:
      # ====== normalizing ====== #
      x_train = self.normalize(X[:, i], test_mode=False)
      # ====== GMM ====== #
      try:
        gmm = GaussianMixture(n_components=self.n_components_per_class,
                              covariance_type='diag',
                              init_params='kmeans',
                              n_init=8,
                              max_iter=120,
                              random_state=self.random_state)
        gmm.fit(x_train[:, np.newaxis])
      except ValueError as e:
        if "ill-defined empirical covariance" in str(e):
          gmm = _DummyGMM()
          gmm.fit(x_train[:, np.newaxis])
        else:
          import traceback
          traceback.print_exc()
          raise e
      # ====== save GMM ====== #
      means_ = gmm.means_.ravel()
      order = np.argsort(means_)
      self._models.append((order, gmm))
//  [python]
def get_diagonal_covariances(data):
    # data: num_points x feat_dim
    model = mixture.GaussianMixture(n_components=1, covariance_type='diag')
    responsibilities = np.ones((data.shape[0], 1))
    model._initialize(data, responsibilities)
    return model.covariances_, model.precisions_cholesky_
//  [python]
def get_diagonal_covariances(data):
    # data: num_points x feat_dim
    model = GaussianMixture(n_components=1, covariance_type='diag')
    responsibilities = np.ones((data.shape[0], 1))
    model._initialize(data, responsibilities)
    return model.covariances_, model.precisions_cholesky_
//  [python]
data = load_iris()
X_train, X_test = train_test_split(data.data)
model = mixture.GaussianMixture()
//  [python]
data = load_iris()
X_train, X_test = train_test_split(data.data)
model = GaussianMixture()
//  [python]
def fit_gmm(samples, ncomponents=2):
    """Given a numpy array of floating point samples, fit a gaussian mixture model."""
    # assume samples is of shape (NSAMPLES,); unsqueeze to (NSAMPLES,1) and train a GMM:
    gmm = mixture.GMM(n_components=ncomponents)
    gmm.fit(samples.reshape(-1,1))
    # return params of GMM in [(coeff, mu, sigma)] format:
    params = [(gmm.weights_[c], gmm.means_[c][0], gmm.covars_[c][0][0]) for c in range(ncomponents)]
    return params
//  [python]
def fit_gmm(samples, ncomponents=2):
    """Given a numpy array of floating point samples, fit a gaussian mixture model."""
    # assume samples is of shape (NSAMPLES,); unsqueeze to (NSAMPLES,1) and train a GMM:
    gmm = GaussianMixture(n_components=ncomponents)
    gmm.fit(samples.reshape(-1,1))
    # return params of GMM in [(coeff, mu, sigma)] format:
    params = [(gmm.weights_[c], gmm.means_[c][0], gmm.covariances_[c][0][0]) for c in range(ncomponents)]
    return params
//  [python]
def cluster(
        embs, n_clusters, method='mbkm', location_weight=None,
        sort=True):

    x, mask = prepare_for_clustering(embs, location_weight)

    print(f'Clustering pixels using {method}...')
    t0 = time()
    if method == 'mbkm':
        model = MiniBatchKMeans(
                n_clusters=n_clusters,
                # batch_size=x.shape[0]//10, max_iter=1000,
                # max_no_improvement=100, n_init=10,
                random_state=0, verbose=0)
    elif method == 'km':
        model = KMeans(
                n_clusters=n_clusters,
                random_state=0, verbose=0)
    elif method == 'gm':
        model = mixture.GaussianMixture(
                n_components=n_clusters,
                covariance_type='diag', init_params='k-means++',
                random_state=0, verbose=1)
    # elif method == 'dbscan':
    #     eps = x.var(0).sum()**0.5 * 0.5
    #     min_samples = 5
    #     model = DBSCAN(eps=eps, min_samples=min_samples, n_jobs=64)
    # elif method == 'hdbscan':
    #     min_cluster_size = min(1000, x.shape[0] // 400 + 1)
    #     min_samples = min_cluster_size // 10 + 1
    #     model = HDBSCAN(
    #             min_cluster_size=min_cluster_size,
    #             min_samples=min_samples,
    #             core_dist_n_jobs=64)
    elif method == 'agglomerative':
        # knn_graph = kneighbors_graph(x, n_neighbors=10, include_self=False)
        model = AgglomerativeClustering(
                n_clusters=n_clusters,
                linkage='ward', compute_distances=True)
    else:
        raise ValueError(f'Method `{method}` not recognized')
    print(x.shape)
    labels = model.fit_predict(x)
    print(int(time() - t0), 'sec')
    print('n_clusters:', np.unique(labels).size)

    if sort:
        labels = sort_labels(labels)[0]

    labels_arr = np.full(mask.shape, labels.min()-1, dtype=int)
    labels_arr[mask] = labels

    # if method == 'gm':
    #     probs = model.predict_proba(embs)
    #     probs = probs[:, order]
    #     assert (probs.argmax(-1) == labels).all()
    #     probs_arr = np.full(
    #             mask.shape+(n_clusters,), np.nan, dtype=np.float32)
    #     probs_arr[mask] = probs
    # else:
    #     probs_arr = None

    return labels_arr, model
//  [python]
def cluster(
        embs, n_clusters, method='mbkm', location_weight=None,
        sort=True):

    x, mask = prepare_for_clustering(embs, location_weight)

    print(f'Clustering pixels using {method}...')
    t0 = time()
    if method == 'mbkm':
        model = MiniBatchKMeans(
                n_clusters=n_clusters,
                # batch_size=x.shape[0]//10, max_iter=1000,
                # max_no_improvement=100, n_init=10,
                random_state=0, verbose=0)
    elif method == 'km':
        model = KMeans(
                n_clusters=n_clusters,
                random_state=0, verbose=0)
    elif method == 'gm':
        model = GaussianMixture(
                n_components=n_clusters,
                covariance_type='diag', init_params='k-means++',
                random_state=0, verbose=1)
    # elif method == 'dbscan':
    #     eps = x.var(0).sum()**0.5 * 0.5
    #     min_samples = 5
    #     model = DBSCAN(eps=eps, min_samples=min_samples, n_jobs=64)
    # elif method == 'hdbscan':
    #     min_cluster_size = min(1000, x.shape[0] // 400 + 1)
    #     min_samples = min_cluster_size // 10 + 1
    #     model = HDBSCAN(
    #             min_cluster_size=min_cluster_size,
    #             min_samples=min_samples,
    #             core_dist_n_jobs=64)
    elif method == 'agglomerative':
        # knn_graph = kneighbors_graph(x, n_neighbors=10, include_self=False)
        model = AgglomerativeClustering(
                n_clusters=n_clusters,
                linkage='ward', compute_distances=True)
    else:
        raise ValueError(f'Method `{method}` not recognized')
    print(x.shape)
    labels = model.fit_predict(x)
    print(int(time() - t0), 'sec')
    print('n_clusters:', np.unique(labels).size)

    if sort:
        labels = sort_labels(labels)[0]

    labels_arr = np.full(mask.shape, labels.min()-1, dtype=int)
    labels_arr[mask] = labels

    # if method == 'gm':
    #     probs = model.predict_proba(embs)
    #     probs = probs[:, order]
    #     assert (probs.argmax(-1) == labels).all()
    #     probs_arr = np.full(
    #             mask.shape+(n_clusters,), np.nan, dtype=np.float32)
    #     probs_arr[mask] = probs
    # else:
    #     probs_arr = None

    return labels_arr, model
//  [python]
def fit(self, X, Xerr, R=None):
        """Fit the XD model to data

        Parameters
        ----------
        X: array_like
            Input data. shape = (n_samples, n_features)
        Xerr: array_like
            Error on input data.  shape = (n_samples, n_features, n_features)
        R : array_like
            (TODO: not implemented)
            Transformation matrix from underlying to observed data.  If
            unspecified, then it is assumed to be the identity matrix.
        """
        if R is not None:
            raise NotImplementedError("mixing matrix R is not yet implemented")

        X = np.asarray(X)
        Xerr = np.asarray(Xerr)
        n_samples, n_features = X.shape

        # assume full covariances of data
        assert Xerr.shape == (n_samples, n_features, n_features)

        # initialize components via a few steps of GaussianMixture
        # this doesn't take into account errors, but is a fast first-guess
        gmm = mixture.GMM(self.n_components, max_iter=10,
                              covariance_type='full',
                              random_state=self.random_state).fit(X)
        self.mu = gmm.means_
        self.alpha = gmm.weights_
        self.V = gmm.covars_

        logL = self.logL(X, Xerr)

        for i in range(self.max_iter):
            t0 = time()
            self._EMstep(X, Xerr)
            logL_next = self.logL(X, Xerr)
            t1 = time()

            if self.verbose:
                print("%i: log(L) = %.5g" % (i + 1, logL_next))
                print("    (%.2g sec)" % (t1 - t0))

            if logL_next < logL + self.tol:
                break
            logL = logL_next

        return self
//  [python]
def fit(self, X, Xerr, R=None):
        """Fit the XD model to data

        Parameters
        ----------
        X: array_like
            Input data. shape = (n_samples, n_features)
        Xerr: array_like
            Error on input data.  shape = (n_samples, n_features, n_features)
        R : array_like
            (TODO: not implemented)
            Transformation matrix from underlying to observed data.  If
            unspecified, then it is assumed to be the identity matrix.
        """
        if R is not None:
            raise NotImplementedError("mixing matrix R is not yet implemented")

        X = np.asarray(X)
        Xerr = np.asarray(Xerr)
        n_samples, n_features = X.shape

        # assume full covariances of data
        assert Xerr.shape == (n_samples, n_features, n_features)

        # initialize components via a few steps of GaussianMixture
        # this doesn't take into account errors, but is a fast first-guess
        gmm = GaussianMixture(self.n_components, max_iter=10,
                              covariance_type='full',
                              random_state=self.random_state).fit(X)
        self.mu = gmm.means_
        self.alpha = gmm.weights_
        self.V = gmm.covariances_

        logL = self.logL(X, Xerr)

        for i in range(self.max_iter):
            t0 = time()
            self._EMstep(X, Xerr)
            logL_next = self.logL(X, Xerr)
            t1 = time()

            if self.verbose:
                print("%i: log(L) = %.5g" % (i + 1, logL_next))
                print("    (%.2g sec)" % (t1 - t0))

            if logL_next < logL + self.tol:
                break
            logL = logL_next

        return self
//  [python]
def select_gmm(train_data, n_components_range, cv_types):
	bic = []
	lowest_bic = np.infty
	for cv_type in cv_types:
		for n_components in n_components_range:
			gmm = mixture.GaussianMixture(n_components=n_components,
				covariance_type=cv_type).fit(negative_examples)
			bic.append(gmm.bic(negative_examples))
			if bic[-1] < lowest_bic:
				lowest_bic = bic[-1]
				best_gmm = gmm

	bic = np.array(bic)
	return bic, best_gmm
//  [python]
def select_gmm(train_data, n_components_range, cv_types):
	bic = []
	lowest_bic = np.infty
	for cv_type in cv_types:
		for n_components in n_components_range:
			gmm = GaussianMixture(n_components=n_components,
				covariance_type=cv_type).fit(negative_examples)
			bic.append(gmm.bic(negative_examples))
			if bic[-1] < lowest_bic:
				lowest_bic = bic[-1]
				best_gmm = gmm

	bic = np.array(bic)
	return bic, best_gmm
//  [python]
def eval_train(model,all_loss):    
    model.eval()
    num_iter = (len(eval_loader.dataset)//eval_loader.batch_size)+1
    losses = torch.zeros(len(eval_loader.dataset))    
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) 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[index[b]]=loss[b]       
            sys.stdout.write('\r')
            sys.stdout.write('| Evaluating loss Iter[%3d/%3d]\t' %(batch_idx,num_iter)) 
            sys.stdout.flush()    
                                    
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    all_loss.append(losses)

    # fit a two-component GMM to the loss
    input_loss = losses.reshape(-1,1)
    gmm = mixture.GaussianMixture(n_components=2,max_iter=10,tol=1e-2,reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss) 
    prob = prob[:,gmm.means_.argmin()]         
    return prob,all_loss
//  [python]
def eval_train(model,all_loss):    
    model.eval()
    num_iter = (len(eval_loader.dataset)//eval_loader.batch_size)+1
    losses = torch.zeros(len(eval_loader.dataset))    
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) 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[index[b]]=loss[b]       
            sys.stdout.write('\r')
            sys.stdout.write('| Evaluating loss Iter[%3d/%3d]\t' %(batch_idx,num_iter)) 
            sys.stdout.flush()    
                                    
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    all_loss.append(losses)

    # fit a two-component GMM to the loss
    input_loss = losses.reshape(-1,1)
    gmm = GaussianMixture(n_components=2,max_iter=10,tol=1e-2,reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss) 
    prob = prob[:,gmm.means_.argmin()]         
    return prob,all_loss
//  [python]
def _get_model(self, k: int) -> Union[GaussianMixture, KMeans]:
        """
        Retrieve clustering model.

        :param k: amount of clusters.
        :return: Clustering model.
        """
        if self.algorithm == 'gmm':
            return GaussianMixture(n_components=k, random_state=self.random_state)

        return KMeans(n_clusters=k, random_state=self.random_state)
//  [python]
def lowest_bic_gmm(points, min_k=1, max_k=5):
    """Find a consensus location by fitting a GMM with lowest BIC.

    points: Array of points with dimension (N, 2).
    min_k: Minimum number of components.
    max_k: Maximum number of components.
    -> (x, y), boolean of whether the fit succeeded
    """
    min_bic = float('inf')
    min_gmm = None
    for k in range(min_k, max_k):
        gmm = s.GaussianMixture(
            n_components=k, covariance_type='full')
        try:
            gmm.fit(points)
        except ValueError:
            break
        bic = gmm.bic(points)
        if bic < min_bic:
            min_bic = bic
            min_gmm = gmm
    
    if not min_gmm:
        return points.mean(axis=0), False
    
    if sum(w == max(min_gmm.weights_) for w in min_gmm.weights_) > 1:
        success = False
    else:
        success = True
    
    return min_gmm.means_[min_gmm.weights_.argmax()], success
//  [python]
def lowest_bic_gmm(points, min_k=1, max_k=5):
    """Find a consensus location by fitting a GMM with lowest BIC.

    points: Array of points with dimension (N, 2).
    min_k: Minimum number of components.
    max_k: Maximum number of components.
    -> (x, y), boolean of whether the fit succeeded
    """
    min_bic = float('inf')
    min_gmm = None
    for k in range(min_k, max_k):
        gmm = sklearn.mixture.GaussianMixture(
            n_components=k, covariance_type='full')
        try:
            gmm.fit(points)
        except ValueError:
            break
        bic = gmm.bic(points)
        if bic < min_bic:
            min_bic = bic
            min_gmm = gmm
    
    if not min_gmm:
        return points.mean(axis=0), False
    
    if sum(w == max(min_gmm.weights_) for w in min_gmm.weights_) > 1:
        success = False
    else:
        success = True
    
    return min_gmm.means_[min_gmm.weights_.argmax()], success
//  [python]
mfcc_delta_second_order = librosa.feature.delta(mfcc,order=2)
temp = librosa.feature.delta(mfcc_delta)
inter = np.vstack((mfcc,mfcc_delta,mfcc_delta_second_order))
ubm_feature = inter.T
#ubm_feature = preprocessing.scale(ubm_feature)

# ubm_feature -= np.mean(ubm_feature)
# ubm_feature /= np.std(ubm_feature)

ubm_model = mixture.GaussianMixture(n_components = components, covariance_type = cov_type)
//  [python]
mfcc_delta_second_order = librosa.feature.delta(mfcc,order=2)
temp = librosa.feature.delta(mfcc_delta)
inter = np.vstack((mfcc,mfcc_delta,mfcc_delta_second_order))
ubm_feature = inter.T
#ubm_feature = preprocessing.scale(ubm_feature)

# ubm_feature -= np.mean(ubm_feature)
# ubm_feature /= np.std(ubm_feature)

ubm_model = GaussianMixture(n_components = components, covariance_type = cov_type)
//  [python]
def semimarkov_sufficient_stats(feature_list, label_list, covariance_type, n_classes, max_k=None):
    assert len(feature_list) == len(label_list)
    tied_diag = covariance_type == 'tied_diag'
    if tied_diag:
        emissions = mixture.GMM(n_classes, covariance_type='diag')
    else:
        emissions = mixture.GMM(n_classes, covariance_type=covariance_type)
    X_l = []
    r_l = []

    span_counts = np.zeros(n_classes, dtype=np.float32)
    span_lengths = np.zeros(n_classes, dtype=np.float32)
    span_start_counts = np.zeros(n_classes, dtype=np.float32)
    # to, from
    span_transition_counts = np.zeros((n_classes, n_classes), dtype=np.float32)

    instance_count = 0

    # for i in tqdm.tqdm(list(range(len(train_data))), ncols=80):
    for X, labels in zip(feature_list, label_list):
        X_l.append(X)
        r = np.zeros((X.shape[0], n_classes))
        r[np.arange(X.shape[0]), labels] = 1
        assert r.sum() == X.shape[0]
        r_l.append(r)
        spans = labels_to_spans(labels.unsqueeze(0), max_k)
        # symbol, length
        spans = rle_spans(spans, torch.LongTensor([spans.size(1)]))[0]
        last_symbol = None
        for index, (symbol, length) in enumerate(spans):
            if index == 0:
                span_start_counts[symbol] += 1
            span_counts[symbol] += 1
            span_lengths[symbol] += length
            if last_symbol is not None:
                span_transition_counts[symbol, last_symbol] += 1
            last_symbol = symbol
        instance_count += 1

    X_arr = np.vstack(X_l)
    r_arr = np.vstack(r_l)
    emissions._initialize(X_arr, r_arr)
    if tied_diag:
        cov, prec_chol = get_diagonal_covariances(X_arr)
        emissions.covariances_[:] = np.copy(cov)
        emissions.precisions_cholesky_[:] = np.copy(prec_chol)
    return emissions, {
        'span_counts': span_counts,
        'span_lengths': span_lengths,
        'span_start_counts': span_start_counts,
        'span_transition_counts': span_transition_counts,
        'instance_count': instance_count,
    }
//  [python]
def semimarkov_sufficient_stats(feature_list, label_list, covariance_type, n_classes, max_k=None):
    assert len(feature_list) == len(label_list)
    tied_diag = covariance_type == 'tied_diag'
    if tied_diag:
        emissions = GaussianMixture(n_classes, covariance_type='diag')
    else:
        emissions = GaussianMixture(n_classes, covariance_type=covariance_type)
    X_l = []
    r_l = []

    span_counts = np.zeros(n_classes, dtype=np.float32)
    span_lengths = np.zeros(n_classes, dtype=np.float32)
    span_start_counts = np.zeros(n_classes, dtype=np.float32)
    # to, from
    span_transition_counts = np.zeros((n_classes, n_classes), dtype=np.float32)

    instance_count = 0

    # for i in tqdm.tqdm(list(range(len(train_data))), ncols=80):
    for X, labels in zip(feature_list, label_list):
        X_l.append(X)
        r = np.zeros((X.shape[0], n_classes))
        r[np.arange(X.shape[0]), labels] = 1
        assert r.sum() == X.shape[0]
        r_l.append(r)
        spans = labels_to_spans(labels.unsqueeze(0), max_k)
        # symbol, length
        spans = rle_spans(spans, torch.LongTensor([spans.size(1)]))[0]
        last_symbol = None
        for index, (symbol, length) in enumerate(spans):
            if index == 0:
                span_start_counts[symbol] += 1
            span_counts[symbol] += 1
            span_lengths[symbol] += length
            if last_symbol is not None:
                span_transition_counts[symbol, last_symbol] += 1
            last_symbol = symbol
        instance_count += 1

    X_arr = np.vstack(X_l)
    r_arr = np.vstack(r_l)
    emissions._initialize(X_arr, r_arr)
    if tied_diag:
        cov, prec_chol = get_diagonal_covariances(X_arr)
        emissions.covariances_[:] = np.copy(cov)
        emissions.precisions_cholesky_[:] = np.copy(prec_chol)
    return emissions, {
        'span_counts': span_counts,
        'span_lengths': span_lengths,
        'span_start_counts': span_start_counts,
        'span_transition_counts': span_transition_counts,
        'instance_count': instance_count,
    }
//  [python]
def learn_gmm(X, n_comp=5, covar='full'):
  """
  Learn a Gaussian mixture model on features X,
  with 5 components and full covariance by default.

  Take
    X: data in N x K ndarray where N is no. samples and K is no. features
    n_comp: number of components in mixture model
    covar: covariance type ('full' or 'diag' most useful)

  Give
    gmm: a fitted sklearn GMM:
    http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html
  """
  gmm = mixture.GMM(n_components=5, covariance_type='full')
  gmm.fit(X)
  return gmm
//  [python]
def learn_gmm(X, n_comp=5, covar='full'):
  """
  Learn a Gaussian mixture model on features X,
  with 5 components and full covariance by default.

  Take
    X: data in N x K ndarray where N is no. samples and K is no. features
    n_comp: number of components in mixture model
    covar: covariance type ('full' or 'diag' most useful)

  Give
    gmm: a fitted sklearn GMM:
    http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html
  """
  gmm = mixture.GaussianMixture(n_components=5, covariance_type='full')
  gmm.fit(X)
  return gmm
//  [python]
def GMM(self, data, max_n_components):
        data = data.reshape(-1, 1)
        nan_array = np.isnan(data)
        not_nan_array = ~nan_array
        data = data[not_nan_array]
        data = data.reshape(-1, 1)

        N = np.arange(1, max_n_components + 1)
        models = [None for i in range(len(N))]
        for i in range(len(N)):
            try:
                models[i] = s.GMM(N[i], covariance_type="full", reg_covar=1e-10).fit(
                    data
                )
            except:
                models[i] = None

        # compute the AIC and the BIC
        AIC = []
        BIC = []
        for m in models:
            if m is not None:
                AIC.append(m.aic(data))
                BIC.append(m.bic(data))
            else:
                AIC.append(np.nan)
                BIC.append(np.nan)

        AIC = [m.aic(data) for m in models if m is not None]
        BIC = [m.bic(data) for m in models if m is not None]

        try:
            num_mixture = np.min([np.nanargmin(AIC), np.nanargmin(BIC)])
            M_best = models[num_mixture]

            means = M_best.means_
            stdevs = np.sqrt(M_best.covariances_)
            weights = M_best.weights_

            means_ = means.tolist()
            stdevs_ = stdevs.tolist()
            weights_ = weights.tolist()

            def sci_num(x):
                return "{:.2e}".format(x)

            means = [sci_num(m_[0]) for m_ in means_]
            stdevs = [sci_num(s_[0][0]) for s_ in stdevs_]
            weights = [sci_num(w_) for w_ in weights_]

            return means, stdevs, weights, AIC, BIC
        except:
            print("---Data is not enough for GMM!---")
            return np.nan, np.nan, np.nan, np.nan, np.nan
//  [python]
def GMM(self, data, max_n_components):
        data = data.reshape(-1, 1)
        nan_array = np.isnan(data)
        not_nan_array = ~nan_array
        data = data[not_nan_array]
        data = data.reshape(-1, 1)

        N = np.arange(1, max_n_components + 1)
        models = [None for i in range(len(N))]
        for i in range(len(N)):
            try:
                models[i] = GaussianMixture(N[i], covariance_type="full", reg_covar=1e-10).fit(
                    data
                )
            except:
                models[i] = None

        # compute the AIC and the BIC
        AIC = []
        BIC = []
        for m in models:
            if m is not None:
                AIC.append(m.aic(data))
                BIC.append(m.bic(data))
            else:
                AIC.append(np.nan)
                BIC.append(np.nan)

        AIC = [m.aic(data) for m in models if m is not None]
        BIC = [m.bic(data) for m in models if m is not None]

        try:
            num_mixture = np.min([np.nanargmin(AIC), np.nanargmin(BIC)])
            M_best = models[num_mixture]

            means = M_best.means_
            stdevs = np.sqrt(M_best.covariances_)
            weights = M_best.weights_

            means_ = means.tolist()
            stdevs_ = stdevs.tolist()
            weights_ = weights.tolist()

            def sci_num(x):
                return "{:.2e}".format(x)

            means = [sci_num(m_[0]) for m_ in means_]
            stdevs = [sci_num(s_[0][0]) for s_ in stdevs_]
            weights = [sci_num(w_) for w_ in weights_]

            return means, stdevs, weights, AIC, BIC
        except:
            print("---Data is not enough for GMM!---")
            return np.nan, np.nan, np.nan, np.nan, np.nan
//  [python]
def _test_pairplot_with_gmm_inputs(caller: unittest.TestCase, **kws):
    X = np.random.rand(15, 3)
    gmm = s.GaussianMixture(n_components=3, **kws).fit(X)
    labels = ["A"] * 5 + ["B"] * 5 + ["C"] * 5
    # test data
    with caller.assertRaises(ValueError):
        pairplot_with_gmm(X="test", gmm=gmm)

    with caller.assertRaises(ValueError):
        pairplot_with_gmm(X=X, gmm=gmm, labels=["A"])

    with caller.assertRaises(NameError):
        pairplot_with_gmm(X, gmm=None)
//  [python]
def _test_pairplot_with_gmm_inputs(caller: unittest.TestCase, **kws):
    X = np.random.rand(15, 3)
    gmm = GaussianMixture(n_components=3, **kws).fit(X)
    labels = ["A"] * 5 + ["B"] * 5 + ["C"] * 5
    # test data
    with caller.assertRaises(ValueError):
        pairplot_with_gmm(X="test", gmm=gmm)

    with caller.assertRaises(ValueError):
        pairplot_with_gmm(X=X, gmm=gmm, labels=["A"])

    with caller.assertRaises(NameError):
        pairplot_with_gmm(X, gmm=None)
//  [python]
def _test_pairplot_with_gmm_outputs(**kws):
    X = np.random.rand(15, 3)
    gmm = mixture.GaussianMixture(n_components=3, **kws).fit(X)
    labels = ["A"] * 5 + ["B"] * 5 + ["C"] * 5
    cluster_palette = {0: "red", 1: "blue", 2: "green"}
    label_palette = {"A": "red", "B": "blue", "C": "green"}
    fig = pairplot_with_gmm(X, gmm)
    fig = pairplot_with_gmm(
        X,
        gmm,
        labels=labels,
        cluster_palette=cluster_palette,
        label_palette=label_palette,
    )
//  [python]
def _test_pairplot_with_gmm_outputs(**kws):
    X = np.random.rand(15, 3)
    gmm = GaussianMixture(n_components=3, **kws).fit(X)
    labels = ["A"] * 5 + ["B"] * 5 + ["C"] * 5
    cluster_palette = {0: "red", 1: "blue", 2: "green"}
    label_palette = {"A": "red", "B": "blue", "C": "green"}
    fig = pairplot_with_gmm(X, gmm)
    fig = pairplot_with_gmm(
        X,
        gmm,
        labels=labels,
        cluster_palette=cluster_palette,
        label_palette=label_palette,
    )