jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def estimate_driver_gmm(input_folder, max_len):
    data_paths = [f for f in os.listdir(input_folder) if f.endswith("pkl")]
    current_set = []
    for p in data_paths:
        pkl_path = os.path.join(input_folder, p)
        with open(pkl_path, "rb") as f:
            current_data = pkl.load(f)["current"]
            current_mask = np.zeros(max_len)
            duration = len(current_data)
            if duration > max_len:
                continue
            current_mask[0:duration] = current_data
            current_set.append(current_mask.tolist())
    X = np.array(current_set)
    gmm = GaussianMixture(n_components=15, random_state=0, max_iter=200).fit(X)
    return gmm
//  [python]
three_multiplexed_models_steps = [
         ('demux', demux),
         ('lm_0', lm_0),
         ('lm_1', lm_1),
         ('lm_2', lm_2),
         ('mux', mux), ]

three_multiplexed_models_connections = {
                'demux': {'X': 'X',
                          'y': 'y',
                          'selection': 'selection'},
                'lm_0': {'X': ('demux', 'X_0'),
                         'y': ('demux', 'y_0')},
                'lm_1': {'X': ('demux', 'X_1'),
                         'y': ('demux', 'y_1')},
                'lm_2': {'X': ('demux', 'X_2'),
                         'y': ('demux', 'y_2')},
                'mux': {'0': 'lm_0',
                        '1': 'lm_1',
                        '2': 'lm_2',
                        'selection': 'selection'}}

three_multiplexed_models = PipeGraph(steps=three_multiplexed_models_steps,
                                     fit_connections=three_multiplexed_models_connections )

#########################################################################################################
#  Now we can treat this PipeGraph as a reusable component and use it as a unitary step in another PipeGraph:
scaler = MinMaxScaler()
gaussian_mixture = mixture.GaussianMixture(n_components=3)
//  [python]
three_multiplexed_models_steps = [
         ('demux', demux),
         ('lm_0', lm_0),
         ('lm_1', lm_1),
         ('lm_2', lm_2),
         ('mux', mux), ]

three_multiplexed_models_connections = {
                'demux': {'X': 'X',
                          'y': 'y',
                          'selection': 'selection'},
                'lm_0': {'X': ('demux', 'X_0'),
                         'y': ('demux', 'y_0')},
                'lm_1': {'X': ('demux', 'X_1'),
                         'y': ('demux', 'y_1')},
                'lm_2': {'X': ('demux', 'X_2'),
                         'y': ('demux', 'y_2')},
                'mux': {'0': 'lm_0',
                        '1': 'lm_1',
                        '2': 'lm_2',
                        'selection': 'selection'}}

three_multiplexed_models = PipeGraph(steps=three_multiplexed_models_steps,
                                     fit_connections=three_multiplexed_models_connections )

#########################################################################################################
#  Now we can treat this PipeGraph as a reusable component and use it as a unitary step in another PipeGraph:
scaler = MinMaxScaler()
gaussian_mixture = GaussianMixture(n_components=3)
//  [python]
def __init__(self, n_components=1, **kwargs):
        super().__init__(**kwargs)

        self.n_components = n_components
        self._gmm = mixture.GaussianMixture(n_components=n_components)
//  [python]
def __init__(self, n_components=1, **kwargs):
        super().__init__(**kwargs)

        self.n_components = n_components
        self._gmm = GaussianMixture(n_components=n_components)
//  [python]
def __init__(
        self, dataset, device='cuda',
        dtype=torch.float, requires_grad=False,
        n_components=1
    ):
        super(NormalTensorDatasetSampler, self).__init__(
            device=device, dtype=dtype, requires_grad=requires_grad
        )
        self.sampler = mixture.GaussianMixture(n_components)
        self.sampler.fit(dataset)
//  [python]
def __init__(
        self, dataset, device='cuda',
        dtype=torch.float, requires_grad=False,
        n_components=1
    ):
        super(NormalTensorDatasetSampler, self).__init__(
            device=device, dtype=dtype, requires_grad=requires_grad
        )
        self.sampler = GaussianMixture(n_components)
        self.sampler.fit(dataset)
//  [python]
def learn_gmm_cluster(self, n_gmm=2, **kwargs):
        self.my_classifier = mixture.GaussianMixture(
            n_components=10, covariance_type="full", max_iter=100
        )
//  [python]
def learn_gmm_cluster(self, n_gmm=2, **kwargs):
        self.my_classifier = GaussianMixture(
            n_components=10, covariance_type="full", max_iter=100
        )
//  [python]
def __init__(self, n_components, n_classes=2, **kwargs):
        self.n_classes = n_classes
        self._gmm_models = [
            mixture.GaussianMixture(n_components=n_components, **kwargs)
            for ii in range(n_classes)
        ]
//  [python]
def __init__(self, n_components, n_classes=2, **kwargs):
        self.n_classes = n_classes
        self._gmm_models = [
            GaussianMixture(n_components=n_components, **kwargs)
            for ii in range(n_classes)
        ]
//  [python]
def __optimize(self, prep_data: array):
        """
        Auxiliary method to optimize the number of components to be considered for the Gaussian or Bayesian Mixture
        Returns:
            n_components (int): Optimal number of components calculated based on Silhouette score
        """
        c = arange(2, 40, 5)
        n_components=2
        max_silhouette=0
        for n in tqdm(c, desc="Hyperparameter search"):
            model = mixture.GaussianMixture(n, covariance_type=self.covariance_type, random_state=self.random_state)
            labels = model.fit_predict(prep_data)
            s = silhouette_score(prep_data, labels, metric='euclidean')
            if model.converged_:
                if max_silhouette < s:
                    n_components = n
                    max_silhouette=s
        return n_components
//  [python]
def __optimize(self, prep_data: array):
        """
        Auxiliary method to optimize the number of components to be considered for the Gaussian or Bayesian Mixture
        Returns:
            n_components (int): Optimal number of components calculated based on Silhouette score
        """
        c = arange(2, 40, 5)
        n_components=2
        max_silhouette=0
        for n in tqdm(c, desc="Hyperparameter search"):
            model = GaussianMixture(n, covariance_type=self.covariance_type, random_state=self.random_state)
            labels = model.fit_predict(prep_data)
            s = silhouette_score(prep_data, labels, metric='euclidean')
            if model.converged_:
                if max_silhouette < s:
                    n_components = n
                    max_silhouette=s
        return n_components
//  [python]
def Recognize(self, fn):
        im = Image.open(fn)
        im = util.CenterExtend(im, radius=20)

        vec = np.asarray(im.convert('L')).copy()
        Y = []
        for i in range(vec.shape[0]):
            for j in range(vec.shape[1]):
                if vec[i][j] <= 200:
                    Y.append([i, j])

        gmm = mixture.GMM(n_components=7, covariance_type='tied', reg_covar=1e2, tol=1e3, n_init=9)
        gmm.fit(Y)
        
        centers = gmm.means_

        points = []
        for i in range(7):
            scoring = 0.0
            for w_i in range(3):
                for w_j in range(3):
                    p_x = centers[i][0] -1 +w_i
                    p_y = centers[i][1] -1 +w_j

                    cr = util.crop(im, p_x, p_y, radius=20)
                    cr = cr.resize((40, 40), Image.ANTIALIAS)

                    X = np.asarray(cr.convert('L'), dtype='float')
                    X = (X.astype("float") - 180) /200

                    x0 = np.expand_dims(X, axis=0)
                    x1 = np.expand_dims(x0, axis=3)

                    global model
                    if self.model.predict(x1)[0][0] < 0.5:
                        scoring += 1

            if scoring > 4:
                points.append((centers[i][0] -20, centers[i][1] -20))
                
        return points
//  [python]
def Recognize(self, fn):
        im = Image.open(fn)
        im = util.CenterExtend(im, radius=20)

        vec = np.asarray(im.convert('L')).copy()
        Y = []
        for i in range(vec.shape[0]):
            for j in range(vec.shape[1]):
                if vec[i][j] <= 200:
                    Y.append([i, j])

        gmm = GaussianMixture(n_components=7, covariance_type='tied', reg_covar=1e2, tol=1e3, n_init=9)
        gmm.fit(Y)
        
        centers = gmm.means_

        points = []
        for i in range(7):
            scoring = 0.0
            for w_i in range(3):
                for w_j in range(3):
                    p_x = centers[i][0] -1 +w_i
                    p_y = centers[i][1] -1 +w_j

                    cr = util.crop(im, p_x, p_y, radius=20)
                    cr = cr.resize((40, 40), Image.ANTIALIAS)

                    X = np.asarray(cr.convert('L'), dtype='float')
                    X = (X.astype("float") - 180) /200

                    x0 = np.expand_dims(X, axis=0)
                    x1 = np.expand_dims(x0, axis=3)

                    global model
                    if self.model.predict(x1)[0][0] < 0.5:
                        scoring += 1

            if scoring > 4:
                points.append((centers[i][0] -20, centers[i][1] -20))
                
        return points
//  [python]
def compute_GMM(n_clusters, max_iter=1000, tol=3, covariance_type='full'):
    clf = mixture.GaussianMixture(n_clusters, covariance_type=covariance_type,
                          max_iter=max_iter, tol=tol, random_state=0)
    clf.fit(X)
    print("converged:", clf.converged_)
    return clf
//  [python]
def compute_GMM(n_clusters, max_iter=1000, tol=3, covariance_type='full'):
    clf = GaussianMixture(n_clusters, covariance_type=covariance_type,
                          max_iter=max_iter, tol=tol, random_state=0)
    clf.fit(X)
    print("converged:", clf.converged_)
    return clf
//  [python]
def fit_intensity_pivot(pc, debug):
    intensity = pc[:, 3]
    # gmm = GaussianMixture(n_components=3, covariance_type="diag",
    #                       max_iter=10000, means_init=np.array([[5], [60], [120]])).fit(intensity.reshape(-1, 1))
    gmm = mixture.GaussianMixture(n_components=2, covariance_type="diag",
                          max_iter=10000, means_init=np.array([[5], [60]])).fit(intensity.reshape(-1, 1))
    pivot = gmm.means_[:2].mean()
    print('Intensity pivot: {}'.format(pivot))
    if debug > 2:
        import matplotlib.pyplot as plt
        plt.hist(intensity, bins=100)
        plt.show()
    return pivot
//  [python]
def fit_intensity_pivot(pc, debug):
    intensity = pc[:, 3]
    # gmm = GaussianMixture(n_components=3, covariance_type="diag",
    #                       max_iter=10000, means_init=np.array([[5], [60], [120]])).fit(intensity.reshape(-1, 1))
    gmm = GaussianMixture(n_components=2, covariance_type="diag",
                          max_iter=10000, means_init=np.array([[5], [60]])).fit(intensity.reshape(-1, 1))
    pivot = gmm.means_[:2].mean()
    print('Intensity pivot: {}'.format(pivot))
    if debug > 2:
        import matplotlib.pyplot as plt
        plt.hist(intensity, bins=100)
        plt.show()
    return pivot
//  [python]
def compute_Pcv_abs(dump_dir, num_class, num_each_class, n_components=6):
    ''' Fit GMM to absolute translation
    '''
    data_dir = f'{dump_dir}/trans_abs'

    return_dict = {}
    for select_sem in range(num_class):
        print(select_sem)
        fname = os.path.join(data_dir, str(select_sem) + '_trans.npy')
        X = np.load(fname)
        gmm_x = mixture.GMM(n_components=n_components, random_state=0).fit(X[:, 0:1])
        gmm_y = mixture.GMM(n_components=n_components, random_state=0).fit(X[:, 1:2])
        return_dict[select_sem] = [gmm_x, gmm_y]

    return return_dict
//  [python]
def compute_Pcv_abs(dump_dir, num_class, num_each_class, n_components=6):
    ''' Fit GMM to absolute translation
    '''
    data_dir = f'{dump_dir}/trans_abs'

    return_dict = {}
    for select_sem in range(num_class):
        print(select_sem)
        fname = os.path.join(data_dir, str(select_sem) + '_trans.npy')
        X = np.load(fname)
        gmm_x = GaussianMixture(n_components=n_components, random_state=0).fit(X[:, 0:1])
        gmm_y = GaussianMixture(n_components=n_components, random_state=0).fit(X[:, 1:2])
        return_dict[select_sem] = [gmm_x, gmm_y]

    return return_dict
//  [python]
def test_sklearn(
    benchmark: BenchmarkFixture,
    num_datapoints: int,
    num_features: int,
    num_components: int,
    covariance_type: CovarianceType,
):
    pl.seed_everything(0)
    data, means = sample_gmm(num_datapoints, num_features, num_components, covariance_type)

    estimator = SklearnGaussianMixture(
        num_components,
        covariance_type=covariance_type,
        tol=0,
        n_init=1,
        max_iter=100,
        reg_covar=1e-3,
        init_params="random",
        means_init=means.numpy(),
    )
    benchmark(estimator.fit, data.numpy())
//  [python]
def transform(self, data):
        """Cluster values using a `skelarn.mixture.GaussianMixture`_ model.

        Args:
            data(numpy.ndarray): Values to cluster in array of shape (n,1).

        Returns:
            tuple[numpy.ndarray, numpy.ndarray, list, list]: Tuple containg the features,
            probabilities, averages and stds of the given data.

        .. _skelarn.mixture.GaussianMixture: https://scikit-learn.org/stable/modules/generated/
            sklearn.mixture.GaussianMixture.html

        """
        model = GaussianMixture(self.num_modes)
        model.fit(data)

        means = model.means_.reshape((1, self.num_modes))
        stds = np.sqrt(model.covariances_).reshape((1, self.num_modes))

        features = (data - means) / (2 * stds)
        probs = model.predict_proba(data)
        argmax = np.argmax(probs, axis=1)
        idx = np.arange(len(features))
        features = features[idx, argmax].reshape([-1, 1])

        features = np.clip(features, -0.99, 0.99)

        return features, probs, list(means.flat), list(stds.flat)
//  [python]
def compute_Pcv_rel(dump_dir, num_class, num_each_class, n_components=8):
    ''' Fit GMM to relative translation for i < j 
    '''
    data_dir = f'{dump_dir}/trans_rel' # (i, i) = 0 same object is removed for same class

    return_dict = {}
    for i in range(num_class):
        for j in range(num_class):
            if i > j:
                continue
            select_sem = (i, j)
            print(select_sem)

            fname = os.path.join(data_dir, str(select_sem[0]) + '_' + str(select_sem[1]) + '_trans.npy')
            X = np.load(fname)
            gmm_x = mixture.GMM(n_components=n_components, random_state=0).fit(X[:, 0:1])
            gmm_y = mixture.GMM(n_components=n_components, random_state=0).fit(X[:, 1:2])

            return_dict[(select_sem[0], select_sem[1])] = [gmm_x, gmm_y]

    return return_dict
//  [python]
def compute_Pcv_rel(dump_dir, num_class, num_each_class, n_components=8):
    ''' Fit GMM to relative translation for i < j 
    '''
    data_dir = f'{dump_dir}/trans_rel' # (i, i) = 0 same object is removed for same class

    return_dict = {}
    for i in range(num_class):
        for j in range(num_class):
            if i > j:
                continue
            select_sem = (i, j)
            print(select_sem)

            fname = os.path.join(data_dir, str(select_sem[0]) + '_' + str(select_sem[1]) + '_trans.npy')
            X = np.load(fname)
            gmm_x = GaussianMixture(n_components=n_components, random_state=0).fit(X[:, 0:1])
            gmm_y = GaussianMixture(n_components=n_components, random_state=0).fit(X[:, 1:2])

            return_dict[(select_sem[0], select_sem[1])] = [gmm_x, gmm_y]

    return return_dict
//  [python]
def main():
    Xtrain, Ytrain, _, _ = getKaggleMNIST()

    sample_size = 1000
    X = Xtrain[:sample_size]
    Y = Ytrain[:sample_size]

    tsne = TSNE()
    Z = tsne.fit_transform(X)
    plt.scatter(Z[:,0], Z[:,1], s=100, c=Y, alpha=0.5)
    plt.show()

    # purity measure from unsupervised machine learning pt 1
    # maximum purity is 1, higher is better
    gmm = mixture.GaussianMixture(n_components=10)
    gmm.fit(X)
    Rfull = gmm.predict_proba(X)
    print("Rfull.shape:", Rfull.shape)
    print("full purity:", purity(Y, Rfull))

    # now try the same thing on the reduced data
    gmm.fit(Z)
    Rreduced = gmm.predict_proba(Z)
    print("reduced purity:", purity(Y, Rreduced))
//  [python]
def main():
    Xtrain, Ytrain, _, _ = getKaggleMNIST()

    sample_size = 1000
    X = Xtrain[:sample_size]
    Y = Ytrain[:sample_size]

    tsne = TSNE()
    Z = tsne.fit_transform(X)
    plt.scatter(Z[:,0], Z[:,1], s=100, c=Y, alpha=0.5)
    plt.show()

    # purity measure from unsupervised machine learning pt 1
    # maximum purity is 1, higher is better
    gmm = GaussianMixture(n_components=10)
    gmm.fit(X)
    Rfull = gmm.predict_proba(X)
    print("Rfull.shape:", Rfull.shape)
    print("full purity:", purity(Y, Rfull))

    # now try the same thing on the reduced data
    gmm.fit(Z)
    Rreduced = gmm.predict_proba(Z)
    print("reduced purity:", purity(Y, Rreduced))
//  [python]
def fit(self, data, categorical_columns=tuple(), ordinal_columns=tuple()):
        self.meta = self.get_metadata(data, categorical_columns, ordinal_columns)
        model = []

        self.output_info = []
        self.output_dim = 0
        for id_, info in enumerate(self.meta):
            if info['type'] == CONTINUOUS:
                gm = mixture.GaussianMixture(self.n_clusters)
                gm.fit(data[:, id_].reshape([-1, 1]))
                model.append(gm)
                self.output_info += [(1, 'tanh'), (self.n_clusters, 'softmax')]
                self.output_dim += 1 + self.n_clusters
            else:
                model.append(None)
                self.output_info += [(info['size'], 'softmax')]
                self.output_dim += info['size']

        self.model = model
//  [python]
def fit(self, data, categorical_columns=tuple(), ordinal_columns=tuple()):
        self.meta = self.get_metadata(data, categorical_columns, ordinal_columns)
        model = []

        self.output_info = []
        self.output_dim = 0
        for id_, info in enumerate(self.meta):
            if info['type'] == CONTINUOUS:
                gm = GaussianMixture(self.n_clusters)
                gm.fit(data[:, id_].reshape([-1, 1]))
                model.append(gm)
                self.output_info += [(1, 'tanh'), (self.n_clusters, 'softmax')]
                self.output_dim += 1 + self.n_clusters
            else:
                model.append(None)
                self.output_info += [(info['size'], 'softmax')]
                self.output_dim += info['size']

        self.model = model
//  [python]
def __init__(self, n_components, train_loader, seed=random.seed('2019')):
        super(JointDistributionGenerator, self).__init__()
        self.seed = seed
        self.n_components = n_components
        trainset = list(train_loader.dataset)
        x_train = torch.stack([x[0] for x in trainset]).cpu().numpy()
        x_train = [x_train[i,:,j] for i in range(x_train.shape[0]) for j in range(x_train.shape[-1])]
        self.gmm = mixture.GaussianMixture(n_components=n_components, covariance_type='full')
        print('Fitting the distribution ...')
        self.gmm.fit(np.array(x_train))
        # print('Cluster means: ', self.gmm.means_)
        print('Loglike scores: ', self.gmm.score(np.array(x_train)))
//  [python]
def __init__(self, n_components, train_loader, seed=random.seed('2019')):
        super(JointDistributionGenerator, self).__init__()
        self.seed = seed
        self.n_components = n_components
        trainset = list(train_loader.dataset)
        x_train = torch.stack([x[0] for x in trainset]).cpu().numpy()
        x_train = [x_train[i,:,j] for i in range(x_train.shape[0]) for j in range(x_train.shape[-1])]
        self.gmm = GaussianMixture(n_components=n_components, covariance_type='full')
        print('Fitting the distribution ...')
        self.gmm.fit(np.array(x_train))
        # print('Cluster means: ', self.gmm.means_)
        print('Loglike scores: ', self.gmm.score(np.array(x_train)))
//  [python]
def fit(self, X, y):
        """Divide X according to y and get two Gaussian model."""
        X_sorted = X[np.argsort(-y)]
        if X.shape[0] < 4:
            gaussian_high = mixture.GMM().fit(X_sorted)
            gaussian_low = gaussian_high
        else:
            point_segmentation = max(2, int(self.gamma * X.shape[0]))
            gaussian_high = mixture.GMM().fit(X_sorted[:point_segmentation])
            gaussian_low = mixture.GMM().fit(X_sorted[point_segmentation:])

        self.means_ = [gaussian_high.means_[0], gaussian_low.means_[0]]
        self.covariances_ = [gaussian_high.covariances_[0], gaussian_low.covariances_[0]]
//  [python]
def fit(self, X, y):
        """Divide X according to y and get two Gaussian model."""
        X_sorted = X[np.argsort(-y)]
        if X.shape[0] < 4:
            gaussian_high = GaussianMixture().fit(X_sorted)
            gaussian_low = gaussian_high
        else:
            point_segmentation = max(2, int(self.gamma * X.shape[0]))
            gaussian_high = GaussianMixture().fit(X_sorted[:point_segmentation])
            gaussian_low = GaussianMixture().fit(X_sorted[point_segmentation:])

        self.means_ = [gaussian_high.means_[0], gaussian_low.means_[0]]
        self.covariances_ = [gaussian_high.covariances_[0], gaussian_low.covariances_[0]]
//  [python]
def cluster(array, n_clusters, max_mask_pts=np.infty):
    """
    Cluster a 2-D binary array.
    Applies a Gaussian Mixture Model on the positive elements of the array,
    and returns the number of clusters.
    
    :param array: Binary array.
    :param n_clusters: Number of clusters (Gaussians) to fit,
    :param max_mask_pts: Randomly subsample "max_pts" points
                         from the array before fitting.
    :return: Centroids in the input array.
    """

    array = np.array(array)
    
    assert array.ndim == 2

    coord = np.where(array > 0)
    y = coord[0].reshape((-1, 1))
    x = coord[1].reshape((-1, 1))
    c = np.concatenate((y, x), axis=1)
    if len(c) == 0:
        centroids = np.array([])
    else:
        # Subsample our points randomly so it is faster
        if max_mask_pts != np.infty:
            n_pts = min(len(c), max_mask_pts)
            np.random.shuffle(c)
            c = c[:n_pts]

        # If the estimation is horrible, we cannot fit a GMM if n_components > n_samples
        n_components = max(min(n_clusters, x.size), 1)
        centroids = s.mixture.GaussianMixture(n_components=n_components,
                                                    n_init=1,
                                                    covariance_type='full').\
            fit(c).means_.astype(np.int)

    return centroids
//  [python]
def cluster(array, n_clusters, max_mask_pts=np.infty):
    """
    Cluster a 2-D binary array.
    Applies a Gaussian Mixture Model on the positive elements of the array,
    and returns the number of clusters.
    
    :param array: Binary array.
    :param n_clusters: Number of clusters (Gaussians) to fit,
    :param max_mask_pts: Randomly subsample "max_pts" points
                         from the array before fitting.
    :return: Centroids in the input array.
    """

    array = np.array(array)
    
    assert array.ndim == 2

    coord = np.where(array > 0)
    y = coord[0].reshape((-1, 1))
    x = coord[1].reshape((-1, 1))
    c = np.concatenate((y, x), axis=1)
    if len(c) == 0:
        centroids = np.array([])
    else:
        # Subsample our points randomly so it is faster
        if max_mask_pts != np.infty:
            n_pts = min(len(c), max_mask_pts)
            np.random.shuffle(c)
            c = c[:n_pts]

        # If the estimation is horrible, we cannot fit a GMM if n_components > n_samples
        n_components = max(min(n_clusters, x.size), 1)
        centroids = sklearn.mixture.GaussianMixture(n_components=n_components,
                                                    n_init=1,
                                                    covariance_type='full').\
            fit(c).means_.astype(np.int)

    return centroids
//  [python]
def gmm(
            embedding: np.ndarray,
            n_components: int,
            threshold: float
    ) -> np.ndarray:
        estimator = mixture.GaussianMixture(
            n_components=n_components,
            **ClusterDocuments.GAUSSIAN_KWARGS
        )
        estimator.fit(embedding)
        probs = estimator.predict_proba(embedding)
        cluster_labels = np.argmax(probs, axis=1)
        max_probs = np.max(probs, axis=1)
        cluster_labels[max_probs < threshold] = -1
        return cluster_labels
//  [python]
def gmm(
            embedding: np.ndarray,
            n_components: int,
            threshold: float
    ) -> np.ndarray:
        estimator = GaussianMixture(
            n_components=n_components,
            **ClusterDocuments.GAUSSIAN_KWARGS
        )
        estimator.fit(embedding)
        probs = estimator.predict_proba(embedding)
        cluster_labels = np.argmax(probs, axis=1)
        max_probs = np.max(probs, axis=1)
        cluster_labels[max_probs < threshold] = -1
        return cluster_labels
//  [python]
def _get_trained_synthesizer(self, real_data, metadata):
        hyper_transformer = HyperTransformer()
        hyper_transformer.detect_initial_config(real_data)

        # This is done to match the behavior of the synthesizer for SDGym <= 0.6.0
        columns_to_remove = [
            column_name for column_name, data in real_data.items()
            if data.dtype.kind in {'O', 'i'}
        ]
        hyper_transformer.remove_transformers(columns_to_remove)

        hyper_transformer.fit(real_data)
        transformed = hyper_transformer.transform(real_data)

        self.length = len(real_data)
        gm_models = {}
        for name, column in transformed.items():
            kind = column.dtype.kind
            if kind != 'O':
                num_components = min(column.nunique(), 5)
                model = mixture.GMM(n_components=num_components)
                model.fit(column.to_numpy().reshape(-1, 1))
                gm_models[name] = model

        return (hyper_transformer, transformed, gm_models)
//  [python]
def _get_trained_synthesizer(self, real_data, metadata):
        hyper_transformer = HyperTransformer()
        hyper_transformer.detect_initial_config(real_data)

        # This is done to match the behavior of the synthesizer for SDGym <= 0.6.0
        columns_to_remove = [
            column_name for column_name, data in real_data.items()
            if data.dtype.kind in {'O', 'i'}
        ]
        hyper_transformer.remove_transformers(columns_to_remove)

        hyper_transformer.fit(real_data)
        transformed = hyper_transformer.transform(real_data)

        self.length = len(real_data)
        gm_models = {}
        for name, column in transformed.items():
            kind = column.dtype.kind
            if kind != 'O':
                num_components = min(column.nunique(), 5)
                model = GaussianMixture(num_components)
                model.fit(column.to_numpy().reshape(-1, 1))
                gm_models[name] = model

        return (hyper_transformer, transformed, gm_models)
//  [python]
def _empirical_tmrca(self, k):
        '''Calculate the empirical distribution of TMRCA in the
        distinguished lineages by counting mutations'''
        w = self._pipeline['mutation_counts'].w
        X = self._pipeline['mutation_counts'].counts
        logger.debug("Computing quantiles of TMRCA distribution from M=%d TMRCA samples", len(X))
        logger.debug("Unresampled quantiles (0/10/25/50/75/100): %s",
                     scipy.stats.mstats.mquantiles(X, [0, .1, .25, .5, .75, 1.]))
        # fit a k-poisson mixture model
        gmm = sklearn.mixture.GMM(n_components=k).fit(X[:, None])
        Y = gmm.sample(n_samples=100000)[0]
        p = np.logspace(np.log10(.01), np.log10(.99), k)
        q = scipy.stats.mstats.mquantiles(Y[Y>0], p) / (2 * self._theta * w)
        logger.debug("Quantiles: %s", " ".join("F(%g)=%g" % c for c in zip(q, p)))
        # 2 * E(TMRCA) * self._theta ~= q
        logger.debug("empirical TMRCA distribution: %s", q)
        self._etmrca_quantiles = q
//  [python]
def _empirical_tmrca(self, k):
        '''Calculate the empirical distribution of TMRCA in the
        distinguished lineages by counting mutations'''
        w = self._pipeline['mutation_counts'].w
        X = self._pipeline['mutation_counts'].counts
        logger.debug("Computing quantiles of TMRCA distribution from M=%d TMRCA samples", len(X))
        logger.debug("Unresampled quantiles (0/10/25/50/75/100): %s",
                     scipy.stats.mstats.mquantiles(X, [0, .1, .25, .5, .75, 1.]))
        # fit a k-poisson mixture model
        gmm = sklearn.mixture.GaussianMixture(n_components=k).fit(X[:, None])
        Y = gmm.sample(n_samples=100000)[0]
        p = np.logspace(np.log10(.01), np.log10(.99), k)
        q = scipy.stats.mstats.mquantiles(Y[Y>0], p) / (2 * self._theta * w)
        logger.debug("Quantiles: %s", " ".join("F(%g)=%g" % c for c in zip(q, p)))
        # 2 * E(TMRCA) * self._theta ~= q
        logger.debug("empirical TMRCA distribution: %s", q)
        self._etmrca_quantiles = q
//  [python]
def GMM(self, x):
        """

        """
        model = mixture.GaussianMixture(n_components=self.max_clusters)
        model.fit(x)
        return model
//  [python]
def GMM(self, x):
        """

        """
        model = GaussianMixture(n_components=self.max_clusters)
        model.fit(x)
        return model
//  [python]
def gmm_compute_n_components(
            embedding: np.ndarray,
            progress_callback: Optional[Callable] = None
    ) -> int:
        if progress_callback is None:
            progress_callback = dummy_callback

        scores = []
        max_n_clusters = min(len(embedding), 11)
        for n_clusters in range(2, max_n_clusters):
            progress_callback(n_clusters / max_n_clusters)
            estimator = mixture.GaussianMixture(
                n_components=n_clusters,
                **ClusterDocuments.GAUSSIAN_KWARGS
            )
            cluster_labels = estimator.fit_predict(embedding)
            silhouette_avg = silhouette_score(embedding, cluster_labels)
            scores.append(silhouette_avg)
        return np.argmax(scores) + 2
//  [python]
def gmm_compute_n_components(
            embedding: np.ndarray,
            progress_callback: Optional[Callable] = None
    ) -> int:
        if progress_callback is None:
            progress_callback = dummy_callback

        scores = []
        max_n_clusters = min(len(embedding), 11)
        for n_clusters in range(2, max_n_clusters):
            progress_callback(n_clusters / max_n_clusters)
            estimator = GaussianMixture(
                n_components=n_clusters,
                **ClusterDocuments.GAUSSIAN_KWARGS
            )
            cluster_labels = estimator.fit_predict(embedding)
            silhouette_avg = silhouette_score(embedding, cluster_labels)
            scores.append(silhouette_avg)
        return np.argmax(scores) + 2
//  [python]
n_gaussians = means.shape[0]

points = []

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

points = np.concatenate(points)

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

points = []

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

points = np.concatenate(points)

#fit the gaussian model
gmm = GaussianMixture(n_components=n_gaussians, covariance_type='diag')
//  [python]
def compute_gassian_ll_scores(embeddings, reg_covar: float=0.0):
    gmm = mixture.GaussianMixture(n_components=1, reg_covar=reg_covar)
    gmm.fit(embeddings)
    log_likelihood = gmm.score_samples(embeddings)
    return log_likelihood
//  [python]
def compute_gassian_ll_scores(embeddings, reg_covar: float=0.0):
    gmm = GaussianMixture(n_components=1, reg_covar=reg_covar)
    gmm.fit(embeddings)
    log_likelihood = gmm.score_samples(embeddings)
    return log_likelihood
//  [python]
def _initialize_variables(self, heterozygotes_white):
        """
        Initializes the variables for the optimization process.

        Parameters
        ----------
        heterozygotes_white : ndarray
            The whitened data for heterozygotes.

        Returns
        -------
        beta_ : Tensor
            The initial values for the optimization variables.
        """
        mod = mixture.GaussianMixture(2)
        mod.fit(heterozygotes_white)
        beta_i = (mod.means_[0] - mod.means_[1]) / 2
        beta_ = torch.tensor(beta_i, requires_grad=True)
        return beta_
//  [python]
def _initialize_variables(self, heterozygotes_white):
        """
        Initializes the variables for the optimization process.

        Parameters
        ----------
        heterozygotes_white : ndarray
            The whitened data for heterozygotes.

        Returns
        -------
        beta_ : Tensor
            The initial values for the optimization variables.
        """
        mod = GaussianMixture(2)
        mod.fit(heterozygotes_white)
        beta_i = (mod.means_[0] - mod.means_[1]) / 2
        beta_ = torch.tensor(beta_i, requires_grad=True)
        return beta_
//  [python]
def _fit_gmm(self, pos_loss_gmm, pos_inds_gmm):
        """GMM fits to pos_losses of a gt obj"""
        # normalize loss may stabilize the GMM fittig
        if self.norm_loss:
            min_val = pos_loss_gmm.min()
            max_val = pos_loss_gmm.max()
            pos_loss_gmm = (pos_loss_gmm - min_val) / (max_val - min_val + EPS)
        device = pos_inds_gmm.device
        pos_loss_gmm, sort_inds = pos_loss_gmm.sort()
        pos_inds_gmm = pos_inds_gmm[sort_inds]
        pos_loss_gmm = pos_loss_gmm.view(-1, 1).cpu().numpy()
        min_loss, max_loss = pos_loss_gmm.min(), pos_loss_gmm.max()
        means_init = np.array([min_loss, max_loss]).reshape(2, 1)
        weights_init = np.array([0.5, 0.5])
        precisions_init = np.array([1.0, 1.0]).reshape(2, 1, 1)  # full
        if self.covariance_type == 'spherical':
            precisions_init = precisions_init.reshape(2)
        elif self.covariance_type == 'diag':
            precisions_init = precisions_init.reshape(2, 1)
        elif self.covariance_type == 'tied':
            precisions_init = np.array([[1.0]])
        if skm is None:
            raise ImportError('Please run "pip install sklearn" '
                              'to install sklearn first.')
        gmm = skm.mixture.GaussianMixture(
            2,
            weights_init=weights_init,
            means_init=means_init,
            precisions_init=precisions_init,
            covariance_type=self.covariance_type)
        try:
            gmm.fit(pos_loss_gmm)
        except:
            print("GMM fitting error")
        gmm_assignment = gmm.predict(pos_loss_gmm)
        scores = gmm.score_samples(pos_loss_gmm)
        gmm_assignment = torch.from_numpy(gmm_assignment).to(device)
        scores = torch.from_numpy(scores).to(device)

        pos_inds_temp, ignore_inds_temp = self.gmm_separation_scheme(
            gmm_assignment, scores, pos_inds_gmm, pos_loss_gmm, gmm)
        return pos_inds_temp, ignore_inds_temp
//  [python]
def _fit_gmm(self, pos_loss_gmm, pos_inds_gmm):
        """GMM fits to pos_losses of a gt obj"""
        # normalize loss may stabilize the GMM fittig
        if self.norm_loss:
            min_val = pos_loss_gmm.min()
            max_val = pos_loss_gmm.max()
            pos_loss_gmm = (pos_loss_gmm - min_val) / (max_val - min_val + EPS)
        device = pos_inds_gmm.device
        pos_loss_gmm, sort_inds = pos_loss_gmm.sort()
        pos_inds_gmm = pos_inds_gmm[sort_inds]
        pos_loss_gmm = pos_loss_gmm.view(-1, 1).cpu().numpy()
        min_loss, max_loss = pos_loss_gmm.min(), pos_loss_gmm.max()
        means_init = np.array([min_loss, max_loss]).reshape(2, 1)
        weights_init = np.array([0.5, 0.5])
        precisions_init = np.array([1.0, 1.0]).reshape(2, 1, 1)  # full
        if self.covariance_type == 'spherical':
            precisions_init = precisions_init.reshape(2)
        elif self.covariance_type == 'diag':
            precisions_init = precisions_init.reshape(2, 1)
        elif self.covariance_type == 'tied':
            precisions_init = np.array([[1.0]])
        if skm is None:
            raise ImportError('Please run "pip install sklearn" '
                              'to install sklearn first.')
        gmm = skm.GaussianMixture(
            2,
            weights_init=weights_init,
            means_init=means_init,
            precisions_init=precisions_init,
            covariance_type=self.covariance_type)
        try:
            gmm.fit(pos_loss_gmm)
        except:
            print("GMM fitting error")
        gmm_assignment = gmm.predict(pos_loss_gmm)
        scores = gmm.score_samples(pos_loss_gmm)
        gmm_assignment = torch.from_numpy(gmm_assignment).to(device)
        scores = torch.from_numpy(scores).to(device)

        pos_inds_temp, ignore_inds_temp = self.gmm_separation_scheme(
            gmm_assignment, scores, pos_inds_gmm, pos_loss_gmm, gmm)
        return pos_inds_temp, ignore_inds_temp
//  [python]
def plot_ic():

    # Reading the Electrical Impedance Tomography Data
    EIT_data = pd.read_excel('EIT_data.xlsx', sheet_name='Data')
    EIT_features = EIT_data.iloc[:, 2:]
    EIT_label = EIT_data['Class']
    
    # One-Hot encoding as there is several classes
    label_enconder = LabelEncoder()
    EIT_label = label_enconder.fit_transform(EIT_label)

    # Scaler
    scaler = StandardScaler()
    GMM_features = data_reduction_analysis(scaler.fit_transform(EIT_features), EIT_label)

    # Select one from 0-2: ISOMAP, tSNE and PCA
    GMM_features = GMM_features[2]
    GMM_label = EIT_label

    n_components = np.arange(1, 10)
    clfs = [mixture.GaussianMixture(n, max_iter = 1000).fit(GMM_features) for n in n_components]
    bics = [clf.bic(GMM_features) for clf in clfs]
    aics = [clf.aic(GMM_features) for clf in clfs]

    fig = go.Figure(data=go.Scatter(x=n_components, y=bics, mode='lines+markers', name='BIC'))
    fig.add_trace(go.Scatter(x=n_components, y=aics,
                    mode='lines+markers',
                    name='AIC'))

    fig.update_layout(
        title="AIC and BIC over the number of components",
        xaxis_title="AIC / BIC",
        yaxis_title="Number of components",
    )

    return dcc.Graph(figure=fig)
//  [python]
def plot_ic():

    # Reading the Electrical Impedance Tomography Data
    EIT_data = pd.read_excel('EIT_data.xlsx', sheet_name='Data')
    EIT_features = EIT_data.iloc[:, 2:]
    EIT_label = EIT_data['Class']
    
    # One-Hot encoding as there is several classes
    label_enconder = LabelEncoder()
    EIT_label = label_enconder.fit_transform(EIT_label)

    # Scaler
    scaler = StandardScaler()
    GMM_features = data_reduction_analysis(scaler.fit_transform(EIT_features), EIT_label)

    # Select one from 0-2: ISOMAP, tSNE and PCA
    GMM_features = GMM_features[2]
    GMM_label = EIT_label

    n_components = np.arange(1, 10)
    clfs = [GaussianMixture(n, max_iter = 1000).fit(GMM_features) for n in n_components]
    bics = [clf.bic(GMM_features) for clf in clfs]
    aics = [clf.aic(GMM_features) for clf in clfs]

    fig = go.Figure(data=go.Scatter(x=n_components, y=bics, mode='lines+markers', name='BIC'))
    fig.add_trace(go.Scatter(x=n_components, y=aics,
                    mode='lines+markers',
                    name='AIC'))

    fig.update_layout(
        title="AIC and BIC over the number of components",
        xaxis_title="AIC / BIC",
        yaxis_title="Number of components",
    )

    return dcc.Graph(figure=fig)
//  [python]
def get_optimal_clusters(
    embeddings: np.ndarray, max_clusters: int = 50, random_state: int = RANDOM_SEED
) -> int:
    max_clusters = min(max_clusters, len(embeddings))
    n_clusters = np.arange(1, max_clusters)
    bics = []
    for n in n_clusters:
        gm = mixture.GaussianMixture(n_components=n, random_state=random_state)
        gm.fit(embeddings)
        bics.append(gm.bic(embeddings))
    optimal_clusters = n_clusters[np.argmin(bics)]
    return optimal_clusters
//  [python]
def get_optimal_clusters(
    embeddings: np.ndarray, max_clusters: int = 50, random_state: int = RANDOM_SEED
) -> int:
    max_clusters = min(max_clusters, len(embeddings))
    n_clusters = np.arange(1, max_clusters)
    bics = []
    for n in n_clusters:
        gm = GaussianMixture(n_components=n, random_state=random_state)
        gm.fit(embeddings)
        bics.append(gm.bic(embeddings))
    optimal_clusters = n_clusters[np.argmin(bics)]
    return optimal_clusters
//  [python]
def GMM_cluster(embeddings: np.ndarray, threshold: float, random_state: int = 0):
    n_clusters = get_optimal_clusters(embeddings)
    gm = mixture.GaussianMixture(n_components=n_clusters, random_state=random_state)
    gm.fit(embeddings)
    probs = gm.predict_proba(embeddings)
    labels = [np.where(prob > threshold)[0] for prob in probs]
    return labels, n_clusters
//  [python]
def GMM_cluster(embeddings: np.ndarray, threshold: float, random_state: int = 0):
    n_clusters = get_optimal_clusters(embeddings)
    gm = GaussianMixture(n_components=n_clusters, random_state=random_state)
    gm.fit(embeddings)
    probs = gm.predict_proba(embeddings)
    labels = [np.where(prob > threshold)[0] for prob in probs]
    return labels, n_clusters
//  [python]
def plot_data(num_components=6, num_iters = 20):

    # Reading the Electrical Impedance Tomography Data
    EIT_data = pd.read_excel('EIT_data.xlsx', sheet_name='Data')
    EIT_features = EIT_data.iloc[:, 2:]
    EIT_label = EIT_data['Class']
    
    # One-Hot encoding as there is several classes
    label_enconder = LabelEncoder()
    EIT_label = label_enconder.fit_transform(EIT_label)

    # Scaler
    scaler = StandardScaler()
    GMM_features = data_reduction_analysis(scaler.fit_transform(EIT_features), EIT_label)

    # Select one from 0-2: ISOMAP, tSNE and PCA
    GMM_features = GMM_features[2]
    GMM_label = EIT_label

    gmm = mixture.GaussianMixture(n_components=num_components, max_iter=num_iters)

    # Creating the GMM model and fitting with the data
    #gmm = GMM(n_components=num_components, n_iters=num_iters, tol=1e-5, seed=10)
    gmm.fit(GMM_features)
    
    min_x = int(min(GMM_features[:, 0])*1.7)
    max_x = int(max(GMM_features[:, 0])*1.7)
    min_y = int(min(GMM_features[:, 1])*1.7)
    max_y = int(max(GMM_features[:, 1])*1.7)
    
    # Plotting the contours and the results 
    fig2 = contours.plot_contours(GMM_features, gmm.means_, gmm.covariances_, 'Initial clusters', 
                           min_x, max_x, min_y, max_y, GMM_label)

    return dcc.Graph(figure=fig2)
//  [python]
def plot_data(num_components=6, num_iters = 20):

    # Reading the Electrical Impedance Tomography Data
    EIT_data = pd.read_excel('EIT_data.xlsx', sheet_name='Data')
    EIT_features = EIT_data.iloc[:, 2:]
    EIT_label = EIT_data['Class']
    
    # One-Hot encoding as there is several classes
    label_enconder = LabelEncoder()
    EIT_label = label_enconder.fit_transform(EIT_label)

    # Scaler
    scaler = StandardScaler()
    GMM_features = data_reduction_analysis(scaler.fit_transform(EIT_features), EIT_label)

    # Select one from 0-2: ISOMAP, tSNE and PCA
    GMM_features = GMM_features[2]
    GMM_label = EIT_label

    gmm = GaussianMixture(n_components=num_components, max_iter=num_iters)

    # Creating the GMM model and fitting with the data
    #gmm = GMM(n_components=num_components, n_iters=num_iters, tol=1e-5, seed=10)
    gmm.fit(GMM_features)
    
    min_x = int(min(GMM_features[:, 0])*1.7)
    max_x = int(max(GMM_features[:, 0])*1.7)
    min_y = int(min(GMM_features[:, 1])*1.7)
    max_y = int(max(GMM_features[:, 1])*1.7)
    
    # Plotting the contours and the results 
    fig2 = contours.plot_contours(GMM_features, gmm.means_, gmm.covariances_, 'Initial clusters', 
                           min_x, max_x, min_y, max_y, GMM_label)

    return dcc.Graph(figure=fig2)
//  [python]
def detect_swich(tracklet, sim_thresh):
    feat = torch.stack(tracklet.features)
    cluster = mixture.GaussianMixture(n_components=2, covariance_type="full", random_state=0)
    cluster.fit(feat)
    _cos = distance.cosine(cluster.means_[0], cluster.means_[1])

    print(tracklet.track_id, _cos)
    if _cos < sim_thresh:
        return (_cos, [])

    cluster_labels = cluster.predict(feat)
    return [_cos, cluster_labels]
//  [python]
def detect_swich(tracklet, sim_thresh):
    feat = torch.stack(tracklet.features)
    cluster = GaussianMixture(n_components=2, covariance_type="full", random_state=0)
    cluster.fit(feat)
    _cos = distance.cosine(cluster.means_[0], cluster.means_[1])

    print(tracklet.track_id, _cos)
    if _cos < sim_thresh:
        return (_cos, [])

    cluster_labels = cluster.predict(feat)
    return [_cos, cluster_labels]
//  [python]
def fit_conditional_parameters(self, i):
        class_wise_scores = self.get_class_wise_scores(i)

        class_wise_parameters = dict()
        for label in self._labels:
            gmm = mixture.GaussianMixture(n_components=1)
            gmm.fit(class_wise_scores[label].reshape(-1, 1))

            class_wise_parameters[label] = \
                self.Gaussian(mu=gmm.means_.flatten()[0],
                              std=np.sqrt(gmm.covariances_.flatten()[0]))

        return class_wise_parameters
//  [python]
def fit_conditional_parameters(self, i):
        class_wise_scores = self.get_class_wise_scores(i)

        class_wise_parameters = dict()
        for label in self._labels:
            gmm = GaussianMixture(n_components=1)
            gmm.fit(class_wise_scores[label].reshape(-1, 1))

            class_wise_parameters[label] = \
                self.Gaussian(mu=gmm.means_.flatten()[0],
                              std=np.sqrt(gmm.covariances_.flatten()[0]))

        return class_wise_parameters
//  [python]
def gmm(nclusters, coords, n_init=50, n_iter=500):
        if USE_GAUSSIAN_MIXTURE:
            est = mixture.GaussianMixture(n_components=nclusters, n_init=n_init, max_iter=n_iter)
        else:
            est = mixture.GMM(n_components=nclusters, n_init=n_init, n_iter=n_iter)
        est.fit(coords)
        return Partition(est.predict(coords))
//  [python]
def gmm(nclusters, coords, n_init=50, n_iter=500):
        if USE_GAUSSIAN_MIXTURE:
            est = GaussianMixture(n_components=nclusters, n_init=n_init, max_iter=n_iter)
        else:
            est = GMM(n_components=nclusters, n_init=n_init, n_iter=n_iter)
        est.fit(coords)
        return Partition(est.predict(coords))
//  [python]
def initialize_GaussianKernel(points, N,covariance_type='diag',reg_covar=1e-1,n_init=10):
    GMM = mixture.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