jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def process_gmm(): 
	K = int(sys.argv[1])

	# load all cols except screen_names (0)
	#X = np.loadtxt(sys.argv[2], delimiter=',', skiprows=1,
	X = np.loadtxt(sys.argv[2], delimiter=',', skiprows=0,
			#usecols=range(1,23)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name
			usecols=range(1,16)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name
	#X = np.genfromtxt(sys.argv[2], delimiter=',', skip_header=1,
	#		#usecols=range(1,23)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name
	#		usecols=range(1,16)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name

	## Tranpose (to normalise per col), Normalise, Tranpose (back to correct matrix arrangement)
	#X_tran = X.transpose()
	#X_norm = preprocessing.normalize(X_tran, norm='l1') # L1 for least absolute deviations
	#X = X_norm.transpose()

	#print("K: {}, data shape: [{}][{}]".format(K, len(X), len(X[0])))

	# Fit a Gaussian mixture with EM using K components
	gmm = mixture.GaussianMixture(n_components=K, covariance_type='full',
			tol=1e-4, max_iter=500, n_init=3, init_params='kmeans',
			warm_start=True, verbose=1).fit(X)

	## generate random samples from the fitted Gaussian distribution
	#sample = gmm.sample(1000)

	# load screen_names
	with open(sys.argv[2]) as csvfile:
		read_csv = csv.reader(csvfile, delimiter=',')
		screen_names = []
		for row in read_csv:
			screen_names.append(row[0])

	# label clusters
	labels = gmm.predict(X)
	clusters = {}
	i = 0
	for label in labels:
		if label in clusters:
			clusters[label].append(screen_names[i])
		else:
			clusters[label] = [screen_names[i]]
		i += 1

	# outputs
	for cluster in clusters:
		f = open(sys.argv[3]+"/gmm."+sys.argv[2].split("/")[-1:].pop()+".K"+sys.argv[1]+".cluster"+str(cluster)+".out", "w+")
		for c in clusters[cluster]:
			f.write("{}\n".format(c))
		f.close()
//  [python]
def eval_train(eval_loader, model, all_losses, cfg):
    model.eval()
    losses = []
    criterion = nn.CrossEntropyLoss(reduction='none').cuda()
    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs)
            loss = criterion(outputs, targets)
            losses.append(loss)
    losses = torch.cat(losses)
    losses = (losses - losses.min()) / (losses.max() - losses.min())
    losses = losses.cpu()
    all_losses.append(losses)

    if cfg.noise_ratio >= 0.9:  # average loss over last 5 epochs to improve convergence stability
        history = torch.stack(all_losses)
        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 = mixture.GaussianMixture(n_components=2, max_iter=10, tol=cfg.get('gmm_tol', 1e-2), reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss)
    prob = prob[:, gmm.means_.argmin()]
    return prob
//  [python]
def eval_train(eval_loader, model, all_losses, cfg):
    model.eval()
    losses = []
    criterion = nn.CrossEntropyLoss(reduction='none').cuda()
    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs)
            loss = criterion(outputs, targets)
            losses.append(loss)
    losses = torch.cat(losses)
    losses = (losses - losses.min()) / (losses.max() - losses.min())
    losses = losses.cpu()
    all_losses.append(losses)

    if cfg.noise_ratio >= 0.9:  # average loss over last 5 epochs to improve convergence stability
        history = torch.stack(all_losses)
        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 = GaussianMixture(n_components=2, max_iter=10, tol=cfg.get('gmm_tol', 1e-2), reg_covar=5e-4)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss)
    prob = prob[:, gmm.means_.argmin()]
    return prob
//  [python]
def estim_class_model_gmm(features, nb_classes, init='kmeans'):
    """ from all features estimate Gaussian Mixture Model and assuming
    each cluster is a single class compute probability that each feature
    belongs to each class

    :param [[float]] features: list of features per segment
    :param int nb_classes: number of classes
    :param int init: initialisation
    :return [[float]]: probabilities that each feature belongs to each class

    >>> np.random.seed(0)
    >>> fts = np.row_stack([np.random.random((50, 3)) - 1,
    ...                     np.random.random((50, 3)) + 1])
    >>> mm = estim_class_model_gmm(fts, 2)
    >>> mm.predict_proba(fts).shape
    (100, 2)
    """
    logging.debug('estimate GMM for all given features %r and %i component', features.shape, nb_classes)
    # http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html
    gmm = mixture.GMM(n_components=nb_classes, covariance_type='full', n_iter=99)
    if init == 'kmeans':
        # http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
        kmeans = cluster.KMeans(n_clusters=nb_classes, init='k-means++', n_jobs=-1)
        y = kmeans.fit_predict(features)
        gmm.fit(features, y)
    else:
        gmm.fit(features)
    logging.info('compute probability of each feature to all component')
    return gmm
//  [python]
def estim_class_model_gmm(features, nb_classes, init='kmeans'):
    """ from all features estimate Gaussian Mixture Model and assuming
    each cluster is a single class compute probability that each feature
    belongs to each class

    :param [[float]] features: list of features per segment
    :param int nb_classes: number of classes
    :param int init: initialisation
    :return [[float]]: probabilities that each feature belongs to each class

    >>> np.random.seed(0)
    >>> fts = np.row_stack([np.random.random((50, 3)) - 1,
    ...                     np.random.random((50, 3)) + 1])
    >>> mm = estim_class_model_gmm(fts, 2)
    >>> mm.predict_proba(fts).shape
    (100, 2)
    """
    logging.debug('estimate GMM for all given features %r and %i component', features.shape, nb_classes)
    # http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html
    gmm = mixture.GaussianMixture(n_components=nb_classes, covariance_type='full', max_iter=99)
    if init == 'kmeans':
        # http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
        kmeans = cluster.KMeans(n_clusters=nb_classes, init='k-means++', n_jobs=-1)
        y = kmeans.fit_predict(features)
        gmm.fit(features, y)
    else:
        gmm.fit(features)
    logging.info('compute probability of each feature to all component')
    return gmm
//  [python]
def gmm_fit(
    net,
    n_components,
    algo="bayes",
    n_init=50,
    random_state=None,
    mean_precision_prior=None,
    weight_concentration_prior_type="dirichlet_process",
    weight_concentration_prior=None,
):
    """
    fit a mixture of gaussians to a set of vectors
    :param net
    :param n_components: int
    :param algo: 'em' or 'bayes'
    :param n_init: number of random seeds for the inference algorithm
    :param random_state: random seed for the rng to eliminate randomness
    :return: vector of population labels (of shape n), best fitted model
    """
    neurons_fs = make_vecs(net)

    if isinstance(neurons_fs, list):
        X = np.vstack(neurons_fs).transpose()
    else:
        X = neurons_fs
    if algo == "em":
        model = mixture.GaussianMixture(n_components=n_components, n_init=n_init, random_state=random_state)
    else:
        model = mixture.BayesianGaussianMixture(
            n_components=n_components,
            n_init=n_init,
            random_state=random_state,
            init_params="random",
            mean_precision_prior=mean_precision_prior,
            weight_concentration_prior_type=weight_concentration_prior_type,
            weight_concentration_prior=weight_concentration_prior,
        )
    model.fit(X)
    z = model.predict(X)
    return z, model
//  [python]
def gmm_fit(
    net,
    n_components,
    algo="bayes",
    n_init=50,
    random_state=None,
    mean_precision_prior=None,
    weight_concentration_prior_type="dirichlet_process",
    weight_concentration_prior=None,
):
    """
    fit a mixture of gaussians to a set of vectors
    :param net
    :param n_components: int
    :param algo: 'em' or 'bayes'
    :param n_init: number of random seeds for the inference algorithm
    :param random_state: random seed for the rng to eliminate randomness
    :return: vector of population labels (of shape n), best fitted model
    """
    neurons_fs = make_vecs(net)

    if isinstance(neurons_fs, list):
        X = np.vstack(neurons_fs).transpose()
    else:
        X = neurons_fs
    if algo == "em":
        model = GaussianMixture(n_components=n_components, n_init=n_init, random_state=random_state)
    else:
        model = BayesianGaussianMixture(
            n_components=n_components,
            n_init=n_init,
            random_state=random_state,
            init_params="random",
            mean_precision_prior=mean_precision_prior,
            weight_concentration_prior_type=weight_concentration_prior_type,
            weight_concentration_prior=weight_concentration_prior,
        )
    model.fit(X)
    z = model.predict(X)
    return z, model
//  [python]
def __init__(self, center_position, 
                 surface_points,
                 points_per_component=15, radial_coordinates=False,
                 fit_init=True):

        self.dim = center_position.shape[0]
        
        self.center_position = center_position
        self.reference_point = np.zeros(self.dim)

        
        self.n_gaussians = int(np.floor(surface_points/points_per_component))

        gmm_model = mixture.GMM(n_components=self.n_gaussians,
                                                    covariance_type="full")
        
        gmm_model.fit(surface_points)
        
        self.obs_list = []
        
        for ii in range(self.n_gaussians):
            self.obs_list[ii].set

        self.n_obstacles = len(self.obs_list)
//  [python]
def __init__(self, center_position, 
                 surface_points,
                 points_per_component=15, radial_coordinates=False,
                 fit_init=True):

        self.dim = center_position.shape[0]
        
        self.center_position = center_position
        self.reference_point = np.zeros(self.dim)

        
        self.n_gaussians = int(np.floor(surface_points/points_per_component))

        gmm_model = sklearn.mixture.GaussianMixture(n_components=self.n_gaussians,
                                                    covariance_type="full")
        
        gmm_model.fit(surface_points)
        
        self.obs_list = []
        
        for ii in range(self.n_gaussians):
            self.obs_list[ii].set

        self.n_obstacles = len(self.obs_list)
//  [python]
def init_gmm(means, weights, covariances):
    """
        Initialise the GMM.

    Args:
        means: (list): Mean value for each mixture component.
        weights: (list): Mixing weights for each mixture component.
        covariances: (list): Covariance for mixture component. In the used scikit-learn model
            a covariance_type of "full" is used.

    Returns:
        gmm: (:obj:): Scikit-learn gaussian mixture model.
    """

    assert 0.99 < sum(weights) < 1.01, 'GMM weights must add up to 1 (1 % tolerance)'
    assert len(weights) == len(means) == len(covariances), 'Parameters for GMM must be of same ' \
                                                           'length'
    assert all(0 < x[0] < 168 for x in means), 'The first dimension of the means is expected to ' \
                                               'be the arrival time in hours counting from ' \
                                               'Monday 0:00. Values should therefore be >0 and <168'

    # 2 dimensions: arrival time, parking time -> 2x2 covariance matrices
    for i in covariances:
        for j in i:
            assert len(i) == len(j) == 2, 'GMM covariances must be quadratic with 2 rows and 2 cols'

    # numpy arrays of parameters
    means_np = np.asarray(means)
    weights_np = np.asarray(weights)
    covariances_np = np.asarray(covariances)

    # scikit-learn GMM
    gmm = mixture.GaussianMixture()

    # initialise parameters
    gmm.means_ = means_np
    gmm.weights_ = weights_np
    gmm.covariances_ = covariances_np

    return gmm
//  [python]
def init_gmm(means, weights, covariances):
    """
        Initialise the GMM.

    Args:
        means: (list): Mean value for each mixture component.
        weights: (list): Mixing weights for each mixture component.
        covariances: (list): Covariance for mixture component. In the used scikit-learn model
            a covariance_type of "full" is used.

    Returns:
        gmm: (:obj:): Scikit-learn gaussian mixture model.
    """

    assert 0.99 < sum(weights) < 1.01, 'GMM weights must add up to 1 (1 % tolerance)'
    assert len(weights) == len(means) == len(covariances), 'Parameters for GMM must be of same ' \
                                                           'length'
    assert all(0 < x[0] < 168 for x in means), 'The first dimension of the means is expected to ' \
                                               'be the arrival time in hours counting from ' \
                                               'Monday 0:00. Values should therefore be >0 and <168'

    # 2 dimensions: arrival time, parking time -> 2x2 covariance matrices
    for i in covariances:
        for j in i:
            assert len(i) == len(j) == 2, 'GMM covariances must be quadratic with 2 rows and 2 cols'

    # numpy arrays of parameters
    means_np = np.asarray(means)
    weights_np = np.asarray(weights)
    covariances_np = np.asarray(covariances)

    # scikit-learn GMM
    gmm = GaussianMixture()

    # initialise parameters
    gmm.means_ = means_np
    gmm.weights_ = weights_np
    gmm.covariances_ = covariances_np

    return gmm
//  [python]
def _gmm_train(self):
        """
        trains several GM models based on the given data (train_data) 
        and returns the best one (evaluated by AIC) (best_gmm)
        Also returns a dataframe with a summary of the different 
        GMM components (gmm_components)
        """

        train_data = self._data_for_gmm_training()
        y_values = np.array([[val] for val in train_data])

        N = np.arange(1, (self.gmm_max_comp + 1))
        models = [None for i in range(len(N))]
        for i in range(len(N)):
            models[i] = mixture.GaussianMixture(N[i]).fit(y_values)
        
        AIC = [m.aic(y_values) for m in models]
        #BIC = [m.bic(y_values) for m in models]
        best_gmm = models[np.argmin(AIC)]

        gmm_components = pd.DataFrame(columns=['component', 
                                               'gauss_mean',
                                               'gauss_covariance'])
        gmm_components['component'] = list(range(0, best_gmm.n_components))
        gmm_components['gauss_mean'] = best_gmm.means_
        gmm_components['gauss_covariance'] = best_gmm.covariances_.reshape(-1,1)

        if self.verbose:
            print(f'calculated GMM') 
            print(f'components:\n {gmm_components}')

        return best_gmm, gmm_components
//  [python]
def _gmm_train(self):
        """
        trains several GM models based on the given data (train_data) 
        and returns the best one (evaluated by AIC) (best_gmm)
        Also returns a dataframe with a summary of the different 
        GMM components (gmm_components)
        """

        train_data = self._data_for_gmm_training()
        y_values = np.array([[val] for val in train_data])

        N = np.arange(1, (self.gmm_max_comp + 1))
        models = [None for i in range(len(N))]
        for i in range(len(N)):
            models[i] = GaussianMixture(N[i]).fit(y_values)
        
        AIC = [m.aic(y_values) for m in models]
        #BIC = [m.bic(y_values) for m in models]
        best_gmm = models[np.argmin(AIC)]

        gmm_components = pd.DataFrame(columns=['component', 
                                               'gauss_mean',
                                               'gauss_covariance'])
        gmm_components['component'] = list(range(0, best_gmm.n_components))
        gmm_components['gauss_mean'] = best_gmm.means_
        gmm_components['gauss_covariance'] = best_gmm.covariances_.reshape(-1,1)

        if self.verbose:
            print(f'calculated GMM') 
            print(f'components:\n {gmm_components}')

        return best_gmm, gmm_components
//  [python]
def make_gmm(n_components=None, max_iter=150, random_state=None):
    """fn: create gmm object"""
    model_kwds = dict(n_components=n_components, 
                      max_iter=max_iter,
                      n_init=100,
                      init_params='random',
                      random_state=random_state)

    gmm = s.GMM(**model_kwds)
    return gmm
//  [python]
def make_gmm(n_components=None, max_iter=150, random_state=None):
    """fn: create gmm object"""
    model_kwds = dict(n_components=n_components, 
                      max_iter=max_iter,
                      n_init=100,
                      init_params='random',
                      random_state=random_state)

    gmm = mix.GaussianMixture(**model_kwds)
    return gmm
//  [python]
def _calculate_grid(mask, nrows, ncols):
    contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
    centers = []
    for c in contours:
        m = cv2.moments(c)
        cmx, cmy = (float(m['m10'] / m['m00']), float(m['m01'] / m['m00']))
        centers.append((cmx, cmy))
    # cluster by x and y coordinates to get grid layout
    centers_x = np.array(np.array([i[0] for i in centers]).reshape(-1, 1))
    centers_y = np.array(np.array([i[1] for i in centers]).reshape(-1, 1))
    gm_x = mixture.GMM(n_components=ncols, random_state=0).fit(centers_x)
    gm_y = mixture.GMM(n_components=nrows, random_state=0).fit(centers_y)
    clusters_x = np.sort(gm_x.means_[:, 0])
    clusters_y = np.sort(gm_y.means_[:, 0])
    spacing_x = (clusters_x[ncols-1] - clusters_x[0])/(ncols-1) if ncols > 1 else 0
    spacing_y = (clusters_y[nrows-1] - clusters_y[0])/(nrows-1) if nrows > 1 else 0
    spacing = (round(spacing_x), round(spacing_y))
    coord = (round(clusters_x[0]), round(clusters_y[0]))
    return coord, spacing
//  [python]
def _calculate_grid(mask, nrows, ncols):
    contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
    centers = []
    for c in contours:
        m = cv2.moments(c)
        cmx, cmy = (float(m['m10'] / m['m00']), float(m['m01'] / m['m00']))
        centers.append((cmx, cmy))
    # cluster by x and y coordinates to get grid layout
    centers_x = np.array(np.array([i[0] for i in centers]).reshape(-1, 1))
    centers_y = np.array(np.array([i[1] for i in centers]).reshape(-1, 1))
    gm_x = GaussianMixture(n_components=ncols, random_state=0).fit(centers_x)
    gm_y = GaussianMixture(n_components=nrows, random_state=0).fit(centers_y)
    clusters_x = np.sort(gm_x.means_[:, 0])
    clusters_y = np.sort(gm_y.means_[:, 0])
    spacing_x = (clusters_x[ncols-1] - clusters_x[0])/(ncols-1) if ncols > 1 else 0
    spacing_y = (clusters_y[nrows-1] - clusters_y[0])/(nrows-1) if nrows > 1 else 0
    spacing = (round(spacing_x), round(spacing_y))
    coord = (round(clusters_x[0]), round(clusters_y[0]))
    return coord, spacing
//  [python]
def reinit_from_data(self, data):
        """
        Reinitializing Gaussians' parameters to better
        cover the latent space
        Also resets TT cores
        Args:
            data: tensor of shape [num_objects, num_vars], data to
                reinitialize the Gaussians
        """
        new_tt_cores = []
        new_means = []
        new_log_stds = []

        for i, var_descr in enumerate(self.distr_descr):
            cur_core = torch.randn(var_descr[1], self.tt_int, self.tt_int)
            cur_core = cur_core / (self.tt_int ** 2 * var_descr[1])
            new_tt_cores.append(nn.Parameter(cur_core))

            if torch.sum(torch.isnan(data[:, i])) == data.shape[0]:
                new_means.append(self.means[i])
                new_log_stds.append(self.log_stds[i])

                continue

            if var_descr[0] == 'd':
                new_means.append(None)
                new_log_stds.append(None)
                cur_components = data[:, i].cpu().detach().numpy()
                cur_components[np.isnan(cur_components)] = -1

            elif var_descr[0] == 'c':
                gmm = mixture.GaussianMixture(n_components=var_descr[1])
                cur_data = data[:, i].cpu().detach().numpy()
                non_missings = np.logical_not(np.isnan(cur_data))

                cur_components = -1 * np.ones_like(cur_data)

                non_missed_data = cur_data[non_missings].reshape(-1, 1)
                gmm.fit(non_missed_data)
                cur_gmm_comp = gmm.predict(non_missed_data)
                cur_components[non_missings] = cur_gmm_comp

                cur_means = torch.from_numpy(gmm.means_[:, 0])
                cur_means = cur_means.float()
                cur_log_stds = torch.from_numpy(gmm.covariances_[:, 0, 0])
                cur_log_stds = torch.log(cur_log_stds.float() + self.eps) / 2

                new_means.append(nn.Parameter(cur_means))
                new_log_stds.append(nn.Parameter(cur_log_stds))

        for i in range(len(self.tt_cores)):
            self.tt_cores[i].data = new_tt_cores[i].data.to(self.tt_cores[i].data.device)
            if new_means[i] is not None:
                self.means[i].data = new_means[i].data.to(self.means[i].data.device)
                self.log_stds[i].data = new_log_stds[i].data.to(self.log_stds[i].data.device)
//  [python]
def reinit_from_data(self, data):
        """
        Reinitializing Gaussians' parameters to better
        cover the latent space
        Also resets TT cores
        Args:
            data: tensor of shape [num_objects, num_vars], data to
                reinitialize the Gaussians
        """
        new_tt_cores = []
        new_means = []
        new_log_stds = []

        for i, var_descr in enumerate(self.distr_descr):
            cur_core = torch.randn(var_descr[1], self.tt_int, self.tt_int)
            cur_core = cur_core / (self.tt_int ** 2 * var_descr[1])
            new_tt_cores.append(nn.Parameter(cur_core))

            if torch.sum(torch.isnan(data[:, i])) == data.shape[0]:
                new_means.append(self.means[i])
                new_log_stds.append(self.log_stds[i])

                continue

            if var_descr[0] == 'd':
                new_means.append(None)
                new_log_stds.append(None)
                cur_components = data[:, i].cpu().detach().numpy()
                cur_components[np.isnan(cur_components)] = -1

            elif var_descr[0] == 'c':
                gmm = GaussianMixture(n_components=var_descr[1])
                cur_data = data[:, i].cpu().detach().numpy()
                non_missings = np.logical_not(np.isnan(cur_data))

                cur_components = -1 * np.ones_like(cur_data)

                non_missed_data = cur_data[non_missings].reshape(-1, 1)
                gmm.fit(non_missed_data)
                cur_gmm_comp = gmm.predict(non_missed_data)
                cur_components[non_missings] = cur_gmm_comp

                cur_means = torch.from_numpy(gmm.means_[:, 0])
                cur_means = cur_means.float()
                cur_log_stds = torch.from_numpy(gmm.covariances_[:, 0, 0])
                cur_log_stds = torch.log(cur_log_stds.float() + self.eps) / 2

                new_means.append(nn.Parameter(cur_means))
                new_log_stds.append(nn.Parameter(cur_log_stds))

        for i in range(len(self.tt_cores)):
            self.tt_cores[i].data = new_tt_cores[i].data.to(self.tt_cores[i].data.device)
            if new_means[i] is not None:
                self.means[i].data = new_means[i].data.to(self.means[i].data.device)
                self.log_stds[i].data = new_log_stds[i].data.to(self.log_stds[i].data.device)
//  [python]
def _fit(self, X, model_dump_path=None, verbose=True):
    """
    :param X: shape (n_videos, n_frames, n_descriptors_per_image, n_dim_descriptor)
    :param model_dump_path: (optional) path where the fitted model shall be dumped
    :param verbose - boolean that controls the verbosity
    :return: fitted Fisher vector object
    """
    assert X.ndim == 4
    self.feature_dim = X.shape[-1]

    X = X.reshape(-1, X.shape[-1])

    # fit GMM and store params of fitted model
    self.gmm = gmm = mixture.GaussianMixture(n_components=self.n_kernels, covariance_type=self.covariance_type, max_iter=1000).fit(X)
    self.covars = gmm.covariances_
    self.means = gmm.means_
    self.weights = gmm.weights_

    # if cov_type is diagonal - make sure that covars holds a diagonal matrix
    if self.covariance_type == 'diag':
      cov_matrices = np.empty(shape=(self.n_kernels, self.covars.shape[1], self.covars.shape[1]))
      for i in range(self.n_kernels):
        cov_matrices[i, :, :] = np.diag(self.covars[i, :])
      self.covars = cov_matrices

    assert self.covars.ndim == 3
    self.fitted = True
    if verbose:
      print('fitted GMM with %i kernels'%self.n_kernels)

    if model_dump_path:
      with open(model_dump_path, 'wb') as f:
        pickle.dump(self,f, protocol=4)
      if verbose:
        print('Dumped fitted model to', model_dump_path)

    return self
//  [python]
def _fit(self, X, model_dump_path=None, verbose=True):
    """
    :param X: shape (n_videos, n_frames, n_descriptors_per_image, n_dim_descriptor)
    :param model_dump_path: (optional) path where the fitted model shall be dumped
    :param verbose - boolean that controls the verbosity
    :return: fitted Fisher vector object
    """
    assert X.ndim == 4
    self.feature_dim = X.shape[-1]

    X = X.reshape(-1, X.shape[-1])

    # fit GMM and store params of fitted model
    self.gmm = gmm = GaussianMixture(n_components=self.n_kernels, covariance_type=self.covariance_type, max_iter=1000).fit(X)
    self.covars = gmm.covariances_
    self.means = gmm.means_
    self.weights = gmm.weights_

    # if cov_type is diagonal - make sure that covars holds a diagonal matrix
    if self.covariance_type == 'diag':
      cov_matrices = np.empty(shape=(self.n_kernels, self.covars.shape[1], self.covars.shape[1]))
      for i in range(self.n_kernels):
        cov_matrices[i, :, :] = np.diag(self.covars[i, :])
      self.covars = cov_matrices

    assert self.covars.ndim == 3
    self.fitted = True
    if verbose:
      print('fitted GMM with %i kernels'%self.n_kernels)

    if model_dump_path:
      with open(model_dump_path, 'wb') as f:
        pickle.dump(self,f, protocol=4)
      if verbose:
        print('Dumped fitted model to', model_dump_path)

    return self
//  [python]
def GaussianMixture(V, **kwargs):
    """Performs clustering on *V* by using Gaussian mixture models. The function uses :func:`sklearn.micture.GaussianMixture`. See sklearn documents 
    for details.

    :arg V: row-normalized eigenvectors for the purpose of clustering.
    :type V: :class:`~numpy.ndarray`

    :arg n_clusters: specifies the number of clusters. 
    :type n_clusters: int
    """

    try:
        from sklearn.mixture import GaussianMixture
    except ImportError:
        raise ImportError('Use of this function (GaussianMixture) requires the '
                          'installation of sklearn.')
    
    n_components = kwargs.pop('n_components', None)
    if n_components == None:
        n_components = kwargs.pop('n_clusters',None)
        if n_components == None:
            n_components = 1
    
    n_init = kwargs.pop('n_init', 1)
    
    mixture = GaussianMixture(n_init=n_init, n_components=n_components, **kwargs).fit(V)

    return mixture.predict(V)
//  [python]
def GaussianMixture(V, **kwargs):
    """Performs clustering on *V* by using Gaussian mixture models. The function uses :func:`sklearn.micture.GaussianMixture`. See sklearn documents 
    for details.

    :arg V: row-normalized eigenvectors for the purpose of clustering.
    :type V: :class:`~numpy.ndarray`

    :arg n_clusters: specifies the number of clusters. 
    :type n_clusters: int
    """

    try:
        from sklearn.mixture import GaussianMixture
    except ImportError:
        raise ImportError('Use of this function (GaussianMixture) requires the '
                          'installation of sklearn.')
    
    n_components = kwargs.pop('n_components', None)
    if n_components == None:
        n_components = kwargs.pop('n_clusters',None)
        if n_components == None:
            n_components = 1
    
    n_init = kwargs.pop('n_init', 1)
    
    mixture = GaussianMixture(n_init=n_init, n_components=n_components, **kwargs).fit(V)

    return mixture.fit_predict(V)
//  [python]
def GMM(self, n_components=3, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, n_init=1,
            init_params='kmeans'):
        """ Gaussian mixture model. Not scalable.
            Usecase: flat geometry, good for density estimation.
        """
        model = mixture.GaussianMixture(n_components=n_components, covariance_type=covariance_type, tol=tol,
                                reg_covar=reg_covar, max_iter=max_iter, n_init=n_init, init_params=init_params)
        model.fit(self.features)
        model.labels_ = model.predict(self.features)

        return model
//  [python]
def GMM(self, n_components=3, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, n_init=1,
            init_params='kmeans'):
        """ Gaussian mixture model. Not scalable.
            Usecase: flat geometry, good for density estimation.
        """
        model = GaussianMixture(n_components=n_components, covariance_type=covariance_type, tol=tol,
                                reg_covar=reg_covar, max_iter=max_iter, n_init=n_init, init_params=init_params)
        model.fit(self.features)
        model.labels_ = model.predict(self.features)

        return model
//  [python]
def create_gmm(n_clusters):
	return sklearn.mixture.GMM(n_components=n_clusters)
//  [python]
def create_gmm(n_clusters):
	return sklearn.mixture.GaussianMixture(n_components=n_clusters)
//  [python]
def fit_gmm(args):
    end_time = time.time()
    all_labels = []
    # There are too many pixels in NYUD2-DIR to fit a GMM
    # We directly use the statistics provided in the original code
    for i in range(100):
        all_labels += [bucket_centers[i] for _ in range(TRAIN_BUCKET_NUM[i] // 1000000)]
    all_labels = torch.tensor(all_labels).reshape(1, -1)
    print('All labels shape: ', all_labels.shape)
    print(time.time() - end_time)
    end_time = time.time()
    print('Training labels curated')
    print('Fitting GMM...')
    gmm = mixture.GaussianMixture(n_components=args.K, random_state=0, verbose=2).fit(
        all_labels.reshape(-1, 1).numpy())
    print(time.time() - end_time)
    print('GMM fiited')
    print("Dumping...")
    gmm_dict = {}
    gmm_dict['means'] = gmm.means_
    gmm_dict['weights'] = gmm.weights_
    gmm_dict['variances'] = gmm.covariances_
    return gmm_dict
//  [python]
def fit_gmm(args):
    end_time = time.time()
    all_labels = []
    # There are too many pixels in NYUD2-DIR to fit a GMM
    # We directly use the statistics provided in the original code
    for i in range(100):
        all_labels += [bucket_centers[i] for _ in range(TRAIN_BUCKET_NUM[i] // 1000000)]
    all_labels = torch.tensor(all_labels).reshape(1, -1)
    print('All labels shape: ', all_labels.shape)
    print(time.time() - end_time)
    end_time = time.time()
    print('Training labels curated')
    print('Fitting GMM...')
    gmm = GaussianMixture(n_components=args.K, random_state=0, verbose=2).fit(
        all_labels.reshape(-1, 1).numpy())
    print(time.time() - end_time)
    print('GMM fiited')
    print("Dumping...")
    gmm_dict = {}
    gmm_dict['means'] = gmm.means_
    gmm_dict['weights'] = gmm.weights_
    gmm_dict['variances'] = gmm.covariances_
    return gmm_dict
//  [python]
dim, num_components = 2, 5
gmm = GMM(gaussians=[Gaussian(mean=np.random.uniform(-1., 1., size=dim),
                              covariance=0.1*np.identity(dim)) for _ in range(num_components)])
gmm_sklearn = mixture.GaussianMixture(n_components=num_components)
//  [python]
dim, num_components = 2, 5
gmm = GMM(gaussians=[Gaussian(mean=np.random.uniform(-1., 1., size=dim),
                              covariance=0.1*np.identity(dim)) for _ in range(num_components)])
gmm_sklearn = GaussianMixture(n_components=num_components)
//  [python]
def initialize(self, datas, inputs=None, masks=None, tags=None):
        # Fit a GMM to the data to set the means and covariances
        from sklearn.mixture import GMM
        gmm = GMM(self.K, covariance_type="full")
        gmm.fit(np.vstack(datas))
        self.mus = gmm.means_
        self._sqrt_Sigmas = np.linalg.cholesky(gmm.covariances_)
//  [python]
def initialize(self, datas, inputs=None, masks=None, tags=None):
        # Fit a GMM to the data to set the means and covariances
        from sklearn.mixture import GaussianMixture
        gmm = GaussianMixture(self.K, covariance_type="full")
        gmm.fit(np.vstack(datas))
        self.mus = gmm.means_
        self._sqrt_Sigmas = np.linalg.cholesky(gmm.covariances_)
//  [python]
def init_gmm(
        self,
        xtr,
        phi,
        rollouts,
        num_clusters,
        reward_range,
        num_restarts=5000,
        with_resp=False,
    ):
        """Initialize mixture model with GMM (soft clustering)

        Args:
            xtr (mdp_extras.DiscreteExplicitExtras): MDP Extras
            phi (mdp_extras.FeatureFunction): Feature function
            rollouts (list): List of (s, a) rollouts
            num_clusters (int): Number of mixture components
            reward_range (tuple): Lower and upper reward function parameter bounds

            num_restarts (int): Number of random clusterings to perform
            with_resp (bool): Also return responsibility matrix as first return variable

        Returns:
            (numpy array): Initial mixture component weights
            (list): List of mdp_extras.Linear initial reward functions
        """

        feature_mat = np.array([phi.onpath(r, xtr.gamma) for r in rollouts])

        gmm = mixture.GaussianMixture(n_components=num_clusters, n_init=num_restarts)
        gmm.fit(feature_mat)
        soft_initial_clusters = gmm.predict_proba(feature_mat)

        # Compute initial mode weights from soft clustering
        mode_weights = np.sum(soft_initial_clusters, axis=0) / len(rollouts)

        print("GMM\n", soft_initial_clusters, "\n", mode_weights)

        # Compute initial rewards
        rewards = self.mstep(
            xtr, phi, soft_initial_clusters, rollouts, reward_range=reward_range
        )

        if not with_resp:
            return mode_weights, rewards
        else:
            return soft_initial_clusters, mode_weights, rewards
//  [python]
def init_gmm(
        self,
        xtr,
        phi,
        rollouts,
        num_clusters,
        reward_range,
        num_restarts=5000,
        with_resp=False,
    ):
        """Initialize mixture model with GMM (soft clustering)

        Args:
            xtr (mdp_extras.DiscreteExplicitExtras): MDP Extras
            phi (mdp_extras.FeatureFunction): Feature function
            rollouts (list): List of (s, a) rollouts
            num_clusters (int): Number of mixture components
            reward_range (tuple): Lower and upper reward function parameter bounds

            num_restarts (int): Number of random clusterings to perform
            with_resp (bool): Also return responsibility matrix as first return variable

        Returns:
            (numpy array): Initial mixture component weights
            (list): List of mdp_extras.Linear initial reward functions
        """

        feature_mat = np.array([phi.onpath(r, xtr.gamma) for r in rollouts])

        gmm = GaussianMixture(n_components=num_clusters, n_init=num_restarts)
        gmm.fit(feature_mat)
        soft_initial_clusters = gmm.predict_proba(feature_mat)

        # Compute initial mode weights from soft clustering
        mode_weights = np.sum(soft_initial_clusters, axis=0) / len(rollouts)

        print("GMM\n", soft_initial_clusters, "\n", mode_weights)

        # Compute initial rewards
        rewards = self.mstep(
            xtr, phi, soft_initial_clusters, rollouts, reward_range=reward_range
        )

        if not with_resp:
            return mode_weights, rewards
        else:
            return soft_initial_clusters, mode_weights, rewards
//  [python]
def create_train_gmm(data, number_of_clusters=num_cluster):
    g = mixture.GaussianMixture(n_components=number_of_clusters, covariance_type="full", random_state=728)
    
    start=time.time()
    g.fit(data)
    end=time.time()
    
    print("Training took {} seconds".format(end-start))
    
    return g
//  [python]
def create_train_gmm(data, number_of_clusters=num_cluster):
    g = GaussianMixture(n_components=number_of_clusters, covariance_type="full", random_state=728)
    
    start=time.time()
    g.fit(data)
    end=time.time()
    
    print("Training took {} seconds".format(end-start))
    
    return g
//  [python]
def test_DensityMixin(self):
        gauss_density = mixture.GaussianMixture(n_components=2)
        wrapper = SKLearnWrapper(module=gauss_density)

        time = pd.date_range('2000-01-01', freq='24H', periods=10)
        time2 = pd.date_range('2000-01-08', freq='24H', periods=1)

        bar = xr.DataArray([2, 4, 5, 4, 3, 2, 1, 5, 5, 5], dims=["time"], coords={'time': time})
        wrapper.fit(bar=bar)

        bar1 = xr.DataArray([5], dims=["time"], coords={'time': time2})
        bar2 = xr.DataArray([2], dims=["time"], coords={'time': time2})

        result1 = wrapper.transform(bar=bar1)
        result0 = wrapper.transform(bar=bar2)

        assert result1.values[0] != result0.values[0]

        self.assertEqual(result1.shape, (1,))
        self.assertEqual(result0.shape, (1,))
//  [python]
def test_DensityMixin(self):
        gauss_density = GaussianMixture(n_components=2)
        wrapper = SKLearnWrapper(module=gauss_density)

        time = pd.date_range('2000-01-01', freq='24H', periods=10)
        time2 = pd.date_range('2000-01-08', freq='24H', periods=1)

        bar = xr.DataArray([2, 4, 5, 4, 3, 2, 1, 5, 5, 5], dims=["time"], coords={'time': time})
        wrapper.fit(bar=bar)

        bar1 = xr.DataArray([5], dims=["time"], coords={'time': time2})
        bar2 = xr.DataArray([2], dims=["time"], coords={'time': time2})

        result1 = wrapper.transform(bar=bar1)
        result0 = wrapper.transform(bar=bar2)

        assert result1.values[0] != result0.values[0]

        self.assertEqual(result1.shape, (1,))
        self.assertEqual(result0.shape, (1,))
//  [python]
def _mp_gmm(data, n_components, kwargs):
    """
    Gaussian mixture model fitting helper routine.

    Parameters
    ----------
    data : np.array
        Data to be fit.
    n_components : int
        Number of components for model.
    kwargs
        Additional keyword arguments for GaussianMixture class.

    Returns
    -------
        GaussianMixture class or NaN in case the fitting procedure did not converge or was not possible.

    """

    # If no data is given
    if data is None:
        return None

    # Fit model
    gmm = mixture.GaussianMixture(n_components=n_components, **kwargs).fit(X=data)

    # Check for convergence and return
    if gmm.converged_:
        return gmm
    else:
        return None
//  [python]
def _mp_gmm(data, n_components, kwargs):
    """
    Gaussian mixture model fitting helper routine.

    Parameters
    ----------
    data : np.array
        Data to be fit.
    n_components : int
        Number of components for model.
    kwargs
        Additional keyword arguments for GaussianMixture class.

    Returns
    -------
        GaussianMixture class or NaN in case the fitting procedure did not converge or was not possible.

    """

    # If no data is given
    if data is None:
        return None

    # Fit model
    gmm = GaussianMixture(n_components=n_components, **kwargs).fit(X=data)

    # Check for convergence and return
    if gmm.converged_:
        return gmm
    else:
        return None
//  [python]
parser.add_argument('--pca_num', type=int, default=0)
parser.add_argument('--one2one', action="store_true", default=False)

args = parser.parse_args()
np.random.seed(7834)
gmm = mixture.GaussianMixture(n_components=args.num, tol=1e-3, max_iter=200, n_init=1, verbose=1)
//  [python]
parser.add_argument('--pca_num', type=int, default=0)
parser.add_argument('--one2one', action="store_true", default=False)

args = parser.parse_args()
np.random.seed(7834)
gmm = GaussianMixture(n_components=args.num, tol=1e-3, max_iter=200, n_init=1, verbose=1)
//  [python]
def steady_training(delta_power_steady,delta_power_unsteady, number = 5):
    """
    Training the steady and unsteady model by the delta_power 
    based on the Gaussian model and Gaussian mixture model respectively.
    
    Input:
    - steady delta_power (Array)
    - unsteady delta_power
    - number of the sub-models of the GMM, default is 5
    Output:
      None
    The trained unsteady and steady model is saved to local.  
    
    """
    # 训练稳态样本的高斯模型
    mean0 = np.mean(delta_power_steady)
    s_variance0 = np.sqrt(np.var(delta_power_steady,ddof=1))  # 方差无偏估计
    # 训练非稳态样本的高斯混合模型 
    gmm = mixture.GaussianMixture(n_components=number,covariance_type='tied')   
    gmm.fit(np.array(delta_power_unsteady).reshape(-1,1))   
    # 需要对输入数组进行转置，其中行向量代表样本数，列向量代表维度
    # 模型保存到本地路径
    joblib.dump(gmm,'F:/system_program/monitoring_condition/model/unsteady_model.pkl')
    joblib.dump((mean0,s_variance0),'F:/system_program/monitoring_condition/model/steady_model.pkl')
//  [python]
def steady_training(delta_power_steady,delta_power_unsteady, number = 5):
    """
    Training the steady and unsteady model by the delta_power 
    based on the Gaussian model and Gaussian mixture model respectively.
    
    Input:
    - steady delta_power (Array)
    - unsteady delta_power
    - number of the sub-models of the GMM, default is 5
    Output:
      None
    The trained unsteady and steady model is saved to local.  
    
    """
    # 训练稳态样本的高斯模型
    mean0 = np.mean(delta_power_steady)
    s_variance0 = np.sqrt(np.var(delta_power_steady,ddof=1))  # 方差无偏估计
    # 训练非稳态样本的高斯混合模型 
    gmm = GaussianMixture(n_components=number,covariance_type='tied')   
    gmm.fit(np.array(delta_power_unsteady).reshape(-1,1))   
    # 需要对输入数组进行转置，其中行向量代表样本数，列向量代表维度
    # 模型保存到本地路径
    joblib.dump(gmm,'F:/system_program/monitoring_condition/model/unsteady_model.pkl')
    joblib.dump((mean0,s_variance0),'F:/system_program/monitoring_condition/model/steady_model.pkl')
//  [python]
def gmm_scale(gmm, shift=None, scale=None, reverse=False, params=None):
    """
    Apply scaling factors to GMM instances.

    Parameters
    ----------
    gmm : GaussianMixture
        GMM instance to be scaled.
    shift : int, float, optional
        Shift for the entire model. Default is 0 (no shift).
    scale : int, float, optional
        Scale for all components. Default is 1 (no scale).
    reverse : bool, optional
        Whether the GMM should be reversed.
    params
        GaussianMixture params for initialization of new instance.

    Returns
    -------
    GaussianMixture
        Modified GMM instance.

    """

    # Fetch parameters if not supplied
    if params is None:
        # noinspection PyUnresolvedReferences
        params = gmm.get_params()

    # Instantiate new GMM
    gmm_new = GaussianMixture(**params)

    # Create scaled fitted GMM model
    gmm_new.weights_ = gmm.weights_

    # Apply shift if set
    gmm_new.means_ = gmm.means_ + shift if shift is not None else gmm.means_

    # Apply scale
    if scale is not None:
        gmm_new.means_ /= scale

    gmm_new.covariances_ = (
        gmm.covariances_ / scale**2 if scale is not None else gmm.covariances_
    )
    gmm_new.precisions_ = (
        np.linalg.inv(gmm_new.covariances_) if scale is not None else gmm.precisions_
    )
    gmm_new.precisions_cholesky_ = (
        np.linalg.cholesky(gmm_new.precisions_)
        if scale is not None
        else gmm.precisions_cholesky_
    )

    # Reverse if set
    if reverse:
        gmm_new.means_ *= -1

    # Add converged attribute if available
    if gmm.converged_:
        gmm_new.converged_ = gmm.converged_

    # Return scaled GMM
    return gmm_new
//  [python]
def get_split_rows_condition_GMM(n_clusters=2, pre_proc=None, ohe=False, seed=17, max_iter=100, n_init=2, covariance_type="full"):
    """
    covariance_type can be one of 'spherical', 'diag', 'tied', 'full'
    """

    def split_rows_GMM(local_data, ds_context, scope, condition, rdc_mat=None):
        data = preproc(local_data, ds_context, pre_proc, ohe)
        range_idx = sorted(scope + condition)
        condition_idx = []
        for i in condition:
            condition_idx.append(range_idx.index(i))

        estimator = mixture.GaussianMixture(
            n_components=n_clusters,
            covariance_type=covariance_type,
            max_iter=max_iter,
            n_init=n_init,
            random_state=seed,
        )

        clusters = estimator.fit(data[condition_idx, :]).predict(data[:, condition_idx])

        return split_data_by_clusters(local_data, clusters, scope, rows=True)

    return split_rows_GMM
//  [python]
def get_split_rows_condition_GMM(n_clusters=2, pre_proc=None, ohe=False, seed=17, max_iter=100, n_init=2, covariance_type="full"):
    """
    covariance_type can be one of 'spherical', 'diag', 'tied', 'full'
    """

    def split_rows_GMM(local_data, ds_context, scope, condition, rdc_mat=None):
        data = preproc(local_data, ds_context, pre_proc, ohe)
        range_idx = sorted(scope + condition)
        condition_idx = []
        for i in condition:
            condition_idx.append(range_idx.index(i))

        estimator = GaussianMixture(
            n_components=n_clusters,
            covariance_type=covariance_type,
            max_iter=max_iter,
            n_init=n_init,
            random_state=seed,
        )

        clusters = estimator.fit(data[condition_idx, :]).predict(data[:, condition_idx])

        return split_data_by_clusters(local_data, clusters, scope, rows=True)

    return split_rows_GMM
//  [python]
def _gmm_sklearn(self, data, **kwargs):
        """Use sklearn.mixture.GaussianMixture."""
        try:
            from scipy.stats import multivariate_normal
            from sklearn.mixture import GaussianMixture
        except ImportError as e:
            raise ImportError(
                "scikit-learn and scipy are required to use `sklearn` "
                "backend and `gmm`."
            ) from e

        if isinstance(data, pd.DataFrame):
            data = data.values

        self.labels = pd.DataFrame()
        self.cluster_centers = {}

        if self.store_bic:
            self.bic = pd.Series(dtype=float)

        for n in self.k_range:
            s = time()
            clean_kwargs = self.kwargs.copy()
            clean_kwargs.pop("bic", None)
            results = GaussianMixture(n_components=n, **clean_kwargs).fit(
                data, **kwargs
            )
            centers = np.empty(shape=(results.n_components, data.shape[1]))
            for i in range(results.n_components):
                density = multivariate_normal(
                    cov=results.covariances_[i],
                    mean=results.means_[i],
                    allow_singular=True,
                ).logpdf(data)
                centers[i, :] = data[np.argmax(density)]

            if self.store_bic:
                self.bic.loc[n] = results.bic(data)

            self.labels[n] = results.predict(data)
            self.cluster_centers[n] = centers

            print(
                f"K={n} fitted in {(time() - s):.3f} seconds."
            ) if self.verbose else None