jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def run_GMM(data_to_fit, data_to_predict, k, seed=1):
    """Apply the GaussianMixtureModel on a dataset.
    For more see: https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html
    Args:
        data_to_fit (int): The dataset
        data_to_predict (int): The dataset for prediction
        k (int): The number of components.
        seed (int): Random seed for initialisation

    Returns:
        [type]: [description]
    """
    gmm = GaussianMixture(n_components=k, covariance_type="full", random_state=seed)
    labels = gmm.fit_predict(data_to_fit)
    if data_to_predict is None:
        preds = None
    else:
        preds = gmm.predict(data_to_predict)
    return labels, preds
//  [python]
def fit(self, X_train, y_train):
        X_train = np.asarray(X_train)
        y_train = np.asarray(y_train)
        from sklearn.mixture import GMM as GaussianMixture

        unlabels = range(0, np.max(y_train) + 1)

        for lab in unlabels:
            if self.each_class_params is not None:
                # print 'eacl'
                # print self.each_class_params[lab]
                model = GaussianMixture(**self.each_class_params[lab])
                # print 'po gmm ', model
            elif len(self.same_params) > 0:
                model = GaussianMixture(**self.same_params)
                # print 'ewe ', model
            else:
                model = GaussianMixture()
            X_train_lab = X_train[y_train == lab]
            # logger.debug('xtr lab shape ' + str(X_train_lab))
            model.fit(X_train_lab)

            self.models.insert(lab, model)
//  [python]
def fit(self, X_train, y_train):
        X_train = np.asarray(X_train)
        y_train = np.asarray(y_train)
        # from sklearn.mixture import GMM as GaussianMixture
        from sklearn.mixture import GaussianMixture

        unlabels = range(0, np.max(y_train) + 1)

        for lab in unlabels:
            if self.each_class_params is not None:
                # print 'eacl'
                # print self.each_class_params[lab]
                model = GaussianMixture(**self.each_class_params[lab])
                # print 'po gmm ', model
            elif len(self.same_params) > 0:
                model = GaussianMixture(**self.same_params)
                # print 'ewe ', model
            else:
                model = GaussianMixture()
            X_train_lab = X_train[y_train == lab]
            # logger.debug('xtr lab shape ' + str(X_train_lab))
            model.fit(X_train_lab)

            self.models.insert(lab, model)
//  [python]
def __init__(self, *args, **kwargs):
        if "logs" in kwargs:
            self.logs = kwargs["logs"]
            del kwargs["logs"]
        else:
            self.logs = []
        if "eps" in kwargs:
            self.eps = kwargs["eps"]
            del kwargs["eps"]
        else:
            self.eps = 1e-10
        self.max_vals = None
        self.gmm = mixture.GaussianMixture(*args, **kwargs)
//  [python]
def __init__(self, *args, **kwargs):
        if "logs" in kwargs:
            self.logs = kwargs["logs"]
            del kwargs["logs"]
        else:
            self.logs = []
        if "eps" in kwargs:
            self.eps = kwargs["eps"]
            del kwargs["eps"]
        else:
            self.eps = 1e-10
        self.max_vals = None
        self.gmm = GaussianMixture(*args, **kwargs)
//  [python]
def _initial_guess(self):
        ## Initial guess for the multi-gaussian fit
        ## The idea is to draw random samples using the data as a PDF, then run a
        ## Gaussian mixture model (ie. clustering) to get a good initial guess for the gaussians.
        ## Note that this is pretty slow...

        B = 0.5*(self.ypts[-1] + self.ypts[0])
        #normalize and center
        y = self.ypts - B
        mask = y>0
        x0 = self.xpts[np.argmax(y)]
        xn = self.xpts[mask] - x0
        yn = y[mask] / np.sum(y[mask])
        #Generate random samples
        samples = np.random.choice(a=xn, size=self.n_samples, p=yn)
        gmm = mixture.GaussianMixture(n_components=self.n_gaussians)
        gmm.fit(samples.reshape(len(samples),1))
        means = gmm.means_.flatten() + x0
        sigmas = np.sqrt(gmm.covariances_.flatten())
        amps = gmm.weights_.flatten() * (2*np.pi)
        output = np.zeros(1+3*self.n_gaussians)
        output[0] = B
        for j in range(self.n_gaussians):
            output[3*j+1] = amps[j]
            output[3*j+2] = means[j]
            output[3*j+3] = sigmas[j]

        return output
//  [python]
def _initial_guess(self):
        ## Initial guess for the multi-gaussian fit
        ## The idea is to draw random samples using the data as a PDF, then run a
        ## Gaussian mixture model (ie. clustering) to get a good initial guess for the gaussians.
        ## Note that this is pretty slow...

        B = 0.5*(self.ypts[-1] + self.ypts[0])
        #normalize and center
        y = self.ypts - B
        mask = y>0
        x0 = self.xpts[np.argmax(y)]
        xn = self.xpts[mask] - x0
        yn = y[mask] / np.sum(y[mask])
        #Generate random samples
        samples = np.random.choice(a=xn, size=self.n_samples, p=yn)
        gmm = GaussianMixture(n_components=self.n_gaussians)
        gmm.fit(samples.reshape(len(samples),1))
        means = gmm.means_.flatten() + x0
        sigmas = np.sqrt(gmm.covariances_.flatten())
        amps = gmm.weights_.flatten() * (2*np.pi)
        output = np.zeros(1+3*self.n_gaussians)
        output[0] = B
        for j in range(self.n_gaussians):
            output[3*j+1] = amps[j]
            output[3*j+2] = means[j]
            output[3*j+3] = sigmas[j]

        return output
//  [python]
def get_kmeans(self):
    model = sklearn.cluster.KMeans(n_clusters=self.num_clusters)
    poses = []
    for batch in tqdm(self.data):
      pose = batch[self.variable]
      pose = self.remove_joints(pose)
      pose = self.get_feats(pose)
      pose = pose.view(-1, pose.shape[-1])
      poses.append(pose)
    pose = torch.cat(poses, dim=0)
    model.fit(pose)
    centers = model.cluster_centers_
    return centers
//  [python]
def get_kmeans(self):
    model = sklearn.mixture.GaussianMixture(n_clusters=self.num_clusters)
    poses = []
    for batch in tqdm(self.data):
      pose = batch[self.variable]
      pose = self.remove_joints(pose)
      pose = self.get_feats(pose)
      pose = pose.view(-1, pose.shape[-1])
      poses.append(pose)
    pose = torch.cat(poses, dim=0)
    model.fit(pose)
    centers = model.cluster_centers_
    return centers
//  [python]
x = pd.read_csv(file_name, index_col=0)

# standardize x
if do_autoscaling:
    autoscaled_x = (x - x.mean(axis=0)) / x.std(axis=0, ddof=1)
else:
    autoscaled_x = x.copy()

# grid search using BIC
bic_values = np.empty([max_number_of_components, len(covariance_types)])
for covariance_type_index, covariance_type in enumerate(covariance_types):
    for number_of_components in range(max_number_of_components):
        gmm_model = mixture.GaussianMixture(n_components=number_of_components + 1, covariance_type=covariance_type)
        gmm_model.fit(autoscaled_x)
        bic_values[number_of_components, covariance_type_index] = gmm_model.bic(autoscaled_x)
//  [python]
x = pd.read_csv(file_name, index_col=0)

# standardize x
if do_autoscaling:
    autoscaled_x = (x - x.mean(axis=0)) / x.std(axis=0, ddof=1)
else:
    autoscaled_x = x.copy()

# grid search using BIC
bic_values = np.empty([max_number_of_components, len(covariance_types)])
for covariance_type_index, covariance_type in enumerate(covariance_types):
    for number_of_components in range(max_number_of_components):
        gmm_model = GaussianMixture(n_components=number_of_components + 1, covariance_type=covariance_type)
        gmm_model.fit(autoscaled_x)
        bic_values[number_of_components, covariance_type_index] = gmm_model.bic(autoscaled_x)
//  [python]
def __init__(self, net, loader):
        self.net = net
        self.net.eval()

        loss_list = []
        with torch.no_grad():
            for batch_idx, batch in enumerate(loader):
                data_ori, data_aug, label = batch[0] + [batch[1], ]
                data_ori, data_aug, label = data_ori.cuda(), data_aug.cuda(), label.cuda()

                preds = self.net(data_ori)
                loss = F.cross_entropy(preds, label, reduction='none')
                loss_list.append(loss)

                preds = self.net(data_aug)
                loss = F.cross_entropy(preds, label, reduction='none')
                loss_list.append(loss)

            loss_list = torch.log(torch.cat(loss_list, dim=0) + 1e-10)

        self.gmm = mixture.GaussianMixture(n_components=2, max_iter=10, tol=1e-2, reg_covar=5e-4)
        self.gmm.fit(loss_list.view(-1, 1).cpu().numpy())
//  [python]
def __init__(self, net, loader):
        self.net = net
        self.net.eval()

        loss_list = []
        with torch.no_grad():
            for batch_idx, batch in enumerate(loader):
                data_ori, data_aug, label = batch[0] + [batch[1], ]
                data_ori, data_aug, label = data_ori.cuda(), data_aug.cuda(), label.cuda()

                preds = self.net(data_ori)
                loss = F.cross_entropy(preds, label, reduction='none')
                loss_list.append(loss)

                preds = self.net(data_aug)
                loss = F.cross_entropy(preds, label, reduction='none')
                loss_list.append(loss)

            loss_list = torch.log(torch.cat(loss_list, dim=0) + 1e-10)

        self.gmm = GaussianMixture(n_components=2, max_iter=10, tol=1e-2, reg_covar=5e-4)
        self.gmm.fit(loss_list.view(-1, 1).cpu().numpy())
//  [python]
def velGMM(ampV, parameters, projectPath, saveplot=True):
    if parameters.method == 'TSNE':
        if parameters.waveletDecomp:
            tsnefolder = projectPath + '/TSNE/'
        else:
            tsnefolder = projectPath + '/TSNE_Projections/'
    else:
        tsnefolder = projectPath+'/UMAP/'
    ampVels = ampV * parameters['samplingFreq']
    vellog10all = np.log10(ampVels[ampVels > 0])
    npoints = min(50000, len(vellog10all))

    vellog10 = np.random.choice(vellog10all, size=npoints, replace=False)


    gm = mixture.GaussianMixture(n_components=2, verbose=1, tol=1e-5, max_iter=2000, n_init=1, reg_covar=1e-3)
    inds = np.random.randint(0, vellog10.shape[0], size=npoints)
    gm = gm.fit(vellog10[inds, None])
    minind = np.argmin(gm.means_.squeeze())

    if saveplot:
        bend = plt.get_backend()
        plt.switch_backend('Agg')
        fig, ax = plt.subplots(figsize=(8, 8))
        bins = ax.hist(vellog10, bins=200, density=True, color='k', alpha=0.5)
        bins = bins[1]
        p_score = np.exp(gm.score_samples(bins[:, None]))
        ax.plot(bins, p_score, color='k', alpha=0.5)

        for (c, compno, mu, sigma, p) in \
                zip(['royalblue', 'firebrick'], [1, 2], gm.means_.squeeze(), np.sqrt(gm.covariances_.squeeze()),
                    gm.weights_):
            ax.plot(bins, getPDF(bins, mu, sigma, p), label='Component %i' % compno, color=c, alpha=0.5)

        ax.plot(bins, gm.predict_proba(bins[:, None])[:, minind], label='pRest')
        ax.axvline(bins[np.where(gm.predict_proba(bins[:, None])[:, minind] < np.min(parameters.pThreshold))[0][0]], color='firebrick',
                   label='pRest=%0.2f'%np.min(parameters.pThreshold))
        ax.legend()
        ax.set_xlabel(r'$log_{10}$ Velocity')
        ax.set_ylabel('PDF')

        fig.savefig(tsnefolder + 'zVelocity.png', )
        plt.close()
        plt.switch_backend(bend)

    pRest = np.zeros_like(ampVels)
    pRest[ampVels == 0] = 0.0
    pRest[ampVels > 0] = gm.predict_proba(vellog10all[:, None])[:, minind]
    return ampV, pRest
//  [python]
def velGMM(ampV, parameters, projectPath, saveplot=True):
    if parameters.method == 'TSNE':
        if parameters.waveletDecomp:
            tsnefolder = projectPath + '/TSNE/'
        else:
            tsnefolder = projectPath + '/TSNE_Projections/'
    else:
        tsnefolder = projectPath+'/UMAP/'
    ampVels = ampV * parameters['samplingFreq']
    vellog10all = np.log10(ampVels[ampVels > 0])
    npoints = min(50000, len(vellog10all))

    vellog10 = np.random.choice(vellog10all, size=npoints, replace=False)


    gm = GaussianMixture(n_components=2, verbose=1, tol=1e-5, max_iter=2000, n_init=1, reg_covar=1e-3)
    inds = np.random.randint(0, vellog10.shape[0], size=npoints)
    gm = gm.fit(vellog10[inds, None])
    minind = np.argmin(gm.means_.squeeze())

    if saveplot:
        bend = plt.get_backend()
        plt.switch_backend('Agg')
        fig, ax = plt.subplots(figsize=(8, 8))
        bins = ax.hist(vellog10, bins=200, density=True, color='k', alpha=0.5)
        bins = bins[1]
        p_score = np.exp(gm.score_samples(bins[:, None]))
        ax.plot(bins, p_score, color='k', alpha=0.5)

        for (c, compno, mu, sigma, p) in \
                zip(['royalblue', 'firebrick'], [1, 2], gm.means_.squeeze(), np.sqrt(gm.covariances_.squeeze()),
                    gm.weights_):
            ax.plot(bins, getPDF(bins, mu, sigma, p), label='Component %i' % compno, color=c, alpha=0.5)

        ax.plot(bins, gm.predict_proba(bins[:, None])[:, minind], label='pRest')
        ax.axvline(bins[np.where(gm.predict_proba(bins[:, None])[:, minind] < np.min(parameters.pThreshold))[0][0]], color='firebrick',
                   label='pRest=%0.2f'%np.min(parameters.pThreshold))
        ax.legend()
        ax.set_xlabel(r'$log_{10}$ Velocity')
        ax.set_ylabel('PDF')

        fig.savefig(tsnefolder + 'zVelocity.png', )
        plt.close()
        plt.switch_backend(bend)

    pRest = np.zeros_like(ampVels)
    pRest[ampVels == 0] = 0.0
    pRest[ampVels > 0] = gm.predict_proba(vellog10all[:, None])[:, minind]
    return ampV, pRest
//  [python]
def learn_gmm(
    features: NDArray,
    labels: NDArray,
    n_mixtures: int,
    cov_type: str,
    seed: int,
    tol: float = 1e-12,
    max_iter: int = 1000,
) -> List[GMMParameters]:
    """Learn a list of 16-bits GMMs for each label.

    Parameters
    ----------
    features : NDArray
        A 2-d array with size (n_samples, feature_dimension) containing
        extracted features for all the samples.
    labels : NDArray
        An array with size (n_samples) containing labels associated for
        each sample in `features`.
    n_mixtures : int
        Number of mixtures in each Gaussian Mixture.
    cov_type : str
        Covariance type of Gaussian Mixtures, e.g. spherical.
    seed: int
        Seed for learning and sampling from Gaussian Mixtures.
    tol: float
        Tolerance of Gaussian Mixtures.
    max_iter: int
        Number of maximum iterations to learn the Gaussian Mixtures.

    Returns
    -------
    List[GMMParameters]
        Returns a list containing the GMMParameters for each class.
    """
    gmm_list = []
    for label in np.unique(labels):
        cond_features = features[label == labels]
        if (
            len(cond_features) > n_mixtures
        ):  # number of samples should be larger than `n_mixtures`.
            gmm = mixture.GMM(
                n_components=n_mixtures,
                covariance_type=cov_type,
                random_state=seed,
                tol=tol,
                max_iter=max_iter,
            )
            gmm.fit(cond_features)
            gmm_list.append(
                GMMParameters(
                    label=np.array(label),
                    means=gmm.means_.astype("float16"),
                    weights=gmm.weights_.astype("float16"),
                    covariances=gmm.covariances_.astype("float16"),
                    num_samples=np.array(len(cond_features)),
                )
            )
    return gmm_list
//  [python]
def learn_gmm(
    features: NDArray,
    labels: NDArray,
    n_mixtures: int,
    cov_type: str,
    seed: int,
    tol: float = 1e-12,
    max_iter: int = 1000,
) -> List[GMMParameters]:
    """Learn a list of 16-bits GMMs for each label.

    Parameters
    ----------
    features : NDArray
        A 2-d array with size (n_samples, feature_dimension) containing
        extracted features for all the samples.
    labels : NDArray
        An array with size (n_samples) containing labels associated for
        each sample in `features`.
    n_mixtures : int
        Number of mixtures in each Gaussian Mixture.
    cov_type : str
        Covariance type of Gaussian Mixtures, e.g. spherical.
    seed: int
        Seed for learning and sampling from Gaussian Mixtures.
    tol: float
        Tolerance of Gaussian Mixtures.
    max_iter: int
        Number of maximum iterations to learn the Gaussian Mixtures.

    Returns
    -------
    List[GMMParameters]
        Returns a list containing the GMMParameters for each class.
    """
    gmm_list = []
    for label in np.unique(labels):
        cond_features = features[label == labels]
        if (
            len(cond_features) > n_mixtures
        ):  # number of samples should be larger than `n_mixtures`.
            gmm = GaussianMixture(
                n_components=n_mixtures,
                covariance_type=cov_type,
                random_state=seed,
                tol=tol,
                max_iter=max_iter,
            )
            gmm.fit(cond_features)
            gmm_list.append(
                GMMParameters(
                    label=np.array(label),
                    means=gmm.means_.astype("float16"),
                    weights=gmm.weights_.astype("float16"),
                    covariances=gmm.covariances_.astype("float16"),
                    num_samples=np.array(len(cond_features)),
                )
            )
    return gmm_list
//  [python]
def fit(self):
        """
        Fits the Viterbi neuralNetwork and fits 
        output PDF using GaussianMixture fitting
        :param none
        :return none
        """
        length = (self._dataset.get_train_data().shape[0])
        print(length)
        print(self._dataset.get_train_data().shape)
        # Computes output PDF using GMM fitting
        print("Training of Gaussian model in progress")
        self._gaussian_model  = mixture.GaussianMixture(n_components = self._number_of_classes)
        self._gaussian_model.fit(self._dataset.get_train_data().reshape(length,1))
        print("Training of Gaussian model done")
        # Check if there is model to be loaded 
        if(self._pre_train == False):
            self._network_model.compile_model()
            self._network_model.fit_model()
        else:
            self._network_model.load_model(self._load_path)
//  [python]
def fit(self):
        """
        Fits the Viterbi neuralNetwork and fits 
        output PDF using GaussianMixture fitting
        :param none
        :return none
        """
        length = (self._dataset.get_train_data().shape[0])
        print(length)
        print(self._dataset.get_train_data().shape)
        # Computes output PDF using GMM fitting
        print("Training of Gaussian model in progress")
        self._gaussian_model  = GaussianMixture(n_components = self._number_of_classes)
        self._gaussian_model.fit(self._dataset.get_train_data().reshape(length,1))
        print("Training of Gaussian model done")
        # Check if there is model to be loaded 
        if(self._pre_train == False):
            self._network_model.compile_model()
            self._network_model.fit_model()
        else:
            self._network_model.load_model(self._load_path)
//  [python]
def eval_train(model,all_loss):    
    model.eval()
    losses = torch.zeros(50000)    
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            outputs = model(inputs) 
            loss = CE(outputs, targets)  
            for b in range(inputs.size(0)):
                losses[index[b]]=loss[b]         
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    all_loss.append(losses)

    if args.r==0.9: # average loss over last 5 epochs to improve convergence stability
        history = torch.stack(all_loss)
        input_loss = history[-5:].mean(0)
        input_loss = input_loss.reshape(-1,1)
    else:
        input_loss = losses.reshape(-1,1)
    
    # fit a two-component GMM to the loss
    gmm = mixture.GaussianMixture(n_components=2,max_iter=10,tol=1e-2,reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss) 
    prob = prob[:,gmm.means_.argmin()]         
    return prob,all_loss
//  [python]
def eval_train(model,all_loss):    
    model.eval()
    losses = torch.zeros(50000)    
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            outputs = model(inputs) 
            loss = CE(outputs, targets)  
            for b in range(inputs.size(0)):
                losses[index[b]]=loss[b]         
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    all_loss.append(losses)

    if args.r==0.9: # average loss over last 5 epochs to improve convergence stability
        history = torch.stack(all_loss)
        input_loss = history[-5:].mean(0)
        input_loss = input_loss.reshape(-1,1)
    else:
        input_loss = losses.reshape(-1,1)
    
    # fit a two-component GMM to the loss
    gmm = GaussianMixture(n_components=2,max_iter=10,tol=1e-2,reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss) 
    prob = prob[:,gmm.means_.argmin()]         
    return prob,all_loss
//  [python]
def test_gaussian_mixture_attributes():
    # test bad parameters
    rng = np.random.RandomState(0)
    X = rng.rand(10, 2)

    # 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 = mixture.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_attributes():
    # test bad parameters
    rng = np.random.RandomState(0)
    X = rng.rand(10, 2)

    # 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 main():
    """Run the IDS using GMM experiment."""
    week3Data = _parseTrainingData()

    # Scale the training data (ignore the timestamp column)
    scaler = preprocessing.RobustScaler().fit(week3Data[:, 1:])
    X_train = scaler.transform(week3Data[:, 1:])
    del week3Data

    try:
        gmm = pickle.load(open("data/gmm.pkl", "rb"))
        print("Loading pre-trained GMM...")
    except IOError:
        print("Training the Gaussian Mixture...")
        gmm = mixture.GaussianMixture(n_components=16,
                              covariance_type='full',
                              #  reg_covar=1,
                              verbose=1,
                              verbose_interval=2).fit(X_train)
        pickle.dump(gmm, open("data/gmm.pkl", "wb"))
    del X_train

    X_orig = _parseTestingData()
    print("Scaling the test data...")
    X_test = scaler.transform(X_orig[:, 1:])

    print("Calculating prosterior probabilies of test data...")
    probs = gmm.predict_proba(X_test)
    del X_test

    scores = _score(probs)
    del probs

    results = np.hstack((X_orig, scores.reshape((scores.shape[0], 1))))

    _outputToCSV(results, "data/gmm_results_max.csv")
//  [python]
def main():
    """Run the IDS using GMM experiment."""
    week3Data = _parseTrainingData()

    # Scale the training data (ignore the timestamp column)
    scaler = preprocessing.RobustScaler().fit(week3Data[:, 1:])
    X_train = scaler.transform(week3Data[:, 1:])
    del week3Data

    try:
        gmm = pickle.load(open("data/gmm.pkl", "rb"))
        print("Loading pre-trained GMM...")
    except IOError:
        print("Training the Gaussian Mixture...")
        gmm = GaussianMixture(n_components=16,
                              covariance_type='full',
                              #  reg_covar=1,
                              verbose=1,
                              verbose_interval=2).fit(X_train)
        pickle.dump(gmm, open("data/gmm.pkl", "wb"))
    del X_train

    X_orig = _parseTestingData()
    print("Scaling the test data...")
    X_test = scaler.transform(X_orig[:, 1:])

    print("Calculating prosterior probabilies of test data...")
    probs = gmm.predict_proba(X_test)
    del X_test

    scores = _score(probs)
    del probs

    results = np.hstack((X_orig, scores.reshape((scores.shape[0], 1))))

    _outputToCSV(results, "data/gmm_results_max.csv")
//  [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
    msg = "The parameter 'means' should have the shape of "
    with pytest.raises(ValueError, match=msg):
        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
    msg = "The parameter 'means' should have the shape of "
    with pytest.raises(ValueError, match=msg):
        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 cluster(self, n_clusters, load_clusters=False, save_dir="", visualize_data=False):
        self.n_clusters = n_clusters

        x_pruned = self.x
        y_pruned = self.y

        file_name = os.path.join(save_dir, 'gmm.pkl')
        loaded = False
        gmm = None
        if os.path.exists(file_name) and load_clusters:
            print("Loaded GP clusters from last GP training session. File: {}".format(file_name))
            gmm = joblib.load(file_name)
            if gmm.n_components != n_clusters:
                print("The loaded GP does not coincide with the number of set clusters: Found {} but requested"
                      "is {}".format(gmm.n_components, n_clusters))
            else:
                loaded = True
        if not loaded:
            gmm = mixture.GaussianMixture(n_clusters).fit(x_pruned)
        clusters = gmm.predict_proba(x_pruned)
        centroids = gmm.means_

        if not load_clusters and n_clusters > 1:
            safe_mknode_recursive(save_dir, 'gmm.pkl', overwrite=True)
            joblib.dump(gmm, file_name)

        index_aux = np.arange(0, clusters.shape[0])
        cluster_agency = {}

        # Add to the points corresponding to each cluster the points that correspond to it with top 2 probability,
        # if this probability is high
        for cluster in range(n_clusters):
            top_1 = np.argmax(clusters, 1)
            clusters_ = copy.deepcopy(clusters)
            clusters_[index_aux, top_1] *= 0
            top_2 = np.argmax(clusters_, 1)
            idx = np.where(top_1 == cluster)[0]
            idx = np.append(idx, np.where((top_2 == cluster) * (clusters_[index_aux, top_2] > 0.2))[0])
            cluster_agency[cluster] = idx

        # Only works in len(x_features) >= 3
        if visualize_data:
            x_aux = self.get_x(pruned=False)
            y_aux = self.get_y(pruned=False)
            visualize_data_distribution(x_aux, y_aux, cluster_agency, x_pruned, y_pruned)

        self.cluster_agency = cluster_agency
        self.centroids = centroids
//  [python]
def cluster(self, n_clusters, load_clusters=False, save_dir="", visualize_data=False):
        self.n_clusters = n_clusters

        x_pruned = self.x
        y_pruned = self.y

        file_name = os.path.join(save_dir, 'gmm.pkl')
        loaded = False
        gmm = None
        if os.path.exists(file_name) and load_clusters:
            print("Loaded GP clusters from last GP training session. File: {}".format(file_name))
            gmm = joblib.load(file_name)
            if gmm.n_components != n_clusters:
                print("The loaded GP does not coincide with the number of set clusters: Found {} but requested"
                      "is {}".format(gmm.n_components, n_clusters))
            else:
                loaded = True
        if not loaded:
            gmm = GaussianMixture(n_clusters).fit(x_pruned)
        clusters = gmm.predict_proba(x_pruned)
        centroids = gmm.means_

        if not load_clusters and n_clusters > 1:
            safe_mknode_recursive(save_dir, 'gmm.pkl', overwrite=True)
            joblib.dump(gmm, file_name)

        index_aux = np.arange(0, clusters.shape[0])
        cluster_agency = {}

        # Add to the points corresponding to each cluster the points that correspond to it with top 2 probability,
        # if this probability is high
        for cluster in range(n_clusters):
            top_1 = np.argmax(clusters, 1)
            clusters_ = copy.deepcopy(clusters)
            clusters_[index_aux, top_1] *= 0
            top_2 = np.argmax(clusters_, 1)
            idx = np.where(top_1 == cluster)[0]
            idx = np.append(idx, np.where((top_2 == cluster) * (clusters_[index_aux, top_2] > 0.2))[0])
            cluster_agency[cluster] = idx

        # Only works in len(x_features) >= 3
        if visualize_data:
            x_aux = self.get_x(pruned=False)
            y_aux = self.get_y(pruned=False)
            visualize_data_distribution(x_aux, y_aux, cluster_agency, x_pruned, y_pruned)

        self.cluster_agency = cluster_agency
        self.centroids = centroids
//  [python]
def _gaussians_fit(self, idx_exclude=-1, save=False):
        """ Fit GMM to video features.

        Define subset of video collection and fit on it gaussian mixture model.
        If idx_exclude = -1, then whole video collection is used for comprising
        the subset, otherwise video collection excluded video with this index.
        Args:
            idx_exclude: video to exclude (-1 or int in range(0, #_of_videos))
            save: in case of mp lib all computed likelihoods are saved on disk
                before the next step
        """
        # logger.debug('Excluded: %d' % idx_exclude)
        for k in range(self._K):
            gmm = mixture.GaussianMixture(n_components=self._Q,
                                  covariance_type='full',
                                  max_iter=150,
                                  random_state=opt.seed,
                                  reg_covar=opt.reg_cov)
            total_indexes = np.zeros(len(self._features), dtype=np.bool)
            for idx, video in enumerate(self._videos):
                if idx == idx_exclude:
                    continue
                if opt.gt_training:
                    indexes = np.where(np.asarray(video._gt) == self._label2gt[k])[0]
                else:
                    indexes = np.where(np.asarray(video._z) == k)[0]
                if len(indexes) == 0:
                    continue
                temp = np.zeros(video.n_frames, dtype=np.bool)
                temp[indexes] = True
                total_indexes[video.global_range] = temp

            total_indexes = np.array(total_indexes, dtype=np.bool)
            if opt.save_embed_feat:
                feature = self._features[total_indexes, :]
            else:
                feature = self._embedded_feat[total_indexes, :]
            time1 = time.time()
            try:
                gmm.fit(feature)
            except ValueError:
                gmm = AuxiliaryGMM()
            time2 = time.time()
            if idx_exclude % 20 == 0:
                logger.debug('fit gmm %0.6f %d ' % ((time2 - time1), len(feature))
                             + str(gmm.converged_))

            self._gaussians[k] = gmm

        if save:
            self._video_likelihood_grid(idx_exclude)
            self._videos[idx_exclude].save_likelihood()

        if opt.bg:
            # with bg model I assume that I use only one component
            assert self._Q == 1
            for gm_idx, gmm in self._gaussians.items():
                self._gaussians[gm_idx] = GMM_trh(gmm)
//  [python]
def _gaussians_fit(self, idx_exclude=-1, save=False):
        """ Fit GMM to video features.

        Define subset of video collection and fit on it gaussian mixture model.
        If idx_exclude = -1, then whole video collection is used for comprising
        the subset, otherwise video collection excluded video with this index.
        Args:
            idx_exclude: video to exclude (-1 or int in range(0, #_of_videos))
            save: in case of mp lib all computed likelihoods are saved on disk
                before the next step
        """
        # logger.debug('Excluded: %d' % idx_exclude)
        for k in range(self._K):
            gmm = GaussianMixture(n_components=self._Q,
                                  covariance_type='full',
                                  max_iter=150,
                                  random_state=opt.seed,
                                  reg_covar=opt.reg_cov)
            total_indexes = np.zeros(len(self._features), dtype=np.bool)
            for idx, video in enumerate(self._videos):
                if idx == idx_exclude:
                    continue
                if opt.gt_training:
                    indexes = np.where(np.asarray(video._gt) == self._label2gt[k])[0]
                else:
                    indexes = np.where(np.asarray(video._z) == k)[0]
                if len(indexes) == 0:
                    continue
                temp = np.zeros(video.n_frames, dtype=np.bool)
                temp[indexes] = True
                total_indexes[video.global_range] = temp

            total_indexes = np.array(total_indexes, dtype=np.bool)
            if opt.save_embed_feat:
                feature = self._features[total_indexes, :]
            else:
                feature = self._embedded_feat[total_indexes, :]
            time1 = time.time()
            try:
                gmm.fit(feature)
            except ValueError:
                gmm = AuxiliaryGMM()
            time2 = time.time()
            if idx_exclude % 20 == 0:
                logger.debug('fit gmm %0.6f %d ' % ((time2 - time1), len(feature))
                             + str(gmm.converged_))

            self._gaussians[k] = gmm

        if save:
            self._video_likelihood_grid(idx_exclude)
            self._videos[idx_exclude].save_likelihood()

        if opt.bg:
            # with bg model I assume that I use only one component
            assert self._Q == 1
            for gm_idx, gmm in self._gaussians.items():
                self._gaussians[gm_idx] = GMM_trh(gmm)
//  [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
    msg = re.escape(
        "The parameter 'weights' should have the shape of "
        f"({n_components},), but got {str(weights_bad_shape.shape)}"
    )
    with pytest.raises(ValueError, match=msg):
        g.fit(X)

    # Check bad range
    weights_bad_range = rng.rand(n_components) + 1
    g.weights_init = weights_bad_range
    msg = re.escape(
        "The parameter 'weights' should be in the range [0, 1], but got"
        f" max value {np.min(weights_bad_range):.5f}, "
        f"min value {np.max(weights_bad_range):.5f}"
    )
    with pytest.raises(ValueError, match=msg):
        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
    msg = re.escape(
        "The parameter 'weights' should be normalized, "
        f"but got sum(weights) = {np.sum(weights_bad_norm):.5f}"
    )
    with pytest.raises(ValueError, match=msg):
        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
    msg = re.escape(
        "The parameter 'weights' should have the shape of "
        f"({n_components},), but got {str(weights_bad_shape.shape)}"
    )
    with pytest.raises(ValueError, match=msg):
        g.fit(X)

    # Check bad range
    weights_bad_range = rng.rand(n_components) + 1
    g.weights_init = weights_bad_range
    msg = re.escape(
        "The parameter 'weights' should be in the range [0, 1], but got"
        f" max value {np.min(weights_bad_range):.5f}, "
        f"min value {np.max(weights_bad_range):.5f}"
    )
    with pytest.raises(ValueError, match=msg):
        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
    msg = re.escape(
        "The parameter 'weights' should be normalized, "
        f"but got sum(weights) = {np.sum(weights_bad_norm):.5f}"
    )
    with pytest.raises(ValueError, match=msg):
        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_gaussian_mixture_fit_predict_n_init():
    # Check that fit_predict is equivalent to fit.predict, when n_init > 1
    X = np.random.RandomState(0).randn(1000, 5)
    gm = mixture.GaussianMixture(n_components=5, n_init=5, random_state=0)
    y_pred1 = gm.fit_predict(X)
    y_pred2 = gm.predict(X)
    assert_array_equal(y_pred1, y_pred2)
//  [python]
def test_gaussian_mixture_fit_predict_n_init():
    # Check that fit_predict is equivalent to fit.predict, when n_init > 1
    X = np.random.RandomState(0).randn(1000, 5)
    gm = GaussianMixture(n_components=5, n_init=5, random_state=0)
    y_pred1 = gm.fit_predict(X)
    y_pred2 = gm.predict(X)
    assert_array_equal(y_pred1, y_pred2)
//  [python]
def test_gaussian_mixture_estimate_log_prob_resp():
    # test whether responsibilities are normalized
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=5)
    n_samples = rand_data.n_samples
    n_features = rand_data.n_features
    n_components = rand_data.n_components

    X = rng.rand(n_samples, n_features)
    for covar_type in COVARIANCE_TYPE:
        weights = rand_data.weights
        means = rand_data.means
        precisions = rand_data.precisions[covar_type]
        g = GaussianMixture(
            n_components=n_components,
            random_state=rng,
            weights_init=weights,
            means_init=means,
            precisions_init=precisions,
            covariance_type=covar_type,
        )
        g.fit(X)
        resp = g.predict_proba(X)
        assert_array_almost_equal(resp.sum(axis=1), np.ones(n_samples))
        assert_array_equal(g.weights_init, weights)
        assert_array_equal(g.means_init, means)
        assert_array_equal(g.precisions_init, precisions)
//  [python]
def test_gaussian_mixture_predict_predict_proba():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = mixture.GaussianMixture(
            n_components=rand_data.n_components,
            random_state=rng,
            weights_init=rand_data.weights,
            means_init=rand_data.means,
            precisions_init=rand_data.precisions[covar_type],
            covariance_type=covar_type,
        )

        # Check a warning message arrive if we don't do fit
        msg = (
            "This GaussianMixture instance is not fitted yet. Call 'fit' "
            "with appropriate arguments before using this estimator."
        )
        with pytest.raises(NotFittedError, match=msg):
            g.predict(X)

        g.fit(X)
        Y_pred = g.predict(X)
        Y_pred_proba = g.predict_proba(X).argmax(axis=1)
        assert_array_equal(Y_pred, Y_pred_proba)
        assert adjusted_rand_score(Y, Y_pred) > 0.95
//  [python]
def test_gaussian_mixture_predict_predict_proba():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = GaussianMixture(
            n_components=rand_data.n_components,
            random_state=rng,
            weights_init=rand_data.weights,
            means_init=rand_data.means,
            precisions_init=rand_data.precisions[covar_type],
            covariance_type=covar_type,
        )

        # Check a warning message arrive if we don't do fit
        msg = (
            "This GaussianMixture instance is not fitted yet. Call 'fit' "
            "with appropriate arguments before using this estimator."
        )
        with pytest.raises(NotFittedError, match=msg):
            g.predict(X)

        g.fit(X)
        Y_pred = g.predict(X)
        Y_pred_proba = g.predict_proba(X).argmax(axis=1)
        assert_array_equal(Y_pred, Y_pred_proba)
        assert adjusted_rand_score(Y, Y_pred) > 0.95
//  [python]
def test_gaussian_mixture_fit_predict(seed, max_iter, tol):
    rng = np.random.RandomState(seed)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = mixture.GaussianMixture(
            n_components=rand_data.n_components,
            random_state=rng,
            weights_init=rand_data.weights,
            means_init=rand_data.means,
            precisions_init=rand_data.precisions[covar_type],
            covariance_type=covar_type,
            max_iter=max_iter,
            tol=tol,
        )

        # check if fit_predict(X) is equivalent to fit(X).predict(X)
        f = copy.deepcopy(g)
        Y_pred1 = f.fit(X).predict(X)
        Y_pred2 = g.fit_predict(X)
        assert_array_equal(Y_pred1, Y_pred2)
        assert adjusted_rand_score(Y, Y_pred2) > 0.95
//  [python]
def test_gaussian_mixture_fit_predict(seed, max_iter, tol):
    rng = np.random.RandomState(seed)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = GaussianMixture(
            n_components=rand_data.n_components,
            random_state=rng,
            weights_init=rand_data.weights,
            means_init=rand_data.means,
            precisions_init=rand_data.precisions[covar_type],
            covariance_type=covar_type,
            max_iter=max_iter,
            tol=tol,
        )

        # check if fit_predict(X) is equivalent to fit(X).predict(X)
        f = copy.deepcopy(g)
        Y_pred1 = f.fit(X).predict(X)
        Y_pred2 = g.fit_predict(X)
        assert_array_equal(Y_pred1, Y_pred2)
        assert adjusted_rand_score(Y, Y_pred2) > 0.95
//  [python]
def test_gaussian_mixture_fit_best_params():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    n_components = rand_data.n_components
    n_init = 10
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        g = s.GaussianMixture(
            n_components=n_components,
            n_init=1,
            reg_covar=0,
            random_state=rng,
            covariance_type=covar_type,
        )
        ll = []
        for _ in range(n_init):
            g.fit(X)
            ll.append(g.score(X))
        ll = np.array(ll)
        g_best = s.GaussianMixture(
            n_components=n_components,
            n_init=n_init,
            reg_covar=0,
            random_state=rng,
            covariance_type=covar_type,
        )
        g_best.fit(X)
        assert_almost_equal(ll.min(), g_best.score(X))
//  [python]
def test_gaussian_mixture_fit_best_params():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    n_components = rand_data.n_components
    n_init = 10
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        g = GaussianMixture(
            n_components=n_components,
            n_init=1,
            reg_covar=0,
            random_state=rng,
            covariance_type=covar_type,
        )
        ll = []
        for _ in range(n_init):
            g.fit(X)
            ll.append(g.score(X))
        ll = np.array(ll)
        g_best = GaussianMixture(
            n_components=n_components,
            n_init=n_init,
            reg_covar=0,
            random_state=rng,
            covariance_type=covar_type,
        )
        g_best.fit(X)
        assert_almost_equal(ll.min(), g_best.score(X))
//  [python]
def test_gaussian_mixture_fit_convergence_warning():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=1)
    n_components = rand_data.n_components
    max_iter = 1
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        g = mixture.GaussianMixture(
            n_components=n_components,
            n_init=1,
            max_iter=max_iter,
            reg_covar=0,
            random_state=rng,
            covariance_type=covar_type,
        )
        msg = (
            "Best performing initialization did not converge. "
            "Try different init parameters, or increase max_iter, "
            "tol, or check for degenerate data."
        )
        with pytest.warns(ConvergenceWarning, match=msg):
            g.fit(X)
//  [python]
def test_gaussian_mixture_fit_convergence_warning():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng, scale=1)
    n_components = rand_data.n_components
    max_iter = 1
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        g = GaussianMixture(
            n_components=n_components,
            n_init=1,
            max_iter=max_iter,
            reg_covar=0,
            random_state=rng,
            covariance_type=covar_type,
        )
        msg = (
            "Best performing initialization did not converge. "
            "Try different init parameters, or increase max_iter, "
            "tol, or check for degenerate data."
        )
        with pytest.warns(ConvergenceWarning, match=msg):
            g.fit(X)