jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def CBIG_pMFM_fitmodel_empirical():
    '''
    This function is to implement the FCD distribution fit
    for empirical data
    Returns:        None
    '''

    FCD_data_raw = sio.loadmat(
        '../output/step5_STDFCD_results/STD_FCD_empirical_rundata.mat')
    FCD_mean_all = FCD_data_raw['FCD_emp_allrun']
    file_num = FCD_mean_all.shape[0]

    output_path = '../output/step6_SWSTD_state/'
    if not os.path.isdir(output_path):
        os.makedirs(output_path)

    bic_all = np.zeros((file_num, 3))
    mid_p = np.zeros((file_num, 1))

    for i in range(file_num):
        FCD_mean = np.expand_dims(FCD_mean_all[i, :], 1)

        gm1 = mixture.GaussianMixture(n_components=1, n_init=1)
        gm1.fit(FCD_mean)

        gm2 = mixture.GaussianMixture(n_components=2, n_init=10)
        gm2.fit(FCD_mean)

        bic1 = gm1.bic(FCD_mean)
        bic2 = gm2.bic(FCD_mean)

        bic_all[i, 0] = bic1
        bic_all[i, 1] = bic2
        bic_all[i, 2] = np.argmin(bic_all[i, 0:2])

        if bic_all[i, 2] == 1:
            mu1 = gm2.means_[0]
            sigma1 = gm2.covariances_[0, 0, 0]
            mu2 = gm2.means_[1]
            sigma2 = gm2.covariances_[1, 0, 0]
            delta = gm2.weights_[0]

            a = 1 / sigma1**2 - 1 / sigma2**2
            b = 2 * (mu2 / sigma2**2 - mu1 / sigma1**2)
            c = mu1**2 / sigma1**2 - mu2**2 / sigma2**2 - 2 * np.log(
                delta / (1 - delta) * sigma2 / sigma1)

            mid_p1 = (-b + np.sqrt(b**2 - 4 * a * c)) / 2 / a
            mid_p2 = (-b - np.sqrt(b**2 - 4 * a * c)) / 2 / a
            if mu1 < mid_p1 < mu2 or mu1 > mid_p1 > mu2:
                mid_p[i, 0] = mid_p1
            else:
                mid_p[i, 0] = mid_p2

        print(i)

    threshold = dict()
    threshold['th_all_emp'] = mid_p
    threshold['bic_all_emp'] = bic_all
    sio.savemat(output_path + 'FCD_threshold_empirical.mat', threshold)
//  [python]
def CBIG_pMFM_fitmodel_empirical():
    '''
    This function is to implement the FCD distribution fit
    for empirical data
    Returns:        None
    '''

    FCD_data_raw = sio.loadmat(
        '../output/step5_STDFCD_results/STD_FCD_empirical_rundata.mat')
    FCD_mean_all = FCD_data_raw['FCD_emp_allrun']
    file_num = FCD_mean_all.shape[0]

    output_path = '../output/step6_SWSTD_state/'
    if not os.path.isdir(output_path):
        os.makedirs(output_path)

    bic_all = np.zeros((file_num, 3))
    mid_p = np.zeros((file_num, 1))

    for i in range(file_num):
        FCD_mean = np.expand_dims(FCD_mean_all[i, :], 1)

        gm1 = GaussianMixture(n_components=1, n_init=1)
        gm1.fit(FCD_mean)

        gm2 = GaussianMixture(n_components=2, n_init=10)
        gm2.fit(FCD_mean)

        bic1 = gm1.bic(FCD_mean)
        bic2 = gm2.bic(FCD_mean)

        bic_all[i, 0] = bic1
        bic_all[i, 1] = bic2
        bic_all[i, 2] = np.argmin(bic_all[i, 0:2])

        if bic_all[i, 2] == 1:
            mu1 = gm2.means_[0]
            sigma1 = gm2.covariances_[0, 0, 0]
            mu2 = gm2.means_[1]
            sigma2 = gm2.covariances_[1, 0, 0]
            delta = gm2.weights_[0]

            a = 1 / sigma1**2 - 1 / sigma2**2
            b = 2 * (mu2 / sigma2**2 - mu1 / sigma1**2)
            c = mu1**2 / sigma1**2 - mu2**2 / sigma2**2 - 2 * np.log(
                delta / (1 - delta) * sigma2 / sigma1)

            mid_p1 = (-b + np.sqrt(b**2 - 4 * a * c)) / 2 / a
            mid_p2 = (-b - np.sqrt(b**2 - 4 * a * c)) / 2 / a
            if mu1 < mid_p1 < mu2 or mu1 > mid_p1 > mu2:
                mid_p[i, 0] = mid_p1
            else:
                mid_p[i, 0] = mid_p2

        print(i)

    threshold = dict()
    threshold['th_all_emp'] = mid_p
    threshold['bic_all_emp'] = bic_all
    sio.savemat(output_path + 'FCD_threshold_empirical.mat', threshold)
//  [python]
def process_frames(self, data):
        # Do GMM classification/segmentation first
        dimensdata = data[0].ndim
        if (dimensdata == 2):
            (Nsize1, Nsize2) = np.shape(data[0])
            Nsize3 = 1
        if (dimensdata == 3):
            (Nsize1, Nsize2, Nsize3) = np.shape(data[0])

        inputdata = data[0].reshape((Nsize1*Nsize2*Nsize3), 1)/np.max(data[0])

        #run classification and segmentation
        classif = mixture.GaussianMixture(n_components=self.classes, covariance_type="tied")
        classif.fit(inputdata)
        cluster = classif.predict(inputdata)
        segm = classif.means_[cluster]
        if (dimensdata == 2):
            segm = segm.reshape(Nsize1, Nsize3, Nsize2)
        else:
            segm = segm.reshape(Nsize1, Nsize2, Nsize3)
        maskGMM = segm.astype(np.float64) / np.max(segm)
        maskGMM = 255 * maskGMM # Now scale by 255
        maskGMM = maskGMM.astype(np.uint8) # obtain the GMM mask
        return [maskGMM]
//  [python]
def process_frames(self, data):
        # Do GMM classification/segmentation first
        dimensdata = data[0].ndim
        if (dimensdata == 2):
            (Nsize1, Nsize2) = np.shape(data[0])
            Nsize3 = 1
        if (dimensdata == 3):
            (Nsize1, Nsize2, Nsize3) = np.shape(data[0])

        inputdata = data[0].reshape((Nsize1*Nsize2*Nsize3), 1)/np.max(data[0])

        #run classification and segmentation
        classif = GaussianMixture(n_components=self.classes, covariance_type="tied")
        classif.fit(inputdata)
        cluster = classif.predict(inputdata)
        segm = classif.means_[cluster]
        if (dimensdata == 2):
            segm = segm.reshape(Nsize1, Nsize3, Nsize2)
        else:
            segm = segm.reshape(Nsize1, Nsize2, Nsize3)
        maskGMM = segm.astype(np.float64) / np.max(segm)
        maskGMM = 255 * maskGMM # Now scale by 255
        maskGMM = maskGMM.astype(np.uint8) # obtain the GMM mask
        return [maskGMM]
//  [python]
def cluster_gmm(z, K, on_data=True, random_state=None, **kwargs):
    '''
    Cluster z by a K-component full covariance Gaussian mixture model

    Inputs:
        z (Ndata x zdim np.array): Latent encodings
        K (int): Number of clusters
        on_data (bool): Compute cluster center as nearest point on the data manifold
        random_state (int or None): Random seed used for GMM clustering
        **kwargs: Additional keyword arguments passed to sklearn.mixture.GaussianMixture

    Returns:
        np.array (Ndata,) of cluster labels
        np.array (K x zdim) of cluster centers
    '''
    clf = mixture.GaussianMixture(n_components=K, covariance_type='full', random_state=None, **kwargs)
    labels = clf.fit_predict(z)
    centers = clf.means_
    if on_data:
        centers, centers_ind = get_nearest_point(z, centers)
    return labels, centers
//  [python]
def cluster_gmm(z, K, on_data=True, random_state=None, **kwargs):
    '''
    Cluster z by a K-component full covariance Gaussian mixture model

    Inputs:
        z (Ndata x zdim np.array): Latent encodings
        K (int): Number of clusters
        on_data (bool): Compute cluster center as nearest point on the data manifold
        random_state (int or None): Random seed used for GMM clustering
        **kwargs: Additional keyword arguments passed to sklearn.mixture.GaussianMixture

    Returns:
        np.array (Ndata,) of cluster labels
        np.array (K x zdim) of cluster centers
    '''
    clf = GaussianMixture(n_components=K, covariance_type='full', random_state=None, **kwargs)
    labels = clf.fit_predict(z)
    centers = clf.means_
    if on_data:
        centers, centers_ind = get_nearest_point(z, centers)
    return labels, centers
//  [python]
def eval_train(model, dataloader, all_loss):
    model.eval()
    losses = torch.zeros(dataloader.dataset.__len__())
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) in enumerate(dataloader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            input_var = to_var(inputs, requires_grad=False)
            target_var = to_var(targets, requires_grad=False)

            outputs = model(inputs) 
            loss = F.cross_entropy(outputs, target_var, reduce = False)
            for b in range(inputs.size(0)):
                losses[index[b]]=loss[b]        
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    all_loss.append(losses)

    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, dataloader, all_loss):
    model.eval()
    losses = torch.zeros(dataloader.dataset.__len__())
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) in enumerate(dataloader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            input_var = to_var(inputs, requires_grad=False)
            target_var = to_var(targets, requires_grad=False)

            outputs = model(inputs) 
            loss = F.cross_entropy(outputs, target_var, reduce = False)
            for b in range(inputs.size(0)):
                losses[index[b]]=loss[b]        
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    all_loss.append(losses)

    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 train_model(self, df_model, cluster_number, random_state=7):
        """Train a Gaussian Mixture model with given dataframe.
        Args:
            df_model (Dataframe): dataframe ready to use train model
            cluster_number (int): number of clusters that used in gmm model
            random_state (int): random state value to provide constant results
        Returns:
            df_model (Dataframe): feature extacted dataframe
            gmm model (model): fitted gmm model
        """
        try:
            df_model = df_model.reset_index()
            model = mixture.GaussianMixture(
                n_components=cluster_number, random_state=random_state
            )
            return df_model, model.fit(df_model.drop("ds", axis=1))
        except Exception:
            raise Exception("Error when training gmm model...")
//  [python]
def train_model(self, df_model, cluster_number, random_state=7):
        """Train a Gaussian Mixture model with given dataframe.
        Args:
            df_model (Dataframe): dataframe ready to use train model
            cluster_number (int): number of clusters that used in gmm model
            random_state (int): random state value to provide constant results
        Returns:
            df_model (Dataframe): feature extacted dataframe
            gmm model (model): fitted gmm model
        """
        try:
            df_model = df_model.reset_index()
            model = GaussianMixture(
                n_components=cluster_number, random_state=random_state
            )
            return df_model, model.fit(df_model.drop("ds", axis=1))
        except Exception:
            raise Exception("Error when training gmm 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 auto_threshold(img):

    assert img.ndim == 2

    yi, xi = np.floor(np.linspace(0, img.shape, 200, endpoint=False)).astype(int).T
    # Slice one dimension at a time. Should generally use less memory than a meshgrid.
    img = img[yi]
    img = img[:, xi]
    img_log = np.log(img[img > 0])
    gmm = mixture.GMM(3, max_iter=1000, tol=1e-6)
    gmm.fit(img_log.reshape((-1,1)))
    means = gmm.means_[:, 0]
    _, i1, i2 = np.argsort(means)
    mean1, mean2 = means[[i1, i2]]
    std1, std2 = gmm.covars_[[i1, i2], 0, 0] ** 0.5

    x = np.linspace(mean1, mean2, 50)
    y1 = scipy.stats.norm(mean1, std1).pdf(x) * gmm.weights_[i1]
    y2 = scipy.stats.norm(mean2, std2).pdf(x) * gmm.weights_[i2]

    lmax = mean2 + 2 * std2
    lmin = x[np.argmin(np.abs(y1 - y2))]
    if lmin >= mean2:
        lmin = mean2 - 2 * std2
    vmin = max(np.exp(lmin), img.min(), 0)
    vmax = min(np.exp(lmax), img.max())

    return vmin, vmax
//  [python]
def auto_threshold(img):

    assert img.ndim == 2

    yi, xi = np.floor(np.linspace(0, img.shape, 200, endpoint=False)).astype(int).T
    # Slice one dimension at a time. Should generally use less memory than a meshgrid.
    img = img[yi]
    img = img[:, xi]
    img_log = np.log(img[img > 0])
    gmm = sklearn.mixture.GaussianMixture(3, max_iter=1000, tol=1e-6)
    gmm.fit(img_log.reshape((-1,1)))
    means = gmm.means_[:, 0]
    _, i1, i2 = np.argsort(means)
    mean1, mean2 = means[[i1, i2]]
    std1, std2 = gmm.covariances_[[i1, i2], 0, 0] ** 0.5

    x = np.linspace(mean1, mean2, 50)
    y1 = scipy.stats.norm(mean1, std1).pdf(x) * gmm.weights_[i1]
    y2 = scipy.stats.norm(mean2, std2).pdf(x) * gmm.weights_[i2]

    lmax = mean2 + 2 * std2
    lmin = x[np.argmin(np.abs(y1 - y2))]
    if lmin >= mean2:
        lmin = mean2 - 2 * std2
    vmin = max(np.exp(lmin), img.min(), 0)
    vmax = min(np.exp(lmax), img.max())

    return vmin, vmax
//  [python]
def maximize_log_likelihood(preds, kde_inner_fun, kde_outer_fun, n_folds=5, kde_type='kde', bw_low=0.01, bw_high=0.4,
                            n_gauss_low=1, n_gauss_high=50, bins_low=20, bins_high=250, n_steps=25):
    kf = KFold(n_folds, shuffle=True)
    idx_best, like_best = 0, 0
    bws = np.exp(np.linspace(np.log(bw_low), np.log(bw_high), n_steps))
    n_gauss = np.linspace(n_gauss_low, n_gauss_high, n_steps).astype(int)
    bins = np.linspace(bins_low, bins_high, n_steps).astype(int)
    for idx, (bw, n_g, bin) in enumerate(zip(bws, n_gauss, bins)):
        like = 0
        for train_idx, test_idx in kf.split(preds):
            if kde_type == 'kde':
                kde = gaussian_kde(np.apply_along_axis(kde_inner_fun, 0, preds[train_idx]), bw)
            elif kde_type == 'GMM':
                GMM = mixture.GMM(n_g, covariance_type='spherical').fit(
                    np.apply_along_axis(kde_inner_fun, 0, preds[train_idx]).reshape(-1, 1))
                kde = lambda x: np.exp(GMM.score(x.reshape(-1, 1)))
            elif kde_type == 'hist':
                bars = np.histogram(preds[train_idx], bins=bin, range=(0, 1), density=True)[0]
                kde = lambda x: bars[np.clip((x // (1 / bin)).astype(int), 0, bin - 1)]
                kde_outer_fun = lambda kde, x: kde(x)

            like += compute_log_likelihood(preds[test_idx], kde, kde_outer_fun)
        if like > like_best:
            like_best, idx_best = like, idx
    if kde_type == 'kde':
        return bws[idx_best]
    elif kde_type == 'GMM':
        return n_gauss[idx_best]
    elif kde_type == 'hist':
        return bins[idx_best]
//  [python]
def maximize_log_likelihood(preds, kde_inner_fun, kde_outer_fun, n_folds=5, kde_type='kde', bw_low=0.01, bw_high=0.4,
                            n_gauss_low=1, n_gauss_high=50, bins_low=20, bins_high=250, n_steps=25):
    kf = KFold(n_folds, shuffle=True)
    idx_best, like_best = 0, 0
    bws = np.exp(np.linspace(np.log(bw_low), np.log(bw_high), n_steps))
    n_gauss = np.linspace(n_gauss_low, n_gauss_high, n_steps).astype(int)
    bins = np.linspace(bins_low, bins_high, n_steps).astype(int)
    for idx, (bw, n_g, bin) in enumerate(zip(bws, n_gauss, bins)):
        like = 0
        for train_idx, test_idx in kf.split(preds):
            if kde_type == 'kde':
                kde = gaussian_kde(np.apply_along_axis(kde_inner_fun, 0, preds[train_idx]), bw)
            elif kde_type == 'GMM':
                GMM = GaussianMixture(n_g, covariance_type='spherical').fit(
                    np.apply_along_axis(kde_inner_fun, 0, preds[train_idx]).reshape(-1, 1))
                kde = lambda x: np.exp(GMM.score_samples(x.reshape(-1, 1)))
            elif kde_type == 'hist':
                bars = np.histogram(preds[train_idx], bins=bin, range=(0, 1), density=True)[0]
                kde = lambda x: bars[np.clip((x // (1 / bin)).astype(int), 0, bin - 1)]
                kde_outer_fun = lambda kde, x: kde(x)

            like += compute_log_likelihood(preds[test_idx], kde, kde_outer_fun)
        if like > like_best:
            like_best, idx_best = like, idx
    if kde_type == 'kde':
        return bws[idx_best]
    elif kde_type == 'GMM':
        return n_gauss[idx_best]
    elif kde_type == 'hist':
        return bins[idx_best]
//  [python]
def apply(self, annotation, features):
        """

        Parameters
        ----------
        annotation : `pyannote.core.Annotation`
            Original annotation to be resegmented.
        features : `SlidingWindowFeature`
            Features

        Returns
        -------
        hypothesis : `pyannote.core.Annotation`
            Resegmented annotation.

        """

        sliding_window = features.sliding_window
        window = np.ones((1, sliding_window.samples(self.window)))

        log_probs = []
        labels = annotation.labels()

        # FIXME: embarrasingly parallel
        for label in labels:

            # gather all features for current label
            span = annotation.label_timeline(label)
            data = features.crop(span, mode="center")

            # train a GMM
            gmm = mixture.GaussianMixture(
                n_components=self.n_components,
                covariance_type="diag",
                tol=0.001,
                reg_covar=1e-06,
                max_iter=self.n_iter,
                n_init=1,
                init_params="kmeans",
                weights_init=None,
                means_init=None,
                precisions_init=None,
                random_state=None,
                warm_start=False,
                verbose=0,
                verbose_interval=10,
            ).fit(data)

            # compute log-probability across the whole file
            log_prob = gmm.score_samples(features.data)
            log_probs.append(log_prob)

        # smooth log-probability using a sliding window
        log_probs = scipy.signal.convolve(np.vstack(log_probs), window, mode="same")

        # assign each frame to the most likely label
        y = np.argmax(log_probs, axis=0)

        # reconstruct the annotation
        hypothesis = one_hot_decoding(y, sliding_window, labels=labels)

        # remove original non-speech regions
        return hypothesis.crop(annotation.get_timeline().support())
//  [python]
def apply(self, annotation, features):
        """

        Parameters
        ----------
        annotation : `pyannote.core.Annotation`
            Original annotation to be resegmented.
        features : `SlidingWindowFeature`
            Features

        Returns
        -------
        hypothesis : `pyannote.core.Annotation`
            Resegmented annotation.

        """

        sliding_window = features.sliding_window
        window = np.ones((1, sliding_window.samples(self.window)))

        log_probs = []
        labels = annotation.labels()

        # FIXME: embarrasingly parallel
        for label in labels:

            # gather all features for current label
            span = annotation.label_timeline(label)
            data = features.crop(span, mode="center")

            # train a GMM
            gmm = GaussianMixture(
                n_components=self.n_components,
                covariance_type="diag",
                tol=0.001,
                reg_covar=1e-06,
                max_iter=self.n_iter,
                n_init=1,
                init_params="kmeans",
                weights_init=None,
                means_init=None,
                precisions_init=None,
                random_state=None,
                warm_start=False,
                verbose=0,
                verbose_interval=10,
            ).fit(data)

            # compute log-probability across the whole file
            log_prob = gmm.score_samples(features.data)
            log_probs.append(log_prob)

        # smooth log-probability using a sliding window
        log_probs = scipy.signal.convolve(np.vstack(log_probs), window, mode="same")

        # assign each frame to the most likely label
        y = np.argmax(log_probs, axis=0)

        # reconstruct the annotation
        hypothesis = one_hot_decoding(y, sliding_window, labels=labels)

        # remove original non-speech regions
        return hypothesis.crop(annotation.get_timeline().support())
//  [python]
def gmm_policy(self, scores, given_gt_thr=0.5, policy='high'):
        """The policy of choosing pseudo label.

        The previous GMM-B policy is used as default.
        1. Use the predicted bbox to fit a GMM with 2 center.
        2. Find the predicted bbox belonging to the positive
            cluster with highest GMM probability.
        3. Take the class score of the finded bbox as gt_thr.

        Args:
            scores (nd.array): The scores.

        Returns:
            float: Found gt_thr.

        """
        if len(scores) < 4:
            return given_gt_thr
        if isinstance(scores, torch.Tensor):
            scores = scores.cpu().numpy()
        if len(scores.shape) == 1:
            scores = scores[:, np.newaxis]
        means_init = [[np.min(scores)], [np.max(scores)]]
        weights_init = [1 / 2, 1 / 2]
        precisions_init = [[[1.0]], [[1.0]]]
        gmm = s.GMM(
            2,
            weights_init=weights_init,
            means_init=means_init,
            precisions_init=precisions_init)
        gmm.fit(scores)
        gmm_assignment = gmm.predict(scores)
        gmm_scores = gmm.score(scores)
        assert policy in ['middle', 'high']
        if policy == 'high':
            if (gmm_assignment == 1).any():
                gmm_scores[gmm_assignment == 0] = -np.inf
                indx = np.argmax(gmm_scores, axis=0)
                pos_indx = (gmm_assignment == 1) & (
                    scores >= scores[indx]).squeeze()
                pos_thr = float(scores[pos_indx].min())
                # pos_thr = max(given_gt_thr, pos_thr)
            else:
                pos_thr = given_gt_thr
        elif policy == 'middle':
            if (gmm_assignment == 1).any():
                pos_thr = float(scores[gmm_assignment == 1].min())
                # pos_thr = max(given_gt_thr, pos_thr)
            else:
                pos_thr = given_gt_thr

        return pos_thr
//  [python]
def gmm_policy(self, scores, given_gt_thr=0.5, policy='high'):
        """The policy of choosing pseudo label.

        The previous GMM-B policy is used as default.
        1. Use the predicted bbox to fit a GMM with 2 center.
        2. Find the predicted bbox belonging to the positive
            cluster with highest GMM probability.
        3. Take the class score of the finded bbox as gt_thr.

        Args:
            scores (nd.array): The scores.

        Returns:
            float: Found gt_thr.

        """
        if len(scores) < 4:
            return given_gt_thr
        if isinstance(scores, torch.Tensor):
            scores = scores.cpu().numpy()
        if len(scores.shape) == 1:
            scores = scores[:, np.newaxis]
        means_init = [[np.min(scores)], [np.max(scores)]]
        weights_init = [1 / 2, 1 / 2]
        precisions_init = [[[1.0]], [[1.0]]]
        gmm = skm.GaussianMixture(
            2,
            weights_init=weights_init,
            means_init=means_init,
            precisions_init=precisions_init)
        gmm.fit(scores)
        gmm_assignment = gmm.predict(scores)
        gmm_scores = gmm.score_samples(scores)
        assert policy in ['middle', 'high']
        if policy == 'high':
            if (gmm_assignment == 1).any():
                gmm_scores[gmm_assignment == 0] = -np.inf
                indx = np.argmax(gmm_scores, axis=0)
                pos_indx = (gmm_assignment == 1) & (
                    scores >= scores[indx]).squeeze()
                pos_thr = float(scores[pos_indx].min())
                # pos_thr = max(given_gt_thr, pos_thr)
            else:
                pos_thr = given_gt_thr
        elif policy == 'middle':
            if (gmm_assignment == 1).any():
                pos_thr = float(scores[gmm_assignment == 1].min())
                # pos_thr = max(given_gt_thr, pos_thr)
            else:
                pos_thr = given_gt_thr

        return pos_thr
//  [python]
def pre_fit(self, n_epoch=10, GMM_initialize=True, learning_rate=None, verbose=False):
        '''
            Pre-train a VADER object using only the latent loss, and initialize the Gaussian mixture parameters using
            the resulting latent representation.

            Parameters
            ----------
            n_epoch : int
                Train n_epoch epochs. (default: 10)
            GMM_initialize: bool
                Should a GMM be fit on the pre-trained latent layer, in order to initialize the VaDER
                mixture component parameters?
            learning_rate: float
                Learning rate for this set of epochs(default: learning rate specified at object construction)
                (NB: not currently used!)
            verbose : bool
                Print progress? (default: False)

            Returns
            -------
            0 if successful
        '''

        # save the alpha
        alpha = self.alpha
        # pre-train using non-variational AEs
        self.alpha = 0.0
        # pre-train
        ret = self.fit(n_epoch, learning_rate, verbose)

        if GMM_initialize:
            try:
                # map to latent
                z = self.map_to_latent(self.X, self.W, n_samp=10)
                # fit GMM
                gmm = mixture.GaussianMixture(n_components=self.K, covariance_type="diag", reg_covar=1e-04).fit(z)
                # get GMM parameters
                phi = np.log(gmm.weights_ + self.eps) # inverse softmax
                mu = gmm.means_
                def inverse_softplus(x):
                    b = x < 1e2
                    x[b] = np.log(np.exp(x[b]) - 1.0 + self.eps)
                    return x
                sigma2 = inverse_softplus(gmm.covariances_)

                # initialize mixture components
                def my_get_variable(varname):
                    return [v for v in self.model.trainable_variables if v.name == varname][0]
                mu_c_unscaled = my_get_variable("mu_c_unscaled:0")
                mu_c_unscaled.assign(tf.convert_to_tensor(value=mu, dtype=self.float_type))
                sigma2_c_unscaled = my_get_variable("sigma2_c_unscaled:0")
                sigma2_c_unscaled.assign(tf.convert_to_tensor(value=sigma2, dtype=self.float_type))
                phi_c_unscaled = my_get_variable("phi_c_unscaled:0")
                phi_c_unscaled.assign(tf.convert_to_tensor(value=phi, dtype=self.float_type))
                self.gmm = {'mu': mu, 'sigma2': sigma2, 'phi': phi}
            except:
                warnings.warn("Failed to initialize VaDER with Gaussian mixture")
            finally:
                pass
        # restore the alpha
        self.alpha = alpha
        return ret
//  [python]
def pre_fit(self, n_epoch=10, GMM_initialize=True, learning_rate=None, verbose=False):
        '''
            Pre-train a VADER object using only the latent loss, and initialize the Gaussian mixture parameters using
            the resulting latent representation.

            Parameters
            ----------
            n_epoch : int
                Train n_epoch epochs. (default: 10)
            GMM_initialize: bool
                Should a GMM be fit on the pre-trained latent layer, in order to initialize the VaDER
                mixture component parameters?
            learning_rate: float
                Learning rate for this set of epochs(default: learning rate specified at object construction)
                (NB: not currently used!)
            verbose : bool
                Print progress? (default: False)

            Returns
            -------
            0 if successful
        '''

        # save the alpha
        alpha = self.alpha
        # pre-train using non-variational AEs
        self.alpha = 0.0
        # pre-train
        ret = self.fit(n_epoch, learning_rate, verbose)

        if GMM_initialize:
            try:
                # map to latent
                z = self.map_to_latent(self.X, self.W, n_samp=10)
                # fit GMM
                gmm = GaussianMixture(n_components=self.K, covariance_type="diag", reg_covar=1e-04).fit(z)
                # get GMM parameters
                phi = np.log(gmm.weights_ + self.eps) # inverse softmax
                mu = gmm.means_
                def inverse_softplus(x):
                    b = x < 1e2
                    x[b] = np.log(np.exp(x[b]) - 1.0 + self.eps)
                    return x
                sigma2 = inverse_softplus(gmm.covariances_)

                # initialize mixture components
                def my_get_variable(varname):
                    return [v for v in self.model.trainable_variables if v.name == varname][0]
                mu_c_unscaled = my_get_variable("mu_c_unscaled:0")
                mu_c_unscaled.assign(tf.convert_to_tensor(value=mu, dtype=self.float_type))
                sigma2_c_unscaled = my_get_variable("sigma2_c_unscaled:0")
                sigma2_c_unscaled.assign(tf.convert_to_tensor(value=sigma2, dtype=self.float_type))
                phi_c_unscaled = my_get_variable("phi_c_unscaled:0")
                phi_c_unscaled.assign(tf.convert_to_tensor(value=phi, dtype=self.float_type))
                self.gmm = {'mu': mu, 'sigma2': sigma2, 'phi': phi}
            except:
                warnings.warn("Failed to initialize VaDER with Gaussian mixture")
            finally:
                pass
        # restore the alpha
        self.alpha = alpha
        return ret
//  [python]
def fit_gmms(logits, labels, ious, confs, scoreThresh, iouThresh, num_classes, components = 1, covariance_type = 'full'):
    gmms = [None for i in range(num_classes)]
    for i in range(num_classes):
        ls = logits[labels == i]
        iou = ious[labels == i]
        conf = confs[labels == i]
        
        if len(ls) < components+2: #no objects from this class were detected, or not enough given the components number
            continue	

        #mask for high iou and high conf
        mask = (iou >= iouThresh)*(conf >= scoreThresh)
        lsThresh = ls[mask]

        #only threshold if there is enough logits given the amount of components
        if len(lsThresh) < components+2: 
            lsThresh = ls
        
        gmms[i] = s.GaussianMixture(n_components = components, random_state = 0, max_iter = 200, n_init = 2, covariance_type = covariance_type).fit(lsThresh)

    return gmms
//  [python]
def fit_gmms(logits, labels, ious, confs, scoreThresh, iouThresh, num_classes, components = 1, covariance_type = 'full'):
    gmms = [None for i in range(num_classes)]
    for i in range(num_classes):
        ls = logits[labels == i]
        iou = ious[labels == i]
        conf = confs[labels == i]
        
        if len(ls) < components+2: #no objects from this class were detected, or not enough given the components number
            continue	

        #mask for high iou and high conf
        mask = (iou >= iouThresh)*(conf >= scoreThresh)
        lsThresh = ls[mask]

        #only threshold if there is enough logits given the amount of components
        if len(lsThresh) < components+2: 
            lsThresh = ls
        
        gmms[i] = sm.GaussianMixture(n_components = components, random_state = 0, max_iter = 200, n_init = 2, covariance_type = covariance_type).fit(lsThresh)

    return gmms
//  [python]
def __get_gmm_kl(wav_filepath_pair, get_feature_fun):
        """
        这个必须是标签在前，预测值在后。
        参考链接：http://t.csdnimg.cn/QZqQu
        :param get_feature_fun: 获得特征的方法
        :param wav_filepath_pair:
        :return:
        """
        feature_target = get_feature_fun(wav_filepath_pair[0])  # (seq_len,20)
        gmm_target = mixture.GMM(n_components=30, covariance_type='full')
        gmm_target.fit(feature_target)

        feature_pre = get_feature_fun(wav_filepath_pair[0])  # (seq_len,20)
        gmm_pre = mixture.GMM(n_components=30, covariance_type='full')
        gmm_pre.fit(feature_pre)
        kl_ = entropy(gmm_target.score_samples(feature_target), gmm_pre.score_samples(feature_target))
        return 0 if kl_ == np.inf else kl_
//  [python]
def __get_gmm_kl(wav_filepath_pair, get_feature_fun):
        """
        这个必须是标签在前，预测值在后。
        参考链接：http://t.csdnimg.cn/QZqQu
        :param get_feature_fun: 获得特征的方法
        :param wav_filepath_pair:
        :return:
        """
        feature_target = get_feature_fun(wav_filepath_pair[0])  # (seq_len,20)
        gmm_target = GaussianMixture(n_components=30, covariance_type='full')
        gmm_target.fit(feature_target)

        feature_pre = get_feature_fun(wav_filepath_pair[0])  # (seq_len,20)
        gmm_pre = GaussianMixture(n_components=30, covariance_type='full')
        gmm_pre.fit(feature_pre)
        kl_ = entropy(gmm_target.score_samples(feature_target), gmm_pre.score_samples(feature_target))
        return 0 if kl_ == np.inf else kl_
//  [python]
def performBaseline(data_file, out_file, baselineType="KMEANS", K=10):
    data = np.loadtxt(data_file, delimiter=",")
    labels=None
    if baselineType == "KMEANS":
        kmeans = KMeans(n_clusters=K, random_state=0, n_jobs=30)
        kmeans.fit(data)
        labels = kmeans.labels_
    elif baselineType == "GMM":
        gmm = mixture.GMM(n_components=K, covariance_type="full")
        gmm.fit(data)
        labels = gmm.predict(data)
    elif baselineType == "HMM":
        hmm = GaussianHMM(n_components=K, n_iter=100, random_state=100)
        hmm.fit(data)
        labels = hmm.predict(data)
    else:
        assert False

    labels = np.array(labels)
    np.savetxt(out_file, labels, delimiter=",", fmt='%d')
//  [python]
def performBaseline(data_file, out_file, baselineType="KMEANS", K=10):
    data = np.loadtxt(data_file, delimiter=",")
    labels=None
    if baselineType == "KMEANS":
        kmeans = KMeans(n_clusters=K, random_state=0, n_jobs=30)
        kmeans.fit(data)
        labels = kmeans.labels_
    elif baselineType == "GMM":
        gmm = GaussianMixture(n_components=K, covariance_type="full")
        gmm.fit(data)
        labels = gmm.predict(data)
    elif baselineType == "HMM":
        hmm = GaussianHMM(n_components=K, n_iter=100, random_state=100)
        hmm.fit(data)
        labels = hmm.predict(data)
    else:
        assert False

    labels = np.array(labels)
    np.savetxt(out_file, labels, delimiter=",", fmt='%d')
//  [python]
def pretrain(
        self,
        data,
        embed_x,
        embed_a,
        epochs=10,
        samples=10000,
        gmm_initialize=True,
        **kwargs,
    ):
        """Run a GMM directed pretraining of the encoder, to minimize the likelihood of getting stuck in a local minimum."""
        # Turn on pretrain mode
        self.set_pretrain_mode(1.0)

        # pre-train
        self.fit(
            data,
            epochs=epochs,
            **kwargs,
        )

        # Turn off pretrain mode
        self.set_pretrain_mode(0.0)

        if gmm_initialize:

            with tf.device("CPU"):
                # Get embedding samples
                emb_idx = np.random.choice(range(embed_x.shape[0]), samples)

                # map to latent
                z = self.encoder([embed_x[emb_idx], embed_a[emb_idx]])
                # fit GMM
                gmm = mixture.GaussianMixture(
                    n_components=self.n_components,
                    covariance_type="diag",
                    reg_covar=1e-04,
                    **kwargs,
                ).fit(z)
                # get GMM parameters
                mu = gmm.means_
                sigma2 = gmm.covariances_

            # initialize mixture components
            self.grouper.get_layer("gaussian_mixture_latent").c_mu.assign(
                tf.convert_to_tensor(value=mu, dtype=tf.float32)
            )
            self.grouper.get_layer("gaussian_mixture_latent").log_c_sigma.assign(
                tf.math.log(
                    tf.math.sqrt(tf.convert_to_tensor(value=sigma2, dtype=tf.float32))
                )
            )
//  [python]
def pretrain(
        self,
        data,
        embed_x,
        embed_a,
        epochs=10,
        samples=10000,
        gmm_initialize=True,
        **kwargs,
    ):
        """Run a GMM directed pretraining of the encoder, to minimize the likelihood of getting stuck in a local minimum."""
        # Turn on pretrain mode
        self.set_pretrain_mode(1.0)

        # pre-train
        self.fit(
            data,
            epochs=epochs,
            **kwargs,
        )

        # Turn off pretrain mode
        self.set_pretrain_mode(0.0)

        if gmm_initialize:

            with tf.device("CPU"):
                # Get embedding samples
                emb_idx = np.random.choice(range(embed_x.shape[0]), samples)

                # map to latent
                z = self.encoder([embed_x[emb_idx], embed_a[emb_idx]])
                # fit GMM
                gmm = GaussianMixture(
                    n_components=self.n_components,
                    covariance_type="diag",
                    reg_covar=1e-04,
                    **kwargs,
                ).fit(z)
                # get GMM parameters
                mu = gmm.means_
                sigma2 = gmm.covariances_

            # initialize mixture components
            self.grouper.get_layer("gaussian_mixture_latent").c_mu.assign(
                tf.convert_to_tensor(value=mu, dtype=tf.float32)
            )
            self.grouper.get_layer("gaussian_mixture_latent").log_c_sigma.assign(
                tf.math.log(
                    tf.math.sqrt(tf.convert_to_tensor(value=sigma2, dtype=tf.float32))
                )
            )
//  [python]
def __init__(self, n_components, train_loader, seed=random.seed("2019")):
        super(JointDistributionGenerator, self).__init__()
        self.seed = seed
        self.n_components = n_components
        trainset = list(train_loader.dataset)
        x_train = torch.stack([x[0] for x in trainset]).cpu().numpy()
        x_train = [x_train[i, :, j] for i in range(x_train.shape[0]) for j in range(x_train.shape[-1])]
        self.gmm = mixture.GMM(n_components=n_components, covariance_type="full")
        print("Fitting the distribution ...")
        self.gmm.fit(np.array(x_train))
        # print('Cluster means: ', self.gmm.means_)
        print("Loglike scores: ", self.gmm.score(np.array(x_train)))
//  [python]
def __init__(self, n_components, train_loader, seed=random.seed("2019")):
        super(JointDistributionGenerator, self).__init__()
        self.seed = seed
        self.n_components = n_components
        trainset = list(train_loader.dataset)
        x_train = torch.stack([x[0] for x in trainset]).cpu().numpy()
        x_train = [x_train[i, :, j] for i in range(x_train.shape[0]) for j in range(x_train.shape[-1])]
        self.gmm = GaussianMixture(n_components=n_components, covariance_type="full")
        print("Fitting the distribution ...")
        self.gmm.fit(np.array(x_train))
        # print('Cluster means: ', self.gmm.means_)
        print("Loglike scores: ", self.gmm.score(np.array(x_train)))
//  [python]
def predict(self, X: np.ndarray) -> np.ndarray:
        """
        Predicts the labels of the input data.

        Parameters
        ----------
        X : np.ndarray
            input data

        Returns
        -------
        predicted_labels : np.ndarray
            The predicted labels
        """
        device = detect_device()
        dataloader = get_dataloader(X, self.batch_size, False, False)
        embedded_data = encode_batchwise(dataloader, self.autoencoder, device)
        gmm = mixture.GaussianMixture(n_components=self.n_clusters, covariance_type='full')
        gmm.means_ = self.cluster_centers_
        gmm.covariances_ = self.covariances_
        gmm.weights_ = self.weights_
        gmm.precisions_cholesky_ = np.linalg.cholesky(np.linalg.inv(self.covariances_))
        predicted_labels = gmm.predict(embedded_data).astype(np.int32)
        return predicted_labels
//  [python]
def predict(self, X: np.ndarray) -> np.ndarray:
        """
        Predicts the labels of the input data.

        Parameters
        ----------
        X : np.ndarray
            input data

        Returns
        -------
        predicted_labels : np.ndarray
            The predicted labels
        """
        device = detect_device()
        dataloader = get_dataloader(X, self.batch_size, False, False)
        embedded_data = encode_batchwise(dataloader, self.autoencoder, device)
        gmm = GaussianMixture(n_components=self.n_clusters, covariance_type='full')
        gmm.means_ = self.cluster_centers_
        gmm.covariances_ = self.covariances_
        gmm.weights_ = self.weights_
        gmm.precisions_cholesky_ = np.linalg.cholesky(np.linalg.inv(self.covariances_))
        predicted_labels = gmm.predict(embedded_data).astype(np.int32)
        return predicted_labels
//  [python]
def fit(self, df, columns_config, unit=None, delta_unit='s'):
        """
        Fits the gausian mixture model for understanding threshold.

        :param df: DataFrame with columns responding for
                   Event Name, Event Timestamp and User ID
        :param columns_config: Dictionary that maps to required column names:
                               {'event_name_col': Event Name Column,
                               'event_timestamp_col' Event Timestamp Column,
                               'user_id_col': User ID Column}
        :param unit: type of string for pd.datetime parsing
        :param delta_unit: step in timestamp column (e.g. seconds from `01-01-1970`)
        :return: None
        """

        self.columns_config = columns_config
        self.unit = unit
        self.delta_unit = delta_unit
        df = self.add_time_from_prev_event(df.copy(), unit=self.unit, delta_unit=self.delta_unit)
        time_from_prev = np.log(df.loc[df.from_prev_event > 0, 'from_prev_event'].values)
        X = time_from_prev.reshape(-1, 1)
        if isinstance(self.n_components, int):
            self.model = mixture.GaussianMixture(n_components=self.n_components)
            self.model.fit(X)
            self.bics.append(self.model.bic(X))
        else:
            best_bic = np.infty
            best_model = None
            for n in self.n_components:
                model = mixture.GaussianMixture(n_components=n)
                model.fit(X)
                curr_bic = model.bic(X)
                self.bics.append(curr_bic)

                if curr_bic < best_bic:
                    best_model = model
                    best_bic = curr_bic

            self.model = best_model
//  [python]
def fit(self, df, columns_config, unit=None, delta_unit='s'):
        """
        Fits the gausian mixture model for understanding threshold.

        :param df: DataFrame with columns responding for
                   Event Name, Event Timestamp and User ID
        :param columns_config: Dictionary that maps to required column names:
                               {'event_name_col': Event Name Column,
                               'event_timestamp_col' Event Timestamp Column,
                               'user_id_col': User ID Column}
        :param unit: type of string for pd.datetime parsing
        :param delta_unit: step in timestamp column (e.g. seconds from `01-01-1970`)
        :return: None
        """

        self.columns_config = columns_config
        self.unit = unit
        self.delta_unit = delta_unit
        df = self.add_time_from_prev_event(df.copy(), unit=self.unit, delta_unit=self.delta_unit)
        time_from_prev = np.log(df.loc[df.from_prev_event > 0, 'from_prev_event'].values)
        X = time_from_prev.reshape(-1, 1)
        if isinstance(self.n_components, int):
            self.model = GaussianMixture(n_components=self.n_components)
            self.model.fit(X)
            self.bics.append(self.model.bic(X))
        else:
            best_bic = np.infty
            best_model = None
            for n in self.n_components:
                model = GaussianMixture(n_components=n)
                model.fit(X)
                curr_bic = model.bic(X)
                self.bics.append(curr_bic)

                if curr_bic < best_bic:
                    best_model = model
                    best_bic = curr_bic

            self.model = best_model
//  [python]
def makeModel(pipelined_data_frame):

    ef = ExtractFeature #object to extract the feature

    stacked_feature = np.asarray(())

    first_audio_location_in_frame = pipelined_data_frame["audio_path"].iloc[0] #first audio path in pandas frame
    stacked_feature = ef.extract_features(first_audio_location_in_frame) #first training audio's feature

    for index, row in pipelined_data_frame.iterrows(): #iterating through the pandas frame
        if index != 0: #escaping the first audio's feature
            ef = ExtractFeature  # object for extracting the MFCC feature
            currently_fetched_feature = ef.extract_features(row['audio_path']) # one feature extracted
            stacked_feature = np.vstack((stacked_feature, currently_fetched_feature)) #stacking the features



    model_save_path = "speakers_model/" #directory in which model is to saved
    model_name = pipelined_data_frame["target_speaker"].iloc[0]+".gmm"  # name of model represent's whose voice model??

    gmm = mixture.GaussianMixture(n_components=16, covariance_type='diag', max_iter=500, n_init=3, verbose=1)
    gmm.fit(stacked_feature) #generating the GMM model of the stacked features

    pickle.dump(gmm, open(model_save_path + model_name, 'wb'))
//  [python]
def makeModel(pipelined_data_frame):

    ef = ExtractFeature #object to extract the feature

    stacked_feature = np.asarray(())

    first_audio_location_in_frame = pipelined_data_frame["audio_path"].iloc[0] #first audio path in pandas frame
    stacked_feature = ef.extract_features(first_audio_location_in_frame) #first training audio's feature

    for index, row in pipelined_data_frame.iterrows(): #iterating through the pandas frame
        if index != 0: #escaping the first audio's feature
            ef = ExtractFeature  # object for extracting the MFCC feature
            currently_fetched_feature = ef.extract_features(row['audio_path']) # one feature extracted
            stacked_feature = np.vstack((stacked_feature, currently_fetched_feature)) #stacking the features



    model_save_path = "speakers_model/" #directory in which model is to saved
    model_name = pipelined_data_frame["target_speaker"].iloc[0]+".gmm"  # name of model represent's whose voice model??

    gmm = GaussianMixture(n_components=16, covariance_type='diag', max_iter=500, n_init=3, verbose=1)
    gmm.fit(stacked_feature) #generating the GMM model of the stacked features

    pickle.dump(gmm, open(model_save_path + model_name, 'wb'))