jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_gaussian_mixture_attributes():
    # test bad parameters
    rng = np.random.RandomState(0)
    X = rng.rand(10, 2)

    n_components_bad = 0
    gmm = GaussianMixture(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 = GaussianMixture(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 = GaussianMixture(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 = GaussianMixture(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 = GaussianMixture(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 = GaussianMixture(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 = GaussianMixture(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 = GaussianMixture(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
//  [python]
def test_gaussian_mixture_aic_bic():
    # Test the aic and bic criteria
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 50, 3, 2
    X = rng.randn(n_samples, n_features)
    # standard gaussian entropy
    sgh = 0.5 * (fast_logdet(np.cov(X.T, bias=1)) +
                 n_features * (1 + np.log(2 * np.pi)))
    for cv_type in COVARIANCE_TYPE:
        g = mixture.GaussianMixture(
            n_components=n_components, covariance_type=cv_type,
            random_state=rng, max_iter=200)
        g.fit(X)
        aic = 2 * n_samples * sgh + 2 * g._n_parameters()
        bic = (2 * n_samples * sgh +
               np.log(n_samples) * g._n_parameters())
        bound = n_features / np.sqrt(n_samples)
        assert (g.aic(X) - aic) / n_samples < bound
        assert (g.bic(X) - bic) / n_samples < bound
//  [python]
def test_gaussian_mixture_aic_bic():
    # Test the aic and bic criteria
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 50, 3, 2
    X = rng.randn(n_samples, n_features)
    # standard gaussian entropy
    sgh = 0.5 * (fast_logdet(np.cov(X.T, bias=1)) +
                 n_features * (1 + np.log(2 * np.pi)))
    for cv_type in COVARIANCE_TYPE:
        g = GaussianMixture(
            n_components=n_components, covariance_type=cv_type,
            random_state=rng, max_iter=200)
        g.fit(X)
        aic = 2 * n_samples * sgh + 2 * g._n_parameters()
        bic = (2 * n_samples * sgh +
               np.log(n_samples) * g._n_parameters())
        bound = n_features / np.sqrt(n_samples)
        assert (g.aic(X) - aic) / n_samples < bound
        assert (g.bic(X) - bic) / n_samples < bound
//  [python]
def test_score_samples():
    covar_type = 'full'
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=7)
    n_components = rand_data.n_components
    X = rand_data.X[covar_type]

    # Check the error message if we don't call fit
    gmm = mixture.GaussianMixture(n_components=n_components, n_init=1, reg_covar=0,
                          random_state=rng, covariance_type=covar_type)
    assert_raise_message(NotFittedError,
                         "This GaussianMixture instance is not fitted "
                         "yet. Call 'fit' with appropriate arguments "
                         "before using this estimator.", gmm.score_samples, X)

    gmm_score_samples = gmm.fit(X).score_samples(X)
    assert gmm_score_samples.shape[0] == rand_data.n_samples
//  [python]
def test_score_samples():
    covar_type = 'full'
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=7)
    n_components = rand_data.n_components
    X = rand_data.X[covar_type]

    # Check the error message if we don't call fit
    gmm = GaussianMixture(n_components=n_components, n_init=1, reg_covar=0,
                          random_state=rng, covariance_type=covar_type)
    assert_raise_message(NotFittedError,
                         "This GaussianMixture instance is not fitted "
                         "yet. Call 'fit' with appropriate arguments "
                         "before using this estimator.", gmm.score_samples, X)

    gmm_score_samples = gmm.fit(X).score_samples(X)
    assert gmm_score_samples.shape[0] == rand_data.n_samples
//  [python]
def test_check_precisions():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)

    n_components, n_features = rand_data.n_components, rand_data.n_features

    # Define the bad precisions for each covariance_type
    precisions_bad_shape = {
        'full': np.ones((n_components + 1, n_features, n_features)),
        'tied': np.ones((n_features + 1, n_features + 1)),
        'diag': np.ones((n_components + 1, n_features)),
        'spherical': np.ones((n_components + 1))}

    # Define not positive-definite precisions
    precisions_not_pos = np.ones((n_components, n_features, n_features))
    precisions_not_pos[0] = np.eye(n_features)
    precisions_not_pos[0, 0, 0] = -1.

    precisions_not_positive = {
        'full': precisions_not_pos,
        'tied': precisions_not_pos[0],
        'diag': np.full((n_components, n_features), -1.),
        'spherical': np.full(n_components, -1.)}

    not_positive_errors = {
        'full': 'symmetric, positive-definite',
        'tied': 'symmetric, positive-definite',
        'diag': 'positive',
        'spherical': 'positive'}

    for covar_type in COVARIANCE_TYPE:
        X = RandomData(rng).X[covar_type]
        g = GaussianMixture(n_components=n_components,
                            covariance_type=covar_type,
                            random_state=rng)

        # Check precisions with bad shapes
        g.precisions_init = precisions_bad_shape[covar_type]
        assert_raise_message(ValueError,
                             "The parameter '%s precision' should have "
                             "the shape of" % covar_type,
                             g.fit, X)

        # Check not positive precisions
        g.precisions_init = precisions_not_positive[covar_type]
        assert_raise_message(ValueError,
                             "'%s precision' should be %s"
                             % (covar_type, not_positive_errors[covar_type]),
                             g.fit, X)

        # Check the correct init of precisions_init
        g.precisions_init = rand_data.precisions[covar_type]
        g.fit(X)
        assert_array_equal(rand_data.precisions[covar_type], g.precisions_init)
//  [python]
def test_regularisation():
    # We train the GaussianMixture on degenerate data by defining two clusters
    # of a 0 covariance.
    rng = np.random.RandomState(0)
    n_samples, n_features = 10, 5

    X = np.vstack((np.ones((n_samples // 2, n_features)),
                   np.zeros((n_samples // 2, n_features))))

    for covar_type in COVARIANCE_TYPE:
        gmm = mixture.GaussianMixture(n_components=n_samples, reg_covar=0,
                              covariance_type=covar_type, random_state=rng)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)
            assert_raise_message(ValueError,
                                 "Fitting the mixture model failed because "
                                 "some components have ill-defined empirical "
                                 "covariance (for instance caused by "
                                 "singleton or collapsed samples). Try to "
                                 "decrease the number of components, or "
                                 "increase reg_covar.", gmm.fit, X)

            gmm.set_params(reg_covar=1e-6).fit(X)
//  [python]
def test_regularisation():
    # We train the GaussianMixture on degenerate data by defining two clusters
    # of a 0 covariance.
    rng = np.random.RandomState(0)
    n_samples, n_features = 10, 5

    X = np.vstack((np.ones((n_samples // 2, n_features)),
                   np.zeros((n_samples // 2, n_features))))

    for covar_type in COVARIANCE_TYPE:
        gmm = GaussianMixture(n_components=n_samples, reg_covar=0,
                              covariance_type=covar_type, random_state=rng)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)
            assert_raise_message(ValueError,
                                 "Fitting the mixture model failed because "
                                 "some components have ill-defined empirical "
                                 "covariance (for instance caused by "
                                 "singleton or collapsed samples). Try to "
                                 "decrease the number of components, or "
                                 "increase reg_covar.", gmm.fit, X)

            gmm.set_params(reg_covar=1e-6).fit(X)
//  [python]
def test_score():
    covar_type = 'full'
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=7)
    n_components = rand_data.n_components
    X = rand_data.X[covar_type]

    # Check the error message if we don't call fit
    gmm1 = mixture.GaussianMixture(n_components=n_components, n_init=1,
                           max_iter=1, reg_covar=0, random_state=rng,
                           covariance_type=covar_type)
    assert_raise_message(NotFittedError,
                         "This GaussianMixture instance is not fitted "
                         "yet. Call 'fit' with appropriate arguments "
                         "before using this estimator.", gmm1.score, X)

    # Check score value
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", ConvergenceWarning)
        gmm1.fit(X)
    gmm_score = gmm1.score(X)
    gmm_score_proba = gmm1.score_samples(X).mean()
    assert_almost_equal(gmm_score, gmm_score_proba)

    # Check if the score increase
    gmm2 = mixture.GaussianMixture(n_components=n_components, n_init=1, reg_covar=0,
                           random_state=rng,
                           covariance_type=covar_type).fit(X)
    assert gmm2.score(X) > gmm1.score(X)
//  [python]
def test_score():
    covar_type = 'full'
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=7)
    n_components = rand_data.n_components
    X = rand_data.X[covar_type]

    # Check the error message if we don't call fit
    gmm1 = GaussianMixture(n_components=n_components, n_init=1,
                           max_iter=1, reg_covar=0, random_state=rng,
                           covariance_type=covar_type)
    assert_raise_message(NotFittedError,
                         "This GaussianMixture instance is not fitted "
                         "yet. Call 'fit' with appropriate arguments "
                         "before using this estimator.", gmm1.score, X)

    # Check score value
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", ConvergenceWarning)
        gmm1.fit(X)
    gmm_score = gmm1.score(X)
    gmm_score_proba = gmm1.score_samples(X).mean()
    assert_almost_equal(gmm_score, gmm_score_proba)

    # Check if the score increase
    gmm2 = GaussianMixture(n_components=n_components, n_init=1, reg_covar=0,
                           random_state=rng,
                           covariance_type=covar_type).fit(X)
    assert gmm2.score(X) > gmm1.score(X)
//  [python]
def test_init():
    # We check that by increasing the n_init number we have a better solution
    for random_state in range(15):
        rand_data = RandomData(np.random.RandomState(random_state),
                               n_samples=50, scale=1)
        n_components = rand_data.n_components
        X = rand_data.X['full']

        gmm1 = mixture.GaussianMixture(n_components=n_components, n_init=1,
                               max_iter=1, random_state=random_state).fit(X)
        gmm2 = mixture.GaussianMixture(n_components=n_components, n_init=10,
                               max_iter=1, random_state=random_state).fit(X)

        assert gmm2.lower_bound_ >= gmm1.lower_bound_
//  [python]
def test_init():
    # We check that by increasing the n_init number we have a better solution
    for random_state in range(15):
        rand_data = RandomData(np.random.RandomState(random_state),
                               n_samples=50, scale=1)
        n_components = rand_data.n_components
        X = rand_data.X['full']

        gmm1 = GaussianMixture(n_components=n_components, n_init=1,
                               max_iter=1, random_state=random_state).fit(X)
        gmm2 = GaussianMixture(n_components=n_components, n_init=10,
                               max_iter=1, random_state=random_state).fit(X)

        assert gmm2.lower_bound_ >= gmm1.lower_bound_
//  [python]
def _set_pX(self):
        # probability density function of X
        self.pX = mixture.GaussianMixture(
            n_components=self.n_mix, covariance_type="full"
        )
        self.pX.weights_ = self.w
        self.pX.means_ = self.meanX
        self.pX.covariances_ = self.covXX

        # following function is required to estimate porsterior
        self.pX.precisions_cholesky_ = _compute_precision_cholesky(self.covXX, "full")
        return
//  [python]
def _set_pX(self):
        # probability density function of X
        self.pX = sklearn.mixture.GaussianMixture(
            n_components=self.n_mix, covariance_type="full"
        )
        self.pX.weights_ = self.w
        self.pX.means_ = self.meanX
        self.pX.covariances_ = self.covXX

        # following function is required to estimate porsterior
        self.pX.precisions_cholesky_ = _compute_precision_cholesky(self.covXX, "full")
        return
//  [python]
def __init__(self, n_mix=32, n_iter=100, covtype="full"):
        self.n_mix = n_mix
        self.n_iter = n_iter
        self.covtype = covtype

        self.random_state = np.random.mtrand._rand

        # construct GMM parameter
        if self.covtype == "full":
            self.param = mixture.GaussianMixture(
                n_components=self.n_mix,
                covariance_type=self.covtype,
                max_iter=self.n_iter,
            )
        elif self.covtype == "block_diag":
            self.param = BlockDiagonalGaussianMixture(
                n_mix=self.n_mix, n_iter=self.n_iter
            )
        else:
            raise ValueError("Covariance type should be full or block_diag")
//  [python]
def __init__(self, n_mix=32, n_iter=100, covtype="full"):
        self.n_mix = n_mix
        self.n_iter = n_iter
        self.covtype = covtype

        self.random_state = np.random.mtrand._rand

        # construct GMM parameter
        if self.covtype == "full":
            self.param = sklearn.mixture.GaussianMixture(
                n_components=self.n_mix,
                covariance_type=self.covtype,
                max_iter=self.n_iter,
            )
        elif self.covtype == "block_diag":
            self.param = BlockDiagonalGaussianMixture(
                n_mix=self.n_mix, n_iter=self.n_iter
            )
        else:
            raise ValueError("Covariance type should be full or block_diag")
//  [python]
def train_singlepath(self, tar_jnt):
        """Fit GMM parameter based on single-path training
        M-step :
            Update GMM parameter using `self.log_resp`, and `tar_jnt`

        Parameters
        ----------
        tar_jnt: array, shape(`T`, `tar_dim`)
            Joint feature vector of original and target feature vector
            consisting of static and delta components, which will be modeled.

        Returns
        -------
        param :
            Sklean-based model parameters of the GMM

        """
        if self.covtype == "full":
            single_param = sklearn.mixture.GMM(
                n_components=self.n_mix, covariance_type=self.covtype, n_iter=1
            )
        elif self.covtype == "block_diag":
            single_param = BlockDiagonalGaussianMixture(
                n_mix=self.n_mix, n_iter=self.n_iter
            )
        else:
            raise ValueError("Covariance type should be full or block_diag")

        # initialize target single-path param
        single_param._initialize(tar_jnt, self.random_state)

        # perform m-step
        single_param._m_step(tar_jnt, self.log_resp)

        return single_param
//  [python]
def train_singlepath(self, tar_jnt):
        """Fit GMM parameter based on single-path training
        M-step :
            Update GMM parameter using `self.log_resp`, and `tar_jnt`

        Parameters
        ----------
        tar_jnt: array, shape(`T`, `tar_dim`)
            Joint feature vector of original and target feature vector
            consisting of static and delta components, which will be modeled.

        Returns
        -------
        param :
            Sklean-based model parameters of the GMM

        """
        if self.covtype == "full":
            single_param = sklearn.mixture.GaussianMixture(
                n_components=self.n_mix, covariance_type=self.covtype, max_iter=1
            )
        elif self.covtype == "block_diag":
            single_param = BlockDiagonalGaussianMixture(
                n_mix=self.n_mix, n_iter=self.n_iter
            )
        else:
            raise ValueError("Covariance type should be full or block_diag")

        # initialize target single-path param
        single_param._initialize_parameters(tar_jnt, self.random_state)

        # perform m-step
        single_param._m_step(tar_jnt, self.log_resp)

        return single_param
//  [python]
def __init__(self, instances, conf):
        ClusteringAlgorithm.__init__(self, instances, conf)
        self.algo = sklearn.mixture.GMM(
            n_components=self.num_clusters,
            covariance_type='diag',
            init_params='kmeans')
//  [python]
def __init__(self, instances, conf):
        ClusteringAlgorithm.__init__(self, instances, conf)
        self.algo = sklearn.mixture.GaussianMixture(
            n_components=self.num_clusters,
            covariance_type='diag',
            init_params='kmeans')
//  [python]
def scoring(scores_file, dict_file, features, eval_ndx, eval_folder, audio_ext, features_cached=True, flag_debug=False):
    logging.info('Scoring eval data')

    gmm_bona = mixture.GMM(covariance_type='diag')
    gmm_spoof = mixture.GMM(covariance_type='diag')
    with open(dict_file, "rb") as tf:
        gmm_dict = pickle.load(tf)
        gmm_bona._set_parameters(gmm_dict['bona'])
        gmm_spoof._set_parameters(gmm_dict['spoof'])

    pd = pandas.read_csv(eval_ndx, sep=' ', header=None)
    if flag_debug:
        pd = pd[:1000]

    files = pd[1].values
    scr = zeros_like(files, dtype=log(1).dtype)
    for i, file in enumerate(files):
        if (i+1) % 1000 == 0:
            logging.info("\t...%d/%d..." % (i+1, len(files)))

        try:
            Tx = extract_features(eval_folder + file + audio_ext, features=features, cached=features_cached)
            scr[i] = gmm_bona.score(Tx.T) - gmm_spoof.score(Tx.T)
        except Exception as e:
            logging.warning(e)
            scr[i] = log(1)

    pd_out = pandas.DataFrame({'files': files, 'scores': scr})
    pd_out.to_csv(scores_file, sep=' ', header=False, index=False)

    logging.info('\t... scoring completed.\n')
//  [python]
def scoring(scores_file, dict_file, features, eval_ndx, eval_folder, audio_ext, features_cached=True, flag_debug=False):
    logging.info('Scoring eval data')

    gmm_bona = GaussianMixture(covariance_type='diag')
    gmm_spoof = GaussianMixture(covariance_type='diag')
    with open(dict_file, "rb") as tf:
        gmm_dict = pickle.load(tf)
        gmm_bona._set_parameters(gmm_dict['bona'])
        gmm_spoof._set_parameters(gmm_dict['spoof'])

    pd = pandas.read_csv(eval_ndx, sep=' ', header=None)
    if flag_debug:
        pd = pd[:1000]

    files = pd[1].values
    scr = zeros_like(files, dtype=log(1).dtype)
    for i, file in enumerate(files):
        if (i+1) % 1000 == 0:
            logging.info("\t...%d/%d..." % (i+1, len(files)))

        try:
            Tx = extract_features(eval_folder + file + audio_ext, features=features, cached=features_cached)
            scr[i] = gmm_bona.score(Tx.T) - gmm_spoof.score(Tx.T)
        except Exception as e:
            logging.warning(e)
            scr[i] = log(1)

    pd_out = pandas.DataFrame({'files': files, 'scores': scr})
    pd_out.to_csv(scores_file, sep=' ', header=False, index=False)

    logging.info('\t... scoring completed.\n')
//  [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 scoring_partials(scores_file, dict_dict_files, features, eval_ndx, eval_folder, audio_ext, features_cached=True, flag_debug=False):
    logging.info('Scoring eval data')

    gmms = dict()
    for i, dict_files in dict_dict_files.items():
        gmms[i] = dict()
        gmms[i]['bona'] = mixture.GMM(covariance_type='diag')
        gmms[i]['spoof'] = mixture.GMM(covariance_type='diag')

        with open(dict_files['bona'], "rb") as tf:
            gmm_dict = pickle.load(tf)
            gmms[i]['bona']._set_parameters(gmm_dict)

        with open(dict_files['spoof'], "rb") as tf:
            gmm_dict = pickle.load(tf)
            gmms[i]['spoof']._set_parameters(gmm_dict)

    pd = pandas.read_csv(eval_ndx, sep=' ', header=None)
    if flag_debug:
        pd = pd[:1000]

    files = pd[1].values
    scr = zeros((files.shape[0], len(gmms.keys())), dtype=log(1).dtype)
    for i, file in enumerate(files):
        if (i+1) % 1000 == 0:
            logging.info("\t...%d/%d..." % (i+1, len(files)))

        try:
            Tx = extract_features(eval_folder + file + audio_ext, features=features, cached=features_cached)
            for j, jkey in enumerate(gmms.keys()):
                scr[i, j] = gmms[j]['bona'].score(Tx.T) - gmms[j]['spoof'].score(Tx.T)
        except Exception as e:
            logging.warning(e)
            for j, _ in enumerate(gmms.keys()):
                scr[i, j] = log(1)

    pd_out = pandas.DataFrame(scr).set_index(files)
    pd_out.to_csv(scores_file, sep=' ', header=False, index=True)

    logging.info('\t... scoring completed.\n')
//  [python]
def scoring_partials(scores_file, dict_dict_files, features, eval_ndx, eval_folder, audio_ext, features_cached=True, flag_debug=False):
    logging.info('Scoring eval data')

    gmms = dict()
    for i, dict_files in dict_dict_files.items():
        gmms[i] = dict()
        gmms[i]['bona'] = GaussianMixture(covariance_type='diag')
        gmms[i]['spoof'] = GaussianMixture(covariance_type='diag')

        with open(dict_files['bona'], "rb") as tf:
            gmm_dict = pickle.load(tf)
            gmms[i]['bona']._set_parameters(gmm_dict)

        with open(dict_files['spoof'], "rb") as tf:
            gmm_dict = pickle.load(tf)
            gmms[i]['spoof']._set_parameters(gmm_dict)

    pd = pandas.read_csv(eval_ndx, sep=' ', header=None)
    if flag_debug:
        pd = pd[:1000]

    files = pd[1].values
    scr = zeros((files.shape[0], len(gmms.keys())), dtype=log(1).dtype)
    for i, file in enumerate(files):
        if (i+1) % 1000 == 0:
            logging.info("\t...%d/%d..." % (i+1, len(files)))

        try:
            Tx = extract_features(eval_folder + file + audio_ext, features=features, cached=features_cached)
            for j, jkey in enumerate(gmms.keys()):
                scr[i, j] = gmms[j]['bona'].score(Tx.T) - gmms[j]['spoof'].score(Tx.T)
        except Exception as e:
            logging.warning(e)
            for j, _ in enumerate(gmms.keys()):
                scr[i, j] = log(1)

    pd_out = pandas.DataFrame(scr).set_index(files)
    pd_out.to_csv(scores_file, sep=' ', header=False, index=True)

    logging.info('\t... scoring completed.\n')
//  [python]
def add_new_model(self, templates, label):
        self.label.append(label)
        gmm = mixture.GaussianMixture(self.gmm_mixture_num, covariance_type='diag')
        gmm.fit(templates)  # label自动从0开始标号
        self.gmm_models.append(gmm)
//  [python]
def add_new_model(self, templates, label):
        self.label.append(label)
        gmm = GaussianMixture(self.gmm_mixture_num, covariance_type='diag')
        gmm.fit(templates)  # label自动从0开始标号
        self.gmm_models.append(gmm)
//  [python]
try:
    import _pickle as pickle #Python3 support
except:
    import cPickle as pickle#Python2 support
    
try:
    from speaker import GMMSet, GMM #Gpu Support
    pass
except:
    print("pycaspgmm")
    import operator
    import numpy as np
    from sklearn.mixture import GMM as GMM
    class GMMSet(object):

        def __init__(self, gmm_order = 32):
            self.gmms = []
            self.gmm_order = gmm_order
            self.y = []

        def fit_new(self, x, label):
            self.y.append(label)
            gmm = GMM()
            gmm.fit(x)
            self.gmms.append(gmm)
            
        def fit_multi_instances_same_class(self, x, label):
            
            if label in self.y:
                idx = self.y.index(label)
                gmm = self.gmms[idx]
                gmm.fit(x)
            else:    
                self.y.append(label)
                gmm = GMM(n_components=13)
                gmm.fit(x)
                self.gmms.append(gmm)
        def gmm_score(self, gmm, x):
            return np.sum(gmm.score(x))

        def predict_one(self, x):
            scores = [self.gmm_score(gmm, x) / len(x) for gmm in self.gmms]
            p = sorted(enumerate(scores), key=operator.itemgetter(1), reverse=True)
            result = [(self.y[index], value) for (index, value) in enumerate(scores)]
            p = max(result, key=operator.itemgetter(1))
            return p
//  [python]
try:
    import _pickle as pickle #Python3 support
except:
    import cPickle as pickle#Python2 support
    
try:
    from speaker import GMMSet, GMM #Gpu Support
    pass
except:
    print("pycaspgmm")
    import operator
    import numpy as np
    from sklearn.mixture import GaussianMixture as GMM
    class GMMSet(object):

        def __init__(self, gmm_order = 32):
            self.gmms = []
            self.gmm_order = gmm_order
            self.y = []

        def fit_new(self, x, label):
            self.y.append(label)
            gmm = GMM()
            gmm.fit(x)
            self.gmms.append(gmm)
            
        def fit_multi_instances_same_class(self, x, label):
            
            if label in self.y:
                idx = self.y.index(label)
                gmm = self.gmms[idx]
                gmm.fit(x)
            else:    
                self.y.append(label)
                gmm = GMM(n_components=13)
                gmm.fit(x)
                self.gmms.append(gmm)
        def gmm_score(self, gmm, x):
            return np.sum(gmm.score(x))

        def predict_one(self, x):
            scores = [self.gmm_score(gmm, x) / len(x) for gmm in self.gmms]
            p = sorted(enumerate(scores), key=operator.itemgetter(1), reverse=True)
            result = [(self.y[index], value) for (index, value) in enumerate(scores)]
            p = max(result, key=operator.itemgetter(1))
            return p
//  [python]
def _gaussian_mixture_train(table, input_cols, number_of_components=1, covariance_type='full', tolerance=0.001, \
                            regularize_covariance=1e-06, max_iteration=100, initial_params='kmeans', seed=None):

    gmm = mixture.GaussianMixture(n_components=number_of_components, covariance_type=covariance_type, tol=tolerance, \
                          reg_covar=regularize_covariance, max_iter=max_iteration, init_params=initial_params, random_state=seed)
    feature_names, X_train = check_col_type(table, input_cols)
    gmm.fit(X_train)
    
    out_table = pd.DataFrame()
    
    comp_num_arr = []
    for i in range(0, number_of_components):
        comp_num_arr.append(i)
    
    mean_arr = []
    for i in range(0, number_of_components):
        mean_arr.append(gmm.means_[i].tolist())
        
    covar_arr = []
    for i in range(0, number_of_components):
        covar_arr.append(gmm.covariances_[i].tolist())
        
    out_table['component_number'] = comp_num_arr
    out_table['weight'] = gmm.weights_
    out_table['mean_coordinate'] = mean_arr
    # temporary string type to covar_arr because it is array(array(array(double))) which is not supported so it causes problems
    out_table['covariance_matrix'] = str(covar_arr)
    
    rb = BrtcReprBuilder()
    params = { 
        'Input Columns': feature_names,
        'Number of Components': number_of_components,
        'Covariance Type': covariance_type,
        'Tolerance': tolerance,
        'Regularization of Covariance': regularize_covariance,
        'Number of Iteration': max_iteration,
        'Method to Initialize': initial_params
    }

    rb.addMD(strip_margin("""
    |## Gaussian Mixture Train Result 
    |
    |### Parameters
    |
    | {params}
    |
    |### Summary
    |
    |{result_table}
    |
    """.format(params=dict2MD(params), result_table=pandasDF2MD(out_table))))

    model = _model_dict('gaussian_mixture_train')
    model['input_cols'] = input_cols
    model['number_of_components'] = number_of_components
    model['covariance_type'] = covariance_type
    model['tolerance'] = tolerance
    model['regularize_covariance'] = regularize_covariance
    model['max_iteration'] = max_iteration
    model['initial_params'] = initial_params
    model['seed'] = seed
    model['summary'] = out_table
    model['gmm'] = gmm
    model['_repr_brtc_'] = rb.get()
    return {'model':model}
//  [python]
def _gaussian_mixture_train(table, input_cols, number_of_components=1, covariance_type='full', tolerance=0.001, \
                            regularize_covariance=1e-06, max_iteration=100, initial_params='kmeans', seed=None):

    gmm = GaussianMixture(n_components=number_of_components, covariance_type=covariance_type, tol=tolerance, \
                          reg_covar=regularize_covariance, max_iter=max_iteration, init_params=initial_params, random_state=seed)
    feature_names, X_train = check_col_type(table, input_cols)
    gmm.fit(X_train)
    
    out_table = pd.DataFrame()
    
    comp_num_arr = []
    for i in range(0, number_of_components):
        comp_num_arr.append(i)
    
    mean_arr = []
    for i in range(0, number_of_components):
        mean_arr.append(gmm.means_[i].tolist())
        
    covar_arr = []
    for i in range(0, number_of_components):
        covar_arr.append(gmm.covariances_[i].tolist())
        
    out_table['component_number'] = comp_num_arr
    out_table['weight'] = gmm.weights_
    out_table['mean_coordinate'] = mean_arr
    # temporary string type to covar_arr because it is array(array(array(double))) which is not supported so it causes problems
    out_table['covariance_matrix'] = str(covar_arr)
    
    rb = BrtcReprBuilder()
    params = { 
        'Input Columns': feature_names,
        'Number of Components': number_of_components,
        'Covariance Type': covariance_type,
        'Tolerance': tolerance,
        'Regularization of Covariance': regularize_covariance,
        'Number of Iteration': max_iteration,
        'Method to Initialize': initial_params
    }

    rb.addMD(strip_margin("""
    |## Gaussian Mixture Train Result 
    |
    |### Parameters
    |
    | {params}
    |
    |### Summary
    |
    |{result_table}
    |
    """.format(params=dict2MD(params), result_table=pandasDF2MD(out_table))))

    model = _model_dict('gaussian_mixture_train')
    model['input_cols'] = input_cols
    model['number_of_components'] = number_of_components
    model['covariance_type'] = covariance_type
    model['tolerance'] = tolerance
    model['regularize_covariance'] = regularize_covariance
    model['max_iteration'] = max_iteration
    model['initial_params'] = initial_params
    model['seed'] = seed
    model['summary'] = out_table
    model['gmm'] = gmm
    model['_repr_brtc_'] = rb.get()
    return {'model':model}
//  [python]
def _gaussian_mixture_train2(table, input_cols, number_of_components=1, prediction_col='prediction', covariance_type='full', tolerance=0.001, \
                            regularize_covariance=1e-06, max_iteration=100, initial_params='kmeans', seed=None):

    gmm = mixture.GaussianMixture(n_components=number_of_components, covariance_type=covariance_type, tol=tolerance, \
                          reg_covar=regularize_covariance, max_iter=max_iteration, init_params=initial_params, random_state=seed)
    feature_names, X_train = check_col_type(table, input_cols)
    gmm.fit(X_train)
    gmm_prediction = gmm.predict(X_train)

    out_table = pd.DataFrame()
    
    comp_num_arr = []
    for i in range(0, number_of_components):
        comp_num_arr.append(i)
    
    mean_arr = []
    for i in range(0, number_of_components):
        mean_arr.append(gmm.means_[i].tolist())
        
    covar_arr = []
    for i in range(0, number_of_components):
        covar_arr.append(gmm.covariances_[i].tolist())
        
    out_table['component_number'] = comp_num_arr
    out_table['weight'] = gmm.weights_
    out_table['mean_coordinate'] = mean_arr
    # temporary string type to covar_arr because it is array(array(array(double))) which is not supported so it causes problems
    out_table['covariance_matrix'] = str(covar_arr)
    
    rb = BrtcReprBuilder()
    params = { 
        'Input Columns': feature_names,
        'Number of Components': number_of_components,
        'Covariance Type': covariance_type,
        'Tolerance': tolerance,
        'Regularization of Covariance': regularize_covariance,
        'Number of Iteration': max_iteration,
        'Method to Initialize': initial_params
    }

    rb.addMD(strip_margin("""
    |## Gaussian Mixture Train Result 
    |
    |### Parameters
    |
    | {params}
    |
    |### Summary
    |
    |{result_table}
    |
    """.format(params=dict2MD(params), result_table=pandasDF2MD(out_table))))

    model = _model_dict('gaussian_mixture_train')
    model['input_cols'] = input_cols
    model['number_of_components'] = number_of_components
    model['covariance_type'] = covariance_type
    model['tolerance'] = tolerance
    model['regularize_covariance'] = regularize_covariance
    model['max_iteration'] = max_iteration
    model['initial_params'] = initial_params
    model['seed'] = seed
    model['summary'] = out_table
    model['gmm'] = gmm
    model['_repr_brtc_'] = rb.get()

    tmp_out_table = table.copy()
    tmp_out_table[prediction_col] = gmm_prediction

    return {'out_table':tmp_out_table, 'model':model}
//  [python]
def _gaussian_mixture_train2(table, input_cols, number_of_components=1, prediction_col='prediction', covariance_type='full', tolerance=0.001, \
                            regularize_covariance=1e-06, max_iteration=100, initial_params='kmeans', seed=None):

    gmm = GaussianMixture(n_components=number_of_components, covariance_type=covariance_type, tol=tolerance, \
                          reg_covar=regularize_covariance, max_iter=max_iteration, init_params=initial_params, random_state=seed)
    feature_names, X_train = check_col_type(table, input_cols)
    gmm.fit(X_train)
    gmm_prediction = gmm.predict(X_train)

    out_table = pd.DataFrame()
    
    comp_num_arr = []
    for i in range(0, number_of_components):
        comp_num_arr.append(i)
    
    mean_arr = []
    for i in range(0, number_of_components):
        mean_arr.append(gmm.means_[i].tolist())
        
    covar_arr = []
    for i in range(0, number_of_components):
        covar_arr.append(gmm.covariances_[i].tolist())
        
    out_table['component_number'] = comp_num_arr
    out_table['weight'] = gmm.weights_
    out_table['mean_coordinate'] = mean_arr
    # temporary string type to covar_arr because it is array(array(array(double))) which is not supported so it causes problems
    out_table['covariance_matrix'] = str(covar_arr)
    
    rb = BrtcReprBuilder()
    params = { 
        'Input Columns': feature_names,
        'Number of Components': number_of_components,
        'Covariance Type': covariance_type,
        'Tolerance': tolerance,
        'Regularization of Covariance': regularize_covariance,
        'Number of Iteration': max_iteration,
        'Method to Initialize': initial_params
    }

    rb.addMD(strip_margin("""
    |## Gaussian Mixture Train Result 
    |
    |### Parameters
    |
    | {params}
    |
    |### Summary
    |
    |{result_table}
    |
    """.format(params=dict2MD(params), result_table=pandasDF2MD(out_table))))

    model = _model_dict('gaussian_mixture_train')
    model['input_cols'] = input_cols
    model['number_of_components'] = number_of_components
    model['covariance_type'] = covariance_type
    model['tolerance'] = tolerance
    model['regularize_covariance'] = regularize_covariance
    model['max_iteration'] = max_iteration
    model['initial_params'] = initial_params
    model['seed'] = seed
    model['summary'] = out_table
    model['gmm'] = gmm
    model['_repr_brtc_'] = rb.get()

    tmp_out_table = table.copy()
    tmp_out_table[prediction_col] = gmm_prediction

    return {'out_table':tmp_out_table, 'model':model}
//  [python]
def fit(
        self,
        X: np.ndarray,
        y: Optional[np.ndarray] = None,
    ) -> 'GaussianMixtureRandomCenters':
        # noqa: D102
        X = sklearn.utils.validation.check_array(X)
        self.n_features_in_ = X.shape[1]
        # Clone and fit estimator
        self.estimator_ = (sklearn.base.clone(self.estimator) if self.estimator
                           is not None else sklearn.mixture.GMM(
                               n_components=2))
        self.estimator_.fit(X)
        # Sample fit distribution
        self.centers_ = self.estimator_.sample(self.n_centers)[0]
        self.n_centers_ = self.centers_.shape[0]
        return self
//  [python]
def fit(
        self,
        X: np.ndarray,
        y: Optional[np.ndarray] = None,
    ) -> 'GaussianMixtureRandomCenters':
        # noqa: D102
        X = sklearn.utils.validation.check_array(X)
        self.n_features_in_ = X.shape[1]
        # Clone and fit estimator
        self.estimator_ = (sklearn.base.clone(self.estimator) if self.estimator
                           is not None else sklearn.mixture.GaussianMixture(
                               n_components=2))
        self.estimator_.fit(X)
        # Sample fit distribution
        self.centers_ = self.estimator_.sample(self.n_centers)[0]
        self.n_centers_ = self.centers_.shape[0]
        return self
//  [python]
def __init__(self, use_aic=True):
        self.gmm_wrapper_dict = {k: mixture.GaussianMixture(n_components=k, random_state=12, n_init=2) for k in range(0, 10)}
        self.use_aic = use_aic
//  [python]
def __init__(self, use_aic=True):
        self.gmm_wrapper_dict = {k: GaussianMixture(n_components=k, random_state=12, n_init=2) for k in range(0, 10)}
        self.use_aic = use_aic
//  [python]
def gmm_estimator(features_np_all, label_np_all):
  """Estimate the GMM posterior assignment."""
  pca_model = sd.PCA(n_components=0.8)
  pca_model.fit(features_np_all)
  features_lowdim_train = pca_model.transform(features_np_all)

  num_examples = label_np_all.shape[0]
  y_classes = max([min([label_np_all.max() + 1, int(num_examples * 0.2)]),
                   int(num_examples * 0.1)])
  clf = s.GaussianMixture(n_components=y_classes)
  clf.fit(features_lowdim_train)
  prob_np_all_gmm = clf.predict_proba(features_lowdim_train)
  return prob_np_all_gmm, features_lowdim_train
//  [python]
def gmm_estimator(features_np_all, label_np_all):
  """Estimate the GMM posterior assignment."""
  pca_model = sd.PCA(n_components=0.8)
  pca_model.fit(features_np_all)
  features_lowdim_train = pca_model.transform(features_np_all)

  num_examples = label_np_all.shape[0]
  y_classes = max([min([label_np_all.max() + 1, int(num_examples * 0.2)]),
                   int(num_examples * 0.1)])
  clf = sm.GaussianMixture(n_components=y_classes)
  clf.fit(features_lowdim_train)
  prob_np_all_gmm = clf.predict_proba(features_lowdim_train)
  return prob_np_all_gmm, features_lowdim_train
//  [python]
def NLEEP(X, y, component_ratio=5):

    n = len(y)
    num_classes = len(np.unique(y))
    # PCA: keep 80% energy
    pca_80 = PCA(n_components=0.8)
    pca_80.fit(X)
    X_pca_80 = pca_80.transform(X)

    # GMM: n_components = component_ratio * class number
    n_components_num = component_ratio * num_classes
    gmm = mixture.GaussianMixture(n_components= n_components_num).fit(X_pca_80)
    prob = gmm.predict_proba(X_pca_80)  # p(z|x)
    
    # NLEEP
    pyz = np.zeros((num_classes, n_components_num))
    for y_ in range(num_classes):
        indices = np.where(y == y_)[0]
        filter_ = np.take(prob, indices, axis=0) 
        pyz[y_] = np.sum(filter_, axis=0) / n   
    pz = np.sum(pyz, axis=0)    
    py_z = pyz / pz             
    py_x = np.dot(prob, py_z.T) 

    # nleep_score
    nleep_score = np.sum(py_x[np.arange(n), y]) / n
    return nleep_score
//  [python]
def NLEEP(X, y, component_ratio=5):

    n = len(y)
    num_classes = len(np.unique(y))
    # PCA: keep 80% energy
    pca_80 = PCA(n_components=0.8)
    pca_80.fit(X)
    X_pca_80 = pca_80.transform(X)

    # GMM: n_components = component_ratio * class number
    n_components_num = component_ratio * num_classes
    gmm = GaussianMixture(n_components= n_components_num).fit(X_pca_80)
    prob = gmm.predict_proba(X_pca_80)  # p(z|x)
    
    # NLEEP
    pyz = np.zeros((num_classes, n_components_num))
    for y_ in range(num_classes):
        indices = np.where(y == y_)[0]
        filter_ = np.take(prob, indices, axis=0) 
        pyz[y_] = np.sum(filter_, axis=0) / n   
    pz = np.sum(pyz, axis=0)    
    py_z = pyz / pz             
    py_x = np.dot(prob, py_z.T) 

    # nleep_score
    nleep_score = np.sum(py_x[np.arange(n), y]) / n
    return nleep_score
//  [python]
def test_tf_gmm_equivalent_to_sklearn_gmm(self, init_params):
    """Test equivalency of jmixture.GaussianMixture vs sklearn.mixture.GaussianMixture."""

    X = np.array(np.random.standard_normal((5, 4)))
    n_components = 3
    random_state = 1

    gm = mixture.GaussianMixture(
        n_components=n_components,
        init_params=init_params,
        random_state=random_state)

    tfgm = tf_gaussian_mixture.GaussianMixture(
        n_components=n_components,
        init_params=init_params,
        random_state=random_state)

    gm.fit(X)
    tfgm.fit(X)

    self.assertAllClose(gm.means_, tfgm.means_, rtol=1e-4)
    self.assertAllClose(gm.covariances_, tfgm.covariances_, rtol=1e-4)
    self.assertAllClose(gm.predict(X), tfgm.predict(X), rtol=1e-4)
//  [python]
def test_tf_gmm_equivalent_to_sklearn_gmm(self, init_params):
    """Test equivalency of jmixture.GaussianMixture vs sklearn.mixture.GaussianMixture."""

    X = np.array(np.random.standard_normal((5, 4)))
    n_components = 3
    random_state = 1

    gm = sklearn.mixture.GaussianMixture(
        n_components=n_components,
        init_params=init_params,
        random_state=random_state)

    tfgm = tf_gaussian_mixture.GaussianMixture(
        n_components=n_components,
        init_params=init_params,
        random_state=random_state)

    gm.fit(X)
    tfgm.fit(X)

    self.assertAllClose(gm.means_, tfgm.means_, rtol=1e-4)
    self.assertAllClose(gm.covariances_, tfgm.covariances_, rtol=1e-4)
    self.assertAllClose(gm.predict(X), tfgm.predict(X), rtol=1e-4)
//  [python]
def assign_gaussian(data, n_components, covariance_type,
	init_params='kmeans',
	n_init=50,
	):
	classifier = mixture.GaussianMixture(
		n_components=n_components,
		covariance_type=covariance_type,
		init_params=init_params,
		n_init=n_init,
		)
	classifier.fit(data)
	aic = classifier.aic(data)
	bic = classifier.bic(data)
	assignment = classifier.predict(data)
	return assignment, classifier, aic, bic
//  [python]
def assign_gaussian(data, n_components, covariance_type,
	init_params='kmeans',
	n_init=50,
	):
	classifier = GaussianMixture(
		n_components=n_components,
		covariance_type=covariance_type,
		init_params=init_params,
		n_init=n_init,
		)
	classifier.fit(data)
	aic = classifier.aic(data)
	bic = classifier.bic(data)
	assignment = classifier.predict(data)
	return assignment, classifier, aic, bic
//  [python]
def GM(self):
        try:
            nclusters = self.kw['nclusters']
            if not self.dataframe is None:
                self.cluster_result = mixture.GaussianMixture(n_components=nclusters).fit_predict(self.dataframe.values)
                self.cluster_plot_data, ok = self.t_sne(2)
                return True
            else:
                self.error_msg = 'Data is null.'
                return False
        except Exception as e:
            self.error_msg = str(e)
            return False