jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def CAPTCHA_to_data(filename):
    '''
    convert CAPTCHA image to 7 chinese character image data.
    kind of slow because of GMM iteration.
    return a 7 * 40 * 40 array
    '''
    
    width=400
    height=88
    padding=20
    padding_color = 249
    
    captcha = Image.open(filename)

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

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

    return data, new_indexes
//  [python]
def get_multimodality_score(means, sigmas, weights):
    means_stacked = np.concatenate(means, axis=0)[:, :, 0, 0]
    sigmas_stacked = np.concatenate(sigmas, axis=0)[:, :, 0, 0]
    weights_stacked = np.concatenate(weights, axis=0)[:, 0, 0, 0]
    gmm = mixture.GaussianMixture(n_components=4, covariance_type='diag')
    vars = 2 * sigmas_stacked * sigmas_stacked
    precisions_cholesky = _compute_precision_cholesky(vars, 'diag')
    gmm.weights_ = weights_stacked
    gmm.means_ = means_stacked
    gmm.precisions_cholesky_ = precisions_cholesky
    gmm.covariances_ = vars
    gmm_uni = mixture.GaussianMixture(n_components=1, covariance_type='diag')
    argmax = np.argmax(gmm.weights_)
    gmm_uni.means_ = gmm.means_[argmax, :].reshape([1, 2])
    gmm_uni.covariances_ = gmm.covariances_[argmax, :].reshape([1, 2])
    gmm_uni.precisions_cholesky_ = gmm.precisions_cholesky_[argmax, :].reshape([1, 2])
    gmm_uni.weights_ = np.array([1]).reshape([1])
    Z_uni = compute_histogram_gmm(gmm_uni)
    Z = compute_histogram_gmm(gmm)
    ratio = computeWEMD(Z, Z_uni)
    return ratio
//  [python]
def get_multimodality_score(means, sigmas, weights):
    means_stacked = np.concatenate(means, axis=0)[:, :, 0, 0]
    sigmas_stacked = np.concatenate(sigmas, axis=0)[:, :, 0, 0]
    weights_stacked = np.concatenate(weights, axis=0)[:, 0, 0, 0]
    gmm = GaussianMixture(n_components=4, covariance_type='diag')
    vars = 2 * sigmas_stacked * sigmas_stacked
    precisions_cholesky = _compute_precision_cholesky(vars, 'diag')
    gmm.weights_ = weights_stacked
    gmm.means_ = means_stacked
    gmm.precisions_cholesky_ = precisions_cholesky
    gmm.covariances_ = vars
    gmm_uni = GaussianMixture(n_components=1, covariance_type='diag')
    argmax = np.argmax(gmm.weights_)
    gmm_uni.means_ = gmm.means_[argmax, :].reshape([1, 2])
    gmm_uni.covariances_ = gmm.covariances_[argmax, :].reshape([1, 2])
    gmm_uni.precisions_cholesky_ = gmm.precisions_cholesky_[argmax, :].reshape([1, 2])
    gmm_uni.weights_ = np.array([1]).reshape([1])
    Z_uni = compute_histogram_gmm(gmm_uni)
    Z = compute_histogram_gmm(gmm)
    ratio = computeWEMD(Z, Z_uni)
    return ratio
//  [python]
def cluster_fits_gmm(self, n_clusters, plot=False):

        std = std(self.fits[2], axis=0)
        whitened = (self.fits[2] / std).reshape(-1, 1)

        gmm = mixture.GaussianMixture(n_components=n_clusters, covariance_type='full').fit(whitened)
        gmm.means_ *= std

        order = argsort(gmm.means_[:, 0])
        weights = gmm.weights_[order]
        means = gmm.means_[order, :]
        covariances = gmm.covariances_[order, :, :]

        cF.set_gmm(weights, means, covariances)
        cF.save_gmm(gmm, "gmm_{}_clusters.h5".format(gmm.n_components))

        if plot:
            bins = StatArray.StatArray(linspace(self.fits[2].min(), self.fits[2].max(), 200))
            binCentres = bins.internalEdges()
            x_predict = binCentres
            x_predict = x_predict.reshape(-1, 1)

            logprob = gmm.score_samples(x_predict)
            responsibilities = gmm.predict_proba(x_predict)
            pdf = exp(logprob)
            pdf_individual = responsibilities * pdf[:, newaxis]

            h = Histogram1D(edges = bins)
            h.update(self.fits[2])

            h._counts = h._counts / max(h._counts)
            h.plot(alpha=0.4, linewidth=0)

            for i in range(gmm.n_components):
                plt.plot(binCentres, pdf_individual[:, i], '--k', linewidth=1)

        return gmm
//  [python]
def cluster_fits_gmm(self, n_clusters, plot=False):

        std = std(self.fits[2], axis=0)
        whitened = (self.fits[2] / std).reshape(-1, 1)

        gmm = GaussianMixture(n_components=n_clusters, covariance_type='full').fit(whitened)
        gmm.means_ *= std

        order = argsort(gmm.means_[:, 0])
        weights = gmm.weights_[order]
        means = gmm.means_[order, :]
        covariances = gmm.covariances_[order, :, :]

        cF.set_gmm(weights, means, covariances)
        cF.save_gmm(gmm, "gmm_{}_clusters.h5".format(gmm.n_components))

        if plot:
            bins = StatArray.StatArray(linspace(self.fits[2].min(), self.fits[2].max(), 200))
            binCentres = bins.internalEdges()
            x_predict = binCentres
            x_predict = x_predict.reshape(-1, 1)

            logprob = gmm.score_samples(x_predict)
            responsibilities = gmm.predict_proba(x_predict)
            pdf = exp(logprob)
            pdf_individual = responsibilities * pdf[:, newaxis]

            h = Histogram1D(edges = bins)
            h.update(self.fits[2])

            h._counts = h._counts / max(h._counts)
            h.plot(alpha=0.4, linewidth=0)

            for i in range(gmm.n_components):
                plt.plot(binCentres, pdf_individual[:, i], '--k', linewidth=1)

        return gmm
//  [python]
def gmm_cluster(self, show_plot=True, n_clusters=5,
                    features=['energy', 'liveness', 'tempo_0_1', 'speechiness', 
                              'acousticness', 'instrumentalness', 'danceability', 
                              'loudness_0_1', 'valence']):
        
        self.check_features(features) # Make sure features are valid/generated already if applicable
        print('Starting GMM clustering with {} clusters and features = {}'.format(n_clusters, features))
        
        X = self.data[features]        
        gm = mixture.GaussianMixture(n_components=n_clusters, covariance_type='full', max_iter=300, n_init=5)
        gm.fit(X)
        Y = gm.predict(X)
        self.data['GMM'] = Y

        if show_plot:
            self.plot_clusters(features=features, algorithm='GMM')
//  [python]
def gmm_cluster(self, show_plot=True, n_clusters=5,
                    features=['energy', 'liveness', 'tempo_0_1', 'speechiness', 
                              'acousticness', 'instrumentalness', 'danceability', 
                              'loudness_0_1', 'valence']):
        
        self.check_features(features) # Make sure features are valid/generated already if applicable
        print('Starting GMM clustering with {} clusters and features = {}'.format(n_clusters, features))
        
        X = self.data[features]        
        gm = GaussianMixture(n_components=n_clusters, covariance_type='full', max_iter=300, n_init=5)
        gm.fit(X)
        Y = gm.predict(X)
        self.data['GMM'] = Y

        if show_plot:
            self.plot_clusters(features=features, algorithm='GMM')
//  [python]
def test_graph2vec(default_plugin_resolver):

    dpr = default_plugin_resolver
    complete_graphs = [
        nx.complete_graph(np.arange(i * 30, i * 30 + 20)) for i in range(12)
    ]
    path_graphs = [nx.path_graph(10 * (i + 1)) for i in range(12)]
    random_graphs = [nx.connected_watts_strogatz_graph(30, 10, 0.5) for i in range(12)]
    nx_graphs = complete_graphs + path_graphs + random_graphs
    graphs = list(map(dpr.wrappers.Graph.NetworkXGraph, nx_graphs))
    subgraph_degree = 10
    embedding_size = 12
    epochs = 10
    learning_rate = 5e-2

    def cmp_func(matrix):
        gmm = mixture.GaussianMixture(3)
        predicted_labels = gmm.fit_predict(matrix)

        complete_graph_labels = predicted_labels[0 : len(complete_graphs)]
        path_graph_labels = predicted_labels[
            len(complete_graphs) : len(complete_graphs) + len(random_graphs)
        ]
        random_graph_labels = predicted_labels[
            len(complete_graphs) + len(random_graphs) :
        ]

        assert len(np.unique(complete_graph_labels)) == 1
        assert len(np.unique(path_graph_labels)) == 1
        assert len(np.unique(random_graph_labels)) == 1

        complete_graph_label = complete_graph_labels[0]
        path_graph_label = path_graph_labels[0]
        random_graph_label = random_graph_labels[0]

        assert complete_graph_label != path_graph_label != random_graph_label

    MultiVerify(dpr).compute(
        "embedding.train.graph2vec",
        graphs,
        subgraph_degree,
        embedding_size,
        epochs,
        learning_rate,
    ).normalize(dpr.types.Matrix.NumpyMatrixType).custom_compare(cmp_func)
//  [python]
def test_graph2vec(default_plugin_resolver):

    dpr = default_plugin_resolver
    complete_graphs = [
        nx.complete_graph(np.arange(i * 30, i * 30 + 20)) for i in range(12)
    ]
    path_graphs = [nx.path_graph(10 * (i + 1)) for i in range(12)]
    random_graphs = [nx.connected_watts_strogatz_graph(30, 10, 0.5) for i in range(12)]
    nx_graphs = complete_graphs + path_graphs + random_graphs
    graphs = list(map(dpr.wrappers.Graph.NetworkXGraph, nx_graphs))
    subgraph_degree = 10
    embedding_size = 12
    epochs = 10
    learning_rate = 5e-2

    def cmp_func(matrix):
        gmm = GaussianMixture(3)
        predicted_labels = gmm.fit_predict(matrix)

        complete_graph_labels = predicted_labels[0 : len(complete_graphs)]
        path_graph_labels = predicted_labels[
            len(complete_graphs) : len(complete_graphs) + len(random_graphs)
        ]
        random_graph_labels = predicted_labels[
            len(complete_graphs) + len(random_graphs) :
        ]

        assert len(np.unique(complete_graph_labels)) == 1
        assert len(np.unique(path_graph_labels)) == 1
        assert len(np.unique(random_graph_labels)) == 1

        complete_graph_label = complete_graph_labels[0]
        path_graph_label = path_graph_labels[0]
        random_graph_label = random_graph_labels[0]

        assert complete_graph_label != path_graph_label != random_graph_label

    MultiVerify(dpr).compute(
        "embedding.train.graph2vec",
        graphs,
        subgraph_degree,
        embedding_size,
        epochs,
        learning_rate,
    ).normalize(dpr.types.Matrix.NumpyMatrixType).custom_compare(cmp_func)
//  [python]
def benchmark_gmm_cluster(self, show_plot=True, 
                              features=['energy', 'liveness', 'tempo_0_1', 'speechiness', 
                                        'acousticness', 'instrumentalness', 'danceability', 
                                        'loudness_0_1', 'valence']):
        self.check_features(features) # Make sure features are valid/generated already if applicable
        print('Benchmarking GMM clustering with features = {}'.format(features))
        
        X = self.data[features]        
        best_score = None  
        best_n = None
        
        for n in range(2,15): # Find the optimal number of components
            gm = mixture.GaussianMixture(n_components=n, covariance_type='full', max_iter=300, n_init=5)
            gm.fit(X)
            score = gm.bic(X)
            if best_score == None or score < best_score:
                best_score = score
                best_n = n
                best_gm = gm
        
        print('Best Score: {} with N = {}'.format(best_score, best_n))
        Y = best_gm.predict(X)
        self.data['GMM'] = Y

        if show_plot:
            self.plot_clusters(features=features, algorithm='GMM')
//  [python]
def benchmark_gmm_cluster(self, show_plot=True, 
                              features=['energy', 'liveness', 'tempo_0_1', 'speechiness', 
                                        'acousticness', 'instrumentalness', 'danceability', 
                                        'loudness_0_1', 'valence']):
        self.check_features(features) # Make sure features are valid/generated already if applicable
        print('Benchmarking GMM clustering with features = {}'.format(features))
        
        X = self.data[features]        
        best_score = None  
        best_n = None
        
        for n in range(2,15): # Find the optimal number of components
            gm = GaussianMixture(n_components=n, covariance_type='full', max_iter=300, n_init=5)
            gm.fit(X)
            score = gm.bic(X)
            if best_score == None or score < best_score:
                best_score = score
                best_n = n
                best_gm = gm
        
        print('Best Score: {} with N = {}'.format(best_score, best_n))
        Y = best_gm.predict(X)
        self.data['GMM'] = Y

        if show_plot:
            self.plot_clusters(features=features, algorithm='GMM')
//  [python]
def AIC_BIC(X):
    n_components = np.arange(1, 10)
    clfs = [mixture.GaussianMixture(n, max_iter=1000).fit(X) for n in n_components]
    bics = [clf.bic(X) for clf in clfs]
    aics = [clf.aic(X) for clf in clfs]

    plt.plot(n_components, bics, label='BIC')
    plt.plot(n_components, aics, label='AIC')
    plt.xlabel('n_components')
    plt.legend()
    plt.show()
//  [python]
def AIC_BIC(X):
    n_components = np.arange(1, 10)
    clfs = [GaussianMixture(n, max_iter=1000).fit(X) for n in n_components]
    bics = [clf.bic(X) for clf in clfs]
    aics = [clf.aic(X) for clf in clfs]

    plt.plot(n_components, bics, label='BIC')
    plt.plot(n_components, aics, label='AIC')
    plt.xlabel('n_components')
    plt.legend()
    plt.show()
//  [python]
def GMM(self):
        assert self.incomplete_experiment is False
        """
        :param label: Which label(class) to be explained
        :param k: Number of nearest neighbors from the centroid
        :param cluster_index : A Cluster index which we want to see
        :return: list of the nearest nodes
        """
        self.model.eval()
        with torch.no_grad():
            self.model(self.data[0])
        embeddings = torch.empty((1, self.model.embs.shape[1]))
        ys = []
        for part_d in self.data:
            with torch.no_grad():
                _ = self.model(part_d)
            embeddings = torch.vstack([embeddings, self.model.embs])
            ys.append(part_d.y.item())
        entire_embs = embeddings[1:, :].detach().numpy()
        self.y1 = np.where(np.array(ys) == 1.0)[0]
        self.y0 = np.where(np.array(ys) == 0.0)[0]
        self.emb1 = entire_embs[self.y1, :]
        self.emb0 = entire_embs[self.y0, :]
        self.clus1 = mixture.GaussianMixture(
            n_components=self.n_components, random_state=0
        ).fit(self.emb1)
        self.clus0 = mixture.GaussianMixture(
            n_components=self.n_components, random_state=0
        ).fit(self.emb0)
//  [python]
def GMM(self):
        assert self.incomplete_experiment is False
        """
        :param label: Which label(class) to be explained
        :param k: Number of nearest neighbors from the centroid
        :param cluster_index : A Cluster index which we want to see
        :return: list of the nearest nodes
        """
        self.model.eval()
        with torch.no_grad():
            self.model(self.data[0])
        embeddings = torch.empty((1, self.model.embs.shape[1]))
        ys = []
        for part_d in self.data:
            with torch.no_grad():
                _ = self.model(part_d)
            embeddings = torch.vstack([embeddings, self.model.embs])
            ys.append(part_d.y.item())
        entire_embs = embeddings[1:, :].detach().numpy()
        self.y1 = np.where(np.array(ys) == 1.0)[0]
        self.y0 = np.where(np.array(ys) == 0.0)[0]
        self.emb1 = entire_embs[self.y1, :]
        self.emb0 = entire_embs[self.y0, :]
        self.clus1 = GaussianMixture(
            n_components=self.n_components, random_state=0
        ).fit(self.emb1)
        self.clus0 = GaussianMixture(
            n_components=self.n_components, random_state=0
        ).fit(self.emb0)
//  [python]
def em_fit_and_predict(xs, num_modes):
  return s.mixture.GaussianMixture(
      n_components=num_modes,
      covariance_type="full",
      init_params="kmeans",
      n_init=3).fit_predict(xs)
//  [python]
def em_fit_and_predict(xs, num_modes):
  return sklearn.mixture.GaussianMixture(
      n_components=num_modes,
      covariance_type="full",
      init_params="kmeans",
      n_init=3).fit_predict(xs)
//  [python]
def setUp(self):
        """
        Set up each test with a new XDGMM object and some data.
        """
        self.xdgmm = XDGMM(n_components=3)
        self.files=[]
        
        """
        Use scikit-learn GaussianMixture for sampling some data points
        """
        self.gmm = skl_GMM(n_components=3, max_iter=10,
                           covariance_type='full',
                           random_state=None)
        self.gmm.weights_=np.array([0.3,0.5,0.2])
        self.gmm.means_=np.array([np.array([0,1]),np.array([5,4]),
                                  np.array([2,4])])
        self.gmm.covariances_=np.array([np.diag((2,1)),
                                        np.array([[1,0.2],[0.2,1]]),
                                        np.diag((0.3,0.5))])
        
        self.gmm.precisions_=np.linalg.inv(self.gmm.covariances_)
        self.gmm.precisions_cholesky_= np.linalg.cholesky(self.gmm.precisions_)
        
        self.X=self.gmm.sample(1000)[0]
        errs=0.2*np.random.random_sample((1000,2))
        self.Xerr = np.zeros(self.X.shape + self.X.shape[-1:])
        diag = np.arange(self.X.shape[-1])
        self.Xerr[:, diag, diag] = np.vstack([errs[:,0]**2, errs[:,1]**2]).T
//  [python]
def find_gaussian_clusters(self):
		self.gaussian = mixture.GaussianMixture(n_components=4).fit(self.X)
//  [python]
def find_gaussian_clusters(self):
		self.gaussian = GaussianMixture(n_components=4).fit(self.X)
//  [python]
def clusterize(points, n_components=2, covariance_type='tied',
               centers=None, weights=None, output=None, random_state=1000):
    if centers is not None:
        n_components = len(centers)

    if output is None:
        output = points

    if len(points) < 2:
        return [list(output)]

    gmm = mixture.GaussianMixture(n_components=n_components,
                          covariance_type=covariance_type,
                          means_init=centers,
                          weights_init=weights,
                          random_state=random_state)
    gmm.fit(points)
    labels = gmm.predict(points)

    clusters = defaultdict(list)
    for label, point in zip(labels, output):
        clusters[label].append(point)

    return sorted(clusters.values(), key=lambda x: len(x), reverse=True)
//  [python]
def clusterize(points, n_components=2, covariance_type='tied',
               centers=None, weights=None, output=None, random_state=1000):
    if centers is not None:
        n_components = len(centers)

    if output is None:
        output = points

    if len(points) < 2:
        return [list(output)]

    gmm = GaussianMixture(n_components=n_components,
                          covariance_type=covariance_type,
                          means_init=centers,
                          weights_init=weights,
                          random_state=random_state)
    gmm.fit(points)
    labels = gmm.predict(points)

    clusters = defaultdict(list)
    for label, point in zip(labels, output):
        clusters[label].append(point)

    return sorted(clusters.values(), key=lambda x: len(x), reverse=True)
//  [python]
def gensamples(datain,targetin,
   ncomponents=2,
   nsamples=10,
   kind='r',
   func=None):
    """
    Generate data using GMM.

    Reads in features array and target column
    uses GMM to generate samples after scaling target col
    scales target col back to original values
    saves data to csv file
    returns augmented data features array and target col

    newfeatures,newtarget =gensamples(wf,codecol,kind='c',func='cbr')
    """
    d0 = datain
    t0 = targetin
    d0t0 = np.concatenate((d0,t0.values.reshape(1,-1).T),axis=1)
    sc = StandardScaler()
    t0min,t0max = t0.min(),t0.max()
    t0s = sc.fit_transform(t0.values.reshape(1,-1))
    d0t0s = np.concatenate((d0,t0s.T),axis=1)
    gmm = mixture.GMM(n_components=ncomponents,covariance_type='spherical', n_iter=500, random_state=0)
    gmm.fit(d0t0s)
    d0sampled = gmm.sample(nsamples)[0]
    d1sampled = d0sampled[:,:-1]
    targetunscaled = d0sampled[:,-1].reshape(1,-1)
    scmm = MinMaxScaler((t0min,t0max))
    if kind == 'c':
        targetscaledback = np.floor(scmm.fit_transform(targetunscaled.T))
    else:
        targetscaledback = scmm.fit_transform(targetunscaled.T)
    d1t1 = np.concatenate((d1sampled,targetscaledback),axis=1)
    d1 = np.concatenate((d0t0,d1t1))
    print(d1.shape)
    fname = 'gensamples_' + func + '.csv'
    np.savetxt(fname,d1,fmt='%.3f',delimiter=',')
    return d1[:,:-1],d1[:,-1]
//  [python]
def gensamples(datain,targetin,
   ncomponents=2,
   nsamples=10,
   kind='r',
   func=None):
    """
    Generate data using GMM.

    Reads in features array and target column
    uses GMM to generate samples after scaling target col
    scales target col back to original values
    saves data to csv file
    returns augmented data features array and target col

    newfeatures,newtarget =gensamples(wf,codecol,kind='c',func='cbr')
    """
    d0 = datain
    t0 = targetin
    d0t0 = np.concatenate((d0,t0.values.reshape(1,-1).T),axis=1)
    sc = StandardScaler()
    t0min,t0max = t0.min(),t0.max()
    t0s = sc.fit_transform(t0.values.reshape(1,-1))
    d0t0s = np.concatenate((d0,t0s.T),axis=1)
    gmm = mixture.GaussianMixture(n_components=ncomponents,covariance_type='spherical', max_iter=500, random_state=0)
    gmm.fit(d0t0s)
    d0sampled = gmm.sample(nsamples)[0]
    d1sampled = d0sampled[:,:-1]
    targetunscaled = d0sampled[:,-1].reshape(1,-1)
    scmm = MinMaxScaler((t0min,t0max))
    if kind == 'c':
        targetscaledback = np.floor(scmm.fit_transform(targetunscaled.T))
    else:
        targetscaledback = scmm.fit_transform(targetunscaled.T)
    d1t1 = np.concatenate((d1sampled,targetscaledback),axis=1)
    d1 = np.concatenate((d0t0,d1t1))
    print(d1.shape)
    fname = 'gensamples_' + func + '.csv'
    np.savetxt(fname,d1,fmt='%.3f',delimiter=',')
    return d1[:,:-1],d1[:,-1]
//  [python]
def gaussian_mixture_model(
        embedding: np.ndarray,
        num_clusters: int = 1,
        seed: int = None,
) -> np.ndarray:
    """
    Performs gaussian mixture model clustering on the feature_matrix.

    :param numpy.ndarray embedding: An n x d feature matrix; it is assumed that the d features are ordered
    :param int num_clusters: How many clusters to look at between min_clusters and max_clusters, default 1
    :param Optional[int] seed: The seed for numpy random, default None
    :return:  The cluster labels for each vector in the given embedding. The vector at index n in the embedding will
        have the label at index n in this returned array
    :rtype: np.ndarray
    """
    np.random.seed(seed)

    classifier = mixture.GaussianMixture(
        n_components=num_clusters,
        covariance_type='spherical'
    )
    classifier.fit(embedding)

    predictions = classifier.predict(embedding)
    predictions = np.array([int(i) for i in predictions])

    return predictions
//  [python]
def gaussian_mixture_model(
        embedding: np.ndarray,
        num_clusters: int = 1,
        seed: int = None,
) -> np.ndarray:
    """
    Performs gaussian mixture model clustering on the feature_matrix.

    :param numpy.ndarray embedding: An n x d feature matrix; it is assumed that the d features are ordered
    :param int num_clusters: How many clusters to look at between min_clusters and max_clusters, default 1
    :param Optional[int] seed: The seed for numpy random, default None
    :return:  The cluster labels for each vector in the given embedding. The vector at index n in the embedding will
        have the label at index n in this returned array
    :rtype: np.ndarray
    """
    np.random.seed(seed)

    classifier = GaussianMixture(
        n_components=num_clusters,
        covariance_type='spherical'
    )
    classifier.fit(embedding)

    predictions = classifier.predict(embedding)
    predictions = np.array([int(i) for i in predictions])

    return predictions
//  [python]
def eval_train(epoch,model):
    model.eval()
    num_samples = args.num_batches*args.batch_size
    losses = torch.zeros(num_samples)
    paths = []
    n=0
    with torch.no_grad():
        for batch_idx, (inputs, targets, path) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            outputs = model(inputs) 
            loss = CE(outputs, targets)  
            for b in range(inputs.size(0)):
                losses[n]=loss[b] 
                paths.append(path[b])
                n+=1
            sys.stdout.write('\r')
            sys.stdout.write('| Evaluating loss Iter %3d\t' %(batch_idx)) 
            sys.stdout.flush()
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    losses = losses.reshape(-1,1)
    gmm = mixture.GaussianMixture(n_components=2,max_iter=10,reg_covar=5e-4,tol=1e-2)
    gmm.fit(losses)
    prob = gmm.predict_proba(losses) 
    prob = prob[:,gmm.means_.argmin()]       
    return prob,paths
//  [python]
def eval_train(epoch,model):
    model.eval()
    num_samples = args.num_batches*args.batch_size
    losses = torch.zeros(num_samples)
    paths = []
    n=0
    with torch.no_grad():
        for batch_idx, (inputs, targets, path) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            outputs = model(inputs) 
            loss = CE(outputs, targets)  
            for b in range(inputs.size(0)):
                losses[n]=loss[b] 
                paths.append(path[b])
                n+=1
            sys.stdout.write('\r')
            sys.stdout.write('| Evaluating loss Iter %3d\t' %(batch_idx)) 
            sys.stdout.flush()
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    losses = losses.reshape(-1,1)
    gmm = GaussianMixture(n_components=2,max_iter=10,reg_covar=5e-4,tol=1e-2)
    gmm.fit(losses)
    prob = gmm.predict_proba(losses) 
    prob = prob[:,gmm.means_.argmin()]       
    return prob,paths
//  [python]
def pca_gmm_and_dip(data, min_data):
    
    if data.shape[0] < min_data:
        return np.zeros(len(data), 'int32'), 1

    pca = PCA(n_components=1)
    score = pca.fit_transform(data)

    label = np.zeros(len(score), 'int32')

    p_val = dp(score[:, 0])[1]
    if p_val < 0.05:
        g = mixture.GMM(n_components=2)
        label_ = g.fit_predict(score)
        idx_0 = label_==0
        if np.sum(idx_0) == 0 or np.sum(~idx_0) == 0:
            return label, 1
        else:
            return label_, p_val

        label_0 = pca_gmm_and_dip(data[idx_0], min_data)[0]
        label_1 = pca_gmm_and_dip(data[~idx_0], min_data)[0]
        label_1 += np.max(label_0) + 1
        label[idx_0] = label_0
        label[~idx_0] = label_1

    return label, p_val
//  [python]
def pca_gmm_and_dip(data, min_data):
    
    if data.shape[0] < min_data:
        return np.zeros(len(data), 'int32'), 1

    pca = PCA(n_components=1)
    score = pca.fit_transform(data)

    label = np.zeros(len(score), 'int32')

    p_val = dp(score[:, 0])[1]
    if p_val < 0.05:
        g = GaussianMixture(n_components=2)
        label_ = g.fit_predict(score)
        idx_0 = label_==0
        if np.sum(idx_0) == 0 or np.sum(~idx_0) == 0:
            return label, 1
        else:
            return label_, p_val

        label_0 = pca_gmm_and_dip(data[idx_0], min_data)[0]
        label_1 = pca_gmm_and_dip(data[~idx_0], min_data)[0]
        label_1 += np.max(label_0) + 1
        label[idx_0] = label_0
        label[~idx_0] = label_1

    return label, p_val
//  [python]
def test_baseadapter__get_fit_signature(self):
        lm = LinearRegression()
        gm = GaussianMixture()
        lm.__class__ = type('newClass', (type(lm), FitPredictMixin), {} )
        gm.__class__ = type('newClass', (type(gm), FitPredictMixin), {} )

        result_lm = lm._get_fit_signature()
        result_gm = gm._get_fit_signature()

        self.assertEqual(sorted(result_lm), sorted(['X', 'y', 'sample_weight']))
        self.assertEqual(sorted(result_gm), sorted(['X', 'y']))
//  [python]
def cluster_gmm(self, acc_matrix, curr_iter):
        self.train_data_weights[curr_iter] = np.zeros((self.model_num, self.client_num))

        gm = mixture.GaussianMixture(n_components=2, random_state=0).fit(acc_matrix.transpose())
        probs = gm.predict_proba(acc_matrix.transpose()).transpose()
        
        # heuristic for mapping cluster to model id
        if gm.means_[0][0] > gm.means_[0][1]:
            self.train_data_weights[curr_iter][0] = probs[0]
            self.train_data_weights[curr_iter][1] = probs[1]
        else:
            self.train_data_weights[curr_iter][0] = probs[1]
            self.train_data_weights[curr_iter][1] = probs[0]
//  [python]
def cluster_gmm(self, acc_matrix, curr_iter):
        self.train_data_weights[curr_iter] = np.zeros((self.model_num, self.client_num))

        gm = GaussianMixture(n_components=2, random_state=0).fit(acc_matrix.transpose())
        probs = gm.predict_proba(acc_matrix.transpose()).transpose()
        
        # heuristic for mapping cluster to model id
        if gm.means_[0][0] > gm.means_[0][1]:
            self.train_data_weights[curr_iter][0] = probs[0]
            self.train_data_weights[curr_iter][1] = probs[1]
        else:
            self.train_data_weights[curr_iter][0] = probs[1]
            self.train_data_weights[curr_iter][1] = probs[0]
//  [python]
def test_infer_input_output_cols(self) -> None:
        test_df = pd.DataFrame({"COL_1": [1, 2, 3], "COL_2": [4, 5, 6], "COL_3": [10, 11, 12]})

        class TestTransformer(BaseTransformer):
            def __init__(self) -> None:
                super().__init__()
                self._sklearn_object: Optional[Any] = None

            def _fit(self, dataset: DataFrame) -> "TestTransformer":
                return self

        with self.subTest("Test with regressor"):
            estimator = TestTransformer()
            estimator._sklearn_object = XGBRegressor()

            estimator.set_input_cols(["COL_1", "COL_2"])
            estimator.set_label_cols("COL_3")

            estimator._infer_input_output_cols(dataset=test_df)

            self.assertEqual(estimator.output_cols, ["OUTPUT_COL_3"])

        with self.subTest("Test with unsupervised model"):
            estimator = TestTransformer()
            estimator._sklearn_object = mixture.GaussianMixture()

            estimator.set_input_cols(["COL_1", "COL_2"])
            estimator._infer_input_output_cols(dataset=test_df)

            self.assertEqual(estimator.output_cols, ["OUTPUT_0"])

        # Users must specify transformer output columns explicitly.
        with self.subTest("Test transformer raises `ValueError`"):
            with self.assertRaises(SnowflakeMLException):
                estimator = TestTransformer()
                estimator._sklearn_object = SimpleImputer()

                estimator.set_input_cols(["COL_1", "COL_2"])
                estimator._infer_input_output_cols(dataset=test_df)
//  [python]
def test_infer_input_output_cols(self) -> None:
        test_df = pd.DataFrame({"COL_1": [1, 2, 3], "COL_2": [4, 5, 6], "COL_3": [10, 11, 12]})

        class TestTransformer(BaseTransformer):
            def __init__(self) -> None:
                super().__init__()
                self._sklearn_object: Optional[Any] = None

            def _fit(self, dataset: DataFrame) -> "TestTransformer":
                return self

        with self.subTest("Test with regressor"):
            estimator = TestTransformer()
            estimator._sklearn_object = XGBRegressor()

            estimator.set_input_cols(["COL_1", "COL_2"])
            estimator.set_label_cols("COL_3")

            estimator._infer_input_output_cols(dataset=test_df)

            self.assertEqual(estimator.output_cols, ["OUTPUT_COL_3"])

        with self.subTest("Test with unsupervised model"):
            estimator = TestTransformer()
            estimator._sklearn_object = GaussianMixture()

            estimator.set_input_cols(["COL_1", "COL_2"])
            estimator._infer_input_output_cols(dataset=test_df)

            self.assertEqual(estimator.output_cols, ["OUTPUT_0"])

        # Users must specify transformer output columns explicitly.
        with self.subTest("Test transformer raises `ValueError`"):
            with self.assertRaises(SnowflakeMLException):
                estimator = TestTransformer()
                estimator._sklearn_object = SimpleImputer()

                estimator.set_input_cols(["COL_1", "COL_2"])
                estimator._infer_input_output_cols(dataset=test_df)
//  [python]
def sample_code_multimodal(self, gt_code_list, num_samples):
        code_list = []
        class_code = gt_code_list 
        class_code = np.unique(class_code, axis = 0)
        s_pre = -1
        K = np.arange(3,10)
        for i, k in enumerate(K):
            model = mixture.GaussianMixture(n_components=k, n_init=5, init_params='kmeans', random_state=0)
            labels = model.fit_predict(class_code)
            s = metrics.silhouette_score(class_code, labels, metric='euclidean')
            if s < s_pre * 0.95:
                break
            else:
                num_multimodality = k 
            s_pre = s 
        gm = mixture.GaussianMixture(n_components=num_multimodality, n_init=5, init_params='kmeans', random_state=0).fit(class_code)
        prediction = gm.predict(class_code)
        _, num_candidate_samples = np.unique(prediction, return_counts=True)
        th = 10
        noisy_cluster_index = np.where(num_candidate_samples <= th)[0]
        good_cluster_index = np.where(num_candidate_samples > th)[0]
        num_candidate_samples[noisy_cluster_index] = 0
        num_candidate_samples = (num_candidate_samples/sum(num_candidate_samples) * num_samples).astype(int) # added 
        num_candidate_samples[good_cluster_index[0]] += num_samples - sum(num_candidate_samples)
        mode_index = []
        for i in range(num_multimodality):
            code_list.append(np.random.multivariate_normal(
                gm.means_[i], gm.covariances_[i], num_candidate_samples[i]))
            mode_index.append(np.zeros(num_candidate_samples[i]) + i)
        return np.concatenate(code_list), np.concatenate(mode_index)
//  [python]
def sample_code_multimodal(self, gt_code_list, num_samples):
        code_list = []
        class_code = gt_code_list 
        class_code = np.unique(class_code, axis = 0)
        s_pre = -1
        K = np.arange(3,10)
        for i, k in enumerate(K):
            model = GaussianMixture(n_components=k, n_init=5, init_params='kmeans', random_state=0)
            labels = model.fit_predict(class_code)
            s = metrics.silhouette_score(class_code, labels, metric='euclidean')
            if s < s_pre * 0.95:
                break
            else:
                num_multimodality = k 
            s_pre = s 
        gm = GaussianMixture(n_components=num_multimodality, n_init=5, init_params='kmeans', random_state=0).fit(class_code)
        prediction = gm.predict(class_code)
        _, num_candidate_samples = np.unique(prediction, return_counts=True)
        th = 10
        noisy_cluster_index = np.where(num_candidate_samples <= th)[0]
        good_cluster_index = np.where(num_candidate_samples > th)[0]
        num_candidate_samples[noisy_cluster_index] = 0
        num_candidate_samples = (num_candidate_samples/sum(num_candidate_samples) * num_samples).astype(int) # added 
        num_candidate_samples[good_cluster_index[0]] += num_samples - sum(num_candidate_samples)
        mode_index = []
        for i in range(num_multimodality):
            code_list.append(np.random.multivariate_normal(
                gm.means_[i], gm.covariances_[i], num_candidate_samples[i]))
            mode_index.append(np.zeros(num_candidate_samples[i]) + i)
        return np.concatenate(code_list), np.concatenate(mode_index)
//  [python]
def estimate_non_detection(data):
    """
    Fits each column in data to a two-component gaussian mixture
    to separate into 'detects' and 'non-detects'.

    Parameters
    ==========
    data:  pandas.DataFrame
        Expresssion Data Matrix (genes x samples)

    Returns
    =======
    p_nd:  pandas.DataFrame
        Same size as input data
        For each measurement, estimated probability of non-detection
    """

    DO_GMM = False

    if DO_GMM:
        p_nd = np.zeros_like(data)

        for i in range(data.shape[1]):
            col = data.iloc[:, [i]].values
            gm = mixture.GaussianMixture(n_components=2, covariance_type='spherical')
            gm.fit(col)
            prob = gm.predict_proba(col)

            # Extract probability for the lower gaussian
            # Need to check since order is random
            mu = gm.means_
            if(mu[0] < mu[1]):
                p_nd[:, i] = prob[:, 0]
            else:
                p_nd[:, i] = prob[:, 1]
    else:
        p_nd = (data.values == 0).astype('float')

    p_nd = pd.DataFrame(p_nd, index=data.index, columns=data.columns)

    return p_nd
//  [python]
def estimate_non_detection(data):
    """
    Fits each column in data to a two-component gaussian mixture
    to separate into 'detects' and 'non-detects'.

    Parameters
    ==========
    data:  pandas.DataFrame
        Expresssion Data Matrix (genes x samples)

    Returns
    =======
    p_nd:  pandas.DataFrame
        Same size as input data
        For each measurement, estimated probability of non-detection
    """

    DO_GMM = False

    if DO_GMM:
        p_nd = np.zeros_like(data)

        for i in range(data.shape[1]):
            col = data.iloc[:, [i]].values
            gm = GaussianMixture(n_components=2, covariance_type='spherical')
            gm.fit(col)
            prob = gm.predict_proba(col)

            # Extract probability for the lower gaussian
            # Need to check since order is random
            mu = gm.means_
            if(mu[0] < mu[1]):
                p_nd[:, i] = prob[:, 0]
            else:
                p_nd[:, i] = prob[:, 1]
    else:
        p_nd = (data.values == 0).astype('float')

    p_nd = pd.DataFrame(p_nd, index=data.index, columns=data.columns)

    return p_nd
//  [python]
def test_FitPredict__predict(self):
        X = self.X
        y = self.y
        lm = LinearRegression()
        lm.__class__ = type('newClass', (type(lm), FitPredictMixin), {})
        lm.fit(X=X, y=y)
        result_lm = lm.predict_dict(X=X)
        self.assertEqual(list(result_lm.keys()), ['predict'])
        self.assertEqual(result_lm['predict'].shape, (self.size, 1))

        gm = GaussianMixture()
        gm.__class__ = type('newClass', (type(gm), FitPredictMixin), {})
        gm.fit_using_varargs(X=X)
        result_gm = gm.predict_dict(X=X)
        self.assertEqual(sorted(list(result_gm.keys())),
                         sorted(['predict', 'predict_proba']))
        self.assertEqual(result_gm['predict'].shape, (self.size,))
//  [python]
def sample_motion(self, data, GMMs, mapping, gt_code_list, gt_end_list, num_samples=5, sample_strategy="standard"):
        # data : dummy data only for feature size
        GMM_data_end = []
        for i in range(len(mapping)):
            GMM_data_end.append(mixture.GaussianMixture(n_components=1, random_state=0).fit(gt_end_list[i]))

        pred_motion_list, code_list, c_onehot_list = [], [], []
        #print("using sample strategy ", sample_strategy)
        with torch.no_grad():
            # class iter
            for i in range(len(mapping)):
                if sample_strategy=="standard":
                    sampled_code = np.random.multivariate_normal(
                            GMMs.means_[mapping[i]], GMMs.covariances_[mapping[i]], num_samples)
                elif sample_strategy=="MM":
                    sampled_code, _ = self.sample_code_multimodal(gt_code_list[i], num_samples)
                else:
                    raise NotImplementedError(f'{sample_strategy} is not implemented')
                    
                code = torch.from_numpy(sampled_code)
                end = self.find_closest_end(code, torch.from_numpy(
                    gt_code_list[i]), torch.from_numpy(gt_end_list[i]))
                c_onehot = torch.from_numpy(
                    np.zeros(len(mapping))).repeat(num_samples, 1)
                c_onehot[:, i] = 1
                code, c_onehot, end = (
                    code.to(self.device).float(),
                    c_onehot.to(self.device).float(),
                    end.to(self.device).float()
                )
                pred_traj = self.tgenerator(code, c_onehot, end)
                pred_traj = self.tgenerator.to_location(pred_traj)
                pred_motion = self.mgenerator(
                    data, c_onehot, code, pred_traj, tf_rate=0, gt_traj=pred_traj, fix_bonelen=True
                )
                pred_motion = pred_motion
                pred_motion_list.append(pred_motion)
                code_list.append(code)
                c_onehot_list.append(c_onehot)
        return pred_motion_list, code_list, c_onehot_list
//  [python]
def sample_motion(self, data, GMMs, mapping, gt_code_list, gt_end_list, num_samples=5, sample_strategy="standard"):
        # data : dummy data only for feature size
        GMM_data_end = []
        for i in range(len(mapping)):
            GMM_data_end.append(GaussianMixture(n_components=1, random_state=0).fit(gt_end_list[i]))

        pred_motion_list, code_list, c_onehot_list = [], [], []
        #print("using sample strategy ", sample_strategy)
        with torch.no_grad():
            # class iter
            for i in range(len(mapping)):
                if sample_strategy=="standard":
                    sampled_code = np.random.multivariate_normal(
                            GMMs.means_[mapping[i]], GMMs.covariances_[mapping[i]], num_samples)
                elif sample_strategy=="MM":
                    sampled_code, _ = self.sample_code_multimodal(gt_code_list[i], num_samples)
                else:
                    raise NotImplementedError(f'{sample_strategy} is not implemented')
                    
                code = torch.from_numpy(sampled_code)
                end = self.find_closest_end(code, torch.from_numpy(
                    gt_code_list[i]), torch.from_numpy(gt_end_list[i]))
                c_onehot = torch.from_numpy(
                    np.zeros(len(mapping))).repeat(num_samples, 1)
                c_onehot[:, i] = 1
                code, c_onehot, end = (
                    code.to(self.device).float(),
                    c_onehot.to(self.device).float(),
                    end.to(self.device).float()
                )
                pred_traj = self.tgenerator(code, c_onehot, end)
                pred_traj = self.tgenerator.to_location(pred_traj)
                pred_motion = self.mgenerator(
                    data, c_onehot, code, pred_traj, tf_rate=0, gt_traj=pred_traj, fix_bonelen=True
                )
                pred_motion = pred_motion
                pred_motion_list.append(pred_motion)
                code_list.append(code)
                c_onehot_list.append(c_onehot)
        return pred_motion_list, code_list, c_onehot_list
//  [python]
def _fit_and_dichotomise(self, predicted_time, n_clusters=2):
        """
        """
        labels = np.zeros(predicted_time.shape)
        predicted_time[predicted_time == 0] = np.inf

        if self._use_gaussian_to_dichotomize:
            glm = mixture.GaussianMixture(n_components=n_clusters)
            self._labels = glm.fit_predict(predicted_time.reshape(1, -1).T)
            self._glm = glm

            return self._labels

        for cluster in range(n_clusters):
            percentile = 100 * (1.0 - 1.0 / (cluster + 1.0))
            value = np.percentile(predicted_time, percentile)
            labels[predicted_time >= value] = n_clusters - cluster

        return labels
//  [python]
def _fit_and_dichotomise(self, predicted_time, n_clusters=2):
        """
        """
        labels = np.zeros(predicted_time.shape)
        predicted_time[predicted_time == 0] = np.inf

        if self._use_gaussian_to_dichotomize:
            glm = GaussianMixture(n_components=n_clusters)
            self._labels = glm.fit_predict(predicted_time.reshape(1, -1).T)
            self._glm = glm

            return self._labels

        for cluster in range(n_clusters):
            percentile = 100 * (1.0 - 1.0 / (cluster + 1.0))
            value = np.percentile(predicted_time, percentile)
            labels[predicted_time >= value] = n_clusters - cluster

        return labels
//  [python]
def fit(self, X, y=None, **fit_params):
        random_state = check_random_state(self.random_state)
        self._validate_params()
        self._validate_data(X)

        combined_data = np.vstack(X)
        combined_data = check_array(combined_data)

        self.mixture_model_ = mixture.GaussianMixture(
            n_components=self.n_components, random_state=random_state
        )
        self.mixture_model_.fit(combined_data)
        self.ground_distance_ = pairwise_gaussian_ground_distance(
            self.mixture_model_.means_,
            self.mixture_model_.covariances_,
        )
        self.metric_ = distances.hellinger
//  [python]
def fit(self, X, y=None, **fit_params):
        random_state = check_random_state(self.random_state)
        self._validate_params()
        self._validate_data(X)

        combined_data = np.vstack(X)
        combined_data = check_array(combined_data)

        self.mixture_model_ = GaussianMixture(
            n_components=self.n_components, random_state=random_state
        )
        self.mixture_model_.fit(combined_data)
        self.ground_distance_ = pairwise_gaussian_ground_distance(
            self.mixture_model_.means_,
            self.mixture_model_.covariances_,
        )
        self.metric_ = distances.hellinger
//  [python]
param_grid = {
    "n_components": range(1, 7),
    "covariance_type": ["spherical", "tied", "diag", "full"],
}
grid_search = GridSearchCV(
    mixture.GaussianMixture(), param_grid=param_grid, scoring=gmm_bic_score
)
//  [python]
param_grid = {
    "n_components": range(1, 7),
    "covariance_type": ["spherical", "tied", "diag", "full"],
}
grid_search = GridSearchCV(
    GaussianMixture(), param_grid=param_grid, scoring=gmm_bic_score
)
//  [python]
def run_GMM(data_to_fit, data_to_predict, k, seed=1):
    """Apply the GaussianMixtureModel on a dataset.
    For more see: https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html
    Args:
        data_to_fit (int): The dataset
        data_to_predict (int): The dataset for prediction
        k (int): The number of components.
        seed (int): Random seed for initialisation

    Returns:
        [type]: [description]
    """
    gmm = mixture.GaussianMixture(n_components=k, covariance_type="full", random_state=seed)
    labels = gmm.fit_predict(data_to_fit)
    if data_to_predict is None:
        preds = None
    else:
        preds = gmm.predict(data_to_predict)
    return labels, preds