jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def fit(self, model, reg_covar=0, n_init=10):
        '''
        Fit the GMM model with the current node embedding and save the result in the model
        :param model: model injected to add the mixture parameters
        '''
        self.g_mixture = mixture.GaussianMixture(n_components=model.k,
                                                 reg_covar=reg_covar,
                                                 covariance_type='full',
                                                 n_init=n_init)

        log.info("Fitting: {} communities".format(model.k))
        self.g_mixture.fit(model.node_embedding)

        # diag_covars = []
        # for covar in g.covariances_:
        #     diag = np.diag(covar)
        #     diag_covars.append(diag)

        model.centroid = self.g_mixture.means_.astype(np.float32)
        model.covariance_mat = self.g_mixture.covariances_.astype(np.float32)
        model.inv_covariance_mat = self.g_mixture.precisions_.astype(np.float32)
        model.pi = self.g_mixture.predict_proba(model.node_embedding).astype(np.float32)
//  [python]
def testEmMatchesFullSkLearn(self):
        """
        Assert that log-probabilities (E-step) and parameter updates (M-step) approximately match those of sklearn.
        """
        d = 20
        n_components = np.random.randint(1, 100)

        # (n, k, d)
        x = torch.randn(400, 1, d).double()
        # (n, d)
        x_np = np.squeeze(x.data.numpy())

        var_init = torch.eye(d,dtype=torch.float64).reshape(1, 1, d, d).repeat(1,n_components,1, 1)

        model = GaussianMixture(n_components, d, init_params="random", var_init=var_init, covariance_type="full")
        model_sk = sklearn.mixture.GaussianMixture(n_components,
            covariance_type="full",
            init_params="random",
            means_init=np.squeeze(model.mu.data.numpy()),
            precisions_init=np.squeeze(np.linalg.inv(var_init)))

        model_sk._initialize_parameters(x_np, np.random.RandomState())
        log_prob_sk = model_sk._estimate_log_prob(x_np)
        log_prob = model._estimate_log_prob(x)

        # Test whether log-probabilities are approximately equal
        np.testing.assert_almost_equal(np.squeeze(log_prob.data.numpy()),
            log_prob_sk,
            decimal=2,
            verbose=True)

        _, log_resp_sk = model_sk._e_step(x_np)
        _, log_resp = model._e_step(x)

        # Test whether E-steps are approximately equal
        np.testing.assert_almost_equal(np.squeeze(log_resp.data.numpy()),
            log_resp_sk,
            decimal=0,
            verbose=True)

        model_sk._m_step(x_np, log_resp_sk)
        pi_sk = model_sk.weights_
        mu_sk = model_sk.means_
        var_sk = model_sk.covariances_

        pi, mu, var = model._m_step(x, log_resp)

        # Test whether pi ..
        np.testing.assert_almost_equal(np.squeeze(pi.data.numpy()),
            pi_sk,
            decimal=1,
            verbose=True)

        # .. mu ..
        np.testing.assert_almost_equal(np.squeeze(mu.data.numpy()),
            mu_sk,
            decimal=1,
            verbose=True)

        # .. and var are approximately equal
        np.testing.assert_almost_equal(np.squeeze(var.data.numpy()),
            var_sk,
            decimal=1,
            verbose=True)
//  [python]
def test_fit(self):
        mixture = s.GaussianMixture(random_state=0, n_components=4)
        cmm = MixtureModelClassifier(
            missing_label="nan",
            mixture_model=mixture,
            classes=[1, 2],
            cost_matrix=1 - np.eye(3),
        )
        self.assertRaises(TypeError, cmm.fit, X=self.X, y=self.y)
        cmm = MixtureModelClassifier(
            missing_label="nan",
            mixture_model=mixture,
            cost_matrix=1 - np.eye(3),
        )
        self.assertRaises(ValueError, cmm.fit, X=self.X, y=self.y)
        cmm = MixtureModelClassifier(missing_label=None, random_state=0)
        self.assertRaises(NotFittedError, check_is_fitted, estimator=cmm)
        cost_matrix = 1 - np.eye(2)
        cmm = MixtureModelClassifier(
            classes=["tokyo", "paris"],
            cost_matrix=cost_matrix,
            missing_label="nan",
        )
        np.testing.assert_array_equal(cost_matrix, cmm.cost_matrix)
        self.assertEqual("nan", cmm.missing_label)
        self.assertEqual(cmm.mixture_model, None)
        np.testing.assert_array_equal(["tokyo", "paris"], cmm.classes)
        mixture = s.BayesianGaussianMixture(n_components=1).fit(X=self.X)
        cmm = MixtureModelClassifier(
            mixture_model=mixture,
            classes=["tokyo", "paris", "new york"],
            missing_label="nan",
        )
        self.assertEqual(None, cmm.cost_matrix)
        self.assertFalse(hasattr(cmm, "F_components_"))
        self.assertFalse(hasattr(cmm, "_refit"))
        self.assertFalse(hasattr(cmm, "classes_"))
        cmm.fit(X=self.X, y=self.y)
        self.assertTrue(hasattr(cmm, "mixture_model_"))
        np.testing.assert_array_equal(
            cmm.classes_, ["new york", "paris", "tokyo"]
        )
        np.testing.assert_array_equal(1 - np.eye(3), cmm.cost_matrix_)
        np.testing.assert_array_equal([[0, 1, 1]], cmm.F_components_)
        cmm.fit(X=self.X, y=self.y, sample_weight=self.w)
        np.testing.assert_array_equal([[0, 1, 2]], cmm.F_components_)
//  [python]
def test_fit(self):
        mixture = GaussianMixture(random_state=0, n_components=4)
        cmm = MixtureModelClassifier(
            missing_label="nan",
            mixture_model=mixture,
            classes=[1, 2],
            cost_matrix=1 - np.eye(3),
        )
        self.assertRaises(TypeError, cmm.fit, X=self.X, y=self.y)
        cmm = MixtureModelClassifier(
            missing_label="nan",
            mixture_model=mixture,
            cost_matrix=1 - np.eye(3),
        )
        self.assertRaises(ValueError, cmm.fit, X=self.X, y=self.y)
        cmm = MixtureModelClassifier(missing_label=None, random_state=0)
        self.assertRaises(NotFittedError, check_is_fitted, estimator=cmm)
        cost_matrix = 1 - np.eye(2)
        cmm = MixtureModelClassifier(
            classes=["tokyo", "paris"],
            cost_matrix=cost_matrix,
            missing_label="nan",
        )
        np.testing.assert_array_equal(cost_matrix, cmm.cost_matrix)
        self.assertEqual("nan", cmm.missing_label)
        self.assertEqual(cmm.mixture_model, None)
        np.testing.assert_array_equal(["tokyo", "paris"], cmm.classes)
        mixture = BayesianGaussianMixture(n_components=1).fit(X=self.X)
        cmm = MixtureModelClassifier(
            mixture_model=mixture,
            classes=["tokyo", "paris", "new york"],
            missing_label="nan",
        )
        self.assertEqual(None, cmm.cost_matrix)
        self.assertFalse(hasattr(cmm, "F_components_"))
        self.assertFalse(hasattr(cmm, "_refit"))
        self.assertFalse(hasattr(cmm, "classes_"))
        cmm.fit(X=self.X, y=self.y)
        self.assertTrue(hasattr(cmm, "mixture_model_"))
        np.testing.assert_array_equal(
            cmm.classes_, ["new york", "paris", "tokyo"]
        )
        np.testing.assert_array_equal(1 - np.eye(3), cmm.cost_matrix_)
        np.testing.assert_array_equal([[0, 1, 1]], cmm.F_components_)
        cmm.fit(X=self.X, y=self.y, sample_weight=self.w)
        np.testing.assert_array_equal([[0, 1, 2]], cmm.F_components_)
//  [python]
def sample_as_a_vector_gmm_and_save(self, start=0, end=1000, siz=64, ch=3, clusters=10, n_sample=10000):

        all_samples = []
        flatten = nn.Flatten()
        dataset = self.ds

        print(len(dataset))
        for idx in range(len(dataset)):
            img = dataset[idx]
            img = torch.unsqueeze(img, 0)
            img = F.interpolate(img, size=siz, mode='bilinear')
            img = flatten(img)
            if idx > start:
                all_samples.append(img[0])
            if idx % 1000 == 0:
                print(idx)
            if end != None:
                if idx == end:
                    print(idx)
                    break

        all_samples = torch.stack(all_samples)
        print(all_samples.shape)

        all_samples = all_samples.cpu().detach().numpy()
        gm = mixture.GaussianMixture(n_components=clusters, random_state=0).fit(all_samples)

        all_num = n_sample
        num_samples = 10000
        it = int(all_num/num_samples)

        create_folder(f'{self.results_folder}_{siz}_{clusters}/')

        print(f'{self.results_folder}_{siz}_{clusters}/')

        cnt=0
        while(it):
            og_x, og_y = gm.sample(n_samples=num_samples)
            og_x = og_x.reshape(num_samples, ch, siz, siz)
            og_x = torch.from_numpy(og_x).cuda()
            og_x = og_x.type(torch.cuda.FloatTensor)
            print(og_x.shape)
            og_img = F.interpolate(og_x, size=self.image_size, mode='bilinear')
            X_0s, X_ts = self.ema_model.all_sample(batch_size=1, img=og_img, times=None)

            x0s = X_0s[-1]
            for i in range(x0s.shape[0]):
                utils.save_image((x0s[i]+1)*0.5, str(f'{self.results_folder}_{siz}_{clusters}/' + f'sample-x0-{cnt}.png'))
                cnt += 1

            it = it - 1
            print(it)
//  [python]
def sample_as_a_vector_gmm_and_save(self, start=0, end=1000, siz=64, ch=3, clusters=10, n_sample=10000):

        all_samples = []
        flatten = nn.Flatten()
        dataset = self.ds

        print(len(dataset))
        for idx in range(len(dataset)):
            img = dataset[idx]
            img = torch.unsqueeze(img, 0)
            img = F.interpolate(img, size=siz, mode='bilinear')
            img = flatten(img)
            if idx > start:
                all_samples.append(img[0])
            if idx % 1000 == 0:
                print(idx)
            if end != None:
                if idx == end:
                    print(idx)
                    break

        all_samples = torch.stack(all_samples)
        print(all_samples.shape)

        all_samples = all_samples.cpu().detach().numpy()
        gm = GaussianMixture(n_components=clusters, random_state=0).fit(all_samples)

        all_num = n_sample
        num_samples = 10000
        it = int(all_num/num_samples)

        create_folder(f'{self.results_folder}_{siz}_{clusters}/')

        print(f'{self.results_folder}_{siz}_{clusters}/')

        cnt=0
        while(it):
            og_x, og_y = gm.sample(n_samples=num_samples)
            og_x = og_x.reshape(num_samples, ch, siz, siz)
            og_x = torch.from_numpy(og_x).cuda()
            og_x = og_x.type(torch.cuda.FloatTensor)
            print(og_x.shape)
            og_img = F.interpolate(og_x, size=self.image_size, mode='bilinear')
            X_0s, X_ts = self.ema_model.all_sample(batch_size=1, img=og_img, times=None)

            x0s = X_0s[-1]
            for i in range(x0s.shape[0]):
                utils.save_image((x0s[i]+1)*0.5, str(f'{self.results_folder}_{siz}_{clusters}/' + f'sample-x0-{cnt}.png'))
                cnt += 1

            it = it - 1
            print(it)
//  [python]
def _create_gmm(self, params=None):
        """
        Creates a Gaussian mixture model instance.

        Args:
            params (Optional[torch.Tensor]): if not None the GMM will be fit using these parameters.

        Returns:
            sklearn.mixture.GaussianMixture: a Gaussian mixture model.
        """
        gmm = mixture.GaussianMixture(covariance_type='diag',
                                              n_components=self.num_gmm_components,
                                              reg_covar=1e-5)
        if params is not None:
            if torch.is_tensor(params):
                params = params.numpy()
            gmm.fit(params)

        return gmm
//  [python]
def _create_gmm(self, params=None):
        """
        Creates a Gaussian mixture model instance.

        Args:
            params (Optional[torch.Tensor]): if not None the GMM will be fit using these parameters.

        Returns:
            sklearn.mixture.GaussianMixture: a Gaussian mixture model.
        """
        gmm = sklearn.mixture.GaussianMixture(covariance_type='diag',
                                              n_components=self.num_gmm_components,
                                              reg_covar=1e-5)
        if params is not None:
            if torch.is_tensor(params):
                params = params.numpy()
            gmm.fit(params)

        return gmm
//  [python]
def create_random_gmm(n_mix, n_features, covariance_type, prng=0):
    prng = check_random_state(prng)
    g = mixture.GaussianMixture(n_mix, covariance_type=covariance_type)
    g.means_ = prng.randint(-20, 20, (n_mix, n_features))
    g.covars_ = make_covar_matrix(covariance_type, n_mix, n_features)
    g.weights_ = normalized(prng.rand(n_mix))
    return g
//  [python]
def get_nleep_score(
    features,
    target_labels,
    num_components_gmm,
    random_state=123):
  """Computes NLEEP (Ranking Neural Checkpoints).

  Args:
    features: matrix [N, D] of source features obtained from the target data,
      where N is the number of datapoints and D their dimensionionality.
    target_labels: ground truth target labels of dimension [N, 1].
    num_components_gmm: gaussian components (5*target class number in the paper)
    random_state: random seed for the GaussianMixture initialization.

  Returns:
    nleep: transferability metric score.

  """
  gmm = mixture.GaussianMixture(
      n_components=num_components_gmm, random_state=random_state).fit(features)
  gmm_predictions = gmm.predict_proba(features)
  nleep = leep.get_leep_score(gmm_predictions.astype('float32'), target_labels)
  return nleep
//  [python]
def get_nleep_score(
    features,
    target_labels,
    num_components_gmm,
    random_state=123):
  """Computes NLEEP (Ranking Neural Checkpoints).

  Args:
    features: matrix [N, D] of source features obtained from the target data,
      where N is the number of datapoints and D their dimensionionality.
    target_labels: ground truth target labels of dimension [N, 1].
    num_components_gmm: gaussian components (5*target class number in the paper)
    random_state: random seed for the GaussianMixture initialization.

  Returns:
    nleep: transferability metric score.

  """
  gmm = GaussianMixture(
      n_components=num_components_gmm, random_state=random_state).fit(features)
  gmm_predictions = gmm.predict_proba(features)
  nleep = leep.get_leep_score(gmm_predictions.astype('float32'), target_labels)
  return nleep
//  [python]
def sample_as_a_vector_gmm(self, start=0, end=1000, siz=64, ch=3, clusters=10):

        all_samples = []
        flatten = nn.Flatten()
        dataset = self.ds

        print(len(dataset))
        for idx in range(len(dataset)):
            img = dataset[idx]
            img = torch.unsqueeze(img, 0).cuda()
            img = F.interpolate(img, size=siz, mode='bilinear')
            img = flatten(img)
            if idx > start:
                all_samples.append(img[0])
            if idx % 1000 == 0:
                print(idx)
            if end != None:
                if idx == end:
                    print(idx)
                    break

        all_samples = torch.stack(all_samples)
        print(all_samples.shape)

        all_samples = all_samples.cpu().detach().numpy()

        num_samples = 100

        gm = mixture.GaussianMixture(n_components=clusters, random_state=0).fit(all_samples)
        og_x, og_y = gm.sample(n_samples=num_samples)
        og_x = og_x.reshape(num_samples, ch, siz, siz)
        og_x = torch.from_numpy(og_x).cuda()
        og_x = og_x.type(torch.cuda.FloatTensor)
        print(og_x.shape)
        og_img = F.interpolate(og_x, size=self.image_size, mode='bilinear')


        X_0s, X_ts = self.ema_model.all_sample(batch_size=1, img=og_img, times=None)

        extra_path = 'vec'
        og_img = (og_img + 1) * 0.5
        utils.save_image(og_img, str(self.results_folder / f'og-{start}-{end}-{siz}-{clusters}-{extra_path}.png'), nrow=6)

        import imageio
        frames_t = []
        frames_0 = []

        for i in range(len(X_0s)):
            print(i)

            x_0 = X_0s[i]
            x_0 = (x_0 + 1) * 0.5
            utils.save_image(x_0, str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-x0.png'),
                             nrow=6)
            self.add_title(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-x0.png'), str(i))
            frames_0.append(
                imageio.imread(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-x0.png')))

            x_t = X_ts[i]
            all_images = (x_t + 1) * 0.5
            utils.save_image(all_images,
                             str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-xt.png'), nrow=6)
            self.add_title(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-xt.png'), str(i))
            frames_t.append(
                imageio.imread(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-xt.png')))

        imageio.mimsave(str(self.results_folder / f'Gif-{start}-{end}-{siz}-{clusters}-{extra_path}-x0.gif'), frames_0)
        imageio.mimsave(str(self.results_folder / f'Gif-{start}-{end}-{siz}-{clusters}-{extra_path}-xt.gif'), frames_t)
//  [python]
def sample_as_a_vector_gmm(self, start=0, end=1000, siz=64, ch=3, clusters=10):

        all_samples = []
        flatten = nn.Flatten()
        dataset = self.ds

        print(len(dataset))
        for idx in range(len(dataset)):
            img = dataset[idx]
            img = torch.unsqueeze(img, 0).cuda()
            img = F.interpolate(img, size=siz, mode='bilinear')
            img = flatten(img)
            if idx > start:
                all_samples.append(img[0])
            if idx % 1000 == 0:
                print(idx)
            if end != None:
                if idx == end:
                    print(idx)
                    break

        all_samples = torch.stack(all_samples)
        print(all_samples.shape)

        all_samples = all_samples.cpu().detach().numpy()

        num_samples = 100

        gm = GaussianMixture(n_components=clusters, random_state=0).fit(all_samples)
        og_x, og_y = gm.sample(n_samples=num_samples)
        og_x = og_x.reshape(num_samples, ch, siz, siz)
        og_x = torch.from_numpy(og_x).cuda()
        og_x = og_x.type(torch.cuda.FloatTensor)
        print(og_x.shape)
        og_img = F.interpolate(og_x, size=self.image_size, mode='bilinear')


        X_0s, X_ts = self.ema_model.all_sample(batch_size=1, img=og_img, times=None)

        extra_path = 'vec'
        og_img = (og_img + 1) * 0.5
        utils.save_image(og_img, str(self.results_folder / f'og-{start}-{end}-{siz}-{clusters}-{extra_path}.png'), nrow=6)

        import imageio
        frames_t = []
        frames_0 = []

        for i in range(len(X_0s)):
            print(i)

            x_0 = X_0s[i]
            x_0 = (x_0 + 1) * 0.5
            utils.save_image(x_0, str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-x0.png'),
                             nrow=6)
            self.add_title(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-x0.png'), str(i))
            frames_0.append(
                imageio.imread(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-x0.png')))

            x_t = X_ts[i]
            all_images = (x_t + 1) * 0.5
            utils.save_image(all_images,
                             str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-xt.png'), nrow=6)
            self.add_title(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-xt.png'), str(i))
            frames_t.append(
                imageio.imread(str(self.results_folder / f'sample-{start}-{end}-{siz}-{clusters}-{i}-{extra_path}-xt.png')))

        imageio.mimsave(str(self.results_folder / f'Gif-{start}-{end}-{siz}-{clusters}-{extra_path}-x0.gif'), frames_0)
        imageio.mimsave(str(self.results_folder / f'Gif-{start}-{end}-{siz}-{clusters}-{extra_path}-xt.gif'), frames_t)
//  [python]
here = os.path.dirname(__file__)
random = numpy.random.randint(0, 1000)
random = 1
print('random_state={}'.format(random))

MODELS = {
    'GMM-full': mixture.GaussianMixture(n_components=3, covariance_type='full', random_state=random),
    'GMM-tied': mixture.GaussianMixture(n_components=3, covariance_type='tied', random_state=random),
    'GMM-diag': mixture.GaussianMixture(n_components=3, covariance_type='diag', random_state=random),
    'GMM-spherical': mixture.GaussianMixture(n_components=3, covariance_type='spherical', random_state=random),
    'B-GMM-full': mixture.BayesianGaussianMixture(n_components=2, covariance_type='full', random_state=random),
    'B-GMM-tied': mixture.BayesianGaussianMixture(n_components=5, covariance_type='tied', random_state=random),
    'B-GMM-diag': mixture.BayesianGaussianMixture(n_components=10, covariance_type='diag', random_state=random),
    'B-GMM-spherical': mixture.BayesianGaussianMixture(n_components=10, covariance_type='spherical', random_state=random),
    'EllipticEnvelope': EllipticEnvelope(),
}
//  [python]
here = os.path.dirname(__file__)
random = numpy.random.randint(0, 1000)
random = 1
print('random_state={}'.format(random))

MODELS = {
    'GMM-full': GaussianMixture(n_components=3, covariance_type='full', random_state=random),
    'GMM-tied': GaussianMixture(n_components=3, covariance_type='tied', random_state=random),
    'GMM-diag': GaussianMixture(n_components=3, covariance_type='diag', random_state=random),
    'GMM-spherical': GaussianMixture(n_components=3, covariance_type='spherical', random_state=random),
    'B-GMM-full': BayesianGaussianMixture(n_components=2, covariance_type='full', random_state=random),
    'B-GMM-tied': BayesianGaussianMixture(n_components=5, covariance_type='tied', random_state=random),
    'B-GMM-diag': BayesianGaussianMixture(n_components=10, covariance_type='diag', random_state=random),
    'B-GMM-spherical': BayesianGaussianMixture(n_components=10, covariance_type='spherical', random_state=random),
    'EllipticEnvelope': EllipticEnvelope(),
}
//  [python]
def gaussian_mixture(self):
        """
        :return: model.predict(X) 返回类中心
        """
        model = mixture.GaussianMixture(self.n_clusters, random_state=self.random_state)
        model.fit(self.X)
        return model
//  [python]
def gaussian_mixture(self):
        """
        :return: model.predict(X) 返回类中心
        """
        model = GaussianMixture(self.n_clusters, random_state=self.random_state)
        model.fit(self.X)
        return model
//  [python]
def gmm(all_medians_list, n_wgds, max_iter=100, n_init=1):
    """
    Log-transforms the segment pairs medians and fits a Gaussian mixture model.

    :param all_medians_list: list containing all segment pair medians
    :param n_wgds: number of putative WGDs
    :param max_iter: maximum number of iteration for the EM algorithm
    :param n_init: number of initializations
    :return labels: list of cluster IDs to which the medians have been assigned (keeps the order of the input medians)
    """
    # log-transform Ks medians (Ks data tend to have a lognormal shape, but GMM uses normal components)
    all_medians_list_logtransformed = log(all_medians_list)

    gmm = mixture.GMM(n_components=n_wgds, covariance_type="spherical", max_iter=max_iter, n_init=n_init)
    gmm.fit(all_medians_list_logtransformed.reshape(len(all_medians_list_logtransformed), 1))
    labels = gmm.predict(all_medians_list_logtransformed.reshape(len(all_medians_list_logtransformed), 1))
    return labels
//  [python]
def gmm(all_medians_list, n_wgds, max_iter=100, n_init=1):
    """
    Log-transforms the segment pairs medians and fits a Gaussian mixture model.

    :param all_medians_list: list containing all segment pair medians
    :param n_wgds: number of putative WGDs
    :param max_iter: maximum number of iteration for the EM algorithm
    :param n_init: number of initializations
    :return labels: list of cluster IDs to which the medians have been assigned (keeps the order of the input medians)
    """
    # log-transform Ks medians (Ks data tend to have a lognormal shape, but GMM uses normal components)
    all_medians_list_logtransformed = log(all_medians_list)

    gmm = GaussianMixture(n_components=n_wgds, covariance_type="spherical", max_iter=max_iter, n_init=n_init)
    gmm.fit(all_medians_list_logtransformed.reshape(len(all_medians_list_logtransformed), 1))
    labels = gmm.predict(all_medians_list_logtransformed.reshape(len(all_medians_list_logtransformed), 1))
    return labels
//  [python]
def analyzeK(cls, X, k_min=2, k_max=20):
        """
        Plot the result of the methods (elbow, silhouette and calinski_harabas) to find the best k

        Parameters
        ----------
        X : array
            values ​​that will be used to find the best k
        k_min : int
            minimum interval for K
        k_max : int
            maximum range for K

        Returns
        -------
        None
        """

        if X is None:
            raise Exception("Error: X is None.")
        if k_min is None or k_max is None:
            raise Exception("Error: Range is None.")
        if k_min < 2:
            raise Exception("Error: k_min < 2")

        wss = []
        s_gmm = []
        s_kmeans = []
        ch_gmm = []
        ch_kmeans = []

        K = range(k_min, k_max)

        for k in K:
            kmeans = KMeans(n_clusters=k)
            kmeans.fit(X)
            gmm = mixture.GaussianMixture(n_components=k, covariance_type="full")
            gmm.fit(X)

            labels_kmeans = kmeans.predict(X)
            labels_gmm = gmm.predict(X)

            s_kmeans.append(
                metrics.silhouette_score(X, labels_kmeans, metric="euclidean")
            )
            s_gmm.append(metrics.silhouette_score(X, labels_gmm, metric="euclidean"))

            ch_kmeans.append(metrics.calinski_harabasz_score(X, labels_kmeans))
            ch_gmm.append(metrics.calinski_harabasz_score(X, labels_gmm))

            wss.append(kmeans.inertia_)

        cls._elbow(K, wss)
        cls._silhouette_coefficient(K, s_kmeans, s_gmm)
        cls._calinski_harabaz(K, ch_kmeans, ch_gmm)
//  [python]
def analyzeK(cls, X, k_min=2, k_max=20):
        """
        Plot the result of the methods (elbow, silhouette and calinski_harabas) to find the best k

        Parameters
        ----------
        X : array
            values ​​that will be used to find the best k
        k_min : int
            minimum interval for K
        k_max : int
            maximum range for K

        Returns
        -------
        None
        """

        if X is None:
            raise Exception("Error: X is None.")
        if k_min is None or k_max is None:
            raise Exception("Error: Range is None.")
        if k_min < 2:
            raise Exception("Error: k_min < 2")

        wss = []
        s_gmm = []
        s_kmeans = []
        ch_gmm = []
        ch_kmeans = []

        K = range(k_min, k_max)

        for k in K:
            kmeans = KMeans(n_clusters=k)
            kmeans.fit(X)
            gmm = GaussianMixture(n_components=k, covariance_type="full")
            gmm.fit(X)

            labels_kmeans = kmeans.predict(X)
            labels_gmm = gmm.predict(X)

            s_kmeans.append(
                metrics.silhouette_score(X, labels_kmeans, metric="euclidean")
            )
            s_gmm.append(metrics.silhouette_score(X, labels_gmm, metric="euclidean"))

            ch_kmeans.append(metrics.calinski_harabasz_score(X, labels_kmeans))
            ch_gmm.append(metrics.calinski_harabasz_score(X, labels_gmm))

            wss.append(kmeans.inertia_)

        cls._elbow(K, wss)
        cls._silhouette_coefficient(K, s_kmeans, s_gmm)
        cls._calinski_harabaz(K, ch_kmeans, ch_gmm)
//  [python]
def __init__(self, n_kernels=1, convars_type='diag', use_bayesian=False):
        # para n_kernels:
        # para convars_type:
        # para use_bayesian:
        assert convars_type in ['diag', 'full']
        assert n_kernels >= 0
        # == 0 dummy instance

        self.name = 'kernels%d_convars%s_bayes%d' % (n_kernels, convars_type, use_bayesian)
        self.n_kernels = n_kernels
        self.convars_type = convars_type
        self.use_bayesian = use_bayesian
        self.fitted = False
        self.config = json.load(open('./config/model.json', 'r'))['fisher_vector']
        self.save_dir = self.config['save_dir']
        self.data_dir = self.config['data_dir']
        self.means = None
        self.covars = None
        self.weights = None

        if not self.use_bayesian:
            self.gmm = mixture.GMM(
                        n_components=self.n_kernels,
                        covariance_type=self.convars_type,
                        n_iter=1000,
                        verbose=2)
        else:
            self.gmm = mixture.BayesianGaussianMixture(
                        n_components=self.n_kernels,
                        covariance_type=self.convars_type,
                        n_iter=1000,
                        verbose=2)
//  [python]
def __init__(self, n_kernels=1, convars_type='diag', use_bayesian=False):
        # para n_kernels:
        # para convars_type:
        # para use_bayesian:
        assert convars_type in ['diag', 'full']
        assert n_kernels >= 0
        # == 0 dummy instance

        self.name = 'kernels%d_convars%s_bayes%d' % (n_kernels, convars_type, use_bayesian)
        self.n_kernels = n_kernels
        self.convars_type = convars_type
        self.use_bayesian = use_bayesian
        self.fitted = False
        self.config = json.load(open('./config/model.json', 'r'))['fisher_vector']
        self.save_dir = self.config['save_dir']
        self.data_dir = self.config['data_dir']
        self.means = None
        self.covars = None
        self.weights = None

        if not self.use_bayesian:
            self.gmm = GaussianMixture(
                        n_components=self.n_kernels,
                        covariance_type=self.convars_type,
                        max_iter=1000,
                        verbose=2)
        else:
            self.gmm = BayesianGaussianMixture(
                        n_components=self.n_kernels,
                        covariance_type=self.convars_type,
                        max_iter=1000,
                        verbose=2)
//  [python]
def find_minima(self):
        
        #Initialization
        potential = self.potential
        delta_t = self.delta_t
        eta = self.eta
        
        start_time = time.time()
        
        #2 minima first estimation with Gaussian Mixture Model
        xy_dict = {'x':self.x,'y':self.y}
        xy_df = pd.DataFrame(xy_dict)
        gm = mixture.GaussianMixture(n_components=2)
        gm.fit(xy_df)
        points = gm.means_
        
        #Refining the minima estimation with the potential descent
        velocities = [np.array([0.0,0.0]) for i in range(len(points))]
        for i in range(1000):
            potential_force = self.calculate_potential_force( points )
            acceleration = [potential_force[k]-eta*velocities[k] for k in range(len(potential_force)) ]
            old_points = copy(points)
            total_acceleration = 0.0
            for j in range(0,len(points)):
                points[j] += 0.5*acceleration[j]*delta_t**2 + velocities[j]*delta_t
                velocities[j] += acceleration[j]*delta_t
                total_acceleration += np.linalg.norm(acceleration[j])
            if np.linalg.norm(points[0]-points[1])<0.1 or total_acceleration < 0.00001:
                break
        
        x0 , x1 = points
        
        #Sort the minima
        if np.linalg.norm(x1) < np.linalg.norm(x0):
            return x1, x0
        else:
            return x0, x1
//  [python]
def find_minima(self):
        
        #Initialization
        potential = self.potential
        delta_t = self.delta_t
        eta = self.eta
        
        start_time = time.time()
        
        #2 minima first estimation with Gaussian Mixture Model
        xy_dict = {'x':self.x,'y':self.y}
        xy_df = pd.DataFrame(xy_dict)
        gm = GaussianMixture(n_components=2)
        gm.fit(xy_df)
        points = gm.means_
        
        #Refining the minima estimation with the potential descent
        velocities = [np.array([0.0,0.0]) for i in range(len(points))]
        for i in range(1000):
            potential_force = self.calculate_potential_force( points )
            acceleration = [potential_force[k]-eta*velocities[k] for k in range(len(potential_force)) ]
            old_points = copy(points)
            total_acceleration = 0.0
            for j in range(0,len(points)):
                points[j] += 0.5*acceleration[j]*delta_t**2 + velocities[j]*delta_t
                velocities[j] += acceleration[j]*delta_t
                total_acceleration += np.linalg.norm(acceleration[j])
            if np.linalg.norm(points[0]-points[1])<0.1 or total_acceleration < 0.00001:
                break
        
        x0 , x1 = points
        
        #Sort the minima
        if np.linalg.norm(x1) < np.linalg.norm(x0):
            return x1, x0
        else:
            return x0, x1
//  [python]
def test_check_means():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)

    n_components, n_features = rand_data.n_components, rand_data.n_features
    X = rand_data.X['full']

    g = mixture.GaussianMixture(n_components=n_components)

    # Check means bad shape
    means_bad_shape = rng.rand(n_components + 1, n_features)
    g.means_init = means_bad_shape
    assert_raise_message(ValueError,
                         "The parameter 'means' should have the shape of ",
                         g.fit, X)

    # Check good means matrix
    means = rand_data.means
    g.means_init = means
    g.fit(X)
    assert_array_equal(means, g.means_init)
//  [python]
def test_check_means():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)

    n_components, n_features = rand_data.n_components, rand_data.n_features
    X = rand_data.X['full']

    g = GaussianMixture(n_components=n_components)

    # Check means bad shape
    means_bad_shape = rng.rand(n_components + 1, n_features)
    g.means_init = means_bad_shape
    assert_raise_message(ValueError,
                         "The parameter 'means' should have the shape of ",
                         g.fit, X)

    # Check good means matrix
    means = rand_data.means
    g.means_init = means
    g.fit(X)
    assert_array_equal(means, g.means_init)
//  [python]
def test_gaussian_mixture_n_parameters():
    # Test that the right number of parameters is estimated
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 50, 5, 2
    X = rng.randn(n_samples, n_features)
    n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41}
    for cv_type in COVARIANCE_TYPE:
        g = mixture.GaussianMixture(
            n_components=n_components, covariance_type=cv_type,
            random_state=rng).fit(X)
        assert g._n_parameters() == n_params[cv_type]
//  [python]
def test_gaussian_mixture_n_parameters():
    # Test that the right number of parameters is estimated
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 50, 5, 2
    X = rng.randn(n_samples, n_features)
    n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41}
    for cv_type in COVARIANCE_TYPE:
        g = GaussianMixture(
            n_components=n_components, covariance_type=cv_type,
            random_state=rng).fit(X)
        assert g._n_parameters() == n_params[cv_type]
//  [python]
def test_multiple_init():
    # Test that multiple inits does not much worse than a single one
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 50, 5, 2
    X = rng.randn(n_samples, n_features)
    for cv_type in COVARIANCE_TYPE:
        train1 = mixture.GaussianMixture(n_components=n_components,
                                 covariance_type=cv_type,
                                 random_state=0).fit(X).score(X)
        train2 = mixture.GaussianMixture(n_components=n_components,
                                 covariance_type=cv_type,
                                 random_state=0, n_init=5).fit(X).score(X)
        assert train2 >= train1
//  [python]
def test_multiple_init():
    # Test that multiple inits does not much worse than a single one
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 50, 5, 2
    X = rng.randn(n_samples, n_features)
    for cv_type in COVARIANCE_TYPE:
        train1 = GaussianMixture(n_components=n_components,
                                 covariance_type=cv_type,
                                 random_state=0).fit(X).score(X)
        train2 = GaussianMixture(n_components=n_components,
                                 covariance_type=cv_type,
                                 random_state=0, n_init=5).fit(X).score(X)
        assert train2 >= train1
//  [python]
def test_check_weights():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)

    n_components = rand_data.n_components
    X = rand_data.X['full']

    g = mixture.GaussianMixture(n_components=n_components)

    # Check bad shape
    weights_bad_shape = rng.rand(n_components, 1)
    g.weights_init = weights_bad_shape
    assert_raise_message(ValueError,
                         "The parameter 'weights' should have the shape of "
                         "(%d,), but got %s" %
                         (n_components, str(weights_bad_shape.shape)),
                         g.fit, X)

    # Check bad range
    weights_bad_range = rng.rand(n_components) + 1
    g.weights_init = weights_bad_range
    assert_raise_message(ValueError,
                         "The parameter 'weights' should be in the range "
                         "[0, 1], but got max value %.5f, min value %.5f"
                         % (np.min(weights_bad_range),
                            np.max(weights_bad_range)),
                         g.fit, X)

    # Check bad normalization
    weights_bad_norm = rng.rand(n_components)
    weights_bad_norm = weights_bad_norm / (weights_bad_norm.sum() + 1)
    g.weights_init = weights_bad_norm
    assert_raise_message(ValueError,
                         "The parameter 'weights' should be normalized, "
                         "but got sum(weights) = %.5f"
                         % np.sum(weights_bad_norm),
                         g.fit, X)

    # Check good weights matrix
    weights = rand_data.weights
    g = mixture.GaussianMixture(weights_init=weights, n_components=n_components)
    g.fit(X)
    assert_array_equal(weights, g.weights_init)
//  [python]
def test_check_weights():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)

    n_components = rand_data.n_components
    X = rand_data.X['full']

    g = GaussianMixture(n_components=n_components)

    # Check bad shape
    weights_bad_shape = rng.rand(n_components, 1)
    g.weights_init = weights_bad_shape
    assert_raise_message(ValueError,
                         "The parameter 'weights' should have the shape of "
                         "(%d,), but got %s" %
                         (n_components, str(weights_bad_shape.shape)),
                         g.fit, X)

    # Check bad range
    weights_bad_range = rng.rand(n_components) + 1
    g.weights_init = weights_bad_range
    assert_raise_message(ValueError,
                         "The parameter 'weights' should be in the range "
                         "[0, 1], but got max value %.5f, min value %.5f"
                         % (np.min(weights_bad_range),
                            np.max(weights_bad_range)),
                         g.fit, X)

    # Check bad normalization
    weights_bad_norm = rng.rand(n_components)
    weights_bad_norm = weights_bad_norm / (weights_bad_norm.sum() + 1)
    g.weights_init = weights_bad_norm
    assert_raise_message(ValueError,
                         "The parameter 'weights' should be normalized, "
                         "but got sum(weights) = %.5f"
                         % np.sum(weights_bad_norm),
                         g.fit, X)

    # Check good weights matrix
    weights = rand_data.weights
    g = GaussianMixture(weights_init=weights, n_components=n_components)
    g.fit(X)
    assert_array_equal(weights, g.weights_init)
//  [python]
def test_bic_1d_1component():
    # Test all of the covariance_types return the same BIC score for
    # 1-dimensional, 1 component fits.
    rng = np.random.RandomState(0)
    n_samples, n_dim, n_components = 100, 1, 1
    X = rng.randn(n_samples, n_dim)
    bic_full = s.GMM(n_components=n_components,
                               covariance_type='full',
                               random_state=rng).fit(X).bic(X)
    for covariance_type in ['tied', 'diag', 'spherical']:
        bic = s.GMM(n_components=n_components,
                              covariance_type=covariance_type,
                              random_state=rng).fit(X).bic(X)
        assert_almost_equal(bic_full, bic)
//  [python]
def test_bic_1d_1component():
    # Test all of the covariance_types return the same BIC score for
    # 1-dimensional, 1 component fits.
    rng = np.random.RandomState(0)
    n_samples, n_dim, n_components = 100, 1, 1
    X = rng.randn(n_samples, n_dim)
    bic_full = GaussianMixture(n_components=n_components,
                               covariance_type='full',
                               random_state=rng).fit(X).bic(X)
    for covariance_type in ['tied', 'diag', 'spherical']:
        bic = GaussianMixture(n_components=n_components,
                              covariance_type=covariance_type,
                              random_state=rng).fit(X).bic(X)
        assert_almost_equal(bic_full, bic)
//  [python]
def test_convergence_detected_with_warm_start():
    # We check that convergence is detected when warm_start=True
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    n_components = rand_data.n_components
    X = rand_data.X['full']

    for max_iter in (1, 2, 50):
        gmm = mixture.GaussianMixture(n_components=n_components, warm_start=True,
                              max_iter=max_iter, random_state=rng)
        for _ in range(100):
            gmm.fit(X)
            if gmm.converged_:
                break
        assert gmm.converged_
        assert max_iter >= gmm.n_iter_
//  [python]
def test_convergence_detected_with_warm_start():
    # We check that convergence is detected when warm_start=True
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    n_components = rand_data.n_components
    X = rand_data.X['full']

    for max_iter in (1, 2, 50):
        gmm = GaussianMixture(n_components=n_components, warm_start=True,
                              max_iter=max_iter, random_state=rng)
        for _ in range(100):
            gmm.fit(X)
            if gmm.converged_:
                break
        assert gmm.converged_
        assert max_iter >= gmm.n_iter_
//  [python]
def test_gaussian_mixture_attributes():
    # test bad parameters
    rng = np.random.RandomState(0)
    X = rng.rand(10, 2)

    n_components_bad = 0
    gmm = GMM(n_components=n_components_bad)
    assert_raise_message(ValueError,
                         "Invalid value for 'n_components': %d "
                         "Estimation requires at least one component"
                         % n_components_bad, gmm.fit, X)

    # covariance_type should be in [spherical, diag, tied, full]
    covariance_type_bad = 'bad_covariance_type'
    gmm = GMM(covariance_type=covariance_type_bad)
    assert_raise_message(ValueError,
                         "Invalid value for 'covariance_type': %s "
                         "'covariance_type' should be in "
                         "['spherical', 'tied', 'diag', 'full']"
                         % covariance_type_bad,
                         gmm.fit, X)

    tol_bad = -1
    gmm = GMM(tol=tol_bad)
    assert_raise_message(ValueError,
                         "Invalid value for 'tol': %.5f "
                         "Tolerance used by the EM must be non-negative"
                         % tol_bad, gmm.fit, X)

    reg_covar_bad = -1
    gmm = GMM(reg_covar=reg_covar_bad)
    assert_raise_message(ValueError,
                         "Invalid value for 'reg_covar': %.5f "
                         "regularization on covariance must be "
                         "non-negative" % reg_covar_bad, gmm.fit, X)

    max_iter_bad = 0
    gmm = GMM(max_iter=max_iter_bad)
    assert_raise_message(ValueError,
                         "Invalid value for 'max_iter': %d "
                         "Estimation requires at least one iteration"
                         % max_iter_bad, gmm.fit, X)

    n_init_bad = 0
    gmm = GMM(n_init=n_init_bad)
    assert_raise_message(ValueError,
                         "Invalid value for 'n_init': %d "
                         "Estimation requires at least one run"
                         % n_init_bad, gmm.fit, X)

    init_params_bad = 'bad_method'
    gmm = GMM(init_params=init_params_bad)
    assert_raise_message(ValueError,
                         "Unimplemented initialization method '%s'"
                         % init_params_bad,
                         gmm.fit, X)

    # test good parameters
    n_components, tol, n_init, max_iter, reg_covar = 2, 1e-4, 3, 30, 1e-1
    covariance_type, init_params = 'full', 'random'
    gmm = GMM(n_components=n_components, tol=tol, n_init=n_init,
                          max_iter=max_iter, reg_covar=reg_covar,
                          covariance_type=covariance_type,
                          init_params=init_params).fit(X)

    assert gmm.n_components == n_components
    assert gmm.covariance_type == covariance_type
    assert gmm.tol == tol
    assert gmm.reg_covar == reg_covar
    assert gmm.max_iter == max_iter
    assert gmm.n_init == n_init
    assert gmm.init_params == init_params